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