]> Dogcows Code - chaz/tint2/blob - src/panel.c
killall -SIGUSR1 tint2 will reload config file. need more fixed.
[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 Task *task_urgent;
56 int tick_urgent;
57 int max_tick_urgent;
58
59 // panel's initial config
60 Panel panel_config;
61 // panels (one panel per monitor)
62 Panel *panel1 = NULL;
63 int nb_panel = 0;
64
65 Imlib_Image default_icon = NULL;
66
67
68
69 void init_panel()
70 {
71 int i, old_nb_panel;
72 Panel *new_panel, *p;
73
74 cleanup_taskbar();
75 for (i=0 ; i < nb_panel ; i++) {
76 free_area(&panel1[i].area);
77 if (panel1[i].temp_pmap) {
78 XFreePixmap(server.dsp, panel1[i].temp_pmap);
79 panel1[i].temp_pmap = 0;
80 }
81 }
82
83 // number of panels
84 old_nb_panel = nb_panel;
85 if (panel_config.monitor >= 0)
86 nb_panel = 1;
87 else
88 nb_panel = server.nb_monitor;
89
90 // freed old panels
91 for (i=nb_panel ; i < old_nb_panel ; i++) {
92 if (panel1[i].main_win) {
93 XDestroyWindow(server.dsp, panel1[i].main_win);
94 panel1[i].main_win = 0;
95 }
96 }
97
98 // alloc & init new panel
99 Window old_win;
100 if (nb_panel != old_nb_panel)
101 new_panel = realloc(panel1, nb_panel * sizeof(Panel));
102 else
103 new_panel = panel1;
104 for (i=0 ; i < nb_panel ; i++) {
105 old_win = new_panel[i].main_win;
106 memcpy(&new_panel[i], &panel_config, sizeof(Panel));
107 new_panel[i].main_win = old_win;
108 }
109
110 fprintf(stderr, "tint2 : nb monitor %d, nb desktop %d\n", nb_panel, server.nb_desktop);
111 for (i=0 ; i < nb_panel ; i++) {
112 p = &new_panel[i];
113
114 p->monitor = i;
115 p->area.parent = p;
116 p->area.panel = p;
117 p->area.on_screen = 1;
118 p->area.resize = 1;
119 p->area._resize = resize_panel;
120 p->g_taskbar.parent = p;
121 p->g_taskbar.panel = p;
122 p->g_task.area.panel = p;
123 init_panel_size_and_position(p);
124
125 // add childs
126 if (p->clock.area.on_screen) {
127 init_clock_panel(p);
128 p->area.list = g_slist_append(p->area.list, &p->clock);
129 }
130 #ifdef ENABLE_BATTERY
131 if (p->battery.area.on_screen) {
132 init_battery_panel(p);
133 p->area.list = g_slist_append(p->area.list, &p->battery);
134 }
135 #endif
136 // systray only on first panel
137 if (systray.area.on_screen && i == 0) {
138 init_systray_panel(p);
139 p->area.list = g_slist_append(p->area.list, &systray);
140 }
141
142 // full width mode
143 if (!p->initial_width) {
144 p->initial_width = 100;
145 p->pourcentx = 1;
146 }
147
148 if (i >= old_nb_panel) {
149 // new panel
150 //printf("new panel %d : %d, %d, %d, %d\n", i, p->posx, p->posy, p->area.width, p->area.height);
151
152 // Catch some events
153 long event_mask = ExposureMask|ButtonPressMask|ButtonReleaseMask;
154 if (g_tooltip.enabled)
155 event_mask |= PointerMotionMask|LeaveWindowMask;
156 XSetWindowAttributes att = { ParentRelative, 0L, 0, 0L, 0, 0, Always, 0L, 0L, False, event_mask, NoEventMask, False, 0, 0 };
157 if (p->main_win) XDestroyWindow(server.dsp, p->main_win);
158 p->main_win = XCreateWindow(server.dsp, server.root_win, p->posx, p->posy, p->area.width, p->area.height, 0, server.depth, InputOutput, CopyFromParent, CWEventMask, &att);
159
160 set_panel_properties(p);
161 set_panel_background(p);
162 XMapWindow (server.dsp, p->main_win);
163 }
164 else {
165 // old panel
166 //printf("old panel %d : %d, %d, %d, %d\n", i, p->posx, p->posy, p->area.width, p->area.height);
167 XMoveResizeWindow(server.dsp, p->main_win, p->posx, p->posy, p->area.width, p->area.height);
168 set_panel_background(p);
169 }
170 }
171
172 panel1 = new_panel;
173 panel_refresh = 1;
174 refresh_systray = 1;
175 init_taskbar();
176 visible_object();
177 task_refresh_tasklist();
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->initial_width / 100;
187 else
188 panel->area.width = panel->initial_width;
189 if (panel->pourcenty)
190 panel->area.height = (float)server.monitor[panel->monitor].height * panel->initial_height / 100;
191 else
192 panel->area.height = panel->initial_height;
193 if (panel->area.pix.border.rounded > panel->area.height/2)
194 panel->area.pix.border.rounded = panel->area.height/2;
195 }
196 else {
197 if (panel->pourcentx)
198 panel->area.height = (float)server.monitor[panel->monitor].height * panel->initial_width / 100;
199 else
200 panel->area.height = panel->initial_width;
201 if (panel->pourcenty)
202 panel->area.width = (float)server.monitor[panel->monitor].width * panel->initial_height / 100;
203 else
204 panel->area.width = panel->initial_height;
205 if (panel->area.pix.border.rounded > panel->area.width/2)
206 panel->area.pix.border.rounded = panel->area.width/2;
207 }
208
209 // panel position determined here
210 if (panel_position & LEFT) {
211 panel->posx = server.monitor[panel->monitor].x + panel->marginx;
212 }
213 else {
214 if (panel_position & RIGHT) {
215 panel->posx = server.monitor[panel->monitor].x + server.monitor[panel->monitor].width - panel->area.width - panel->marginx;
216 }
217 else {
218 if (panel_horizontal)
219 panel->posx = server.monitor[panel->monitor].x + ((server.monitor[panel->monitor].width - panel->area.width) / 2);
220 else
221 panel->posx = server.monitor[panel->monitor].x + panel->marginx;
222 }
223 }
224 if (panel_position & TOP) {
225 panel->posy = server.monitor[panel->monitor].y + panel->marginy;
226 }
227 else {
228 if (panel_position & BOTTOM) {
229 panel->posy = server.monitor[panel->monitor].y + server.monitor[panel->monitor].height - panel->area.height - panel->marginy;
230 }
231 else {
232 panel->posy = server.monitor[panel->monitor].y + ((server.monitor[panel->monitor].height - panel->area.height) / 2);
233 }
234 }
235 // printf("panel : posx %d, posy %d, width %d, height %d\n", panel->posx, panel->posy, panel->area.width, panel->area.height);
236 }
237
238
239 void cleanup_panel()
240 {
241 if (!panel1) return;
242
243 task_active = 0;
244 task_drag = 0;
245 task_urgent = 0;
246 cleanup_taskbar();
247
248 // font allocated once
249 if (panel1[0].g_task.font_desc) {
250 pango_font_description_free(panel1[0].g_task.font_desc);
251 panel1[0].g_task.font_desc = 0;
252 }
253
254 int i;
255 Panel *p;
256 for (i=0 ; i < nb_panel ; i++) {
257 p = &panel1[i];
258
259 free_area(&p->area);
260
261 if (p->temp_pmap) {
262 XFreePixmap(server.dsp, p->temp_pmap);
263 p->temp_pmap = 0;
264 }
265 if (p->main_win) {
266 XDestroyWindow(server.dsp, p->main_win);
267 p->main_win = 0;
268 }
269 }
270
271 if (panel1) free(panel1);
272 panel1 = 0;
273 }
274
275
276 void resize_panel(void *obj)
277 {
278 Panel *panel = (Panel*)obj;
279
280 if (panel_horizontal) {
281 int taskbar_width, modulo_width = 0;
282
283 taskbar_width = panel->area.width - (2 * panel->area.paddingxlr) - (2 * panel->area.pix.border.width);
284 if (panel->clock.area.on_screen && panel->clock.area.width)
285 taskbar_width -= (panel->clock.area.width + panel->area.paddingx);
286 #ifdef ENABLE_BATTERY
287 if (panel->battery.area.on_screen && panel->battery.area.width)
288 taskbar_width -= (panel->battery.area.width + panel->area.paddingx);
289 #endif
290 // TODO : systray only on first panel. search better implementation !
291 if (systray.area.on_screen && systray.area.width && panel == &panel1[0])
292 taskbar_width -= (systray.area.width + panel->area.paddingx);
293
294 if (panel_mode == MULTI_DESKTOP) {
295 int width = taskbar_width - ((panel->nb_desktop-1) * panel->area.paddingx);
296 taskbar_width = width / panel->nb_desktop;
297 modulo_width = width % panel->nb_desktop;
298 }
299
300 // change posx and width for all taskbar
301 int i, posx;
302 posx = panel->area.pix.border.width + panel->area.paddingxlr;
303 for (i=0 ; i < panel->nb_desktop ; i++) {
304 panel->taskbar[i].area.posx = posx;
305 panel->taskbar[i].area.width = taskbar_width;
306 panel->taskbar[i].area.resize = 1;
307 if (modulo_width) {
308 panel->taskbar[i].area.width++;
309 modulo_width--;
310 }
311 //printf("taskbar %d : posx %d, width, %d, posy %d\n", i, posx, panel->taskbar[i].area.width, posx + panel->taskbar[i].area.width);
312 if (panel_mode == MULTI_DESKTOP)
313 posx += panel->taskbar[i].area.width + panel->area.paddingx;
314 }
315 }
316 else {
317 int taskbar_height, modulo_height = 0;
318 int i, posy;
319
320 taskbar_height = panel->area.height - (2 * panel->area.paddingxlr) - (2 * panel->area.pix.border.width);
321 if (panel->clock.area.on_screen && panel->clock.area.height)
322 taskbar_height -= (panel->clock.area.height + panel->area.paddingx);
323 #ifdef ENABLE_BATTERY
324 if (panel->battery.area.on_screen && panel->battery.area.height)
325 taskbar_height -= (panel->battery.area.height + panel->area.paddingx);
326 #endif
327 // TODO : systray only on first panel. search better implementation !
328 if (systray.area.on_screen && systray.area.height && panel == &panel1[0])
329 taskbar_height -= (systray.area.height + panel->area.paddingx);
330
331 posy = panel->area.height - panel->area.pix.border.width - panel->area.paddingxlr - taskbar_height;
332 if (panel_mode == MULTI_DESKTOP) {
333 int height = taskbar_height - ((panel->nb_desktop-1) * panel->area.paddingx);
334 taskbar_height = height / panel->nb_desktop;
335 modulo_height = height % panel->nb_desktop;
336 }
337
338 // change posy and height for all taskbar
339 for (i=0 ; i < panel->nb_desktop ; i++) {
340 panel->taskbar[i].area.posy = posy;
341 panel->taskbar[i].area.height = taskbar_height;
342 panel->taskbar[i].area.resize = 1;
343 if (modulo_height) {
344 panel->taskbar[i].area.height++;
345 modulo_height--;
346 }
347 if (panel_mode == MULTI_DESKTOP)
348 posy += panel->taskbar[i].area.height + panel->area.paddingx;
349 }
350 }
351 }
352
353
354 void visible_object()
355 {
356 Panel *panel;
357 int i, j;
358
359 for (i=0 ; i < nb_panel ; i++) {
360 panel = &panel1[i];
361
362 Taskbar *taskbar;
363 for (j=0 ; j < panel->nb_desktop ; j++) {
364 taskbar = &panel->taskbar[j];
365 if (panel_mode != MULTI_DESKTOP && taskbar->desktop != server.desktop) {
366 // SINGLE_DESKTOP and not current desktop
367 taskbar->area.on_screen = 0;
368 }
369 else {
370 taskbar->area.on_screen = 1;
371 }
372 }
373 }
374 panel_refresh = 1;
375 }
376
377
378 void set_panel_properties(Panel *p)
379 {
380 XStoreName (server.dsp, p->main_win, "tint2");
381
382 gsize len;
383 gchar *name = g_locale_to_utf8("tint2", -1, NULL, &len, NULL);
384 if (name != NULL) {
385 XChangeProperty(server.dsp, p->main_win, server.atom._NET_WM_NAME, server.atom.UTF8_STRING, 8, PropModeReplace, (unsigned char *) name, (int) len);
386 g_free(name);
387 }
388
389 // Dock
390 long val = server.atom._NET_WM_WINDOW_TYPE_DOCK;
391 XChangeProperty (server.dsp, p->main_win, server.atom._NET_WM_WINDOW_TYPE, XA_ATOM, 32, PropModeReplace, (unsigned char *) &val, 1);
392
393 // Reserved space
394 long struts [12] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
395 if (panel_horizontal) {
396 if (panel_position & TOP) {
397 struts[2] = p->area.height + p->marginy;
398 struts[8] = p->posx;
399 // p->area.width - 1 allowed full screen on monitor 2
400 struts[9] = p->posx + p->area.width - 1;
401 }
402 else {
403 struts[3] = p->area.height + p->marginy;
404 struts[10] = p->posx;
405 // p->area.width - 1 allowed full screen on monitor 2
406 struts[11] = p->posx + p->area.width - 1;
407 }
408 }
409 else {
410 if (panel_position & LEFT) {
411 struts[0] = p->area.width + p->marginx;
412 struts[4] = p->posy;
413 // p->area.width - 1 allowed full screen on monitor 2
414 struts[5] = p->posy + p->area.height - 1;
415 }
416 else {
417 struts[1] = p->area.width + p->marginx;
418 struts[6] = p->posy;
419 // p->area.width - 1 allowed full screen on monitor 2
420 struts[7] = p->posy + p->area.height - 1;
421 }
422 }
423 // Old specification : fluxbox need _NET_WM_STRUT.
424 XChangeProperty (server.dsp, p->main_win, server.atom._NET_WM_STRUT, XA_CARDINAL, 32, PropModeReplace, (unsigned char *) &struts, 4);
425 XChangeProperty (server.dsp, p->main_win, server.atom._NET_WM_STRUT_PARTIAL, XA_CARDINAL, 32, PropModeReplace, (unsigned char *) &struts, 12);
426
427 // Sticky and below other window
428 val = 0xFFFFFFFF;
429 XChangeProperty (server.dsp, p->main_win, server.atom._NET_WM_DESKTOP, XA_CARDINAL, 32, PropModeReplace, (unsigned char *) &val, 1);
430 Atom state[4];
431 state[0] = server.atom._NET_WM_STATE_SKIP_PAGER;
432 state[1] = server.atom._NET_WM_STATE_SKIP_TASKBAR;
433 state[2] = server.atom._NET_WM_STATE_STICKY;
434 state[3] = server.atom._NET_WM_STATE_BELOW;
435 XChangeProperty (server.dsp, p->main_win, server.atom._NET_WM_STATE, XA_ATOM, 32, PropModeReplace, (unsigned char *) state, 4);
436
437 // Fixed position and non-resizable window
438 XSizeHints size_hints;
439 size_hints.flags = PPosition|PMinSize|PMaxSize;
440 size_hints.min_width = size_hints.max_width = p->area.width;
441 size_hints.min_height = size_hints.max_height = p->area.height;
442 XSetWMNormalHints(server.dsp, p->main_win, &size_hints);
443
444 // Unfocusable
445 XWMHints wmhints;
446 if (panel_dock) {
447 // TODO: Xdnd feature cannot be used in withdrawn state at the moment (at least GTK apps fail, qt seems to work)
448 wmhints.icon_window = wmhints.window_group = p->main_win;
449 wmhints.flags = StateHint | IconWindowHint;
450 wmhints.initial_state = WithdrawnState;
451 }
452 else {
453 wmhints.flags = InputHint;
454 wmhints.input = False;
455 }
456 XSetWMHints(server.dsp, p->main_win, &wmhints);
457
458 // Undecorated
459 long prop[5] = { 2, 0, 0, 0, 0 };
460 XChangeProperty(server.dsp, p->main_win, server.atom._MOTIF_WM_HINTS, server.atom._MOTIF_WM_HINTS, 32, PropModeReplace, (unsigned char *) prop, 5);
461
462 // XdndAware - Register for Xdnd events
463 int version=5;
464 XChangeProperty(server.dsp, p->main_win, server.atom.XdndAware, XA_ATOM, 32, PropModeReplace, (unsigned char*)&version, 1);
465 }
466
467
468 void set_panel_background(Panel *p)
469 {
470 get_root_pixmap();
471
472 if (p->area.pix.pmap) XFreePixmap (server.dsp, p->area.pix.pmap);
473 p->area.pix.pmap = XCreatePixmap (server.dsp, server.root_win, p->area.width, p->area.height, server.depth);
474
475 // copy background (server.root_pmap) in panel.area.pix.pmap
476 Window dummy;
477 int x, y;
478 XTranslateCoordinates(server.dsp, p->main_win, server.root_win, 0, 0, &x, &y, &dummy);
479 XSetTSOrigin(server.dsp, server.gc, -x, -y) ;
480 XFillRectangle(server.dsp, p->area.pix.pmap, server.gc, 0, 0, p->area.width, p->area.height);
481
482 // draw background panel
483 cairo_surface_t *cs;
484 cairo_t *c;
485 cs = cairo_xlib_surface_create (server.dsp, p->area.pix.pmap, server.visual, p->area.width, p->area.height);
486 c = cairo_create (cs);
487
488 draw_background(&p->area, c, 0);
489
490 cairo_destroy (c);
491 cairo_surface_destroy (cs);
492
493 // redraw panel's object
494 GSList *l0;
495 Area *a;
496 for (l0 = p->area.list; l0 ; l0 = l0->next) {
497 a = l0->data;
498 set_redraw(a);
499 }
500 }
501
502
503 Panel *get_panel(Window win)
504 {
505 int i;
506 for (i=0 ; i < nb_panel ; i++) {
507 if (panel1[i].main_win == win) {
508 return &panel1[i];
509 }
510 }
511 return 0;
512 }
513
This page took 0.060671 seconds and 5 git commands to generate.