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