1 /**************************************************************************
3 * Copyright (C) 2008 Pål Staurland (staura@gmail.com)
4 * Modified (C) 2008 thierry lorthiois (lorthiois@bbsoft.fr)
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.
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 **************************************************************************/
23 #include <X11/Xutil.h>
24 #include <X11/Xatom.h>
26 #include <cairo-xlib.h>
27 #include <pango/pangocairo.h>
37 // --------------------------------------------------
42 int mouse_scroll_down
;
48 int panel_dock
=0; // default not in the dock
58 // panel's initial config
60 // panels (one panel per monitor)
64 Imlib_Image default_icon
= NULL
;
81 for (i
=0 ; i
< nb_panel
; i
++) {
82 free_area(&panel1
[i
].area
);
83 if (panel1
[i
].temp_pmap
) {
84 XFreePixmap(server
.dsp
, panel1
[i
].temp_pmap
);
85 panel1
[i
].temp_pmap
= 0;
90 old_nb_panel
= nb_panel
;
91 if (panel_config
.monitor
>= 0)
94 nb_panel
= server
.nb_monitor
;
97 for (i
=nb_panel
; i
< old_nb_panel
; i
++) {
98 if (panel1
[i
].main_win
) {
99 XDestroyWindow(server
.dsp
, panel1
[i
].main_win
);
100 panel1
[i
].main_win
= 0;
104 // alloc & init new panel
106 if (nb_panel
!= old_nb_panel
)
107 new_panel
= realloc(panel1
, nb_panel
* sizeof(Panel
));
110 for (i
=0 ; i
< nb_panel
; i
++) {
111 old_win
= new_panel
[i
].main_win
;
112 memcpy(&new_panel
[i
], &panel_config
, sizeof(Panel
));
113 new_panel
[i
].main_win
= old_win
;
116 fprintf(stderr
, "tint2 : nb monitor %d, nb desktop %d\n", nb_panel
, server
.nb_desktop
);
117 for (i
=0 ; i
< nb_panel
; i
++) {
123 p
->area
.on_screen
= 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
);
134 p
->area
.list
= g_slist_append(p
->area
.list
, &p
->clock
);
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
);
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
);
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
);
159 XMoveResizeWindow(server
.dsp
, p
->main_win
, p
->posx
, p
->posy
, p
->area
.width
, p
->area
.height
);
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
) {
167 XMapWindow (server
.dsp
, p
->main_win
);
175 task_refresh_tasklist();
180 void init_panel_size_and_position(Panel
*panel
)
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;
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;
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;
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;
205 // panel position determined here
206 if (panel_position
& LEFT
) {
207 panel
->posx
= server
.monitor
[panel
->monitor
].x
+ panel
->marginx
;
210 if (panel_position
& RIGHT
) {
211 panel
->posx
= server
.monitor
[panel
->monitor
].x
+ server
.monitor
[panel
->monitor
].width
- panel
->area
.width
- panel
->marginx
;
214 if (panel_horizontal
)
215 panel
->posx
= server
.monitor
[panel
->monitor
].x
+ ((server
.monitor
[panel
->monitor
].width
- panel
->area
.width
) / 2);
217 panel
->posx
= server
.monitor
[panel
->monitor
].x
+ panel
->marginx
;
220 if (panel_position
& TOP
) {
221 panel
->posy
= server
.monitor
[panel
->monitor
].y
+ panel
->marginy
;
224 if (panel_position
& BOTTOM
) {
225 panel
->posy
= server
.monitor
[panel
->monitor
].y
+ server
.monitor
[panel
->monitor
].height
- panel
->area
.height
- panel
->marginy
;
228 panel
->posy
= server
.monitor
[panel
->monitor
].y
+ ((server
.monitor
[panel
->monitor
].height
- panel
->area
.height
) / 2);
231 // printf("panel : posx %d, posy %d, width %d, height %d\n", panel->posx, panel->posy, panel->area.width, panel->area.height);
241 while (urgent_list
) {
242 Task_urgent
* t
= urgent_list
->data
;
243 urgent_list
= g_slist_remove(urgent_list
, urgent_list
->data
);
251 for (i
=0 ; i
< nb_panel
; i
++) {
257 XFreePixmap(server
.dsp
, p
->temp_pmap
);
261 XDestroyWindow(server
.dsp
, p
->main_win
);
266 if (panel1
) free(panel1
);
269 if (panel_config
.g_task
.font_desc
) {
270 pango_font_description_free(panel_config
.g_task
.font_desc
);
271 panel_config
.g_task
.font_desc
= 0;
276 void resize_panel(void *obj
)
278 Panel
*panel
= (Panel
*)obj
;
280 if (panel_horizontal
) {
281 int taskbar_width
, modulo_width
= 0;
283 taskbar_width
= panel
->area
.width
- (2 * panel
->area
.paddingxlr
) - (2 * panel
->area
.pix
.border
.width
);
284 if (panel
->clock
.area
.on_screen
&& panel
->clock
.area
.width
)
285 taskbar_width
-= (panel
->clock
.area
.width
+ panel
->area
.paddingx
);
286 #ifdef ENABLE_BATTERY
287 if (panel
->battery
.area
.on_screen
&& panel
->battery
.area
.width
)
288 taskbar_width
-= (panel
->battery
.area
.width
+ panel
->area
.paddingx
);
290 // TODO : systray only on first panel. search better implementation !
291 if (systray
.area
.on_screen
&& systray
.area
.width
&& panel
== &panel1
[0])
292 taskbar_width
-= (systray
.area
.width
+ panel
->area
.paddingx
);
294 if (panel_mode
== MULTI_DESKTOP
) {
295 int width
= taskbar_width
- ((panel
->nb_desktop
-1) * panel
->area
.paddingx
);
296 taskbar_width
= width
/ panel
->nb_desktop
;
297 modulo_width
= width
% panel
->nb_desktop
;
300 // change posx and width for all taskbar
302 posx
= panel
->area
.pix
.border
.width
+ panel
->area
.paddingxlr
;
303 for (i
=0 ; i
< panel
->nb_desktop
; i
++) {
304 panel
->taskbar
[i
].area
.posx
= posx
;
305 panel
->taskbar
[i
].area
.width
= taskbar_width
;
306 panel
->taskbar
[i
].area
.resize
= 1;
308 panel
->taskbar
[i
].area
.width
++;
311 //printf("taskbar %d : posx %d, width, %d, posy %d\n", i, posx, panel->taskbar[i].area.width, posx + panel->taskbar[i].area.width);
312 if (panel_mode
== MULTI_DESKTOP
)
313 posx
+= panel
->taskbar
[i
].area
.width
+ panel
->area
.paddingx
;
317 int taskbar_height
, modulo_height
= 0;
320 taskbar_height
= panel
->area
.height
- (2 * panel
->area
.paddingxlr
) - (2 * panel
->area
.pix
.border
.width
);
321 if (panel
->clock
.area
.on_screen
&& panel
->clock
.area
.height
)
322 taskbar_height
-= (panel
->clock
.area
.height
+ panel
->area
.paddingx
);
323 #ifdef ENABLE_BATTERY
324 if (panel
->battery
.area
.on_screen
&& panel
->battery
.area
.height
)
325 taskbar_height
-= (panel
->battery
.area
.height
+ panel
->area
.paddingx
);
327 // TODO : systray only on first panel. search better implementation !
328 if (systray
.area
.on_screen
&& systray
.area
.height
&& panel
== &panel1
[0])
329 taskbar_height
-= (systray
.area
.height
+ panel
->area
.paddingx
);
331 posy
= panel
->area
.height
- panel
->area
.pix
.border
.width
- panel
->area
.paddingxlr
- taskbar_height
;
332 if (panel_mode
== MULTI_DESKTOP
) {
333 int height
= taskbar_height
- ((panel
->nb_desktop
-1) * panel
->area
.paddingx
);
334 taskbar_height
= height
/ panel
->nb_desktop
;
335 modulo_height
= height
% panel
->nb_desktop
;
338 // change posy and height for all taskbar
339 for (i
=0 ; i
< panel
->nb_desktop
; i
++) {
340 panel
->taskbar
[i
].area
.posy
= posy
;
341 panel
->taskbar
[i
].area
.height
= taskbar_height
;
342 panel
->taskbar
[i
].area
.resize
= 1;
344 panel
->taskbar
[i
].area
.height
++;
347 if (panel_mode
== MULTI_DESKTOP
)
348 posy
+= panel
->taskbar
[i
].area
.height
+ panel
->area
.paddingx
;
354 void visible_object()
359 for (i
=0 ; i
< nb_panel
; i
++) {
363 for (j
=0 ; j
< panel
->nb_desktop
; j
++) {
364 taskbar
= &panel
->taskbar
[j
];
365 if (panel_mode
!= MULTI_DESKTOP
&& taskbar
->desktop
!= server
.desktop
) {
366 // SINGLE_DESKTOP and not current desktop
367 taskbar
->area
.on_screen
= 0;
370 taskbar
->area
.on_screen
= 1;
378 void set_panel_properties(Panel
*p
)
380 XStoreName (server
.dsp
, p
->main_win
, "tint2");
383 gchar
*name
= g_locale_to_utf8("tint2", -1, NULL
, &len
, NULL
);
385 XChangeProperty(server
.dsp
, p
->main_win
, server
.atom
._NET_WM_NAME
, server
.atom
.UTF8_STRING
, 8, PropModeReplace
, (unsigned char *) name
, (int) len
);
390 long val
= server
.atom
._NET_WM_WINDOW_TYPE_DOCK
;
391 XChangeProperty (server
.dsp
, p
->main_win
, server
.atom
._NET_WM_WINDOW_TYPE
, XA_ATOM
, 32, PropModeReplace
, (unsigned char *) &val
, 1);
393 // Sticky and below other window
395 XChangeProperty (server
.dsp
, p
->main_win
, server
.atom
._NET_WM_DESKTOP
, XA_CARDINAL
, 32, PropModeReplace
, (unsigned char *) &val
, 1);
397 state
[0] = server
.atom
._NET_WM_STATE_SKIP_PAGER
;
398 state
[1] = server
.atom
._NET_WM_STATE_SKIP_TASKBAR
;
399 state
[2] = server
.atom
._NET_WM_STATE_STICKY
;
400 state
[3] = server
.atom
._NET_WM_STATE_BELOW
;
401 XChangeProperty (server
.dsp
, p
->main_win
, server
.atom
._NET_WM_STATE
, XA_ATOM
, 32, PropModeReplace
, (unsigned char *) state
, 4);
406 // TODO: Xdnd feature cannot be used in withdrawn state at the moment (at least GTK apps fail, qt seems to work)
407 wmhints
.icon_window
= wmhints
.window_group
= p
->main_win
;
408 wmhints
.flags
= StateHint
| IconWindowHint
;
409 wmhints
.initial_state
= WithdrawnState
;
412 wmhints
.flags
= InputHint
;
413 wmhints
.input
= False
;
415 XSetWMHints(server
.dsp
, p
->main_win
, &wmhints
);
418 long prop
[5] = { 2, 0, 0, 0, 0 };
419 XChangeProperty(server
.dsp
, p
->main_win
, server
.atom
._MOTIF_WM_HINTS
, server
.atom
._MOTIF_WM_HINTS
, 32, PropModeReplace
, (unsigned char *) prop
, 5);
421 // XdndAware - Register for Xdnd events
423 XChangeProperty(server
.dsp
, p
->main_win
, server
.atom
.XdndAware
, XA_ATOM
, 32, PropModeReplace
, (unsigned char*)&version
, 1);
426 long struts
[12] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
427 if (panel_horizontal
) {
428 if (panel_position
& TOP
) {
429 struts
[2] = p
->area
.height
+ p
->marginy
;
431 // p->area.width - 1 allowed full screen on monitor 2
432 struts
[9] = p
->posx
+ p
->area
.width
- 1;
435 struts
[3] = p
->area
.height
+ p
->marginy
;
436 struts
[10] = p
->posx
;
437 // p->area.width - 1 allowed full screen on monitor 2
438 struts
[11] = p
->posx
+ p
->area
.width
- 1;
442 if (panel_position
& LEFT
) {
443 struts
[0] = p
->area
.width
+ p
->marginx
;
445 // p->area.width - 1 allowed full screen on monitor 2
446 struts
[5] = p
->posy
+ p
->area
.height
- 1;
449 struts
[1] = p
->area
.width
+ p
->marginx
;
451 // p->area.width - 1 allowed full screen on monitor 2
452 struts
[7] = p
->posy
+ p
->area
.height
- 1;
455 // Old specification : fluxbox need _NET_WM_STRUT.
456 XChangeProperty (server
.dsp
, p
->main_win
, server
.atom
._NET_WM_STRUT
, XA_CARDINAL
, 32, PropModeReplace
, (unsigned char *) &struts
, 4);
457 XChangeProperty (server
.dsp
, p
->main_win
, server
.atom
._NET_WM_STRUT_PARTIAL
, XA_CARDINAL
, 32, PropModeReplace
, (unsigned char *) &struts
, 12);
459 // Fixed position and non-resizable window
460 // Allow panel move and resize when tint2 reload config file
461 XSizeHints size_hints
;
462 size_hints
.flags
= PPosition
|PMinSize
|PMaxSize
;
463 size_hints
.min_width
= size_hints
.max_width
= p
->area
.width
;
464 size_hints
.min_height
= size_hints
.max_height
= p
->area
.height
;
465 XSetWMNormalHints(server
.dsp
, p
->main_win
, &size_hints
);
469 void set_panel_background(Panel
*p
)
473 if (p
->area
.pix
.pmap
) XFreePixmap (server
.dsp
, p
->area
.pix
.pmap
);
474 p
->area
.pix
.pmap
= XCreatePixmap (server
.dsp
, server
.root_win
, p
->area
.width
, p
->area
.height
, server
.depth
);
476 // copy background (server.root_pmap) in panel.area.pix.pmap
479 XTranslateCoordinates(server
.dsp
, p
->main_win
, server
.root_win
, 0, 0, &x
, &y
, &dummy
);
480 XSetTSOrigin(server
.dsp
, server
.gc
, -x
, -y
) ;
481 XFillRectangle(server
.dsp
, p
->area
.pix
.pmap
, server
.gc
, 0, 0, p
->area
.width
, p
->area
.height
);
483 // draw background panel
486 cs
= cairo_xlib_surface_create (server
.dsp
, p
->area
.pix
.pmap
, server
.visual
, p
->area
.width
, p
->area
.height
);
487 c
= cairo_create (cs
);
489 draw_background(&p
->area
, c
, 0);
492 cairo_surface_destroy (cs
);
494 // redraw panel's object
497 for (l0
= p
->area
.list
; l0
; l0
= l0
->next
) {
504 Panel
*get_panel(Window win
)
507 for (i
=0 ; i
< nb_panel
; i
++) {
508 if (panel1
[i
].main_win
== win
) {
516 Taskbar
*click_taskbar (Panel
*panel
, int x
, int y
)
521 if (panel_horizontal
) {
522 for (i
=0; i
< panel
->nb_desktop
; i
++) {
523 tskbar
= &panel
->taskbar
[i
];
524 if (tskbar
->area
.on_screen
&& x
>= tskbar
->area
.posx
&& x
<= (tskbar
->area
.posx
+ tskbar
->area
.width
))
529 for (i
=0; i
< panel
->nb_desktop
; i
++) {
530 tskbar
= &panel
->taskbar
[i
];
531 if (tskbar
->area
.on_screen
&& y
>= tskbar
->area
.posy
&& y
<= (tskbar
->area
.posy
+ tskbar
->area
.height
))
539 Task
*click_task (Panel
*panel
, int x
, int y
)
544 if ( (tskbar
= click_taskbar(panel
, x
, y
)) ) {
545 if (panel_horizontal
) {
547 for (l0
= tskbar
->area
.list
; l0
; l0
= l0
->next
) {
549 if (tsk
->area
.on_screen
&& x
>= tsk
->area
.posx
&& x
<= (tsk
->area
.posx
+ tsk
->area
.width
)) {
556 for (l0
= tskbar
->area
.list
; l0
; l0
= l0
->next
) {
558 if (tsk
->area
.on_screen
&& y
>= tsk
->area
.posy
&& y
<= (tsk
->area
.posy
+ tsk
->area
.height
)) {
568 int click_padding(Panel
*panel
, int x
, int y
)
570 if (panel_horizontal
) {
571 if (x
< panel
->area
.paddingxlr
|| x
> panel
->area
.width
-panel
->area
.paddingxlr
)
575 if (y
< panel
->area
.paddingxlr
|| y
> panel
->area
.height
-panel
->area
.paddingxlr
)
582 int click_clock(Panel
*panel
, int x
, int y
)
584 Clock clk
= panel
->clock
;
585 if (panel_horizontal
) {
586 if (clk
.area
.on_screen
&& x
>= clk
.area
.posx
&& x
<= (clk
.area
.posx
+ clk
.area
.width
))
589 if (clk
.area
.on_screen
&& y
>= clk
.area
.posy
&& y
<= (clk
.area
.posy
+ clk
.area
.height
))
596 Area
* click_area(Panel
*panel
, int x
, int y
)
598 Area
* result
= &panel
->area
;
599 Area
* new_result
= result
;
602 GSList
* it
= result
->list
;
605 if (panel_horizontal
) {
606 if (a
->on_screen
&& x
>= a
->posx
&& x
<= (a
->posx
+ a
->width
)) {
611 if (a
->on_screen
&& y
>= a
->posy
&& y
<= (a
->posy
+ a
->height
)) {
618 } while (new_result
!= result
);