]> Dogcows Code - chaz/tint2/blob - src/tint.c
fixed issue 90 and issue 81 : window manager s menu in panel padding
[chaz/tint2] / src / tint.c
1 /**************************************************************************
2 *
3 * Tint2 panel
4 *
5 * Copyright (C) 2007 Pål Staurland (staura@gmail.com)
6 * Modified (C) 2008 thierry lorthiois (lorthiois@bbsoft.fr)
7 *
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License version 2
10 * as published by the Free Software Foundation.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 **************************************************************************/
20
21 #include <sys/stat.h>
22 #include <sys/time.h>
23 #include <unistd.h>
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <X11/Xutil.h>
28 #include <X11/Xatom.h>
29 #include <X11/Xlocale.h>
30 #include <Imlib2.h>
31 #include <signal.h>
32
33 #include "server.h"
34 #include "window.h"
35 #include "config.h"
36 #include "task.h"
37 #include "taskbar.h"
38 #include "systraybar.h"
39 #include "panel.h"
40
41
42 void signal_handler(int sig)
43 {
44 // signal handler is light as it should be
45 signal_pending = sig;
46 }
47
48
49 void init ()
50 {
51 // Set signal handler
52 signal(SIGUSR1, signal_handler);
53 signal(SIGINT, signal_handler);
54 signal(SIGTERM, signal_handler);
55
56 // set global data
57 memset(&server, 0, sizeof(Server_global));
58 memset(&systray, 0, sizeof(Systraybar));
59
60 server.dsp = XOpenDisplay (NULL);
61 if (!server.dsp) {
62 fprintf(stderr, "tint2 exit : could not open display.\n");
63 exit(0);
64 }
65 server_init_atoms ();
66 server.screen = DefaultScreen (server.dsp);
67 server.root_win = RootWindow(server.dsp, server.screen);
68 server.depth = DefaultDepth (server.dsp, server.screen);
69 server.visual = DefaultVisual (server.dsp, server.screen);
70 server.desktop = server_get_current_desktop ();
71 XGCValues gcv;
72 server.gc = XCreateGC (server.dsp, server.root_win, (unsigned long)0, &gcv) ;
73
74 XSetErrorHandler ((XErrorHandler) server_catch_error);
75
76 imlib_context_set_display (server.dsp);
77 imlib_context_set_visual (server.visual);
78 imlib_context_set_colormap (DefaultColormap (server.dsp, server.screen));
79
80 /* Catch events */
81 XSelectInput (server.dsp, server.root_win, PropertyChangeMask|StructureNotifyMask);
82
83 setlocale (LC_ALL, "");
84 }
85
86
87 void cleanup()
88 {
89 cleanup_panel();
90
91 if (time1_font_desc) pango_font_description_free(time1_font_desc);
92 if (time2_font_desc) pango_font_description_free(time2_font_desc);
93 if (time1_format) g_free(time1_format);
94 if (time2_format) g_free(time2_format);
95 if (bat1_font_desc) pango_font_description_free(bat1_font_desc);
96 if (bat2_font_desc) pango_font_description_free(bat2_font_desc);
97 if (battery_low_cmd) g_free(battery_low_cmd);
98 if (path_energy_now) g_free(path_energy_now);
99 if (path_energy_full) g_free(path_energy_full);
100 if (path_current_now) g_free(path_current_now);
101 if (path_status) g_free(path_status);
102 if (clock_lclick_command) g_free(clock_lclick_command);
103 if (clock_rclick_command) g_free(clock_rclick_command);
104
105 if (server.monitor) free(server.monitor);
106 XFreeGC(server.dsp, server.gc);
107 XCloseDisplay(server.dsp);
108 }
109
110
111 void window_action (Task *tsk, int action)
112 {
113 switch (action) {
114 case CLOSE:
115 set_close (tsk->win);
116 break;
117 case TOGGLE:
118 set_active(tsk->win);
119 break;
120 case ICONIFY:
121 XIconifyWindow (server.dsp, tsk->win, server.screen);
122 break;
123 case TOGGLE_ICONIFY:
124 if (tsk == task_active) XIconifyWindow (server.dsp, tsk->win, server.screen);
125 else set_active (tsk->win);
126 break;
127 case SHADE:
128 window_toggle_shade (tsk->win);
129 break;
130 }
131 }
132
133
134 void event_button_press (XEvent *e)
135 {
136 Panel *panel = get_panel(e->xany.window);
137 if (!panel) return;
138
139 if ((e->xbutton.x < panel->area.paddingxlr) || (e->xbutton.x > panel->area.width-panel->area.paddingxlr) || (e->xbutton.y < panel->area.paddingy) || (e->xbutton.y > panel->area.paddingy+panel->g_taskbar.height)) {
140 // forward the click to the desktop window (thanks conky)
141 XUngrabPointer(server.dsp, e->xbutton.time);
142 e->xbutton.window = server.root_win;
143 XSetInputFocus(server.dsp, e->xbutton.window, RevertToParent, e->xbutton.time);
144 XSendEvent(server.dsp, e->xbutton.window, False, ButtonPressMask, e);
145 return;
146 }
147
148 if (e->xbutton.button != 1) return;
149
150 if (panel_mode != MULTI_DESKTOP) {
151 // drag and drop disabled
152 XLowerWindow (server.dsp, panel->main_win);
153 return;
154 }
155
156 GSList *l0;
157 Taskbar *tskbar;
158 int x = e->xbutton.x;
159 for (l0 = panel->area.list; l0 ; l0 = l0->next) {
160 tskbar = l0->data;
161 if (!tskbar->area.on_screen) continue;
162 if (x >= tskbar->area.posx && x <= (tskbar->area.posx + tskbar->area.width))
163 break;
164 }
165
166 if (l0) {
167 Task *tsk;
168 for (l0 = tskbar->area.list; l0 ; l0 = l0->next) {
169 tsk = l0->data;
170 if (x >= tsk->area.posx && x <= (tsk->area.posx + tsk->area.width)) {
171 task_drag = tsk;
172 break;
173 }
174 }
175 }
176
177 XLowerWindow (server.dsp, panel->main_win);
178 }
179
180
181 void event_button_release (XEvent *e)
182 {
183 Panel *panel = get_panel(e->xany.window);
184 if (!panel) return;
185
186 if ((e->xbutton.x < panel->area.paddingxlr) || (e->xbutton.x > panel->area.width-panel->area.paddingxlr) || (e->xbutton.y < panel->area.paddingy) || (e->xbutton.y > panel->area.paddingy+panel->g_taskbar.height)) {
187 // forward the click to the desktop window (thanks conky)
188 e->xbutton.window = server.root_win;
189 XSendEvent(server.dsp, e->xbutton.window, False, ButtonReleaseMask, e);
190 return;
191 }
192
193 int action = TOGGLE_ICONIFY;
194 int x = e->xbutton.x;
195 //int y = e->xbutton.y; // unused
196 switch (e->xbutton.button) {
197 case 2:
198 action = mouse_middle;
199 break;
200 case 3:
201 action = mouse_right;
202 break;
203 case 4:
204 action = mouse_scroll_up;
205 break;
206 case 5:
207 action = mouse_scroll_down;
208 break;
209 }
210
211 // search taskbar
212 Taskbar *tskbar;
213 GSList *l0;
214 Clock clk = panel->clock;
215 if (clk.area.on_screen && x >= clk.area.posx && x <= (clk.area.posx + clk.area.width))
216 clock_action(e->xbutton.button);
217 else {
218 for (l0 = panel->area.list; l0 ; l0 = l0->next) {
219 tskbar = l0->data;
220 if (!tskbar->area.on_screen) continue;
221 if (x >= tskbar->area.posx && x <= (tskbar->area.posx + tskbar->area.width))
222 goto suite;
223 }
224 }
225
226 // TODO: check better solution to keep window below
227 XLowerWindow (server.dsp, panel->main_win);
228 task_drag = 0;
229 return;
230
231 suite:
232 // drag and drop task
233 if (task_drag) {
234 if (tskbar != task_drag->area.parent && action == TOGGLE_ICONIFY) {
235 if (task_drag->desktop != ALLDESKTOP && panel_mode == MULTI_DESKTOP) {
236 windows_set_desktop(task_drag->win, tskbar->desktop);
237 if (tskbar->desktop == server.desktop)
238 set_active(task_drag->win);
239 task_drag = 0;
240 }
241 return;
242 }
243 else task_drag = 0;
244 }
245
246 // switch desktop
247 if (panel_mode == MULTI_DESKTOP)
248 if (tskbar->desktop != server.desktop && action != CLOSE)
249 set_desktop (tskbar->desktop);
250
251 // action on task
252 Task *tsk;
253 GSList *l;
254 for (l = tskbar->area.list ; l ; l = l->next) {
255 tsk = l->data;
256 if (x >= tsk->area.posx && x <= (tsk->area.posx + tsk->area.width)) {
257 window_action (tsk, action);
258 break;
259 }
260 }
261
262 // to keep window below
263 XLowerWindow (server.dsp, panel->main_win);
264 }
265
266
267 void event_property_notify (XEvent *e)
268 {
269 int i, j;
270 Task *tsk;
271 Window win = e->xproperty.window;
272 Atom at = e->xproperty.atom;
273
274 if (win == server.root_win) {
275 if (!server.got_root_win) {
276 XSelectInput (server.dsp, server.root_win, PropertyChangeMask|StructureNotifyMask);
277 server.got_root_win = 1;
278 }
279
280 // Change number of desktops
281 else if (at == server.atom._NET_NUMBER_OF_DESKTOPS) {
282 server.nb_desktop = server_get_number_of_desktop ();
283 cleanup_taskbar();
284 init_taskbar();
285 visible_object();
286 for (i=0 ; i < nb_panel ; i++) {
287 panel1[i].area.resize = 1;
288 }
289 task_refresh_tasklist();
290 panel_refresh = 1;
291 }
292 // Change desktop
293 else if (at == server.atom._NET_CURRENT_DESKTOP) {
294 server.desktop = server_get_current_desktop ();
295 if (panel_mode != MULTI_DESKTOP) {
296 visible_object();
297 }
298 }
299 // Window list
300 else if (at == server.atom._NET_CLIENT_LIST) {
301 task_refresh_tasklist();
302 panel_refresh = 1;
303 }
304 // Change active
305 else if (at == server.atom._NET_ACTIVE_WINDOW) {
306 GSList *l0;
307 if (task_active) {
308 for (i=0 ; i < nb_panel ; i++) {
309 for (j=0 ; j < panel1[i].nb_desktop ; j++) {
310 for (l0 = panel1[i].taskbar[j].area.list; l0 ; l0 = l0->next) {
311 tsk = l0->data;
312 tsk->area.is_active = 0;
313 }
314 }
315 }
316 task_active = 0;
317 }
318 Window w1 = window_get_active ();
319 Task *t = task_get_task(w1);
320 if (!t) {
321 Window w2;
322 if (XGetTransientForHint(server.dsp, w1, &w2) != 0)
323 if (w2) t = task_get_task(w2);
324 }
325 if (task_urgent == t) {
326 init_precision();
327 task_urgent = 0;
328 }
329 if (t) {
330 for (i=0 ; i < nb_panel ; i++) {
331 for (j=0 ; j < panel1[i].nb_desktop ; j++) {
332 for (l0 = panel1[i].taskbar[j].area.list; l0 ; l0 = l0->next) {
333 tsk = l0->data;
334 if (tsk->win == t->win) {
335 tsk->area.is_active = 1;
336 //printf("active monitor %d, task %s\n", panel1[i].monitor, tsk->title);
337 }
338 }
339 }
340 }
341 task_active = t;
342 }
343 panel_refresh = 1;
344 }
345 // Wallpaper changed
346 else if (at == server.atom._XROOTPMAP_ID) {
347 for (i=0 ; i < nb_panel ; i++) {
348 set_panel_background(&panel1[i]);
349 }
350 panel_refresh = 1;
351 }
352 }
353 else {
354 tsk = task_get_task (win);
355 if (!tsk) return;
356 //printf("atom root_win = %s, %s\n", XGetAtomName(server.dsp, at), tsk->title);
357
358 // Window title changed
359 if (at == server.atom._NET_WM_VISIBLE_NAME || at == server.atom._NET_WM_NAME || at == server.atom.WM_NAME) {
360 Task *tsk2;
361 GSList *l0;
362 get_title(tsk);
363 // changed other tsk->title
364 for (i=0 ; i < nb_panel ; i++) {
365 for (j=0 ; j < panel1[i].nb_desktop ; j++) {
366 for (l0 = panel1[i].taskbar[j].area.list; l0 ; l0 = l0->next) {
367 tsk2 = l0->data;
368 if (tsk->win == tsk2->win && tsk != tsk2) {
369 tsk2->title = tsk->title;
370 tsk2->area.redraw = 1;
371 }
372 }
373 }
374 }
375 panel_refresh = 1;
376 }
377 // Demand attention
378 else if (at == server.atom._NET_WM_STATE) {
379 if (window_is_urgent (win)) {
380 task_urgent = tsk;
381 time_precision = 1;
382 }
383 }
384 else if (at == server.atom.WM_STATE) {
385 // Iconic state
386 // TODO : try to delete following code
387 if (window_is_iconified (win)) {
388 if (task_active) {
389 if (task_active->win == tsk->win) {
390 Task *tsk2;
391 GSList *l0;
392 for (i=0 ; i < nb_panel ; i++) {
393 for (j=0 ; j < panel1[i].nb_desktop ; j++) {
394 for (l0 = panel1[i].taskbar[j].area.list; l0 ; l0 = l0->next) {
395 tsk2 = l0->data;
396 tsk2->area.is_active = 0;
397 }
398 }
399 }
400 task_active = 0;
401 }
402 }
403 }
404 }
405 // Window icon changed
406 else if (at == server.atom._NET_WM_ICON) {
407 get_icon(tsk);
408 Task *tsk2;
409 GSList *l0;
410 for (i=0 ; i < nb_panel ; i++) {
411 for (j=0 ; j < panel1[i].nb_desktop ; j++) {
412 for (l0 = panel1[i].taskbar[j].area.list; l0 ; l0 = l0->next) {
413 tsk2 = l0->data;
414 if (tsk->win == tsk2->win && tsk != tsk2) {
415 tsk2->icon_width = tsk->icon_width;
416 tsk2->icon_height = tsk->icon_height;
417 tsk2->icon_data = tsk->icon_data;
418 tsk2->area.redraw = 1;
419 }
420 }
421 }
422 }
423 panel_refresh = 1;
424 }
425 // Window desktop changed
426 else if (at == server.atom._NET_WM_DESKTOP) {
427 remove_task (tsk);
428 add_task (win);
429 panel_refresh = 1;
430 }
431
432 if (!server.got_root_win) server.root_win = RootWindow (server.dsp, server.screen);
433 }
434 }
435
436
437 void event_expose (XEvent *e)
438 {
439 Panel *panel;
440
441 panel = get_panel(e->xany.window);
442 if (!panel) return;
443 /*
444 if (systray.area.on_screen) {
445 // force trayer refresh
446 //XClearWindow(tray_data.dpy, ti->mid_parent);
447 //x11_send_visibility(tray_data.dpy, dst, VisibilityFullyObscured);
448 //x11_send_visibility(tray_data.dpy, dst, VisibilityUnobscured);
449
450 GSList *l;
451 TrayWindow *traywin;
452 for (l = systray.list_icons; l ; l = l->next) {
453 traywin = (TrayWindow*)l->data;
454 // send Expose event
455 XClearArea(server.dsp, traywin->id, 0, 0, systray.area.width, systray.area.height, True);
456 //printf("expose %lx\n", traywin->id);
457 }
458
459 //x11_refresh_window(tray_data.dpy, ti->wid, ti->l.wnd_sz.x, ti->l.wnd_sz.y, True);
460 }
461 */
462 panel_refresh = 1;
463 //XCopyArea (server.dsp, panel->temp_pmap, panel->main_win, server.gc, 0, 0, panel->area.width, panel->area.height, 0, 0);
464
465 }
466
467
468 void event_configure_notify (Window win)
469 {
470 if (panel_mode != SINGLE_MONITOR) return;
471 if (server.nb_monitor == 1) return;
472
473 Task *tsk = task_get_task (win);
474 if (!tsk) return;
475
476 Panel *p = tsk->area.panel;
477 if (p->monitor != window_get_monitor (win)) {
478 // task on another monitor
479 remove_task (tsk);
480 add_task (win);
481 if (win == window_get_active ()) {
482 Task *tsk = task_get_task (win);
483 tsk->area.is_active = 1;
484 task_active = tsk;
485 }
486 panel_refresh = 1;
487 }
488 }
489
490
491 void event_timer()
492 {
493 struct timeval stv;
494 int i;
495
496 if (gettimeofday(&stv, 0)) return;
497
498 if (abs(stv.tv_sec - time_clock.tv_sec) < time_precision) return;
499
500 // urgent task
501 if (task_urgent) {
502 task_urgent->area.is_active = !task_urgent->area.is_active;
503 task_urgent->area.redraw = 1;
504 }
505
506 // update battery
507 if (panel1[0].battery.area.on_screen) {
508 update_battery(&battery_state);
509 for (i=0 ; i < nb_panel ; i++)
510 panel1[i].battery.area.resize = 1;
511 }
512
513 // update clock
514 if (time1_format) {
515 time_clock.tv_sec = stv.tv_sec;
516 time_clock.tv_sec -= time_clock.tv_sec % time_precision;
517 for (i=0 ; i < nb_panel ; i++)
518 panel1[i].clock.area.resize = 1;
519 }
520 panel_refresh = 1;
521 }
522
523
524 int main (int argc, char *argv[])
525 {
526 XEvent e;
527 fd_set fd;
528 int x11_fd, i, c;
529 struct timeval tv;
530 Panel *panel;
531 GSList *it;
532
533 c = getopt (argc, argv, "c:");
534 init ();
535
536 load_config:
537 i = 0;
538 init_config();
539 if (c != -1)
540 i = config_read_file (optarg);
541 if (!i)
542 i = config_read ();
543 if (!i) {
544 fprintf(stderr, "usage: tint2 [-c] <config_file>\n");
545 cleanup();
546 exit(1);
547 }
548 config_finish();
549
550 x11_fd = ConnectionNumber(server.dsp);
551 XSync(server.dsp, False);
552
553 while (1) {
554 // thanks to AngryLlama for the timer
555 // Create a File Description Set containing x11_fd
556 FD_ZERO (&fd);
557 FD_SET (x11_fd, &fd);
558
559 tv.tv_usec = 500000;
560 tv.tv_sec = 0;
561
562 // Wait for X Event or a Timer
563 if (select(x11_fd+1, &fd, 0, 0, &tv)) {
564 while (XPending (server.dsp)) {
565 XNextEvent(server.dsp, &e);
566
567 switch (e.type) {
568 case ButtonPress:
569 event_button_press (&e);
570 break;
571
572 case ButtonRelease:
573 event_button_release(&e);
574 break;
575
576 case Expose:
577 event_expose(&e);
578 break;
579
580 case PropertyNotify:
581 event_property_notify(&e);
582 break;
583
584 case ConfigureNotify:
585 if (e.xconfigure.window == server.root_win)
586 goto load_config;
587 else
588 event_configure_notify (e.xconfigure.window);
589 break;
590
591 case UnmapNotify:
592 case DestroyNotify:
593 if (!systray.area.on_screen) break;
594 for (it = systray.list_icons; it; it = g_slist_next(it)) {
595 if (((TrayWindow*)it->data)->id == e.xany.window) {
596 remove_icon((TrayWindow*)it->data);
597 break;
598 }
599 }
600 break;
601
602 case ClientMessage:
603 if (!systray.area.on_screen) break;
604 //printf("ClientMessage\n");
605 if (e.xclient.message_type == server.atom._NET_SYSTEM_TRAY_OPCODE && e.xclient.format == 32 && e.xclient.window == net_sel_win) {
606 net_message(&e.xclient);
607 }
608 break;
609 }
610 }
611 }
612 else event_timer();
613
614 switch (signal_pending) {
615 case SIGUSR1:
616 goto load_config;
617 case SIGINT:
618 case SIGTERM:
619 cleanup ();
620 return 0;
621 }
622
623 if (panel_refresh) {
624 panel_refresh = 0;
625
626 for (i=0 ; i < nb_panel ; i++) {
627 panel = &panel1[i];
628
629 if (panel->temp_pmap) XFreePixmap(server.dsp, panel->temp_pmap);
630 panel->temp_pmap = XCreatePixmap(server.dsp, server.root_win, panel->area.width, panel->area.height, server.depth);
631
632 refresh(&panel->area);
633 XCopyArea(server.dsp, panel->temp_pmap, panel->main_win, server.gc, 0, 0, panel->area.width, panel->area.height, 0, 0);
634 }
635 XFlush (server.dsp);
636 }
637 }
638 }
639
640 // ****************************************************
641 // main_win doesn't include panel.area.paddingx, so we have WM capabilities on left and right.
642 // this feature is disabled !
643 //XCopyArea (server.dsp, server.pmap, p->main_win, server.gc, p->area.paddingxlr, 0, p->area.width-(2*p->area.paddingxlr), p->area.height, 0, 0);
644 //XCopyArea (server.dsp, panel.area.pix.pmap, server.root_win, server.gc_root, 0, 0, panel.area.width, panel.area.height, server.posx, server.posy);
645 //XCopyArea (server.dsp, server.pmap, panel.main_win, server.gc, panel.area.paddingxlr, 0, panel.area.width-(2*panel.area.paddingxlr), panel.area.height, 0, 0);
646
This page took 0.074755 seconds and 5 git commands to generate.