]> Dogcows Code - chaz/tint2/blob - src/systray/systraybar.c
patch by Christian Ruppert, configure option for battery
[chaz/tint2] / src / systray / systraybar.c
1 /**************************************************************************
2 * Tint2 : systraybar
3 *
4 * Copyright (C) 2009 thierry lorthiois (lorthiois@bbsoft.fr)
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License version 2
8 * as published by the Free Software Foundation.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 **************************************************************************/
18
19 #include <X11/Xlib.h>
20 #include <X11/Xutil.h>
21 #include <X11/Xatom.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <glib.h>
26 #include <Imlib2.h>
27
28 #include "systraybar.h"
29 #include "server.h"
30 #include "panel.h"
31
32 GSList *icons;
33
34 /* defined in the systray spec */
35 #define SYSTEM_TRAY_REQUEST_DOCK 0
36 #define SYSTEM_TRAY_BEGIN_MESSAGE 1
37 #define SYSTEM_TRAY_CANCEL_MESSAGE 2
38
39 // selection window
40 Window net_sel_win = None, hint_win = None;
41
42 // freedesktop specification doesn't allow multi systray
43 Systraybar systray;
44
45
46 void init_systray()
47 {
48 Panel *panel = &panel1[0];
49 systray.area.parent = panel;
50 systray.area.panel = panel;
51 systray.area._draw_foreground = draw_systray;
52 systray.area._resize = resize_systray;
53
54 if (systray.area.on_screen)
55 systray.area.on_screen = init_net();
56
57 if (!systray.area.on_screen)
58 return;
59
60 // configure systray
61 // draw only one systray (even with multi panel)
62 systray.area.posy = panel->area.pix.border.width + panel->area.paddingy;
63 systray.area.height = panel->area.height - (2 * systray.area.posy);
64 systray.area.width = 0;
65
66 systray.area.posx = panel->area.width - panel->area.paddingxlr - panel->area.pix.border.width - systray.area.width;
67 if (panel->clock.area.on_screen)
68 systray.area.posx -= (panel->clock.area.width + panel->area.paddingx);
69 #ifdef ENABLE_BATTERY
70 if (panel->battery.area.on_screen)
71 systray.area.posx -= (panel->battery.area.width + panel->area.paddingx);
72 #endif
73
74 systray.area.redraw = 1;
75 }
76
77
78 void cleanup_systray()
79 {
80 if (systray.list_icons) {
81 GSList *it;
82
83 for (it = systray.list_icons; it; it = it->next)
84 remove_icon((TrayWindow*)it->data);
85
86 g_slist_free(systray.list_icons);
87 systray.list_icons = 0;
88 }
89
90 free_area(&systray.area);
91
92 cleanup_net();
93 }
94
95
96 void draw_systray(void *obj, cairo_t *c, int active)
97 {
98 Systraybar *sysbar = obj;
99 Panel *panel = sysbar->area.panel;
100 TrayWindow *traywin;
101 GSList *l;
102 int icon_size;
103
104 //printf("draw_systray %d %d\n", systray.area.posx, systray.area.width);
105 icon_size = sysbar->area.height - (2 * sysbar->area.pix.border.width) - (2 * sysbar->area.paddingy);
106 for (l = systray.list_icons; l ; l = l->next) {
107 traywin = (TrayWindow*)l->data;
108
109 // watch for the icon trying to resize itself!
110 //XSelectInput(server.dsp, traywin->id, StructureNotifyMask|ResizeRedirectMask);
111 XSelectInput(server.dsp, traywin->id, StructureNotifyMask);
112
113 // position and size the icon window
114 XMoveResizeWindow(server.dsp, traywin->id, traywin->x, traywin->y, icon_size, icon_size);
115 //printf("icon %d, %d, %d\n", traywin->x, traywin->y, icon_size);
116 // ceci intervertie les fonds : le premier icone prend le fond du dernier
117 // le dernier prend le fond de l'avant dernier, ...
118 XSetWindowBackgroundPixmap (server.dsp, panel->main_win, systray.area.pix.pmap);
119
120 // flush before clearing, otherwise the clear isn't effective.
121 XFlush(server.dsp);
122 // make sure the new child will get the right stuff in its background
123 // for ParentRelative.
124 XClearWindow(server.dsp, panel->main_win);
125
126 // show the window
127 XMapRaised(server.dsp, traywin->id);
128 }
129 }
130
131
132 void resize_systray(void *obj)
133 {
134 Systraybar *sysbar = obj;
135 Panel *panel = sysbar->area.panel;
136 TrayWindow *traywin;
137 GSList *l;
138 int count, posx, posy;
139 int icon_size;
140
141 icon_size = sysbar->area.height - (2 * sysbar->area.pix.border.width) - (2 * sysbar->area.paddingy);
142 count = g_slist_length(systray.list_icons);
143
144 if (!count) systray.area.width = 0;
145 else systray.area.width = (2 * systray.area.pix.border.width) + (2 * systray.area.paddingxlr) + (icon_size * count) + ((count-1) * systray.area.paddingx);
146
147 systray.area.posx = panel->area.width - panel->area.pix.border.width - panel->area.paddingxlr - systray.area.width;
148 if (panel->clock.area.on_screen)
149 systray.area.posx -= (panel->clock.area.width + panel->area.paddingx);
150 #ifdef ENABLE_BATTERY
151 if (panel->battery.area.on_screen)
152 systray.area.posx -= (panel->battery.area.width + panel->area.paddingx);
153 #endif
154
155 systray.area.redraw = 1;
156
157 posy = panel->area.pix.border.width + panel->area.paddingy + systray.area.pix.border.width + systray.area.paddingy;
158 posx = systray.area.posx + systray.area.pix.border.width + systray.area.paddingxlr;
159 for (l = systray.list_icons; l ; l = l->next) {
160 traywin = (TrayWindow*)l->data;
161
162 traywin->y = posy;
163 traywin->x = posx;
164 traywin->width = icon_size;
165 traywin->height = icon_size;
166 posx += (icon_size + systray.area.paddingx);
167 }
168
169 // resize other objects on panel
170 printf("resize_systray %d %d\n", systray.area.posx, systray.area.width);
171 }
172
173 /*
174 void create_hint_win()
175 {
176 XWMHints hints;
177 XClassHint classhints;
178 Panel *panel = systray.area.panel;
179
180 hint_win = XCreateSimpleWindow(server.dsp, server.root_win, 0, 0, 1, 1, 0, 0, 0);
181
182 hints.flags = StateHint | WindowGroupHint | IconWindowHint;
183 hints.initial_state = WithdrawnState;
184 hints.window_group = hint_win;
185 hints.icon_window = panel->main_win;
186
187 classhints.res_name = "docker";
188 classhints.res_class = "Docker";
189
190 XSetWMProperties(server.dsp, hint_win, NULL, NULL, NULL, 0,
191 NULL, &hints, &classhints);
192
193 XMapWindow(server.dsp, hint_win);
194 }
195 */
196
197 int init_net()
198 {
199 if (XGetSelectionOwner(server.dsp, server.atom._NET_SYSTEM_TRAY_SCREEN) != None) {
200 fprintf(stderr, "tint2 : another systray is running\n");
201 return 0;
202 }
203
204 //create_hint_win();
205
206 // init systray protocol
207 net_sel_win = XCreateSimpleWindow(server.dsp, server.root_win, -1, -1, 1, 1, 0, 0, 0);
208
209 // v0.2 trayer specification. tint2 always orizontal.
210 int orient = 0;
211 XChangeProperty(server.dsp, net_sel_win, server.atom._NET_SYSTEM_TRAY_ORIENTATION, XA_CARDINAL, 32, PropModeReplace, (unsigned char *) &orient, 1);
212
213 XSetSelectionOwner(server.dsp, server.atom._NET_SYSTEM_TRAY_SCREEN, net_sel_win, CurrentTime);
214 if (XGetSelectionOwner(server.dsp, server.atom._NET_SYSTEM_TRAY_SCREEN) != net_sel_win) {
215 fprintf(stderr, "tint2 : can't get systray manager\n");
216 return 0;
217 }
218
219 XClientMessageEvent ev;
220 ev.type = ClientMessage;
221 ev.window = server.root_win;
222 ev.message_type = server.atom.MANAGER;
223 ev.format = 32;
224 ev.data.l[0] = CurrentTime;
225 ev.data.l[1] = server.atom._NET_SYSTEM_TRAY_SCREEN;
226 ev.data.l[2] = net_sel_win;
227 ev.data.l[3] = 0;
228 ev.data.l[4] = 0;
229 XSendEvent(server.dsp, server.root_win, False, StructureNotifyMask, (XEvent*)&ev);
230
231 return 1;
232 }
233
234
235 void cleanup_net()
236 {
237 if (net_sel_win != None) {
238 XDestroyWindow(server.dsp, net_sel_win);
239 net_sel_win = None;
240 }
241 }
242
243
244 /*
245 void fix_geometry()
246 {
247 GSList *it;
248 Panel *panel = systray.area.panel;
249
250 // find the proper width and height
251 width = 0;
252 height = icon_size;
253 for (it = icons; it != NULL; it = g_slist_next(it)) {
254 width += icon_size;
255 }
256
257 XResizeWindow(server.dsp, panel->main_win, width + border * 2, height + border * 2);
258 }
259 */
260
261 gboolean error;
262 int window_error_handler(Display *d, XErrorEvent *e)
263 {
264 d=d;e=e;
265 error = TRUE;
266 if (e->error_code != BadWindow) {
267 printf("error_handler %d\n", e->error_code);
268 }
269 return 0;
270 }
271
272
273 gboolean icon_swallow(Window id)
274 {
275 XErrorHandler old;
276 Panel *panel = systray.area.panel;
277
278 error = FALSE;
279 old = XSetErrorHandler(window_error_handler);
280 XReparentWindow(server.dsp, id, panel->main_win, 0, 0);
281 printf("icon_swallow %lx %lx\n", id, panel->main_win);
282 XSync(server.dsp, False);
283 XSetErrorHandler(old);
284
285 return !error;
286 }
287
288
289 // The traywin must have its id and type set.
290 gboolean add_icon(Window id)
291 {
292 TrayWindow *traywin;
293
294 if (!icon_swallow(id)) {
295 fprintf(stderr, "tint2 : not icon_swallow\n");
296 return FALSE;
297 }
298
299 traywin = g_new0(TrayWindow, 1);
300 traywin->id = id;
301
302 systray.list_icons = g_slist_prepend(systray.list_icons, traywin);
303 printf("ajout d'un icone %d (%lx)\n", g_slist_length(systray.list_icons), id);
304 systray.area.resize = 1;
305 systray.area.redraw = 1;
306
307 // changed in systray force resize on panel
308 Panel *panel = systray.area.panel;
309 panel->area.resize = 1;
310 panel_refresh = 1;
311
312 return TRUE;
313 }
314
315
316 void remove_icon(TrayWindow *traywin)
317 {
318 XErrorHandler old;
319
320 XSelectInput(server.dsp, traywin->id, NoEventMask);
321
322 // reparent to root
323 error = FALSE;
324 old = XSetErrorHandler(window_error_handler);
325 XReparentWindow(server.dsp, traywin->id, server.root_win, 0, 0);
326 XSync(server.dsp, False);
327 XSetErrorHandler(old);
328
329 // remove from our list
330 systray.list_icons = g_slist_remove(systray.list_icons, traywin);
331 g_free(traywin);
332 printf("suppression d'un icone %d\n", g_slist_length(systray.list_icons));
333 systray.area.resize = 1;
334
335 // changed in systray force resize on panel
336 Panel *panel = systray.area.panel;
337 panel->area.resize = 1;
338 panel_refresh = 1;
339
340 }
341
342
343 void net_message(XClientMessageEvent *e)
344 {
345 unsigned long opcode;
346 Window id;
347
348 opcode = e->data.l[1];
349 switch (opcode) {
350 case SYSTEM_TRAY_REQUEST_DOCK:
351 id = e->data.l[2];
352 if (id) add_icon(id);
353 break;
354
355 case SYSTEM_TRAY_BEGIN_MESSAGE:
356 case SYSTEM_TRAY_CANCEL_MESSAGE:
357 // we don't show baloons messages.
358 break;
359
360 default:
361 if (opcode == server.atom._NET_SYSTEM_TRAY_MESSAGE_DATA) {
362 printf("message from dockapp: %s\n", e->data.b);
363 }
364 else
365 printf("SYSTEM_TRAY : unknown message type\n");
366 break;
367 }
368 }
369
370
371 void refresh_systray()
372 {
373 TrayWindow *traywin;
374 GSList *l;
375 for (l = systray.list_icons; l ; l = l->next) {
376 traywin = (TrayWindow*)l->data;
377 XClearArea(server.dsp, traywin->id, 0, 0, traywin->width, traywin->height, True);
378 }
379 }
380
381
This page took 0.051337 seconds and 5 git commands to generate.