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 <xcb/xcb_icccm.h>
24 #include <xcb/xcb_atom.h>
30 #include "common/array.h"
31 #include "common/atoms.h"
32 #include "common/xutil.h"
33 #include "objects/drawin.h"
35 #define SYSTEM_TRAY_REQUEST_DOCK 0 /* Begin icon docking */
37 /** Initialize systray information in X.
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
,
53 /** Register systray in X.
56 systray_register(void)
58 xcb_client_message_event_t ev
;
59 xcb_screen_t
*xscreen
= globalconf
.screen
;
61 xcb_intern_atom_cookie_t atom_systray_q
;
62 xcb_intern_atom_reply_t
*atom_systray_r
;
63 xcb_atom_t atom_systray
;
66 if(!(atom_name
= xcb_atom_name_by_screen("_NET_SYSTEM_TRAY", globalconf
.default_screen
)))
68 warn("error getting systray atom");
72 atom_systray_q
= xcb_intern_atom_unchecked(globalconf
.connection
, false,
73 a_strlen(atom_name
), atom_name
);
79 ev
.response_type
= XCB_CLIENT_MESSAGE
;
80 ev
.window
= xscreen
->root
;
83 ev
.data
.data32
[0] = XCB_CURRENT_TIME
;
84 ev
.data
.data32
[2] = globalconf
.systray
.window
;
85 ev
.data
.data32
[3] = ev
.data
.data32
[4] = 0;
87 if(!(atom_systray_r
= xcb_intern_atom_reply(globalconf
.connection
, atom_systray_q
, NULL
)))
89 warn("error getting systray atom");
93 ev
.data
.data32
[1] = atom_systray
= atom_systray_r
->atom
;
95 p_delete(&atom_systray_r
);
97 xcb_set_selection_owner(globalconf
.connection
,
98 globalconf
.systray
.window
,
102 xcb_send_event(globalconf
.connection
, false, xscreen
->root
, 0xFFFFFF, (char *) &ev
);
105 /** Remove systray information in X.
108 systray_cleanup(void)
110 xcb_intern_atom_reply_t
*atom_systray_r
;
113 if(!(atom_name
= xcb_atom_name_by_screen("_NET_SYSTEM_TRAY", globalconf
.default_screen
))
114 || !(atom_systray_r
= xcb_intern_atom_reply(globalconf
.connection
,
115 xcb_intern_atom_unchecked(globalconf
.connection
,
121 warn("error getting systray atom");
122 p_delete(&atom_name
);
126 p_delete(&atom_name
);
128 xcb_set_selection_owner(globalconf
.connection
,
130 atom_systray_r
->atom
,
133 p_delete(&atom_systray_r
);
135 xcb_unmap_window(globalconf
.connection
,
136 globalconf
.systray
.window
);
139 /** Handle a systray request.
140 * \param embed_win The window to embed.
141 * \param info The embedding info
142 * \return 0 on no error.
145 systray_request_handle(xcb_window_t embed_win
, xembed_info_t
*info
)
148 xcb_get_property_cookie_t em_cookie
;
149 const uint32_t select_input_val
[] =
151 XCB_EVENT_MASK_STRUCTURE_NOTIFY
152 | XCB_EVENT_MASK_PROPERTY_CHANGE
153 | XCB_EVENT_MASK_ENTER_WINDOW
156 /* check if not already trayed */
157 if(xembed_getbywin(&globalconf
.embedded
, embed_win
))
160 p_clear(&em_cookie
, 1);
163 em_cookie
= xembed_info_get_unchecked(globalconf
.connection
, embed_win
);
165 xcb_change_window_attributes(globalconf
.connection
, embed_win
, XCB_CW_EVENT_MASK
,
167 xwindow_set_state(embed_win
, XCB_ICCCM_WM_STATE_WITHDRAWN
);
169 /* we grab the window, but also make sure it's automatically reparented back
170 * to the root window if we should die.
172 xcb_change_save_set(globalconf
.connection
, XCB_SET_MODE_INSERT
, embed_win
);
173 xcb_reparent_window(globalconf
.connection
, embed_win
,
174 globalconf
.systray
.window
,
182 xembed_info_get_reply(globalconf
.connection
, em_cookie
, &em
.info
);
184 xembed_embedded_notify(globalconf
.connection
, em
.win
,
185 globalconf
.systray
.window
,
186 MIN(XEMBED_VERSION
, em
.info
.version
));
188 xembed_window_array_append(&globalconf
.embedded
, em
);
189 luaA_systray_invalidate();
194 /** Handle systray message.
195 * \param ev The event.
196 * \return 0 on no error.
199 systray_process_client_message(xcb_client_message_event_t
*ev
)
202 xcb_get_geometry_cookie_t geom_c
;
203 xcb_get_geometry_reply_t
*geom_r
;
205 switch(ev
->data
.data32
[1])
207 case SYSTEM_TRAY_REQUEST_DOCK
:
208 geom_c
= xcb_get_geometry_unchecked(globalconf
.connection
, ev
->window
);
210 if(!(geom_r
= xcb_get_geometry_reply(globalconf
.connection
, geom_c
, NULL
)))
213 if(globalconf
.screen
->root
== geom_r
->root
)
214 ret
= systray_request_handle(ev
->data
.data32
[2], NULL
);
223 /** Check if a window is a KDE tray.
224 * \param w The window to check.
225 * \return True if it is, false otherwise.
228 systray_iskdedockapp(xcb_window_t w
)
230 xcb_get_property_cookie_t kde_check_q
;
231 xcb_get_property_reply_t
*kde_check
;
234 /* Check if that is a KDE tray because it does not respect fdo standards,
236 kde_check_q
= xcb_get_property_unchecked(globalconf
.connection
, false, w
,
237 _KDE_NET_WM_SYSTEM_TRAY_WINDOW_FOR
,
238 XCB_ATOM_WINDOW
, 0, 1);
240 kde_check
= xcb_get_property_reply(globalconf
.connection
, kde_check_q
, NULL
);
242 /* it's a KDE systray ?*/
243 ret
= (kde_check
&& kde_check
->value_len
);
245 p_delete(&kde_check
);
250 /** Handle xembed client message.
251 * \param ev The event.
252 * \return 0 on no error.
255 xembed_process_client_message(xcb_client_message_event_t
*ev
)
257 switch(ev
->data
.data32
[1])
259 case XEMBED_REQUEST_FOCUS
:
260 xembed_focus_in(globalconf
.connection
, ev
->window
, XEMBED_FOCUS_CURRENT
);
266 /** Inform lua that the systray needs to be updated.
269 luaA_systray_invalidate(void)
271 signal_object_emit(globalconf
.L
, &global_signals
, "systray::update", 0);
275 systray_update(int base_size
, bool horizontal
)
280 /* Give the systray window the correct size */
281 uint32_t config_vals
[4] = { base_size
, base_size
, 0, 0 };
283 config_vals
[0] = base_size
* globalconf
.embedded
.len
;
285 config_vals
[1] = base_size
* globalconf
.embedded
.len
;
286 xcb_configure_window(globalconf
.connection
,
287 globalconf
.systray
.window
,
288 XCB_CONFIG_WINDOW_WIDTH
| XCB_CONFIG_WINDOW_HEIGHT
,
291 /* Now resize each embedded window */
292 config_vals
[0] = config_vals
[1] = 0;
293 config_vals
[2] = config_vals
[3] = base_size
;
294 for(int i
= 0; i
< globalconf
.embedded
.len
; i
++)
296 xembed_window_t
*em
= &globalconf
.embedded
.tab
[i
];
297 xcb_configure_window(globalconf
.connection
, em
->win
,
298 XCB_CONFIG_WINDOW_X
| XCB_CONFIG_WINDOW_Y
| XCB_CONFIG_WINDOW_WIDTH
| XCB_CONFIG_WINDOW_HEIGHT
,
300 xcb_map_window(globalconf
.connection
, em
->win
);
302 config_vals
[0] += base_size
;
304 config_vals
[1] += base_size
;
308 /** Update the systray
309 * \param L The Lua VM state.
310 * \return The number of elements pushed on stack.
312 * \lparam The drawin to display the systray in.
313 * \lparam x X position for the systray.
314 * \lparam y Y position for the systray.
315 * \lparam base_size The size (width and height) each systray item gets.
316 * \lparam horiz If true, the systray is horizontal, else vertical
319 luaA_systray(lua_State
*L
)
321 if(lua_gettop(L
) != 0)
324 drawin_t
*w
= luaA_checkudata(L
, 1, &drawin_class
);
325 int x
= luaL_checknumber(L
, 2);
326 int y
= luaL_checknumber(L
, 3);
327 int base_size
= luaL_checknumber(L
, 4);
328 bool horiz
= lua_toboolean(L
, 5);
329 const char *bg
= luaL_checklstring(L
, 6, &bg_len
);
332 if(color_init_reply(color_init_unchecked(&bg_color
, bg
, bg_len
)))
334 uint32_t config_back
[] = { bg_color
.pixel
};
335 xcb_change_window_attributes(globalconf
.connection
,
336 globalconf
.systray
.window
,
337 XCB_CW_BACK_PIXEL
, config_back
);
340 if(globalconf
.systray
.parent
== NULL
)
343 if(globalconf
.systray
.parent
!= w
)
344 xcb_reparent_window(globalconf
.connection
,
345 globalconf
.systray
.window
,
350 uint32_t config_vals
[2] = { x
, y
};
351 xcb_configure_window(globalconf
.connection
,
352 globalconf
.systray
.window
,
353 XCB_CONFIG_WINDOW_X
| XCB_CONFIG_WINDOW_Y
,
357 globalconf
.systray
.parent
= w
;
359 if(globalconf
.embedded
.len
!= 0)
361 systray_update(base_size
, horiz
);
362 xcb_map_window(globalconf
.connection
,
363 globalconf
.systray
.window
);
366 xcb_unmap_window(globalconf
.connection
,
367 globalconf
.systray
.window
);
370 lua_pushnumber(L
, globalconf
.embedded
.len
);
371 luaA_object_push(L
, globalconf
.systray
.parent
);
375 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:textwidth=80