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