]> Dogcows Code - chaz/tint2/blob - src/systray/systraybar.c
remove flickering on systray
[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
29 #include "systraybar.h"
30 #include "server.h"
31 #include "panel.h"
32
33 GSList *icons;
34
35 /* defined in the systray spec */
36 #define SYSTEM_TRAY_REQUEST_DOCK 0
37 #define SYSTEM_TRAY_BEGIN_MESSAGE 1
38 #define SYSTEM_TRAY_CANCEL_MESSAGE 2
39
40 // selection window
41 Window net_sel_win = None, hint_win = None;
42
43 // freedesktop specification doesn't allow multi systray
44 Systraybar systray;
45 int refresh_systray;
46
47
48 void init_systray()
49 {
50 Panel *panel = &panel1[0];
51
52 if (systray.area.on_screen)
53 systray.area.on_screen = init_net();
54
55 if (!systray.area.on_screen)
56 return;
57
58 systray.area.parent = panel;
59 systray.area.panel = panel;
60 systray.area._draw_foreground = draw_systray;
61 systray.area._resize = resize_systray;
62 refresh_systray = 0;
63
64 // configure systray
65 // draw only one systray (even with multi panel)
66 systray.area.posy = panel->area.pix.border.width + panel->area.paddingy;
67 systray.area.height = panel->area.height - (2 * systray.area.posy);
68 systray.area.width = 0;
69 systray.area.redraw = 1;
70
71 systray.area.posx = panel->area.width - panel->area.paddingxlr - panel->area.pix.border.width - systray.area.width;
72 if (panel->clock.area.on_screen)
73 systray.area.posx -= (panel->clock.area.width + panel->area.paddingx);
74 #ifdef ENABLE_BATTERY
75 if (panel->battery.area.on_screen)
76 systray.area.posx -= (panel->battery.area.width + panel->area.paddingx);
77 #endif
78 }
79
80
81 void cleanup_systray()
82 {
83 if (systray.list_icons) {
84 GSList *it;
85
86 for (it = systray.list_icons; it; it = it->next)
87 remove_icon((TrayWindow*)it->data);
88
89 g_slist_free(systray.list_icons);
90 systray.list_icons = 0;
91 }
92
93 free_area(&systray.area);
94
95 cleanup_net();
96 }
97
98
99 void draw_systray(void *obj, cairo_t *c, int active)
100 {
101 // tint2 don't draw systray icons. just the background.
102 refresh_systray = 1;
103 }
104
105
106 void resize_systray(void *obj)
107 {
108 Systraybar *sysbar = obj;
109 Panel *panel = sysbar->area.panel;
110 TrayWindow *traywin;
111 GSList *l;
112 int count, posx, posy;
113 int icon_size;
114
115 icon_size = sysbar->area.height - (2 * sysbar->area.pix.border.width) - (2 * sysbar->area.paddingy);
116 count = g_slist_length(systray.list_icons);
117
118 if (!count) systray.area.width = 0;
119 else systray.area.width = (2 * systray.area.pix.border.width) + (2 * systray.area.paddingxlr) + (icon_size * count) + ((count-1) * systray.area.paddingx);
120
121 systray.area.posx = panel->area.width - panel->area.pix.border.width - panel->area.paddingxlr - systray.area.width;
122 if (panel->clock.area.on_screen)
123 systray.area.posx -= (panel->clock.area.width + panel->area.paddingx);
124 #ifdef ENABLE_BATTERY
125 if (panel->battery.area.on_screen)
126 systray.area.posx -= (panel->battery.area.width + panel->area.paddingx);
127 #endif
128
129 posy = panel->area.pix.border.width + panel->area.paddingy + systray.area.pix.border.width + systray.area.paddingy;
130 posx = systray.area.posx + systray.area.pix.border.width + systray.area.paddingxlr;
131 for (l = systray.list_icons; l ; l = l->next) {
132 traywin = (TrayWindow*)l->data;
133
134 traywin->y = posy;
135 traywin->x = posx;
136 traywin->width = icon_size;
137 traywin->height = icon_size;
138 posx += (icon_size + systray.area.paddingx);
139
140 // position and size the icon window
141 XMoveResizeWindow(server.dsp, traywin->id, traywin->x, traywin->y, icon_size, icon_size);
142 }
143 //printf("resize_systray %d %d\n", systray.area.posx, systray.area.width);
144 }
145
146 /*
147 void create_hint_win()
148 {
149 XWMHints hints;
150 XClassHint classhints;
151 Panel *panel = systray.area.panel;
152
153 hint_win = XCreateSimpleWindow(server.dsp, server.root_win, 0, 0, 1, 1, 0, 0, 0);
154
155 hints.flags = StateHint | WindowGroupHint | IconWindowHint;
156 hints.initial_state = WithdrawnState;
157 hints.window_group = hint_win;
158 hints.icon_window = panel->main_win;
159
160 classhints.res_name = "docker";
161 classhints.res_class = "Docker";
162
163 XSetWMProperties(server.dsp, hint_win, NULL, NULL, NULL, 0,
164 NULL, &hints, &classhints);
165
166 XMapWindow(server.dsp, hint_win);
167 }
168 */
169
170 int init_net()
171 {
172 if (XGetSelectionOwner(server.dsp, server.atom._NET_SYSTEM_TRAY_SCREEN) != None) {
173 fprintf(stderr, "tint2 : another systray is running\n");
174 return 0;
175 }
176
177 //create_hint_win();
178
179 // init systray protocol
180 net_sel_win = XCreateSimpleWindow(server.dsp, server.root_win, -1, -1, 1, 1, 0, 0, 0);
181
182 // v0.2 trayer specification. tint2 always orizontal.
183 int orient = 0;
184 XChangeProperty(server.dsp, net_sel_win, server.atom._NET_SYSTEM_TRAY_ORIENTATION, XA_CARDINAL, 32, PropModeReplace, (unsigned char *) &orient, 1);
185
186 XSetSelectionOwner(server.dsp, server.atom._NET_SYSTEM_TRAY_SCREEN, net_sel_win, CurrentTime);
187 if (XGetSelectionOwner(server.dsp, server.atom._NET_SYSTEM_TRAY_SCREEN) != net_sel_win) {
188 fprintf(stderr, "tint2 : can't get systray manager\n");
189 return 0;
190 }
191
192 XClientMessageEvent ev;
193 ev.type = ClientMessage;
194 ev.window = server.root_win;
195 ev.message_type = server.atom.MANAGER;
196 ev.format = 32;
197 ev.data.l[0] = CurrentTime;
198 ev.data.l[1] = server.atom._NET_SYSTEM_TRAY_SCREEN;
199 ev.data.l[2] = net_sel_win;
200 ev.data.l[3] = 0;
201 ev.data.l[4] = 0;
202 XSendEvent(server.dsp, server.root_win, False, StructureNotifyMask, (XEvent*)&ev);
203 return 1;
204 }
205
206
207 void cleanup_net()
208 {
209 if (net_sel_win != None) {
210 XDestroyWindow(server.dsp, net_sel_win);
211 net_sel_win = None;
212 }
213 }
214
215
216 gboolean error;
217 int window_error_handler(Display *d, XErrorEvent *e)
218 {
219 d=d;e=e;
220 error = TRUE;
221 if (e->error_code != BadWindow) {
222 printf("error_handler %d\n", e->error_code);
223 }
224 return 0;
225 }
226
227
228 // The traywin must have its id and type set.
229 gboolean add_icon(Window id)
230 {
231 TrayWindow *traywin;
232 XErrorHandler old;
233 Panel *panel = systray.area.panel;
234
235 error = FALSE;
236 old = XSetErrorHandler(window_error_handler);
237 XReparentWindow(server.dsp, id, panel->main_win, 0, 0);
238 XSync(server.dsp, False);
239 XSetErrorHandler(old);
240
241 if (error != FALSE) {
242 fprintf(stderr, "tint2 : not icon_swallow\n");
243 return FALSE;
244 }
245
246 traywin = g_new0(TrayWindow, 1);
247 traywin->id = id;
248
249 systray.list_icons = g_slist_prepend(systray.list_icons, traywin);
250 //printf("ajout d'un icone %d (%lx)\n", g_slist_length(systray.list_icons), id);
251 systray.area.resize = 1;
252 systray.area.redraw = 1;
253
254 // watch for the icon trying to resize itself!
255 XSelectInput(server.dsp, traywin->id, StructureNotifyMask);
256
257 // show the window
258 XMapRaised(server.dsp, traywin->id);
259
260 // changed in systray force resize on panel
261 panel->area.resize = 1;
262 panel_refresh = 1;
263 return TRUE;
264 }
265
266
267 void remove_icon(TrayWindow *traywin)
268 {
269 XErrorHandler old;
270
271 XSelectInput(server.dsp, traywin->id, NoEventMask);
272
273 // reparent to root
274 error = FALSE;
275 old = XSetErrorHandler(window_error_handler);
276 XReparentWindow(server.dsp, traywin->id, server.root_win, 0, 0);
277 XSync(server.dsp, False);
278 XSetErrorHandler(old);
279
280 // remove from our list
281 systray.list_icons = g_slist_remove(systray.list_icons, traywin);
282 g_free(traywin);
283 //printf("suppression d'un icone %d\n", g_slist_length(systray.list_icons));
284 systray.area.resize = 1;
285 systray.area.redraw = 1;
286
287 // changed in systray force resize on panel
288 Panel *panel = systray.area.panel;
289 panel->area.resize = 1;
290 panel_refresh = 1;
291
292 }
293
294
295 void net_message(XClientMessageEvent *e)
296 {
297 unsigned long opcode;
298 Window id;
299
300 opcode = e->data.l[1];
301 switch (opcode) {
302 case SYSTEM_TRAY_REQUEST_DOCK:
303 id = e->data.l[2];
304 if (id) add_icon(id);
305 break;
306
307 case SYSTEM_TRAY_BEGIN_MESSAGE:
308 case SYSTEM_TRAY_CANCEL_MESSAGE:
309 // we don't show baloons messages.
310 break;
311
312 default:
313 if (opcode == server.atom._NET_SYSTEM_TRAY_MESSAGE_DATA) {
314 printf("message from dockapp: %s\n", e->data.b);
315 }
316 else
317 printf("SYSTEM_TRAY : unknown message type\n");
318 break;
319 }
320 }
321
322
323 void refresh_systray_icon()
324 {
325 TrayWindow *traywin;
326 GSList *l;
327 for (l = systray.list_icons; l ; l = l->next) {
328 traywin = (TrayWindow*)l->data;
329 XClearArea(server.dsp, traywin->id, 0, 0, traywin->width, traywin->height, True);
330 }
331 }
332
333
This page took 0.055915 seconds and 5 git commands to generate.