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
57 // panel's initial config
59 // panels (one panel per monitor)
63 Imlib_Image default_icon
= NULL
;
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;
89 old_nb_panel
= nb_panel
;
90 if (panel_config
.monitor
>= 0)
93 nb_panel
= server
.nb_monitor
;
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;
103 // alloc & init new panel
105 if (nb_panel
!= old_nb_panel
)
106 new_panel
= realloc(panel1
, nb_panel
* sizeof(Panel
));
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
;
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
++) {
119 if (panel_config
.monitor
< 0)
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
= { .event_mask
=event_mask
, .colormap
=server
.colormap
, .background_pixel
=0, .border_pixel
=0 };
155 unsigned long mask
= CWEventMask
|CWColormap
|CWBackPixel
|CWBorderPixel
;
156 p
->main_win
= XCreateWindow(server
.dsp
, server
.root_win
, p
->posx
, p
->posy
, p
->area
.width
, p
->area
.height
, 0, server
.depth
, InputOutput
, server
.visual
, mask
, &att
);
160 XMoveResizeWindow(server
.dsp
, p
->main_win
, p
->posx
, p
->posy
, p
->area
.width
, p
->area
.height
);
165 if (real_transparency
)
166 server
.gc
= XCreateGC(server
.dsp
, p
->main_win
, 0, &gcv
);
168 server
.gc
= XCreateGC (server
.dsp
, server
.root_win
, (unsigned long)0, &gcv
);
170 //printf("panel %d : %d, %d, %d, %d\n", i, p->posx, p->posy, p->area.width, p->area.height);
171 set_panel_properties(p
);
172 set_panel_background(p
);
173 if (i
>= old_nb_panel
) {
175 XMapWindow (server
.dsp
, p
->main_win
);
183 task_refresh_tasklist();
188 void init_panel_size_and_position(Panel
*panel
)
191 if (panel_horizontal
) {
192 if (panel
->pourcentx
)
193 panel
->area
.width
= (float)server
.monitor
[panel
->monitor
].width
* panel
->area
.width
/ 100;
194 if (panel
->pourcenty
)
195 panel
->area
.height
= (float)server
.monitor
[panel
->monitor
].height
* panel
->area
.height
/ 100;
196 if (panel
->area
.pix
.border
.rounded
> panel
->area
.height
/2)
197 panel
->area
.pix
.border
.rounded
= panel
->area
.height
/2;
200 int old_panel_height
= panel
->area
.height
;
201 if (panel
->pourcentx
)
202 panel
->area
.height
= (float)server
.monitor
[panel
->monitor
].height
* panel
->area
.width
/ 100;
204 panel
->area
.height
= panel
->area
.width
;
205 if (panel
->pourcenty
)
206 panel
->area
.width
= (float)server
.monitor
[panel
->monitor
].width
* old_panel_height
/ 100;
208 panel
->area
.width
= old_panel_height
;
209 if (panel
->area
.pix
.border
.rounded
> panel
->area
.width
/2)
210 panel
->area
.pix
.border
.rounded
= panel
->area
.width
/2;
213 // panel position determined here
214 if (panel_position
& LEFT
) {
215 panel
->posx
= server
.monitor
[panel
->monitor
].x
+ panel
->marginx
;
218 if (panel_position
& RIGHT
) {
219 panel
->posx
= server
.monitor
[panel
->monitor
].x
+ server
.monitor
[panel
->monitor
].width
- panel
->area
.width
- panel
->marginx
;
222 if (panel_horizontal
)
223 panel
->posx
= server
.monitor
[panel
->monitor
].x
+ ((server
.monitor
[panel
->monitor
].width
- panel
->area
.width
) / 2);
225 panel
->posx
= server
.monitor
[panel
->monitor
].x
+ panel
->marginx
;
228 if (panel_position
& TOP
) {
229 panel
->posy
= server
.monitor
[panel
->monitor
].y
+ panel
->marginy
;
232 if (panel_position
& BOTTOM
) {
233 panel
->posy
= server
.monitor
[panel
->monitor
].y
+ server
.monitor
[panel
->monitor
].height
- panel
->area
.height
- panel
->marginy
;
236 panel
->posy
= server
.monitor
[panel
->monitor
].y
+ ((server
.monitor
[panel
->monitor
].height
- panel
->area
.height
) / 2);
239 // printf("panel : posx %d, posy %d, width %d, height %d\n", panel->posx, panel->posy, panel->area.width, panel->area.height);
254 for (i
=0 ; i
< nb_panel
; i
++) {
260 XFreePixmap(server
.dsp
, p
->temp_pmap
);
264 XDestroyWindow(server
.dsp
, p
->main_win
);
275 if (panel_config
.g_task
.font_desc
) {
276 pango_font_description_free(panel_config
.g_task
.font_desc
);
277 panel_config
.g_task
.font_desc
= 0;
282 void resize_panel(void *obj
)
284 Panel
*panel
= (Panel
*)obj
;
286 if (panel_horizontal
) {
287 int taskbar_width
, modulo_width
= 0;
289 taskbar_width
= panel
->area
.width
- (2 * panel
->area
.paddingxlr
) - (2 * panel
->area
.pix
.border
.width
);
290 if (panel
->clock
.area
.on_screen
&& panel
->clock
.area
.width
)
291 taskbar_width
-= (panel
->clock
.area
.width
+ panel
->area
.paddingx
);
292 #ifdef ENABLE_BATTERY
293 if (panel
->battery
.area
.on_screen
&& panel
->battery
.area
.width
)
294 taskbar_width
-= (panel
->battery
.area
.width
+ panel
->area
.paddingx
);
296 // TODO : systray only on first panel. search better implementation !
297 if (systray
.area
.on_screen
&& systray
.area
.width
&& panel
== &panel1
[0])
298 taskbar_width
-= (systray
.area
.width
+ panel
->area
.paddingx
);
300 if (panel_mode
== MULTI_DESKTOP
) {
301 int width
= taskbar_width
- ((panel
->nb_desktop
-1) * panel
->area
.paddingx
);
302 taskbar_width
= width
/ panel
->nb_desktop
;
303 modulo_width
= width
% panel
->nb_desktop
;
306 // change posx and width for all taskbar
308 posx
= panel
->area
.pix
.border
.width
+ panel
->area
.paddingxlr
;
309 for (i
=0 ; i
< panel
->nb_desktop
; i
++) {
310 panel
->taskbar
[i
].area
.posx
= posx
;
311 panel
->taskbar
[i
].area
.width
= taskbar_width
;
312 panel
->taskbar
[i
].area
.resize
= 1;
314 panel
->taskbar
[i
].area
.width
++;
317 //printf("taskbar %d : posx %d, width, %d, posy %d\n", i, posx, panel->taskbar[i].area.width, posx + panel->taskbar[i].area.width);
318 if (panel_mode
== MULTI_DESKTOP
)
319 posx
+= panel
->taskbar
[i
].area
.width
+ panel
->area
.paddingx
;
323 int taskbar_height
, modulo_height
= 0;
326 taskbar_height
= panel
->area
.height
- (2 * panel
->area
.paddingxlr
) - (2 * panel
->area
.pix
.border
.width
);
327 if (panel
->clock
.area
.on_screen
&& panel
->clock
.area
.height
)
328 taskbar_height
-= (panel
->clock
.area
.height
+ panel
->area
.paddingx
);
329 #ifdef ENABLE_BATTERY
330 if (panel
->battery
.area
.on_screen
&& panel
->battery
.area
.height
)
331 taskbar_height
-= (panel
->battery
.area
.height
+ panel
->area
.paddingx
);
333 // TODO : systray only on first panel. search better implementation !
334 if (systray
.area
.on_screen
&& systray
.area
.height
&& panel
== &panel1
[0])
335 taskbar_height
-= (systray
.area
.height
+ panel
->area
.paddingx
);
337 posy
= panel
->area
.height
- panel
->area
.pix
.border
.width
- panel
->area
.paddingxlr
- taskbar_height
;
338 if (panel_mode
== MULTI_DESKTOP
) {
339 int height
= taskbar_height
- ((panel
->nb_desktop
-1) * panel
->area
.paddingx
);
340 taskbar_height
= height
/ panel
->nb_desktop
;
341 modulo_height
= height
% panel
->nb_desktop
;
344 // change posy and height for all taskbar
345 for (i
=0 ; i
< panel
->nb_desktop
; i
++) {
346 panel
->taskbar
[i
].area
.posy
= posy
;
347 panel
->taskbar
[i
].area
.height
= taskbar_height
;
348 panel
->taskbar
[i
].area
.resize
= 1;
350 panel
->taskbar
[i
].area
.height
++;
353 if (panel_mode
== MULTI_DESKTOP
)
354 posy
+= panel
->taskbar
[i
].area
.height
+ panel
->area
.paddingx
;
360 void visible_object()
365 for (i
=0 ; i
< nb_panel
; i
++) {
369 for (j
=0 ; j
< panel
->nb_desktop
; j
++) {
370 taskbar
= &panel
->taskbar
[j
];
371 if (panel_mode
!= MULTI_DESKTOP
&& taskbar
->desktop
!= server
.desktop
) {
372 // SINGLE_DESKTOP and not current desktop
373 taskbar
->area
.on_screen
= 0;
376 taskbar
->area
.on_screen
= 1;
384 void set_panel_properties(Panel
*p
)
386 XStoreName (server
.dsp
, p
->main_win
, "tint2");
389 gchar
*name
= g_locale_to_utf8("tint2", -1, NULL
, &len
, NULL
);
391 XChangeProperty(server
.dsp
, p
->main_win
, server
.atom
._NET_WM_NAME
, server
.atom
.UTF8_STRING
, 8, PropModeReplace
, (unsigned char *) name
, (int) len
);
396 long val
= server
.atom
._NET_WM_WINDOW_TYPE_DOCK
;
397 XChangeProperty (server
.dsp
, p
->main_win
, server
.atom
._NET_WM_WINDOW_TYPE
, XA_ATOM
, 32, PropModeReplace
, (unsigned char *) &val
, 1);
399 // Sticky and below other window
401 XChangeProperty (server
.dsp
, p
->main_win
, server
.atom
._NET_WM_DESKTOP
, XA_CARDINAL
, 32, PropModeReplace
, (unsigned char *) &val
, 1);
403 state
[0] = server
.atom
._NET_WM_STATE_SKIP_PAGER
;
404 state
[1] = server
.atom
._NET_WM_STATE_SKIP_TASKBAR
;
405 state
[2] = server
.atom
._NET_WM_STATE_STICKY
;
406 state
[3] = server
.atom
._NET_WM_STATE_BELOW
;
407 XChangeProperty (server
.dsp
, p
->main_win
, server
.atom
._NET_WM_STATE
, XA_ATOM
, 32, PropModeReplace
, (unsigned char *) state
, 4);
412 // TODO: Xdnd feature cannot be used in withdrawn state at the moment (at least GTK apps fail, qt seems to work)
413 wmhints
.icon_window
= wmhints
.window_group
= p
->main_win
;
414 wmhints
.flags
= StateHint
| IconWindowHint
;
415 wmhints
.initial_state
= WithdrawnState
;
418 wmhints
.flags
= InputHint
;
419 wmhints
.input
= False
;
421 XSetWMHints(server
.dsp
, p
->main_win
, &wmhints
);
424 long prop
[5] = { 2, 0, 0, 0, 0 };
425 XChangeProperty(server
.dsp
, p
->main_win
, server
.atom
._MOTIF_WM_HINTS
, server
.atom
._MOTIF_WM_HINTS
, 32, PropModeReplace
, (unsigned char *) prop
, 5);
427 // XdndAware - Register for Xdnd events
429 XChangeProperty(server
.dsp
, p
->main_win
, server
.atom
.XdndAware
, XA_ATOM
, 32, PropModeReplace
, (unsigned char*)&version
, 1);
432 unsigned int d1
, screen_width
, screen_height
;
435 XGetGeometry(server
.dsp
, server
.root_win
, &d2
, &d3
, &d3
, &screen_width
, &screen_height
, &d1
, &d1
);
436 Monitor monitor
= server
.monitor
[p
->monitor
];
437 long struts
[12] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
438 if (panel_horizontal
) {
439 if (panel_position
& TOP
) {
440 struts
[2] = p
->area
.height
+ p
->marginy
+ monitor
.y
;
442 // p->area.width - 1 allowed full screen on monitor 2
443 struts
[9] = p
->posx
+ p
->area
.width
- 1;
446 struts
[3] = p
->area
.height
+ p
->marginy
+ screen_height
- monitor
.y
- monitor
.height
;
447 struts
[10] = p
->posx
;
448 // p->area.width - 1 allowed full screen on monitor 2
449 struts
[11] = p
->posx
+ p
->area
.width
- 1;
453 if (panel_position
& LEFT
) {
454 struts
[0] = p
->area
.width
+ p
->marginx
+ monitor
.x
;
456 // p->area.width - 1 allowed full screen on monitor 2
457 struts
[5] = p
->posy
+ p
->area
.height
- 1;
460 struts
[1] = p
->area
.width
+ p
->marginx
+ screen_width
- monitor
.x
- monitor
.width
;
462 // p->area.width - 1 allowed full screen on monitor 2
463 struts
[7] = p
->posy
+ p
->area
.height
- 1;
466 // Old specification : fluxbox need _NET_WM_STRUT.
467 XChangeProperty (server
.dsp
, p
->main_win
, server
.atom
._NET_WM_STRUT
, XA_CARDINAL
, 32, PropModeReplace
, (unsigned char *) &struts
, 4);
468 XChangeProperty (server
.dsp
, p
->main_win
, server
.atom
._NET_WM_STRUT_PARTIAL
, XA_CARDINAL
, 32, PropModeReplace
, (unsigned char *) &struts
, 12);
470 // Fixed position and non-resizable window
471 // Allow panel move and resize when tint2 reload config file
472 XSizeHints size_hints
;
473 size_hints
.flags
= PPosition
|PMinSize
|PMaxSize
;
474 size_hints
.min_width
= size_hints
.max_width
= p
->area
.width
;
475 size_hints
.min_height
= size_hints
.max_height
= p
->area
.height
;
476 XSetWMNormalHints(server
.dsp
, p
->main_win
, &size_hints
);
479 XClassHint
* classhint
= XAllocClassHint();
480 classhint
->res_name
= "tint2";
481 classhint
->res_class
= "Tint2";
482 XSetClassHint(server
.dsp
, p
->main_win
, classhint
);
487 void set_panel_background(Panel
*p
)
489 if (p
->area
.pix
.pmap
) XFreePixmap (server
.dsp
, p
->area
.pix
.pmap
);
490 p
->area
.pix
.pmap
= XCreatePixmap (server
.dsp
, server
.root_win
, p
->area
.width
, p
->area
.height
, server
.depth
);
492 if (real_transparency
) {
493 cairo_surface_t
*tmp
= cairo_xlib_surface_create (server
.dsp
, p
->area
.pix
.pmap
, server
.visual
, p
->area
.width
, p
->area
.height
);
494 cairo_t
*cr
= cairo_create(tmp
);
495 cairo_set_operator (cr
, CAIRO_OPERATOR_SOURCE
);
496 cairo_rectangle(cr
, 0, 0, p
->area
.width
, p
->area
.height
);
497 cairo_set_source_rgba(cr
, 1, 1, 1, 0);
500 cairo_surface_destroy (tmp
);
506 // copy background (server.root_pmap) in panel.area.pix.pmap
509 XTranslateCoordinates(server
.dsp
, p
->main_win
, server
.root_win
, 0, 0, &x
, &y
, &dummy
);
510 XSetTSOrigin(server
.dsp
, server
.gc
, -x
, -y
) ;
511 XFillRectangle(server
.dsp
, p
->area
.pix
.pmap
, server
.gc
, 0, 0, p
->area
.width
, p
->area
.height
);
513 // draw background panel
516 cs
= cairo_xlib_surface_create (server
.dsp
, p
->area
.pix
.pmap
, server
.visual
, p
->area
.width
, p
->area
.height
);
517 c
= cairo_create (cs
);
519 draw_background(&p
->area
, c
, 0);
522 cairo_surface_destroy (cs
);
524 // redraw panel's object
527 for (l0
= p
->area
.list
; l0
; l0
= l0
->next
) {
534 Panel
*get_panel(Window win
)
537 for (i
=0 ; i
< nb_panel
; i
++) {
538 if (panel1
[i
].main_win
== win
) {
546 Taskbar
*click_taskbar (Panel
*panel
, int x
, int y
)
551 if (panel_horizontal
) {
552 for (i
=0; i
< panel
->nb_desktop
; i
++) {
553 tskbar
= &panel
->taskbar
[i
];
554 if (tskbar
->area
.on_screen
&& x
>= tskbar
->area
.posx
&& x
<= (tskbar
->area
.posx
+ tskbar
->area
.width
))
559 for (i
=0; i
< panel
->nb_desktop
; i
++) {
560 tskbar
= &panel
->taskbar
[i
];
561 if (tskbar
->area
.on_screen
&& y
>= tskbar
->area
.posy
&& y
<= (tskbar
->area
.posy
+ tskbar
->area
.height
))
569 Task
*click_task (Panel
*panel
, int x
, int y
)
574 if ( (tskbar
= click_taskbar(panel
, x
, y
)) ) {
575 if (panel_horizontal
) {
577 for (l0
= tskbar
->area
.list
; l0
; l0
= l0
->next
) {
579 if (tsk
->area
.on_screen
&& x
>= tsk
->area
.posx
&& x
<= (tsk
->area
.posx
+ tsk
->area
.width
)) {
586 for (l0
= tskbar
->area
.list
; l0
; l0
= l0
->next
) {
588 if (tsk
->area
.on_screen
&& y
>= tsk
->area
.posy
&& y
<= (tsk
->area
.posy
+ tsk
->area
.height
)) {
598 int click_padding(Panel
*panel
, int x
, int y
)
600 if (panel_horizontal
) {
601 if (x
< panel
->area
.paddingxlr
|| x
> panel
->area
.width
-panel
->area
.paddingxlr
)
605 if (y
< panel
->area
.paddingxlr
|| y
> panel
->area
.height
-panel
->area
.paddingxlr
)
612 int click_clock(Panel
*panel
, int x
, int y
)
614 Clock clk
= panel
->clock
;
615 if (panel_horizontal
) {
616 if (clk
.area
.on_screen
&& x
>= clk
.area
.posx
&& x
<= (clk
.area
.posx
+ clk
.area
.width
))
619 if (clk
.area
.on_screen
&& y
>= clk
.area
.posy
&& y
<= (clk
.area
.posy
+ clk
.area
.height
))
626 Area
* click_area(Panel
*panel
, int x
, int y
)
628 Area
* result
= &panel
->area
;
629 Area
* new_result
= result
;
632 GSList
* it
= result
->list
;
635 if (panel_horizontal
) {
636 if (a
->on_screen
&& x
>= a
->posx
&& x
<= (a
->posx
+ a
->width
)) {
641 if (a
->on_screen
&& y
>= a
->posy
&& y
<= (a
->posy
+ a
->height
)) {
648 } while (new_result
!= result
);