]> Dogcows Code - chaz/tint2/blob - src/systray/systraybar.c
*fix* broke systray with last checkin for fake transparency
[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 #include <X11/extensions/Xdamage.h>
29 #include <X11/extensions/Xrender.h>
30 #include <X11/extensions/Xcomposite.h>
31
32 #include "systraybar.h"
33 #include "server.h"
34 #include "panel.h"
35
36 GSList *icons;
37
38 /* defined in the systray spec */
39 #define SYSTEM_TRAY_REQUEST_DOCK 0
40 #define SYSTEM_TRAY_BEGIN_MESSAGE 1
41 #define SYSTEM_TRAY_CANCEL_MESSAGE 2
42
43 // selection window
44 Window net_sel_win = None, hint_win = None;
45
46 // freedesktop specification doesn't allow multi systray
47 Systraybar systray;
48 int refresh_systray;
49 int systray_enabled;
50
51
52 void init_systray()
53 {
54 start_net();
55
56 if (!systray_enabled)
57 return;
58
59 systray.area._draw_foreground = draw_systray;
60 systray.area._resize = resize_systray;
61 systray.area.resize = 1;
62 systray.area.redraw = 1;
63 systray.area.on_screen = 1;
64 refresh_systray = 0;
65 }
66
67
68 void init_systray_panel(void *p)
69 {
70 Panel *panel =(Panel*)p;
71
72 if (panel_horizontal) {
73 systray.area.posy = panel->area.pix.border.width + panel->area.paddingy;
74 systray.area.height = panel->area.height - (2 * systray.area.posy);
75 }
76 else {
77 systray.area.posx = panel->area.pix.border.width + panel->area.paddingy;
78 systray.area.width = panel->area.width - (2 * panel->area.pix.border.width) - (2 * panel->area.paddingy);
79 }
80 systray.area.parent = p;
81 systray.area.panel = p;
82 }
83
84
85 void cleanup_systray()
86 {
87 systray_enabled = 0;
88 systray.area.on_screen = 0;
89 free_area(&systray.area);
90 }
91
92
93 void draw_systray(void *obj, cairo_t *c, int active)
94 {
95 // tint2 don't draw systray icons. just the background.
96 refresh_systray = 1;
97 }
98
99
100 void resize_systray(void *obj)
101 {
102 Systraybar *sysbar = obj;
103 Panel *panel = sysbar->area.panel;
104 TrayWindow *traywin;
105 GSList *l;
106 int count, posx, posy;
107 int icon_size;
108
109 if (panel_horizontal)
110 icon_size = sysbar->area.height;
111 else
112 icon_size = sysbar->area.width;
113 icon_size = icon_size - (2 * sysbar->area.pix.border.width) - (2 * sysbar->area.paddingy);
114 count = 0;
115 for (l = systray.list_icons; l ; l = l->next) {
116 if (!((TrayWindow*)l->data)->hide)
117 count++;
118 }
119 //printf("count %d\n", count);
120
121 if (panel_horizontal) {
122 if (!count) systray.area.width = 0;
123 else systray.area.width = (2 * systray.area.pix.border.width) + (2 * systray.area.paddingxlr) + (icon_size * count) + ((count-1) * systray.area.paddingx);
124
125 systray.area.posx = panel->area.width - panel->area.pix.border.width - panel->area.paddingxlr - systray.area.width;
126 if (panel->clock.area.on_screen)
127 systray.area.posx -= (panel->clock.area.width + panel->area.paddingx);
128 #ifdef ENABLE_BATTERY
129 if (panel->battery.area.on_screen)
130 systray.area.posx -= (panel->battery.area.width + panel->area.paddingx);
131 #endif
132 }
133 else {
134 if (!count) systray.area.height = 0;
135 else systray.area.height = (2 * systray.area.pix.border.width) + (2 * systray.area.paddingxlr) + (icon_size * count) + ((count-1) * systray.area.paddingx);
136
137 systray.area.posy = panel->area.pix.border.width + panel->area.paddingxlr;
138 if (panel->clock.area.on_screen)
139 systray.area.posy += (panel->clock.area.height + panel->area.paddingx);
140 #ifdef ENABLE_BATTERY
141 if (panel->battery.area.on_screen)
142 systray.area.posy += (panel->battery.area.height + panel->area.paddingx);
143 #endif
144 }
145
146 if (panel_horizontal) {
147 posy = panel->area.pix.border.width + panel->area.paddingy + systray.area.pix.border.width + systray.area.paddingy;
148 posx = systray.area.posx + systray.area.pix.border.width + systray.area.paddingxlr;
149 }
150 else {
151 posx = panel->area.pix.border.width + panel->area.paddingy + systray.area.pix.border.width + systray.area.paddingy;
152 posy = systray.area.posy + systray.area.pix.border.width + systray.area.paddingxlr;
153 }
154 for (l = systray.list_icons; l ; l = l->next) {
155 traywin = (TrayWindow*)l->data;
156 if (traywin->hide) continue;
157
158 traywin->y = posy;
159 traywin->x = posx;
160 traywin->width = icon_size;
161 traywin->height = icon_size;
162 if (panel_horizontal)
163 posx += (icon_size + systray.area.paddingx);
164 else
165 posy += (icon_size + systray.area.paddingx);
166
167 // position and size the icon window
168 XMoveResizeWindow(server.dsp, traywin->id, traywin->x, traywin->y, icon_size, icon_size);
169 XResizeWindow(server.dsp, traywin->tray_id, icon_size, icon_size);
170 }
171 }
172
173
174 // ***********************************************
175 // systray protocol
176
177 void start_net()
178 {
179 if (net_sel_win) {
180 // protocol already started
181 if (!systray_enabled)
182 stop_net();
183 return;
184 }
185 else
186 if (!systray_enabled)
187 return;
188
189 Window win = XGetSelectionOwner(server.dsp, server.atom._NET_SYSTEM_TRAY_SCREEN);
190
191 // freedesktop systray specification
192 if (win != None) {
193 // search pid
194 Atom _NET_WM_PID, actual_type;
195 int actual_format;
196 unsigned long nitems;
197 unsigned long bytes_after;
198 unsigned char *prop = 0;
199 int pid;
200
201 _NET_WM_PID = XInternAtom(server.dsp, "_NET_WM_PID", True);
202 int ret = XGetWindowProperty(server.dsp, win, _NET_WM_PID, 0, 1024, False, AnyPropertyType, &actual_type, &actual_format, &nitems, &bytes_after, &prop);
203
204 fprintf(stderr, "tint2 : another systray is running");
205 if (ret == Success && prop) {
206 pid = prop[1] * 256;
207 pid += prop[0];
208 fprintf(stderr, " pid=%d", pid);
209 }
210 fprintf(stderr, "\n");
211 return;
212 }
213
214 // init systray protocol
215 net_sel_win = XCreateSimpleWindow(server.dsp, server.root_win, -1, -1, 1, 1, 0, 0, 0);
216
217 // v0.2 trayer specification. tint2 always horizontal.
218 // Vertical panel will draw the systray horizontal.
219 int orient = 0;
220 XChangeProperty(server.dsp, net_sel_win, server.atom._NET_SYSTEM_TRAY_ORIENTATION, XA_CARDINAL, 32, PropModeReplace, (unsigned char *) &orient, 1);
221 VisualID vid = XVisualIDFromVisual(server.visual);
222 XChangeProperty(server.dsp, net_sel_win, XInternAtom(server.dsp, "_NET_SYSTEM_TRAY_VISUAL", False), XA_VISUALID, 32, PropModeReplace, (unsigned char*)&vid, 1);
223
224 XSetSelectionOwner(server.dsp, server.atom._NET_SYSTEM_TRAY_SCREEN, net_sel_win, CurrentTime);
225 if (XGetSelectionOwner(server.dsp, server.atom._NET_SYSTEM_TRAY_SCREEN) != net_sel_win) {
226 stop_net();
227 fprintf(stderr, "tint2 : can't get systray manager\n");
228 return;
229 }
230
231 //fprintf(stderr, "tint2 : systray started\n");
232 XClientMessageEvent ev;
233 ev.type = ClientMessage;
234 ev.window = server.root_win;
235 ev.message_type = server.atom.MANAGER;
236 ev.format = 32;
237 ev.data.l[0] = CurrentTime;
238 ev.data.l[1] = server.atom._NET_SYSTEM_TRAY_SCREEN;
239 ev.data.l[2] = net_sel_win;
240 ev.data.l[3] = 0;
241 ev.data.l[4] = 0;
242 XSendEvent(server.dsp, server.root_win, False, StructureNotifyMask, (XEvent*)&ev);
243 }
244
245
246 void stop_net()
247 {
248 //fprintf(stderr, "tint2 : systray stopped\n");
249 if (systray.list_icons) {
250 // remove_icon change systray.list_icons
251 while(systray.list_icons)
252 remove_icon((TrayWindow*)systray.list_icons->data);
253
254 g_slist_free(systray.list_icons);
255 systray.list_icons = 0;
256 }
257
258 if (net_sel_win != None) {
259 XDestroyWindow(server.dsp, net_sel_win);
260 net_sel_win = None;
261 }
262 }
263
264
265 gboolean error;
266 int window_error_handler(Display *d, XErrorEvent *e)
267 {
268 d=d;e=e;
269 error = TRUE;
270 if (e->error_code != BadWindow) {
271 printf("error_handler %d\n", e->error_code);
272 }
273 return 0;
274 }
275
276
277 static gint compare_traywindows(gconstpointer a, gconstpointer b)
278 {
279 const TrayWindow * traywin_a = (TrayWindow*)a;
280 const TrayWindow * traywin_b = (TrayWindow*)b;
281 XTextProperty name_a, name_b;
282
283 if(XGetWMName(server.dsp, traywin_a->tray_id, &name_a) == 0) {
284 return -1;
285 }
286 else if(XGetWMName(server.dsp, traywin_b->tray_id, &name_b) == 0) {
287 XFree(name_a.value);
288 return 1;
289 }
290 else {
291 gint retval = g_ascii_strncasecmp((char*)name_a.value, (char*)name_b.value, -1) * systray.sort;
292 XFree(name_a.value);
293 XFree(name_b.value);
294 return retval;
295 }
296 }
297
298
299 gboolean add_icon(Window id)
300 {
301 TrayWindow *traywin;
302 XErrorHandler old;
303 Panel *panel = systray.area.panel;
304 int hide = 0;
305
306 error = FALSE;
307 int wrong_format = 0;
308 old = XSetErrorHandler(window_error_handler);
309 XWindowAttributes attr;
310 XGetWindowAttributes(server.dsp, id, &attr);
311 XSetWindowAttributes set_attr;
312 wrong_format = (attr.depth != server.depth);
313 set_attr.colormap = attr.colormap;
314 set_attr.background_pixel = 0;
315 set_attr.border_pixel = 0;
316 unsigned long mask = CWColormap|CWBackPixel|CWBorderPixel;
317 Window parent_window;
318 if (real_transparency)
319 parent_window = XCreateWindow(server.dsp, panel->main_win, 0, 0, 30, 30, 0, attr.depth, InputOutput, attr.visual, mask, &set_attr);
320 else
321 parent_window = panel->main_win;
322 XReparentWindow(server.dsp, id, parent_window, 0, 0);
323 XSync(server.dsp, False);
324 XSetErrorHandler(old);
325 if (error != FALSE) {
326 fprintf(stderr, "tint2 : not icon_swallow\n");
327 return FALSE;
328 }
329
330 {
331 Atom acttype;
332 int actfmt;
333 unsigned long nbitem, bytes;
334 unsigned char *data = 0;
335 int ret;
336
337 ret = XGetWindowProperty(server.dsp, id, server.atom._XEMBED_INFO, 0, 2, False, server.atom._XEMBED_INFO, &acttype, &actfmt, &nbitem, &bytes, &data);
338 if (ret == Success) {
339 if (data) {
340 if (nbitem == 2) {
341 //hide = ((data[1] & XEMBED_MAPPED) == 0);
342 //printf("hide %d\n", hide);
343 }
344 XFree(data);
345 }
346 }
347 else {
348 fprintf(stderr, "tint2 : xembed error\n");
349 return FALSE;
350 }
351 }
352 {
353 XEvent e;
354 e.xclient.type = ClientMessage;
355 e.xclient.serial = 0;
356 e.xclient.send_event = True;
357 e.xclient.message_type = server.atom._XEMBED;
358 e.xclient.window = id;
359 e.xclient.format = 32;
360 e.xclient.data.l[0] = CurrentTime;
361 e.xclient.data.l[1] = XEMBED_EMBEDDED_NOTIFY;
362 e.xclient.data.l[2] = 0;
363 e.xclient.data.l[3] = parent_window;
364 e.xclient.data.l[4] = 0;
365 XSendEvent(server.dsp, id, False, 0xFFFFFF, &e);
366 }
367
368 traywin = g_new0(TrayWindow, 1);
369 if (real_transparency)
370 traywin->id = parent_window;
371 else
372 traywin->id = id;
373 traywin->tray_id = id;
374 traywin->hide = hide;
375 traywin->wrong_format = wrong_format;
376
377 if (systray.sort == 3)
378 systray.list_icons = g_slist_prepend(systray.list_icons, traywin);
379 else if (systray.sort == 2)
380 systray.list_icons = g_slist_append(systray.list_icons, traywin);
381 else
382 systray.list_icons = g_slist_insert_sorted(systray.list_icons, traywin, compare_traywindows);
383 systray.area.resize = 1;
384 systray.area.redraw = 1;
385 //printf("add_icon id %lx, %d\n", id, g_slist_length(systray.list_icons));
386
387 // watch for the icon trying to resize itself!
388 XSelectInput(server.dsp, traywin->tray_id, StructureNotifyMask);
389 if (real_transparency) {
390 XDamageCreate(server.dsp, traywin->id, XDamageReportRawRectangles);
391 XCompositeRedirectWindow(server.dsp, traywin->id, CompositeRedirectManual);
392 }
393
394 // show the window
395 if (!traywin->hide) {
396 XMapRaised(server.dsp, traywin->id);
397 XMapRaised(server.dsp, traywin->tray_id);
398 }
399
400 // changed in systray force resize on panel
401 panel->area.resize = 1;
402 panel_refresh = 1;
403 return TRUE;
404 }
405
406
407 void remove_icon(TrayWindow *traywin)
408 {
409 XErrorHandler old;
410
411 // remove from our list
412 systray.list_icons = g_slist_remove(systray.list_icons, traywin);
413 systray.area.resize = 1;
414 systray.area.redraw = 1;
415 //printf("remove_icon id %lx, %d\n", traywin->id);
416
417 XSelectInput(server.dsp, traywin->tray_id, NoEventMask);
418
419 // reparent to root
420 error = FALSE;
421 old = XSetErrorHandler(window_error_handler);
422 if (!traywin->hide)
423 XUnmapWindow(server.dsp, traywin->id);
424 XReparentWindow(server.dsp, traywin->tray_id, server.root_win, 0, 0);
425 if (traywin->id != traywin->tray_id)
426 XDestroyWindow(server.dsp, traywin->id);
427 XSync(server.dsp, False);
428 XSetErrorHandler(old);
429 g_free(traywin);
430
431 // changed in systray force resize on panel
432 Panel *panel = systray.area.panel;
433 panel->area.resize = 1;
434 panel_refresh = 1;
435 }
436
437
438 void net_message(XClientMessageEvent *e)
439 {
440 unsigned long opcode;
441 Window id;
442
443 opcode = e->data.l[1];
444 switch (opcode) {
445 case SYSTEM_TRAY_REQUEST_DOCK:
446 id = e->data.l[2];
447 if (id) add_icon(id);
448 break;
449
450 case SYSTEM_TRAY_BEGIN_MESSAGE:
451 case SYSTEM_TRAY_CANCEL_MESSAGE:
452 // we don't show baloons messages.
453 break;
454
455 default:
456 if (opcode == server.atom._NET_SYSTEM_TRAY_MESSAGE_DATA)
457 printf("message from dockapp: %s\n", e->data.b);
458 else
459 fprintf(stderr, "SYSTEM_TRAY : unknown message type\n");
460 break;
461 }
462 }
463
464
465 void systray_render_icons(TrayWindow* traywin)
466 {
467 // most systray icons support 32 bit depth, but some icons are still 24 bit.
468 // We create a heuristic mask for these icons, i.e. we get the rgb value in the top left corner, and
469 // mask out all pixel with the same rgb value
470
471 Picture picture_systray, picture_tray, picture_panel;
472 Drawable mask, tray_pixmap;
473 Panel* panel = systray.area.panel;
474 XWindowAttributes attr;
475 XGetWindowAttributes(server.dsp, traywin->id, &attr);
476 XRenderPictFormat *format = XRenderFindVisualFormat(server.dsp, attr.visual);
477 XRenderPictFormat *panel_format = XRenderFindVisualFormat(server.dsp, server.visual);
478 if (traywin->wrong_format) {
479 imlib_context_set_drawable(traywin->id);
480 Imlib_Image image = imlib_create_image_from_drawable(0, 0, 0, traywin->width, traywin->height, 0);
481 imlib_context_set_image(image);
482 imlib_image_set_has_alpha(1);
483 DATA32* data = imlib_image_get_data();
484 createHeuristicMask(data, traywin->width, traywin->height);
485 imlib_image_put_back_data(data);
486 imlib_render_pixmaps_for_whole_image(&tray_pixmap, &mask);
487 picture_tray = XRenderCreatePicture( server.dsp, tray_pixmap, panel_format, 0, 0);
488 Picture mask2 = XRenderCreatePicture( server.dsp, mask, XRenderFindStandardFormat(server.dsp, PictStandardA1), 0, 0);
489 picture_systray = XRenderCreatePicture( server.dsp, systray.area.pix.pmap, panel_format, 0, 0);
490 picture_panel = XRenderCreatePicture(server.dsp, panel->main_win, panel_format, 0, 0);
491 XRenderComposite(server.dsp, PictOpOver, picture_tray, mask2, picture_systray, 0, 0, 0, 0, traywin->x-systray.area.posx, traywin->y-systray.area.posy, traywin->width, traywin->height);
492 XRenderComposite(server.dsp, PictOpOver, picture_tray, mask2, picture_panel, 0, 0, 0, 0, traywin->x, traywin->y, traywin->width, traywin->height);
493 imlib_free_pixmap_and_mask(tray_pixmap);
494 imlib_free_image();
495 }
496 else {
497 picture_tray = XRenderCreatePicture( server.dsp, traywin->id, format, 0, 0);
498 picture_systray = XRenderCreatePicture( server.dsp, systray.area.pix.pmap, panel_format, 0, 0);
499 picture_panel = XRenderCreatePicture(server.dsp, panel->main_win, panel_format, 0, 0);
500 XRenderComposite(server.dsp, PictOpOver, picture_tray, None, picture_systray, 0, 0, 0, 0, traywin->x-systray.area.posx, traywin->y-systray.area.posy, traywin->width, traywin->height);
501 XRenderComposite(server.dsp, PictOpOver, picture_tray, None, picture_panel, 0, 0, 0, 0, traywin->x, traywin->y, traywin->width, traywin->height);
502 }
503 XRenderFreePicture(server.dsp, picture_systray);
504 XRenderFreePicture(server.dsp, picture_tray);
505 XRenderFreePicture(server.dsp, picture_panel);
506 }
507
508
509 void refresh_systray_icon()
510 {
511 TrayWindow *traywin;
512 GSList *l;
513 for (l = systray.list_icons; l ; l = l->next) {
514 traywin = (TrayWindow*)l->data;
515 if (traywin->hide) continue;
516 if (real_transparency) systray_render_icons(traywin);
517 else XClearArea(server.dsp, traywin->id, 0, 0, traywin->width, traywin->height, True);
518 }
519 if (real_transparency)
520 XFlush(server.dsp);
521 }
This page took 0.063871 seconds and 5 git commands to generate.