]> Dogcows Code - chaz/tint2/blob - src/systray/systraybar.c
*add* strut_policy = none
[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/Xcomposite.h>
30 #include <X11/extensions/Xrender.h>
31
32
33 #include "systraybar.h"
34 #include "server.h"
35 #include "panel.h"
36
37 GSList *icons;
38
39 /* defined in the systray spec */
40 #define SYSTEM_TRAY_REQUEST_DOCK 0
41 #define SYSTEM_TRAY_BEGIN_MESSAGE 1
42 #define SYSTEM_TRAY_CANCEL_MESSAGE 2
43
44 // selection window
45 Window net_sel_win = None;
46
47 // freedesktop specification doesn't allow multi systray
48 Systraybar systray;
49 int refresh_systray;
50 int systray_enabled;
51 int systray_max_icon_size = 0;
52
53 // background pixmap if we render ourselves the icons
54 static Pixmap render_background = 0;
55
56
57 void init_systray()
58 {
59 start_net();
60
61 if (!systray_enabled)
62 return;
63
64 systray.area._draw_foreground = draw_systray;
65 systray.area._resize = resize_systray;
66 systray.area.resize = 1;
67 systray.area.redraw = 1;
68 systray.area.on_screen = 1;
69 refresh_systray = 0;
70 }
71
72
73 void init_systray_panel(void *p)
74 {
75 Panel *panel =(Panel*)p;
76
77 if (panel_horizontal) {
78 systray.area.posy = panel->area.bg->border.width + panel->area.paddingy;
79 systray.area.height = panel->area.height - (2 * systray.area.posy);
80 }
81 else {
82 systray.area.posx = panel->area.bg->border.width + panel->area.paddingy;
83 systray.area.width = panel->area.width - (2 * panel->area.bg->border.width) - (2 * panel->area.paddingy);
84 }
85 systray.area.parent = p;
86 systray.area.panel = p;
87 }
88
89
90 void cleanup_systray()
91 {
92 systray_enabled = 0;
93 systray.area.on_screen = 0;
94 free_area(&systray.area);
95 if (render_background) XFreePixmap(server.dsp, render_background);
96 }
97
98
99 void draw_systray(void *obj, cairo_t *c)
100 {
101 if (real_transparency || systray.alpha != 100 || systray.brightness != 0 || systray.saturation != 0) {
102 if (render_background) XFreePixmap(server.dsp, render_background);
103 render_background = XCreatePixmap(server.dsp, server.root_win, systray.area.width, systray.area.height, server.depth);
104 XCopyArea(server.dsp, systray.area.pix, render_background, server.gc, 0, 0, systray.area.width, systray.area.height, 0, 0);
105 }
106
107 refresh_systray = 1;
108 }
109
110
111 void resize_systray(void *obj)
112 {
113 Systraybar *sysbar = obj;
114 Panel *panel = sysbar->area.panel;
115 TrayWindow *traywin;
116 GSList *l;
117 int count, icon_size;
118 int icons_per_column=1, icons_per_row=1, marging=0;
119
120 if (panel_horizontal)
121 icon_size = sysbar->area.height;
122 else
123 icon_size = sysbar->area.width;
124 icon_size = icon_size - (2 * sysbar->area.bg->border.width) - (2 * sysbar->area.paddingy);
125 if (systray_max_icon_size > 0 && icon_size > systray_max_icon_size)
126 icon_size = systray_max_icon_size;
127 count = 0;
128 for (l = systray.list_icons; l ; l = l->next) {
129 if (!((TrayWindow*)l->data)->hide)
130 count++;
131 }
132 //printf("count %d\n", count);
133
134 if (panel_horizontal) {
135 if (!count) systray.area.width = 0;
136 else {
137 int height = sysbar->area.height - 2*sysbar->area.bg->border.width - 2*sysbar->area.paddingy;
138 // here icons_per_column always higher than 0
139 icons_per_column = (height+sysbar->area.paddingx) / (icon_size+sysbar->area.paddingx);
140 marging = height - (icons_per_column-1)*(icon_size+sysbar->area.paddingx) - icon_size;
141 icons_per_row = count / icons_per_column + (count%icons_per_column != 0);
142 systray.area.width = (2 * systray.area.bg->border.width) + (2 * systray.area.paddingxlr) + (icon_size * icons_per_row) + ((icons_per_row-1) * systray.area.paddingx);
143 }
144
145 systray.area.posx = panel->area.width - panel->area.bg->border.width - panel->area.paddingxlr - systray.area.width;
146 if (panel->clock.area.on_screen)
147 systray.area.posx -= (panel->clock.area.width + panel->area.paddingx);
148 #ifdef ENABLE_BATTERY
149 if (panel->battery.area.on_screen)
150 systray.area.posx -= (panel->battery.area.width + panel->area.paddingx);
151 #endif
152 }
153 else {
154 if (!count) systray.area.height = 0;
155 else {
156 int width = sysbar->area.width - 2*sysbar->area.bg->border.width - 2*sysbar->area.paddingy;
157 // here icons_per_row always higher than 0
158 icons_per_row = (width+sysbar->area.paddingx) / (icon_size+sysbar->area.paddingx);
159 marging = width - (icons_per_row-1)*(icon_size+sysbar->area.paddingx) - icon_size;
160 icons_per_column = count / icons_per_row+ (count%icons_per_row != 0);
161 systray.area.height = (2 * systray.area.bg->border.width) + (2 * systray.area.paddingxlr) + (icon_size * icons_per_column) + ((icons_per_column-1) * systray.area.paddingx);
162 }
163
164 systray.area.posy = panel->area.bg->border.width + panel->area.paddingxlr;
165 if (panel->clock.area.on_screen)
166 systray.area.posy += (panel->clock.area.height + panel->area.paddingx);
167 #ifdef ENABLE_BATTERY
168 if (panel->battery.area.on_screen)
169 systray.area.posy += (panel->battery.area.height + panel->area.paddingx);
170 #endif
171 }
172
173 int i, posx, posy;
174 int start = panel->area.bg->border.width + panel->area.paddingy + systray.area.bg->border.width + systray.area.paddingy +marging/2;
175 if (panel_horizontal) {
176 posy = start;
177 posx = systray.area.posx + systray.area.bg->border.width + systray.area.paddingxlr;
178 }
179 else {
180 posx = start;
181 posy = systray.area.posy + systray.area.bg->border.width + systray.area.paddingxlr;
182 }
183
184 for (i=1, l = systray.list_icons; l ; i++, l = l->next) {
185 traywin = (TrayWindow*)l->data;
186 if (traywin->hide) continue;
187
188 traywin->y = posy;
189 traywin->x = posx;
190 traywin->width = icon_size;
191 traywin->height = icon_size;
192 if (panel_horizontal) {
193 if (i % icons_per_column)
194 posy += icon_size + sysbar->area.paddingx;
195 else {
196 posy = start;
197 posx += (icon_size + systray.area.paddingx);
198 }
199 }
200 else {
201 if (i % icons_per_row)
202 posx += icon_size + systray.area.paddingx;
203 else {
204 posx = start;
205 posy += (icon_size + systray.area.paddingx);
206 }
207 }
208
209 // position and size the icon window
210 XMoveResizeWindow(server.dsp, traywin->id, traywin->x, traywin->y, icon_size, icon_size);
211 XResizeWindow(server.dsp, traywin->tray_id, icon_size, icon_size);
212 }
213 // resize force the redraw
214 systray.area.redraw = 1;
215 }
216
217
218 // ***********************************************
219 // systray protocol
220
221 void start_net()
222 {
223 if (net_sel_win) {
224 // protocol already started
225 if (!systray_enabled)
226 stop_net();
227 return;
228 }
229 else
230 if (!systray_enabled)
231 return;
232
233 Window win = XGetSelectionOwner(server.dsp, server.atom._NET_SYSTEM_TRAY_SCREEN);
234
235 // freedesktop systray specification
236 if (win != None) {
237 // search pid
238 Atom _NET_WM_PID, actual_type;
239 int actual_format;
240 unsigned long nitems;
241 unsigned long bytes_after;
242 unsigned char *prop = 0;
243 int pid;
244
245 _NET_WM_PID = XInternAtom(server.dsp, "_NET_WM_PID", True);
246 int ret = XGetWindowProperty(server.dsp, win, _NET_WM_PID, 0, 1024, False, AnyPropertyType, &actual_type, &actual_format, &nitems, &bytes_after, &prop);
247
248 fprintf(stderr, "tint2 : another systray is running");
249 if (ret == Success && prop) {
250 pid = prop[1] * 256;
251 pid += prop[0];
252 fprintf(stderr, " pid=%d", pid);
253 }
254 fprintf(stderr, "\n");
255 return;
256 }
257
258 // init systray protocol
259 net_sel_win = XCreateSimpleWindow(server.dsp, server.root_win, -1, -1, 1, 1, 0, 0, 0);
260
261 // v0.3 trayer specification. tint2 always horizontal.
262 // Vertical panel will draw the systray horizontal.
263 int orient = 0;
264 XChangeProperty(server.dsp, net_sel_win, server.atom._NET_SYSTEM_TRAY_ORIENTATION, XA_CARDINAL, 32, PropModeReplace, (unsigned char *) &orient, 1);
265 VisualID vid = XVisualIDFromVisual(server.visual);
266 XChangeProperty(server.dsp, net_sel_win, XInternAtom(server.dsp, "_NET_SYSTEM_TRAY_VISUAL", False), XA_VISUALID, 32, PropModeReplace, (unsigned char*)&vid, 1);
267
268 XSetSelectionOwner(server.dsp, server.atom._NET_SYSTEM_TRAY_SCREEN, net_sel_win, CurrentTime);
269 if (XGetSelectionOwner(server.dsp, server.atom._NET_SYSTEM_TRAY_SCREEN) != net_sel_win) {
270 stop_net();
271 fprintf(stderr, "tint2 : can't get systray manager\n");
272 return;
273 }
274
275 //fprintf(stderr, "tint2 : systray started\n");
276 XClientMessageEvent ev;
277 ev.type = ClientMessage;
278 ev.window = server.root_win;
279 ev.message_type = server.atom.MANAGER;
280 ev.format = 32;
281 ev.data.l[0] = CurrentTime;
282 ev.data.l[1] = server.atom._NET_SYSTEM_TRAY_SCREEN;
283 ev.data.l[2] = net_sel_win;
284 ev.data.l[3] = 0;
285 ev.data.l[4] = 0;
286 XSendEvent(server.dsp, server.root_win, False, StructureNotifyMask, (XEvent*)&ev);
287 }
288
289
290 void stop_net()
291 {
292 //fprintf(stderr, "tint2 : systray stopped\n");
293 if (systray.list_icons) {
294 // remove_icon change systray.list_icons
295 while(systray.list_icons)
296 remove_icon((TrayWindow*)systray.list_icons->data);
297
298 g_slist_free(systray.list_icons);
299 systray.list_icons = 0;
300 }
301
302 if (net_sel_win != None) {
303 XDestroyWindow(server.dsp, net_sel_win);
304 net_sel_win = None;
305 }
306 }
307
308
309 gboolean error;
310 int window_error_handler(Display *d, XErrorEvent *e)
311 {
312 d=d;e=e;
313 error = TRUE;
314 if (e->error_code != BadWindow) {
315 printf("error_handler %d\n", e->error_code);
316 }
317 return 0;
318 }
319
320
321 static gint compare_traywindows(gconstpointer a, gconstpointer b)
322 {
323 const TrayWindow * traywin_a = (TrayWindow*)a;
324 const TrayWindow * traywin_b = (TrayWindow*)b;
325 XTextProperty name_a, name_b;
326
327 if(XGetWMName(server.dsp, traywin_a->tray_id, &name_a) == 0) {
328 return -1;
329 }
330 else if(XGetWMName(server.dsp, traywin_b->tray_id, &name_b) == 0) {
331 XFree(name_a.value);
332 return 1;
333 }
334 else {
335 gint retval = g_ascii_strncasecmp((char*)name_a.value, (char*)name_b.value, -1) * systray.sort;
336 XFree(name_a.value);
337 XFree(name_b.value);
338 return retval;
339 }
340 }
341
342
343 gboolean add_icon(Window id)
344 {
345 TrayWindow *traywin;
346 XErrorHandler old;
347 Panel *panel = systray.area.panel;
348 int hide = 0;
349
350 error = FALSE;
351 XWindowAttributes attr;
352 XGetWindowAttributes(server.dsp, id, &attr);
353 unsigned long mask = 0;
354 XSetWindowAttributes set_attr;
355 // printf("icon with depth: %d\n", attr.depth);
356 if (attr.depth != server.depth || systray.alpha != 100 || systray.brightness != 0 || systray.saturation != 0 ) {
357 set_attr.colormap = attr.colormap;
358 set_attr.background_pixel = 0;
359 set_attr.border_pixel = 0;
360 mask = CWColormap|CWBackPixel|CWBorderPixel;
361 }
362 else {
363 set_attr.background_pixmap = ParentRelative;
364 mask = CWBackPixmap;
365 }
366 Window parent_window;
367 parent_window = XCreateWindow(server.dsp, panel->main_win, 0, 0, 30, 30, 0, attr.depth, InputOutput, attr.visual, mask, &set_attr);
368 old = XSetErrorHandler(window_error_handler);
369 XReparentWindow(server.dsp, id, parent_window, 0, 0);
370 XSync(server.dsp, False);
371 XSetErrorHandler(old);
372 if (error != FALSE) {
373 fprintf(stderr, "tint2 : not icon_swallow\n");
374 XDestroyWindow(server.dsp, parent_window);
375 return FALSE;
376 }
377
378 {
379 Atom acttype;
380 int actfmt;
381 unsigned long nbitem, bytes;
382 unsigned char *data = 0;
383 int ret;
384
385 ret = XGetWindowProperty(server.dsp, id, server.atom._XEMBED_INFO, 0, 2, False, server.atom._XEMBED_INFO, &acttype, &actfmt, &nbitem, &bytes, &data);
386 if (ret == Success) {
387 if (data) {
388 if (nbitem == 2) {
389 //hide = ((data[1] & XEMBED_MAPPED) == 0);
390 //printf("hide %d\n", hide);
391 }
392 XFree(data);
393 }
394 }
395 else {
396 fprintf(stderr, "tint2 : xembed error\n");
397 XDestroyWindow(server.dsp, parent_window);
398 return FALSE;
399 }
400 }
401 {
402 XEvent e;
403 e.xclient.type = ClientMessage;
404 e.xclient.serial = 0;
405 e.xclient.send_event = True;
406 e.xclient.message_type = server.atom._XEMBED;
407 e.xclient.window = id;
408 e.xclient.format = 32;
409 e.xclient.data.l[0] = CurrentTime;
410 e.xclient.data.l[1] = XEMBED_EMBEDDED_NOTIFY;
411 e.xclient.data.l[2] = 0;
412 e.xclient.data.l[3] = parent_window;
413 e.xclient.data.l[4] = 0;
414 XSendEvent(server.dsp, id, False, 0xFFFFFF, &e);
415 }
416
417 traywin = g_new0(TrayWindow, 1);
418 traywin->id = parent_window;
419 traywin->tray_id = id;
420 traywin->hide = hide;
421 traywin->depth = attr.depth;
422 traywin->damage = 0;
423
424 if (systray.sort == 3)
425 systray.list_icons = g_slist_prepend(systray.list_icons, traywin);
426 else if (systray.sort == 2)
427 systray.list_icons = g_slist_append(systray.list_icons, traywin);
428 else
429 systray.list_icons = g_slist_insert_sorted(systray.list_icons, traywin, compare_traywindows);
430 systray.area.resize = 1;
431 //printf("add_icon id %lx, %d\n", id, g_slist_length(systray.list_icons));
432
433 // watch for the icon trying to resize itself!
434 XSelectInput(server.dsp, traywin->tray_id, StructureNotifyMask);
435 if (real_transparency || systray.alpha != 100 || systray.brightness != 0 || systray.saturation != 0) {
436 traywin->damage = XDamageCreate(server.dsp, traywin->id, XDamageReportNonEmpty);
437 XCompositeRedirectWindow(server.dsp, traywin->id, CompositeRedirectManual);
438 }
439
440 // show the window
441 if (!traywin->hide)
442 XMapWindow(server.dsp, traywin->tray_id);
443 if (!traywin->hide && !panel->is_hidden)
444 XMapRaised(server.dsp, traywin->id);
445
446 // changed in systray force resize on panel
447 panel->area.resize = 1;
448 panel_refresh = 1;
449 return TRUE;
450 }
451
452
453 void remove_icon(TrayWindow *traywin)
454 {
455 XErrorHandler old;
456
457 // remove from our list
458 systray.list_icons = g_slist_remove(systray.list_icons, traywin);
459 systray.area.resize = 1;
460 //printf("remove_icon id %lx, %d\n", traywin->id);
461
462 XSelectInput(server.dsp, traywin->tray_id, NoEventMask);
463 if (traywin->damage)
464 XDamageDestroy(server.dsp, traywin->damage);
465
466 // reparent to root
467 error = FALSE;
468 old = XSetErrorHandler(window_error_handler);
469 if (!traywin->hide)
470 XUnmapWindow(server.dsp, traywin->tray_id);
471 XReparentWindow(server.dsp, traywin->tray_id, server.root_win, 0, 0);
472 XDestroyWindow(server.dsp, traywin->id);
473 XSync(server.dsp, False);
474 XSetErrorHandler(old);
475 g_free(traywin);
476
477 // changed in systray force resize on panel
478 Panel *panel = systray.area.panel;
479 panel->area.resize = 1;
480 panel_refresh = 1;
481 }
482
483
484 void net_message(XClientMessageEvent *e)
485 {
486 unsigned long opcode;
487 Window id;
488
489 opcode = e->data.l[1];
490 switch (opcode) {
491 case SYSTEM_TRAY_REQUEST_DOCK:
492 id = e->data.l[2];
493 if (id) add_icon(id);
494 break;
495
496 case SYSTEM_TRAY_BEGIN_MESSAGE:
497 case SYSTEM_TRAY_CANCEL_MESSAGE:
498 // we don't show baloons messages.
499 break;
500
501 default:
502 if (opcode == server.atom._NET_SYSTEM_TRAY_MESSAGE_DATA)
503 printf("message from dockapp: %s\n", e->data.b);
504 else
505 fprintf(stderr, "SYSTEM_TRAY : unknown message type\n");
506 break;
507 }
508 }
509
510 void systray_render_icon_now(void* t)
511 {
512 // we end up in this function only in real transparency mode or if systray_task_asb != 100 0 0
513 TrayWindow* traywin = t;
514 traywin->render_timeout = 0;
515
516 // good systray icons support 32 bit depth, but some icons are still 24 bit.
517 // We create a heuristic mask for these icons, i.e. we get the rgb value in the top left corner, and
518 // mask out all pixel with the same rgb value
519 Panel* panel = systray.area.panel;
520
521 // Very ugly hack, but somehow imlib2 is not able to get the image from the traywindow itself,
522 // so we first render the tray window onto a pixmap, and then we tell imlib2 to use this pixmap as
523 // drawable. If someone knows why it does not work with the traywindow itself, please tell me ;)
524 Pixmap tmp_pmap = XCreatePixmap(server.dsp, server.root_win, traywin->width, traywin->height, server.depth);
525 XRenderPictFormat* f;
526 if (traywin->depth == 24)
527 f = XRenderFindStandardFormat(server.dsp, PictStandardRGB24);
528 else if (traywin->depth == 32)
529 f = XRenderFindStandardFormat(server.dsp, PictStandardARGB32);
530 else {
531 printf("Strange tray icon found with depth: %d\n", traywin->depth);
532 return;
533 }
534 Picture pict_image = XRenderCreatePicture(server.dsp, traywin->tray_id, f, 0, 0);
535 Picture pict_drawable = XRenderCreatePicture(server.dsp, tmp_pmap, XRenderFindVisualFormat(server.dsp, server.visual), 0, 0);
536 XRenderComposite(server.dsp, PictOpSrc, pict_image, None, pict_drawable, 0, 0, 0, 0, 0, 0, traywin->width, traywin->height);
537 XRenderFreePicture(server.dsp, pict_image);
538 XRenderFreePicture(server.dsp, pict_drawable);
539 // end of the ugly hack and we can continue as before
540
541 imlib_context_set_drawable(tmp_pmap);
542 Imlib_Image image = imlib_create_image_from_drawable(0, 0, 0, traywin->width, traywin->height, 1);
543 if (image == 0)
544 return;
545
546 imlib_context_set_image(image);
547 imlib_image_set_has_alpha(1);
548 DATA32* data = imlib_image_get_data();
549 if (traywin->depth == 24) {
550 createHeuristicMask(data, traywin->width, traywin->height);
551 }
552 if (systray.alpha != 100 || systray.brightness != 0 || systray.saturation != 0)
553 adjust_asb(data, traywin->width, traywin->height, systray.alpha, (float)systray.saturation/100, (float)systray.brightness/100);
554 imlib_image_put_back_data(data);
555 XCopyArea(server.dsp, render_background, systray.area.pix, server.gc, traywin->x-systray.area.posx, traywin->y-systray.area.posy, traywin->width, traywin->height, traywin->x-systray.area.posx, traywin->y-systray.area.posy);
556 if ( !real_transparency ) {
557 imlib_context_set_drawable(systray.area.pix);
558 imlib_render_image_on_drawable(traywin->x-systray.area.posx, traywin->y-systray.area.posy);
559 }
560 else {
561 render_image(systray.area.pix, traywin->x-systray.area.posx, traywin->y-systray.area.posy, traywin->width, traywin->height);
562 }
563 XCopyArea(server.dsp, systray.area.pix, panel->main_win, server.gc, traywin->x-systray.area.posx, traywin->y-systray.area.posy, traywin->width, traywin->height, traywin->x, traywin->y);
564 imlib_free_image_and_decache();
565 XFreePixmap(server.dsp, tmp_pmap);
566
567 if (traywin->damage)
568 XDamageSubtract(server.dsp, traywin->damage, None, None);
569 XFlush(server.dsp);
570 }
571
572
573 void systray_render_icon(TrayWindow* traywin)
574 {
575 if (real_transparency || systray.alpha != 100 || systray.brightness != 0 || systray.saturation != 0) {
576 // wine tray icons update whenever mouse is over them, so we limit the updates to 50 ms
577 if (traywin->render_timeout == 0)
578 traywin->render_timeout = add_timeout(50, 0, systray_render_icon_now, traywin);
579 }
580 else {
581 // comment by andreas: I'm still not sure, what exactly we need to do here... Somehow trayicons which do not
582 // offer the same depth as tint2 does, need to draw a background pixmap, but this cannot be done with
583 // XCopyArea... So we actually need XRenderComposite???
584 // Pixmap pix = XCreatePixmap(server.dsp, server.root_win, traywin->width, traywin->height, server.depth);
585 // XCopyArea(server.dsp, panel->temp_pmap, pix, server.gc, traywin->x, traywin->y, traywin->width, traywin->height, 0, 0);
586 // XSetWindowBackgroundPixmap(server.dsp, traywin->id, pix);
587 XClearArea(server.dsp, traywin->tray_id, 0, 0, traywin->width, traywin->height, True);
588 }
589 }
590
591
592 void refresh_systray_icon()
593 {
594 TrayWindow *traywin;
595 GSList *l;
596 for (l = systray.list_icons; l ; l = l->next) {
597 traywin = (TrayWindow*)l->data;
598 if (traywin->hide) continue;
599 systray_render_icon(traywin);
600 }
601 }
This page took 0.069797 seconds and 5 git commands to generate.