]> Dogcows Code - chaz/tint2/blob - src/panel.c
*fix* better internal urgent task handling
[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 int max_tick_urgent;
56
57 // panel's initial config
58 Panel panel_config;
59 // panels (one panel per monitor)
60 Panel *panel1 = 0;
61 int nb_panel = 0;
62
63 Imlib_Image default_icon = NULL;
64
65
66
67 void init_panel()
68 {
69 int i, old_nb_panel;
70 Panel *new_panel, *p;
71
72 init_tooltip();
73 init_systray();
74 init_clock();
75 #ifdef ENABLE_BATTERY
76 init_battery();
77 #endif
78
79 cleanup_taskbar();
80 for (i=0 ; i < nb_panel ; i++) {
81 free_area(&panel1[i].area);
82 if (panel1[i].temp_pmap) {
83 XFreePixmap(server.dsp, panel1[i].temp_pmap);
84 panel1[i].temp_pmap = 0;
85 }
86 }
87
88 // number of panels
89 old_nb_panel = nb_panel;
90 if (panel_config.monitor >= 0)
91 nb_panel = 1;
92 else
93 nb_panel = server.nb_monitor;
94
95 // freed old panels
96 for (i=nb_panel ; i < old_nb_panel ; i++) {
97 if (panel1[i].main_win) {
98 XDestroyWindow(server.dsp, panel1[i].main_win);
99 panel1[i].main_win = 0;
100 }
101 }
102
103 // alloc & init new panel
104 Window old_win;
105 if (nb_panel != old_nb_panel)
106 new_panel = realloc(panel1, nb_panel * sizeof(Panel));
107 else
108 new_panel = panel1;
109 for (i=0 ; i < nb_panel ; i++) {
110 old_win = new_panel[i].main_win;
111 memcpy(&new_panel[i], &panel_config, sizeof(Panel));
112 new_panel[i].main_win = old_win;
113 }
114
115 fprintf(stderr, "tint2 : nb monitor %d, nb monitor used %d, nb desktop %d\n", server.nb_monitor, nb_panel, server.nb_desktop);
116 for (i=0 ; i < nb_panel ; i++) {
117 p = &new_panel[i];
118
119 if (panel_config.monitor < 0)
120 p->monitor = i;
121 p->area.parent = p;
122 p->area.panel = p;
123 p->area.on_screen = 1;
124 p->area.resize = 1;
125 p->area._resize = resize_panel;
126 p->g_taskbar.parent = p;
127 p->g_taskbar.panel = p;
128 p->g_task.area.panel = p;
129 init_panel_size_and_position(p);
130
131 // add childs
132 if (clock_enabled) {
133 init_clock_panel(p);
134 p->area.list = g_slist_append(p->area.list, &p->clock);
135 }
136 #ifdef ENABLE_BATTERY
137 if (battery_enabled) {
138 init_battery_panel(p);
139 p->area.list = g_slist_append(p->area.list, &p->battery);
140 }
141 #endif
142 // systray only on first panel
143 if (systray.area.on_screen && i == 0) {
144 init_systray_panel(p);
145 p->area.list = g_slist_append(p->area.list, &systray);
146 refresh_systray = 1;
147 }
148
149 if (i >= old_nb_panel) {
150 // new panel : 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 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);
156 }
157 else {
158 // old panel
159 XMoveResizeWindow(server.dsp, p->main_win, p->posx, p->posy, p->area.width, p->area.height);
160 }
161
162 //printf("panel %d : %d, %d, %d, %d\n", i, p->posx, p->posy, p->area.width, p->area.height);
163 set_panel_properties(p);
164 set_panel_background(p);
165 if (i >= old_nb_panel) {
166 // map new panel
167 XMapWindow (server.dsp, p->main_win);
168 }
169 }
170
171 panel1 = new_panel;
172 panel_refresh = 1;
173 init_taskbar();
174 visible_object();
175 task_refresh_tasklist();
176 active_task();
177 }
178
179
180 void init_panel_size_and_position(Panel *panel)
181 {
182 // detect panel size
183 if (panel_horizontal) {
184 if (panel->pourcentx)
185 panel->area.width = (float)server.monitor[panel->monitor].width * panel->area.width / 100;
186 if (panel->pourcenty)
187 panel->area.height = (float)server.monitor[panel->monitor].height * panel->area.height / 100;
188 if (panel->area.pix.border.rounded > panel->area.height/2)
189 panel->area.pix.border.rounded = panel->area.height/2;
190 }
191 else {
192 int old_panel_height = panel->area.height;
193 if (panel->pourcentx)
194 panel->area.height = (float)server.monitor[panel->monitor].height * panel->area.width / 100;
195 else
196 panel->area.height = panel->area.width;
197 if (panel->pourcenty)
198 panel->area.width = (float)server.monitor[panel->monitor].width * old_panel_height / 100;
199 else
200 panel->area.width = old_panel_height;
201 if (panel->area.pix.border.rounded > panel->area.width/2)
202 panel->area.pix.border.rounded = panel->area.width/2;
203 }
204
205 // panel position determined here
206 if (panel_position & LEFT) {
207 panel->posx = server.monitor[panel->monitor].x + panel->marginx;
208 }
209 else {
210 if (panel_position & RIGHT) {
211 panel->posx = server.monitor[panel->monitor].x + server.monitor[panel->monitor].width - panel->area.width - panel->marginx;
212 }
213 else {
214 if (panel_horizontal)
215 panel->posx = server.monitor[panel->monitor].x + ((server.monitor[panel->monitor].width - panel->area.width) / 2);
216 else
217 panel->posx = server.monitor[panel->monitor].x + panel->marginx;
218 }
219 }
220 if (panel_position & TOP) {
221 panel->posy = server.monitor[panel->monitor].y + panel->marginy;
222 }
223 else {
224 if (panel_position & BOTTOM) {
225 panel->posy = server.monitor[panel->monitor].y + server.monitor[panel->monitor].height - panel->area.height - panel->marginy;
226 }
227 else {
228 panel->posy = server.monitor[panel->monitor].y + ((server.monitor[panel->monitor].height - panel->area.height) / 2);
229 }
230 }
231 // printf("panel : posx %d, posy %d, width %d, height %d\n", panel->posx, panel->posy, panel->area.width, panel->area.height);
232 }
233
234
235 void cleanup_panel()
236 {
237 if (!panel1) return;
238
239 task_active = 0;
240 task_drag = 0;
241
242 cleanup_taskbar();
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) {
262 free(panel1);
263 panel1 = 0;
264 nb_panel = 0;
265 }
266
267 if (panel_config.g_task.font_desc) {
268 pango_font_description_free(panel_config.g_task.font_desc);
269 panel_config.g_task.font_desc = 0;
270 }
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 // 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 // Unfocusable
402 XWMHints wmhints;
403 if (panel_dock) {
404 // TODO: Xdnd feature cannot be used in withdrawn state at the moment (at least GTK apps fail, qt seems to work)
405 wmhints.icon_window = wmhints.window_group = p->main_win;
406 wmhints.flags = StateHint | IconWindowHint;
407 wmhints.initial_state = WithdrawnState;
408 }
409 else {
410 wmhints.flags = InputHint;
411 wmhints.input = False;
412 }
413 XSetWMHints(server.dsp, p->main_win, &wmhints);
414
415 // Undecorated
416 long prop[5] = { 2, 0, 0, 0, 0 };
417 XChangeProperty(server.dsp, p->main_win, server.atom._MOTIF_WM_HINTS, server.atom._MOTIF_WM_HINTS, 32, PropModeReplace, (unsigned char *) prop, 5);
418
419 // XdndAware - Register for Xdnd events
420 int version=5;
421 XChangeProperty(server.dsp, p->main_win, server.atom.XdndAware, XA_ATOM, 32, PropModeReplace, (unsigned char*)&version, 1);
422
423 // Reserved space
424 unsigned int d1, screen_width, screen_height;
425 Window d2;
426 int d3;
427 XGetGeometry(server.dsp, server.root_win, &d2, &d3, &d3, &screen_width, &screen_height, &d1, &d1);
428 Monitor monitor = server.monitor[p->monitor];
429 long struts [12] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
430 if (panel_horizontal) {
431 if (panel_position & TOP) {
432 struts[2] = p->area.height + p->marginy + monitor.y;
433 struts[8] = p->posx;
434 // p->area.width - 1 allowed full screen on monitor 2
435 struts[9] = p->posx + p->area.width - 1;
436 }
437 else {
438 struts[3] = p->area.height + p->marginy + screen_height - monitor.y - monitor.height;
439 struts[10] = p->posx;
440 // p->area.width - 1 allowed full screen on monitor 2
441 struts[11] = p->posx + p->area.width - 1;
442 }
443 }
444 else {
445 if (panel_position & LEFT) {
446 struts[0] = p->area.width + p->marginx + monitor.x;
447 struts[4] = p->posy;
448 // p->area.width - 1 allowed full screen on monitor 2
449 struts[5] = p->posy + p->area.height - 1;
450 }
451 else {
452 struts[1] = p->area.width + p->marginx + screen_width - monitor.x - monitor.width;
453 struts[6] = p->posy;
454 // p->area.width - 1 allowed full screen on monitor 2
455 struts[7] = p->posy + p->area.height - 1;
456 }
457 }
458 // Old specification : fluxbox need _NET_WM_STRUT.
459 XChangeProperty (server.dsp, p->main_win, server.atom._NET_WM_STRUT, XA_CARDINAL, 32, PropModeReplace, (unsigned char *) &struts, 4);
460 XChangeProperty (server.dsp, p->main_win, server.atom._NET_WM_STRUT_PARTIAL, XA_CARDINAL, 32, PropModeReplace, (unsigned char *) &struts, 12);
461
462 // Fixed position and non-resizable window
463 // Allow panel move and resize when tint2 reload config file
464 XSizeHints size_hints;
465 size_hints.flags = PPosition|PMinSize|PMaxSize;
466 size_hints.min_width = size_hints.max_width = p->area.width;
467 size_hints.min_height = size_hints.max_height = p->area.height;
468 XSetWMNormalHints(server.dsp, p->main_win, &size_hints);
469
470 // Set WM_CLASS
471 XClassHint* classhint = XAllocClassHint();
472 classhint->res_name = "tint2";
473 classhint->res_class = "Tint2";
474 XSetClassHint(server.dsp, p->main_win, classhint);
475 XFree(classhint);
476 }
477
478
479 void set_panel_background(Panel *p)
480 {
481 get_root_pixmap();
482
483 if (p->area.pix.pmap) XFreePixmap (server.dsp, p->area.pix.pmap);
484 p->area.pix.pmap = XCreatePixmap (server.dsp, server.root_win, p->area.width, p->area.height, server.depth);
485
486 // copy background (server.root_pmap) in panel.area.pix.pmap
487 Window dummy;
488 int x, y;
489 XTranslateCoordinates(server.dsp, p->main_win, server.root_win, 0, 0, &x, &y, &dummy);
490 XSetTSOrigin(server.dsp, server.gc, -x, -y) ;
491 XFillRectangle(server.dsp, p->area.pix.pmap, server.gc, 0, 0, p->area.width, p->area.height);
492
493 // draw background panel
494 cairo_surface_t *cs;
495 cairo_t *c;
496 cs = cairo_xlib_surface_create (server.dsp, p->area.pix.pmap, server.visual, p->area.width, p->area.height);
497 c = cairo_create (cs);
498
499 draw_background(&p->area, c, 0);
500
501 cairo_destroy (c);
502 cairo_surface_destroy (cs);
503
504 // redraw panel's object
505 GSList *l0;
506 Area *a;
507 for (l0 = p->area.list; l0 ; l0 = l0->next) {
508 a = l0->data;
509 set_redraw(a);
510 }
511 }
512
513
514 Panel *get_panel(Window win)
515 {
516 int i;
517 for (i=0 ; i < nb_panel ; i++) {
518 if (panel1[i].main_win == win) {
519 return &panel1[i];
520 }
521 }
522 return 0;
523 }
524
525
526 Taskbar *click_taskbar (Panel *panel, int x, int y)
527 {
528 Taskbar *tskbar;
529 int i;
530
531 if (panel_horizontal) {
532 for (i=0; i < panel->nb_desktop ; i++) {
533 tskbar = &panel->taskbar[i];
534 if (tskbar->area.on_screen && x >= tskbar->area.posx && x <= (tskbar->area.posx + tskbar->area.width))
535 return tskbar;
536 }
537 }
538 else {
539 for (i=0; i < panel->nb_desktop ; i++) {
540 tskbar = &panel->taskbar[i];
541 if (tskbar->area.on_screen && y >= tskbar->area.posy && y <= (tskbar->area.posy + tskbar->area.height))
542 return tskbar;
543 }
544 }
545 return NULL;
546 }
547
548
549 Task *click_task (Panel *panel, int x, int y)
550 {
551 GSList *l0;
552 Taskbar *tskbar;
553
554 if ( (tskbar = click_taskbar(panel, x, y)) ) {
555 if (panel_horizontal) {
556 Task *tsk;
557 for (l0 = tskbar->area.list; l0 ; l0 = l0->next) {
558 tsk = l0->data;
559 if (tsk->area.on_screen && x >= tsk->area.posx && x <= (tsk->area.posx + tsk->area.width)) {
560 return tsk;
561 }
562 }
563 }
564 else {
565 Task *tsk;
566 for (l0 = tskbar->area.list; l0 ; l0 = l0->next) {
567 tsk = l0->data;
568 if (tsk->area.on_screen && y >= tsk->area.posy && y <= (tsk->area.posy + tsk->area.height)) {
569 return tsk;
570 }
571 }
572 }
573 }
574 return NULL;
575 }
576
577
578 int click_padding(Panel *panel, int x, int y)
579 {
580 if (panel_horizontal) {
581 if (x < panel->area.paddingxlr || x > panel->area.width-panel->area.paddingxlr)
582 return 1;
583 }
584 else {
585 if (y < panel->area.paddingxlr || y > panel->area.height-panel->area.paddingxlr)
586 return 1;
587 }
588 return 0;
589 }
590
591
592 int click_clock(Panel *panel, int x, int y)
593 {
594 Clock clk = panel->clock;
595 if (panel_horizontal) {
596 if (clk.area.on_screen && x >= clk.area.posx && x <= (clk.area.posx + clk.area.width))
597 return TRUE;
598 } else {
599 if (clk.area.on_screen && y >= clk.area.posy && y <= (clk.area.posy + clk.area.height))
600 return TRUE;
601 }
602 return FALSE;
603 }
604
605
606 Area* click_area(Panel *panel, int x, int y)
607 {
608 Area* result = &panel->area;
609 Area* new_result = result;
610 do {
611 result = new_result;
612 GSList* it = result->list;
613 while (it) {
614 Area* a = it->data;
615 if (panel_horizontal) {
616 if (a->on_screen && x >= a->posx && x <= (a->posx + a->width)) {
617 new_result = a;
618 break;
619 }
620 } else {
621 if (a->on_screen && y >= a->posy && y <= (a->posy + a->height)) {
622 new_result = a;
623 break;
624 }
625 }
626 it = it->next;
627 }
628 } while (new_result != result);
629 return result;
630 }
This page took 0.069366 seconds and 5 git commands to generate.