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