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
) {
82 // remove_icon change systray.list_icons
83 while(systray
.list_icons
)
84 remove_icon((TrayWindow
*)systray
.list_icons
->data
);
86 g_slist_free(systray
.list_icons
);
87 systray
.list_icons
= 0;
90 free_area(&systray
.area
);
95 void draw_systray(void *obj
, cairo_t
*c
, int active
)
97 // tint2 don't draw systray icons. just the background.
102 void resize_systray(void *obj
)
104 Systraybar
*sysbar
= obj
;
105 Panel
*panel
= sysbar
->area
.panel
;
108 int count
, posx
, posy
;
111 if (panel_horizontal
)
112 icon_size
= sysbar
->area
.height
;
114 icon_size
= sysbar
->area
.width
;
115 icon_size
= icon_size
- (2 * sysbar
->area
.pix
.border
.width
) - (2 * sysbar
->area
.paddingy
);
116 count
= g_slist_length(systray
.list_icons
);
118 if (panel_horizontal
) {
119 if (!count
) systray
.area
.width
= 0;
120 else systray
.area
.width
= (2 * systray
.area
.pix
.border
.width
) + (2 * systray
.area
.paddingxlr
) + (icon_size
* count
) + ((count
-1) * systray
.area
.paddingx
);
122 systray
.area
.posx
= panel
->area
.width
- panel
->area
.pix
.border
.width
- panel
->area
.paddingxlr
- systray
.area
.width
;
123 if (panel
->clock
.area
.on_screen
)
124 systray
.area
.posx
-= (panel
->clock
.area
.width
+ panel
->area
.paddingx
);
125 #ifdef ENABLE_BATTERY
126 if (panel
->battery
.area
.on_screen
)
127 systray
.area
.posx
-= (panel
->battery
.area
.width
+ panel
->area
.paddingx
);
131 if (!count
) systray
.area
.height
= 0;
132 else systray
.area
.height
= (2 * systray
.area
.pix
.border
.width
) + (2 * systray
.area
.paddingxlr
) + (icon_size
* count
) + ((count
-1) * systray
.area
.paddingx
);
134 systray
.area
.posy
= panel
->area
.pix
.border
.width
+ panel
->area
.paddingxlr
;
135 if (panel
->clock
.area
.on_screen
)
136 systray
.area
.posy
+= (panel
->clock
.area
.height
+ panel
->area
.paddingx
);
137 #ifdef ENABLE_BATTERY
138 if (panel
->battery
.area
.on_screen
)
139 systray
.area
.posy
+= (panel
->battery
.area
.height
+ panel
->area
.paddingx
);
143 if (panel_horizontal
) {
144 posy
= panel
->area
.pix
.border
.width
+ panel
->area
.paddingy
+ systray
.area
.pix
.border
.width
+ systray
.area
.paddingy
;
145 posx
= systray
.area
.posx
+ systray
.area
.pix
.border
.width
+ systray
.area
.paddingxlr
;
148 posx
= panel
->area
.pix
.border
.width
+ panel
->area
.paddingy
+ systray
.area
.pix
.border
.width
+ systray
.area
.paddingy
;
149 posy
= systray
.area
.posy
+ systray
.area
.pix
.border
.width
+ systray
.area
.paddingxlr
;
151 for (l
= systray
.list_icons
; l
; l
= l
->next
) {
152 traywin
= (TrayWindow
*)l
->data
;
156 traywin
->width
= icon_size
;
157 traywin
->height
= icon_size
;
158 if (panel_horizontal
)
159 posx
+= (icon_size
+ systray
.area
.paddingx
);
161 posy
+= (icon_size
+ systray
.area
.paddingx
);
163 // position and size the icon window
164 XMoveResizeWindow(server
.dsp
, traywin
->id
, traywin
->x
, traywin
->y
, icon_size
, icon_size
);
169 // ***********************************************
174 // freedesktop systray specification
175 if (XGetSelectionOwner(server
.dsp
, server
.atom
._NET_SYSTEM_TRAY_SCREEN
) != None
) {
176 fprintf(stderr
, "tint2 : another systray is running\n");
180 // init systray protocol
181 net_sel_win
= XCreateSimpleWindow(server
.dsp
, server
.root_win
, -1, -1, 1, 1, 0, 0, 0);
183 // v0.2 trayer specification. tint2 always orizontal.
184 // TODO : vertical panel ??
186 XChangeProperty(server
.dsp
, net_sel_win
, server
.atom
._NET_SYSTEM_TRAY_ORIENTATION
, XA_CARDINAL
, 32, PropModeReplace
, (unsigned char *) &orient
, 1);
188 XSetSelectionOwner(server
.dsp
, server
.atom
._NET_SYSTEM_TRAY_SCREEN
, net_sel_win
, CurrentTime
);
189 if (XGetSelectionOwner(server
.dsp
, server
.atom
._NET_SYSTEM_TRAY_SCREEN
) != net_sel_win
) {
190 fprintf(stderr
, "tint2 : can't get systray manager\n");
194 XClientMessageEvent ev
;
195 ev
.type
= ClientMessage
;
196 ev
.window
= server
.root_win
;
197 ev
.message_type
= server
.atom
.MANAGER
;
199 ev
.data
.l
[0] = CurrentTime
;
200 ev
.data
.l
[1] = server
.atom
._NET_SYSTEM_TRAY_SCREEN
;
201 ev
.data
.l
[2] = net_sel_win
;
204 XSendEvent(server
.dsp
, server
.root_win
, False
, StructureNotifyMask
, (XEvent
*)&ev
);
211 if (net_sel_win
!= None
) {
212 XDestroyWindow(server
.dsp
, net_sel_win
);
219 int window_error_handler(Display
*d
, XErrorEvent
*e
)
223 if (e
->error_code
!= BadWindow
) {
224 printf("error_handler %d\n", e
->error_code
);
230 gboolean
add_icon(Window id
)
234 Panel
*panel
= systray
.area
.panel
;
237 old
= XSetErrorHandler(window_error_handler
);
238 XReparentWindow(server
.dsp
, id
, panel
->main_win
, 0, 0);
239 XSync(server
.dsp
, False
);
240 XSetErrorHandler(old
);
241 if (error
!= FALSE
) {
242 fprintf(stderr
, "tint2 : not icon_swallow\n");
249 unsigned long nbitem
, bytes
;
250 unsigned char *data
= 0;
253 ret
= XGetWindowProperty(server
.dsp
, id
, server
.atom
._XEMBED_INFO
, 0, 2, False
, server
.atom
._XEMBED_INFO
, &acttype
, &actfmt
, &nbitem
, &bytes
, &data
);
254 if (data
) XFree(data
);
255 if (ret
!= Success
) {
256 fprintf(stderr
, "tint2 : xembed error\n");
262 e
.xclient
.type
= ClientMessage
;
263 e
.xclient
.serial
= 0;
264 e
.xclient
.send_event
= True
;
265 e
.xclient
.message_type
= server
.atom
._XEMBED
;
266 e
.xclient
.window
= id
;
267 e
.xclient
.format
= 32;
268 e
.xclient
.data
.l
[0] = CurrentTime
;
269 e
.xclient
.data
.l
[1] = XEMBED_EMBEDDED_NOTIFY
;
270 e
.xclient
.data
.l
[2] = 0;
271 e
.xclient
.data
.l
[3] = panel
->main_win
;
272 e
.xclient
.data
.l
[4] = 0;
273 XSendEvent(server
.dsp
, id
, False
, 0xFFFFFF, &e
);
276 traywin
= g_new0(TrayWindow
, 1);
279 systray
.list_icons
= g_slist_prepend(systray
.list_icons
, traywin
);
280 systray
.area
.resize
= 1;
281 systray
.area
.redraw
= 1;
282 //printf("add_icon id %lx, %d\n", id, g_slist_length(systray.list_icons));
284 // watch for the icon trying to resize itself!
285 XSelectInput(server
.dsp
, traywin
->id
, StructureNotifyMask
);
288 XMapRaised(server
.dsp
, traywin
->id
);
290 // changed in systray force resize on panel
291 panel
->area
.resize
= 1;
297 void remove_icon(TrayWindow
*traywin
)
300 Window id
= traywin
->id
;
302 // remove from our list
303 systray
.list_icons
= g_slist_remove(systray
.list_icons
, traywin
);
305 systray
.area
.resize
= 1;
306 systray
.area
.redraw
= 1;
307 //printf("remove_icon id %lx, %d\n", traywin->id);
309 XSelectInput(server
.dsp
, id
, NoEventMask
);
313 old
= XSetErrorHandler(window_error_handler
);
314 XUnmapWindow(server
.dsp
, id
);
315 XReparentWindow(server
.dsp
, id
, server
.root_win
, 0, 0);
316 XSync(server
.dsp
, False
);
317 XSetErrorHandler(old
);
319 // changed in systray force resize on panel
320 Panel
*panel
= systray
.area
.panel
;
321 panel
->area
.resize
= 1;
326 void net_message(XClientMessageEvent
*e
)
328 unsigned long opcode
;
331 opcode
= e
->data
.l
[1];
333 case SYSTEM_TRAY_REQUEST_DOCK
:
335 if (id
) add_icon(id
);
338 case SYSTEM_TRAY_BEGIN_MESSAGE
:
339 case SYSTEM_TRAY_CANCEL_MESSAGE
:
340 // we don't show baloons messages.
344 if (opcode
== server
.atom
._NET_SYSTEM_TRAY_MESSAGE_DATA
)
345 printf("message from dockapp: %s\n", e
->data
.b
);
347 fprintf(stderr
, "SYSTEM_TRAY : unknown message type\n");
353 void refresh_systray_icon()
357 for (l
= systray
.list_icons
; l
; l
= l
->next
) {
358 traywin
= (TrayWindow
*)l
->data
;
359 XClearArea(server
.dsp
, traywin
->id
, 0, 0, traywin
->width
, traywin
->height
, True
);