]> Dogcows Code - chaz/tint2/blob - src/systray/systraybar.c
*fix* clear tooltip window before redrawing
[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 int systray_enabled;
47
48
49 void init_systray()
50 {
51 start_net();
52
53 if (!systray_enabled)
54 return;
55
56 systray.area._draw_foreground = draw_systray;
57 systray.area._resize = resize_systray;
58 systray.area.resize = 1;
59 systray.area.redraw = 1;
60 systray.area.on_screen = 1;
61 refresh_systray = 0;
62 }
63
64
65 void init_systray_panel(void *p)
66 {
67 Panel *panel =(Panel*)p;
68
69 if (panel_horizontal) {
70 systray.area.posy = panel->area.pix.border.width + panel->area.paddingy;
71 systray.area.height = panel->area.height - (2 * systray.area.posy);
72 }
73 else {
74 systray.area.posx = panel->area.pix.border.width + panel->area.paddingy;
75 systray.area.width = panel->area.width - (2 * panel->area.pix.border.width) - (2 * panel->area.paddingy);
76 }
77 systray.area.parent = p;
78 systray.area.panel = p;
79 }
80
81
82 void cleanup_systray()
83 {
84 systray_enabled = 0;
85 systray.area.on_screen = 0;
86 free_area(&systray.area);
87 }
88
89
90 void draw_systray(void *obj, cairo_t *c, int active)
91 {
92 // tint2 don't draw systray icons. just the background.
93 refresh_systray = 1;
94 }
95
96
97 void resize_systray(void *obj)
98 {
99 Systraybar *sysbar = obj;
100 Panel *panel = sysbar->area.panel;
101 TrayWindow *traywin;
102 GSList *l;
103 int count, posx, posy;
104 int icon_size;
105
106 if (panel_horizontal)
107 icon_size = sysbar->area.height;
108 else
109 icon_size = sysbar->area.width;
110 icon_size = icon_size - (2 * sysbar->area.pix.border.width) - (2 * sysbar->area.paddingy);
111 count = 0;
112 for (l = systray.list_icons; l ; l = l->next) {
113 if (!((TrayWindow*)l->data)->hide)
114 count++;
115 }
116 //printf("count %d\n", count);
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 if (traywin->hide) continue;
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 void start_net()
174 {
175 if (net_sel_win) {
176 // protocol already started
177 if (!systray_enabled)
178 stop_net();
179 return;
180 }
181 else
182 if (!systray_enabled)
183 return;
184
185 Window win = XGetSelectionOwner(server.dsp, server.atom._NET_SYSTEM_TRAY_SCREEN);
186
187 // freedesktop systray specification
188 if (win != None) {
189 // search pid
190 Atom _NET_WM_PID, actual_type;
191 int actual_format;
192 unsigned long nitems;
193 unsigned long bytes_after;
194 unsigned char *prop = 0;
195 int pid;
196
197 _NET_WM_PID = XInternAtom(server.dsp, "_NET_WM_PID", True);
198 int ret = XGetWindowProperty(server.dsp, win, _NET_WM_PID, 0, 1024, False, AnyPropertyType, &actual_type, &actual_format, &nitems, &bytes_after, &prop);
199
200 fprintf(stderr, "tint2 : another systray is running");
201 if (ret == Success && prop) {
202 pid = prop[1] * 256;
203 pid += prop[0];
204 fprintf(stderr, " pid=%d", pid);
205 }
206 fprintf(stderr, "\n");
207 return;
208 }
209
210 // init systray protocol
211 net_sel_win = XCreateSimpleWindow(server.dsp, server.root_win, -1, -1, 1, 1, 0, 0, 0);
212
213 // v0.2 trayer specification. tint2 always horizontal.
214 // Vertical panel will draw the systray horizontal.
215 int orient = 0;
216 XChangeProperty(server.dsp, net_sel_win, server.atom._NET_SYSTEM_TRAY_ORIENTATION, XA_CARDINAL, 32, PropModeReplace, (unsigned char *) &orient, 1);
217 VisualID vid = XVisualIDFromVisual(server.visual);
218 XChangeProperty(server.dsp, net_sel_win, XInternAtom(server.dsp, "_NET_SYSTEM_TRAY_VISUAL", False), XA_VISUALID, 32, PropModeReplace, (unsigned char*)&vid, 1);
219
220 XSetSelectionOwner(server.dsp, server.atom._NET_SYSTEM_TRAY_SCREEN, net_sel_win, CurrentTime);
221 if (XGetSelectionOwner(server.dsp, server.atom._NET_SYSTEM_TRAY_SCREEN) != net_sel_win) {
222 stop_net();
223 fprintf(stderr, "tint2 : can't get systray manager\n");
224 return;
225 }
226
227 //fprintf(stderr, "tint2 : systray started\n");
228 XClientMessageEvent ev;
229 ev.type = ClientMessage;
230 ev.window = server.root_win;
231 ev.message_type = server.atom.MANAGER;
232 ev.format = 32;
233 ev.data.l[0] = CurrentTime;
234 ev.data.l[1] = server.atom._NET_SYSTEM_TRAY_SCREEN;
235 ev.data.l[2] = net_sel_win;
236 ev.data.l[3] = 0;
237 ev.data.l[4] = 0;
238 XSendEvent(server.dsp, server.root_win, False, StructureNotifyMask, (XEvent*)&ev);
239 }
240
241
242 void stop_net()
243 {
244 //fprintf(stderr, "tint2 : systray stopped\n");
245 if (systray.list_icons) {
246 // remove_icon change systray.list_icons
247 while(systray.list_icons)
248 remove_icon((TrayWindow*)systray.list_icons->data);
249
250 g_slist_free(systray.list_icons);
251 systray.list_icons = 0;
252 }
253
254 if (net_sel_win != None) {
255 XDestroyWindow(server.dsp, net_sel_win);
256 net_sel_win = None;
257 }
258 }
259
260
261 gboolean error;
262 int window_error_handler(Display *d, XErrorEvent *e)
263 {
264 d=d;e=e;
265 error = TRUE;
266 if (e->error_code != BadWindow) {
267 printf("error_handler %d\n", e->error_code);
268 }
269 return 0;
270 }
271
272
273 static gint compare_traywindows(gconstpointer a, gconstpointer b)
274 {
275 const TrayWindow * traywin_a = (TrayWindow*)a;
276 const TrayWindow * traywin_b = (TrayWindow*)b;
277 XTextProperty name_a, name_b;
278
279 if(XGetWMName(server.dsp, traywin_a->id, &name_a) == 0) {
280 return -1;
281 }
282 else if(XGetWMName(server.dsp, traywin_b->id, &name_b) == 0) {
283 XFree(name_a.value);
284 return 1;
285 }
286 else {
287 gint retval = g_ascii_strncasecmp((char*)name_a.value, (char*)name_b.value, -1) * systray.sort;
288 XFree(name_a.value);
289 XFree(name_b.value);
290 return retval;
291 }
292 }
293
294
295 gboolean add_icon(Window id)
296 {
297 TrayWindow *traywin;
298 XErrorHandler old;
299 Panel *panel = systray.area.panel;
300 int hide = 0;
301
302 error = FALSE;
303 old = XSetErrorHandler(window_error_handler);
304 XWindowAttributes attr;
305 XGetWindowAttributes(server.dsp, id, &attr);
306 if ( attr.depth != server.depth ) {
307 XSetWindowAttributes a;
308 a.background_pixmap = None; // set to none, otherwise XReparentWindow fails...
309 a.background_pixel = 0; // set background pixel to 0. Looks ugly, but at least the icon appears
310 // TODO: maybe the XShape extension can be used, to clip the icon
311 XChangeWindowAttributes(server.dsp, id, CWBackPixmap|CWBackPixel, &a);
312 }
313 XReparentWindow(server.dsp, id, panel->main_win, 0, 0);
314 XSync(server.dsp, False);
315 XSetErrorHandler(old);
316 if (error != FALSE) {
317 fprintf(stderr, "tint2 : not icon_swallow\n");
318 return FALSE;
319 }
320
321 {
322 Atom acttype;
323 int actfmt;
324 unsigned long nbitem, bytes;
325 unsigned char *data = 0;
326 int ret;
327
328 ret = XGetWindowProperty(server.dsp, id, server.atom._XEMBED_INFO, 0, 2, False, server.atom._XEMBED_INFO, &acttype, &actfmt, &nbitem, &bytes, &data);
329 if (ret == Success) {
330 if (data) {
331 if (nbitem == 2) {
332 //hide = ((data[1] & XEMBED_MAPPED) == 0);
333 //printf("hide %d\n", hide);
334 }
335 XFree(data);
336 }
337 }
338 else {
339 fprintf(stderr, "tint2 : xembed error\n");
340 return FALSE;
341 }
342 }
343 {
344 XEvent e;
345 e.xclient.type = ClientMessage;
346 e.xclient.serial = 0;
347 e.xclient.send_event = True;
348 e.xclient.message_type = server.atom._XEMBED;
349 e.xclient.window = id;
350 e.xclient.format = 32;
351 e.xclient.data.l[0] = CurrentTime;
352 e.xclient.data.l[1] = XEMBED_EMBEDDED_NOTIFY;
353 e.xclient.data.l[2] = 0;
354 e.xclient.data.l[3] = panel->main_win;
355 e.xclient.data.l[4] = 0;
356 XSendEvent(server.dsp, id, False, 0xFFFFFF, &e);
357 }
358
359 traywin = g_new0(TrayWindow, 1);
360 traywin->id = id;
361 traywin->hide = hide;
362
363 if (systray.sort == 3)
364 systray.list_icons = g_slist_prepend(systray.list_icons, traywin);
365 else if (systray.sort == 2)
366 systray.list_icons = g_slist_append(systray.list_icons, traywin);
367 else
368 systray.list_icons = g_slist_insert_sorted(systray.list_icons, traywin, compare_traywindows);
369 systray.area.resize = 1;
370 systray.area.redraw = 1;
371 //printf("add_icon id %lx, %d\n", id, g_slist_length(systray.list_icons));
372
373 // watch for the icon trying to resize itself!
374 XSelectInput(server.dsp, traywin->id, StructureNotifyMask);
375
376 // show the window
377 if (!traywin->hide)
378 XMapRaised(server.dsp, traywin->id);
379
380 // changed in systray force resize on panel
381 panel->area.resize = 1;
382 panel_refresh = 1;
383 return TRUE;
384 }
385
386
387 void remove_icon(TrayWindow *traywin)
388 {
389 XErrorHandler old;
390
391 // remove from our list
392 systray.list_icons = g_slist_remove(systray.list_icons, traywin);
393 systray.area.resize = 1;
394 systray.area.redraw = 1;
395 //printf("remove_icon id %lx, %d\n", traywin->id);
396
397 XSelectInput(server.dsp, traywin->id, NoEventMask);
398
399 // reparent to root
400 error = FALSE;
401 old = XSetErrorHandler(window_error_handler);
402 if (!traywin->hide)
403 XUnmapWindow(server.dsp, traywin->id);
404 XReparentWindow(server.dsp, traywin->id, server.root_win, 0, 0);
405 XSync(server.dsp, False);
406 XSetErrorHandler(old);
407 g_free(traywin);
408
409 // changed in systray force resize on panel
410 Panel *panel = systray.area.panel;
411 panel->area.resize = 1;
412 panel_refresh = 1;
413 }
414
415
416 void net_message(XClientMessageEvent *e)
417 {
418 unsigned long opcode;
419 Window id;
420
421 opcode = e->data.l[1];
422 switch (opcode) {
423 case SYSTEM_TRAY_REQUEST_DOCK:
424 id = e->data.l[2];
425 if (id) add_icon(id);
426 break;
427
428 case SYSTEM_TRAY_BEGIN_MESSAGE:
429 case SYSTEM_TRAY_CANCEL_MESSAGE:
430 // we don't show baloons messages.
431 break;
432
433 default:
434 if (opcode == server.atom._NET_SYSTEM_TRAY_MESSAGE_DATA)
435 printf("message from dockapp: %s\n", e->data.b);
436 else
437 fprintf(stderr, "SYSTEM_TRAY : unknown message type\n");
438 break;
439 }
440 }
441
442
443 void refresh_systray_icon()
444 {
445 TrayWindow *traywin;
446 GSList *l;
447 for (l = systray.list_icons; l ; l = l->next) {
448 traywin = (TrayWindow*)l->data;
449 if (traywin->hide) continue;
450 XClearArea(server.dsp, traywin->id, 0, 0, traywin->width, traywin->height, True);
451 }
452 }
453
454
This page took 0.058638 seconds and 5 git commands to generate.