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