]> Dogcows Code - chaz/tint2/blob - src/tint.c
fixed issue 56 : with windowmaker
[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
56 // set global data
57 memset(&server, 0, sizeof(Server_global));
58 memset(&systray, 0, sizeof(Systraybar));
59
60 server.dsp = XOpenDisplay (NULL);
61 if (!server.dsp) {
62 fprintf(stderr, "tint2 exit : could not open display.\n");
63 exit(0);
64 }
65 server_init_atoms ();
66 server.screen = DefaultScreen (server.dsp);
67 server.root_win = RootWindow(server.dsp, server.screen);
68 server.depth = DefaultDepth (server.dsp, server.screen);
69 server.visual = DefaultVisual (server.dsp, server.screen);
70 server.desktop = server_get_current_desktop ();
71 XGCValues gcv;
72 server.gc = XCreateGC (server.dsp, server.root_win, (unsigned long)0, &gcv) ;
73
74 XSetErrorHandler ((XErrorHandler) server_catch_error);
75
76 imlib_context_set_display (server.dsp);
77 imlib_context_set_visual (server.visual);
78 imlib_context_set_colormap (DefaultColormap (server.dsp, server.screen));
79
80 /* Catch events */
81 XSelectInput (server.dsp, server.root_win, PropertyChangeMask|StructureNotifyMask);
82
83 setlocale (LC_ALL, "");
84 }
85
86
87 void cleanup()
88 {
89 cleanup_panel();
90
91 if (time1_font_desc) pango_font_description_free(time1_font_desc);
92 if (time2_font_desc) pango_font_description_free(time2_font_desc);
93 if (time1_format) g_free(time1_format);
94 if (time2_format) g_free(time2_format);
95 if (bat1_font_desc) pango_font_description_free(bat1_font_desc);
96 if (bat2_font_desc) pango_font_description_free(bat2_font_desc);
97 if (battery_low_cmd) g_free(battery_low_cmd);
98 if (path_energy_now) g_free(path_energy_now);
99 if (path_energy_full) g_free(path_energy_full);
100 if (path_current_now) g_free(path_current_now);
101 if (path_status) g_free(path_status);
102 if (clock_lclick_command) g_free(clock_lclick_command);
103 if (clock_rclick_command) g_free(clock_rclick_command);
104
105 if (server.monitor) free(server.monitor);
106 XFreeGC(server.dsp, server.gc);
107 XCloseDisplay(server.dsp);
108 }
109
110
111 void window_action (Task *tsk, int action)
112 {
113 switch (action) {
114 case CLOSE:
115 set_close (tsk->win);
116 break;
117 case TOGGLE:
118 set_active(tsk->win);
119 break;
120 case ICONIFY:
121 XIconifyWindow (server.dsp, tsk->win, server.screen);
122 break;
123 case TOGGLE_ICONIFY:
124 if (tsk == task_active) XIconifyWindow (server.dsp, tsk->win, server.screen);
125 else set_active (tsk->win);
126 break;
127 case SHADE:
128 window_toggle_shade (tsk->win);
129 break;
130 }
131 }
132
133
134 void event_button_press (XEvent *e)
135 {
136 Panel *panel = get_panel(e->xany.window);
137 if (!panel) return;
138
139 if (wm_menu) {
140 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)) {
141 // forward the click to the desktop window (thanks conky)
142 XUngrabPointer(server.dsp, e->xbutton.time);
143 e->xbutton.window = server.root_win;
144 XSetInputFocus(server.dsp, e->xbutton.window, RevertToParent, e->xbutton.time);
145 XSendEvent(server.dsp, e->xbutton.window, False, ButtonPressMask, e);
146 return;
147 }
148 }
149
150 if (e->xbutton.button != 1) return;
151
152 if (panel_mode != MULTI_DESKTOP) {
153 // drag and drop disabled
154 XLowerWindow (server.dsp, panel->main_win);
155 return;
156 }
157
158 GSList *l0;
159 Taskbar *tskbar;
160 int x = e->xbutton.x;
161 for (l0 = panel->area.list; l0 ; l0 = l0->next) {
162 tskbar = l0->data;
163 if (!tskbar->area.on_screen) continue;
164 if (x >= tskbar->area.posx && x <= (tskbar->area.posx + tskbar->area.width))
165 break;
166 }
167
168 if (l0) {
169 Task *tsk;
170 for (l0 = tskbar->area.list; l0 ; l0 = l0->next) {
171 tsk = l0->data;
172 if (x >= tsk->area.posx && x <= (tsk->area.posx + tsk->area.width)) {
173 task_drag = tsk;
174 break;
175 }
176 }
177 }
178
179 XLowerWindow (server.dsp, panel->main_win);
180 }
181
182
183 void event_button_release (XEvent *e)
184 {
185 Panel *panel = get_panel(e->xany.window);
186 if (!panel) return;
187
188 if (wm_menu) {
189 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)) {
190 // forward the click to the desktop window (thanks conky)
191 e->xbutton.window = server.root_win;
192 XSendEvent(server.dsp, e->xbutton.window, False, ButtonReleaseMask, e);
193 return;
194 }
195 }
196
197 int action = TOGGLE_ICONIFY;
198 int x = e->xbutton.x;
199 //int y = e->xbutton.y; // unused
200 switch (e->xbutton.button) {
201 case 2:
202 action = mouse_middle;
203 break;
204 case 3:
205 action = mouse_right;
206 break;
207 case 4:
208 action = mouse_scroll_up;
209 break;
210 case 5:
211 action = mouse_scroll_down;
212 break;
213 }
214
215 // search taskbar
216 Taskbar *tskbar;
217 GSList *l0;
218 Clock clk = panel->clock;
219 if (clk.area.on_screen && x >= clk.area.posx && x <= (clk.area.posx + clk.area.width))
220 clock_action(e->xbutton.button);
221 else {
222 for (l0 = panel->area.list; l0 ; l0 = l0->next) {
223 tskbar = l0->data;
224 if (!tskbar->area.on_screen) continue;
225 if (x >= tskbar->area.posx && x <= (tskbar->area.posx + tskbar->area.width))
226 goto suite;
227 }
228 }
229
230 // TODO: check better solution to keep window below
231 XLowerWindow (server.dsp, panel->main_win);
232 task_drag = 0;
233 return;
234
235 suite:
236 // drag and drop task
237 if (task_drag) {
238 if (tskbar != task_drag->area.parent && action == TOGGLE_ICONIFY) {
239 if (task_drag->desktop != ALLDESKTOP && panel_mode == MULTI_DESKTOP) {
240 windows_set_desktop(task_drag->win, tskbar->desktop);
241 if (tskbar->desktop == server.desktop)
242 set_active(task_drag->win);
243 task_drag = 0;
244 }
245 return;
246 }
247 else task_drag = 0;
248 }
249
250 // switch desktop
251 if (panel_mode == MULTI_DESKTOP)
252 if (tskbar->desktop != server.desktop && action != CLOSE)
253 set_desktop (tskbar->desktop);
254
255 // action on task
256 Task *tsk;
257 GSList *l;
258 for (l = tskbar->area.list ; l ; l = l->next) {
259 tsk = l->data;
260 if (x >= tsk->area.posx && x <= (tsk->area.posx + tsk->area.width)) {
261 window_action (tsk, action);
262 break;
263 }
264 }
265
266 // to keep window below
267 XLowerWindow (server.dsp, panel->main_win);
268 }
269
270
271 void event_property_notify (XEvent *e)
272 {
273 int i, j;
274 Task *tsk;
275 Window win = e->xproperty.window;
276 Atom at = e->xproperty.atom;
277
278 if (win == server.root_win) {
279 if (!server.got_root_win) {
280 XSelectInput (server.dsp, server.root_win, PropertyChangeMask|StructureNotifyMask);
281 server.got_root_win = 1;
282 }
283
284 // Change number of desktops
285 else if (at == server.atom._NET_NUMBER_OF_DESKTOPS) {
286 server.nb_desktop = server_get_number_of_desktop ();
287 cleanup_taskbar();
288 init_taskbar();
289 visible_object();
290 for (i=0 ; i < nb_panel ; i++) {
291 panel1[i].area.resize = 1;
292 }
293 task_refresh_tasklist();
294 panel_refresh = 1;
295 }
296 // Change desktop
297 else if (at == server.atom._NET_CURRENT_DESKTOP) {
298 server.desktop = server_get_current_desktop ();
299 if (panel_mode != MULTI_DESKTOP) {
300 visible_object();
301 }
302 }
303 // Window list
304 else if (at == server.atom._NET_CLIENT_LIST) {
305 //printf("Window list\n");
306 task_refresh_tasklist();
307 panel_refresh = 1;
308 }
309 // Change active
310 else if (at == server.atom._NET_ACTIVE_WINDOW) {
311 //printf("Change active\n");
312 GSList *l0;
313 if (task_active) {
314 for (i=0 ; i < nb_panel ; i++) {
315 for (j=0 ; j < panel1[i].nb_desktop ; j++) {
316 for (l0 = panel1[i].taskbar[j].area.list; l0 ; l0 = l0->next) {
317 tsk = l0->data;
318 tsk->area.is_active = 0;
319 }
320 }
321 }
322 task_active = 0;
323 }
324 Window w1 = window_get_active ();
325 Task *t = task_get_task(w1);
326 if (!t) {
327 Window w2;
328 if (XGetTransientForHint(server.dsp, w1, &w2) != 0)
329 if (w2) t = task_get_task(w2);
330 }
331 if (task_urgent == t) {
332 init_precision();
333 task_urgent = 0;
334 }
335 if (t) {
336 for (i=0 ; i < nb_panel ; i++) {
337 for (j=0 ; j < panel1[i].nb_desktop ; j++) {
338 for (l0 = panel1[i].taskbar[j].area.list; l0 ; l0 = l0->next) {
339 tsk = l0->data;
340 if (tsk->win == t->win) {
341 tsk->area.is_active = 1;
342 //printf("active monitor %d, task %s\n", panel1[i].monitor, tsk->title);
343 }
344 }
345 }
346 }
347 task_active = t;
348 }
349 panel_refresh = 1;
350 }
351 // Wallpaper changed
352 else if (at == server.atom._XROOTPMAP_ID) {
353 for (i=0 ; i < nb_panel ; i++) {
354 set_panel_background(&panel1[i]);
355 }
356 panel_refresh = 1;
357 }
358 }
359 else {
360 tsk = task_get_task (win);
361 if (!tsk) return;
362 //printf("atom root_win = %s, %s\n", XGetAtomName(server.dsp, at), tsk->title);
363
364 // Window title changed
365 if (at == server.atom._NET_WM_VISIBLE_NAME || at == server.atom._NET_WM_NAME || at == server.atom.WM_NAME) {
366 Task *tsk2;
367 GSList *l0;
368 get_title(tsk);
369 // changed other tsk->title
370 for (i=0 ; i < nb_panel ; i++) {
371 for (j=0 ; j < panel1[i].nb_desktop ; j++) {
372 for (l0 = panel1[i].taskbar[j].area.list; l0 ; l0 = l0->next) {
373 tsk2 = l0->data;
374 if (tsk->win == tsk2->win && tsk != tsk2) {
375 tsk2->title = tsk->title;
376 tsk2->area.redraw = 1;
377 }
378 }
379 }
380 }
381 panel_refresh = 1;
382 }
383 // Demand attention
384 else if (at == server.atom._NET_WM_STATE) {
385 if (window_is_urgent (win)) {
386 task_urgent = tsk;
387 time_precision = 1;
388 }
389 }
390 else if (at == server.atom.WM_STATE) {
391 // Iconic state
392 // TODO : try to delete following code
393 if (window_is_iconified (win)) {
394 if (task_active) {
395 if (task_active->win == tsk->win) {
396 Task *tsk2;
397 GSList *l0;
398 for (i=0 ; i < nb_panel ; i++) {
399 for (j=0 ; j < panel1[i].nb_desktop ; j++) {
400 for (l0 = panel1[i].taskbar[j].area.list; l0 ; l0 = l0->next) {
401 tsk2 = l0->data;
402 tsk2->area.is_active = 0;
403 }
404 }
405 }
406 task_active = 0;
407 }
408 }
409 }
410 }
411 // Window icon changed
412 else if (at == server.atom._NET_WM_ICON) {
413 get_icon(tsk);
414 Task *tsk2;
415 GSList *l0;
416 for (i=0 ; i < nb_panel ; i++) {
417 for (j=0 ; j < panel1[i].nb_desktop ; j++) {
418 for (l0 = panel1[i].taskbar[j].area.list; l0 ; l0 = l0->next) {
419 tsk2 = l0->data;
420 if (tsk->win == tsk2->win && tsk != tsk2) {
421 tsk2->icon_width = tsk->icon_width;
422 tsk2->icon_height = tsk->icon_height;
423 tsk2->icon_data = tsk->icon_data;
424 tsk2->area.redraw = 1;
425 }
426 }
427 }
428 }
429 panel_refresh = 1;
430 }
431 // Window desktop changed
432 else if (at == server.atom._NET_WM_DESKTOP) {
433 int desktop = window_get_desktop (win);
434 //printf(" Window desktop changed %d, %d\n", tsk->desktop, desktop);
435 // bug in windowmaker : send unecessary 'desktop changed' when focus changed
436 if (desktop != tsk->desktop) {
437 remove_task (tsk);
438 add_task (win);
439 panel_refresh = 1;
440 }
441 }
442
443 if (!server.got_root_win) server.root_win = RootWindow (server.dsp, server.screen);
444 }
445 }
446
447
448 void event_expose (XEvent *e)
449 {
450 Panel *panel;
451
452 panel = get_panel(e->xany.window);
453 if (!panel) return;
454 /*
455 if (systray.area.on_screen) {
456 // force trayer refresh
457 //XClearWindow(tray_data.dpy, ti->mid_parent);
458 //x11_send_visibility(tray_data.dpy, dst, VisibilityFullyObscured);
459 //x11_send_visibility(tray_data.dpy, dst, VisibilityUnobscured);
460
461 GSList *l;
462 TrayWindow *traywin;
463 for (l = systray.list_icons; l ; l = l->next) {
464 traywin = (TrayWindow*)l->data;
465 // send Expose event
466 XClearArea(server.dsp, traywin->id, 0, 0, systray.area.width, systray.area.height, True);
467 //printf("expose %lx\n", traywin->id);
468 }
469
470 //x11_refresh_window(tray_data.dpy, ti->wid, ti->l.wnd_sz.x, ti->l.wnd_sz.y, True);
471 }
472 */
473 panel_refresh = 1;
474 //XCopyArea (server.dsp, panel->temp_pmap, panel->main_win, server.gc, 0, 0, panel->area.width, panel->area.height, 0, 0);
475
476 }
477
478
479 void event_configure_notify (Window win)
480 {
481 if (panel_mode != SINGLE_MONITOR) return;
482 if (server.nb_monitor == 1) return;
483
484 Task *tsk = task_get_task (win);
485 if (!tsk) return;
486
487 Panel *p = tsk->area.panel;
488 if (p->monitor != window_get_monitor (win)) {
489 // task on another monitor
490 remove_task (tsk);
491 add_task (win);
492 if (win == window_get_active ()) {
493 Task *tsk = task_get_task (win);
494 tsk->area.is_active = 1;
495 task_active = tsk;
496 }
497 panel_refresh = 1;
498 }
499 }
500
501
502 void event_timer()
503 {
504 struct timeval stv;
505 int i;
506
507 if (gettimeofday(&stv, 0)) return;
508
509 if (abs(stv.tv_sec - time_clock.tv_sec) < time_precision) return;
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 if (panel1[0].battery.area.on_screen) {
519 update_battery(&battery_state);
520 for (i=0 ; i < nb_panel ; i++)
521 panel1[i].battery.area.resize = 1;
522 }
523
524 // update clock
525 if (time1_format) {
526 time_clock.tv_sec = stv.tv_sec;
527 time_clock.tv_sec -= time_clock.tv_sec % time_precision;
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 UnmapNotify:
603 case DestroyNotify:
604 if (!systray.area.on_screen) break;
605 for (it = systray.list_icons; it; it = g_slist_next(it)) {
606 if (((TrayWindow*)it->data)->id == e.xany.window) {
607 remove_icon((TrayWindow*)it->data);
608 break;
609 }
610 }
611 break;
612
613 case ClientMessage:
614 if (!systray.area.on_screen) break;
615 //printf("ClientMessage\n");
616 if (e.xclient.message_type == server.atom._NET_SYSTEM_TRAY_OPCODE && e.xclient.format == 32 && e.xclient.window == net_sel_win) {
617 net_message(&e.xclient);
618 }
619 break;
620 }
621 }
622 }
623 else event_timer();
624
625 switch (signal_pending) {
626 case SIGUSR1:
627 goto load_config;
628 case SIGINT:
629 case SIGTERM:
630 cleanup ();
631 return 0;
632 }
633
634 if (panel_refresh) {
635 panel_refresh = 0;
636
637 for (i=0 ; i < nb_panel ; i++) {
638 panel = &panel1[i];
639
640 if (panel->temp_pmap) XFreePixmap(server.dsp, panel->temp_pmap);
641 panel->temp_pmap = XCreatePixmap(server.dsp, server.root_win, panel->area.width, panel->area.height, server.depth);
642
643 refresh(&panel->area);
644 XCopyArea(server.dsp, panel->temp_pmap, panel->main_win, server.gc, 0, 0, panel->area.width, panel->area.height, 0, 0);
645 }
646 XFlush (server.dsp);
647 }
648 }
649 }
650
651 // ****************************************************
652 // main_win doesn't include panel.area.paddingx, so we have WM capabilities on left and right.
653 // this feature is disabled !
654 //XCopyArea (server.dsp, server.pmap, p->main_win, server.gc, p->area.paddingxlr, 0, p->area.width-(2*p->area.paddingxlr), p->area.height, 0, 0);
655 //XCopyArea (server.dsp, panel.area.pix.pmap, server.root_win, server.gc_root, 0, 0, panel.area.width, panel.area.height, server.posx, server.posy);
656 //XCopyArea (server.dsp, server.pmap, panel.main_win, server.gc, panel.area.paddingxlr, 0, panel.area.width-(2*panel.area.paddingxlr), panel.area.height, 0, 0);
657
This page took 0.062978 seconds and 5 git commands to generate.