]> Dogcows Code - chaz/tint2/blob - src/systray/systraybar.c
fixed issue 110 and issue 107
[chaz/tint2] / src / systray / systraybar.c
1 /**************************************************************************
2 * Tint2 : systraybar
3 *
4 * Copyright (C) 2009 thierry lorthiois (lorthiois@bbsoft.fr)
5 * based on 'docker-1.5' from Ben Jansens.
6 *
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.
10 *
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 **************************************************************************/
19
20 #include <X11/Xlib.h>
21 #include <X11/Xutil.h>
22 #include <X11/Xatom.h>
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <glib.h>
27 #include <Imlib2.h>
28
29 #include "systraybar.h"
30 #include "server.h"
31 #include "panel.h"
32
33 GSList *icons;
34
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
39
40 // selection window
41 Window net_sel_win = None, hint_win = None;
42
43 // freedesktop specification doesn't allow multi systray
44 Systraybar systray;
45 int refresh_systray;
46
47
48 void init_systray()
49 {
50 Panel *panel = &panel1[0];
51
52 if (systray.area.on_screen)
53 systray.area.on_screen = init_net();
54
55 if (!systray.area.on_screen)
56 return;
57
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;
64 refresh_systray = 0;
65
66 // configure systray
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);
71 }
72 else {
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);
75 }
76 }
77
78
79 void cleanup_systray()
80 {
81 if (systray.list_icons) {
82 GSList *it;
83
84 for (it = systray.list_icons; it; it = it->next)
85 remove_icon((TrayWindow*)it->data);
86
87 g_slist_free(systray.list_icons);
88 systray.list_icons = 0;
89 }
90
91 free_area(&systray.area);
92 cleanup_net();
93 }
94
95
96 void draw_systray(void *obj, cairo_t *c, int active)
97 {
98 // tint2 don't draw systray icons. just the background.
99 refresh_systray = 1;
100 }
101
102
103 void resize_systray(void *obj)
104 {
105 Systraybar *sysbar = obj;
106 Panel *panel = sysbar->area.panel;
107 TrayWindow *traywin;
108 GSList *l;
109 int count, posx, posy;
110 int icon_size;
111
112 if (panel_horizontal)
113 icon_size = sysbar->area.height;
114 else
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);
118
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);
122
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);
129 #endif
130 }
131 else {
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);
134
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);
141 #endif
142 }
143
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;
147 }
148 else {
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;
151 }
152 for (l = systray.list_icons; l ; l = l->next) {
153 traywin = (TrayWindow*)l->data;
154
155 traywin->y = posy;
156 traywin->x = posx;
157 traywin->width = icon_size;
158 traywin->height = icon_size;
159 if (panel_horizontal)
160 posx += (icon_size + systray.area.paddingx);
161 else
162 posy += (icon_size + systray.area.paddingx);
163
164 // position and size the icon window
165 XMoveResizeWindow(server.dsp, traywin->id, traywin->x, traywin->y, icon_size, icon_size);
166 }
167 }
168
169
170 // ***********************************************
171 // systray protocol
172
173 int init_net()
174 {
175 // freedesktop systray specification
176 if (XGetSelectionOwner(server.dsp, server.atom._NET_SYSTEM_TRAY_SCREEN) != None) {
177 fprintf(stderr, "tint2 : another systray is running\n");
178 return 0;
179 }
180
181 // init systray protocol
182 net_sel_win = XCreateSimpleWindow(server.dsp, server.root_win, -1, -1, 1, 1, 0, 0, 0);
183
184 // v0.2 trayer specification. tint2 always orizontal.
185 // TODO : vertical panel ??
186 int orient = 0;
187 XChangeProperty(server.dsp, net_sel_win, server.atom._NET_SYSTEM_TRAY_ORIENTATION, XA_CARDINAL, 32, PropModeReplace, (unsigned char *) &orient, 1);
188
189 XSetSelectionOwner(server.dsp, server.atom._NET_SYSTEM_TRAY_SCREEN, net_sel_win, CurrentTime);
190 if (XGetSelectionOwner(server.dsp, server.atom._NET_SYSTEM_TRAY_SCREEN) != net_sel_win) {
191 fprintf(stderr, "tint2 : can't get systray manager\n");
192 return 0;
193 }
194
195 XClientMessageEvent ev;
196 ev.type = ClientMessage;
197 ev.window = server.root_win;
198 ev.message_type = server.atom.MANAGER;
199 ev.format = 32;
200 ev.data.l[0] = CurrentTime;
201 ev.data.l[1] = server.atom._NET_SYSTEM_TRAY_SCREEN;
202 ev.data.l[2] = net_sel_win;
203 ev.data.l[3] = 0;
204 ev.data.l[4] = 0;
205 XSendEvent(server.dsp, server.root_win, False, StructureNotifyMask, (XEvent*)&ev);
206 return 1;
207 }
208
209
210 void cleanup_net()
211 {
212 if (net_sel_win != None) {
213 XDestroyWindow(server.dsp, net_sel_win);
214 net_sel_win = None;
215 }
216 }
217
218
219 gboolean error;
220 int window_error_handler(Display *d, XErrorEvent *e)
221 {
222 d=d;e=e;
223 error = TRUE;
224 if (e->error_code != BadWindow) {
225 printf("error_handler %d\n", e->error_code);
226 }
227 return 0;
228 }
229
230
231 gboolean add_icon(Window id)
232 {
233 TrayWindow *traywin;
234 XErrorHandler old;
235 Panel *panel = systray.area.panel;
236
237 error = FALSE;
238 old = XSetErrorHandler(window_error_handler);
239 XReparentWindow(server.dsp, id, panel->main_win, 0, 0);
240 XSync(server.dsp, False);
241 XSetErrorHandler(old);
242 if (error != FALSE) {
243 fprintf(stderr, "tint2 : not icon_swallow\n");
244 return FALSE;
245 }
246
247 {
248 Atom acttype;
249 int actfmt;
250 unsigned long nbitem, bytes;
251 unsigned char *data = 0;
252 int ret;
253
254 ret = XGetWindowProperty(server.dsp, id, server.atom._XEMBED_INFO, 0, 2, False, server.atom._XEMBED_INFO, &acttype, &actfmt, &nbitem, &bytes, &data);
255 if (data) XFree(data);
256 if (ret != Success) {
257 fprintf(stderr, "tint2 : xembed error\n");
258 return FALSE;
259 }
260 }
261 {
262 XEvent e;
263 e.xclient.type = ClientMessage;
264 e.xclient.serial = 0;
265 e.xclient.send_event = True;
266 e.xclient.message_type = server.atom._XEMBED;
267 e.xclient.window = id;
268 e.xclient.format = 32;
269 e.xclient.data.l[0] = CurrentTime;
270 e.xclient.data.l[1] = XEMBED_EMBEDDED_NOTIFY;
271 e.xclient.data.l[2] = 0;
272 e.xclient.data.l[3] = panel->main_win;
273 e.xclient.data.l[4] = 0;
274 XSendEvent(server.dsp, id, False, 0xFFFFFF, &e);
275 }
276
277 traywin = g_new0(TrayWindow, 1);
278 traywin->id = id;
279
280 systray.list_icons = g_slist_prepend(systray.list_icons, traywin);
281 systray.area.resize = 1;
282 systray.area.redraw = 1;
283 //printf("add_icon id %lx, %d\n", id, g_slist_length(systray.list_icons));
284
285 // watch for the icon trying to resize itself!
286 XSelectInput(server.dsp, traywin->id, StructureNotifyMask);
287
288 // show the window
289 XMapRaised(server.dsp, traywin->id);
290
291 // changed in systray force resize on panel
292 panel->area.resize = 1;
293 panel_refresh = 1;
294 return TRUE;
295 }
296
297
298 void remove_icon(TrayWindow *traywin)
299 {
300 XErrorHandler old;
301 Window id = traywin->id;
302
303 // remove from our list
304 systray.list_icons = g_slist_remove(systray.list_icons, traywin);
305 g_free(traywin);
306 systray.area.resize = 1;
307 systray.area.redraw = 1;
308 //printf("remove_icon id %lx, %d\n", traywin->id);
309
310 XSelectInput(server.dsp, id, NoEventMask);
311
312 // reparent to root
313 error = FALSE;
314 old = XSetErrorHandler(window_error_handler);
315 XUnmapWindow(server.dsp, id);
316 XReparentWindow(server.dsp, id, server.root_win, 0, 0);
317 XSync(server.dsp, False);
318 XSetErrorHandler(old);
319
320 // changed in systray force resize on panel
321 Panel *panel = systray.area.panel;
322 panel->area.resize = 1;
323 panel_refresh = 1;
324 }
325
326
327 void net_message(XClientMessageEvent *e)
328 {
329 unsigned long opcode;
330 Window id;
331
332 opcode = e->data.l[1];
333 switch (opcode) {
334 case SYSTEM_TRAY_REQUEST_DOCK:
335 id = e->data.l[2];
336 if (id) add_icon(id);
337 break;
338
339 case SYSTEM_TRAY_BEGIN_MESSAGE:
340 case SYSTEM_TRAY_CANCEL_MESSAGE:
341 // we don't show baloons messages.
342 break;
343
344 default:
345 if (opcode == server.atom._NET_SYSTEM_TRAY_MESSAGE_DATA)
346 printf("message from dockapp: %s\n", e->data.b);
347 else
348 fprintf(stderr, "SYSTEM_TRAY : unknown message type\n");
349 break;
350 }
351 }
352
353
354 void refresh_systray_icon()
355 {
356 TrayWindow *traywin;
357 GSList *l;
358 for (l = systray.list_icons; l ; l = l->next) {
359 traywin = (TrayWindow*)l->data;
360 XClearArea(server.dsp, traywin->id, 0, 0, traywin->width, traywin->height, True);
361 }
362 }
363
364
This page took 0.053809 seconds and 5 git commands to generate.