awful.mouse: load finder
[awesome.git] / systray.c
blob1f573eef84ab5fc68112d52646c6f353e6a2b7c4
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 "common/atoms.h"
31 #include "common/xutil.h"
33 #define SYSTEM_TRAY_REQUEST_DOCK 0 /* Begin icon docking */
35 /** Initialize systray information in X.
36 * \param phys_screen Physical screen.
38 void
39 systray_init(int phys_screen)
41 xcb_client_message_event_t ev;
42 xcb_screen_t *xscreen = xutil_screen_get(globalconf.connection, phys_screen);
43 char *atom_name;
44 xcb_intern_atom_cookie_t atom_systray_q;
45 xcb_intern_atom_reply_t *atom_systray_r;
46 xcb_atom_t atom_systray;
48 /* Send requests */
49 if(!(atom_name = xcb_atom_name_by_screen("_NET_SYSTEM_TRAY", phys_screen)))
51 warn("error getting systray atom");
52 return;
55 atom_systray_q = xcb_intern_atom_unchecked(globalconf.connection, false,
56 a_strlen(atom_name), atom_name);
58 p_delete(&atom_name);
60 globalconf.screens.tab[phys_screen].systray.window = xcb_generate_id(globalconf.connection);
61 xcb_create_window(globalconf.connection, xscreen->root_depth,
62 globalconf.screens.tab[phys_screen].systray.window,
63 xscreen->root,
64 -1, -1, 1, 1, 0,
65 XCB_COPY_FROM_PARENT, xscreen->root_visual, 0, NULL);
67 /* Fill event */
68 p_clear(&ev, 1);
69 ev.response_type = XCB_CLIENT_MESSAGE;
70 ev.window = xscreen->root;
71 ev.format = 32;
72 ev.type = MANAGER;
73 ev.data.data32[0] = XCB_CURRENT_TIME;
74 ev.data.data32[2] = globalconf.screens.tab[phys_screen].systray.window;
75 ev.data.data32[3] = ev.data.data32[4] = 0;
77 if(!(atom_systray_r = xcb_intern_atom_reply(globalconf.connection, atom_systray_q, NULL)))
79 warn("error getting systray atom");
80 return;
83 ev.data.data32[1] = atom_systray = atom_systray_r->atom;
85 p_delete(&atom_systray_r);
87 xcb_set_selection_owner(globalconf.connection,
88 globalconf.screens.tab[phys_screen].systray.window,
89 atom_systray,
90 XCB_CURRENT_TIME);
92 xcb_send_event(globalconf.connection, false, xscreen->root, 0xFFFFFF, (char *) &ev);
95 /** Remove systray information in X.
96 * \param phys_screen Physical screen.
98 void
99 systray_cleanup(int phys_screen)
101 xcb_intern_atom_reply_t *atom_systray_r;
102 char *atom_name;
104 if(!(atom_name = xcb_atom_name_by_screen("_NET_SYSTEM_TRAY", phys_screen))
105 || !(atom_systray_r = xcb_intern_atom_reply(globalconf.connection,
106 xcb_intern_atom_unchecked(globalconf.connection,
107 false,
108 a_strlen(atom_name),
109 atom_name),
110 NULL)))
112 warn("error getting systray atom");
113 p_delete(&atom_name);
114 return;
117 p_delete(&atom_name);
119 xcb_set_selection_owner(globalconf.connection,
120 XCB_NONE,
121 atom_systray_r->atom,
122 XCB_CURRENT_TIME);
124 p_delete(&atom_systray_r);
127 /** Handle a systray request.
128 * \param embed_win The window to embed.
129 * \param phys_screen The physical monitor to display on.
130 * \param info The embedding info
131 * \return 0 on no error.
134 systray_request_handle(xcb_window_t embed_win, int phys_screen, xembed_info_t *info)
136 xembed_window_t em;
137 xcb_get_property_cookie_t em_cookie;
138 const uint32_t select_input_val[] =
140 XCB_EVENT_MASK_STRUCTURE_NOTIFY
141 | XCB_EVENT_MASK_PROPERTY_CHANGE
142 | XCB_EVENT_MASK_ENTER_WINDOW
145 /* check if not already trayed */
146 if(xembed_getbywin(&globalconf.embedded, embed_win))
147 return -1;
149 p_clear(&em_cookie, 1);
151 if(!info)
152 em_cookie = xembed_info_get_unchecked(globalconf.connection, embed_win);
154 xcb_change_window_attributes(globalconf.connection, embed_win, XCB_CW_EVENT_MASK,
155 select_input_val);
156 window_state_set(embed_win, XCB_WM_STATE_WITHDRAWN);
158 xcb_reparent_window(globalconf.connection, embed_win,
159 globalconf.screens.tab[phys_screen].systray.window,
160 0, 0);
162 em.win = embed_win;
163 em.phys_screen = phys_screen;
165 if(info)
166 em.info = *info;
167 else
168 xembed_info_get_reply(globalconf.connection, em_cookie, &em.info);
170 xembed_embedded_notify(globalconf.connection, em.win,
171 globalconf.screens.tab[phys_screen].systray.window,
172 MIN(XEMBED_VERSION, em.info.version));
174 xembed_window_array_append(&globalconf.embedded, em);
176 widget_invalidate_bytype(widget_systray);
178 return 0;
181 /** Handle systray message.
182 * \param ev The event.
183 * \return 0 on no error.
186 systray_process_client_message(xcb_client_message_event_t *ev)
188 int screen_nbr = 0, ret = 0;
189 xcb_get_geometry_cookie_t geom_c;
190 xcb_get_geometry_reply_t *geom_r;
191 xcb_screen_iterator_t iter;
193 switch(ev->data.data32[1])
195 case SYSTEM_TRAY_REQUEST_DOCK:
196 geom_c = xcb_get_geometry_unchecked(globalconf.connection, ev->window);
198 if(!(geom_r = xcb_get_geometry_reply(globalconf.connection, geom_c, NULL)))
199 return -1;
201 for(iter = xcb_setup_roots_iterator(xcb_get_setup(globalconf.connection)), screen_nbr = 0;
202 iter.rem && iter.data->root != geom_r->root; xcb_screen_next (&iter), ++screen_nbr);
204 p_delete(&geom_r);
206 ret = systray_request_handle(ev->data.data32[2], screen_nbr, NULL);
207 break;
210 return ret;
213 /** Check if a window is a KDE tray.
214 * \param w The window to check.
215 * \return True if it is, false otherwise.
217 bool
218 systray_iskdedockapp(xcb_window_t w)
220 xcb_get_property_cookie_t kde_check_q;
221 xcb_get_property_reply_t *kde_check;
222 bool ret;
224 /* Check if that is a KDE tray because it does not respect fdo standards,
225 * thanks KDE. */
226 kde_check_q = xcb_get_property_unchecked(globalconf.connection, false, w,
227 _KDE_NET_WM_SYSTEM_TRAY_WINDOW_FOR,
228 WINDOW, 0, 1);
230 kde_check = xcb_get_property_reply(globalconf.connection, kde_check_q, NULL);
232 /* it's a KDE systray ?*/
233 ret = (kde_check && kde_check->value_len);
235 p_delete(&kde_check);
237 return ret;
240 /** Handle xembed client message.
241 * \param ev The event.
242 * \return 0 on no error.
245 xembed_process_client_message(xcb_client_message_event_t *ev)
247 switch(ev->data.data32[1])
249 case XEMBED_REQUEST_FOCUS:
250 xembed_focus_in(globalconf.connection, ev->window, XEMBED_FOCUS_CURRENT);
251 break;
253 return 0;
256 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80