]> Dogcows Code - chaz/tint2/blob - src/systray/systraybar.c
change systray code. fixed issue 91.
[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
46
47 void init_systray()
48 {
49 Panel *panel = &panel1[0];
50
51 if (systray.area.on_screen)
52 systray.area.on_screen = init_net();
53
54 if (!systray.area.on_screen)
55 return;
56
57 systray.area.parent = panel;
58 systray.area.panel = panel;
59 systray.area._resize = resize_systray;
60
61 // configure systray
62 // draw only one systray (even with multi panel)
63 systray.area.posy = panel->area.pix.border.width + panel->area.paddingy;
64 systray.area.height = panel->area.height - (2 * systray.area.posy);
65 systray.area.width = 0;
66 systray.area.redraw = 1;
67
68 systray.area.posx = panel->area.width - panel->area.paddingxlr - panel->area.pix.border.width - systray.area.width;
69 if (panel->clock.area.on_screen)
70 systray.area.posx -= (panel->clock.area.width + panel->area.paddingx);
71 #ifdef ENABLE_BATTERY
72 if (panel->battery.area.on_screen)
73 systray.area.posx -= (panel->battery.area.width + panel->area.paddingx);
74 #endif
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 resize_systray(void *obj)
97 {
98 Systraybar *sysbar = obj;
99 Panel *panel = sysbar->area.panel;
100 TrayWindow *traywin;
101 GSList *l;
102 int count, posx, posy;
103 int icon_size;
104
105 icon_size = sysbar->area.height - (2 * sysbar->area.pix.border.width) - (2 * sysbar->area.paddingy);
106 count = g_slist_length(systray.list_icons);
107
108 if (!count) systray.area.width = 0;
109 else systray.area.width = (2 * systray.area.pix.border.width) + (2 * systray.area.paddingxlr) + (icon_size * count) + ((count-1) * systray.area.paddingx);
110
111 systray.area.posx = panel->area.width - panel->area.pix.border.width - panel->area.paddingxlr - systray.area.width;
112 if (panel->clock.area.on_screen)
113 systray.area.posx -= (panel->clock.area.width + panel->area.paddingx);
114 #ifdef ENABLE_BATTERY
115 if (panel->battery.area.on_screen)
116 systray.area.posx -= (panel->battery.area.width + panel->area.paddingx);
117 #endif
118
119 posy = panel->area.pix.border.width + panel->area.paddingy + systray.area.pix.border.width + systray.area.paddingy;
120 posx = systray.area.posx + systray.area.pix.border.width + systray.area.paddingxlr;
121 for (l = systray.list_icons; l ; l = l->next) {
122 traywin = (TrayWindow*)l->data;
123
124 traywin->y = posy;
125 traywin->x = posx;
126 traywin->width = icon_size;
127 traywin->height = icon_size;
128 posx += (icon_size + systray.area.paddingx);
129
130 // position and size the icon window
131 XMoveResizeWindow(server.dsp, traywin->id, traywin->x, traywin->y, icon_size, icon_size);
132 }
133 //printf("resize_systray %d %d\n", systray.area.posx, systray.area.width);
134 }
135
136 /*
137 void create_hint_win()
138 {
139 XWMHints hints;
140 XClassHint classhints;
141 Panel *panel = systray.area.panel;
142
143 hint_win = XCreateSimpleWindow(server.dsp, server.root_win, 0, 0, 1, 1, 0, 0, 0);
144
145 hints.flags = StateHint | WindowGroupHint | IconWindowHint;
146 hints.initial_state = WithdrawnState;
147 hints.window_group = hint_win;
148 hints.icon_window = panel->main_win;
149
150 classhints.res_name = "docker";
151 classhints.res_class = "Docker";
152
153 XSetWMProperties(server.dsp, hint_win, NULL, NULL, NULL, 0,
154 NULL, &hints, &classhints);
155
156 XMapWindow(server.dsp, hint_win);
157 }
158 */
159
160 int init_net()
161 {
162 if (XGetSelectionOwner(server.dsp, server.atom._NET_SYSTEM_TRAY_SCREEN) != None) {
163 fprintf(stderr, "tint2 : another systray is running\n");
164 return 0;
165 }
166
167 //create_hint_win();
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 return 1;
194 }
195
196
197 void cleanup_net()
198 {
199 if (net_sel_win != None) {
200 XDestroyWindow(server.dsp, net_sel_win);
201 net_sel_win = None;
202 }
203 }
204
205
206 gboolean error;
207 int window_error_handler(Display *d, XErrorEvent *e)
208 {
209 d=d;e=e;
210 error = TRUE;
211 if (e->error_code != BadWindow) {
212 printf("error_handler %d\n", e->error_code);
213 }
214 return 0;
215 }
216
217
218 // The traywin must have its id and type set.
219 gboolean add_icon(Window id)
220 {
221 TrayWindow *traywin;
222 XErrorHandler old;
223 Panel *panel = systray.area.panel;
224
225 error = FALSE;
226 old = XSetErrorHandler(window_error_handler);
227 XReparentWindow(server.dsp, id, panel->main_win, 0, 0);
228 XSync(server.dsp, False);
229 XSetErrorHandler(old);
230
231 if (error != FALSE) {
232 fprintf(stderr, "tint2 : not icon_swallow\n");
233 return FALSE;
234 }
235
236 traywin = g_new0(TrayWindow, 1);
237 traywin->id = id;
238
239 systray.list_icons = g_slist_prepend(systray.list_icons, traywin);
240 //printf("ajout d'un icone %d (%lx)\n", g_slist_length(systray.list_icons), id);
241 systray.area.resize = 1;
242 systray.area.redraw = 1;
243
244 // watch for the icon trying to resize itself!
245 XSelectInput(server.dsp, traywin->id, StructureNotifyMask);
246
247 // show the window
248 XMapRaised(server.dsp, traywin->id);
249
250 // changed in systray force resize on panel
251 panel->area.resize = 1;
252 panel_refresh = 1;
253 return TRUE;
254 }
255
256
257 void remove_icon(TrayWindow *traywin)
258 {
259 XErrorHandler old;
260
261 XSelectInput(server.dsp, traywin->id, NoEventMask);
262
263 // reparent to root
264 error = FALSE;
265 old = XSetErrorHandler(window_error_handler);
266 XReparentWindow(server.dsp, traywin->id, server.root_win, 0, 0);
267 XSync(server.dsp, False);
268 XSetErrorHandler(old);
269
270 // remove from our list
271 systray.list_icons = g_slist_remove(systray.list_icons, traywin);
272 g_free(traywin);
273 //printf("suppression d'un icone %d\n", g_slist_length(systray.list_icons));
274 systray.area.resize = 1;
275 systray.area.redraw = 1;
276
277 // changed in systray force resize on panel
278 Panel *panel = systray.area.panel;
279 panel->area.resize = 1;
280 panel_refresh = 1;
281
282 }
283
284
285 void net_message(XClientMessageEvent *e)
286 {
287 unsigned long opcode;
288 Window id;
289
290 opcode = e->data.l[1];
291 switch (opcode) {
292 case SYSTEM_TRAY_REQUEST_DOCK:
293 id = e->data.l[2];
294 if (id) add_icon(id);
295 break;
296
297 case SYSTEM_TRAY_BEGIN_MESSAGE:
298 case SYSTEM_TRAY_CANCEL_MESSAGE:
299 // we don't show baloons messages.
300 break;
301
302 default:
303 if (opcode == server.atom._NET_SYSTEM_TRAY_MESSAGE_DATA) {
304 printf("message from dockapp: %s\n", e->data.b);
305 }
306 else
307 printf("SYSTEM_TRAY : unknown message type\n");
308 break;
309 }
310 }
311
312
313 void refresh_systray()
314 {
315 TrayWindow *traywin;
316 GSList *l;
317 for (l = systray.list_icons; l ; l = l->next) {
318 traywin = (TrayWindow*)l->data;
319 XClearArea(server.dsp, traywin->id, 0, 0, traywin->width, traywin->height, True);
320 }
321 }
322
323
This page took 0.04931 seconds and 5 git commands to generate.