]> Dogcows Code - chaz/tint2/blob - src/systray/systraybar.c
c014fd91362e842061b90ac040b451af2389e8b4
[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 // remove_icon change systray.list_icons
83 while(systray.list_icons)
84 remove_icon((TrayWindow*)systray.list_icons->data);
85
86 g_slist_free(systray.list_icons);
87 systray.list_icons = 0;
88 }
89
90 free_area(&systray.area);
91 cleanup_net();
92 }
93
94
95 void draw_systray(void *obj, cairo_t *c, int active)
96 {
97 // tint2 don't draw systray icons. just the background.
98 refresh_systray = 1;
99 }
100
101
102 void resize_systray(void *obj)
103 {
104 Systraybar *sysbar = obj;
105 Panel *panel = sysbar->area.panel;
106 TrayWindow *traywin;
107 GSList *l;
108 int count, posx, posy;
109 int icon_size;
110
111 if (panel_horizontal)
112 icon_size = sysbar->area.height;
113 else
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);
117
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);
121
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);
128 #endif
129 }
130 else {
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);
133
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);
140 #endif
141 }
142
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;
146 }
147 else {
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;
150 }
151 for (l = systray.list_icons; l ; l = l->next) {
152 traywin = (TrayWindow*)l->data;
153
154 traywin->y = posy;
155 traywin->x = posx;
156 traywin->width = icon_size;
157 traywin->height = icon_size;
158 if (panel_horizontal)
159 posx += (icon_size + systray.area.paddingx);
160 else
161 posy += (icon_size + systray.area.paddingx);
162
163 // position and size the icon window
164 XMoveResizeWindow(server.dsp, traywin->id, traywin->x, traywin->y, icon_size, icon_size);
165 }
166 }
167
168
169 // ***********************************************
170 // systray protocol
171
172 int init_net()
173 {
174 Window win = XGetSelectionOwner(server.dsp, server.atom._NET_SYSTEM_TRAY_SCREEN);
175
176 // freedesktop systray specification
177 if (win != None) {
178 // search pid
179 Atom _NET_WM_PID, actual_type;
180 int actual_format;
181 unsigned long nitems;
182 unsigned long bytes_after;
183 unsigned char *prop;
184 int pid;
185
186 _NET_WM_PID = XInternAtom(server.dsp, "_NET_WM_PID", True);
187 //atom_name = XGetAtomName (dpy,atom);
188
189 int ret = XGetWindowProperty(server.dsp, win, _NET_WM_PID, 0, 1024, False, AnyPropertyType, &actual_type, &actual_format, &nitems, &bytes_after, &prop);
190
191 fprintf(stderr, "tint2 : another systray is running");
192 if (ret == 0) {
193 pid = prop[1] * 256;
194 pid += prop[0];
195 fprintf(stderr, " pid=%d", pid);
196 }
197 fprintf(stderr, "\n");
198 return 0;
199 }
200
201 // init systray protocol
202 net_sel_win = XCreateSimpleWindow(server.dsp, server.root_win, -1, -1, 1, 1, 0, 0, 0);
203
204 // v0.2 trayer specification. tint2 always orizontal.
205 // TODO : vertical panel ??
206 int orient = 0;
207 XChangeProperty(server.dsp, net_sel_win, server.atom._NET_SYSTEM_TRAY_ORIENTATION, XA_CARDINAL, 32, PropModeReplace, (unsigned char *) &orient, 1);
208
209 XSetSelectionOwner(server.dsp, server.atom._NET_SYSTEM_TRAY_SCREEN, net_sel_win, CurrentTime);
210 if (XGetSelectionOwner(server.dsp, server.atom._NET_SYSTEM_TRAY_SCREEN) != net_sel_win) {
211 fprintf(stderr, "tint2 : can't get systray manager\n");
212 return 0;
213 }
214
215 XClientMessageEvent ev;
216 ev.type = ClientMessage;
217 ev.window = server.root_win;
218 ev.message_type = server.atom.MANAGER;
219 ev.format = 32;
220 ev.data.l[0] = CurrentTime;
221 ev.data.l[1] = server.atom._NET_SYSTEM_TRAY_SCREEN;
222 ev.data.l[2] = net_sel_win;
223 ev.data.l[3] = 0;
224 ev.data.l[4] = 0;
225 XSendEvent(server.dsp, server.root_win, False, StructureNotifyMask, (XEvent*)&ev);
226 return 1;
227 }
228
229
230 void cleanup_net()
231 {
232 if (net_sel_win != None) {
233 XDestroyWindow(server.dsp, net_sel_win);
234 net_sel_win = None;
235 }
236 }
237
238
239 gboolean error;
240 int window_error_handler(Display *d, XErrorEvent *e)
241 {
242 d=d;e=e;
243 error = TRUE;
244 if (e->error_code != BadWindow) {
245 printf("error_handler %d\n", e->error_code);
246 }
247 return 0;
248 }
249
250
251 static gint compare_traywindows(gconstpointer a, gconstpointer b)
252 {
253 const TrayWindow * traywin_a = (TrayWindow*)a;
254 const TrayWindow * traywin_b = (TrayWindow*)b;
255 XTextProperty name_a, name_b;
256
257 if(XGetWMName(server.dsp, traywin_a->id, &name_a) == 0) {
258 return -1;
259 }
260 else if(XGetWMName(server.dsp, traywin_b->id, &name_b) == 0) {
261 XFree(name_a.value);
262 return 1;
263 }
264 else {
265 gint retval = g_ascii_strncasecmp((char*)name_a.value, (char*)name_b.value, -1) * systray.sort;
266 XFree(name_a.value);
267 XFree(name_b.value);
268 return retval;
269 }
270 }
271
272
273 gboolean add_icon(Window id)
274 {
275 TrayWindow *traywin;
276 XErrorHandler old;
277 Panel *panel = systray.area.panel;
278
279 error = FALSE;
280 old = XSetErrorHandler(window_error_handler);
281 XReparentWindow(server.dsp, id, panel->main_win, 0, 0);
282 XSync(server.dsp, False);
283 XSetErrorHandler(old);
284 if (error != FALSE) {
285 fprintf(stderr, "tint2 : not icon_swallow\n");
286 return FALSE;
287 }
288
289 {
290 Atom acttype;
291 int actfmt;
292 unsigned long nbitem, bytes;
293 unsigned char *data = 0;
294 int ret;
295
296 ret = XGetWindowProperty(server.dsp, id, server.atom._XEMBED_INFO, 0, 2, False, server.atom._XEMBED_INFO, &acttype, &actfmt, &nbitem, &bytes, &data);
297 if (data) XFree(data);
298 if (ret != Success) {
299 fprintf(stderr, "tint2 : xembed error\n");
300 return FALSE;
301 }
302 }
303 {
304 XEvent e;
305 e.xclient.type = ClientMessage;
306 e.xclient.serial = 0;
307 e.xclient.send_event = True;
308 e.xclient.message_type = server.atom._XEMBED;
309 e.xclient.window = id;
310 e.xclient.format = 32;
311 e.xclient.data.l[0] = CurrentTime;
312 e.xclient.data.l[1] = XEMBED_EMBEDDED_NOTIFY;
313 e.xclient.data.l[2] = 0;
314 e.xclient.data.l[3] = panel->main_win;
315 e.xclient.data.l[4] = 0;
316 XSendEvent(server.dsp, id, False, 0xFFFFFF, &e);
317 }
318
319 traywin = g_new0(TrayWindow, 1);
320 traywin->id = id;
321
322 // systray.list_icons = g_slist_prepend(systray.list_icons, traywin);
323 systray.list_icons = g_slist_insert_sorted(systray.list_icons, traywin, compare_traywindows);
324 systray.area.resize = 1;
325 systray.area.redraw = 1;
326 //printf("add_icon id %lx, %d\n", id, g_slist_length(systray.list_icons));
327
328 // watch for the icon trying to resize itself!
329 XSelectInput(server.dsp, traywin->id, StructureNotifyMask);
330
331 // show the window
332 XMapRaised(server.dsp, traywin->id);
333
334 // changed in systray force resize on panel
335 panel->area.resize = 1;
336 panel_refresh = 1;
337 return TRUE;
338 }
339
340
341 void remove_icon(TrayWindow *traywin)
342 {
343 XErrorHandler old;
344 Window id = traywin->id;
345
346 // remove from our list
347 systray.list_icons = g_slist_remove(systray.list_icons, traywin);
348 g_free(traywin);
349 systray.area.resize = 1;
350 systray.area.redraw = 1;
351 //printf("remove_icon id %lx, %d\n", traywin->id);
352
353 XSelectInput(server.dsp, id, NoEventMask);
354
355 // reparent to root
356 error = FALSE;
357 old = XSetErrorHandler(window_error_handler);
358 XUnmapWindow(server.dsp, id);
359 XReparentWindow(server.dsp, id, server.root_win, 0, 0);
360 XSync(server.dsp, False);
361 XSetErrorHandler(old);
362
363 // changed in systray force resize on panel
364 Panel *panel = systray.area.panel;
365 panel->area.resize = 1;
366 panel_refresh = 1;
367 }
368
369
370 void net_message(XClientMessageEvent *e)
371 {
372 unsigned long opcode;
373 Window id;
374
375 opcode = e->data.l[1];
376 switch (opcode) {
377 case SYSTEM_TRAY_REQUEST_DOCK:
378 id = e->data.l[2];
379 if (id) add_icon(id);
380 break;
381
382 case SYSTEM_TRAY_BEGIN_MESSAGE:
383 case SYSTEM_TRAY_CANCEL_MESSAGE:
384 // we don't show baloons messages.
385 break;
386
387 default:
388 if (opcode == server.atom._NET_SYSTEM_TRAY_MESSAGE_DATA)
389 printf("message from dockapp: %s\n", e->data.b);
390 else
391 fprintf(stderr, "SYSTEM_TRAY : unknown message type\n");
392 break;
393 }
394 }
395
396
397 void refresh_systray_icon()
398 {
399 TrayWindow *traywin;
400 GSList *l;
401 for (l = systray.list_icons; l ; l = l->next) {
402 traywin = (TrayWindow*)l->data;
403 XClearArea(server.dsp, traywin->id, 0, 0, traywin->width, traywin->height, True);
404 }
405 }
406
407
This page took 0.052266 seconds and 4 git commands to generate.