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.
23 #include "common/atoms.h"
24 #include "objects/drawin.h"
26 #include "globalconf.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.
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
,
46 XCB_COPY_FROM_PARENT
, xscreen
->root_visual
,
50 /** Register systray in X.
53 systray_register(void)
55 xcb_client_message_event_t ev
;
56 xcb_screen_t
*xscreen
= globalconf
.screen
;
58 xcb_intern_atom_cookie_t atom_systray_q
;
59 xcb_intern_atom_reply_t
*atom_systray_r
;
60 xcb_atom_t atom_systray
;
63 if(!(atom_name
= xcb_atom_name_by_screen("_NET_SYSTEM_TRAY", globalconf
.default_screen
)))
65 warn("error getting systray atom");
69 atom_systray_q
= xcb_intern_atom_unchecked(globalconf
.connection
, false,
70 a_strlen(atom_name
), atom_name
);
76 ev
.response_type
= XCB_CLIENT_MESSAGE
;
77 ev
.window
= xscreen
->root
;
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");
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
,
99 xcb_send_event(globalconf
.connection
, false, xscreen
->root
, 0xFFFFFF, (char *) &ev
);
102 /** Remove systray information in X.
105 systray_cleanup(void)
107 xcb_intern_atom_reply_t
*atom_systray_r
;
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
,
118 warn("error getting systray atom");
119 p_delete(&atom_name
);
123 p_delete(&atom_name
);
125 xcb_set_selection_owner(globalconf
.connection
,
127 atom_systray_r
->atom
,
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
)
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
))
157 p_clear(&em_cookie
, 1);
160 em_cookie
= xembed_info_get_unchecked(globalconf
.connection
, embed_win
);
162 xcb_change_window_attributes(globalconf
.connection
, embed_win
, XCB_CW_EVENT_MASK
,
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
,
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();
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
)
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
)))
209 if(globalconf
.screen
->root
== geom_r
->root
)
210 ret
= systray_request_handle(ev
->data
.data32
[2], NULL
);
219 /** Check if a window is a KDE tray.
220 * \param w The window to check.
221 * \return True if it is, false otherwise.
224 systray_iskdedockapp(xcb_window_t w
)
226 xcb_get_property_cookie_t kde_check_q
;
227 xcb_get_property_reply_t
*kde_check
;
230 /* Check if that is a KDE tray because it does not respect fdo standards,
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
);
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
);
262 /** Inform lua that the systray needs to be updated.
265 luaA_systray_invalidate(void)
267 signal_object_emit(globalconf
.L
, &global_signals
, "systray::update", 0);
271 systray_update(int base_size
, bool horizontal
, bool reverse
)
276 /* Give the systray window the correct size */
277 uint32_t config_vals
[4] = { base_size
, base_size
, 0, 0 };
279 config_vals
[0] = base_size
* globalconf
.embedded
.len
;
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
,
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
++)
295 em
= &globalconf
.embedded
.tab
[(globalconf
.embedded
.len
- i
- 1)];
297 em
= &globalconf
.embedded
.tab
[i
];
299 xcb_configure_window(globalconf
.connection
, em
->win
,
300 XCB_CONFIG_WINDOW_X
| XCB_CONFIG_WINDOW_Y
| XCB_CONFIG_WINDOW_WIDTH
| XCB_CONFIG_WINDOW_HEIGHT
,
302 xcb_map_window(globalconf
.connection
, em
->win
);
304 config_vals
[0] += base_size
;
306 config_vals
[1] += base_size
;
310 /** Update the systray
311 * \param L The Lua VM state.
312 * \return The number of elements pushed on stack.
314 * \lparam The drawin to display the systray in.
315 * \lparam x X position for the systray.
316 * \lparam y Y position for the systray.
317 * \lparam base_size The size (width and height) each systray item gets.
318 * \lparam horiz If true, the systray is horizontal, else vertical
319 * \lparam bg Color of the systray background
320 * \lparam revers If true, the systray icon order will be reversed, else default
323 luaA_systray(lua_State
*L
)
325 if(lua_gettop(L
) != 0)
328 drawin_t
*w
= luaA_checkudata(L
, 1, &drawin_class
);
329 int x
= luaL_checknumber(L
, 2);
330 int y
= luaL_checknumber(L
, 3);
331 int base_size
= luaL_checknumber(L
, 4);
332 bool horiz
= lua_toboolean(L
, 5);
333 const char *bg
= luaL_checklstring(L
, 6, &bg_len
);
334 bool revers
= lua_toboolean(L
, 7);
337 if(color_init_reply(color_init_unchecked(&bg_color
, bg
, bg_len
)))
339 uint32_t config_back
[] = { bg_color
.pixel
};
340 xcb_change_window_attributes(globalconf
.connection
,
341 globalconf
.systray
.window
,
342 XCB_CW_BACK_PIXEL
, config_back
);
345 if(globalconf
.systray
.parent
== NULL
)
348 if(globalconf
.systray
.parent
!= w
)
349 xcb_reparent_window(globalconf
.connection
,
350 globalconf
.systray
.window
,
355 uint32_t config_vals
[2] = { x
, y
};
356 xcb_configure_window(globalconf
.connection
,
357 globalconf
.systray
.window
,
358 XCB_CONFIG_WINDOW_X
| XCB_CONFIG_WINDOW_Y
,
362 globalconf
.systray
.parent
= w
;
364 if(globalconf
.embedded
.len
!= 0)
366 systray_update(base_size
, horiz
, revers
);
367 xcb_map_window(globalconf
.connection
,
368 globalconf
.systray
.window
);
371 xcb_unmap_window(globalconf
.connection
,
372 globalconf
.systray
.window
);
375 lua_pushnumber(L
, globalconf
.embedded
.len
);
376 luaA_object_push(L
, globalconf
.systray
.parent
);
380 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:textwidth=80