1 /**************************************************************************
4 * Copyright (C) 2009 thierry lorthiois (lorthiois@bbsoft.fr)
5 * based on 'docker-1.5' from Ben Jansens.
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License version 2
9 * as published by the Free Software Foundation.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 **************************************************************************/
21 #include <X11/Xutil.h>
22 #include <X11/Xatom.h>
29 #include "systraybar.h"
35 /* defined in the systray spec */
36 #define SYSTEM_TRAY_REQUEST_DOCK 0
37 #define SYSTEM_TRAY_BEGIN_MESSAGE 1
38 #define SYSTEM_TRAY_CANCEL_MESSAGE 2
41 Window net_sel_win
= None
, hint_win
= None
;
43 // freedesktop specification doesn't allow multi systray
50 Panel
*panel
= &panel1
[0];
52 if (systray
.area
.on_screen
)
53 systray
.area
.on_screen
= init_net();
55 if (!systray
.area
.on_screen
)
58 systray
.area
.parent
= panel
;
59 systray
.area
.panel
= panel
;
60 systray
.area
._draw_foreground
= draw_systray
;
61 systray
.area
._resize
= resize_systray
;
62 systray
.area
.resize
= 1;
63 systray
.area
.redraw
= 1;
67 // draw only one systray (even with multi panel)
68 if (panel_horizontal
) {
69 systray
.area
.posy
= panel
->area
.pix
.border
.width
+ panel
->area
.paddingy
;
70 systray
.area
.height
= panel
->area
.height
- (2 * systray
.area
.posy
);
73 systray
.area
.posx
= panel
->area
.pix
.border
.width
+ panel
->area
.paddingy
;
74 systray
.area
.width
= panel
->area
.width
- (2 * panel
->area
.pix
.border
.width
) - (2 * panel
->area
.paddingy
);
79 void cleanup_systray()
81 if (systray
.list_icons
) {
84 for (it
= systray
.list_icons
; it
; it
= it
->next
)
85 remove_icon((TrayWindow
*)it
->data
);
87 g_slist_free(systray
.list_icons
);
88 systray
.list_icons
= 0;
91 free_area(&systray
.area
);
96 void draw_systray(void *obj
, cairo_t
*c
, int active
)
98 // tint2 don't draw systray icons. just the background.
103 void resize_systray(void *obj
)
105 Systraybar
*sysbar
= obj
;
106 Panel
*panel
= sysbar
->area
.panel
;
109 int count
, posx
, posy
;
112 if (panel_horizontal
)
113 icon_size
= sysbar
->area
.height
;
115 icon_size
= sysbar
->area
.width
;
116 icon_size
= icon_size
- (2 * sysbar
->area
.pix
.border
.width
) - (2 * sysbar
->area
.paddingy
);
117 count
= g_slist_length(systray
.list_icons
);
119 if (panel_horizontal
) {
120 if (!count
) systray
.area
.width
= 0;
121 else systray
.area
.width
= (2 * systray
.area
.pix
.border
.width
) + (2 * systray
.area
.paddingxlr
) + (icon_size
* count
) + ((count
-1) * systray
.area
.paddingx
);
123 systray
.area
.posx
= panel
->area
.width
- panel
->area
.pix
.border
.width
- panel
->area
.paddingxlr
- systray
.area
.width
;
124 if (panel
->clock
.area
.on_screen
)
125 systray
.area
.posx
-= (panel
->clock
.area
.width
+ panel
->area
.paddingx
);
126 #ifdef ENABLE_BATTERY
127 if (panel
->battery
.area
.on_screen
)
128 systray
.area
.posx
-= (panel
->battery
.area
.width
+ panel
->area
.paddingx
);
132 if (!count
) systray
.area
.height
= 0;
133 else systray
.area
.height
= (2 * systray
.area
.pix
.border
.width
) + (2 * systray
.area
.paddingxlr
) + (icon_size
* count
) + ((count
-1) * systray
.area
.paddingx
);
135 systray
.area
.posy
= panel
->area
.pix
.border
.width
+ panel
->area
.paddingxlr
;
136 if (panel
->clock
.area
.on_screen
)
137 systray
.area
.posy
+= (panel
->clock
.area
.height
+ panel
->area
.paddingx
);
138 #ifdef ENABLE_BATTERY
139 if (panel
->battery
.area
.on_screen
)
140 systray
.area
.posy
+= (panel
->battery
.area
.height
+ panel
->area
.paddingx
);
144 if (panel_horizontal
) {
145 posy
= panel
->area
.pix
.border
.width
+ panel
->area
.paddingy
+ systray
.area
.pix
.border
.width
+ systray
.area
.paddingy
;
146 posx
= systray
.area
.posx
+ systray
.area
.pix
.border
.width
+ systray
.area
.paddingxlr
;
149 posx
= panel
->area
.pix
.border
.width
+ panel
->area
.paddingy
+ systray
.area
.pix
.border
.width
+ systray
.area
.paddingy
;
150 posy
= systray
.area
.posy
+ systray
.area
.pix
.border
.width
+ systray
.area
.paddingxlr
;
152 for (l
= systray
.list_icons
; l
; l
= l
->next
) {
153 traywin
= (TrayWindow
*)l
->data
;
157 traywin
->width
= icon_size
;
158 traywin
->height
= icon_size
;
159 if (panel_horizontal
)
160 posx
+= (icon_size
+ systray
.area
.paddingx
);
162 posy
+= (icon_size
+ systray
.area
.paddingx
);
164 // position and size the icon window
165 XMoveResizeWindow(server
.dsp
, traywin
->id
, traywin
->x
, traywin
->y
, icon_size
, icon_size
);
167 //printf("resize_systray %d %d\n", systray.area.posx, systray.area.width);
171 void create_hint_win()
174 XClassHint classhints;
175 Panel *panel = systray.area.panel;
177 hint_win = XCreateSimpleWindow(server.dsp, server.root_win, 0, 0, 1, 1, 0, 0, 0);
179 hints.flags = StateHint | WindowGroupHint | IconWindowHint;
180 hints.initial_state = WithdrawnState;
181 hints.window_group = hint_win;
182 hints.icon_window = panel->main_win;
184 classhints.res_name = "docker";
185 classhints.res_class = "Docker";
187 XSetWMProperties(server.dsp, hint_win, NULL, NULL, NULL, 0,
188 NULL, &hints, &classhints);
190 XMapWindow(server.dsp, hint_win);
196 if (XGetSelectionOwner(server
.dsp
, server
.atom
._NET_SYSTEM_TRAY_SCREEN
) != None
) {
197 fprintf(stderr
, "tint2 : another systray is running\n");
203 // init systray protocol
204 net_sel_win
= XCreateSimpleWindow(server
.dsp
, server
.root_win
, -1, -1, 1, 1, 0, 0, 0);
206 // v0.2 trayer specification. tint2 always orizontal.
208 XChangeProperty(server
.dsp
, net_sel_win
, server
.atom
._NET_SYSTEM_TRAY_ORIENTATION
, XA_CARDINAL
, 32, PropModeReplace
, (unsigned char *) &orient
, 1);
210 XSetSelectionOwner(server
.dsp
, server
.atom
._NET_SYSTEM_TRAY_SCREEN
, net_sel_win
, CurrentTime
);
211 if (XGetSelectionOwner(server
.dsp
, server
.atom
._NET_SYSTEM_TRAY_SCREEN
) != net_sel_win
) {
212 fprintf(stderr
, "tint2 : can't get systray manager\n");
216 XClientMessageEvent ev
;
217 ev
.type
= ClientMessage
;
218 ev
.window
= server
.root_win
;
219 ev
.message_type
= server
.atom
.MANAGER
;
221 ev
.data
.l
[0] = CurrentTime
;
222 ev
.data
.l
[1] = server
.atom
._NET_SYSTEM_TRAY_SCREEN
;
223 ev
.data
.l
[2] = net_sel_win
;
226 XSendEvent(server
.dsp
, server
.root_win
, False
, StructureNotifyMask
, (XEvent
*)&ev
);
233 if (net_sel_win
!= None
) {
234 XDestroyWindow(server
.dsp
, net_sel_win
);
241 int window_error_handler(Display
*d
, XErrorEvent
*e
)
245 if (e
->error_code
!= BadWindow
) {
246 printf("error_handler %d\n", e
->error_code
);
252 // The traywin must have its id and type set.
253 gboolean
add_icon(Window id
)
257 Panel
*panel
= systray
.area
.panel
;
260 old
= XSetErrorHandler(window_error_handler
);
261 XReparentWindow(server
.dsp
, id
, panel
->main_win
, 0, 0);
262 XSync(server
.dsp
, False
);
263 XSetErrorHandler(old
);
265 if (error
!= FALSE
) {
266 fprintf(stderr
, "tint2 : not icon_swallow\n");
270 traywin
= g_new0(TrayWindow
, 1);
273 systray
.list_icons
= g_slist_prepend(systray
.list_icons
, traywin
);
274 //printf("ajout d'un icone %d (%lx)\n", g_slist_length(systray.list_icons), id);
275 systray
.area
.resize
= 1;
276 systray
.area
.redraw
= 1;
278 // watch for the icon trying to resize itself!
279 XSelectInput(server
.dsp
, traywin
->id
, StructureNotifyMask
);
282 XMapRaised(server
.dsp
, traywin
->id
);
284 // changed in systray force resize on panel
285 panel
->area
.resize
= 1;
291 void remove_icon(TrayWindow
*traywin
)
295 XSelectInput(server
.dsp
, traywin
->id
, NoEventMask
);
299 old
= XSetErrorHandler(window_error_handler
);
300 XReparentWindow(server
.dsp
, traywin
->id
, server
.root_win
, 0, 0);
301 XSync(server
.dsp
, False
);
302 XSetErrorHandler(old
);
304 // remove from our list
305 systray
.list_icons
= g_slist_remove(systray
.list_icons
, traywin
);
307 //printf("suppression d'un icone %d\n", g_slist_length(systray.list_icons));
308 systray
.area
.resize
= 1;
309 systray
.area
.redraw
= 1;
311 // changed in systray force resize on panel
312 Panel
*panel
= systray
.area
.panel
;
313 panel
->area
.resize
= 1;
319 void net_message(XClientMessageEvent
*e
)
321 unsigned long opcode
;
324 opcode
= e
->data
.l
[1];
326 case SYSTEM_TRAY_REQUEST_DOCK
:
328 if (id
) add_icon(id
);
331 case SYSTEM_TRAY_BEGIN_MESSAGE
:
332 case SYSTEM_TRAY_CANCEL_MESSAGE
:
333 // we don't show baloons messages.
337 if (opcode
== server
.atom
._NET_SYSTEM_TRAY_MESSAGE_DATA
) {
338 printf("message from dockapp: %s\n", e
->data
.b
);
341 printf("SYSTEM_TRAY : unknown message type\n");
347 void refresh_systray_icon()
351 for (l
= systray
.list_icons
; l
; l
= l
->next
) {
352 traywin
= (TrayWindow
*)l
->data
;
353 XClearArea(server
.dsp
, traywin
->id
, 0, 0, traywin
->width
, traywin
->height
, True
);