change codename
[awesome.git] / systray.c
blob92b7d39f5c117a24198cf138062f39e086fda8eb
1 /*
2 * systray.c - systray handling
4 * Copyright © 2008 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 "window.h"
29 #include "widget.h"
30 #include "wibox.h"
31 #include "common/array.h"
32 #include "common/atoms.h"
33 #include "common/xutil.h"
35 #define SYSTEM_TRAY_REQUEST_DOCK 0 /* Begin icon docking */
37 /** Initialize systray information in X.
38 * \param phys_screen Physical screen.
40 void
41 systray_init(int phys_screen)
43 xcb_screen_t *xscreen = xutil_screen_get(globalconf.connection, phys_screen);
45 globalconf.screens.tab[phys_screen].systray.window = xcb_generate_id(globalconf.connection);
46 xcb_create_window(globalconf.connection, xscreen->root_depth,
47 globalconf.screens.tab[phys_screen].systray.window,
48 xscreen->root,
49 -1, -1, 1, 1, 0,
50 XCB_COPY_FROM_PARENT, xscreen->root_visual, 0, NULL);
54 /** Refresh all systrays registrations per physical screen
56 void
57 systray_refresh()
59 int nscreen = xcb_setup_roots_length(xcb_get_setup(globalconf.connection));
61 for(int phys_screen = 0; phys_screen < nscreen; phys_screen++)
63 bool has_systray = false;
64 foreach(w, globalconf.wiboxes)
65 if(phys_screen == (*w)->ctx.phys_screen && (*w)->has_systray)
66 /* Can't use "break" with foreach() :( */
67 has_systray = true;
69 if(has_systray)
70 systray_register(phys_screen);
71 else
72 systray_cleanup(phys_screen);
77 /** Register systray in X.
78 * \param phys_screen Physical screen.
80 void
81 systray_register(int phys_screen)
83 xcb_client_message_event_t ev;
84 xcb_screen_t *xscreen = xutil_screen_get(globalconf.connection, phys_screen);
85 char *atom_name;
86 xcb_intern_atom_cookie_t atom_systray_q;
87 xcb_intern_atom_reply_t *atom_systray_r;
88 xcb_atom_t atom_systray;
90 /* Set registered even if it fails to don't try again unless forced */
91 if(globalconf.screens.tab[phys_screen].systray.registered)
92 return;
93 globalconf.screens.tab[phys_screen].systray.registered = true;
95 /* Send requests */
96 if(!(atom_name = xcb_atom_name_by_screen("_NET_SYSTEM_TRAY", phys_screen)))
98 warn("error getting systray atom");
99 return;
102 atom_systray_q = xcb_intern_atom_unchecked(globalconf.connection, false,
103 a_strlen(atom_name), atom_name);
105 p_delete(&atom_name);
107 /* Fill event */
108 p_clear(&ev, 1);
109 ev.response_type = XCB_CLIENT_MESSAGE;
110 ev.window = xscreen->root;
111 ev.format = 32;
112 ev.type = MANAGER;
113 ev.data.data32[0] = XCB_CURRENT_TIME;
114 ev.data.data32[2] = globalconf.screens.tab[phys_screen].systray.window;
115 ev.data.data32[3] = ev.data.data32[4] = 0;
117 if(!(atom_systray_r = xcb_intern_atom_reply(globalconf.connection, atom_systray_q, NULL)))
119 warn("error getting systray atom");
120 return;
123 ev.data.data32[1] = atom_systray = atom_systray_r->atom;
125 p_delete(&atom_systray_r);
127 xcb_set_selection_owner(globalconf.connection,
128 globalconf.screens.tab[phys_screen].systray.window,
129 atom_systray,
130 XCB_CURRENT_TIME);
132 xcb_send_event(globalconf.connection, false, xscreen->root, 0xFFFFFF, (char *) &ev);
135 /** Remove systray information in X.
136 * \param phys_screen Physical screen.
138 void
139 systray_cleanup(int phys_screen)
141 xcb_intern_atom_reply_t *atom_systray_r;
142 char *atom_name;
144 if(!globalconf.screens.tab[phys_screen].systray.registered)
145 return;
146 globalconf.screens.tab[phys_screen].systray.registered = false;
148 if(!(atom_name = xcb_atom_name_by_screen("_NET_SYSTEM_TRAY", phys_screen))
149 || !(atom_systray_r = xcb_intern_atom_reply(globalconf.connection,
150 xcb_intern_atom_unchecked(globalconf.connection,
151 false,
152 a_strlen(atom_name),
153 atom_name),
154 NULL)))
156 warn("error getting systray atom");
157 p_delete(&atom_name);
158 return;
161 p_delete(&atom_name);
163 xcb_set_selection_owner(globalconf.connection,
164 XCB_NONE,
165 atom_systray_r->atom,
166 XCB_CURRENT_TIME);
168 p_delete(&atom_systray_r);
171 /** Handle a systray request.
172 * \param embed_win The window to embed.
173 * \param phys_screen The physical monitor to display on.
174 * \param info The embedding info
175 * \return 0 on no error.
178 systray_request_handle(xcb_window_t embed_win, int phys_screen, xembed_info_t *info)
180 xembed_window_t em;
181 xcb_get_property_cookie_t em_cookie;
182 const uint32_t select_input_val[] =
184 XCB_EVENT_MASK_STRUCTURE_NOTIFY
185 | XCB_EVENT_MASK_PROPERTY_CHANGE
186 | XCB_EVENT_MASK_ENTER_WINDOW
189 /* check if not already trayed */
190 if(xembed_getbywin(&globalconf.embedded, embed_win))
191 return -1;
193 p_clear(&em_cookie, 1);
195 if(!info)
196 em_cookie = xembed_info_get_unchecked(globalconf.connection, embed_win);
198 xcb_change_window_attributes(globalconf.connection, embed_win, XCB_CW_EVENT_MASK,
199 select_input_val);
200 window_state_set(embed_win, XCB_ICCCM_WM_STATE_WITHDRAWN);
202 /* we grab the window, but also make sure it's automatically reparented back
203 * to the root window if we should die.
205 xcb_change_save_set(globalconf.connection, XCB_SET_MODE_INSERT, embed_win);
206 xcb_reparent_window(globalconf.connection, embed_win,
207 globalconf.screens.tab[phys_screen].systray.window,
208 0, 0);
210 em.win = embed_win;
211 em.phys_screen = phys_screen;
213 if(info)
214 em.info = *info;
215 else
216 xembed_info_get_reply(globalconf.connection, em_cookie, &em.info);
218 xembed_embedded_notify(globalconf.connection, em.win,
219 globalconf.screens.tab[phys_screen].systray.window,
220 MIN(XEMBED_VERSION, em.info.version));
222 xembed_window_array_append(&globalconf.embedded, em);
224 widget_invalidate_bytype(widget_systray);
226 return 0;
229 /** Handle systray message.
230 * \param ev The event.
231 * \return 0 on no error.
234 systray_process_client_message(xcb_client_message_event_t *ev)
236 int screen_nbr = 0, ret = 0;
237 xcb_get_geometry_cookie_t geom_c;
238 xcb_get_geometry_reply_t *geom_r;
239 xcb_screen_iterator_t iter;
241 switch(ev->data.data32[1])
243 case SYSTEM_TRAY_REQUEST_DOCK:
244 geom_c = xcb_get_geometry_unchecked(globalconf.connection, ev->window);
246 if(!(geom_r = xcb_get_geometry_reply(globalconf.connection, geom_c, NULL)))
247 return -1;
249 for(iter = xcb_setup_roots_iterator(xcb_get_setup(globalconf.connection)), screen_nbr = 0;
250 iter.rem && iter.data->root != geom_r->root; xcb_screen_next (&iter), ++screen_nbr);
252 p_delete(&geom_r);
254 ret = systray_request_handle(ev->data.data32[2], screen_nbr, NULL);
255 break;
258 return ret;
261 /** Check if a window is a KDE tray.
262 * \param w The window to check.
263 * \return True if it is, false otherwise.
265 bool
266 systray_iskdedockapp(xcb_window_t w)
268 xcb_get_property_cookie_t kde_check_q;
269 xcb_get_property_reply_t *kde_check;
270 bool ret;
272 /* Check if that is a KDE tray because it does not respect fdo standards,
273 * thanks KDE. */
274 kde_check_q = xcb_get_property_unchecked(globalconf.connection, false, w,
275 _KDE_NET_WM_SYSTEM_TRAY_WINDOW_FOR,
276 XCB_ATOM_WINDOW, 0, 1);
278 kde_check = xcb_get_property_reply(globalconf.connection, kde_check_q, NULL);
280 /* it's a KDE systray ?*/
281 ret = (kde_check && kde_check->value_len);
283 p_delete(&kde_check);
285 return ret;
288 /** Handle xembed client message.
289 * \param ev The event.
290 * \return 0 on no error.
293 xembed_process_client_message(xcb_client_message_event_t *ev)
295 switch(ev->data.data32[1])
297 case XEMBED_REQUEST_FOCUS:
298 xembed_focus_in(globalconf.connection, ev->window, XEMBED_FOCUS_CURRENT);
299 break;
301 return 0;
304 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80