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