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