]> Dogcows Code - chaz/tint2/blob - src/systray/systraybar.c
fixed segfault on tray application due to tint2
[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;
41
42 // freedesktop specification doesn't allow multi systray
43 Systraybar systray;
44
45
46 void init_systray()
47 {
48 cleanup_systray();
49
50 Panel *panel = &panel1[0];
51 systray.area.parent = panel;
52 systray.area.panel = panel;
53 systray.area._draw_foreground = draw_systray;
54 systray.area._resize = resize_systray;
55
56 if (systray.area.on_screen) {
57 if (XGetSelectionOwner(server.dsp, server.atom._NET_SYSTEM_TRAY_SCREEN) != None) {
58 fprintf(stderr, "tint2 : another systray is running\n");
59 systray.area.on_screen = 0;
60 }
61 }
62
63 if (systray.area.on_screen)
64 systray.area.on_screen = net_init();
65
66 if (!systray.area.on_screen)
67 return;
68
69 // configure systray
70 // draw only one systray (even with multi panel)
71 systray.area.posy = panel->area.pix.border.width + panel->area.paddingy;
72 systray.area.height = panel->area.height - (2 * systray.area.posy);
73 systray.area.width = 0;
74
75 systray.area.posx = panel->area.width - panel->area.paddingxlr - panel->area.pix.border.width - systray.area.width;
76 if (panel->clock.area.on_screen)
77 systray.area.posx -= (panel->clock.area.width + panel->area.paddingx);
78
79 systray.area.redraw = 1;
80 }
81
82
83 void cleanup_systray()
84 {
85 if (systray.list_icons) {
86 GSList *it;
87
88 for (it = systray.list_icons; it; it = it->next)
89 remove_icon((TrayWindow*)it->data);
90
91 g_slist_free(systray.list_icons);
92 systray.list_icons = 0;
93 }
94
95 free_area(&systray.area);
96
97 if (net_sel_win != None) {
98 XDestroyWindow(server.dsp, net_sel_win);
99 net_sel_win = None;
100 }
101 }
102
103
104 void draw_systray(void *obj, cairo_t *c, int active)
105 {
106 Systraybar *sysbar = obj;
107 Panel *panel = sysbar->area.panel;
108 TrayWindow *traywin;
109 GSList *l;
110 int icon_size;
111
112 icon_size = sysbar->area.height - (2 * sysbar->area.pix.border.width) - (2 * sysbar->area.paddingy);
113 for (l = systray.list_icons; l ; l = l->next) {
114 traywin = (TrayWindow*)l->data;
115
116 printf("draw_systray %d %d\n", systray.area.posx, systray.area.width);
117 // watch for the icon trying to resize itself!
118 XSelectInput(server.dsp, traywin->id, StructureNotifyMask);
119
120 // position and size the icon window
121 XMoveResizeWindow(server.dsp, traywin->id, traywin->x, traywin->y, icon_size, icon_size);
122
123 // resize our window so that the new window can fit in it
124 //fix_geometry();
125
126 // flush before clearing, otherwise the clear isn't effective.
127 XFlush(server.dsp);
128 // make sure the new child will get the right stuff in its background
129 // for ParentRelative.
130 XClearWindow(server.dsp, panel->main_win);
131
132 // show the window
133 XMapRaised(server.dsp, traywin->id);
134 }
135 }
136
137
138 void resize_systray(void *obj)
139 {
140 Systraybar *sysbar = obj;
141 Panel *panel = sysbar->area.panel;
142 TrayWindow *traywin;
143 GSList *l;
144 int count, posx, posy;
145 int icon_size;
146
147 icon_size = sysbar->area.height - (2 * sysbar->area.pix.border.width) - (2 * sysbar->area.paddingy);
148 count = g_slist_length(systray.list_icons);
149
150 if (!count) systray.area.width = 0;
151 else systray.area.width = (2 * systray.area.pix.border.width) + (2 * systray.area.paddingxlr) + (icon_size * count) + ((count-1) * systray.area.paddingx);
152
153 systray.area.posx = panel->area.width - panel->area.pix.border.width - panel->area.paddingxlr - systray.area.width;
154 if (panel->clock.area.on_screen)
155 systray.area.posx -= (panel->clock.area.width + panel->area.paddingx);
156
157 systray.area.redraw = 1;
158
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 for (l = systray.list_icons; l ; l = l->next) {
162 traywin = (TrayWindow*)l->data;
163
164 traywin->y = posy;
165 traywin->x = posx;
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 int net_init()
175 {
176 // init systray protocol
177 net_sel_win = XCreateSimpleWindow(server.dsp, server.root_win, -1, -1, 1, 1, 0, 0, 0);
178
179 // v0.2 trayer specification. tint2 always orizontal.
180 int orient = 0;
181 XChangeProperty(server.dsp, net_sel_win, server.atom._NET_SYSTEM_TRAY_ORIENTATION, XA_CARDINAL, 32, PropModeReplace, (unsigned char *) &orient, 1);
182
183 XSetSelectionOwner(server.dsp, server.atom._NET_SYSTEM_TRAY_SCREEN, net_sel_win, CurrentTime);
184 if (XGetSelectionOwner(server.dsp, server.atom._NET_SYSTEM_TRAY_SCREEN) != net_sel_win) {
185 fprintf(stderr, "tint2 : can't get systray manager\n");
186 return 0;
187 }
188
189 XClientMessageEvent ev;
190 ev.type = ClientMessage;
191 ev.window = server.root_win;
192 ev.message_type = server.atom.MANAGER;
193 ev.format = 32;
194 ev.data.l[0] = CurrentTime;
195 ev.data.l[1] = server.atom._NET_SYSTEM_TRAY_SCREEN;
196 ev.data.l[2] = net_sel_win;
197 ev.data.l[3] = 0;
198 ev.data.l[4] = 0;
199 XSendEvent(server.dsp, server.root_win, False, StructureNotifyMask, (XEvent*)&ev);
200
201 return 1;
202 }
203
204
205 //int width, height;
206
207 /*
208 void fix_geometry()
209 {
210 GSList *it;
211 Panel *panel = systray.area.panel;
212
213 // find the proper width and height
214 width = 0;
215 height = icon_size;
216 for (it = icons; it != NULL; it = g_slist_next(it)) {
217 width += icon_size;
218 }
219
220 XResizeWindow(server.dsp, panel->main_win, width + border * 2, height + border * 2);
221 }
222 */
223
224 gboolean error;
225 int window_error_handler(Display *d, XErrorEvent *e)
226 {
227 d=d;e=e;
228 if (e->error_code == BadWindow) {
229 error = TRUE;
230 } else {
231 //g_printerr("X ERROR NOT BAD WINDOW!\n");
232 abort();
233 }
234 return 0;
235 }
236
237
238 gboolean icon_swallow(Window id)
239 {
240 XErrorHandler old;
241 Panel *panel = systray.area.panel;
242
243 error = FALSE;
244 old = XSetErrorHandler(window_error_handler);
245 XReparentWindow(server.dsp, id, panel->main_win, 0, 0);
246 XSync(server.dsp, False);
247 XSetErrorHandler(old);
248
249 return !error;
250 }
251
252
253 // The traywin must have its id and type set.
254 gboolean add_icon(Window id)
255 {
256 TrayWindow *traywin;
257
258 if (!icon_swallow(id)) {
259 fprintf(stderr, "tint2 : not icon_swallow\n");
260 return FALSE;
261 }
262
263 traywin = g_new0(TrayWindow, 1);
264 traywin->id = id;
265
266 systray.list_icons = g_slist_prepend(systray.list_icons, traywin);
267 printf("ajout d'un icone %d (%lx)\n", g_slist_length(systray.list_icons), id);
268 systray.area.resize = 1;
269 systray.area.redraw = 1;
270
271 // changed in systray force resize on panel
272 Panel *panel = systray.area.panel;
273 panel->area.resize = 1;
274 panel_refresh = 1;
275
276 // => calcul x, y, width, height dans resize
277 /*
278 // find the positon for the systray app window
279 int count = g_slist_length(icons);
280 traywin->x = border + ((width % icon_size) / 2) +
281 (count % (width / icon_size)) * icon_size;
282 traywin->y = border + ((height % icon_size) / 2) +
283 (count / (height / icon_size)) * icon_size;
284
285 // add the new icon to the list
286 icons = g_slist_append(icons, traywin);
287 */
288
289 return TRUE;
290 }
291
292
293 void remove_icon(TrayWindow *traywin)
294 {
295 XErrorHandler old;
296
297 XSelectInput(server.dsp, traywin->id, NoEventMask);
298
299 // reparent to root
300 error = FALSE;
301 old = XSetErrorHandler(window_error_handler);
302 XReparentWindow(server.dsp, traywin->id, server.root_win, 0, 0);
303 XSync(server.dsp, False);
304 XSetErrorHandler(old);
305
306 // remove from our list
307 systray.list_icons = g_slist_remove(systray.list_icons, traywin);
308 g_free(traywin);
309 printf("suppression d'un icone %d\n", g_slist_length(systray.list_icons));
310 systray.area.resize = 1;
311
312 // changed in systray force resize on panel
313 Panel *panel = systray.area.panel;
314 panel->area.resize = 1;
315 panel_refresh = 1;
316
317 }
318
319
320 void net_message(XClientMessageEvent *e)
321 {
322 unsigned long opcode;
323 Window id;
324
325 opcode = e->data.l[1];
326
327 switch (opcode) {
328 case SYSTEM_TRAY_REQUEST_DOCK:
329 id = e->data.l[2];
330 if (id) add_icon(id);
331 break;
332
333 case SYSTEM_TRAY_BEGIN_MESSAGE:
334 printf("message from dockapp\n");
335 id = e->window;
336 break;
337
338 case SYSTEM_TRAY_CANCEL_MESSAGE:
339 printf("message cancelled\n");
340 id = e->window;
341 break;
342
343 default:
344 if (opcode == server.atom._NET_SYSTEM_TRAY_MESSAGE_DATA) {
345 printf("message from dockapp:\n %s\n", e->data.b);
346 id = e->window;
347 }
348 // unknown message type. not in the spec
349 break;
350 }
351 }
352
353
354
This page took 0.054579 seconds and 5 git commands to generate.