1 /**************************************************************************
4 * Copyright (C) 2009 thierry lorthiois (lorthiois@bbsoft.fr)
5 * based on 'docker-1.5' from Ben Jansens.
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.
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 **************************************************************************/
21 #include <X11/Xutil.h>
22 #include <X11/Xatom.h>
28 #include <X11/extensions/Xdamage.h>
29 #include <X11/extensions/Xcomposite.h>
30 #include <X11/extensions/Xrender.h>
33 #include "systraybar.h"
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
45 Window net_sel_win
= None
;
47 // freedesktop specification doesn't allow multi systray
51 int systray_max_icon_size
;
53 // background pixmap if we render ourselves the icons
54 static Pixmap render_background
;
57 void default_systray()
59 memset(&systray
, 0, sizeof(Systraybar
));
60 render_background
= 0;
63 systray
.area
._draw_foreground
= draw_systray
;
64 systray
.area
.size_mode
= SIZE_BY_CONTENT
;
65 systray
.area
._resize
= resize_systray
;
68 void cleanup_systray()
72 systray_max_icon_size
= 0;
73 systray
.area
.on_screen
= 0;
74 free_area(&systray
.area
);
75 if (render_background
) {
76 XFreePixmap(server
.dsp
, render_background
);
77 render_background
= 0;
88 if (!server
.visual32
&& (systray
.alpha
!= 100 || systray
.brightness
!= 0 || systray
.saturation
!= 0)) {
89 printf("No 32 bit visual for your X implementation. 'systray_asb = 100 0 0' will be forced\n");
91 systray
.brightness
= systray
.saturation
= 0;
93 systray
.area
.resize
= 1;
94 systray
.area
.redraw
= 1;
95 systray
.area
.on_screen
= 1;
100 void init_systray_panel(void *p
)
102 Panel
*panel
=(Panel
*)p
;
104 if (panel_horizontal
) {
105 systray
.area
.posy
= panel
->area
.bg
->border
.width
+ panel
->area
.paddingy
;
106 systray
.area
.height
= panel
->area
.height
- (2 * systray
.area
.posy
);
109 systray
.area
.posx
= panel
->area
.bg
->border
.width
+ panel
->area
.paddingy
;
110 systray
.area
.width
= panel
->area
.width
- (2 * panel
->area
.bg
->border
.width
) - (2 * panel
->area
.paddingy
);
112 systray
.area
.parent
= p
;
113 systray
.area
.panel
= p
;
117 void draw_systray(void *obj
, cairo_t
*c
)
119 // TODO : position and size the icon window when position of systray is known
120 Systraybar
*sysbar
= obj
;
121 Panel
*panel
= sysbar
->area
.panel
;
122 int i
, posx
, posy
, marging
=3, icons_per_column
=1, icons_per_row
=1;
124 int start
= panel
->area
.bg
->border
.width
+ panel
->area
.paddingy
+ systray
.area
.bg
->border
.width
+ systray
.area
.paddingy
+marging
/2;
125 if (panel_horizontal
) {
127 posx
= systray
.area
.posx
+ systray
.area
.bg
->border
.width
+ systray
.area
.paddingxlr
;
131 posy
= systray
.area
.posy
+ systray
.area
.bg
->border
.width
+ systray
.area
.paddingxlr
;
136 for (i
=1, l
= systray
.list_icons
; l
; i
++, l
= l
->next
) {
137 traywin
= (TrayWindow
*)l
->data
;
138 if (traywin
->hide
) continue;
142 traywin
->width
= icon_size
;
143 traywin
->height
= icon_size
;
144 if (panel_horizontal
) {
145 if (i
% icons_per_column
)
146 posy
+= icon_size
+ sysbar
->area
.paddingx
;
149 posx
+= (icon_size
+ systray
.area
.paddingx
);
153 if (i
% icons_per_row
)
154 posx
+= icon_size
+ systray
.area
.paddingx
;
157 posy
+= (icon_size
+ systray
.area
.paddingx
);
161 // position and size the icon window
162 XMoveResizeWindow(server
.dsp
, traywin
->id
, traywin
->x
, traywin
->y
, icon_size
, icon_size
);
163 XResizeWindow(server
.dsp
, traywin
->tray_id
, icon_size
, icon_size
);
166 if (server
.real_transparency
|| systray
.alpha
!= 100 || systray
.brightness
!= 0 || systray
.saturation
!= 0) {
167 if (render_background
) XFreePixmap(server
.dsp
, render_background
);
168 render_background
= XCreatePixmap(server
.dsp
, server
.root_win
, systray
.area
.width
, systray
.area
.height
, server
.depth
);
169 XCopyArea(server
.dsp
, systray
.area
.pix
, render_background
, server
.gc
, 0, 0, systray
.area
.width
, systray
.area
.height
, 0, 0);
176 int resize_systray(void *obj
)
178 Systraybar
*sysbar
= obj
;
180 int count
, icon_size
;
181 int icons_per_column
=1, icons_per_row
=1, marging
=0;
183 if (panel_horizontal
)
184 icon_size
= sysbar
->area
.height
;
186 icon_size
= sysbar
->area
.width
;
187 icon_size
= icon_size
- (2 * sysbar
->area
.bg
->border
.width
) - (2 * sysbar
->area
.paddingy
);
188 if (systray_max_icon_size
> 0 && icon_size
> systray_max_icon_size
)
189 icon_size
= systray_max_icon_size
;
191 for (l
= systray
.list_icons
; l
; l
= l
->next
) {
192 if (!((TrayWindow
*)l
->data
)->hide
)
195 //printf("count %d\n", count);
197 if (panel_horizontal
) {
198 if (!count
) systray
.area
.width
= 0;
200 int height
= sysbar
->area
.height
- 2*sysbar
->area
.bg
->border
.width
- 2*sysbar
->area
.paddingy
;
201 // here icons_per_column always higher than 0
202 icons_per_column
= (height
+sysbar
->area
.paddingx
) / (icon_size
+sysbar
->area
.paddingx
);
203 marging
= height
- (icons_per_column
-1)*(icon_size
+sysbar
->area
.paddingx
) - icon_size
;
204 icons_per_row
= count
/ icons_per_column
+ (count%icons_per_column
!= 0);
205 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
);
209 if (!count
) systray
.area
.height
= 0;
211 int width
= sysbar
->area
.width
- 2*sysbar
->area
.bg
->border
.width
- 2*sysbar
->area
.paddingy
;
212 // here icons_per_row always higher than 0
213 icons_per_row
= (width
+sysbar
->area
.paddingx
) / (icon_size
+sysbar
->area
.paddingx
);
214 marging
= width
- (icons_per_row
-1)*(icon_size
+sysbar
->area
.paddingx
) - icon_size
;
215 icons_per_column
= count
/ icons_per_row
+ (count%icons_per_row
!= 0);
216 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
);
223 // ***********************************************
229 // protocol already started
230 if (!systray_enabled
)
235 if (!systray_enabled
)
238 Window win
= XGetSelectionOwner(server
.dsp
, server
.atom
._NET_SYSTEM_TRAY_SCREEN
);
240 // freedesktop systray specification
243 Atom _NET_WM_PID
, actual_type
;
245 unsigned long nitems
;
246 unsigned long bytes_after
;
247 unsigned char *prop
= 0;
250 _NET_WM_PID
= XInternAtom(server
.dsp
, "_NET_WM_PID", True
);
251 int ret
= XGetWindowProperty(server
.dsp
, win
, _NET_WM_PID
, 0, 1024, False
, AnyPropertyType
, &actual_type
, &actual_format
, &nitems
, &bytes_after
, &prop
);
253 fprintf(stderr
, "tint2 : another systray is running");
254 if (ret
== Success
&& prop
) {
257 fprintf(stderr
, " pid=%d", pid
);
259 fprintf(stderr
, "\n");
263 // init systray protocol
264 net_sel_win
= XCreateSimpleWindow(server
.dsp
, server
.root_win
, -1, -1, 1, 1, 0, 0, 0);
266 // v0.3 trayer specification. tint2 always horizontal.
267 // Vertical panel will draw the systray horizontal.
269 XChangeProperty(server
.dsp
, net_sel_win
, server
.atom
._NET_SYSTEM_TRAY_ORIENTATION
, XA_CARDINAL
, 32, PropModeReplace
, (unsigned char *) &orient
, 1);
271 if (server
.visual32
&& (systray
.alpha
!= 100 || systray
.brightness
!= 0 || systray
.saturation
!= 0))
272 vid
= XVisualIDFromVisual(server
.visual32
);
274 vid
= XVisualIDFromVisual(server
.visual
);
275 XChangeProperty(server
.dsp
, net_sel_win
, XInternAtom(server
.dsp
, "_NET_SYSTEM_TRAY_VISUAL", False
), XA_VISUALID
, 32, PropModeReplace
, (unsigned char*)&vid
, 1);
277 XSetSelectionOwner(server
.dsp
, server
.atom
._NET_SYSTEM_TRAY_SCREEN
, net_sel_win
, CurrentTime
);
278 if (XGetSelectionOwner(server
.dsp
, server
.atom
._NET_SYSTEM_TRAY_SCREEN
) != net_sel_win
) {
280 fprintf(stderr
, "tint2 : can't get systray manager\n");
284 //fprintf(stderr, "tint2 : systray started\n");
285 XClientMessageEvent ev
;
286 ev
.type
= ClientMessage
;
287 ev
.window
= server
.root_win
;
288 ev
.message_type
= server
.atom
.MANAGER
;
290 ev
.data
.l
[0] = CurrentTime
;
291 ev
.data
.l
[1] = server
.atom
._NET_SYSTEM_TRAY_SCREEN
;
292 ev
.data
.l
[2] = net_sel_win
;
295 XSendEvent(server
.dsp
, server
.root_win
, False
, StructureNotifyMask
, (XEvent
*)&ev
);
301 //fprintf(stderr, "tint2 : systray stopped\n");
302 if (systray
.list_icons
) {
303 // remove_icon change systray.list_icons
304 while(systray
.list_icons
)
305 remove_icon((TrayWindow
*)systray
.list_icons
->data
);
307 g_slist_free(systray
.list_icons
);
308 systray
.list_icons
= 0;
311 if (net_sel_win
!= None
) {
312 XDestroyWindow(server
.dsp
, net_sel_win
);
319 int window_error_handler(Display
*d
, XErrorEvent
*e
)
323 if (e
->error_code
!= BadWindow
) {
324 printf("error_handler %d\n", e
->error_code
);
330 static gint
compare_traywindows(gconstpointer a
, gconstpointer b
)
332 const TrayWindow
* traywin_a
= (TrayWindow
*)a
;
333 const TrayWindow
* traywin_b
= (TrayWindow
*)b
;
334 XTextProperty name_a
, name_b
;
336 if(XGetWMName(server
.dsp
, traywin_a
->tray_id
, &name_a
) == 0) {
339 else if(XGetWMName(server
.dsp
, traywin_b
->tray_id
, &name_b
) == 0) {
344 gint retval
= g_ascii_strncasecmp((char*)name_a
.value
, (char*)name_b
.value
, -1) * systray
.sort
;
352 gboolean
add_icon(Window id
)
356 Panel
*panel
= systray
.area
.panel
;
360 XWindowAttributes attr
;
361 if ( XGetWindowAttributes(server
.dsp
, id
, &attr
) == False
) return FALSE
;
362 unsigned long mask
= 0;
363 XSetWindowAttributes set_attr
;
364 Visual
* visual
= server
.visual
;
365 //printf("icon with depth: %d, width %d, height %d\n", attr.depth, attr.width, attr.height);
366 //printf("icon with depth: %d\n", attr.depth);
367 if (attr
.depth
!= server
.depth
|| systray
.alpha
!= 100 || systray
.brightness
!= 0 || systray
.saturation
!= 0) {
368 visual
= attr
.visual
;
369 set_attr
.colormap
= attr
.colormap
;
370 set_attr
.background_pixel
= 0;
371 set_attr
.border_pixel
= 0;
372 mask
= CWColormap
|CWBackPixel
|CWBorderPixel
;
375 set_attr
.background_pixmap
= ParentRelative
;
378 Window parent_window
;
379 parent_window
= XCreateWindow(server
.dsp
, panel
->main_win
, 0, 0, 30, 30, 0, attr
.depth
, InputOutput
, visual
, mask
, &set_attr
);
380 old
= XSetErrorHandler(window_error_handler
);
381 XReparentWindow(server
.dsp
, id
, parent_window
, 0, 0);
382 XSync(server
.dsp
, False
);
383 XSetErrorHandler(old
);
384 if (error
!= FALSE
) {
385 fprintf(stderr
, "tint2 : not icon_swallow\n");
386 XDestroyWindow(server
.dsp
, parent_window
);
393 unsigned long nbitem
, bytes
;
394 unsigned char *data
= 0;
397 ret
= XGetWindowProperty(server
.dsp
, id
, server
.atom
._XEMBED_INFO
, 0, 2, False
, server
.atom
._XEMBED_INFO
, &acttype
, &actfmt
, &nbitem
, &bytes
, &data
);
398 if (ret
== Success
) {
401 //hide = ((data[1] & XEMBED_MAPPED) == 0);
402 //printf("hide %d\n", hide);
408 fprintf(stderr
, "tint2 : xembed error\n");
409 XDestroyWindow(server
.dsp
, parent_window
);
415 e
.xclient
.type
= ClientMessage
;
416 e
.xclient
.serial
= 0;
417 e
.xclient
.send_event
= True
;
418 e
.xclient
.message_type
= server
.atom
._XEMBED
;
419 e
.xclient
.window
= id
;
420 e
.xclient
.format
= 32;
421 e
.xclient
.data
.l
[0] = CurrentTime
;
422 e
.xclient
.data
.l
[1] = XEMBED_EMBEDDED_NOTIFY
;
423 e
.xclient
.data
.l
[2] = 0;
424 e
.xclient
.data
.l
[3] = parent_window
;
425 e
.xclient
.data
.l
[4] = 0;
426 XSendEvent(server
.dsp
, id
, False
, 0xFFFFFF, &e
);
429 traywin
= g_new0(TrayWindow
, 1);
430 traywin
->id
= parent_window
;
431 traywin
->tray_id
= id
;
432 traywin
->hide
= hide
;
433 traywin
->depth
= attr
.depth
;
436 if (systray
.sort
== 3)
437 systray
.list_icons
= g_slist_prepend(systray
.list_icons
, traywin
);
438 else if (systray
.sort
== 2)
439 systray
.list_icons
= g_slist_append(systray
.list_icons
, traywin
);
441 systray
.list_icons
= g_slist_insert_sorted(systray
.list_icons
, traywin
, compare_traywindows
);
442 systray
.area
.resize
= 1;
443 //printf("add_icon id %lx, %d\n", id, g_slist_length(systray.list_icons));
445 // watch for the icon trying to resize itself!
446 XSelectInput(server
.dsp
, traywin
->tray_id
, StructureNotifyMask
);
447 if (server
.real_transparency
|| systray
.alpha
!= 100 || systray
.brightness
!= 0 || systray
.saturation
!= 0) {
448 traywin
->damage
= XDamageCreate(server
.dsp
, traywin
->id
, XDamageReportRawRectangles
);
449 XCompositeRedirectWindow(server
.dsp
, traywin
->id
, CompositeRedirectManual
);
454 XMapWindow(server
.dsp
, traywin
->tray_id
);
455 if (!traywin
->hide
&& !panel
->is_hidden
)
456 XMapRaised(server
.dsp
, traywin
->id
);
458 // changed in systray force resize on panel
459 panel
->area
.resize
= 1;
465 void remove_icon(TrayWindow
*traywin
)
469 // remove from our list
470 systray
.list_icons
= g_slist_remove(systray
.list_icons
, traywin
);
471 systray
.area
.resize
= 1;
472 //printf("remove_icon id %lx, %d\n", traywin->id);
474 XSelectInput(server
.dsp
, traywin
->tray_id
, NoEventMask
);
476 XDamageDestroy(server
.dsp
, traywin
->damage
);
480 old
= XSetErrorHandler(window_error_handler
);
482 XUnmapWindow(server
.dsp
, traywin
->tray_id
);
483 XReparentWindow(server
.dsp
, traywin
->tray_id
, server
.root_win
, 0, 0);
484 XDestroyWindow(server
.dsp
, traywin
->id
);
485 XSync(server
.dsp
, False
);
486 XSetErrorHandler(old
);
487 if (traywin
->render_timeout
)
488 stop_timeout(traywin
->render_timeout
);
491 // changed in systray force resize on panel
492 Panel
*panel
= systray
.area
.panel
;
493 panel
->area
.resize
= 1;
498 void net_message(XClientMessageEvent
*e
)
500 unsigned long opcode
;
503 opcode
= e
->data
.l
[1];
505 case SYSTEM_TRAY_REQUEST_DOCK
:
507 if (id
) add_icon(id
);
510 case SYSTEM_TRAY_BEGIN_MESSAGE
:
511 case SYSTEM_TRAY_CANCEL_MESSAGE
:
512 // we don't show baloons messages.
516 if (opcode
== server
.atom
._NET_SYSTEM_TRAY_MESSAGE_DATA
)
517 printf("message from dockapp: %s\n", e
->data
.b
);
519 fprintf(stderr
, "SYSTEM_TRAY : unknown message type\n");
524 void systray_render_icon_now(void* t
)
526 // we end up in this function only in real transparency mode or if systray_task_asb != 100 0 0
527 // we made also sure, that we always have a 32 bit visual, i.e. we can safely create 32 bit pixmaps here
528 TrayWindow
* traywin
= t
;
529 traywin
->render_timeout
= 0;
531 // good systray icons support 32 bit depth, but some icons are still 24 bit.
532 // We create a heuristic mask for these icons, i.e. we get the rgb value in the top left corner, and
533 // mask out all pixel with the same rgb value
534 Panel
* panel
= systray
.area
.panel
;
536 // Very ugly hack, but somehow imlib2 is not able to get the image from the traywindow itself,
537 // so we first render the tray window onto a pixmap, and then we tell imlib2 to use this pixmap as
538 // drawable. If someone knows why it does not work with the traywindow itself, please tell me ;)
539 Pixmap tmp_pmap
= XCreatePixmap(server
.dsp
, server
.root_win
, traywin
->width
, traywin
->height
, 32);
540 XRenderPictFormat
* f
;
541 if (traywin
->depth
== 24)
542 f
= XRenderFindStandardFormat(server
.dsp
, PictStandardRGB24
);
543 else if (traywin
->depth
== 32)
544 f
= XRenderFindStandardFormat(server
.dsp
, PictStandardARGB32
);
546 printf("Strange tray icon found with depth: %d\n", traywin
->depth
);
550 //if (server.real_transparency)
551 //pict_image = XRenderCreatePicture(server.dsp, traywin->id, f, 0, 0);
552 // reverted Rev 407 because here it's breaking alls icon with systray + xcompmgr
553 pict_image
= XRenderCreatePicture(server
.dsp
, traywin
->tray_id
, f
, 0, 0);
554 Picture pict_drawable
= XRenderCreatePicture(server
.dsp
, tmp_pmap
, XRenderFindVisualFormat(server
.dsp
, server
.visual32
), 0, 0);
555 XRenderComposite(server
.dsp
, PictOpSrc
, pict_image
, None
, pict_drawable
, 0, 0, 0, 0, 0, 0, traywin
->width
, traywin
->height
);
556 XRenderFreePicture(server
.dsp
, pict_image
);
557 XRenderFreePicture(server
.dsp
, pict_drawable
);
558 // end of the ugly hack and we can continue as before
560 imlib_context_set_visual(server
.visual32
);
561 imlib_context_set_colormap(server
.colormap32
);
562 imlib_context_set_drawable(tmp_pmap
);
563 Imlib_Image image
= imlib_create_image_from_drawable(0, 0, 0, traywin
->width
, traywin
->height
, 1);
567 imlib_context_set_image(image
);
568 //if (traywin->depth == 24)
569 //imlib_save_image("/home/thil77/test.jpg");
570 imlib_image_set_has_alpha(1);
571 DATA32
* data
= imlib_image_get_data();
572 if (traywin
->depth
== 24) {
573 createHeuristicMask(data
, traywin
->width
, traywin
->height
);
575 if (systray
.alpha
!= 100 || systray
.brightness
!= 0 || systray
.saturation
!= 0)
576 adjust_asb(data
, traywin
->width
, traywin
->height
, systray
.alpha
, (float)systray
.saturation
/100, (float)systray
.brightness
/100);
577 imlib_image_put_back_data(data
);
578 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
);
579 render_image(systray
.area
.pix
, traywin
->x
-systray
.area
.posx
, traywin
->y
-systray
.area
.posy
, traywin
->width
, traywin
->height
);
580 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
);
581 imlib_free_image_and_decache();
582 XFreePixmap(server
.dsp
, tmp_pmap
);
583 imlib_context_set_visual(server
.visual
);
584 imlib_context_set_colormap(server
.colormap
);
587 XDamageSubtract(server
.dsp
, traywin
->damage
, None
, None
);
592 void systray_render_icon(TrayWindow
* traywin
)
594 if (server
.real_transparency
|| systray
.alpha
!= 100 || systray
.brightness
!= 0 || systray
.saturation
!= 0) {
595 // wine tray icons update whenever mouse is over them, so we limit the updates to 50 ms
596 if (traywin
->render_timeout
== 0)
597 traywin
->render_timeout
= add_timeout(50, 0, systray_render_icon_now
, traywin
);
600 // comment by andreas: I'm still not sure, what exactly we need to do here... Somehow trayicons which do not
601 // offer the same depth as tint2 does, need to draw a background pixmap, but this cannot be done with
602 // XCopyArea... So we actually need XRenderComposite???
603 // Pixmap pix = XCreatePixmap(server.dsp, server.root_win, traywin->width, traywin->height, server.depth);
604 // XCopyArea(server.dsp, panel->temp_pmap, pix, server.gc, traywin->x, traywin->y, traywin->width, traywin->height, 0, 0);
605 // XSetWindowBackgroundPixmap(server.dsp, traywin->id, pix);
606 XClearArea(server
.dsp
, traywin
->tray_id
, 0, 0, traywin
->width
, traywin
->height
, True
);
611 void refresh_systray_icon()
615 for (l
= systray
.list_icons
; l
; l
= l
->next
) {
616 traywin
= (TrayWindow
*)l
->data
;
617 if (traywin
->hide
) continue;
618 systray_render_icon(traywin
);