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