]> Dogcows Code - chaz/tint2/blob - src/tint.c
merge change described in issue 122 (by jackp)
[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 signal(SIGHUP, signal_handler);
56 signal(SIGCHLD, SIG_IGN); // don't have to wait() after fork()
57
58 // set global data
59 memset(&server, 0, sizeof(Server_global));
60 memset(&systray, 0, sizeof(Systraybar));
61
62 server.dsp = XOpenDisplay (NULL);
63 if (!server.dsp) {
64 fprintf(stderr, "tint2 exit : could not open display.\n");
65 exit(0);
66 }
67 server_init_atoms ();
68 server.screen = DefaultScreen (server.dsp);
69 server.root_win = RootWindow(server.dsp, server.screen);
70 server.depth = DefaultDepth (server.dsp, server.screen);
71 server.visual = DefaultVisual (server.dsp, server.screen);
72 server.desktop = server_get_current_desktop ();
73 XGCValues gcv;
74 server.gc = XCreateGC (server.dsp, server.root_win, (unsigned long)0, &gcv) ;
75
76 XSetErrorHandler ((XErrorHandler) server_catch_error);
77
78 imlib_context_set_display (server.dsp);
79 imlib_context_set_visual (server.visual);
80 imlib_context_set_colormap (DefaultColormap (server.dsp, server.screen));
81
82 /* Catch events */
83 XSelectInput (server.dsp, server.root_win, PropertyChangeMask|StructureNotifyMask);
84
85 setlocale (LC_ALL, "");
86 }
87
88
89 void cleanup()
90 {
91 cleanup_panel();
92
93 if (time1_font_desc) pango_font_description_free(time1_font_desc);
94 if (time2_font_desc) pango_font_description_free(time2_font_desc);
95 if (time1_format) g_free(time1_format);
96 if (time2_format) g_free(time2_format);
97 #ifdef ENABLE_BATTERY
98 if (bat1_font_desc) pango_font_description_free(bat1_font_desc);
99 if (bat2_font_desc) pango_font_description_free(bat2_font_desc);
100 if (battery_low_cmd) g_free(battery_low_cmd);
101 if (path_energy_now) g_free(path_energy_now);
102 if (path_energy_full) g_free(path_energy_full);
103 if (path_current_now) g_free(path_current_now);
104 if (path_status) g_free(path_status);
105 #endif
106 if (clock_lclick_command) g_free(clock_lclick_command);
107 if (clock_rclick_command) g_free(clock_rclick_command);
108
109 if (server.monitor) free(server.monitor);
110 XFreeGC(server.dsp, server.gc);
111 XCloseDisplay(server.dsp);
112 }
113
114
115 Taskbar *click_taskbar (Panel *panel, XEvent *e)
116 {
117 GSList *l0;
118 Taskbar *tskbar = NULL;
119 if (panel_horizontal) {
120 int x = e->xbutton.x;
121 for (l0 = panel->area.list; l0 ; l0 = l0->next) {
122 tskbar = l0->data;
123 if (!tskbar->area.on_screen) continue;
124 if (x >= tskbar->area.posx && x <= (tskbar->area.posx + tskbar->area.width))
125 break;
126 }
127 }
128 else {
129 int y = e->xbutton.y;
130 for (l0 = panel->area.list; l0 ; l0 = l0->next) {
131 tskbar = l0->data;
132 if (!tskbar->area.on_screen) continue;
133 if (y >= tskbar->area.posy && y <= (tskbar->area.posy + tskbar->area.height))
134 break;
135 }
136 }
137 return tskbar;
138 }
139
140
141 Task *click_task (Panel *panel, XEvent *e)
142 {
143 GSList *l0;
144 Taskbar *tskbar;
145
146 if ( (tskbar = click_taskbar(panel, e)) ) {
147 if (panel_horizontal) {
148 int x = e->xbutton.x;
149 Task *tsk;
150 for (l0 = tskbar->area.list; l0 ; l0 = l0->next) {
151 tsk = l0->data;
152 if (x >= tsk->area.posx && x <= (tsk->area.posx + tsk->area.width)) {
153 return tsk;
154 }
155 }
156 }
157 else {
158 int y = e->xbutton.y;
159 Task *tsk;
160 for (l0 = tskbar->area.list; l0 ; l0 = l0->next) {
161 tsk = l0->data;
162 if (y >= tsk->area.posy && y <= (tsk->area.posy + tsk->area.height)) {
163 return tsk;
164 }
165 }
166 }
167 }
168 return NULL;
169 }
170
171
172 int click_padding(Panel *panel, XEvent *e)
173 {
174 if (panel_horizontal) {
175 if (e->xbutton.x < panel->area.paddingxlr || e->xbutton.x > panel->area.width-panel->area.paddingxlr)
176 return 1;
177 }
178 else {
179 if (e->xbutton.y < panel->area.paddingxlr || e->xbutton.y > panel->area.height-panel->area.paddingxlr)
180 return 1;
181 }
182 return 0;
183 }
184
185
186 int click_clock(Panel *panel, XEvent *e)
187 {
188 Clock clk = panel->clock;
189 if (panel_horizontal) {
190 if (clk.area.on_screen && e->xbutton.x >= clk.area.posx && e->xbutton.x <= (clk.area.posx + clk.area.width))
191 return TRUE;
192 } else {
193 if (clk.area.on_screen && e->xbutton.y >= clk.area.posy && e->xbutton.y <= (clk.area.posy + clk.area.height))
194 return TRUE;
195 }
196 return FALSE;
197 }
198
199
200 void window_action (Task *tsk, int action)
201 {
202 if (!tsk) return;
203 switch (action) {
204 case CLOSE:
205 set_close (tsk->win);
206 break;
207 case TOGGLE:
208 set_active(tsk->win);
209 break;
210 case ICONIFY:
211 XIconifyWindow (server.dsp, tsk->win, server.screen);
212 break;
213 case TOGGLE_ICONIFY:
214 if (tsk == task_active) XIconifyWindow (server.dsp, tsk->win, server.screen);
215 else set_active (tsk->win);
216 break;
217 case SHADE:
218 window_toggle_shade (tsk->win);
219 break;
220 case MAXIMIZE_RESTORE:
221 window_maximize_restore (tsk->win);
222 break;
223 case MAXIMIZE:
224 window_maximize_restore (tsk->win);
225 break;
226 case RESTORE:
227 window_maximize_restore (tsk->win);
228 break;
229 }
230 }
231
232
233 void event_button_press (XEvent *e)
234 {
235 Panel *panel = get_panel(e->xany.window);
236 if (!panel) return;
237 /*
238 if (wm_menu) {
239 if ((panel_horizontal && (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)) || (!panel_horizontal && (e->xbutton.y < panel->area.paddingxlr || e->xbutton.y > panel->area.height-panel->area.paddingxlr || e->xbutton.x < panel->area.paddingy || e->xbutton.x > panel->area.paddingy+panel->g_taskbar.width))) {
240 // forward the click to the desktop window (thanks conky)
241 XUngrabPointer(server.dsp, e->xbutton.time);
242 e->xbutton.window = server.root_win;
243 // icewm doesn't open under the mouse.
244 // and xfce doesn't open at all.
245 //e->xbutton.x = e->xbutton.x_root;
246 //e->xbutton.y = e->xbutton.y_root;
247 //printf("**** %d, %d\n", e->xbutton.x, e->xbutton.y);
248 XSetInputFocus(server.dsp, e->xbutton.window, RevertToParent, e->xbutton.time);
249 XSendEvent(server.dsp, e->xbutton.window, False, ButtonPressMask, e);
250 return;
251 }
252 }
253 */
254 if (panel_mode == MULTI_DESKTOP)
255 task_drag = click_task(panel, e);
256
257 if (wm_menu && !task_drag && !click_clock(panel, e) && (e->xbutton.button != 1) ) {
258 // forward the click to the desktop window (thanks conky)
259 XUngrabPointer(server.dsp, e->xbutton.time);
260 e->xbutton.window = server.root_win;
261 // icewm doesn't open under the mouse.
262 // and xfce doesn't open at all.
263 //e->xbutton.x = e->xbutton.x_root;
264 //e->xbutton.y = e->xbutton.y_root;
265 //printf("**** %d, %d\n", e->xbutton.x, e->xbutton.y);
266 XSetInputFocus(server.dsp, e->xbutton.window, RevertToParent, e->xbutton.time);
267 XSendEvent(server.dsp, e->xbutton.window, False, ButtonPressMask, e);
268 return;
269 }
270 /*
271 if (e->xbutton.button != 1) return;
272 GSList *l0;
273 Taskbar *tskbar;
274 if (panel_horizontal) {
275 int x = e->xbutton.x;
276 for (l0 = panel->area.list; l0 ; l0 = l0->next) {
277 tskbar = l0->data;
278 if (!tskbar->area.on_screen) continue;
279 if (x >= tskbar->area.posx && x <= (tskbar->area.posx + tskbar->area.width))
280 break;
281 }
282 if (l0) {
283 Task *tsk;
284 for (l0 = tskbar->area.list; l0 ; l0 = l0->next) {
285 tsk = l0->data;
286 if (x >= tsk->area.posx && x <= (tsk->area.posx + tsk->area.width)) {
287 task_drag = tsk;
288 break;
289 }
290 }
291 }
292 }
293 else {
294 int y = e->xbutton.y;
295 for (l0 = panel->area.list; l0 ; l0 = l0->next) {
296 tskbar = l0->data;
297 if (!tskbar->area.on_screen) continue;
298 if (y >= tskbar->area.posy && y <= (tskbar->area.posy + tskbar->area.height))
299 break;
300 }
301 if (l0) {
302 Task *tsk;
303 for (l0 = tskbar->area.list; l0 ; l0 = l0->next) {
304 tsk = l0->data;
305 if (y >= tsk->area.posy && y <= (tsk->area.posy + tsk->area.height)) {
306 task_drag = tsk;
307 break;
308 }
309 }
310 }
311 }
312 */
313 XLowerWindow (server.dsp, panel->main_win);
314 }
315
316
317 void event_button_release (XEvent *e)
318 {
319 Panel *panel = get_panel(e->xany.window);
320 if (!panel) return;
321
322 if (wm_menu && click_padding(panel, e)) {
323 // forward the click to the desktop window (thanks conky)
324 e->xbutton.window = server.root_win;
325 XSendEvent(server.dsp, e->xbutton.window, False, ButtonReleaseMask, e);
326 return;
327 }
328 /*
329 if (wm_menu) {
330 if ((panel_horizontal && (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)) || (!panel_horizontal && (e->xbutton.y < panel->area.paddingxlr || e->xbutton.y > panel->area.height-panel->area.paddingxlr || e->xbutton.x < panel->area.paddingy || e->xbutton.x > panel->area.paddingy+panel->g_taskbar.width))) {
331 // forward the click to the desktop window (thanks conky)
332 e->xbutton.window = server.root_win;
333 XSendEvent(server.dsp, e->xbutton.window, False, ButtonReleaseMask, e);
334 return;
335 }
336 }
337 */
338
339 int action = TOGGLE_ICONIFY;
340 switch (e->xbutton.button) {
341 case 2:
342 action = mouse_middle;
343 break;
344 case 3:
345 action = mouse_right;
346 break;
347 case 4:
348 action = mouse_scroll_up;
349 break;
350 case 5:
351 action = mouse_scroll_down;
352 break;
353 case 6:
354 action = mouse_tilt_left;
355 break;
356 case 7:
357 action = mouse_tilt_right;
358 break;
359 }
360
361 if ( click_clock(panel, e)) {
362 clock_action(e->xbutton.button);
363 XLowerWindow (server.dsp, panel->main_win);
364 task_drag = 0;
365 return;
366 }
367
368 Taskbar *tskbar;
369 if ( !(tskbar = click_taskbar(panel, e)) ) {
370 // TODO: check better solution to keep window below
371 XLowerWindow (server.dsp, panel->main_win);
372 task_drag = 0;
373 return;
374 }
375
376 // drag and drop task
377 if (task_drag) {
378 if (tskbar != task_drag->area.parent && action == TOGGLE_ICONIFY) {
379 if (task_drag->desktop != ALLDESKTOP && panel_mode == MULTI_DESKTOP) {
380 windows_set_desktop(task_drag->win, tskbar->desktop);
381 if (tskbar->desktop == server.desktop)
382 set_active(task_drag->win);
383 task_drag = 0;
384 }
385 return;
386 }
387 else task_drag = 0;
388 }
389
390 // switch desktop
391 if (panel_mode == MULTI_DESKTOP && action != mouse_tilt_left && action != mouse_tilt_right) {
392 if (tskbar->desktop != server.desktop && action != CLOSE)
393 set_desktop (tskbar->desktop);
394 }
395
396 // action on task
397 window_action( click_task(panel, e), action);
398
399 // to keep window below
400 XLowerWindow (server.dsp, panel->main_win);
401 /*
402 int x = e->xbutton.x;
403 int y = e->xbutton.y;
404 // search taskbar
405 Taskbar *tskbar;
406 GSList *l0;
407 Clock clk = panel->clock;
408 if (panel_horizontal) {
409 if (clk.area.on_screen && x >= clk.area.posx && x <= (clk.area.posx + clk.area.width))
410 clock_action(e->xbutton.button);
411 else {
412 for (l0 = panel->area.list; l0 ; l0 = l0->next) {
413 tskbar = l0->data;
414 if (!tskbar->area.on_screen) continue;
415 if (x >= tskbar->area.posx && x <= (tskbar->area.posx + tskbar->area.width))
416 goto suite;
417 }
418 }
419 }
420 else {
421 if (clk.area.on_screen && y >= clk.area.posy && y <= (clk.area.posy + clk.area.height))
422 clock_action(e->xbutton.button);
423 else {
424 for (l0 = panel->area.list; l0 ; l0 = l0->next) {
425 tskbar = l0->data;
426 if (!tskbar->area.on_screen) continue;
427 if (y >= tskbar->area.posy && y <= (tskbar->area.posy + tskbar->area.height))
428 goto suite;
429 }
430 }
431 }
432
433 // TODO: check better solution to keep window below
434 XLowerWindow (server.dsp, panel->main_win);
435 task_drag = 0;
436 return;
437
438 suite:
439 // drag and drop task
440 if (task_drag) {
441 if (tskbar != task_drag->area.parent && action == TOGGLE_ICONIFY) {
442 if (task_drag->desktop != ALLDESKTOP && panel_mode == MULTI_DESKTOP) {
443 windows_set_desktop(task_drag->win, tskbar->desktop);
444 if (tskbar->desktop == server.desktop)
445 set_active(task_drag->win);
446 task_drag = 0;
447 }
448 return;
449 }
450 else task_drag = 0;
451 }
452
453 // switch desktop
454 if (panel_mode == MULTI_DESKTOP) {
455 if (tskbar->desktop != server.desktop && action != CLOSE)
456 set_desktop (tskbar->desktop);
457 }
458
459 // action on task
460 Task *tsk;
461 GSList *l;
462 for (l = tskbar->area.list ; l ; l = l->next) {
463 tsk = l->data;
464 if (panel_horizontal) {
465 if (x >= tsk->area.posx && x <= (tsk->area.posx + tsk->area.width)) {
466 window_action (tsk, action);
467 break;
468 }
469 }
470 else {
471 if (y >= tsk->area.posy && y <= (tsk->area.posy + tsk->area.height)) {
472 window_action (tsk, action);
473 break;
474 }
475 }
476 }
477
478 // to keep window below
479 XLowerWindow (server.dsp, panel->main_win);
480 */
481 }
482
483
484 void event_property_notify (XEvent *e)
485 {
486 int i, j;
487 Task *tsk;
488 Window win = e->xproperty.window;
489 Atom at = e->xproperty.atom;
490
491 if (win == server.root_win) {
492 if (!server.got_root_win) {
493 XSelectInput (server.dsp, server.root_win, PropertyChangeMask|StructureNotifyMask);
494 server.got_root_win = 1;
495 }
496
497 // Change number of desktops
498 else if (at == server.atom._NET_NUMBER_OF_DESKTOPS) {
499 server.nb_desktop = server_get_number_of_desktop ();
500 cleanup_taskbar();
501 init_taskbar();
502 visible_object();
503 for (i=0 ; i < nb_panel ; i++) {
504 panel1[i].area.resize = 1;
505 }
506 task_refresh_tasklist();
507 panel_refresh = 1;
508 }
509 // Change desktop
510 else if (at == server.atom._NET_CURRENT_DESKTOP) {
511 server.desktop = server_get_current_desktop ();
512 if (panel_mode != MULTI_DESKTOP) {
513 visible_object();
514 }
515 }
516 // Window list
517 else if (at == server.atom._NET_CLIENT_LIST) {
518 task_refresh_tasklist();
519 panel_refresh = 1;
520 }
521 // Change active
522 else if (at == server.atom._NET_ACTIVE_WINDOW) {
523 GSList *l0;
524 if (task_active) {
525 for (i=0 ; i < nb_panel ; i++) {
526 for (j=0 ; j < panel1[i].nb_desktop ; j++) {
527 for (l0 = panel1[i].taskbar[j].area.list; l0 ; l0 = l0->next) {
528 tsk = l0->data;
529 tsk->area.is_active = 0;
530 }
531 }
532 }
533 task_active = 0;
534 }
535 Window w1 = window_get_active ();
536 Task *t = task_get_task(w1);
537 if (!t) {
538 Window w2;
539 if (XGetTransientForHint(server.dsp, w1, &w2) != 0)
540 if (w2) t = task_get_task(w2);
541 }
542 if (task_urgent == t) {
543 init_precision();
544 task_urgent = 0;
545 }
546 // put active state on all task (multi_desktop)
547 if (t) {
548 for (i=0 ; i < nb_panel ; i++) {
549 for (j=0 ; j < panel1[i].nb_desktop ; j++) {
550 for (l0 = panel1[i].taskbar[j].area.list; l0 ; l0 = l0->next) {
551 tsk = l0->data;
552 if (tsk->win == t->win) {
553 tsk->area.is_active = 1;
554 //printf("active monitor %d, task %s\n", panel1[i].monitor, tsk->title);
555 }
556 }
557 }
558 }
559 task_active = t;
560 }
561 panel_refresh = 1;
562 }
563 else if (at == server.atom._XROOTPMAP_ID) {
564 // change Wallpaper
565 for (i=0 ; i < nb_panel ; i++) {
566 set_panel_background(&panel1[i]);
567 }
568 panel_refresh = 1;
569 }
570 }
571 else {
572 tsk = task_get_task (win);
573 if (!tsk) return;
574 //printf("atom root_win = %s, %s\n", XGetAtomName(server.dsp, at), tsk->title);
575
576 // Window title changed
577 if (at == server.atom._NET_WM_VISIBLE_NAME || at == server.atom._NET_WM_NAME || at == server.atom.WM_NAME) {
578 Task *tsk2;
579 GSList *l0;
580 get_title(tsk);
581 // changed other tsk->title
582 for (i=0 ; i < nb_panel ; i++) {
583 for (j=0 ; j < panel1[i].nb_desktop ; j++) {
584 for (l0 = panel1[i].taskbar[j].area.list; l0 ; l0 = l0->next) {
585 tsk2 = l0->data;
586 if (tsk->win == tsk2->win && tsk != tsk2) {
587 tsk2->title = tsk->title;
588 tsk2->area.redraw = 1;
589 }
590 }
591 }
592 }
593 panel_refresh = 1;
594 }
595 // Demand attention
596 else if (at == server.atom._NET_WM_STATE) {
597 if (window_is_urgent (win)) {
598 task_urgent = tsk;
599 tick_urgent = 0;
600 time_precision = 1;
601 }
602 }
603 else if (at == server.atom.WM_STATE) {
604 // Iconic state
605 // TODO : try to delete following code
606 if (window_is_iconified (win)) {
607 if (task_active) {
608 if (task_active->win == tsk->win) {
609 Task *tsk2;
610 GSList *l0;
611 for (i=0 ; i < nb_panel ; i++) {
612 for (j=0 ; j < panel1[i].nb_desktop ; j++) {
613 for (l0 = panel1[i].taskbar[j].area.list; l0 ; l0 = l0->next) {
614 tsk2 = l0->data;
615 tsk2->area.is_active = 0;
616 }
617 }
618 }
619 task_active = 0;
620 }
621 }
622 }
623 }
624 // Window icon changed
625 else if (at == server.atom._NET_WM_ICON) {
626 get_icon(tsk);
627 Task *tsk2;
628 GSList *l0;
629 for (i=0 ; i < nb_panel ; i++) {
630 for (j=0 ; j < panel1[i].nb_desktop ; j++) {
631 for (l0 = panel1[i].taskbar[j].area.list; l0 ; l0 = l0->next) {
632 tsk2 = l0->data;
633 if (tsk->win == tsk2->win && tsk != tsk2) {
634 tsk2->icon_width = tsk->icon_width;
635 tsk2->icon_height = tsk->icon_height;
636 tsk2->icon_data = tsk->icon_data;
637 tsk2->icon_data_active = tsk->icon_data_active;
638 tsk2->area.redraw = 1;
639 }
640 }
641 }
642 }
643 panel_refresh = 1;
644 }
645 // Window desktop changed
646 else if (at == server.atom._NET_WM_DESKTOP) {
647 int desktop = window_get_desktop (win);
648 int active = tsk->area.is_active;
649 //printf(" Window desktop changed %d, %d\n", tsk->desktop, desktop);
650 // bug in windowmaker : send unecessary 'desktop changed' when focus changed
651 if (desktop != tsk->desktop) {
652 remove_task (tsk);
653 tsk = add_task (win);
654 if (tsk && active) {
655 tsk->area.is_active = 1;
656 task_active = tsk;
657 }
658 panel_refresh = 1;
659 }
660 }
661
662 if (!server.got_root_win) server.root_win = RootWindow (server.dsp, server.screen);
663 }
664 }
665
666
667 void event_expose (XEvent *e)
668 {
669 Panel *panel;
670
671 panel = get_panel(e->xany.window);
672 if (!panel) return;
673 // TODO : one panel_refresh per panel ?
674 panel_refresh = 1;
675 }
676
677
678 void event_configure_notify (Window win)
679 {
680 // check 'win' move in systray
681 TrayWindow *traywin;
682 GSList *l;
683 for (l = systray.list_icons; l ; l = l->next) {
684 traywin = (TrayWindow*)l->data;
685 if (traywin->id == win) {
686 //printf("move tray %d\n", traywin->x);
687 XMoveResizeWindow(server.dsp, traywin->id, traywin->x, traywin->y, traywin->width, traywin->height);
688 panel_refresh = 1;
689 return;
690 }
691 }
692
693 // check 'win' move in another monitor
694 if (nb_panel == 1) return;
695 if (server.nb_monitor == 1) return;
696 Task *tsk = task_get_task (win);
697 if (!tsk) return;
698
699 Panel *p = tsk->area.panel;
700 if (p->monitor != window_get_monitor (win)) {
701 remove_task (tsk);
702 add_task (win);
703 if (win == window_get_active ()) {
704 Task *tsk = task_get_task (win);
705 tsk->area.is_active = 1;
706 task_active = tsk;
707 }
708 panel_refresh = 1;
709 }
710 }
711
712
713 void event_timer()
714 {
715 struct timeval stv;
716 int i;
717
718 if (gettimeofday(&stv, 0)) return;
719
720 if (abs(stv.tv_sec - time_clock.tv_sec) < time_precision) return;
721 time_clock.tv_sec = stv.tv_sec;
722 time_clock.tv_sec -= time_clock.tv_sec % time_precision;
723
724 // urgent task
725 if (task_urgent) {
726 if (tick_urgent < max_tick_urgent) {
727 task_urgent->area.is_active = !task_urgent->area.is_active;
728 task_urgent->area.redraw = 1;
729 tick_urgent++;
730 }
731 }
732
733 // update battery
734 #ifdef ENABLE_BATTERY
735 if (panel1[0].battery.area.on_screen) {
736 update_battery();
737 for (i=0 ; i < nb_panel ; i++)
738 panel1[i].battery.area.resize = 1;
739 }
740 #endif
741
742 // update clock
743 if (time1_format) {
744 for (i=0 ; i < nb_panel ; i++)
745 panel1[i].clock.area.resize = 1;
746 }
747 panel_refresh = 1;
748 }
749
750
751 int main (int argc, char *argv[])
752 {
753 XEvent e;
754 fd_set fd;
755 int x11_fd, i, c;
756 struct timeval tv;
757 Panel *panel;
758 GSList *it;
759
760 init ();
761
762 load_config:
763 i = 0;
764 c = getopt (argc, argv, "c:");
765 init_config();
766 if (c != -1) {
767 i = config_read_file (optarg);
768 c = getopt (argc, argv, "j:");
769 if (c != -1) {
770 // usage: tint2 [-c] <config_file> -j <file> for internal use
771 printf("file %s\n", optarg);
772 cleanup();
773 exit(0);
774 }
775 }
776 if (!i)
777 i = config_read ();
778 if (!i) {
779 fprintf(stderr, "usage: tint2 [-c] <config_file>\n");
780 cleanup();
781 exit(1);
782 }
783 config_finish();
784
785 x11_fd = ConnectionNumber(server.dsp);
786 XSync(server.dsp, False);
787
788 while (1) {
789 // thanks to AngryLlama for the timer
790 // Create a File Description Set containing x11_fd
791 FD_ZERO (&fd);
792 FD_SET (x11_fd, &fd);
793
794 tv.tv_usec = 500000;
795 tv.tv_sec = 0;
796
797 // Wait for X Event or a Timer
798 if (select(x11_fd+1, &fd, 0, 0, &tv)) {
799 while (XPending (server.dsp)) {
800 XNextEvent(server.dsp, &e);
801
802 switch (e.type) {
803 case ButtonPress:
804 event_button_press (&e);
805 break;
806
807 case ButtonRelease:
808 event_button_release(&e);
809 break;
810
811 case Expose:
812 event_expose(&e);
813 break;
814
815 case PropertyNotify:
816 event_property_notify(&e);
817 break;
818
819 case ConfigureNotify:
820 if (e.xconfigure.window == server.root_win)
821 goto load_config;
822 else
823 event_configure_notify (e.xconfigure.window);
824 break;
825
826 case ReparentNotify:
827 if (!systray.area.on_screen)
828 break;
829 panel = (Panel*)systray.area.panel;
830 if (e.xany.window == panel->main_win) // reparented to us
831 break;
832 // FIXME: 'reparent to us' badly detected => disabled
833 break;
834 case UnmapNotify:
835 case DestroyNotify:
836 if (!systray.area.on_screen)
837 break;
838 for (it = systray.list_icons; it; it = g_slist_next(it)) {
839 if (((TrayWindow*)it->data)->id == e.xany.window) {
840 remove_icon((TrayWindow*)it->data);
841 break;
842 }
843 }
844 break;
845
846 case ClientMessage:
847 if (!systray.area.on_screen) break;
848 if (e.xclient.message_type == server.atom._NET_SYSTEM_TRAY_OPCODE && e.xclient.format == 32 && e.xclient.window == net_sel_win) {
849 net_message(&e.xclient);
850 }
851 break;
852 }
853 }
854 }
855 event_timer();
856
857 switch (signal_pending) {
858 case SIGUSR1:
859 signal_pending = 0;
860 goto load_config;
861 case SIGINT:
862 case SIGTERM:
863 case SIGHUP:
864 cleanup ();
865 return 0;
866 }
867
868 if (panel_refresh) {
869 panel_refresh = 0;
870
871 if (refresh_systray) {
872 panel = (Panel*)systray.area.panel;
873 XSetWindowBackgroundPixmap (server.dsp, panel->main_win, None);
874 }
875 for (i=0 ; i < nb_panel ; i++) {
876 panel = &panel1[i];
877
878 if (panel->temp_pmap) XFreePixmap(server.dsp, panel->temp_pmap);
879 panel->temp_pmap = XCreatePixmap(server.dsp, server.root_win, panel->area.width, panel->area.height, server.depth);
880
881 refresh(&panel->area);
882 XCopyArea(server.dsp, panel->temp_pmap, panel->main_win, server.gc, 0, 0, panel->area.width, panel->area.height, 0, 0);
883 }
884 XFlush (server.dsp);
885
886 if (refresh_systray) {
887 refresh_systray = 0;
888 panel = (Panel*)systray.area.panel;
889 // tint2 doen't draw systray icons. it just redraw background.
890 XSetWindowBackgroundPixmap (server.dsp, panel->main_win, panel->temp_pmap);
891 // force icon's refresh
892 refresh_systray_icon();
893 }
894 }
895 }
896 }
897
898
This page took 0.076663 seconds and 5 git commands to generate.