1 /**************************************************************************
4 * Copyright (C) 2009 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 **************************************************************************/
20 #include <X11/Xutil.h>
21 #include <X11/Xatom.h>
28 #include "systraybar.h"
34 /* defined in the systray spec */
35 #define SYSTEM_TRAY_REQUEST_DOCK 0
36 #define SYSTEM_TRAY_BEGIN_MESSAGE 1
37 #define SYSTEM_TRAY_CANCEL_MESSAGE 2
40 Window net_sel_win
= None
;
52 for (i
=0 ; i
< nb_panel
; i
++) {
53 if (panel1
[i
].systray
.area
.visible
) {
59 if (XGetSelectionOwner(server
.dsp
, server
.atom
._NET_SYSTEM_TRAY_SCREEN
) != None
) {
60 fprintf(stderr
, "tint2 : another systray is running\n");
66 run_systray
= net_init();
68 // configure sysbar on all panels
69 for (i
=0 ; i
< nb_panel
; i
++) {
71 sysbar
= &panel
->systray
;
74 sysbar
->area
.visible
= 0;
77 if (!sysbar
->area
.visible
)
80 sysbar
->area
.parent
= panel
;
81 sysbar
->area
.panel
= panel
;
83 sysbar
->area
.posy
= panel
->area
.pix
.border
.width
+ panel
->area
.paddingy
;
84 sysbar
->area
.height
= panel
->area
.height
- (2 * sysbar
->area
.posy
);
85 sysbar
->area
.width
= 100;
87 sysbar
->area
.posx
= panel
->area
.width
- panel
->area
.paddingxlr
- panel
->area
.pix
.border
.width
- sysbar
->area
.width
;
88 if (panel
->clock
.area
.visible
)
89 sysbar
->area
.posx
-= (panel
->clock
.area
.width
+ panel
->area
.paddingx
);
91 sysbar
->area
.redraw
= 1;
96 void cleanup_systray()
101 for (i
=0 ; i
< nb_panel
; i
++) {
103 if (!panel
->systray
.area
.visible
) continue;
105 free_area(&panel
->systray
.area
);
108 if (net_sel_win
!= None
) {
109 XDestroyWindow(server
.dsp
, net_sel_win
);
117 // init systray protocol
118 net_sel_win
= XCreateSimpleWindow(server
.dsp
, server
.root_win
, -1, -1, 1, 1, 0, 0, 0);
120 // v0.2 trayer specification. tint2 always orizontal.
122 XChangeProperty(server
.dsp
, net_sel_win
, server
.atom
._NET_SYSTEM_TRAY_ORIENTATION
, XA_CARDINAL
, 32, PropModeReplace
, (unsigned char *) &orient
, 1);
124 XSetSelectionOwner(server
.dsp
, server
.atom
._NET_SYSTEM_TRAY_SCREEN
, net_sel_win
, CurrentTime
);
125 if (XGetSelectionOwner(server
.dsp
, server
.atom
._NET_SYSTEM_TRAY_SCREEN
) != net_sel_win
) {
126 fprintf(stderr
, "tint2 : can't get systray manager\n");
130 XClientMessageEvent ev
;
131 ev
.type
= ClientMessage
;
132 ev
.window
= server
.root_win
;
133 ev
.message_type
= server
.atom
.MANAGER
;
135 ev
.data
.l
[0] = CurrentTime
;
136 ev
.data
.l
[1] = server
.atom
._NET_SYSTEM_TRAY_SCREEN
;
137 ev
.data
.l
[2] = net_sel_win
;
140 XSendEvent(server
.dsp
, server
.root_win
, False
, StructureNotifyMask
, &ev
);
146 int resize_systray (Systraybar
*sysbar
)
163 // find the proper width and height
166 for (it
= icons
; it
!= NULL
; it
= g_slist_next(it
)) {
170 XResizeWindow(server
.dsp
, win
, width
+ border
* 2, height
+ border
* 2);
175 int window_error_handler(Display
*d
, XErrorEvent
*e
)
178 if (e
->error_code
== BadWindow
) {
181 //g_printerr("X ERROR NOT BAD WINDOW!\n");
188 gboolean
icon_swallow(TrayWindow
*traywin
)
193 old
= XSetErrorHandler(window_error_handler
);
194 XReparentWindow(server
.dsp
, traywin
->id
, win
, 0, 0);
195 XSync(server
.dsp
, False
);
196 XSetErrorHandler(old
);
202 // The traywin must have its id and type set.
203 gboolean
icon_add(Window id
)
207 traywin
= g_new0(TrayWindow
, 1);
210 if (!icon_swallow(traywin
)) {
211 printf("not icon_swallow\n");
216 // find the positon for the systray app window
217 int count
= g_slist_length(icons
);
218 traywin
->x
= border
+ ((width
% icon_size
) / 2) +
219 (count
% (width
/ icon_size
)) * icon_size
;
220 traywin
->y
= border
+ ((height
% icon_size
) / 2) +
221 (count
/ (height
/ icon_size
)) * icon_size
;
223 // add the new icon to the list
224 icons
= g_slist_append(icons
, traywin
);
226 // watch for the icon trying to resize itself!
227 XSelectInput(server
.dsp
, traywin
->id
, StructureNotifyMask
);
229 // position and size the icon window
230 XMoveResizeWindow(server
.dsp
, traywin
->id
, traywin
->x
, traywin
->y
, icon_size
, icon_size
);
232 // resize our window so that the new window can fit in it
235 // flush before clearing, otherwise the clear isn't effective.
237 // make sure the new child will get the right stuff in its background
238 // for ParentRelative.
239 XClearWindow(server
.dsp
, win
);
242 XMapRaised(server
.dsp
, traywin
->id
);
248 void net_message(XClientMessageEvent
*e
)
250 unsigned long opcode
;
253 opcode
= e
->data
.l
[1];
256 case SYSTEM_TRAY_REQUEST_DOCK
:
259 printf("add dockapp\n");
260 if (id
&& icon_add(id
)) {
261 XSelectInput(server
.dsp
, id
, StructureNotifyMask
);
265 case SYSTEM_TRAY_BEGIN_MESSAGE
:
266 //g_printerr("Message From Dockapp\n");
270 case SYSTEM_TRAY_CANCEL_MESSAGE
:
271 //g_printerr("Message Cancelled\n");
276 if (opcode
== server
.atom
._NET_SYSTEM_TRAY_MESSAGE_DATA
) {
277 printf("message from dockapp:\n %s\n", e
->data
.b
);
280 // unknown message type. not in the spec
294 while (XPending(server.dsp)) {
295 XNextEvent(display, &e);
300 // systray window list has changed?
301 if (e.xproperty.atom == kde_systray_prop) {
302 XSelectInput(display, win, NoEventMask);
304 XSelectInput(display, win, StructureNotifyMask);
306 while (XCheckTypedEvent(display, PropertyNotify, &e));
311 case ConfigureNotify:
312 if (e.xany.window != win) {
313 // find the icon it pertains to and beat it into submission
316 for (it = icons; it != NULL; it = g_slist_next(it)) {
317 TrayWindow *traywin = it->data;
318 if (traywin->id == e.xany.window) {
319 XMoveResizeWindow(display, traywin->id, traywin->x, traywin->y,
320 icon_size, icon_size);
327 // briefly cover the entire containing window, which causes it and
328 // all of the icons to refresh their windows. finally, they update
329 // themselves when the background of the main window's parent changes.
331 cover = XCreateSimpleWindow(display, win, 0, 0,
332 border * 2 + width, border * 2 + height,
334 XMapWindow(display, cover);
335 XDestroyWindow(display, cover);
340 if (e.xany.window == win) // reparented to us
344 for (it = icons; it; it = g_slist_next(it)) {
345 if (((TrayWindow*)it->data)->id == e.xany.window) {
353 if (e.xclient.message_type == net_opcode_atom &&
354 e.xclient.format == 32 &&
355 e.xclient.window == net_sel_win)
356 net_message(&e.xclient);
365 // remove/unparent all the icons
367 // do the remove here explicitly, cuz the event handler isn't going to