]> Dogcows Code - chaz/tint2/blob - src/systray/systraybar.c
revert r78, fixed issue 100
[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 if (panel->battery.area.on_screen)
70 systray.area.posx -= (panel->battery.area.width + panel->area.paddingx);
71
72 systray.area.redraw = 1;
73 }
74
75
76 void cleanup_systray()
77 {
78 if (systray.list_icons) {
79 GSList *it;
80
81 for (it = systray.list_icons; it; it = it->next)
82 remove_icon((TrayWindow*)it->data);
83
84 g_slist_free(systray.list_icons);
85 systray.list_icons = 0;
86 }
87
88 free_area(&systray.area);
89
90 cleanup_net();
91 }
92
93
94 void draw_systray(void *obj, cairo_t *c, int active)
95 {
96 Systraybar *sysbar = obj;
97 Panel *panel = sysbar->area.panel;
98 TrayWindow *traywin;
99 GSList *l;
100 int icon_size;
101
102 //printf("draw_systray %d %d\n", systray.area.posx, systray.area.width);
103 icon_size = sysbar->area.height - (2 * sysbar->area.pix.border.width) - (2 * sysbar->area.paddingy);
104 for (l = systray.list_icons; l ; l = l->next) {
105 traywin = (TrayWindow*)l->data;
106
107 // watch for the icon trying to resize itself!
108 //XSelectInput(server.dsp, traywin->id, StructureNotifyMask|ResizeRedirectMask);
109 XSelectInput(server.dsp, traywin->id, StructureNotifyMask);
110
111 // position and size the icon window
112 XMoveResizeWindow(server.dsp, traywin->id, traywin->x, traywin->y, icon_size, icon_size);
113 //printf("icon %d, %d, %d\n", traywin->x, traywin->y, icon_size);
114 // ceci intervertie les fonds : le premier icone prend le fond du dernier
115 // le dernier prend le fond de l'avant dernier, ...
116 XSetWindowBackgroundPixmap (server.dsp, panel->main_win, systray.area.pix.pmap);
117
118 // flush before clearing, otherwise the clear isn't effective.
119 XFlush(server.dsp);
120 // make sure the new child will get the right stuff in its background
121 // for ParentRelative.
122 XClearWindow(server.dsp, panel->main_win);
123
124 // show the window
125 XMapRaised(server.dsp, traywin->id);
126 }
127 }
128
129
130 void resize_systray(void *obj)
131 {
132 Systraybar *sysbar = obj;
133 Panel *panel = sysbar->area.panel;
134 TrayWindow *traywin;
135 GSList *l;
136 int count, posx, posy;
137 int icon_size;
138
139 icon_size = sysbar->area.height - (2 * sysbar->area.pix.border.width) - (2 * sysbar->area.paddingy);
140 count = g_slist_length(systray.list_icons);
141
142 if (!count) systray.area.width = 0;
143 else systray.area.width = (2 * systray.area.pix.border.width) + (2 * systray.area.paddingxlr) + (icon_size * count) + ((count-1) * systray.area.paddingx);
144
145 systray.area.posx = panel->area.width - panel->area.pix.border.width - panel->area.paddingxlr - systray.area.width;
146 if (panel->clock.area.on_screen)
147 systray.area.posx -= (panel->clock.area.width + panel->area.paddingx);
148 if (panel->battery.area.on_screen)
149 systray.area.posx -= (panel->battery.area.width + panel->area.paddingx);
150
151 systray.area.redraw = 1;
152
153 posy = panel->area.pix.border.width + panel->area.paddingy + systray.area.pix.border.width + systray.area.paddingy;
154 posx = systray.area.posx + systray.area.pix.border.width + systray.area.paddingxlr;
155 for (l = systray.list_icons; l ; l = l->next) {
156 traywin = (TrayWindow*)l->data;
157
158 traywin->y = posy;
159 traywin->x = posx;
160 traywin->width = icon_size;
161 traywin->height = icon_size;
162 posx += (icon_size + systray.area.paddingx);
163 }
164
165 // resize other objects on panel
166 printf("resize_systray %d %d\n", systray.area.posx, systray.area.width);
167 }
168
169 /*
170 void create_hint_win()
171 {
172 XWMHints hints;
173 XClassHint classhints;
174 Panel *panel = systray.area.panel;
175
176 hint_win = XCreateSimpleWindow(server.dsp, server.root_win, 0, 0, 1, 1, 0, 0, 0);
177
178 hints.flags = StateHint | WindowGroupHint | IconWindowHint;
179 hints.initial_state = WithdrawnState;
180 hints.window_group = hint_win;
181 hints.icon_window = panel->main_win;
182
183 classhints.res_name = "docker";
184 classhints.res_class = "Docker";
185
186 XSetWMProperties(server.dsp, hint_win, NULL, NULL, NULL, 0,
187 NULL, &hints, &classhints);
188
189 XMapWindow(server.dsp, hint_win);
190 }
191 */
192
193 int init_net()
194 {
195 if (XGetSelectionOwner(server.dsp, server.atom._NET_SYSTEM_TRAY_SCREEN) != None) {
196 fprintf(stderr, "tint2 : another systray is running\n");
197 return 0;
198 }
199
200 //create_hint_win();
201
202 // init systray protocol
203 net_sel_win = XCreateSimpleWindow(server.dsp, server.root_win, -1, -1, 1, 1, 0, 0, 0);
204
205 // v0.2 trayer specification. tint2 always orizontal.
206 int orient = 0;
207 XChangeProperty(server.dsp, net_sel_win, server.atom._NET_SYSTEM_TRAY_ORIENTATION, XA_CARDINAL, 32, PropModeReplace, (unsigned char *) &orient, 1);
208
209 XSetSelectionOwner(server.dsp, server.atom._NET_SYSTEM_TRAY_SCREEN, net_sel_win, CurrentTime);
210 if (XGetSelectionOwner(server.dsp, server.atom._NET_SYSTEM_TRAY_SCREEN) != net_sel_win) {
211 fprintf(stderr, "tint2 : can't get systray manager\n");
212 return 0;
213 }
214
215 XClientMessageEvent ev;
216 ev.type = ClientMessage;
217 ev.window = server.root_win;
218 ev.message_type = server.atom.MANAGER;
219 ev.format = 32;
220 ev.data.l[0] = CurrentTime;
221 ev.data.l[1] = server.atom._NET_SYSTEM_TRAY_SCREEN;
222 ev.data.l[2] = net_sel_win;
223 ev.data.l[3] = 0;
224 ev.data.l[4] = 0;
225 XSendEvent(server.dsp, server.root_win, False, StructureNotifyMask, (XEvent*)&ev);
226
227 return 1;
228 }
229
230
231 void cleanup_net()
232 {
233 if (net_sel_win != None) {
234 XDestroyWindow(server.dsp, net_sel_win);
235 net_sel_win = None;
236 }
237 }
238
239
240 /*
241 void fix_geometry()
242 {
243 GSList *it;
244 Panel *panel = systray.area.panel;
245
246 // find the proper width and height
247 width = 0;
248 height = icon_size;
249 for (it = icons; it != NULL; it = g_slist_next(it)) {
250 width += icon_size;
251 }
252
253 XResizeWindow(server.dsp, panel->main_win, width + border * 2, height + border * 2);
254 }
255 */
256
257 gboolean error;
258 int window_error_handler(Display *d, XErrorEvent *e)
259 {
260 d=d;e=e;
261 if (e->error_code == BadWindow) {
262 error = TRUE;
263 } else {
264 printf("error_handler %d\n", e->error_code);
265 abort();
266 }
267 return 0;
268 }
269
270
271 gboolean icon_swallow(Window id)
272 {
273 XErrorHandler old;
274 Panel *panel = systray.area.panel;
275
276 error = FALSE;
277 old = XSetErrorHandler(window_error_handler);
278 XReparentWindow(server.dsp, id, panel->main_win, 0, 0);
279 XSync(server.dsp, False);
280 XSetErrorHandler(old);
281
282 return !error;
283 }
284
285
286 // The traywin must have its id and type set.
287 gboolean add_icon(Window id)
288 {
289 TrayWindow *traywin;
290
291 if (!icon_swallow(id)) {
292 fprintf(stderr, "tint2 : not icon_swallow\n");
293 return FALSE;
294 }
295
296 traywin = g_new0(TrayWindow, 1);
297 traywin->id = id;
298
299 systray.list_icons = g_slist_prepend(systray.list_icons, traywin);
300 printf("ajout d'un icone %d (%lx)\n", g_slist_length(systray.list_icons), id);
301 systray.area.resize = 1;
302 systray.area.redraw = 1;
303
304 // changed in systray force resize on panel
305 Panel *panel = systray.area.panel;
306 panel->area.resize = 1;
307 panel_refresh = 1;
308
309 // => calcul x, y, width, height dans resize
310 /*
311 // find the positon for the systray app window
312 int count = g_slist_length(icons);
313 traywin->x = border + ((width % icon_size) / 2) +
314 (count % (width / icon_size)) * icon_size;
315 traywin->y = border + ((height % icon_size) / 2) +
316 (count / (height / icon_size)) * icon_size;
317
318 // add the new icon to the list
319 icons = g_slist_append(icons, traywin);
320 */
321
322 return TRUE;
323 }
324
325
326 void remove_icon(TrayWindow *traywin)
327 {
328 XErrorHandler old;
329
330 XSelectInput(server.dsp, traywin->id, NoEventMask);
331
332 // reparent to root
333 error = FALSE;
334 old = XSetErrorHandler(window_error_handler);
335 XReparentWindow(server.dsp, traywin->id, server.root_win, 0, 0);
336 XSync(server.dsp, False);
337 XSetErrorHandler(old);
338
339 // remove from our list
340 systray.list_icons = g_slist_remove(systray.list_icons, traywin);
341 g_free(traywin);
342 printf("suppression d'un icone %d\n", g_slist_length(systray.list_icons));
343 systray.area.resize = 1;
344
345 // changed in systray force resize on panel
346 Panel *panel = systray.area.panel;
347 panel->area.resize = 1;
348 panel_refresh = 1;
349
350 }
351
352
353 void net_message(XClientMessageEvent *e)
354 {
355 unsigned long opcode;
356 Window id;
357
358 opcode = e->data.l[1];
359 switch (opcode) {
360 case SYSTEM_TRAY_REQUEST_DOCK:
361 id = e->data.l[2];
362 if (id) add_icon(id);
363 break;
364
365 case SYSTEM_TRAY_BEGIN_MESSAGE:
366 case SYSTEM_TRAY_CANCEL_MESSAGE:
367 // we don't show baloons messages.
368 break;
369
370 default:
371 if (opcode == server.atom._NET_SYSTEM_TRAY_MESSAGE_DATA) {
372 printf("message from dockapp: %s\n", e->data.b);
373 }
374 else
375 printf("SYSTEM_TRAY : unknown message type\n");
376 break;
377 }
378 }
379
380
381 void refresh_systray()
382 {
383 TrayWindow *traywin;
384 GSList *l;
385 for (l = systray.list_icons; l ; l = l->next) {
386 traywin = (TrayWindow*)l->data;
387 XClearArea(server.dsp, traywin->id, 0, 0, traywin->width, traywin->height, True);
388 }
389 }
390
391
This page took 0.057271 seconds and 5 git commands to generate.