]> Dogcows Code - chaz/tint2/blob - src/panel.c
*add* clock supports timezones
[chaz/tint2] / src / panel.c
1 /**************************************************************************
2 *
3 * Copyright (C) 2008 Pål Staurland (staura@gmail.com)
4 * Modified (C) 2008 thierry lorthiois (lorthiois@bbsoft.fr)
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License version 2
8 * as published by the Free Software Foundation.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 **************************************************************************/
18
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <string.h>
22 #include <X11/Xlib.h>
23 #include <X11/Xutil.h>
24 #include <X11/Xatom.h>
25 #include <cairo.h>
26 #include <cairo-xlib.h>
27 #include <pango/pangocairo.h>
28
29 #include "server.h"
30 #include "window.h"
31 #include "task.h"
32 #include "panel.h"
33 #include "tooltip.h"
34
35
36 int signal_pending;
37 // --------------------------------------------------
38 // mouse events
39 int mouse_middle;
40 int mouse_right;
41 int mouse_scroll_up;
42 int mouse_scroll_down;
43 int mouse_tilt_left;
44 int mouse_tilt_right;
45
46 int panel_mode;
47 int wm_menu;
48 int panel_dock=0; // default not in the dock
49 int panel_position;
50 int panel_horizontal;
51 int panel_refresh;
52
53 Task *task_active;
54 Task *task_drag;
55 int max_tick_urgent;
56
57 // panel's initial config
58 Panel panel_config;
59 // panels (one panel per monitor)
60 Panel *panel1 = 0;
61 int nb_panel = 0;
62
63 Imlib_Image default_icon = NULL;
64
65
66
67 void init_panel()
68 {
69 int i, old_nb_panel;
70 Panel *new_panel, *p;
71
72 init_tooltip();
73 init_systray();
74 init_clock();
75 #ifdef ENABLE_BATTERY
76 init_battery();
77 #endif
78
79 cleanup_taskbar();
80 for (i=0 ; i < nb_panel ; i++) {
81 free_area(&panel1[i].area);
82 if (panel1[i].temp_pmap) {
83 XFreePixmap(server.dsp, panel1[i].temp_pmap);
84 panel1[i].temp_pmap = 0;
85 }
86 }
87
88 // number of panels
89 old_nb_panel = nb_panel;
90 if (panel_config.monitor >= 0)
91 nb_panel = 1;
92 else
93 nb_panel = server.nb_monitor;
94
95 // freed old panels
96 for (i=nb_panel ; i < old_nb_panel ; i++) {
97 if (panel1[i].main_win) {
98 XDestroyWindow(server.dsp, panel1[i].main_win);
99 panel1[i].main_win = 0;
100 }
101 }
102
103 // alloc & init new panel
104 Window old_win;
105 if (nb_panel != old_nb_panel)
106 new_panel = realloc(panel1, nb_panel * sizeof(Panel));
107 else
108 new_panel = panel1;
109 for (i=0 ; i < nb_panel ; i++) {
110 old_win = new_panel[i].main_win;
111 memcpy(&new_panel[i], &panel_config, sizeof(Panel));
112 new_panel[i].main_win = old_win;
113 }
114
115 fprintf(stderr, "tint2 : nb monitor %d, nb monitor used %d, nb desktop %d\n", server.nb_monitor, nb_panel, server.nb_desktop);
116 for (i=0 ; i < nb_panel ; i++) {
117 p = &new_panel[i];
118
119 if (panel_config.monitor < 0)
120 p->monitor = i;
121 p->area.parent = p;
122 p->area.panel = p;
123 p->area.on_screen = 1;
124 p->area.resize = 1;
125 p->area._resize = resize_panel;
126 p->g_taskbar.parent = p;
127 p->g_taskbar.panel = p;
128 p->g_task.area.panel = p;
129 init_panel_size_and_position(p);
130
131 // add childs
132 if (clock_enabled) {
133 init_clock_panel(p);
134 p->area.list = g_slist_append(p->area.list, &p->clock);
135 }
136 #ifdef ENABLE_BATTERY
137 if (battery_enabled) {
138 init_battery_panel(p);
139 p->area.list = g_slist_append(p->area.list, &p->battery);
140 }
141 #endif
142 // systray only on first panel
143 if (systray.area.on_screen && i == 0) {
144 init_systray_panel(p);
145 p->area.list = g_slist_append(p->area.list, &systray);
146 refresh_systray = 1;
147 }
148
149 if (i >= old_nb_panel) {
150 // new panel : catch some events
151 long event_mask = ExposureMask|ButtonPressMask|ButtonReleaseMask;
152 if (g_tooltip.enabled)
153 event_mask |= PointerMotionMask|LeaveWindowMask;
154 XSetWindowAttributes att = { .event_mask=event_mask, .colormap=server.colormap, .background_pixel=0, .border_pixel=0 };
155 unsigned long mask = CWEventMask|CWColormap|CWBackPixel|CWBorderPixel;
156 p->main_win = XCreateWindow(server.dsp, server.root_win, p->posx, p->posy, p->area.width, p->area.height, 0, server.depth, InputOutput, server.visual, mask, &att);
157 }
158 else {
159 // old panel
160 XMoveResizeWindow(server.dsp, p->main_win, p->posx, p->posy, p->area.width, p->area.height);
161 }
162
163 //printf("panel %d : %d, %d, %d, %d\n", i, p->posx, p->posy, p->area.width, p->area.height);
164 set_panel_properties(p);
165 set_panel_background(p);
166 if (i >= old_nb_panel) {
167 // map new panel
168 XMapWindow (server.dsp, p->main_win);
169 }
170 }
171
172 panel1 = new_panel;
173 panel_refresh = 1;
174 init_taskbar();
175 visible_object();
176 task_refresh_tasklist();
177 active_task();
178 }
179
180
181 void init_panel_size_and_position(Panel *panel)
182 {
183 // detect panel size
184 if (panel_horizontal) {
185 if (panel->pourcentx)
186 panel->area.width = (float)server.monitor[panel->monitor].width * panel->area.width / 100;
187 if (panel->pourcenty)
188 panel->area.height = (float)server.monitor[panel->monitor].height * panel->area.height / 100;
189 if (panel->area.pix.border.rounded > panel->area.height/2)
190 panel->area.pix.border.rounded = panel->area.height/2;
191 }
192 else {
193 int old_panel_height = panel->area.height;
194 if (panel->pourcentx)
195 panel->area.height = (float)server.monitor[panel->monitor].height * panel->area.width / 100;
196 else
197 panel->area.height = panel->area.width;
198 if (panel->pourcenty)
199 panel->area.width = (float)server.monitor[panel->monitor].width * old_panel_height / 100;
200 else
201 panel->area.width = old_panel_height;
202 if (panel->area.pix.border.rounded > panel->area.width/2)
203 panel->area.pix.border.rounded = panel->area.width/2;
204 }
205
206 // panel position determined here
207 if (panel_position & LEFT) {
208 panel->posx = server.monitor[panel->monitor].x + panel->marginx;
209 }
210 else {
211 if (panel_position & RIGHT) {
212 panel->posx = server.monitor[panel->monitor].x + server.monitor[panel->monitor].width - panel->area.width - panel->marginx;
213 }
214 else {
215 if (panel_horizontal)
216 panel->posx = server.monitor[panel->monitor].x + ((server.monitor[panel->monitor].width - panel->area.width) / 2);
217 else
218 panel->posx = server.monitor[panel->monitor].x + panel->marginx;
219 }
220 }
221 if (panel_position & TOP) {
222 panel->posy = server.monitor[panel->monitor].y + panel->marginy;
223 }
224 else {
225 if (panel_position & BOTTOM) {
226 panel->posy = server.monitor[panel->monitor].y + server.monitor[panel->monitor].height - panel->area.height - panel->marginy;
227 }
228 else {
229 panel->posy = server.monitor[panel->monitor].y + ((server.monitor[panel->monitor].height - panel->area.height) / 2);
230 }
231 }
232 // printf("panel : posx %d, posy %d, width %d, height %d\n", panel->posx, panel->posy, panel->area.width, panel->area.height);
233 }
234
235
236 void cleanup_panel()
237 {
238 if (!panel1) return;
239
240 task_active = 0;
241 task_drag = 0;
242
243 cleanup_taskbar();
244
245 int i;
246 Panel *p;
247 for (i=0 ; i < nb_panel ; i++) {
248 p = &panel1[i];
249
250 free_area(&p->area);
251
252 if (p->temp_pmap) {
253 XFreePixmap(server.dsp, p->temp_pmap);
254 p->temp_pmap = 0;
255 }
256 if (p->main_win) {
257 XDestroyWindow(server.dsp, p->main_win);
258 p->main_win = 0;
259 }
260 }
261
262 if (panel1) {
263 free(panel1);
264 panel1 = 0;
265 nb_panel = 0;
266 }
267
268 if (panel_config.g_task.font_desc) {
269 pango_font_description_free(panel_config.g_task.font_desc);
270 panel_config.g_task.font_desc = 0;
271 }
272 }
273
274
275 void resize_panel(void *obj)
276 {
277 Panel *panel = (Panel*)obj;
278
279 if (panel_horizontal) {
280 int taskbar_width, modulo_width = 0;
281
282 taskbar_width = panel->area.width - (2 * panel->area.paddingxlr) - (2 * panel->area.pix.border.width);
283 if (panel->clock.area.on_screen && panel->clock.area.width)
284 taskbar_width -= (panel->clock.area.width + panel->area.paddingx);
285 #ifdef ENABLE_BATTERY
286 if (panel->battery.area.on_screen && panel->battery.area.width)
287 taskbar_width -= (panel->battery.area.width + panel->area.paddingx);
288 #endif
289 // TODO : systray only on first panel. search better implementation !
290 if (systray.area.on_screen && systray.area.width && panel == &panel1[0])
291 taskbar_width -= (systray.area.width + panel->area.paddingx);
292
293 if (panel_mode == MULTI_DESKTOP) {
294 int width = taskbar_width - ((panel->nb_desktop-1) * panel->area.paddingx);
295 taskbar_width = width / panel->nb_desktop;
296 modulo_width = width % panel->nb_desktop;
297 }
298
299 // change posx and width for all taskbar
300 int i, posx;
301 posx = panel->area.pix.border.width + panel->area.paddingxlr;
302 for (i=0 ; i < panel->nb_desktop ; i++) {
303 panel->taskbar[i].area.posx = posx;
304 panel->taskbar[i].area.width = taskbar_width;
305 panel->taskbar[i].area.resize = 1;
306 if (modulo_width) {
307 panel->taskbar[i].area.width++;
308 modulo_width--;
309 }
310 //printf("taskbar %d : posx %d, width, %d, posy %d\n", i, posx, panel->taskbar[i].area.width, posx + panel->taskbar[i].area.width);
311 if (panel_mode == MULTI_DESKTOP)
312 posx += panel->taskbar[i].area.width + panel->area.paddingx;
313 }
314 }
315 else {
316 int taskbar_height, modulo_height = 0;
317 int i, posy;
318
319 taskbar_height = panel->area.height - (2 * panel->area.paddingxlr) - (2 * panel->area.pix.border.width);
320 if (panel->clock.area.on_screen && panel->clock.area.height)
321 taskbar_height -= (panel->clock.area.height + panel->area.paddingx);
322 #ifdef ENABLE_BATTERY
323 if (panel->battery.area.on_screen && panel->battery.area.height)
324 taskbar_height -= (panel->battery.area.height + panel->area.paddingx);
325 #endif
326 // TODO : systray only on first panel. search better implementation !
327 if (systray.area.on_screen && systray.area.height && panel == &panel1[0])
328 taskbar_height -= (systray.area.height + panel->area.paddingx);
329
330 posy = panel->area.height - panel->area.pix.border.width - panel->area.paddingxlr - taskbar_height;
331 if (panel_mode == MULTI_DESKTOP) {
332 int height = taskbar_height - ((panel->nb_desktop-1) * panel->area.paddingx);
333 taskbar_height = height / panel->nb_desktop;
334 modulo_height = height % panel->nb_desktop;
335 }
336
337 // change posy and height for all taskbar
338 for (i=0 ; i < panel->nb_desktop ; i++) {
339 panel->taskbar[i].area.posy = posy;
340 panel->taskbar[i].area.height = taskbar_height;
341 panel->taskbar[i].area.resize = 1;
342 if (modulo_height) {
343 panel->taskbar[i].area.height++;
344 modulo_height--;
345 }
346 if (panel_mode == MULTI_DESKTOP)
347 posy += panel->taskbar[i].area.height + panel->area.paddingx;
348 }
349 }
350 }
351
352
353 void visible_object()
354 {
355 Panel *panel;
356 int i, j;
357
358 for (i=0 ; i < nb_panel ; i++) {
359 panel = &panel1[i];
360
361 Taskbar *taskbar;
362 for (j=0 ; j < panel->nb_desktop ; j++) {
363 taskbar = &panel->taskbar[j];
364 if (panel_mode != MULTI_DESKTOP && taskbar->desktop != server.desktop) {
365 // SINGLE_DESKTOP and not current desktop
366 taskbar->area.on_screen = 0;
367 }
368 else {
369 taskbar->area.on_screen = 1;
370 }
371 }
372 }
373 panel_refresh = 1;
374 }
375
376
377 void set_panel_properties(Panel *p)
378 {
379 XStoreName (server.dsp, p->main_win, "tint2");
380
381 gsize len;
382 gchar *name = g_locale_to_utf8("tint2", -1, NULL, &len, NULL);
383 if (name != NULL) {
384 XChangeProperty(server.dsp, p->main_win, server.atom._NET_WM_NAME, server.atom.UTF8_STRING, 8, PropModeReplace, (unsigned char *) name, (int) len);
385 g_free(name);
386 }
387
388 // Dock
389 long val = server.atom._NET_WM_WINDOW_TYPE_DOCK;
390 XChangeProperty (server.dsp, p->main_win, server.atom._NET_WM_WINDOW_TYPE, XA_ATOM, 32, PropModeReplace, (unsigned char *) &val, 1);
391
392 // Sticky and below other window
393 val = 0xFFFFFFFF;
394 XChangeProperty (server.dsp, p->main_win, server.atom._NET_WM_DESKTOP, XA_CARDINAL, 32, PropModeReplace, (unsigned char *) &val, 1);
395 Atom state[4];
396 state[0] = server.atom._NET_WM_STATE_SKIP_PAGER;
397 state[1] = server.atom._NET_WM_STATE_SKIP_TASKBAR;
398 state[2] = server.atom._NET_WM_STATE_STICKY;
399 state[3] = server.atom._NET_WM_STATE_BELOW;
400 XChangeProperty (server.dsp, p->main_win, server.atom._NET_WM_STATE, XA_ATOM, 32, PropModeReplace, (unsigned char *) state, 4);
401
402 // Unfocusable
403 XWMHints wmhints;
404 if (panel_dock) {
405 // TODO: Xdnd feature cannot be used in withdrawn state at the moment (at least GTK apps fail, qt seems to work)
406 wmhints.icon_window = wmhints.window_group = p->main_win;
407 wmhints.flags = StateHint | IconWindowHint;
408 wmhints.initial_state = WithdrawnState;
409 }
410 else {
411 wmhints.flags = InputHint;
412 wmhints.input = False;
413 }
414 XSetWMHints(server.dsp, p->main_win, &wmhints);
415
416 // Undecorated
417 long prop[5] = { 2, 0, 0, 0, 0 };
418 XChangeProperty(server.dsp, p->main_win, server.atom._MOTIF_WM_HINTS, server.atom._MOTIF_WM_HINTS, 32, PropModeReplace, (unsigned char *) prop, 5);
419
420 // XdndAware - Register for Xdnd events
421 int version=5;
422 XChangeProperty(server.dsp, p->main_win, server.atom.XdndAware, XA_ATOM, 32, PropModeReplace, (unsigned char*)&version, 1);
423
424 // Reserved space
425 unsigned int d1, screen_width, screen_height;
426 Window d2;
427 int d3;
428 XGetGeometry(server.dsp, server.root_win, &d2, &d3, &d3, &screen_width, &screen_height, &d1, &d1);
429 Monitor monitor = server.monitor[p->monitor];
430 long struts [12] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
431 if (panel_horizontal) {
432 if (panel_position & TOP) {
433 struts[2] = p->area.height + p->marginy + monitor.y;
434 struts[8] = p->posx;
435 // p->area.width - 1 allowed full screen on monitor 2
436 struts[9] = p->posx + p->area.width - 1;
437 }
438 else {
439 struts[3] = p->area.height + p->marginy + screen_height - monitor.y - monitor.height;
440 struts[10] = p->posx;
441 // p->area.width - 1 allowed full screen on monitor 2
442 struts[11] = p->posx + p->area.width - 1;
443 }
444 }
445 else {
446 if (panel_position & LEFT) {
447 struts[0] = p->area.width + p->marginx + monitor.x;
448 struts[4] = p->posy;
449 // p->area.width - 1 allowed full screen on monitor 2
450 struts[5] = p->posy + p->area.height - 1;
451 }
452 else {
453 struts[1] = p->area.width + p->marginx + screen_width - monitor.x - monitor.width;
454 struts[6] = p->posy;
455 // p->area.width - 1 allowed full screen on monitor 2
456 struts[7] = p->posy + p->area.height - 1;
457 }
458 }
459 // Old specification : fluxbox need _NET_WM_STRUT.
460 XChangeProperty (server.dsp, p->main_win, server.atom._NET_WM_STRUT, XA_CARDINAL, 32, PropModeReplace, (unsigned char *) &struts, 4);
461 XChangeProperty (server.dsp, p->main_win, server.atom._NET_WM_STRUT_PARTIAL, XA_CARDINAL, 32, PropModeReplace, (unsigned char *) &struts, 12);
462
463 // Fixed position and non-resizable window
464 // Allow panel move and resize when tint2 reload config file
465 XSizeHints size_hints;
466 size_hints.flags = PPosition|PMinSize|PMaxSize;
467 size_hints.min_width = size_hints.max_width = p->area.width;
468 size_hints.min_height = size_hints.max_height = p->area.height;
469 XSetWMNormalHints(server.dsp, p->main_win, &size_hints);
470
471 // Set WM_CLASS
472 XClassHint* classhint = XAllocClassHint();
473 classhint->res_name = "tint2";
474 classhint->res_class = "Tint2";
475 XSetClassHint(server.dsp, p->main_win, classhint);
476 XFree(classhint);
477 }
478
479
480 void set_panel_background(Panel *p)
481 {
482 get_root_pixmap();
483
484 if (p->area.pix.pmap) XFreePixmap (server.dsp, p->area.pix.pmap);
485 p->area.pix.pmap = XCreatePixmap (server.dsp, server.root_win, p->area.width, p->area.height, server.depth);
486
487 // copy background (server.root_pmap) in panel.area.pix.pmap
488 Window dummy;
489 int x, y;
490 XTranslateCoordinates(server.dsp, p->main_win, server.root_win, 0, 0, &x, &y, &dummy);
491 XSetTSOrigin(server.dsp, server.gc, -x, -y) ;
492 XFillRectangle(server.dsp, p->area.pix.pmap, server.gc, 0, 0, p->area.width, p->area.height);
493
494 // draw background panel
495 cairo_surface_t *cs;
496 cairo_t *c;
497 cs = cairo_xlib_surface_create (server.dsp, p->area.pix.pmap, server.visual, p->area.width, p->area.height);
498 c = cairo_create (cs);
499
500 draw_background(&p->area, c, 0);
501
502 cairo_destroy (c);
503 cairo_surface_destroy (cs);
504
505 // redraw panel's object
506 GSList *l0;
507 Area *a;
508 for (l0 = p->area.list; l0 ; l0 = l0->next) {
509 a = l0->data;
510 set_redraw(a);
511 }
512 }
513
514
515 Panel *get_panel(Window win)
516 {
517 int i;
518 for (i=0 ; i < nb_panel ; i++) {
519 if (panel1[i].main_win == win) {
520 return &panel1[i];
521 }
522 }
523 return 0;
524 }
525
526
527 Taskbar *click_taskbar (Panel *panel, int x, int y)
528 {
529 Taskbar *tskbar;
530 int i;
531
532 if (panel_horizontal) {
533 for (i=0; i < panel->nb_desktop ; i++) {
534 tskbar = &panel->taskbar[i];
535 if (tskbar->area.on_screen && x >= tskbar->area.posx && x <= (tskbar->area.posx + tskbar->area.width))
536 return tskbar;
537 }
538 }
539 else {
540 for (i=0; i < panel->nb_desktop ; i++) {
541 tskbar = &panel->taskbar[i];
542 if (tskbar->area.on_screen && y >= tskbar->area.posy && y <= (tskbar->area.posy + tskbar->area.height))
543 return tskbar;
544 }
545 }
546 return NULL;
547 }
548
549
550 Task *click_task (Panel *panel, int x, int y)
551 {
552 GSList *l0;
553 Taskbar *tskbar;
554
555 if ( (tskbar = click_taskbar(panel, x, y)) ) {
556 if (panel_horizontal) {
557 Task *tsk;
558 for (l0 = tskbar->area.list; l0 ; l0 = l0->next) {
559 tsk = l0->data;
560 if (tsk->area.on_screen && x >= tsk->area.posx && x <= (tsk->area.posx + tsk->area.width)) {
561 return tsk;
562 }
563 }
564 }
565 else {
566 Task *tsk;
567 for (l0 = tskbar->area.list; l0 ; l0 = l0->next) {
568 tsk = l0->data;
569 if (tsk->area.on_screen && y >= tsk->area.posy && y <= (tsk->area.posy + tsk->area.height)) {
570 return tsk;
571 }
572 }
573 }
574 }
575 return NULL;
576 }
577
578
579 int click_padding(Panel *panel, int x, int y)
580 {
581 if (panel_horizontal) {
582 if (x < panel->area.paddingxlr || x > panel->area.width-panel->area.paddingxlr)
583 return 1;
584 }
585 else {
586 if (y < panel->area.paddingxlr || y > panel->area.height-panel->area.paddingxlr)
587 return 1;
588 }
589 return 0;
590 }
591
592
593 int click_clock(Panel *panel, int x, int y)
594 {
595 Clock clk = panel->clock;
596 if (panel_horizontal) {
597 if (clk.area.on_screen && x >= clk.area.posx && x <= (clk.area.posx + clk.area.width))
598 return TRUE;
599 } else {
600 if (clk.area.on_screen && y >= clk.area.posy && y <= (clk.area.posy + clk.area.height))
601 return TRUE;
602 }
603 return FALSE;
604 }
605
606
607 Area* click_area(Panel *panel, int x, int y)
608 {
609 Area* result = &panel->area;
610 Area* new_result = result;
611 do {
612 result = new_result;
613 GSList* it = result->list;
614 while (it) {
615 Area* a = it->data;
616 if (panel_horizontal) {
617 if (a->on_screen && x >= a->posx && x <= (a->posx + a->width)) {
618 new_result = a;
619 break;
620 }
621 } else {
622 if (a->on_screen && y >= a->posy && y <= (a->posy + a->height)) {
623 new_result = a;
624 break;
625 }
626 }
627 it = it->next;
628 }
629 } while (new_result != result);
630 return result;
631 }
This page took 0.064662 seconds and 5 git commands to generate.