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