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_intern_atom_cookie_t atom_systray_q
;
40 xcb_intern_atom_reply_t
*atom_systray_r
;
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
,
49 XCB_COPY_FROM_PARENT
, xscreen
->root_visual
,
52 atom_name
= xcb_atom_name_by_screen("_NET_SYSTEM_TRAY", globalconf
.default_screen
);
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
);
61 atom_systray_r
= xcb_intern_atom_reply(globalconf
.connection
, atom_systray_q
, NULL
);
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.
72 systray_register(void)
74 xcb_client_message_event_t ev
;
75 xcb_screen_t
*xscreen
= globalconf
.screen
;
77 if(globalconf
.systray
.registered
)
80 globalconf
.systray
.registered
= true;
84 ev
.response_type
= XCB_CLIENT_MESSAGE
;
85 ev
.window
= xscreen
->root
;
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
,
98 xcb_send_event(globalconf
.connection
, false, xscreen
->root
, 0xFFFFFF, (char *) &ev
);
101 /** Remove systray information in X.
104 systray_cleanup(void)
106 if(!globalconf
.systray
.registered
)
109 globalconf
.systray
.registered
= false;
111 xcb_set_selection_owner(globalconf
.connection
,
113 globalconf
.systray
.atom
,
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
)
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
))
141 p_clear(&em_cookie
, 1);
144 em_cookie
= xembed_info_get_unchecked(globalconf
.connection
, embed_win
);
146 xcb_change_window_attributes(globalconf
.connection
, embed_win
, XCB_CW_EVENT_MASK
,
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
,
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();
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
)
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
)))
193 if(globalconf
.screen
->root
== geom_r
->root
)
194 ret
= systray_request_handle(ev
->data
.data32
[2], NULL
);
203 /** Check if a window is a KDE tray.
204 * \param w The window to check.
205 * \return True if it is, false otherwise.
208 systray_iskdedockapp(xcb_window_t w
)
210 xcb_get_property_cookie_t kde_check_q
;
211 xcb_get_property_reply_t
*kde_check
;
214 /* Check if that is a KDE tray because it does not respect fdo standards,
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
);
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
);
246 /** Inform lua that the systray needs to be updated.
249 luaA_systray_invalidate(void)
251 signal_object_emit(globalconf
.L
, &global_signals
, "systray::update", 0);
255 systray_update(int base_size
, bool horizontal
, bool reverse
, int spacing
)
260 /* Give the systray window the correct size */
261 uint32_t config_vals
[4] = { base_size
, base_size
, 0, 0 };
263 config_vals
[0] = base_size
* globalconf
.embedded
.len
+ spacing
* (globalconf
.embedded
.len
- 1);
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
,
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
++)
279 em
= &globalconf
.embedded
.tab
[(globalconf
.embedded
.len
- i
- 1)];
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
,
286 xcb_map_window(globalconf
.connection
, em
->win
);
288 config_vals
[0] += base_size
+ spacing
;
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.
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
)
312 if(lua_gettop(L
) != 0)
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);
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
,
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
,
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
);
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
);
365 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:textwidth=80