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