]> Dogcows Code - chaz/tint2/blob - src/tint.c
change systray code. fixed issue 91.
[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
41
42 void signal_handler(int sig)
43 {
44 // signal handler is light as it should be
45 signal_pending = sig;
46 }
47
48
49 void init ()
50 {
51 // Set signal handler
52 signal(SIGUSR1, signal_handler);
53 signal(SIGINT, signal_handler);
54 signal(SIGTERM, signal_handler);
55 signal(SIGHUP, signal_handler);
56
57 // set global data
58 memset(&server, 0, sizeof(Server_global));
59 memset(&systray, 0, sizeof(Systraybar));
60
61 server.dsp = XOpenDisplay (NULL);
62 if (!server.dsp) {
63 fprintf(stderr, "tint2 exit : could not open display.\n");
64 exit(0);
65 }
66 server_init_atoms ();
67 server.screen = DefaultScreen (server.dsp);
68 server.root_win = RootWindow(server.dsp, server.screen);
69 server.depth = DefaultDepth (server.dsp, server.screen);
70 server.visual = DefaultVisual (server.dsp, server.screen);
71 server.desktop = server_get_current_desktop ();
72 XGCValues gcv;
73 server.gc = XCreateGC (server.dsp, server.root_win, (unsigned long)0, &gcv) ;
74
75 XSetErrorHandler ((XErrorHandler) server_catch_error);
76
77 imlib_context_set_display (server.dsp);
78 imlib_context_set_visual (server.visual);
79 imlib_context_set_colormap (DefaultColormap (server.dsp, server.screen));
80
81 /* Catch events */
82 XSelectInput (server.dsp, server.root_win, PropertyChangeMask|StructureNotifyMask);
83
84 setlocale (LC_ALL, "");
85 }
86
87
88 void cleanup()
89 {
90 cleanup_panel();
91
92 if (time1_font_desc) pango_font_description_free(time1_font_desc);
93 if (time2_font_desc) pango_font_description_free(time2_font_desc);
94 if (time1_format) g_free(time1_format);
95 if (time2_format) g_free(time2_format);
96 #ifdef ENABLE_BATTERY
97 if (bat1_font_desc) pango_font_description_free(bat1_font_desc);
98 if (bat2_font_desc) pango_font_description_free(bat2_font_desc);
99 if (battery_low_cmd) g_free(battery_low_cmd);
100 if (path_energy_now) g_free(path_energy_now);
101 if (path_energy_full) g_free(path_energy_full);
102 if (path_current_now) g_free(path_current_now);
103 if (path_status) g_free(path_status);
104 #endif
105 if (clock_lclick_command) g_free(clock_lclick_command);
106 if (clock_rclick_command) g_free(clock_rclick_command);
107
108 if (server.monitor) free(server.monitor);
109 XFreeGC(server.dsp, server.gc);
110 XCloseDisplay(server.dsp);
111 }
112
113
114 void window_action (Task *tsk, int action)
115 {
116 switch (action) {
117 case CLOSE:
118 set_close (tsk->win);
119 break;
120 case TOGGLE:
121 set_active(tsk->win);
122 break;
123 case ICONIFY:
124 XIconifyWindow (server.dsp, tsk->win, server.screen);
125 break;
126 case TOGGLE_ICONIFY:
127 if (tsk == task_active) XIconifyWindow (server.dsp, tsk->win, server.screen);
128 else set_active (tsk->win);
129 break;
130 case SHADE:
131 window_toggle_shade (tsk->win);
132 break;
133 }
134 }
135
136
137 void event_button_press (XEvent *e)
138 {
139 Panel *panel = get_panel(e->xany.window);
140 if (!panel) return;
141
142 if (wm_menu) {
143 if ((e->xbutton.x < panel->area.paddingxlr) || (e->xbutton.x > panel->area.width-panel->area.paddingxlr) || (e->xbutton.y < panel->area.paddingy) || (e->xbutton.y > panel->area.paddingy+panel->g_taskbar.height)) {
144 // forward the click to the desktop window (thanks conky)
145 XUngrabPointer(server.dsp, e->xbutton.time);
146 e->xbutton.window = server.root_win;
147 // icewm doesn't open under the mouse.
148 // and xfce doesn't open at all.
149 //e->xbutton.x = e->xbutton.x_root;
150 //e->xbutton.y = e->xbutton.y_root;
151 //printf("**** %d, %d\n", e->xbutton.x, e->xbutton.y);
152 XSetInputFocus(server.dsp, e->xbutton.window, RevertToParent, e->xbutton.time);
153 XSendEvent(server.dsp, e->xbutton.window, False, ButtonPressMask, e);
154 return;
155 }
156 }
157
158 if (e->xbutton.button != 1) return;
159
160 if (panel_mode != MULTI_DESKTOP) {
161 // drag and drop disabled
162 XLowerWindow (server.dsp, panel->main_win);
163 return;
164 }
165
166 GSList *l0;
167 Taskbar *tskbar;
168 int x = e->xbutton.x;
169 for (l0 = panel->area.list; l0 ; l0 = l0->next) {
170 tskbar = l0->data;
171 if (!tskbar->area.on_screen) continue;
172 if (x >= tskbar->area.posx && x <= (tskbar->area.posx + tskbar->area.width))
173 break;
174 }
175
176 if (l0) {
177 Task *tsk;
178 for (l0 = tskbar->area.list; l0 ; l0 = l0->next) {
179 tsk = l0->data;
180 if (x >= tsk->area.posx && x <= (tsk->area.posx + tsk->area.width)) {
181 task_drag = tsk;
182 break;
183 }
184 }
185 }
186
187 XLowerWindow (server.dsp, panel->main_win);
188 }
189
190
191 void event_button_release (XEvent *e)
192 {
193 Panel *panel = get_panel(e->xany.window);
194 if (!panel) return;
195
196 if (wm_menu) {
197 if ((e->xbutton.x < panel->area.paddingxlr) || (e->xbutton.x > panel->area.width-panel->area.paddingxlr) || (e->xbutton.y < panel->area.paddingy) || (e->xbutton.y > panel->area.paddingy+panel->g_taskbar.height)) {
198 // forward the click to the desktop window (thanks conky)
199 e->xbutton.window = server.root_win;
200 XSendEvent(server.dsp, e->xbutton.window, False, ButtonReleaseMask, e);
201 return;
202 }
203 }
204
205 int action = TOGGLE_ICONIFY;
206 int x = e->xbutton.x;
207 //int y = e->xbutton.y; // unused
208 switch (e->xbutton.button) {
209 case 2:
210 action = mouse_middle;
211 break;
212 case 3:
213 action = mouse_right;
214 break;
215 case 4:
216 action = mouse_scroll_up;
217 break;
218 case 5:
219 action = mouse_scroll_down;
220 break;
221 }
222
223 // search taskbar
224 Taskbar *tskbar;
225 GSList *l0;
226 Clock clk = panel->clock;
227 if (clk.area.on_screen && x >= clk.area.posx && x <= (clk.area.posx + clk.area.width))
228 clock_action(e->xbutton.button);
229 else {
230 for (l0 = panel->area.list; l0 ; l0 = l0->next) {
231 tskbar = l0->data;
232 if (!tskbar->area.on_screen) continue;
233 if (x >= tskbar->area.posx && x <= (tskbar->area.posx + tskbar->area.width))
234 goto suite;
235 }
236 }
237
238 // TODO: check better solution to keep window below
239 XLowerWindow (server.dsp, panel->main_win);
240 task_drag = 0;
241 return;
242
243 suite:
244 // drag and drop task
245 if (task_drag) {
246 if (tskbar != task_drag->area.parent && action == TOGGLE_ICONIFY) {
247 if (task_drag->desktop != ALLDESKTOP && panel_mode == MULTI_DESKTOP) {
248 windows_set_desktop(task_drag->win, tskbar->desktop);
249 if (tskbar->desktop == server.desktop)
250 set_active(task_drag->win);
251 task_drag = 0;
252 }
253 return;
254 }
255 else task_drag = 0;
256 }
257
258 // switch desktop
259 if (panel_mode == MULTI_DESKTOP)
260 if (tskbar->desktop != server.desktop && action != CLOSE)
261 set_desktop (tskbar->desktop);
262
263 // action on task
264 Task *tsk;
265 GSList *l;
266 for (l = tskbar->area.list ; l ; l = l->next) {
267 tsk = l->data;
268 if (x >= tsk->area.posx && x <= (tsk->area.posx + tsk->area.width)) {
269 window_action (tsk, action);
270 break;
271 }
272 }
273
274 // to keep window below
275 XLowerWindow (server.dsp, panel->main_win);
276 }
277
278
279 void event_property_notify (XEvent *e)
280 {
281 int i, j;
282 Task *tsk;
283 Window win = e->xproperty.window;
284 Atom at = e->xproperty.atom;
285
286 if (win == server.root_win) {
287 if (!server.got_root_win) {
288 XSelectInput (server.dsp, server.root_win, PropertyChangeMask|StructureNotifyMask);
289 server.got_root_win = 1;
290 }
291
292 // Change number of desktops
293 else if (at == server.atom._NET_NUMBER_OF_DESKTOPS) {
294 server.nb_desktop = server_get_number_of_desktop ();
295 cleanup_taskbar();
296 init_taskbar();
297 visible_object();
298 for (i=0 ; i < nb_panel ; i++) {
299 panel1[i].area.resize = 1;
300 }
301 task_refresh_tasklist();
302 panel_refresh = 1;
303 }
304 // Change desktop
305 else if (at == server.atom._NET_CURRENT_DESKTOP) {
306 server.desktop = server_get_current_desktop ();
307 if (panel_mode != MULTI_DESKTOP) {
308 visible_object();
309 }
310 }
311 // Window list
312 else if (at == server.atom._NET_CLIENT_LIST) {
313 task_refresh_tasklist();
314 panel_refresh = 1;
315 }
316 // Change active
317 else if (at == server.atom._NET_ACTIVE_WINDOW) {
318 GSList *l0;
319 if (task_active) {
320 for (i=0 ; i < nb_panel ; i++) {
321 for (j=0 ; j < panel1[i].nb_desktop ; j++) {
322 for (l0 = panel1[i].taskbar[j].area.list; l0 ; l0 = l0->next) {
323 tsk = l0->data;
324 tsk->area.is_active = 0;
325 }
326 }
327 }
328 task_active = 0;
329 }
330 Window w1 = window_get_active ();
331 Task *t = task_get_task(w1);
332 if (!t) {
333 Window w2;
334 if (XGetTransientForHint(server.dsp, w1, &w2) != 0)
335 if (w2) t = task_get_task(w2);
336 }
337 if (task_urgent == t) {
338 init_precision();
339 task_urgent = 0;
340 }
341 if (t) {
342 for (i=0 ; i < nb_panel ; i++) {
343 for (j=0 ; j < panel1[i].nb_desktop ; j++) {
344 for (l0 = panel1[i].taskbar[j].area.list; l0 ; l0 = l0->next) {
345 tsk = l0->data;
346 if (tsk->win == t->win) {
347 tsk->area.is_active = 1;
348 //printf("active monitor %d, task %s\n", panel1[i].monitor, tsk->title);
349 }
350 }
351 }
352 }
353 task_active = t;
354 }
355 panel_refresh = 1;
356 }
357 else if (at == server.atom._XROOTPMAP_ID) {
358 // change Wallpaper
359 for (i=0 ; i < nb_panel ; i++) {
360 set_panel_background(&panel1[i]);
361 }
362 panel_refresh = 1;
363 }
364 }
365 else {
366 tsk = task_get_task (win);
367 if (!tsk) return;
368 //printf("atom root_win = %s, %s\n", XGetAtomName(server.dsp, at), tsk->title);
369
370 // Window title changed
371 if (at == server.atom._NET_WM_VISIBLE_NAME || at == server.atom._NET_WM_NAME || at == server.atom.WM_NAME) {
372 Task *tsk2;
373 GSList *l0;
374 get_title(tsk);
375 // changed other tsk->title
376 for (i=0 ; i < nb_panel ; i++) {
377 for (j=0 ; j < panel1[i].nb_desktop ; j++) {
378 for (l0 = panel1[i].taskbar[j].area.list; l0 ; l0 = l0->next) {
379 tsk2 = l0->data;
380 if (tsk->win == tsk2->win && tsk != tsk2) {
381 tsk2->title = tsk->title;
382 tsk2->area.redraw = 1;
383 }
384 }
385 }
386 }
387 panel_refresh = 1;
388 }
389 // Demand attention
390 else if (at == server.atom._NET_WM_STATE) {
391 if (window_is_urgent (win)) {
392 task_urgent = tsk;
393 time_precision = 1;
394 }
395 }
396 else if (at == server.atom.WM_STATE) {
397 // Iconic state
398 // TODO : try to delete following code
399 if (window_is_iconified (win)) {
400 if (task_active) {
401 if (task_active->win == tsk->win) {
402 Task *tsk2;
403 GSList *l0;
404 for (i=0 ; i < nb_panel ; i++) {
405 for (j=0 ; j < panel1[i].nb_desktop ; j++) {
406 for (l0 = panel1[i].taskbar[j].area.list; l0 ; l0 = l0->next) {
407 tsk2 = l0->data;
408 tsk2->area.is_active = 0;
409 }
410 }
411 }
412 task_active = 0;
413 }
414 }
415 }
416 }
417 // Window icon changed
418 else if (at == server.atom._NET_WM_ICON) {
419 get_icon(tsk);
420 Task *tsk2;
421 GSList *l0;
422 for (i=0 ; i < nb_panel ; i++) {
423 for (j=0 ; j < panel1[i].nb_desktop ; j++) {
424 for (l0 = panel1[i].taskbar[j].area.list; l0 ; l0 = l0->next) {
425 tsk2 = l0->data;
426 if (tsk->win == tsk2->win && tsk != tsk2) {
427 tsk2->icon_width = tsk->icon_width;
428 tsk2->icon_height = tsk->icon_height;
429 tsk2->icon_data = tsk->icon_data;
430 tsk2->area.redraw = 1;
431 }
432 }
433 }
434 }
435 panel_refresh = 1;
436 }
437 // Window desktop changed
438 else if (at == server.atom._NET_WM_DESKTOP) {
439 int desktop = window_get_desktop (win);
440 //printf(" Window desktop changed %d, %d\n", tsk->desktop, desktop);
441 // bug in windowmaker : send unecessary 'desktop changed' when focus changed
442 if (desktop != tsk->desktop) {
443 remove_task (tsk);
444 add_task (win);
445 panel_refresh = 1;
446 }
447 }
448
449 if (!server.got_root_win) server.root_win = RootWindow (server.dsp, server.screen);
450 }
451 }
452
453
454 void event_expose (XEvent *e)
455 {
456 Panel *panel;
457
458 panel = get_panel(e->xany.window);
459 if (!panel) return;
460 // TODO : one panel_refresh per panel ?
461 panel_refresh = 1;
462 }
463
464
465 void event_configure_notify (Window win)
466 {
467 // check 'win' move in systray
468 TrayWindow *traywin;
469 GSList *l;
470 for (l = systray.list_icons; l ; l = l->next) {
471 traywin = (TrayWindow*)l->data;
472 if (traywin->id == win) {
473 //printf("move tray %d\n", traywin->x);
474 XMoveResizeWindow(server.dsp, traywin->id, traywin->x, traywin->y, traywin->width, traywin->height);
475 panel_refresh = 1;
476 return;
477 }
478 }
479
480 // check 'win' move in another monitor
481 if (nb_panel == 1) return;
482 if (server.nb_monitor == 1) return;
483 Task *tsk = task_get_task (win);
484 if (!tsk) return;
485
486 Panel *p = tsk->area.panel;
487 if (p->monitor != window_get_monitor (win)) {
488 remove_task (tsk);
489 add_task (win);
490 if (win == window_get_active ()) {
491 Task *tsk = task_get_task (win);
492 tsk->area.is_active = 1;
493 task_active = tsk;
494 }
495 panel_refresh = 1;
496 }
497 }
498
499
500 void event_timer()
501 {
502 struct timeval stv;
503 int i;
504
505 if (gettimeofday(&stv, 0)) return;
506
507 if (abs(stv.tv_sec - time_clock.tv_sec) < time_precision) return;
508 time_clock.tv_sec = stv.tv_sec;
509 time_clock.tv_sec -= time_clock.tv_sec % time_precision;
510
511 // urgent task
512 if (task_urgent) {
513 task_urgent->area.is_active = !task_urgent->area.is_active;
514 task_urgent->area.redraw = 1;
515 }
516
517 // update battery
518 #ifdef ENABLE_BATTERY
519 if (panel1[0].battery.area.on_screen) {
520 update_battery();
521 for (i=0 ; i < nb_panel ; i++)
522 panel1[i].battery.area.resize = 1;
523 }
524 #endif
525
526 // update clock
527 if (time1_format) {
528 for (i=0 ; i < nb_panel ; i++)
529 panel1[i].clock.area.resize = 1;
530 }
531 panel_refresh = 1;
532 }
533
534
535 int main (int argc, char *argv[])
536 {
537 XEvent e;
538 fd_set fd;
539 int x11_fd, i, c;
540 struct timeval tv;
541 Panel *panel;
542 GSList *it;
543
544 c = getopt (argc, argv, "c:");
545 init ();
546
547 load_config:
548 i = 0;
549 init_config();
550 if (c != -1)
551 i = config_read_file (optarg);
552 if (!i)
553 i = config_read ();
554 if (!i) {
555 fprintf(stderr, "usage: tint2 [-c] <config_file>\n");
556 cleanup();
557 exit(1);
558 }
559 config_finish();
560
561 x11_fd = ConnectionNumber(server.dsp);
562 XSync(server.dsp, False);
563
564 while (1) {
565 // thanks to AngryLlama for the timer
566 // Create a File Description Set containing x11_fd
567 FD_ZERO (&fd);
568 FD_SET (x11_fd, &fd);
569
570 tv.tv_usec = 500000;
571 tv.tv_sec = 0;
572
573 // Wait for X Event or a Timer
574 if (select(x11_fd+1, &fd, 0, 0, &tv)) {
575 while (XPending (server.dsp)) {
576 XNextEvent(server.dsp, &e);
577
578 switch (e.type) {
579 case ButtonPress:
580 event_button_press (&e);
581 break;
582
583 case ButtonRelease:
584 event_button_release(&e);
585 break;
586
587 case Expose:
588 event_expose(&e);
589 break;
590
591 case PropertyNotify:
592 event_property_notify(&e);
593 break;
594
595 case ConfigureNotify:
596 if (e.xconfigure.window == server.root_win)
597 goto load_config;
598 else
599 event_configure_notify (e.xconfigure.window);
600 break;
601
602 case ReparentNotify:
603 if (e.xany.window == server.root_win) // reparented to us
604 break;
605 case UnmapNotify:
606 case DestroyNotify:
607 if (!systray.area.on_screen)
608 break;
609 for (it = systray.list_icons; it; it = g_slist_next(it)) {
610 if (((TrayWindow*)it->data)->id == e.xany.window) {
611 remove_icon((TrayWindow*)it->data);
612 break;
613 }
614 }
615 break;
616
617 case ClientMessage:
618 if (!systray.area.on_screen) break;
619 if (e.xclient.message_type == server.atom._NET_SYSTEM_TRAY_OPCODE && e.xclient.format == 32 && e.xclient.window == net_sel_win) {
620 net_message(&e.xclient);
621 }
622 break;
623 }
624 }
625 }
626 event_timer();
627
628 switch (signal_pending) {
629 case SIGUSR1:
630 goto load_config;
631 case SIGINT:
632 case SIGTERM:
633 case SIGHUP:
634 cleanup ();
635 return 0;
636 }
637
638 if (panel_refresh) {
639 panel_refresh = 0;
640
641 for (i=0 ; i < nb_panel ; i++) {
642 panel = &panel1[i];
643
644 if (panel == systray.area.panel)
645 XSetWindowBackgroundPixmap (server.dsp, panel->main_win, None);
646
647 if (panel->temp_pmap) XFreePixmap(server.dsp, panel->temp_pmap);
648 panel->temp_pmap = XCreatePixmap(server.dsp, server.root_win, panel->area.width, panel->area.height, server.depth);
649
650 refresh(&panel->area);
651 XCopyArea(server.dsp, panel->temp_pmap, panel->main_win, server.gc, 0, 0, panel->area.width, panel->area.height, 0, 0);
652
653 if (panel == systray.area.panel) {
654 // tint2 doen't draw systray icons. it just redraw background.
655 XSetWindowBackgroundPixmap (server.dsp, panel->main_win, panel->temp_pmap);
656 // force icon's refresh
657 refresh_systray();
658 }
659 }
660 XFlush (server.dsp);
661 }
662 }
663 }
664
665
This page took 0.06244 seconds and 4 git commands to generate.