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