]> Dogcows Code - chaz/tint2/blob - src/tint.c
db4095985ffa2668afdf23ec237e8b55b13c1d47
[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) from Omega distribution
7 *
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License version 2
10 * as published by the Free Software Foundation.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 **************************************************************************/
20
21 #include <sys/stat.h>
22 #include <unistd.h>
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <stdint.h>
26 #include <string.h>
27 #include <X11/Xutil.h>
28 #include <X11/Xatom.h>
29 #include <X11/Xlocale.h>
30 #include <X11/extensions/Xdamage.h>
31 #include <Imlib2.h>
32 #include <signal.h>
33
34 #include <version.h>
35 #include "server.h"
36 #include "window.h"
37 #include "config.h"
38 #include "task.h"
39 #include "taskbar.h"
40 #include "systraybar.h"
41 #include "launcher.h"
42 #include "panel.h"
43 #include "tooltip.h"
44 #include "timer.h"
45 #include "xsettings-client.h"
46
47
48 void signal_handler(int sig)
49 {
50 // signal handler is light as it should be
51 signal_pending = sig;
52 }
53
54
55 void init (int argc, char *argv[])
56 {
57 int i;
58
59 // set global data
60 default_config();
61 default_timeout();
62 default_systray();
63 memset(&server, 0, sizeof(Server_global));
64 #ifdef ENABLE_BATTERY
65 default_battery();
66 #endif
67 default_clock();
68 default_launcher();
69 default_taskbar();
70 default_tooltip();
71 default_panel();
72
73 // read options
74 for (i = 1; i < argc; ++i) {
75 if (!strcmp(argv[i], "-h") || !strcmp(argv[i], "--help")) {
76 printf("Usage: tint2 [-c] <config_file>\n");
77 exit(0);
78 }
79 if (!strcmp(argv[i], "-v") || !strcmp(argv[i], "--version")) {
80 printf("tint2 version %s\n", VERSION_STRING);
81 exit(0);
82 }
83 if (!strcmp(argv[i], "-c")) {
84 i++;
85 if (i < argc)
86 config_path = strdup(argv[i]);
87 }
88 if (!strcmp(argv[i], "-s")) {
89 i++;
90 if (i < argc)
91 snapshot_path = strdup(argv[i]);
92 }
93 }
94 // Set signal handler
95 signal_pending = 0;
96 struct sigaction sa = { .sa_handler = signal_handler };
97 struct sigaction sa_chld = { .sa_handler = SIG_DFL, .sa_flags = SA_NOCLDWAIT };
98 sigaction(SIGUSR1, &sa, 0);
99 sigaction(SIGINT, &sa, 0);
100 sigaction(SIGTERM, &sa, 0);
101 sigaction(SIGHUP, &sa, 0);
102 sigaction(SIGCHLD, &sa_chld, 0);
103
104 // BSD does not support pselect(), therefore we have to use select and hope that we do not
105 // end up in a race condition there (see 'man select()' on a linux machine for more information)
106 // block all signals, such that no race conditions occur before pselect in our main loop
107 // sigset_t block_mask;
108 // sigaddset(&block_mask, SIGINT);
109 // sigaddset(&block_mask, SIGTERM);
110 // sigaddset(&block_mask, SIGHUP);
111 // sigaddset(&block_mask, SIGUSR1);
112 // sigprocmask(SIG_BLOCK, &block_mask, 0);
113 }
114
115 void init_X11()
116 {
117 server.dsp = XOpenDisplay (NULL);
118 if (!server.dsp) {
119 fprintf(stderr, "tint2 exit : could not open display.\n");
120 exit(0);
121 }
122 server_init_atoms ();
123 server.screen = DefaultScreen (server.dsp);
124 server.root_win = RootWindow(server.dsp, server.screen);
125 server.desktop = server_get_current_desktop ();
126 server_init_visual();
127 XSetErrorHandler ((XErrorHandler) server_catch_error);
128
129 imlib_context_set_display (server.dsp);
130 imlib_context_set_visual (server.visual);
131 imlib_context_set_colormap (server.colormap);
132
133 /* Catch events */
134 XSelectInput (server.dsp, server.root_win, PropertyChangeMask|StructureNotifyMask);
135
136 setlocale (LC_ALL, "");
137 // config file use '.' as decimal separator
138 setlocale(LC_NUMERIC, "POSIX");
139
140 // load default icon
141 gchar *path;
142 const gchar * const *data_dirs;
143 data_dirs = g_get_system_data_dirs ();
144 int i;
145 for (i = 0; data_dirs[i] != NULL; i++) {
146 path = g_build_filename(data_dirs[i], "tint2", "default_icon.png", NULL);
147 if (g_file_test (path, G_FILE_TEST_EXISTS))
148 default_icon = imlib_load_image(path);
149 g_free(path);
150 }
151
152 // get monitor and desktop config
153 get_monitors();
154 get_desktops();
155 }
156
157
158 void cleanup()
159 {
160 cleanup_systray();
161 cleanup_tooltip();
162 cleanup_clock();
163 cleanup_launcher();
164 #ifdef ENABLE_BATTERY
165 cleanup_battery();
166 #endif
167 cleanup_panel();
168 cleanup_config();
169
170 if (default_icon) {
171 imlib_context_set_image(default_icon);
172 imlib_free_image();
173 }
174 imlib_context_disconnect_display();
175
176 cleanup_server();
177 cleanup_timeout();
178 if (server.dsp) XCloseDisplay(server.dsp);
179 }
180
181
182 void get_snapshot(const char *path)
183 {
184 Panel *panel = &panel1[0];
185
186 if (panel->area.width > server.monitor[0].width)
187 panel->area.width = server.monitor[0].width;
188
189 panel->temp_pmap = XCreatePixmap(server.dsp, server.root_win, panel->area.width, panel->area.height, server.depth);
190 rendering(panel);
191
192 Imlib_Image img = NULL;
193 imlib_context_set_drawable(panel->temp_pmap);
194 img = imlib_create_image_from_drawable(0, 0, 0, panel->area.width, panel->area.height, 0);
195
196 imlib_context_set_image(img);
197 if (!panel_horizontal) {
198 // rotate 90° vertical panel
199 imlib_image_flip_horizontal();
200 imlib_image_flip_diagonal();
201 }
202 imlib_save_image(path);
203 imlib_free_image();
204 }
205
206
207 void window_action (Task *tsk, int action)
208 {
209 if (!tsk) return;
210 int desk;
211 switch (action) {
212 case CLOSE:
213 set_close (tsk->win);
214 break;
215 case TOGGLE:
216 set_active(tsk->win);
217 break;
218 case ICONIFY:
219 XIconifyWindow (server.dsp, tsk->win, server.screen);
220 break;
221 case TOGGLE_ICONIFY:
222 if (task_active && tsk->win == task_active->win)
223 XIconifyWindow (server.dsp, tsk->win, server.screen);
224 else
225 set_active (tsk->win);
226 break;
227 case SHADE:
228 window_toggle_shade (tsk->win);
229 break;
230 case MAXIMIZE_RESTORE:
231 window_maximize_restore (tsk->win);
232 break;
233 case MAXIMIZE:
234 window_maximize_restore (tsk->win);
235 break;
236 case RESTORE:
237 window_maximize_restore (tsk->win);
238 break;
239 case DESKTOP_LEFT:
240 if ( tsk->desktop == 0 ) break;
241 desk = tsk->desktop - 1;
242 windows_set_desktop(tsk->win, desk);
243 if (desk == server.desktop)
244 set_active(tsk->win);
245 break;
246 case DESKTOP_RIGHT:
247 if (tsk->desktop == server.nb_desktop ) break;
248 desk = tsk->desktop + 1;
249 windows_set_desktop(tsk->win, desk);
250 if (desk == server.desktop)
251 set_active(tsk->win);
252 break;
253 case NEXT_TASK:
254 if (task_active) {
255 Task *tsk1;
256 tsk1 = next_task(task_active);
257 set_active(tsk1->win);
258 }
259 break;
260 case PREV_TASK:
261 if (task_active) {
262 Task *tsk1;
263 tsk1 = prev_task(task_active);
264 set_active(tsk1->win);
265 }
266 }
267 }
268
269
270 int tint2_handles_click(Panel* panel, XButtonEvent* e)
271 {
272 Task* task = click_task(panel, e->x, e->y);
273 if (task) {
274 if( (e->button == 1)
275 || (e->button == 2 && mouse_middle != 0)
276 || (e->button == 3 && mouse_right != 0)
277 || (e->button == 4 && mouse_scroll_up != 0)
278 || (e->button == 5 && mouse_scroll_down !=0) )
279 {
280 return 1;
281 }
282 else
283 return 0;
284 }
285 LauncherIcon *icon = click_launcher_icon(panel, e->x, e->y);
286 if (icon) {
287 if (e->button == 1) {
288 return 1;
289 } else {
290 return 0;
291 }
292 }
293 // no launcher/task clicked --> check if taskbar clicked
294 Taskbar *tskbar = click_taskbar(panel, e->x, e->y);
295 if (tskbar && e->button == 1 && panel_mode == MULTI_DESKTOP)
296 return 1;
297 if (click_clock(panel, e->x, e->y)) {
298 if ( (e->button == 1 && clock_lclick_command) || (e->button == 3 && clock_rclick_command) )
299 return 1;
300 else
301 return 0;
302 }
303 return 0;
304 }
305
306
307 void forward_click(XEvent* e)
308 {
309 // forward the click to the desktop window (thanks conky)
310 XUngrabPointer(server.dsp, e->xbutton.time);
311 e->xbutton.window = server.root_win;
312 // icewm doesn't open under the mouse.
313 // and xfce doesn't open at all.
314 e->xbutton.x = e->xbutton.x_root;
315 e->xbutton.y = e->xbutton.y_root;
316 //printf("**** %d, %d\n", e->xbutton.x, e->xbutton.y);
317 //XSetInputFocus(server.dsp, e->xbutton.window, RevertToParent, e->xbutton.time);
318 XSendEvent(server.dsp, e->xbutton.window, False, ButtonPressMask, e);
319 }
320
321
322 void event_button_press (XEvent *e)
323 {
324 Panel *panel = get_panel(e->xany.window);
325 if (!panel) return;
326
327
328 if (wm_menu && !tint2_handles_click(panel, &e->xbutton) ) {
329 forward_click(e);
330 return;
331 }
332 task_drag = click_task(panel, e->xbutton.x, e->xbutton.y);
333
334 if (panel_layer == BOTTOM_LAYER)
335 XLowerWindow (server.dsp, panel->main_win);
336 }
337
338 void event_button_motion_notify (XEvent *e)
339 {
340 Panel * panel = get_panel(e->xany.window);
341 if(!panel || !task_drag)
342 return;
343
344 // Find the taskbar on the event's location
345 Taskbar * event_taskbar = click_taskbar(panel, e->xbutton.x, e->xbutton.y);
346 if(event_taskbar == NULL)
347 return;
348
349 // Find the task on the event's location
350 Task * event_task = click_task(panel, e->xbutton.x, e->xbutton.y);
351
352 // If the event takes place on the same taskbar as the task being dragged
353 if(event_taskbar == task_drag->area.parent) {
354 // Swap the task_drag with the task on the event's location (if they differ)
355 if(event_task && event_task != task_drag) {
356 GSList * drag_iter = g_slist_find(event_taskbar->area.list, task_drag);
357 GSList * task_iter = g_slist_find(event_taskbar->area.list, event_task);
358 if(drag_iter && task_iter) {
359 gpointer temp = task_iter->data;
360 task_iter->data = drag_iter->data;
361 drag_iter->data = temp;
362 event_taskbar->area.resize = 1;
363 panel_refresh = 1;
364 task_dragged = 1;
365 }
366 }
367 }
368 else { // The event is on another taskbar than the task being dragged
369 if(task_drag->desktop == ALLDESKTOP || panel_mode != MULTI_DESKTOP)
370 return;
371
372 Taskbar * drag_taskbar = (Taskbar*)task_drag->area.parent;
373 drag_taskbar->area.list = g_slist_remove(drag_taskbar->area.list, task_drag);
374
375 if(event_taskbar->area.posx > drag_taskbar->area.posx || event_taskbar->area.posy > drag_taskbar->area.posy) {
376 int i = (taskbarname_enabled) ? 1 : 0;
377 event_taskbar->area.list = g_slist_insert(event_taskbar->area.list, task_drag, i);
378 }
379 else
380 event_taskbar->area.list = g_slist_append(event_taskbar->area.list, task_drag);
381
382 // Move task to other desktop (but avoid the 'Window desktop changed' code in 'event_property_notify')
383 task_drag->area.parent = event_taskbar;
384 task_drag->desktop = event_taskbar->desktop;
385
386 windows_set_desktop(task_drag->win, event_taskbar->desktop);
387
388 event_taskbar->area.resize = 1;
389 drag_taskbar->area.resize = 1;
390 task_dragged = 1;
391 panel_refresh = 1;
392 }
393 }
394
395 void event_button_release (XEvent *e)
396 {
397 Panel *panel = get_panel(e->xany.window);
398 if (!panel) return;
399
400 if (wm_menu && !tint2_handles_click(panel, &e->xbutton)) {
401 forward_click(e);
402 if (panel_layer == BOTTOM_LAYER)
403 XLowerWindow (server.dsp, panel->main_win);
404 task_drag = 0;
405 return;
406 }
407
408 int action = TOGGLE_ICONIFY;
409 switch (e->xbutton.button) {
410 case 2:
411 action = mouse_middle;
412 break;
413 case 3:
414 action = mouse_right;
415 break;
416 case 4:
417 action = mouse_scroll_up;
418 break;
419 case 5:
420 action = mouse_scroll_down;
421 break;
422 case 6:
423 action = mouse_tilt_left;
424 break;
425 case 7:
426 action = mouse_tilt_right;
427 break;
428 }
429
430 if ( click_clock(panel, e->xbutton.x, e->xbutton.y)) {
431 clock_action(e->xbutton.button);
432 if (panel_layer == BOTTOM_LAYER)
433 XLowerWindow (server.dsp, panel->main_win);
434 task_drag = 0;
435 return;
436 }
437
438 if ( click_launcher(panel, e->xbutton.x, e->xbutton.y)) {
439 LauncherIcon *icon = click_launcher_icon(panel, e->xbutton.x, e->xbutton.y);
440 if (icon) {
441 launcher_action(icon);
442 }
443 task_drag = 0;
444 return;
445 }
446
447 Taskbar *tskbar;
448 if ( !(tskbar = click_taskbar(panel, e->xbutton.x, e->xbutton.y)) ) {
449 // TODO: check better solution to keep window below
450 if (panel_layer == BOTTOM_LAYER)
451 XLowerWindow (server.dsp, panel->main_win);
452 task_drag = 0;
453 return;
454 }
455
456 // drag and drop task
457 if (task_dragged) {
458 task_drag = 0;
459 task_dragged = 0;
460 return;
461 }
462
463 // switch desktop
464 if (panel_mode == MULTI_DESKTOP) {
465 if (tskbar->desktop != server.desktop && action != CLOSE && action != DESKTOP_LEFT && action != DESKTOP_RIGHT)
466 set_desktop (tskbar->desktop);
467 }
468
469 // action on task
470 window_action( click_task(panel, e->xbutton.x, e->xbutton.y), action);
471
472 // to keep window below
473 if (panel_layer == BOTTOM_LAYER)
474 XLowerWindow (server.dsp, panel->main_win);
475 }
476
477
478 void event_property_notify (XEvent *e)
479 {
480 int i;
481 Task *tsk;
482 Window win = e->xproperty.window;
483 Atom at = e->xproperty.atom;
484
485 if (xsettings_client)
486 xsettings_client_process_event(xsettings_client, e);
487 if (win == server.root_win) {
488 if (!server.got_root_win) {
489 XSelectInput (server.dsp, server.root_win, PropertyChangeMask|StructureNotifyMask);
490 server.got_root_win = 1;
491 }
492
493 // Change name of desktops
494 else if (at == server.atom._NET_DESKTOP_NAMES) {
495 if (!taskbarname_enabled) return;
496 GSList *l, *list = server_get_name_of_desktop();
497 int j;
498 gchar *name;
499 Taskbar *tskbar;
500 for (i=0 ; i < nb_panel ; i++) {
501 for (j=0, l=list ; j < panel1[i].nb_desktop ; j++) {
502 if (l) {
503 name = g_strdup(l->data);
504 l = l->next;
505 }
506 else
507 name = g_strdup_printf("%d", j+1);
508 tskbar = &panel1[i].taskbar[j];
509 if (strcmp(name, tskbar->bar_name.name) != 0) {
510 g_free(tskbar->bar_name.name);
511 tskbar->bar_name.name = name;
512 tskbar->bar_name.area.resize = 1;
513 }
514 else
515 g_free(name);
516 }
517 }
518 for (l=list ; l ; l = l->next)
519 g_free(l->data);
520 g_slist_free(list);
521 panel_refresh = 1;
522 }
523 // Change number of desktops
524 else if (at == server.atom._NET_NUMBER_OF_DESKTOPS) {
525 if (!taskbar_enabled) return;
526 server.nb_desktop = server_get_number_of_desktop ();
527 cleanup_taskbar();
528 init_taskbar();
529 for (i=0 ; i < nb_panel ; i++) {
530 init_taskbar_panel(&panel1[i]);
531 set_panel_items_order(&panel1[i]);
532 visible_taskbar(&panel1[i]);
533 panel1[i].area.resize = 1;
534 }
535 task_refresh_tasklist();
536 active_task();
537 panel_refresh = 1;
538 }
539 // Change desktop
540 else if (at == server.atom._NET_CURRENT_DESKTOP) {
541 if (!taskbar_enabled) return;
542 int old_desktop = server.desktop;
543 server.desktop = server_get_current_desktop ();
544 for (i=0 ; i < nb_panel ; i++) {
545 Panel *panel = &panel1[i];
546 set_taskbar_state(&panel->taskbar[old_desktop], TASKBAR_NORMAL);
547 set_taskbar_state(&panel->taskbar[server.desktop], TASKBAR_ACTIVE);
548 // check ALLDESKTOP task => resize taskbar
549 Taskbar *tskbar;
550 Task *tsk;
551 GSList *l;
552 if (server.nb_desktop > old_desktop) {
553 tskbar = &panel->taskbar[old_desktop];
554 l = tskbar->area.list;
555 if (taskbarname_enabled) l = l->next;
556 for (; l ; l = l->next) {
557 tsk = l->data;
558 if (tsk->desktop == ALLDESKTOP) {
559 tsk->area.on_screen = 0;
560 tskbar->area.resize = 1;
561 panel_refresh = 1;
562 }
563 }
564 }
565 tskbar = &panel->taskbar[server.desktop];
566 l = tskbar->area.list;
567 if (taskbarname_enabled) l = l->next;
568 for (; l ; l = l->next) {
569 tsk = l->data;
570 if (tsk->desktop == ALLDESKTOP) {
571 tsk->area.on_screen = 1;
572 tskbar->area.resize = 1;
573 }
574 }
575 }
576 }
577 // Window list
578 else if (at == server.atom._NET_CLIENT_LIST) {
579 task_refresh_tasklist();
580 panel_refresh = 1;
581 }
582 // Change active
583 else if (at == server.atom._NET_ACTIVE_WINDOW) {
584 active_task();
585 panel_refresh = 1;
586 }
587 else if (at == server.atom._XROOTPMAP_ID) {
588 // change Wallpaper
589 for (i=0 ; i < nb_panel ; i++) {
590 set_panel_background(&panel1[i]);
591 }
592 panel_refresh = 1;
593 }
594 }
595 else {
596 tsk = task_get_task (win);
597 if (!tsk) {
598 if (at != server.atom._NET_WM_STATE)
599 return;
600 else {
601 // xfce4 sends _NET_WM_STATE after minimized to tray, so we need to check if window is mapped
602 // if it is mapped and not set as skip_taskbar, we must add it to our task list
603 XWindowAttributes wa;
604 XGetWindowAttributes(server.dsp, win, &wa);
605 if (wa.map_state == IsViewable && !window_is_skip_taskbar(win)) {
606 if ( (tsk = add_task(win)) )
607 panel_refresh = 1;
608 else
609 return;
610 }
611 else
612 return;
613 }
614 }
615 //printf("atom root_win = %s, %s\n", XGetAtomName(server.dsp, at), tsk->title);
616
617 // Window title changed
618 if (at == server.atom._NET_WM_VISIBLE_NAME || at == server.atom._NET_WM_NAME || at == server.atom.WM_NAME) {
619 if (get_title(tsk)) {
620 if (g_tooltip.mapped && (g_tooltip.area == (Area*)tsk)) {
621 tooltip_copy_text((Area*)tsk);
622 tooltip_update();
623 }
624 panel_refresh = 1;
625 }
626 }
627 // Demand attention
628 else if (at == server.atom._NET_WM_STATE) {
629 if (window_is_urgent (win)) {
630 add_urgent(tsk);
631 }
632 if (window_is_skip_taskbar(win)) {
633 remove_task( tsk );
634 panel_refresh = 1;
635 }
636 }
637 else if (at == server.atom.WM_STATE) {
638 // Iconic state
639 int state = (task_active && tsk->win == task_active->win ? TASK_ACTIVE : TASK_NORMAL);
640 if (window_is_iconified(win))
641 state = TASK_ICONIFIED;
642 set_task_state(tsk, state);
643 panel_refresh = 1;
644 }
645 // Window icon changed
646 else if (at == server.atom._NET_WM_ICON) {
647 get_icon(tsk);
648 panel_refresh = 1;
649 }
650 // Window desktop changed
651 else if (at == server.atom._NET_WM_DESKTOP) {
652 int desktop = window_get_desktop (win);
653 //printf(" Window desktop changed %d, %d\n", tsk->desktop, desktop);
654 // bug in windowmaker : send unecessary 'desktop changed' when focus changed
655 if (desktop != tsk->desktop) {
656 remove_task (tsk);
657 tsk = add_task (win);
658 active_task();
659 panel_refresh = 1;
660 }
661 }
662 else if (at == server.atom.WM_HINTS) {
663 XWMHints* wmhints = XGetWMHints(server.dsp, win);
664 if (wmhints && wmhints->flags & XUrgencyHint) {
665 add_urgent(tsk);
666 }
667 XFree(wmhints);
668 }
669
670 if (!server.got_root_win) server.root_win = RootWindow (server.dsp, server.screen);
671 }
672 }
673
674
675 void event_expose (XEvent *e)
676 {
677 Panel *panel;
678 panel = get_panel(e->xany.window);
679 if (!panel) return;
680 // TODO : one panel_refresh per panel ?
681 panel_refresh = 1;
682 }
683
684
685 void event_configure_notify (Window win)
686 {
687 // change in root window (xrandr)
688 if (win == server.root_win) {
689 signal_pending = SIGUSR1;
690 return;
691 }
692
693 // 'win' is a trayer icon
694 TrayWindow *traywin;
695 GSList *l;
696 for (l = systray.list_icons; l ; l = l->next) {
697 traywin = (TrayWindow*)l->data;
698 if (traywin->tray_id == win) {
699 //printf("move tray %d\n", traywin->x);
700 XMoveResizeWindow(server.dsp, traywin->id, traywin->x, traywin->y, traywin->width, traywin->height);
701 XResizeWindow(server.dsp, traywin->tray_id, traywin->width, traywin->height);
702 panel_refresh = 1;
703 return;
704 }
705 }
706
707 // 'win' move in another monitor
708 if (nb_panel == 1) return;
709 Task *tsk = task_get_task (win);
710 if (!tsk) return;
711
712 Panel *p = tsk->area.panel;
713 if (p->monitor != window_get_monitor (win)) {
714 remove_task (tsk);
715 tsk = add_task (win);
716 if (win == window_get_active ()) {
717 set_task_state(tsk, TASK_ACTIVE);
718 task_active = tsk;
719 }
720 panel_refresh = 1;
721 }
722 }
723
724
725 void dnd_message(XClientMessageEvent *e)
726 {
727 Panel *panel = get_panel(e->window);
728 int x, y, mapX, mapY;
729 Window child;
730 x = (e->data.l[2] >> 16) & 0xFFFF;
731 y = e->data.l[2] & 0xFFFF;
732 XTranslateCoordinates(server.dsp, server.root_win, e->window, x, y, &mapX, &mapY, &child);
733 Task* task = click_task(panel, mapX, mapY);
734 if (task) {
735 if (task->desktop != server.desktop )
736 set_desktop (task->desktop);
737 window_action(task, TOGGLE);
738 }
739
740 // send XdndStatus event to get more XdndPosition events
741 XClientMessageEvent se;
742 se.type = ClientMessage;
743 se.window = e->data.l[0];
744 se.message_type = server.atom.XdndStatus;
745 se.format = 32;
746 se.data.l[0] = e->window; // XID of the target window
747 se.data.l[1] = 0; // bit 0: accept drop bit 1: send XdndPosition events if inside rectangle
748 se.data.l[2] = 0; // Rectangle x,y for which no more XdndPosition events
749 se.data.l[3] = (1 << 16) | 1; // Rectangle w,h for which no more XdndPosition events
750 se.data.l[4] = None; // None = drop will not be accepted
751 XSendEvent(server.dsp, e->data.l[0], False, NoEventMask, (XEvent*)&se);
752 }
753
754
755 int main (int argc, char *argv[])
756 {
757 XEvent e;
758 XClientMessageEvent *ev;
759 fd_set fdset;
760 int x11_fd, i;
761 Panel *panel;
762 GSList *it;
763 struct timeval* timeout;
764 int hidden_dnd = 0;
765
766 start:
767 init (argc, argv);
768 init_X11();
769
770 i = 0;
771 if (config_path)
772 i = config_read_file (config_path);
773 else
774 i = config_read ();
775 if (!i) {
776 fprintf(stderr, "usage: tint2 [-c] <config_file>\n");
777 cleanup();
778 exit(1);
779 }
780
781 init_panel();
782 if (snapshot_path) {
783 get_snapshot(snapshot_path);
784 cleanup();
785 exit(0);
786 }
787
788 int damage_event, damage_error;
789 XDamageQueryExtension(server.dsp, &damage_event, &damage_error);
790 x11_fd = ConnectionNumber(server.dsp);
791 XSync(server.dsp, False);
792
793 // sigset_t empty_mask;
794 // sigemptyset(&empty_mask);
795
796 while (1) {
797 if (panel_refresh) {
798 panel_refresh = 0;
799
800 for (i=0 ; i < nb_panel ; i++) {
801 panel = &panel1[i];
802
803 if (panel->is_hidden) {
804 XCopyArea(server.dsp, panel->hidden_pixmap, panel->main_win, server.gc, 0, 0, panel->hidden_width, panel->hidden_height, 0, 0);
805 XSetWindowBackgroundPixmap(server.dsp, panel->main_win, panel->hidden_pixmap);
806 }
807 else {
808 if (panel->temp_pmap) XFreePixmap(server.dsp, panel->temp_pmap);
809 panel->temp_pmap = XCreatePixmap(server.dsp, server.root_win, panel->area.width, panel->area.height, server.depth);
810 rendering(panel);
811 XCopyArea(server.dsp, panel->temp_pmap, panel->main_win, server.gc, 0, 0, panel->area.width, panel->area.height, 0, 0);
812 }
813 }
814 XFlush (server.dsp);
815
816 panel = (Panel*)systray.area.panel;
817 if (refresh_systray && panel && !panel->is_hidden) {
818 refresh_systray = 0;
819 // tint2 doen't draw systray icons. it just redraw background.
820 XSetWindowBackgroundPixmap (server.dsp, panel->main_win, panel->temp_pmap);
821 // force icon's refresh
822 refresh_systray_icon();
823 }
824 }
825
826 // thanks to AngryLlama for the timer
827 // Create a File Description Set containing x11_fd
828 FD_ZERO (&fdset);
829 FD_SET (x11_fd, &fdset);
830 update_next_timeout();
831 if (next_timeout.tv_sec >= 0 && next_timeout.tv_usec >= 0)
832 timeout = &next_timeout;
833 else
834 timeout = 0;
835
836 // Wait for X Event or a Timer
837 if (select(x11_fd+1, &fdset, 0, 0, timeout) > 0) {
838 while (XPending (server.dsp)) {
839 XNextEvent(server.dsp, &e);
840
841 panel = get_panel(e.xany.window);
842 if (panel && panel_autohide) {
843 if (e.type == EnterNotify)
844 autohide_trigger_show(panel);
845 else if (e.type == LeaveNotify)
846 autohide_trigger_hide(panel);
847 if (panel->is_hidden) {
848 if (e.type == ClientMessage && e.xclient.message_type == server.atom.XdndPosition) {
849 hidden_dnd = 1;
850 autohide_show(panel);
851 }
852 else
853 continue; // discard further processing of this event because the panel is not visible yet
854 }
855 else if (hidden_dnd && e.type == ClientMessage && e.xclient.message_type == server.atom.XdndLeave) {
856 hidden_dnd = 0;
857 autohide_hide(panel);
858 }
859 }
860
861 switch (e.type) {
862 case ButtonPress:
863 tooltip_hide(0);
864 event_button_press (&e);
865 break;
866
867 case ButtonRelease:
868 event_button_release(&e);
869 break;
870
871 case MotionNotify: {
872 unsigned int button_mask = Button1Mask | Button2Mask | Button3Mask | Button4Mask | Button5Mask;
873 if (e.xmotion.state & button_mask)
874 event_button_motion_notify (&e);
875
876 if (!g_tooltip.enabled) break;
877 Panel* panel = get_panel(e.xmotion.window);
878 Area* area = click_area(panel, e.xmotion.x, e.xmotion.y);
879 if (area->_get_tooltip_text)
880 tooltip_trigger_show(area, panel, &e);
881 else
882 tooltip_trigger_hide();
883 break;
884 }
885
886 case LeaveNotify:
887 if (g_tooltip.enabled)
888 tooltip_trigger_hide();
889 break;
890
891 case Expose:
892 event_expose(&e);
893 break;
894
895 case PropertyNotify:
896 event_property_notify(&e);
897 break;
898
899 case ConfigureNotify:
900 event_configure_notify (e.xconfigure.window);
901 break;
902
903 case ReparentNotify:
904 if (!systray_enabled)
905 break;
906 panel = (Panel*)systray.area.panel;
907 if (e.xany.window == panel->main_win) // reparented to us
908 break;
909 // FIXME: 'reparent to us' badly detected => disabled
910 break;
911 case UnmapNotify:
912 case DestroyNotify:
913 if (e.xany.window == server.composite_manager) {
914 // Stop real_transparency
915 signal_pending = SIGUSR1;
916 break;
917 }
918 if (e.xany.window == g_tooltip.window || !systray_enabled)
919 break;
920 for (it = systray.list_icons; it; it = g_slist_next(it)) {
921 if (((TrayWindow*)it->data)->tray_id == e.xany.window) {
922 remove_icon((TrayWindow*)it->data);
923 break;
924 }
925 }
926 break;
927
928 case ClientMessage:
929 ev = &e.xclient;
930 if (ev->data.l[1] == server.atom._NET_WM_CM_S0) {
931 if (ev->data.l[2] == None)
932 // Stop real_transparency
933 signal_pending = SIGUSR1;
934 else
935 // Start real_transparency
936 signal_pending = SIGUSR1;
937 }
938 if (systray_enabled && e.xclient.message_type == server.atom._NET_SYSTEM_TRAY_OPCODE && e.xclient.format == 32 && e.xclient.window == net_sel_win) {
939 net_message(&e.xclient);
940 }
941 else if (e.xclient.message_type == server.atom.XdndPosition) {
942 dnd_message(&e.xclient);
943 }
944 break;
945
946 default:
947 if (e.type == XDamageNotify+damage_event) {
948 // union needed to avoid strict-aliasing warnings by gcc
949 union { XEvent e; XDamageNotifyEvent de; } event_union = {.e=e};
950 TrayWindow *traywin;
951 GSList *l;
952 XDamageNotifyEvent* de = &event_union.de;
953 for (l = systray.list_icons; l ; l = l->next) {
954 traywin = (TrayWindow*)l->data;
955 if ( traywin->id == de->drawable ) {
956 systray_render_icon(traywin);
957 break;
958 }
959 }
960 }
961 }
962 }
963 }
964
965 callback_timeout_expired();
966
967 if (signal_pending) {
968 cleanup();
969 if (signal_pending == SIGUSR1) {
970 // restart tint2
971 // SIGUSR1 used when : user's signal, composite manager stop/start or xrandr
972 FD_CLR (x11_fd, &fdset); // not sure if needed
973 goto start;
974 }
975 else {
976 // SIGINT, SIGTERM, SIGHUP
977 return 0;
978 }
979 }
980 }
981 }
982
983
This page took 0.080065 seconds and 4 git commands to generate.