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>
29 #include "objects/wibox.h"
30 #include "common/array.h"
31 #include "common/atoms.h"
32 #include "common/xutil.h"
34 #define SYSTEM_TRAY_REQUEST_DOCK 0 /* Begin icon docking */
36 /** Initialize systray information in X.
41 xcb_screen_t
*xscreen
= globalconf
.screen
;
43 globalconf
.systray
.window
= xcb_generate_id(globalconf
.connection
);
44 xcb_create_window(globalconf
.connection
, xscreen
->root_depth
,
45 globalconf
.systray
.window
,
48 XCB_COPY_FROM_PARENT
, xscreen
->root_visual
, 0, NULL
);
52 /** Refresh all systrays registrations per physical screen
59 bool has_systray
= false;
60 foreach(w
, globalconf
.wiboxes
)
62 /* Can't use "break" with foreach() :( */
73 /** Register systray in X.
76 systray_register(void)
78 xcb_client_message_event_t ev
;
79 xcb_screen_t
*xscreen
= globalconf
.screen
;
81 xcb_intern_atom_cookie_t atom_systray_q
;
82 xcb_intern_atom_reply_t
*atom_systray_r
;
83 xcb_atom_t atom_systray
;
85 /* Set registered even if it fails to don't try again unless forced */
86 if(globalconf
.systray
.registered
)
88 globalconf
.systray
.registered
= true;
91 if(!(atom_name
= xcb_atom_name_by_screen("_NET_SYSTEM_TRAY", globalconf
.default_screen
)))
93 warn("error getting systray atom");
97 atom_systray_q
= xcb_intern_atom_unchecked(globalconf
.connection
, false,
98 a_strlen(atom_name
), atom_name
);
100 p_delete(&atom_name
);
104 ev
.response_type
= XCB_CLIENT_MESSAGE
;
105 ev
.window
= xscreen
->root
;
108 ev
.data
.data32
[0] = XCB_CURRENT_TIME
;
109 ev
.data
.data32
[2] = globalconf
.systray
.window
;
110 ev
.data
.data32
[3] = ev
.data
.data32
[4] = 0;
112 if(!(atom_systray_r
= xcb_intern_atom_reply(globalconf
.connection
, atom_systray_q
, NULL
)))
114 warn("error getting systray atom");
118 ev
.data
.data32
[1] = atom_systray
= atom_systray_r
->atom
;
120 p_delete(&atom_systray_r
);
122 xcb_set_selection_owner(globalconf
.connection
,
123 globalconf
.systray
.window
,
127 xcb_send_event(globalconf
.connection
, false, xscreen
->root
, 0xFFFFFF, (char *) &ev
);
130 /** Remove systray information in X.
133 systray_cleanup(void)
135 xcb_intern_atom_reply_t
*atom_systray_r
;
138 if(!globalconf
.systray
.registered
)
140 globalconf
.systray
.registered
= false;
142 if(!(atom_name
= xcb_atom_name_by_screen("_NET_SYSTEM_TRAY", globalconf
.default_screen
))
143 || !(atom_systray_r
= xcb_intern_atom_reply(globalconf
.connection
,
144 xcb_intern_atom_unchecked(globalconf
.connection
,
150 warn("error getting systray atom");
151 p_delete(&atom_name
);
155 p_delete(&atom_name
);
157 xcb_set_selection_owner(globalconf
.connection
,
159 atom_systray_r
->atom
,
162 p_delete(&atom_systray_r
);
165 /** Handle a systray request.
166 * \param embed_win The window to embed.
167 * \param info The embedding info
168 * \return 0 on no error.
171 systray_request_handle(xcb_window_t embed_win
, xembed_info_t
*info
)
174 xcb_get_property_cookie_t em_cookie
;
175 const uint32_t select_input_val
[] =
177 XCB_EVENT_MASK_STRUCTURE_NOTIFY
178 | XCB_EVENT_MASK_PROPERTY_CHANGE
179 | XCB_EVENT_MASK_ENTER_WINDOW
182 /* check if not already trayed */
183 if(xembed_getbywin(&globalconf
.embedded
, embed_win
))
186 p_clear(&em_cookie
, 1);
189 em_cookie
= xembed_info_get_unchecked(globalconf
.connection
, embed_win
);
191 xcb_change_window_attributes(globalconf
.connection
, embed_win
, XCB_CW_EVENT_MASK
,
193 xwindow_set_state(embed_win
, XCB_WM_STATE_WITHDRAWN
);
195 /* we grab the window, but also make sure it's automatically reparented back
196 * to the root window if we should die.
198 xcb_change_save_set(globalconf
.connection
, XCB_SET_MODE_INSERT
, embed_win
);
199 xcb_reparent_window(globalconf
.connection
, embed_win
,
200 globalconf
.systray
.window
,
208 xembed_info_get_reply(globalconf
.connection
, em_cookie
, &em
.info
);
210 xembed_embedded_notify(globalconf
.connection
, em
.win
,
211 globalconf
.systray
.window
,
212 MIN(XEMBED_VERSION
, em
.info
.version
));
214 xembed_window_array_append(&globalconf
.embedded
, em
);
219 /** Handle systray message.
220 * \param ev The event.
221 * \return 0 on no error.
224 systray_process_client_message(xcb_client_message_event_t
*ev
)
227 xcb_get_geometry_cookie_t geom_c
;
228 xcb_get_geometry_reply_t
*geom_r
;
230 switch(ev
->data
.data32
[1])
232 case SYSTEM_TRAY_REQUEST_DOCK
:
233 geom_c
= xcb_get_geometry_unchecked(globalconf
.connection
, ev
->window
);
235 if(!(geom_r
= xcb_get_geometry_reply(globalconf
.connection
, geom_c
, NULL
)))
238 if(globalconf
.screen
->root
== geom_r
->root
)
239 ret
= systray_request_handle(ev
->data
.data32
[2], NULL
);
248 /** Check if a window is a KDE tray.
249 * \param w The window to check.
250 * \return True if it is, false otherwise.
253 systray_iskdedockapp(xcb_window_t w
)
255 xcb_get_property_cookie_t kde_check_q
;
256 xcb_get_property_reply_t
*kde_check
;
259 /* Check if that is a KDE tray because it does not respect fdo standards,
261 kde_check_q
= xcb_get_property_unchecked(globalconf
.connection
, false, w
,
262 _KDE_NET_WM_SYSTEM_TRAY_WINDOW_FOR
,
263 XCB_ATOM_WINDOW
, 0, 1);
265 kde_check
= xcb_get_property_reply(globalconf
.connection
, kde_check_q
, NULL
);
267 /* it's a KDE systray ?*/
268 ret
= (kde_check
&& kde_check
->value_len
);
270 p_delete(&kde_check
);
275 /** Handle xembed client message.
276 * \param ev The event.
277 * \return 0 on no error.
280 xembed_process_client_message(xcb_client_message_event_t
*ev
)
282 switch(ev
->data
.data32
[1])
284 case XEMBED_REQUEST_FOCUS
:
285 xembed_focus_in(globalconf
.connection
, ev
->window
, XEMBED_FOCUS_CURRENT
);
291 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80