]> Dogcows Code - chaz/tint2/blob - src/tint.c
revert r78, fixed issue 100
[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 // icewm doesn't open under the mouse.
145 // and xfce doesn't open at all.
146 //e->xbutton.x = e->xbutton.x_root;
147 //e->xbutton.y = e->xbutton.y_root;
148 //printf("**** %d, %d\n", e->xbutton.x, e->xbutton.y);
149 XSetInputFocus(server.dsp, e->xbutton.window, RevertToParent, e->xbutton.time);
150 XSendEvent(server.dsp, e->xbutton.window, False, ButtonPressMask, e);
151 return;
152 }
153 }
154
155 if (e->xbutton.button != 1) return;
156
157 if (panel_mode != MULTI_DESKTOP) {
158 // drag and drop disabled
159 XLowerWindow (server.dsp, panel->main_win);
160 return;
161 }
162
163 GSList *l0;
164 Taskbar *tskbar;
165 int x = e->xbutton.x;
166 for (l0 = panel->area.list; l0 ; l0 = l0->next) {
167 tskbar = l0->data;
168 if (!tskbar->area.on_screen) continue;
169 if (x >= tskbar->area.posx && x <= (tskbar->area.posx + tskbar->area.width))
170 break;
171 }
172
173 if (l0) {
174 Task *tsk;
175 for (l0 = tskbar->area.list; l0 ; l0 = l0->next) {
176 tsk = l0->data;
177 if (x >= tsk->area.posx && x <= (tsk->area.posx + tsk->area.width)) {
178 task_drag = tsk;
179 break;
180 }
181 }
182 }
183
184 XLowerWindow (server.dsp, panel->main_win);
185 }
186
187
188 void event_button_release (XEvent *e)
189 {
190 Panel *panel = get_panel(e->xany.window);
191 if (!panel) return;
192
193 if (wm_menu) {
194 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)) {
195 // forward the click to the desktop window (thanks conky)
196 e->xbutton.window = server.root_win;
197 XSendEvent(server.dsp, e->xbutton.window, False, ButtonReleaseMask, e);
198 return;
199 }
200 }
201
202 int action = TOGGLE_ICONIFY;
203 int x = e->xbutton.x;
204 //int y = e->xbutton.y; // unused
205 switch (e->xbutton.button) {
206 case 2:
207 action = mouse_middle;
208 break;
209 case 3:
210 action = mouse_right;
211 break;
212 case 4:
213 action = mouse_scroll_up;
214 break;
215 case 5:
216 action = mouse_scroll_down;
217 break;
218 }
219
220 // search taskbar
221 Taskbar *tskbar;
222 GSList *l0;
223 Clock clk = panel->clock;
224 if (clk.area.on_screen && x >= clk.area.posx && x <= (clk.area.posx + clk.area.width))
225 clock_action(e->xbutton.button);
226 else {
227 for (l0 = panel->area.list; l0 ; l0 = l0->next) {
228 tskbar = l0->data;
229 if (!tskbar->area.on_screen) continue;
230 if (x >= tskbar->area.posx && x <= (tskbar->area.posx + tskbar->area.width))
231 goto suite;
232 }
233 }
234
235 // TODO: check better solution to keep window below
236 XLowerWindow (server.dsp, panel->main_win);
237 task_drag = 0;
238 return;
239
240 suite:
241 // drag and drop task
242 if (task_drag) {
243 if (tskbar != task_drag->area.parent && action == TOGGLE_ICONIFY) {
244 if (task_drag->desktop != ALLDESKTOP && panel_mode == MULTI_DESKTOP) {
245 windows_set_desktop(task_drag->win, tskbar->desktop);
246 if (tskbar->desktop == server.desktop)
247 set_active(task_drag->win);
248 task_drag = 0;
249 }
250 return;
251 }
252 else task_drag = 0;
253 }
254
255 // switch desktop
256 if (panel_mode == MULTI_DESKTOP)
257 if (tskbar->desktop != server.desktop && action != CLOSE)
258 set_desktop (tskbar->desktop);
259
260 // action on task
261 Task *tsk;
262 GSList *l;
263 for (l = tskbar->area.list ; l ; l = l->next) {
264 tsk = l->data;
265 if (x >= tsk->area.posx && x <= (tsk->area.posx + tsk->area.width)) {
266 window_action (tsk, action);
267 break;
268 }
269 }
270
271 // to keep window below
272 XLowerWindow (server.dsp, panel->main_win);
273 }
274
275
276 void event_property_notify (XEvent *e)
277 {
278 int i, j;
279 Task *tsk;
280 Window win = e->xproperty.window;
281 Atom at = e->xproperty.atom;
282
283 if (win == server.root_win) {
284 if (!server.got_root_win) {
285 XSelectInput (server.dsp, server.root_win, PropertyChangeMask|StructureNotifyMask);
286 server.got_root_win = 1;
287 }
288
289 // Change number of desktops
290 else if (at == server.atom._NET_NUMBER_OF_DESKTOPS) {
291 server.nb_desktop = server_get_number_of_desktop ();
292 cleanup_taskbar();
293 init_taskbar();
294 visible_object();
295 for (i=0 ; i < nb_panel ; i++) {
296 panel1[i].area.resize = 1;
297 }
298 task_refresh_tasklist();
299 panel_refresh = 1;
300 }
301 // Change desktop
302 else if (at == server.atom._NET_CURRENT_DESKTOP) {
303 server.desktop = server_get_current_desktop ();
304 if (panel_mode != MULTI_DESKTOP) {
305 visible_object();
306 }
307 }
308 // Window list
309 else if (at == server.atom._NET_CLIENT_LIST) {
310 task_refresh_tasklist();
311 panel_refresh = 1;
312 }
313 // Change active
314 else if (at == server.atom._NET_ACTIVE_WINDOW) {
315 GSList *l0;
316 if (task_active) {
317 for (i=0 ; i < nb_panel ; i++) {
318 for (j=0 ; j < panel1[i].nb_desktop ; j++) {
319 for (l0 = panel1[i].taskbar[j].area.list; l0 ; l0 = l0->next) {
320 tsk = l0->data;
321 tsk->area.is_active = 0;
322 }
323 }
324 }
325 task_active = 0;
326 }
327 Window w1 = window_get_active ();
328 Task *t = task_get_task(w1);
329 if (!t) {
330 Window w2;
331 if (XGetTransientForHint(server.dsp, w1, &w2) != 0)
332 if (w2) t = task_get_task(w2);
333 }
334 if (task_urgent == t) {
335 init_precision();
336 task_urgent = 0;
337 }
338 if (t) {
339 for (i=0 ; i < nb_panel ; i++) {
340 for (j=0 ; j < panel1[i].nb_desktop ; j++) {
341 for (l0 = panel1[i].taskbar[j].area.list; l0 ; l0 = l0->next) {
342 tsk = l0->data;
343 if (tsk->win == t->win) {
344 tsk->area.is_active = 1;
345 //printf("active monitor %d, task %s\n", panel1[i].monitor, tsk->title);
346 }
347 }
348 }
349 }
350 task_active = t;
351 }
352 panel_refresh = 1;
353 }
354 else if (at == server.atom._XROOTPMAP_ID) {
355 // change Wallpaper
356 for (i=0 ; i < nb_panel ; i++) {
357 set_panel_background(&panel1[i]);
358 }
359 refresh_systray();
360 panel_refresh = 1;
361 }
362 }
363 else {
364 tsk = task_get_task (win);
365 if (!tsk) return;
366 //printf("atom root_win = %s, %s\n", XGetAtomName(server.dsp, at), tsk->title);
367
368 // Window title changed
369 if (at == server.atom._NET_WM_VISIBLE_NAME || at == server.atom._NET_WM_NAME || at == server.atom.WM_NAME) {
370 Task *tsk2;
371 GSList *l0;
372 get_title(tsk);
373 // changed other tsk->title
374 for (i=0 ; i < nb_panel ; i++) {
375 for (j=0 ; j < panel1[i].nb_desktop ; j++) {
376 for (l0 = panel1[i].taskbar[j].area.list; l0 ; l0 = l0->next) {
377 tsk2 = l0->data;
378 if (tsk->win == tsk2->win && tsk != tsk2) {
379 tsk2->title = tsk->title;
380 tsk2->area.redraw = 1;
381 }
382 }
383 }
384 }
385 panel_refresh = 1;
386 }
387 // Demand attention
388 else if (at == server.atom._NET_WM_STATE) {
389 if (window_is_urgent (win)) {
390 task_urgent = tsk;
391 time_precision = 1;
392 }
393 }
394 else if (at == server.atom.WM_STATE) {
395 // Iconic state
396 // TODO : try to delete following code
397 if (window_is_iconified (win)) {
398 if (task_active) {
399 if (task_active->win == tsk->win) {
400 Task *tsk2;
401 GSList *l0;
402 for (i=0 ; i < nb_panel ; i++) {
403 for (j=0 ; j < panel1[i].nb_desktop ; j++) {
404 for (l0 = panel1[i].taskbar[j].area.list; l0 ; l0 = l0->next) {
405 tsk2 = l0->data;
406 tsk2->area.is_active = 0;
407 }
408 }
409 }
410 task_active = 0;
411 }
412 }
413 }
414 }
415 // Window icon changed
416 else if (at == server.atom._NET_WM_ICON) {
417 get_icon(tsk);
418 Task *tsk2;
419 GSList *l0;
420 for (i=0 ; i < nb_panel ; i++) {
421 for (j=0 ; j < panel1[i].nb_desktop ; j++) {
422 for (l0 = panel1[i].taskbar[j].area.list; l0 ; l0 = l0->next) {
423 tsk2 = l0->data;
424 if (tsk->win == tsk2->win && tsk != tsk2) {
425 tsk2->icon_width = tsk->icon_width;
426 tsk2->icon_height = tsk->icon_height;
427 tsk2->icon_data = tsk->icon_data;
428 tsk2->area.redraw = 1;
429 }
430 }
431 }
432 }
433 panel_refresh = 1;
434 }
435 // Window desktop changed
436 else if (at == server.atom._NET_WM_DESKTOP) {
437 int desktop = window_get_desktop (win);
438 //printf(" Window desktop changed %d, %d\n", tsk->desktop, desktop);
439 // bug in windowmaker : send unecessary 'desktop changed' when focus changed
440 if (desktop != tsk->desktop) {
441 remove_task (tsk);
442 add_task (win);
443 panel_refresh = 1;
444 }
445 }
446
447 if (!server.got_root_win) server.root_win = RootWindow (server.dsp, server.screen);
448 }
449 }
450
451
452 void event_expose (XEvent *e)
453 {
454 Panel *panel;
455
456 panel = get_panel(e->xany.window);
457 if (!panel) return;
458 // TODO : one panel_refresh per panel ?
459 panel_refresh = 1;
460 }
461
462
463 void event_configure_notify (Window win)
464 {
465 // check 'win' move in systray
466 TrayWindow *traywin;
467 GSList *l;
468 for (l = systray.list_icons; l ; l = l->next) {
469 traywin = (TrayWindow*)l->data;
470 if (traywin->id == win) {
471 //printf("move tray %d\n", traywin->x);
472 XMoveResizeWindow(server.dsp, traywin->id, traywin->x, traywin->y, traywin->width, traywin->height);
473 panel_refresh = 1;
474 return;
475 }
476 }
477
478 // check 'win' move in another monitor
479 if (nb_panel == 1) return;
480 if (server.nb_monitor == 1) return;
481 Task *tsk = task_get_task (win);
482 if (!tsk) return;
483
484 Panel *p = tsk->area.panel;
485 if (p->monitor != window_get_monitor (win)) {
486 remove_task (tsk);
487 add_task (win);
488 if (win == window_get_active ()) {
489 Task *tsk = task_get_task (win);
490 tsk->area.is_active = 1;
491 task_active = tsk;
492 }
493 panel_refresh = 1;
494 }
495 }
496
497
498 void event_timer()
499 {
500 struct timeval stv;
501 int i;
502
503 if (gettimeofday(&stv, 0)) return;
504
505 if (abs(stv.tv_sec - time_clock.tv_sec) < time_precision) return;
506 time_clock.tv_sec = stv.tv_sec;
507 time_clock.tv_sec -= time_clock.tv_sec % time_precision;
508
509 // urgent task
510 if (task_urgent) {
511 task_urgent->area.is_active = !task_urgent->area.is_active;
512 task_urgent->area.redraw = 1;
513 }
514
515 // update battery
516 if (panel1[0].battery.area.on_screen) {
517 update_battery(&battery_state);
518 for (i=0 ; i < nb_panel ; i++)
519 panel1[i].battery.area.resize = 1;
520 }
521
522 // update clock
523 if (time1_format) {
524 for (i=0 ; i < nb_panel ; i++)
525 panel1[i].clock.area.resize = 1;
526 }
527 panel_refresh = 1;
528 }
529
530
531 int main (int argc, char *argv[])
532 {
533 XEvent e;
534 fd_set fd;
535 int x11_fd, i, c;
536 struct timeval tv;
537 Panel *panel;
538 GSList *it;
539
540 c = getopt (argc, argv, "c:");
541 init ();
542
543 load_config:
544 i = 0;
545 init_config();
546 if (c != -1)
547 i = config_read_file (optarg);
548 if (!i)
549 i = config_read ();
550 if (!i) {
551 fprintf(stderr, "usage: tint2 [-c] <config_file>\n");
552 cleanup();
553 exit(1);
554 }
555 config_finish();
556
557 x11_fd = ConnectionNumber(server.dsp);
558 XSync(server.dsp, False);
559
560 while (1) {
561 // thanks to AngryLlama for the timer
562 // Create a File Description Set containing x11_fd
563 FD_ZERO (&fd);
564 FD_SET (x11_fd, &fd);
565
566 tv.tv_usec = 500000;
567 tv.tv_sec = 0;
568
569 // Wait for X Event or a Timer
570 if (select(x11_fd+1, &fd, 0, 0, &tv)) {
571 while (XPending (server.dsp)) {
572 XNextEvent(server.dsp, &e);
573
574 switch (e.type) {
575 case ButtonPress:
576 event_button_press (&e);
577 break;
578
579 case ButtonRelease:
580 event_button_release(&e);
581 break;
582
583 case Expose:
584 event_expose(&e);
585 break;
586
587 case PropertyNotify:
588 event_property_notify(&e);
589 break;
590
591 case ConfigureNotify:
592 if (e.xconfigure.window == server.root_win)
593 goto load_config;
594 else
595 event_configure_notify (e.xconfigure.window);
596 break;
597
598 case ReparentNotify:
599 if (e.xany.window == server.root_win) // reparented to us
600 break;
601 case UnmapNotify:
602 case DestroyNotify:
603 if (!systray.area.on_screen)
604 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 if (e.xclient.message_type == server.atom._NET_SYSTEM_TRAY_OPCODE && e.xclient.format == 32 && e.xclient.window == net_sel_win) {
616 net_message(&e.xclient);
617 }
618 break;
619 }
620 }
621 }
622 event_timer();
623
624 switch (signal_pending) {
625 case SIGUSR1:
626 goto load_config;
627 case SIGINT:
628 case SIGTERM:
629 cleanup ();
630 return 0;
631 }
632
633 if (panel_refresh) {
634 panel_refresh = 0;
635
636 for (i=0 ; i < nb_panel ; i++) {
637 panel = &panel1[i];
638
639 if (panel->temp_pmap) XFreePixmap(server.dsp, panel->temp_pmap);
640 panel->temp_pmap = XCreatePixmap(server.dsp, server.root_win, panel->area.width, panel->area.height, server.depth);
641
642 refresh(&panel->area);
643 XCopyArea(server.dsp, panel->temp_pmap, panel->main_win, server.gc, 0, 0, panel->area.width, panel->area.height, 0, 0);
644 }
645 XFlush (server.dsp);
646 }
647 }
648 }
649
650
This page took 0.066395 seconds and 5 git commands to generate.