Widgets: Remove
[awesome.git] / systray.c
blob66ad6d914bd238913cdc7b387b59a1310c08b1a3
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 <xcb/xcb.h>
23 #include <xcb/xcb_icccm.h>
24 #include <xcb/xcb_atom.h>
26 #include "screen.h"
27 #include "systray.h"
28 #include "xwindow.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.
38 void
39 systray_init(void)
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,
46 xscreen->root,
47 -1, -1, 1, 1, 0,
48 XCB_COPY_FROM_PARENT, xscreen->root_visual, 0, NULL);
52 /** Refresh all systrays registrations per physical screen
54 void
55 systray_refresh(void)
57 #warning
58 #if 0
59 bool has_systray = false;
60 foreach(w, globalconf.wiboxes)
61 if((*w)->has_systray)
62 /* Can't use "break" with foreach() :( */
63 has_systray = true;
65 if(has_systray)
66 systray_register();
67 else
68 systray_cleanup();
69 #endif
73 /** Register systray in X.
75 void
76 systray_register(void)
78 xcb_client_message_event_t ev;
79 xcb_screen_t *xscreen = globalconf.screen;
80 char *atom_name;
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)
87 return;
88 globalconf.systray.registered = true;
90 /* Send requests */
91 if(!(atom_name = xcb_atom_name_by_screen("_NET_SYSTEM_TRAY", globalconf.default_screen)))
93 warn("error getting systray atom");
94 return;
97 atom_systray_q = xcb_intern_atom_unchecked(globalconf.connection, false,
98 a_strlen(atom_name), atom_name);
100 p_delete(&atom_name);
102 /* Fill event */
103 p_clear(&ev, 1);
104 ev.response_type = XCB_CLIENT_MESSAGE;
105 ev.window = xscreen->root;
106 ev.format = 32;
107 ev.type = MANAGER;
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");
115 return;
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,
124 atom_systray,
125 XCB_CURRENT_TIME);
127 xcb_send_event(globalconf.connection, false, xscreen->root, 0xFFFFFF, (char *) &ev);
130 /** Remove systray information in X.
132 void
133 systray_cleanup(void)
135 xcb_intern_atom_reply_t *atom_systray_r;
136 char *atom_name;
138 if(!globalconf.systray.registered)
139 return;
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,
145 false,
146 a_strlen(atom_name),
147 atom_name),
148 NULL)))
150 warn("error getting systray atom");
151 p_delete(&atom_name);
152 return;
155 p_delete(&atom_name);
157 xcb_set_selection_owner(globalconf.connection,
158 XCB_NONE,
159 atom_systray_r->atom,
160 XCB_CURRENT_TIME);
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)
173 xembed_window_t em;
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))
184 return -1;
186 p_clear(&em_cookie, 1);
188 if(!info)
189 em_cookie = xembed_info_get_unchecked(globalconf.connection, embed_win);
191 xcb_change_window_attributes(globalconf.connection, embed_win, XCB_CW_EVENT_MASK,
192 select_input_val);
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,
201 0, 0);
203 em.win = embed_win;
205 if(info)
206 em.info = *info;
207 else
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);
216 return 0;
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)
226 int ret = 0;
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)))
236 return -1;
238 if(globalconf.screen->root == geom_r->root)
239 ret = systray_request_handle(ev->data.data32[2], NULL);
241 p_delete(&geom_r);
242 break;
245 return ret;
248 /** Check if a window is a KDE tray.
249 * \param w The window to check.
250 * \return True if it is, false otherwise.
252 bool
253 systray_iskdedockapp(xcb_window_t w)
255 xcb_get_property_cookie_t kde_check_q;
256 xcb_get_property_reply_t *kde_check;
257 bool ret;
259 /* Check if that is a KDE tray because it does not respect fdo standards,
260 * thanks KDE. */
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);
272 return ret;
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);
286 break;
288 return 0;
291 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80