]> Dogcows Code - chaz/tint2/blob - src/panel.c
mem leak
[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 init_tooltip();
75 init_systray();
76 init_clock();
77 #ifdef ENABLE_BATTERY
78 init_battery();
79 #endif
80
81 cleanup_taskbar();
82 for (i=0 ; i < nb_panel ; i++) {
83 free_area(&panel1[i].area);
84 if (panel1[i].temp_pmap) {
85 XFreePixmap(server.dsp, panel1[i].temp_pmap);
86 panel1[i].temp_pmap = 0;
87 }
88 }
89
90 // number of panels
91 old_nb_panel = nb_panel;
92 if (panel_config.monitor >= 0)
93 nb_panel = 1;
94 else
95 nb_panel = server.nb_monitor;
96
97 // freed old panels
98 for (i=nb_panel ; i < old_nb_panel ; i++) {
99 if (panel1[i].main_win) {
100 XDestroyWindow(server.dsp, panel1[i].main_win);
101 panel1[i].main_win = 0;
102 }
103 }
104
105 // alloc & init new panel
106 Window old_win;
107 if (nb_panel != old_nb_panel)
108 new_panel = realloc(panel1, nb_panel * sizeof(Panel));
109 else
110 new_panel = panel1;
111 for (i=0 ; i < nb_panel ; i++) {
112 old_win = new_panel[i].main_win;
113 memcpy(&new_panel[i], &panel_config, sizeof(Panel));
114 new_panel[i].main_win = old_win;
115 }
116
117 fprintf(stderr, "tint2 : nb monitor %d, nb desktop %d\n", nb_panel, server.nb_desktop);
118 for (i=0 ; i < nb_panel ; i++) {
119 p = &new_panel[i];
120
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 = { ParentRelative, 0L, 0, 0L, 0, 0, Always, 0L, 0L, False, event_mask, NoEventMask, False, 0, 0 };
156 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);
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 task_urgent = 0;
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) free(panel1);
263 panel1 = 0;
264 }
265
266
267 void resize_panel(void *obj)
268 {
269 Panel *panel = (Panel*)obj;
270
271 if (panel_horizontal) {
272 int taskbar_width, modulo_width = 0;
273
274 taskbar_width = panel->area.width - (2 * panel->area.paddingxlr) - (2 * panel->area.pix.border.width);
275 if (panel->clock.area.on_screen && panel->clock.area.width)
276 taskbar_width -= (panel->clock.area.width + panel->area.paddingx);
277 #ifdef ENABLE_BATTERY
278 if (panel->battery.area.on_screen && panel->battery.area.width)
279 taskbar_width -= (panel->battery.area.width + panel->area.paddingx);
280 #endif
281 // TODO : systray only on first panel. search better implementation !
282 if (systray.area.on_screen && systray.area.width && panel == &panel1[0])
283 taskbar_width -= (systray.area.width + panel->area.paddingx);
284
285 if (panel_mode == MULTI_DESKTOP) {
286 int width = taskbar_width - ((panel->nb_desktop-1) * panel->area.paddingx);
287 taskbar_width = width / panel->nb_desktop;
288 modulo_width = width % panel->nb_desktop;
289 }
290
291 // change posx and width for all taskbar
292 int i, posx;
293 posx = panel->area.pix.border.width + panel->area.paddingxlr;
294 for (i=0 ; i < panel->nb_desktop ; i++) {
295 panel->taskbar[i].area.posx = posx;
296 panel->taskbar[i].area.width = taskbar_width;
297 panel->taskbar[i].area.resize = 1;
298 if (modulo_width) {
299 panel->taskbar[i].area.width++;
300 modulo_width--;
301 }
302 //printf("taskbar %d : posx %d, width, %d, posy %d\n", i, posx, panel->taskbar[i].area.width, posx + panel->taskbar[i].area.width);
303 if (panel_mode == MULTI_DESKTOP)
304 posx += panel->taskbar[i].area.width + panel->area.paddingx;
305 }
306 }
307 else {
308 int taskbar_height, modulo_height = 0;
309 int i, posy;
310
311 taskbar_height = panel->area.height - (2 * panel->area.paddingxlr) - (2 * panel->area.pix.border.width);
312 if (panel->clock.area.on_screen && panel->clock.area.height)
313 taskbar_height -= (panel->clock.area.height + panel->area.paddingx);
314 #ifdef ENABLE_BATTERY
315 if (panel->battery.area.on_screen && panel->battery.area.height)
316 taskbar_height -= (panel->battery.area.height + panel->area.paddingx);
317 #endif
318 // TODO : systray only on first panel. search better implementation !
319 if (systray.area.on_screen && systray.area.height && panel == &panel1[0])
320 taskbar_height -= (systray.area.height + panel->area.paddingx);
321
322 posy = panel->area.height - panel->area.pix.border.width - panel->area.paddingxlr - taskbar_height;
323 if (panel_mode == MULTI_DESKTOP) {
324 int height = taskbar_height - ((panel->nb_desktop-1) * panel->area.paddingx);
325 taskbar_height = height / panel->nb_desktop;
326 modulo_height = height % panel->nb_desktop;
327 }
328
329 // change posy and height for all taskbar
330 for (i=0 ; i < panel->nb_desktop ; i++) {
331 panel->taskbar[i].area.posy = posy;
332 panel->taskbar[i].area.height = taskbar_height;
333 panel->taskbar[i].area.resize = 1;
334 if (modulo_height) {
335 panel->taskbar[i].area.height++;
336 modulo_height--;
337 }
338 if (panel_mode == MULTI_DESKTOP)
339 posy += panel->taskbar[i].area.height + panel->area.paddingx;
340 }
341 }
342 }
343
344
345 void visible_object()
346 {
347 Panel *panel;
348 int i, j;
349
350 for (i=0 ; i < nb_panel ; i++) {
351 panel = &panel1[i];
352
353 Taskbar *taskbar;
354 for (j=0 ; j < panel->nb_desktop ; j++) {
355 taskbar = &panel->taskbar[j];
356 if (panel_mode != MULTI_DESKTOP && taskbar->desktop != server.desktop) {
357 // SINGLE_DESKTOP and not current desktop
358 taskbar->area.on_screen = 0;
359 }
360 else {
361 taskbar->area.on_screen = 1;
362 }
363 }
364 }
365 panel_refresh = 1;
366 }
367
368
369 void set_panel_properties(Panel *p)
370 {
371 XStoreName (server.dsp, p->main_win, "tint2");
372
373 gsize len;
374 gchar *name = g_locale_to_utf8("tint2", -1, NULL, &len, NULL);
375 if (name != NULL) {
376 XChangeProperty(server.dsp, p->main_win, server.atom._NET_WM_NAME, server.atom.UTF8_STRING, 8, PropModeReplace, (unsigned char *) name, (int) len);
377 g_free(name);
378 }
379
380 // Dock
381 long val = server.atom._NET_WM_WINDOW_TYPE_DOCK;
382 XChangeProperty (server.dsp, p->main_win, server.atom._NET_WM_WINDOW_TYPE, XA_ATOM, 32, PropModeReplace, (unsigned char *) &val, 1);
383
384 // Sticky and below other window
385 val = 0xFFFFFFFF;
386 XChangeProperty (server.dsp, p->main_win, server.atom._NET_WM_DESKTOP, XA_CARDINAL, 32, PropModeReplace, (unsigned char *) &val, 1);
387 Atom state[4];
388 state[0] = server.atom._NET_WM_STATE_SKIP_PAGER;
389 state[1] = server.atom._NET_WM_STATE_SKIP_TASKBAR;
390 state[2] = server.atom._NET_WM_STATE_STICKY;
391 state[3] = server.atom._NET_WM_STATE_BELOW;
392 XChangeProperty (server.dsp, p->main_win, server.atom._NET_WM_STATE, XA_ATOM, 32, PropModeReplace, (unsigned char *) state, 4);
393
394 // Unfocusable
395 XWMHints wmhints;
396 if (panel_dock) {
397 // TODO: Xdnd feature cannot be used in withdrawn state at the moment (at least GTK apps fail, qt seems to work)
398 wmhints.icon_window = wmhints.window_group = p->main_win;
399 wmhints.flags = StateHint | IconWindowHint;
400 wmhints.initial_state = WithdrawnState;
401 }
402 else {
403 wmhints.flags = InputHint;
404 wmhints.input = False;
405 }
406 XSetWMHints(server.dsp, p->main_win, &wmhints);
407
408 // Undecorated
409 long prop[5] = { 2, 0, 0, 0, 0 };
410 XChangeProperty(server.dsp, p->main_win, server.atom._MOTIF_WM_HINTS, server.atom._MOTIF_WM_HINTS, 32, PropModeReplace, (unsigned char *) prop, 5);
411
412 // XdndAware - Register for Xdnd events
413 int version=5;
414 XChangeProperty(server.dsp, p->main_win, server.atom.XdndAware, XA_ATOM, 32, PropModeReplace, (unsigned char*)&version, 1);
415
416 // Reserved space
417 long struts [12] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
418 if (panel_horizontal) {
419 if (panel_position & TOP) {
420 struts[2] = p->area.height + p->marginy;
421 struts[8] = p->posx;
422 // p->area.width - 1 allowed full screen on monitor 2
423 struts[9] = p->posx + p->area.width - 1;
424 }
425 else {
426 struts[3] = p->area.height + p->marginy;
427 struts[10] = p->posx;
428 // p->area.width - 1 allowed full screen on monitor 2
429 struts[11] = p->posx + p->area.width - 1;
430 }
431 }
432 else {
433 if (panel_position & LEFT) {
434 struts[0] = p->area.width + p->marginx;
435 struts[4] = p->posy;
436 // p->area.width - 1 allowed full screen on monitor 2
437 struts[5] = p->posy + p->area.height - 1;
438 }
439 else {
440 struts[1] = p->area.width + p->marginx;
441 struts[6] = p->posy;
442 // p->area.width - 1 allowed full screen on monitor 2
443 struts[7] = p->posy + p->area.height - 1;
444 }
445 }
446 // Old specification : fluxbox need _NET_WM_STRUT.
447 XChangeProperty (server.dsp, p->main_win, server.atom._NET_WM_STRUT, XA_CARDINAL, 32, PropModeReplace, (unsigned char *) &struts, 4);
448 XChangeProperty (server.dsp, p->main_win, server.atom._NET_WM_STRUT_PARTIAL, XA_CARDINAL, 32, PropModeReplace, (unsigned char *) &struts, 12);
449
450 // Fixed position and non-resizable window
451 // Allow panel move and resize when tint2 reload config file
452 XSizeHints size_hints;
453 size_hints.flags = PPosition|PMinSize|PMaxSize;
454 size_hints.min_width = size_hints.max_width = p->area.width;
455 size_hints.min_height = size_hints.max_height = p->area.height;
456 XSetWMNormalHints(server.dsp, p->main_win, &size_hints);
457 }
458
459
460 void set_panel_background(Panel *p)
461 {
462 get_root_pixmap();
463
464 if (p->area.pix.pmap) XFreePixmap (server.dsp, p->area.pix.pmap);
465 p->area.pix.pmap = XCreatePixmap (server.dsp, server.root_win, p->area.width, p->area.height, server.depth);
466
467 // copy background (server.root_pmap) in panel.area.pix.pmap
468 Window dummy;
469 int x, y;
470 XTranslateCoordinates(server.dsp, p->main_win, server.root_win, 0, 0, &x, &y, &dummy);
471 XSetTSOrigin(server.dsp, server.gc, -x, -y) ;
472 XFillRectangle(server.dsp, p->area.pix.pmap, server.gc, 0, 0, p->area.width, p->area.height);
473
474 // draw background panel
475 cairo_surface_t *cs;
476 cairo_t *c;
477 cs = cairo_xlib_surface_create (server.dsp, p->area.pix.pmap, server.visual, p->area.width, p->area.height);
478 c = cairo_create (cs);
479
480 draw_background(&p->area, c, 0);
481
482 cairo_destroy (c);
483 cairo_surface_destroy (cs);
484
485 // redraw panel's object
486 GSList *l0;
487 Area *a;
488 for (l0 = p->area.list; l0 ; l0 = l0->next) {
489 a = l0->data;
490 set_redraw(a);
491 }
492 }
493
494
495 Panel *get_panel(Window win)
496 {
497 int i;
498 for (i=0 ; i < nb_panel ; i++) {
499 if (panel1[i].main_win == win) {
500 return &panel1[i];
501 }
502 }
503 return 0;
504 }
505
This page took 0.06791 seconds and 4 git commands to generate.