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
42 void init_systray(Systraybar
*sysbar
, Area
*parent
)
44 Panel
*panel
= (Panel
*)parent
;
46 sysbar
->area
.parent
= parent
;
47 sysbar
->area
.panel
= panel
;
49 sysbar
->area
.posy
= parent
->pix
.border
.width
+ parent
->paddingy
;
50 sysbar
->area
.height
= parent
->height
- (2 * sysbar
->area
.posy
);
51 sysbar
->area
.width
= 100;
53 sysbar
->area
.posx
= panel
->area
.width
- panel
->clock
.area
.width
- panel
->area
.paddingxlr
- panel
->area
.pix
.border
.width
- panel
->area
.paddingx
- sysbar
->area
.width
;
55 sysbar
->area
.redraw
= 1;
57 //printf("init_systray");
63 // net_sel_atom == server.atom._NET_SYSTEM_TRAY
64 // net_opcode_atom == server.atom._NET_SYSTEM_TRAY_OPCODE
65 // net_manager_atom == server.atom.MANAGER
66 // net_message_data_atom == server.atom._NET_SYSTEM_TRAY_MESSAGE_DATA
68 int resize_systray (Systraybar
*sysbar
)
85 //* find the proper width and height
88 for (it
= icons
; it
!= NULL
; it
= g_slist_next(it
)) {
92 XResizeWindow(server
.dsp
, win
, width
+ border
* 2, height
+ border
* 2);
96 static void net_create_selection_window()
98 net_sel_win
= XCreateSimpleWindow(server
.dsp
, root
, -1, -1, 1, 1, 0, 0, 0);
103 int window_error_handler(Display
*d
, XErrorEvent
*e
)
106 if (e
->error_code
== BadWindow
) {
109 //g_printerr("X ERROR NOT BAD WINDOW!\n");
116 gboolean
icon_swallow(TrayWindow
*traywin
)
121 old
= XSetErrorHandler(window_error_handler
);
122 XReparentWindow(server
.dsp
, traywin
->id
, win
, 0, 0);
123 XSync(server
.dsp
, False
);
124 XSetErrorHandler(old
);
130 // The traywin must have its id and type set.
131 gboolean
icon_add(Window id
)
135 traywin
= g_new0(TrayWindow
, 1);
138 if (!icon_swallow(traywin
)) {
143 // find the positon for the systray app window
144 int count
= g_slist_length(icons
);
145 traywin
->x
= border
+ ((width
% icon_size
) / 2) +
146 (count
% (width
/ icon_size
)) * icon_size
;
147 traywin
->y
= border
+ ((height
% icon_size
) / 2) +
148 (count
/ (height
/ icon_size
)) * icon_size
;
150 // add the new icon to the list
151 icons
= g_slist_append(icons
, traywin
);
153 // watch for the icon trying to resize itself!
154 XSelectInput(server
.dsp
, traywin
->id
, StructureNotifyMask
);
156 // position and size the icon window
157 XMoveResizeWindow(server
.dsp
, traywin
->id
, traywin
->x
, traywin
->y
, icon_size
, icon_size
);
159 // resize our window so that the new window can fit in it
162 // flush before clearing, otherwise the clear isn't effective.
164 // make sure the new child will get the right stuff in its background
165 // for ParentRelative.
166 XClearWindow(server
.dsp
, win
);
169 XMapRaised(server
.dsp
, traywin
->id
);
177 // init systray protocol
178 net_sel_win
= XCreateSimpleWindow(server
.dsp
, server
.root_win
, -1, -1, 1, 1, 0, 0, 0);
180 XSetSelectionOwner(server
.dsp
, server
.atom
._NET_SYSTEM_TRAY
, net_sel_win
, CurrentTime
);
181 if (XGetSelectionOwner(server
.dsp
, server
.atom
._NET_SYSTEM_TRAY
) != net_sel_win
) {
182 fprintf(stderr
, "tint error : can't get trayer selection");
187 m
.type
= ClientMessage
;
188 m
.xclient
.message_type
= server
.atom
.MANAGER
;
189 m
.xclient
.format
= 32;
190 m
.xclient
.data
.l
[0] = CurrentTime
;
191 m
.xclient
.data
.l
[1] = server
.atom
._NET_SYSTEM_TRAY
;
192 m
.xclient
.data
.l
[2] = net_sel_win
;
193 m
.xclient
.data
.l
[3] = 0;
194 m
.xclient
.data
.l
[4] = 0;
195 XSendEvent(server
.dsp
, server
.root_win
, False
, StructureNotifyMask
, &m
);
199 void net_message(XClientMessageEvent
*e
)
201 unsigned long opcode
;
204 opcode
= e
->data
.l
[1];
208 case SYSTEM_TRAY_REQUEST_DOCK
: /* dock a new icon */
210 if (id
&& icon_add(id
))
211 XSelectInput(server
.dsp
, id
, StructureNotifyMask
);
214 case SYSTEM_TRAY_BEGIN_MESSAGE
:
215 //g_printerr("Message From Dockapp\n");
219 case SYSTEM_TRAY_CANCEL_MESSAGE
:
220 //g_printerr("Message Cancelled\n");
225 if (opcode
== server
.atom
._NET_SYSTEM_TRAY_MESSAGE_DATA
) {
226 //g_printerr("Text For Message From Dockapp:\n%s\n", e->data.b);
231 /* unknown message type. not in the spec. */
232 //g_printerr("Warning: Received unknown client message to System Tray selection window.\n");
246 while (XPending(server.dsp)) {
247 XNextEvent(display, &e);
252 // systray window list has changed?
253 if (e.xproperty.atom == kde_systray_prop) {
254 XSelectInput(display, win, NoEventMask);
256 XSelectInput(display, win, StructureNotifyMask);
258 while (XCheckTypedEvent(display, PropertyNotify, &e));
263 case ConfigureNotify:
264 if (e.xany.window != win) {
265 // find the icon it pertains to and beat it into submission
268 for (it = icons; it != NULL; it = g_slist_next(it)) {
269 TrayWindow *traywin = it->data;
270 if (traywin->id == e.xany.window) {
271 XMoveResizeWindow(display, traywin->id, traywin->x, traywin->y,
272 icon_size, icon_size);
279 // briefly cover the entire containing window, which causes it and
280 // all of the icons to refresh their windows. finally, they update
281 // themselves when the background of the main window's parent changes.
283 cover = XCreateSimpleWindow(display, win, 0, 0,
284 border * 2 + width, border * 2 + height,
286 XMapWindow(display, cover);
287 XDestroyWindow(display, cover);
292 if (e.xany.window == win) // reparented to us
296 for (it = icons; it; it = g_slist_next(it)) {
297 if (((TrayWindow*)it->data)->id == e.xany.window) {
305 if (e.xclient.message_type == net_opcode_atom &&
306 e.xclient.format == 32 &&
307 e.xclient.window == net_sel_win)
308 net_message(&e.xclient);
317 // remove/unparent all the icons
319 // do the remove here explicitly, cuz the event handler isn't going to