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