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