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