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