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