]> Dogcows Code - chaz/tint2/blob - src/panel.c
ee3e27df6bb6042eca449152538c98cf102ce962
[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 if (!server.gc) {
164 XGCValues gcv;
165 if (real_transparency)
166 server.gc = XCreateGC(server.dsp, p->main_win, 0, &gcv);
167 else
168 server.gc = XCreateGC (server.dsp, server.root_win, (unsigned long)0, &gcv);
169 }
170 //printf("panel %d : %d, %d, %d, %d\n", i, p->posx, p->posy, p->area.width, p->area.height);
171 set_panel_properties(p);
172 set_panel_background(p);
173 if (i >= old_nb_panel) {
174 // map new panel
175 XMapWindow (server.dsp, p->main_win);
176 }
177 }
178
179 panel1 = new_panel;
180 panel_refresh = 1;
181 init_taskbar();
182 visible_object();
183 task_refresh_tasklist();
184 active_task();
185 }
186
187
188 void init_panel_size_and_position(Panel *panel)
189 {
190 // detect panel size
191 if (panel_horizontal) {
192 if (panel->pourcentx)
193 panel->area.width = (float)server.monitor[panel->monitor].width * panel->area.width / 100;
194 if (panel->pourcenty)
195 panel->area.height = (float)server.monitor[panel->monitor].height * panel->area.height / 100;
196 if (panel->area.pix.border.rounded > panel->area.height/2)
197 panel->area.pix.border.rounded = panel->area.height/2;
198 }
199 else {
200 int old_panel_height = panel->area.height;
201 if (panel->pourcentx)
202 panel->area.height = (float)server.monitor[panel->monitor].height * panel->area.width / 100;
203 else
204 panel->area.height = panel->area.width;
205 if (panel->pourcenty)
206 panel->area.width = (float)server.monitor[panel->monitor].width * old_panel_height / 100;
207 else
208 panel->area.width = old_panel_height;
209 if (panel->area.pix.border.rounded > panel->area.width/2)
210 panel->area.pix.border.rounded = panel->area.width/2;
211 }
212
213 // panel position determined here
214 if (panel_position & LEFT) {
215 panel->posx = server.monitor[panel->monitor].x + panel->marginx;
216 }
217 else {
218 if (panel_position & RIGHT) {
219 panel->posx = server.monitor[panel->monitor].x + server.monitor[panel->monitor].width - panel->area.width - panel->marginx;
220 }
221 else {
222 if (panel_horizontal)
223 panel->posx = server.monitor[panel->monitor].x + ((server.monitor[panel->monitor].width - panel->area.width) / 2);
224 else
225 panel->posx = server.monitor[panel->monitor].x + panel->marginx;
226 }
227 }
228 if (panel_position & TOP) {
229 panel->posy = server.monitor[panel->monitor].y + panel->marginy;
230 }
231 else {
232 if (panel_position & BOTTOM) {
233 panel->posy = server.monitor[panel->monitor].y + server.monitor[panel->monitor].height - panel->area.height - panel->marginy;
234 }
235 else {
236 panel->posy = server.monitor[panel->monitor].y + ((server.monitor[panel->monitor].height - panel->area.height) / 2);
237 }
238 }
239 // printf("panel : posx %d, posy %d, width %d, height %d\n", panel->posx, panel->posy, panel->area.width, panel->area.height);
240 }
241
242
243 void cleanup_panel()
244 {
245 if (!panel1) return;
246
247 task_active = 0;
248 task_drag = 0;
249
250 cleanup_taskbar();
251
252 int i;
253 Panel *p;
254 for (i=0 ; i < nb_panel ; i++) {
255 p = &panel1[i];
256
257 free_area(&p->area);
258
259 if (p->temp_pmap) {
260 XFreePixmap(server.dsp, p->temp_pmap);
261 p->temp_pmap = 0;
262 }
263 if (p->main_win) {
264 XDestroyWindow(server.dsp, p->main_win);
265 p->main_win = 0;
266 }
267 }
268
269 if (panel1) {
270 free(panel1);
271 panel1 = 0;
272 nb_panel = 0;
273 }
274
275 if (panel_config.g_task.font_desc) {
276 pango_font_description_free(panel_config.g_task.font_desc);
277 panel_config.g_task.font_desc = 0;
278 }
279 }
280
281
282 void resize_panel(void *obj)
283 {
284 Panel *panel = (Panel*)obj;
285
286 if (panel_horizontal) {
287 int taskbar_width, modulo_width = 0;
288
289 taskbar_width = panel->area.width - (2 * panel->area.paddingxlr) - (2 * panel->area.pix.border.width);
290 if (panel->clock.area.on_screen && panel->clock.area.width)
291 taskbar_width -= (panel->clock.area.width + panel->area.paddingx);
292 #ifdef ENABLE_BATTERY
293 if (panel->battery.area.on_screen && panel->battery.area.width)
294 taskbar_width -= (panel->battery.area.width + panel->area.paddingx);
295 #endif
296 // TODO : systray only on first panel. search better implementation !
297 if (systray.area.on_screen && systray.area.width && panel == &panel1[0])
298 taskbar_width -= (systray.area.width + panel->area.paddingx);
299
300 if (panel_mode == MULTI_DESKTOP) {
301 int width = taskbar_width - ((panel->nb_desktop-1) * panel->area.paddingx);
302 taskbar_width = width / panel->nb_desktop;
303 modulo_width = width % panel->nb_desktop;
304 }
305
306 // change posx and width for all taskbar
307 int i, posx;
308 posx = panel->area.pix.border.width + panel->area.paddingxlr;
309 for (i=0 ; i < panel->nb_desktop ; i++) {
310 panel->taskbar[i].area.posx = posx;
311 panel->taskbar[i].area.width = taskbar_width;
312 panel->taskbar[i].area.resize = 1;
313 if (modulo_width) {
314 panel->taskbar[i].area.width++;
315 modulo_width--;
316 }
317 //printf("taskbar %d : posx %d, width, %d, posy %d\n", i, posx, panel->taskbar[i].area.width, posx + panel->taskbar[i].area.width);
318 if (panel_mode == MULTI_DESKTOP)
319 posx += panel->taskbar[i].area.width + panel->area.paddingx;
320 }
321 }
322 else {
323 int taskbar_height, modulo_height = 0;
324 int i, posy;
325
326 taskbar_height = panel->area.height - (2 * panel->area.paddingxlr) - (2 * panel->area.pix.border.width);
327 if (panel->clock.area.on_screen && panel->clock.area.height)
328 taskbar_height -= (panel->clock.area.height + panel->area.paddingx);
329 #ifdef ENABLE_BATTERY
330 if (panel->battery.area.on_screen && panel->battery.area.height)
331 taskbar_height -= (panel->battery.area.height + panel->area.paddingx);
332 #endif
333 // TODO : systray only on first panel. search better implementation !
334 if (systray.area.on_screen && systray.area.height && panel == &panel1[0])
335 taskbar_height -= (systray.area.height + panel->area.paddingx);
336
337 posy = panel->area.height - panel->area.pix.border.width - panel->area.paddingxlr - taskbar_height;
338 if (panel_mode == MULTI_DESKTOP) {
339 int height = taskbar_height - ((panel->nb_desktop-1) * panel->area.paddingx);
340 taskbar_height = height / panel->nb_desktop;
341 modulo_height = height % panel->nb_desktop;
342 }
343
344 // change posy and height for all taskbar
345 for (i=0 ; i < panel->nb_desktop ; i++) {
346 panel->taskbar[i].area.posy = posy;
347 panel->taskbar[i].area.height = taskbar_height;
348 panel->taskbar[i].area.resize = 1;
349 if (modulo_height) {
350 panel->taskbar[i].area.height++;
351 modulo_height--;
352 }
353 if (panel_mode == MULTI_DESKTOP)
354 posy += panel->taskbar[i].area.height + panel->area.paddingx;
355 }
356 }
357 }
358
359
360 void visible_object()
361 {
362 Panel *panel;
363 int i, j;
364
365 for (i=0 ; i < nb_panel ; i++) {
366 panel = &panel1[i];
367
368 Taskbar *taskbar;
369 for (j=0 ; j < panel->nb_desktop ; j++) {
370 taskbar = &panel->taskbar[j];
371 if (panel_mode != MULTI_DESKTOP && taskbar->desktop != server.desktop) {
372 // SINGLE_DESKTOP and not current desktop
373 taskbar->area.on_screen = 0;
374 }
375 else {
376 taskbar->area.on_screen = 1;
377 }
378 }
379 }
380 panel_refresh = 1;
381 }
382
383
384 void set_panel_properties(Panel *p)
385 {
386 XStoreName (server.dsp, p->main_win, "tint2");
387
388 gsize len;
389 gchar *name = g_locale_to_utf8("tint2", -1, NULL, &len, NULL);
390 if (name != NULL) {
391 XChangeProperty(server.dsp, p->main_win, server.atom._NET_WM_NAME, server.atom.UTF8_STRING, 8, PropModeReplace, (unsigned char *) name, (int) len);
392 g_free(name);
393 }
394
395 // Dock
396 long val = server.atom._NET_WM_WINDOW_TYPE_DOCK;
397 XChangeProperty (server.dsp, p->main_win, server.atom._NET_WM_WINDOW_TYPE, XA_ATOM, 32, PropModeReplace, (unsigned char *) &val, 1);
398
399 // Sticky and below other window
400 val = 0xFFFFFFFF;
401 XChangeProperty (server.dsp, p->main_win, server.atom._NET_WM_DESKTOP, XA_CARDINAL, 32, PropModeReplace, (unsigned char *) &val, 1);
402 Atom state[4];
403 state[0] = server.atom._NET_WM_STATE_SKIP_PAGER;
404 state[1] = server.atom._NET_WM_STATE_SKIP_TASKBAR;
405 state[2] = server.atom._NET_WM_STATE_STICKY;
406 state[3] = server.atom._NET_WM_STATE_BELOW;
407 XChangeProperty (server.dsp, p->main_win, server.atom._NET_WM_STATE, XA_ATOM, 32, PropModeReplace, (unsigned char *) state, 4);
408
409 // Unfocusable
410 XWMHints wmhints;
411 if (panel_dock) {
412 // TODO: Xdnd feature cannot be used in withdrawn state at the moment (at least GTK apps fail, qt seems to work)
413 wmhints.icon_window = wmhints.window_group = p->main_win;
414 wmhints.flags = StateHint | IconWindowHint;
415 wmhints.initial_state = WithdrawnState;
416 }
417 else {
418 wmhints.flags = InputHint;
419 wmhints.input = False;
420 }
421 XSetWMHints(server.dsp, p->main_win, &wmhints);
422
423 // Undecorated
424 long prop[5] = { 2, 0, 0, 0, 0 };
425 XChangeProperty(server.dsp, p->main_win, server.atom._MOTIF_WM_HINTS, server.atom._MOTIF_WM_HINTS, 32, PropModeReplace, (unsigned char *) prop, 5);
426
427 // XdndAware - Register for Xdnd events
428 int version=5;
429 XChangeProperty(server.dsp, p->main_win, server.atom.XdndAware, XA_ATOM, 32, PropModeReplace, (unsigned char*)&version, 1);
430
431 // Reserved space
432 unsigned int d1, screen_width, screen_height;
433 Window d2;
434 int d3;
435 XGetGeometry(server.dsp, server.root_win, &d2, &d3, &d3, &screen_width, &screen_height, &d1, &d1);
436 Monitor monitor = server.monitor[p->monitor];
437 long struts [12] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
438 if (panel_horizontal) {
439 if (panel_position & TOP) {
440 struts[2] = p->area.height + p->marginy + monitor.y;
441 struts[8] = p->posx;
442 // p->area.width - 1 allowed full screen on monitor 2
443 struts[9] = p->posx + p->area.width - 1;
444 }
445 else {
446 struts[3] = p->area.height + p->marginy + screen_height - monitor.y - monitor.height;
447 struts[10] = p->posx;
448 // p->area.width - 1 allowed full screen on monitor 2
449 struts[11] = p->posx + p->area.width - 1;
450 }
451 }
452 else {
453 if (panel_position & LEFT) {
454 struts[0] = p->area.width + p->marginx + monitor.x;
455 struts[4] = p->posy;
456 // p->area.width - 1 allowed full screen on monitor 2
457 struts[5] = p->posy + p->area.height - 1;
458 }
459 else {
460 struts[1] = p->area.width + p->marginx + screen_width - monitor.x - monitor.width;
461 struts[6] = p->posy;
462 // p->area.width - 1 allowed full screen on monitor 2
463 struts[7] = p->posy + p->area.height - 1;
464 }
465 }
466 // Old specification : fluxbox need _NET_WM_STRUT.
467 XChangeProperty (server.dsp, p->main_win, server.atom._NET_WM_STRUT, XA_CARDINAL, 32, PropModeReplace, (unsigned char *) &struts, 4);
468 XChangeProperty (server.dsp, p->main_win, server.atom._NET_WM_STRUT_PARTIAL, XA_CARDINAL, 32, PropModeReplace, (unsigned char *) &struts, 12);
469
470 // Fixed position and non-resizable window
471 // Allow panel move and resize when tint2 reload config file
472 XSizeHints size_hints;
473 size_hints.flags = PPosition|PMinSize|PMaxSize;
474 size_hints.min_width = size_hints.max_width = p->area.width;
475 size_hints.min_height = size_hints.max_height = p->area.height;
476 XSetWMNormalHints(server.dsp, p->main_win, &size_hints);
477
478 // Set WM_CLASS
479 XClassHint* classhint = XAllocClassHint();
480 classhint->res_name = "tint2";
481 classhint->res_class = "Tint2";
482 XSetClassHint(server.dsp, p->main_win, classhint);
483 XFree(classhint);
484 }
485
486
487 void set_panel_background(Panel *p)
488 {
489 if (p->area.pix.pmap) XFreePixmap (server.dsp, p->area.pix.pmap);
490 p->area.pix.pmap = XCreatePixmap (server.dsp, server.root_win, p->area.width, p->area.height, server.depth);
491
492 if (real_transparency) {
493 cairo_surface_t *tmp = cairo_xlib_surface_create (server.dsp, p->area.pix.pmap, server.visual, p->area.width, p->area.height);
494 cairo_t *cr = cairo_create(tmp);
495 cairo_set_operator (cr, CAIRO_OPERATOR_SOURCE);
496 cairo_rectangle(cr, 0, 0, p->area.width, p->area.height);
497 cairo_set_source_rgba(cr, 1, 1, 1, 0);
498 cairo_paint (cr);
499 cairo_destroy (cr);
500 cairo_surface_destroy (tmp);
501 return;
502 }
503
504 get_root_pixmap();
505
506 // copy background (server.root_pmap) in panel.area.pix.pmap
507 Window dummy;
508 int x, y;
509 XTranslateCoordinates(server.dsp, p->main_win, server.root_win, 0, 0, &x, &y, &dummy);
510 XSetTSOrigin(server.dsp, server.gc, -x, -y) ;
511 XFillRectangle(server.dsp, p->area.pix.pmap, server.gc, 0, 0, p->area.width, p->area.height);
512
513 // draw background panel
514 cairo_surface_t *cs;
515 cairo_t *c;
516 cs = cairo_xlib_surface_create (server.dsp, p->area.pix.pmap, server.visual, p->area.width, p->area.height);
517 c = cairo_create (cs);
518
519 draw_background(&p->area, c, 0);
520
521 cairo_destroy (c);
522 cairo_surface_destroy (cs);
523
524 // redraw panel's object
525 GSList *l0;
526 Area *a;
527 for (l0 = p->area.list; l0 ; l0 = l0->next) {
528 a = l0->data;
529 set_redraw(a);
530 }
531 }
532
533
534 Panel *get_panel(Window win)
535 {
536 int i;
537 for (i=0 ; i < nb_panel ; i++) {
538 if (panel1[i].main_win == win) {
539 return &panel1[i];
540 }
541 }
542 return 0;
543 }
544
545
546 Taskbar *click_taskbar (Panel *panel, int x, int y)
547 {
548 Taskbar *tskbar;
549 int i;
550
551 if (panel_horizontal) {
552 for (i=0; i < panel->nb_desktop ; i++) {
553 tskbar = &panel->taskbar[i];
554 if (tskbar->area.on_screen && x >= tskbar->area.posx && x <= (tskbar->area.posx + tskbar->area.width))
555 return tskbar;
556 }
557 }
558 else {
559 for (i=0; i < panel->nb_desktop ; i++) {
560 tskbar = &panel->taskbar[i];
561 if (tskbar->area.on_screen && y >= tskbar->area.posy && y <= (tskbar->area.posy + tskbar->area.height))
562 return tskbar;
563 }
564 }
565 return NULL;
566 }
567
568
569 Task *click_task (Panel *panel, int x, int y)
570 {
571 GSList *l0;
572 Taskbar *tskbar;
573
574 if ( (tskbar = click_taskbar(panel, x, y)) ) {
575 if (panel_horizontal) {
576 Task *tsk;
577 for (l0 = tskbar->area.list; l0 ; l0 = l0->next) {
578 tsk = l0->data;
579 if (tsk->area.on_screen && x >= tsk->area.posx && x <= (tsk->area.posx + tsk->area.width)) {
580 return tsk;
581 }
582 }
583 }
584 else {
585 Task *tsk;
586 for (l0 = tskbar->area.list; l0 ; l0 = l0->next) {
587 tsk = l0->data;
588 if (tsk->area.on_screen && y >= tsk->area.posy && y <= (tsk->area.posy + tsk->area.height)) {
589 return tsk;
590 }
591 }
592 }
593 }
594 return NULL;
595 }
596
597
598 int click_padding(Panel *panel, int x, int y)
599 {
600 if (panel_horizontal) {
601 if (x < panel->area.paddingxlr || x > panel->area.width-panel->area.paddingxlr)
602 return 1;
603 }
604 else {
605 if (y < panel->area.paddingxlr || y > panel->area.height-panel->area.paddingxlr)
606 return 1;
607 }
608 return 0;
609 }
610
611
612 int click_clock(Panel *panel, int x, int y)
613 {
614 Clock clk = panel->clock;
615 if (panel_horizontal) {
616 if (clk.area.on_screen && x >= clk.area.posx && x <= (clk.area.posx + clk.area.width))
617 return TRUE;
618 } else {
619 if (clk.area.on_screen && y >= clk.area.posy && y <= (clk.area.posy + clk.area.height))
620 return TRUE;
621 }
622 return FALSE;
623 }
624
625
626 Area* click_area(Panel *panel, int x, int y)
627 {
628 Area* result = &panel->area;
629 Area* new_result = result;
630 do {
631 result = new_result;
632 GSList* it = result->list;
633 while (it) {
634 Area* a = it->data;
635 if (panel_horizontal) {
636 if (a->on_screen && x >= a->posx && x <= (a->posx + a->width)) {
637 new_result = a;
638 break;
639 }
640 } else {
641 if (a->on_screen && y >= a->posy && y <= (a->posy + a->height)) {
642 new_result = a;
643 break;
644 }
645 }
646 it = it->next;
647 }
648 } while (new_result != result);
649 return result;
650 }
This page took 0.066356 seconds and 4 git commands to generate.