tag: Improve tag property::index support (FS#1229)
[awesome.git] / systray.c
blob86ffe9e6584c2e90fc1858de78e622db52066808
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_intern_atom_cookie_t atom_systray_q;
40 xcb_intern_atom_reply_t *atom_systray_r;
41 char *atom_name;
42 xcb_screen_t *xscreen = globalconf.screen;
44 globalconf.systray.window = xcb_generate_id(globalconf.connection);
45 xcb_create_window(globalconf.connection, xscreen->root_depth,
46 globalconf.systray.window,
47 xscreen->root,
48 -1, -1, 1, 1, 0,
49 XCB_COPY_FROM_PARENT, xscreen->root_visual,
50 0, NULL);
52 atom_name = xcb_atom_name_by_screen("_NET_SYSTEM_TRAY", globalconf.default_screen);
53 if(!atom_name)
54 fatal("error getting systray atom name");
56 atom_systray_q = xcb_intern_atom_unchecked(globalconf.connection, false,
57 a_strlen(atom_name), atom_name);
59 p_delete(&atom_name);
61 atom_systray_r = xcb_intern_atom_reply(globalconf.connection, atom_systray_q, NULL);
62 if(!atom_systray_r)
63 fatal("error getting systray atom");
65 globalconf.systray.atom = atom_systray_r->atom;
66 p_delete(&atom_systray_r);
69 /** Register systray in X.
71 static void
72 systray_register(void)
74 xcb_client_message_event_t ev;
75 xcb_screen_t *xscreen = globalconf.screen;
77 if(globalconf.systray.registered)
78 return;
80 globalconf.systray.registered = true;
82 /* Fill event */
83 p_clear(&ev, 1);
84 ev.response_type = XCB_CLIENT_MESSAGE;
85 ev.window = xscreen->root;
86 ev.format = 32;
87 ev.type = MANAGER;
88 ev.data.data32[0] = XCB_CURRENT_TIME;
89 ev.data.data32[1] = globalconf.systray.atom;
90 ev.data.data32[2] = globalconf.systray.window;
91 ev.data.data32[3] = ev.data.data32[4] = 0;
93 xcb_set_selection_owner(globalconf.connection,
94 globalconf.systray.window,
95 globalconf.systray.atom,
96 XCB_CURRENT_TIME);
98 xcb_send_event(globalconf.connection, false, xscreen->root, 0xFFFFFF, (char *) &ev);
101 /** Remove systray information in X.
103 void
104 systray_cleanup(void)
106 if(!globalconf.systray.registered)
107 return;
109 globalconf.systray.registered = false;
111 xcb_set_selection_owner(globalconf.connection,
112 XCB_NONE,
113 globalconf.systray.atom,
114 XCB_CURRENT_TIME);
116 xcb_unmap_window(globalconf.connection,
117 globalconf.systray.window);
120 /** Handle a systray request.
121 * \param embed_win The window to embed.
122 * \param info The embedding info
123 * \return 0 on no error.
126 systray_request_handle(xcb_window_t embed_win, xembed_info_t *info)
128 xembed_window_t em;
129 xcb_get_property_cookie_t em_cookie;
130 const uint32_t select_input_val[] =
132 XCB_EVENT_MASK_STRUCTURE_NOTIFY
133 | XCB_EVENT_MASK_PROPERTY_CHANGE
134 | XCB_EVENT_MASK_ENTER_WINDOW
137 /* check if not already trayed */
138 if(xembed_getbywin(&globalconf.embedded, embed_win))
139 return -1;
141 p_clear(&em_cookie, 1);
143 if(!info)
144 em_cookie = xembed_info_get_unchecked(globalconf.connection, embed_win);
146 xcb_change_window_attributes(globalconf.connection, embed_win, XCB_CW_EVENT_MASK,
147 select_input_val);
149 /* we grab the window, but also make sure it's automatically reparented back
150 * to the root window if we should die.
152 xcb_change_save_set(globalconf.connection, XCB_SET_MODE_INSERT, embed_win);
153 xcb_reparent_window(globalconf.connection, embed_win,
154 globalconf.systray.window,
155 0, 0);
157 em.win = embed_win;
159 if(info)
160 em.info = *info;
161 else
162 xembed_info_get_reply(globalconf.connection, em_cookie, &em.info);
164 xembed_embedded_notify(globalconf.connection, em.win,
165 globalconf.systray.window,
166 MIN(XEMBED_VERSION, em.info.version));
168 xembed_window_array_append(&globalconf.embedded, em);
169 luaA_systray_invalidate();
171 return 0;
174 /** Handle systray message.
175 * \param ev The event.
176 * \return 0 on no error.
179 systray_process_client_message(xcb_client_message_event_t *ev)
181 int ret = 0;
182 xcb_get_geometry_cookie_t geom_c;
183 xcb_get_geometry_reply_t *geom_r;
185 switch(ev->data.data32[1])
187 case SYSTEM_TRAY_REQUEST_DOCK:
188 geom_c = xcb_get_geometry_unchecked(globalconf.connection, ev->window);
190 if(!(geom_r = xcb_get_geometry_reply(globalconf.connection, geom_c, NULL)))
191 return -1;
193 if(globalconf.screen->root == geom_r->root)
194 ret = systray_request_handle(ev->data.data32[2], NULL);
196 p_delete(&geom_r);
197 break;
200 return ret;
203 /** Check if a window is a KDE tray.
204 * \param w The window to check.
205 * \return True if it is, false otherwise.
207 bool
208 systray_iskdedockapp(xcb_window_t w)
210 xcb_get_property_cookie_t kde_check_q;
211 xcb_get_property_reply_t *kde_check;
212 bool ret;
214 /* Check if that is a KDE tray because it does not respect fdo standards,
215 * thanks KDE. */
216 kde_check_q = xcb_get_property_unchecked(globalconf.connection, false, w,
217 _KDE_NET_WM_SYSTEM_TRAY_WINDOW_FOR,
218 XCB_ATOM_WINDOW, 0, 1);
220 kde_check = xcb_get_property_reply(globalconf.connection, kde_check_q, NULL);
222 /* it's a KDE systray ?*/
223 ret = (kde_check && kde_check->value_len);
225 p_delete(&kde_check);
227 return ret;
230 /** Handle xembed client message.
231 * \param ev The event.
232 * \return 0 on no error.
235 xembed_process_client_message(xcb_client_message_event_t *ev)
237 switch(ev->data.data32[1])
239 case XEMBED_REQUEST_FOCUS:
240 xembed_focus_in(globalconf.connection, ev->window, XEMBED_FOCUS_CURRENT);
241 break;
243 return 0;
246 /** Inform lua that the systray needs to be updated.
248 void
249 luaA_systray_invalidate(void)
251 signal_object_emit(globalconf.L, &global_signals, "systray::update", 0);
254 static void
255 systray_update(int base_size, bool horizontal, bool reverse, int spacing)
257 if(base_size <= 0)
258 return;
260 /* Give the systray window the correct size */
261 uint32_t config_vals[4] = { base_size, base_size, 0, 0 };
262 if(horizontal)
263 config_vals[0] = base_size * globalconf.embedded.len + spacing * (globalconf.embedded.len - 1);
264 else
265 config_vals[1] = base_size * globalconf.embedded.len + spacing * (globalconf.embedded.len - 1);
266 xcb_configure_window(globalconf.connection,
267 globalconf.systray.window,
268 XCB_CONFIG_WINDOW_WIDTH | XCB_CONFIG_WINDOW_HEIGHT,
269 config_vals);
271 /* Now resize each embedded window */
272 config_vals[0] = config_vals[1] = 0;
273 config_vals[2] = config_vals[3] = base_size;
274 for(int i = 0; i < globalconf.embedded.len; i++)
276 xembed_window_t *em;
278 if(reverse)
279 em = &globalconf.embedded.tab[(globalconf.embedded.len - i - 1)];
280 else
281 em = &globalconf.embedded.tab[i];
283 xcb_configure_window(globalconf.connection, em->win,
284 XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y | XCB_CONFIG_WINDOW_WIDTH | XCB_CONFIG_WINDOW_HEIGHT,
285 config_vals);
286 xcb_map_window(globalconf.connection, em->win);
287 if(horizontal)
288 config_vals[0] += base_size + spacing;
289 else
290 config_vals[1] += base_size + spacing;
294 /** Update the systray
295 * \param L The Lua VM state.
296 * \return The number of elements pushed on stack.
297 * \luastack
298 * \lparam The drawin to display the systray in.
299 * \lparam x X position for the systray.
300 * \lparam y Y position for the systray.
301 * \lparam base_size The size (width and height) each systray item gets.
302 * \lparam horiz If true, the systray is horizontal, else vertical.
303 * \lparam bg Color of the systray background.
304 * \lparam revers If true, the systray icon order will be reversed, else default.
305 * \lparam spacing The size of the spacing between icons.
308 luaA_systray(lua_State *L)
310 systray_register();
312 if(lua_gettop(L) != 0)
314 size_t bg_len;
315 drawin_t *w = luaA_checkudata(L, 1, &drawin_class);
316 int x = luaL_checkinteger(L, 2);
317 int y = luaL_checkinteger(L, 3);
318 int base_size = luaL_checkinteger(L, 4);
319 bool horiz = lua_toboolean(L, 5);
320 const char *bg = luaL_checklstring(L, 6, &bg_len);
321 bool revers = lua_toboolean(L, 7);
322 int spacing = luaL_checkinteger(L, 8);
323 color_t bg_color;
325 if(color_init_reply(color_init_unchecked(&bg_color, bg, bg_len)))
327 uint32_t config_back[] = { bg_color.pixel };
328 xcb_change_window_attributes(globalconf.connection,
329 globalconf.systray.window,
330 XCB_CW_BACK_PIXEL, config_back);
333 if(globalconf.systray.parent != w)
334 xcb_reparent_window(globalconf.connection,
335 globalconf.systray.window,
336 w->window,
337 x, y);
338 else
340 uint32_t config_vals[2] = { x, y };
341 xcb_configure_window(globalconf.connection,
342 globalconf.systray.window,
343 XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y,
344 config_vals);
347 globalconf.systray.parent = w;
349 if(globalconf.embedded.len != 0)
351 systray_update(base_size, horiz, revers, spacing);
352 xcb_map_window(globalconf.connection,
353 globalconf.systray.window);
355 else
356 xcb_unmap_window(globalconf.connection,
357 globalconf.systray.window);
360 lua_pushinteger(L, globalconf.embedded.len);
361 luaA_object_push(L, globalconf.systray.parent);
362 return 2;
365 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:textwidth=80