]> Dogcows Code - chaz/tint2/blob - src/tint.c
*add* multiple tasks can be urgent now
[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 add_urgent(tsk);
523 }
524 if (window_is_skip_taskbar(win)) {
525 remove_task( tsk );
526 panel_refresh = 1;
527 }
528 }
529 // We do not check for the iconified state, since it only unsets our active window
530 // but in openbox a shaded window is considered iconified. So we would loose the active window
531 // property on unshading it again (commented 01.10.2009)
532 // else if (at == server.atom.WM_STATE) {
533 // // Iconic state
534 // // TODO : try to delete following code
535 // if (window_is_iconified (win)) {
536 // if (task_active) {
537 // if (task_active->win == tsk->win) {
538 // Task *tsk2;
539 // GSList *l0;
540 // for (i=0 ; i < nb_panel ; i++) {
541 // for (j=0 ; j < panel1[i].nb_desktop ; j++) {
542 // for (l0 = panel1[i].taskbar[j].area.list; l0 ; l0 = l0->next) {
543 // tsk2 = l0->data;
544 // tsk2->area.is_active = 0;
545 // }
546 // }
547 // }
548 // task_active = 0;
549 // }
550 // }
551 // }
552 // }
553 // Window icon changed
554 else if (at == server.atom._NET_WM_ICON) {
555 get_icon(tsk);
556 Task *tsk2;
557 GSList *l0;
558 for (i=0 ; i < nb_panel ; i++) {
559 for (j=0 ; j < panel1[i].nb_desktop ; j++) {
560 for (l0 = panel1[i].taskbar[j].area.list; l0 ; l0 = l0->next) {
561 tsk2 = l0->data;
562 if (tsk->win == tsk2->win && tsk != tsk2) {
563 tsk2->icon_width = tsk->icon_width;
564 tsk2->icon_height = tsk->icon_height;
565 tsk2->icon = tsk->icon;
566 tsk2->icon_active = tsk->icon_active;
567 tsk2->area.redraw = 1;
568 }
569 }
570 }
571 }
572 panel_refresh = 1;
573 }
574 // Window desktop changed
575 else if (at == server.atom._NET_WM_DESKTOP) {
576 int desktop = window_get_desktop (win);
577 int active = tsk->area.is_active;
578 //printf(" Window desktop changed %d, %d\n", tsk->desktop, desktop);
579 // bug in windowmaker : send unecessary 'desktop changed' when focus changed
580 if (desktop != tsk->desktop) {
581 remove_task (tsk);
582 tsk = add_task (win);
583 if (tsk && active) {
584 tsk->area.is_active = 1;
585 task_active = tsk;
586 }
587 panel_refresh = 1;
588 }
589 }
590 else if (at == server.atom.WM_HINTS) {
591 XWMHints* wmhints = XGetWMHints(server.dsp, win);
592 if (wmhints && wmhints->flags & XUrgencyHint) {
593 add_urgent(tsk);
594 }
595 XFree(wmhints);
596 }
597
598 if (!server.got_root_win) server.root_win = RootWindow (server.dsp, server.screen);
599 }
600 }
601
602
603 void event_expose (XEvent *e)
604 {
605 if (e->xany.window == g_tooltip.window)
606 tooltip_update();
607 else {
608 Panel *panel;
609 panel = get_panel(e->xany.window);
610 if (!panel) return;
611 // TODO : one panel_refresh per panel ?
612 panel_refresh = 1;
613 }
614 }
615
616
617 void event_configure_notify (Window win)
618 {
619 // change in root window (xrandr)
620 if (win == server.root_win) {
621 get_monitors();
622 init_config();
623 config_read_file (config_path);
624 init_panel();
625 cleanup_config();
626 return;
627 }
628
629 // 'win' is a trayer icon
630 TrayWindow *traywin;
631 GSList *l;
632 for (l = systray.list_icons; l ; l = l->next) {
633 traywin = (TrayWindow*)l->data;
634 if (traywin->id == win) {
635 //printf("move tray %d\n", traywin->x);
636 XMoveResizeWindow(server.dsp, traywin->id, traywin->x, traywin->y, traywin->width, traywin->height);
637 panel_refresh = 1;
638 return;
639 }
640 }
641
642 // 'win' move in another monitor
643 if (nb_panel == 1) return;
644 Task *tsk = task_get_task (win);
645 if (!tsk) return;
646
647 Panel *p = tsk->area.panel;
648 if (p->monitor != window_get_monitor (win)) {
649 remove_task (tsk);
650 add_task (win);
651 if (win == window_get_active ()) {
652 Task *tsk = task_get_task (win);
653 tsk->area.is_active = 1;
654 task_active = tsk;
655 }
656 panel_refresh = 1;
657 }
658 }
659
660
661 void event_timer()
662 {
663 struct timeval stv;
664 int i;
665
666 if (gettimeofday(&stv, 0)) return;
667
668 if (abs(stv.tv_sec - time_clock.tv_sec) < time_precision) return;
669 time_clock.tv_sec = stv.tv_sec;
670 time_clock.tv_sec -= time_clock.tv_sec % time_precision;
671
672 // urgent task
673 GSList* urgent_task = urgent_list;
674 while (urgent_task) {
675 Task_urgent* t = urgent_task->data;
676 if ( t->tick < max_tick_urgent) {
677 t->tsk->area.is_active = !t->tsk->area.is_active;
678 t->tsk->area.redraw = 1;
679 t->tick++;
680 }
681 urgent_task = urgent_task->next;
682 }
683
684 // update battery
685 #ifdef ENABLE_BATTERY
686 if (battery_enabled) {
687 update_battery();
688 for (i=0 ; i < nb_panel ; i++)
689 panel1[i].battery.area.resize = 1;
690 }
691 #endif
692
693 // update clock
694 if (time1_format) {
695 for (i=0 ; i < nb_panel ; i++)
696 panel1[i].clock.area.resize = 1;
697 }
698 panel_refresh = 1;
699 }
700
701
702 void dnd_message(XClientMessageEvent *e)
703 {
704 Panel *panel = get_panel(e->window);
705 int x, y, mapX, mapY;
706 Window child;
707 x = (e->data.l[2] >> 16) & 0xFFFF;
708 y = e->data.l[2] & 0xFFFF;
709 XTranslateCoordinates(server.dsp, server.root_win, e->window, x, y, &mapX, &mapY, &child);
710 Task* task = click_task(panel, mapX, mapY);
711 if (task) {
712 if (task->desktop != server.desktop )
713 set_desktop (task->desktop);
714 window_action(task, TOGGLE);
715 }
716
717 // send XdndStatus event to get more XdndPosition events
718 XClientMessageEvent se;
719 se.type = ClientMessage;
720 se.window = e->data.l[0];
721 se.message_type = server.atom.XdndStatus;
722 se.format = 32;
723 se.data.l[0] = e->window; // XID of the target window
724 se.data.l[1] = 0; // bit 0: accept drop bit 1: send XdndPosition events if inside rectangle
725 se.data.l[2] = 0; // Rectangle x,y for which no more XdndPosition events
726 se.data.l[3] = (1 << 16) | 1; // Rectangle w,h for which no more XdndPosition events
727 se.data.l[4] = None; // None = drop will not be accepted
728 XSendEvent(server.dsp, e->data.l[0], False, NoEventMask, (XEvent*)&se);
729 }
730
731
732 int main (int argc, char *argv[])
733 {
734 XEvent e;
735 fd_set fd;
736 int x11_fd, i;
737 struct timeval tv;
738 Panel *panel;
739 GSList *it;
740
741 init (argc, argv);
742
743 i = 0;
744 init_config();
745 if (config_path)
746 i = config_read_file (config_path);
747 else
748 i = config_read ();
749 if (!i) {
750 fprintf(stderr, "usage: tint2 [-c] <config_file>\n");
751 cleanup();
752 exit(1);
753 }
754 init_panel();
755 cleanup_config();
756 if (snapshot_path) {
757 get_snapshot(snapshot_path);
758 cleanup();
759 exit(0);
760 }
761
762 x11_fd = ConnectionNumber(server.dsp);
763 XSync(server.dsp, False);
764
765 while (1) {
766 // thanks to AngryLlama for the timer
767 // Create a File Description Set containing x11_fd
768 FD_ZERO (&fd);
769 FD_SET (x11_fd, &fd);
770
771 tv.tv_usec = 500000;
772 tv.tv_sec = 0;
773
774 // Wait for X Event or a Timer
775 if (select(x11_fd+1, &fd, 0, 0, &tv)) {
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 PropertyNotify:
809 event_property_notify(&e);
810 break;
811
812 case ConfigureNotify:
813 event_configure_notify (e.xconfigure.window);
814 break;
815
816 case ReparentNotify:
817 if (!systray_enabled)
818 break;
819 panel = (Panel*)systray.area.panel;
820 if (e.xany.window == panel->main_win) // reparented to us
821 break;
822 // FIXME: 'reparent to us' badly detected => disabled
823 break;
824 case UnmapNotify:
825 case DestroyNotify:
826 if (!systray.area.on_screen)
827 break;
828 for (it = systray.list_icons; it; it = g_slist_next(it)) {
829 if (((TrayWindow*)it->data)->id == e.xany.window) {
830 remove_icon((TrayWindow*)it->data);
831 break;
832 }
833 }
834 break;
835
836 case ClientMessage:
837 if (!systray.area.on_screen) break;
838 if (e.xclient.message_type == server.atom._NET_SYSTEM_TRAY_OPCODE && e.xclient.format == 32 && e.xclient.window == net_sel_win) {
839 net_message(&e.xclient);
840 }
841 else if (e.xclient.message_type == server.atom.XdndPosition) {
842 dnd_message(&e.xclient);
843 }
844 break;
845 }
846 }
847 }
848 event_timer();
849
850 switch (signal_pending) {
851 case SIGUSR1: // reload config file
852 signal_pending = 0;
853 init_config();
854 config_read_file (config_path);
855 init_panel();
856 cleanup_config();
857 break;
858 case SIGINT:
859 case SIGTERM:
860 case SIGHUP:
861 cleanup ();
862 return 0;
863 }
864
865 if (panel_refresh) {
866 panel_refresh = 0;
867
868 if (refresh_systray) {
869 panel = (Panel*)systray.area.panel;
870 XSetWindowBackgroundPixmap (server.dsp, panel->main_win, None);
871 }
872 for (i=0 ; i < nb_panel ; i++) {
873 panel = &panel1[i];
874
875 if (panel->temp_pmap) XFreePixmap(server.dsp, panel->temp_pmap);
876 panel->temp_pmap = XCreatePixmap(server.dsp, server.root_win, panel->area.width, panel->area.height, server.depth);
877
878 refresh(&panel->area);
879 XCopyArea(server.dsp, panel->temp_pmap, panel->main_win, server.gc, 0, 0, panel->area.width, panel->area.height, 0, 0);
880 }
881 XFlush (server.dsp);
882
883 if (refresh_systray) {
884 refresh_systray = 0;
885 panel = (Panel*)systray.area.panel;
886 // tint2 doen't draw systray icons. it just redraw background.
887 XSetWindowBackgroundPixmap (server.dsp, panel->main_win, panel->temp_pmap);
888 // force icon's refresh
889 refresh_systray_icon();
890 }
891 }
892 }
893 }
894
895
This page took 0.076061 seconds and 5 git commands to generate.