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