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