Make callbacks to rules.execute optional
[awesome.git] / systray.c
blobe3423acc729996ef54eab98042c6bf8fd0ba9342
1 /*
2 * systray.c - systray handling
4 * Copyright © 2008-2009 Julien Danjou <julien@danjou.info>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
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.
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 #include "systray.h"
23 #include "common/atoms.h"
24 #include "objects/drawin.h"
25 #include "xwindow.h"
26 #include "globalconf.h"
28 #include <xcb/xcb.h>
29 #include <xcb/xcb_icccm.h>
30 #include <xcb/xcb_atom.h>
32 #define SYSTEM_TRAY_REQUEST_DOCK 0 /* Begin icon docking */
34 /** Initialize systray information in X.
36 void
37 systray_init(void)
39 xcb_screen_t *xscreen = globalconf.screen;
41 globalconf.systray.window = xcb_generate_id(globalconf.connection);
42 xcb_create_window(globalconf.connection, xscreen->root_depth,
43 globalconf.systray.window,
44 xscreen->root,
45 -1, -1, 1, 1, 0,
46 XCB_COPY_FROM_PARENT, xscreen->root_visual,
47 0, NULL);
50 /** Register systray in X.
52 static void
53 systray_register(void)
55 xcb_client_message_event_t ev;
56 xcb_screen_t *xscreen = globalconf.screen;
57 char *atom_name;
58 xcb_intern_atom_cookie_t atom_systray_q;
59 xcb_intern_atom_reply_t *atom_systray_r;
60 xcb_atom_t atom_systray;
62 /* Send requests */
63 if(!(atom_name = xcb_atom_name_by_screen("_NET_SYSTEM_TRAY", globalconf.default_screen)))
65 warn("error getting systray atom");
66 return;
69 atom_systray_q = xcb_intern_atom_unchecked(globalconf.connection, false,
70 a_strlen(atom_name), atom_name);
72 p_delete(&atom_name);
74 /* Fill event */
75 p_clear(&ev, 1);
76 ev.response_type = XCB_CLIENT_MESSAGE;
77 ev.window = xscreen->root;
78 ev.format = 32;
79 ev.type = MANAGER;
80 ev.data.data32[0] = XCB_CURRENT_TIME;
81 ev.data.data32[2] = globalconf.systray.window;
82 ev.data.data32[3] = ev.data.data32[4] = 0;
84 if(!(atom_systray_r = xcb_intern_atom_reply(globalconf.connection, atom_systray_q, NULL)))
86 warn("error getting systray atom");
87 return;
90 ev.data.data32[1] = atom_systray = atom_systray_r->atom;
92 p_delete(&atom_systray_r);
94 xcb_set_selection_owner(globalconf.connection,
95 globalconf.systray.window,
96 atom_systray,
97 XCB_CURRENT_TIME);
99 xcb_send_event(globalconf.connection, false, xscreen->root, 0xFFFFFF, (char *) &ev);
102 /** Remove systray information in X.
104 void
105 systray_cleanup(void)
107 xcb_intern_atom_reply_t *atom_systray_r;
108 char *atom_name;
110 if(!(atom_name = xcb_atom_name_by_screen("_NET_SYSTEM_TRAY", globalconf.default_screen))
111 || !(atom_systray_r = xcb_intern_atom_reply(globalconf.connection,
112 xcb_intern_atom_unchecked(globalconf.connection,
113 false,
114 a_strlen(atom_name),
115 atom_name),
116 NULL)))
118 warn("error getting systray atom");
119 p_delete(&atom_name);
120 return;
123 p_delete(&atom_name);
125 xcb_set_selection_owner(globalconf.connection,
126 XCB_NONE,
127 atom_systray_r->atom,
128 XCB_CURRENT_TIME);
130 p_delete(&atom_systray_r);
132 xcb_unmap_window(globalconf.connection,
133 globalconf.systray.window);
136 /** Handle a systray request.
137 * \param embed_win The window to embed.
138 * \param info The embedding info
139 * \return 0 on no error.
142 systray_request_handle(xcb_window_t embed_win, xembed_info_t *info)
144 xembed_window_t em;
145 xcb_get_property_cookie_t em_cookie;
146 const uint32_t select_input_val[] =
148 XCB_EVENT_MASK_STRUCTURE_NOTIFY
149 | XCB_EVENT_MASK_PROPERTY_CHANGE
150 | XCB_EVENT_MASK_ENTER_WINDOW
153 /* check if not already trayed */
154 if(xembed_getbywin(&globalconf.embedded, embed_win))
155 return -1;
157 p_clear(&em_cookie, 1);
159 if(!info)
160 em_cookie = xembed_info_get_unchecked(globalconf.connection, embed_win);
162 xcb_change_window_attributes(globalconf.connection, embed_win, XCB_CW_EVENT_MASK,
163 select_input_val);
165 /* we grab the window, but also make sure it's automatically reparented back
166 * to the root window if we should die.
168 xcb_change_save_set(globalconf.connection, XCB_SET_MODE_INSERT, embed_win);
169 xcb_reparent_window(globalconf.connection, embed_win,
170 globalconf.systray.window,
171 0, 0);
173 em.win = embed_win;
175 if(info)
176 em.info = *info;
177 else
178 xembed_info_get_reply(globalconf.connection, em_cookie, &em.info);
180 xembed_embedded_notify(globalconf.connection, em.win,
181 globalconf.systray.window,
182 MIN(XEMBED_VERSION, em.info.version));
184 xembed_window_array_append(&globalconf.embedded, em);
185 luaA_systray_invalidate();
187 return 0;
190 /** Handle systray message.
191 * \param ev The event.
192 * \return 0 on no error.
195 systray_process_client_message(xcb_client_message_event_t *ev)
197 int ret = 0;
198 xcb_get_geometry_cookie_t geom_c;
199 xcb_get_geometry_reply_t *geom_r;
201 switch(ev->data.data32[1])
203 case SYSTEM_TRAY_REQUEST_DOCK:
204 geom_c = xcb_get_geometry_unchecked(globalconf.connection, ev->window);
206 if(!(geom_r = xcb_get_geometry_reply(globalconf.connection, geom_c, NULL)))
207 return -1;
209 if(globalconf.screen->root == geom_r->root)
210 ret = systray_request_handle(ev->data.data32[2], NULL);
212 p_delete(&geom_r);
213 break;
216 return ret;
219 /** Check if a window is a KDE tray.
220 * \param w The window to check.
221 * \return True if it is, false otherwise.
223 bool
224 systray_iskdedockapp(xcb_window_t w)
226 xcb_get_property_cookie_t kde_check_q;
227 xcb_get_property_reply_t *kde_check;
228 bool ret;
230 /* Check if that is a KDE tray because it does not respect fdo standards,
231 * thanks KDE. */
232 kde_check_q = xcb_get_property_unchecked(globalconf.connection, false, w,
233 _KDE_NET_WM_SYSTEM_TRAY_WINDOW_FOR,
234 XCB_ATOM_WINDOW, 0, 1);
236 kde_check = xcb_get_property_reply(globalconf.connection, kde_check_q, NULL);
238 /* it's a KDE systray ?*/
239 ret = (kde_check && kde_check->value_len);
241 p_delete(&kde_check);
243 return ret;
246 /** Handle xembed client message.
247 * \param ev The event.
248 * \return 0 on no error.
251 xembed_process_client_message(xcb_client_message_event_t *ev)
253 switch(ev->data.data32[1])
255 case XEMBED_REQUEST_FOCUS:
256 xembed_focus_in(globalconf.connection, ev->window, XEMBED_FOCUS_CURRENT);
257 break;
259 return 0;
262 /** Inform lua that the systray needs to be updated.
264 void
265 luaA_systray_invalidate(void)
267 signal_object_emit(globalconf.L, &global_signals, "systray::update", 0);
270 static void
271 systray_update(int base_size, bool horizontal)
273 if(base_size <= 0)
274 return;
276 /* Give the systray window the correct size */
277 uint32_t config_vals[4] = { base_size, base_size, 0, 0 };
278 if(horizontal)
279 config_vals[0] = base_size * globalconf.embedded.len;
280 else
281 config_vals[1] = base_size * globalconf.embedded.len;
282 xcb_configure_window(globalconf.connection,
283 globalconf.systray.window,
284 XCB_CONFIG_WINDOW_WIDTH | XCB_CONFIG_WINDOW_HEIGHT,
285 config_vals);
287 /* Now resize each embedded window */
288 config_vals[0] = config_vals[1] = 0;
289 config_vals[2] = config_vals[3] = base_size;
290 for(int i = 0; i < globalconf.embedded.len; i++)
292 xembed_window_t *em = &globalconf.embedded.tab[i];
293 xcb_configure_window(globalconf.connection, em->win,
294 XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y | XCB_CONFIG_WINDOW_WIDTH | XCB_CONFIG_WINDOW_HEIGHT,
295 config_vals);
296 xcb_map_window(globalconf.connection, em->win);
297 if(horizontal)
298 config_vals[0] += base_size;
299 else
300 config_vals[1] += base_size;
304 /** Update the systray
305 * \param L The Lua VM state.
306 * \return The number of elements pushed on stack.
307 * \luastack
308 * \lparam The drawin to display the systray in.
309 * \lparam x X position for the systray.
310 * \lparam y Y position for the systray.
311 * \lparam base_size The size (width and height) each systray item gets.
312 * \lparam horiz If true, the systray is horizontal, else vertical
315 luaA_systray(lua_State *L)
317 if(lua_gettop(L) != 0)
319 size_t bg_len;
320 drawin_t *w = luaA_checkudata(L, 1, &drawin_class);
321 int x = luaL_checknumber(L, 2);
322 int y = luaL_checknumber(L, 3);
323 int base_size = luaL_checknumber(L, 4);
324 bool horiz = lua_toboolean(L, 5);
325 const char *bg = luaL_checklstring(L, 6, &bg_len);
326 color_t bg_color;
328 if(color_init_reply(color_init_unchecked(&bg_color, bg, bg_len)))
330 uint32_t config_back[] = { bg_color.pixel };
331 xcb_change_window_attributes(globalconf.connection,
332 globalconf.systray.window,
333 XCB_CW_BACK_PIXEL, config_back);
336 if(globalconf.systray.parent == NULL)
337 systray_register();
339 if(globalconf.systray.parent != w)
340 xcb_reparent_window(globalconf.connection,
341 globalconf.systray.window,
342 w->window,
343 x, y);
344 else
346 uint32_t config_vals[2] = { x, y };
347 xcb_configure_window(globalconf.connection,
348 globalconf.systray.window,
349 XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y,
350 config_vals);
353 globalconf.systray.parent = w;
355 if(globalconf.embedded.len != 0)
357 systray_update(base_size, horiz);
358 xcb_map_window(globalconf.connection,
359 globalconf.systray.window);
361 else
362 xcb_unmap_window(globalconf.connection,
363 globalconf.systray.window);
366 lua_pushnumber(L, globalconf.embedded.len);
367 luaA_object_push(L, globalconf.systray.parent);
368 return 2;
371 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:textwidth=80