wibox: Add pseudo-transparency support
[awesome.git] / xwindow.c
blob9821c7576f296fd1278531fb204b3c65aa395972
1 /*
2 * xwindow.c - X window handling functions
4 * Copyright © 2007-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_atom.h>
25 #include "xwindow.h"
26 #include "objects/button.h"
27 #include "common/atoms.h"
29 /** Mask shorthands */
30 #define BUTTONMASK (XCB_EVENT_MASK_BUTTON_PRESS | XCB_EVENT_MASK_BUTTON_RELEASE)
32 /** Set client state (WM_STATE) property.
33 * \param win The window to set state.
34 * \param state The state to set.
36 void
37 xwindow_set_state(xcb_window_t win, uint32_t state)
39 uint32_t data[] = { state, XCB_NONE };
40 xcb_change_property(globalconf.connection, XCB_PROP_MODE_REPLACE, win,
41 WM_STATE, WM_STATE, 32, 2, data);
44 /** Send request to get a window state (WM_STATE).
45 * \param w A client window.
46 * \return The cookie associated with the request.
48 xcb_get_property_cookie_t
49 xwindow_get_state_unchecked(xcb_window_t w)
51 return xcb_get_property_unchecked(globalconf.connection, false, w, WM_STATE,
52 WM_STATE, 0L, 2L);
55 /** Get a window state (WM_STATE).
56 * \param cookie The cookie.
57 * \return The current state of the window, or 0 on error.
59 uint32_t
60 xwindow_get_state_reply(xcb_get_property_cookie_t cookie)
62 /* If no property is set, we just assume a sane default. */
63 uint32_t result = XCB_ICCCM_WM_STATE_NORMAL;
64 xcb_get_property_reply_t *prop_r;
66 if((prop_r = xcb_get_property_reply(globalconf.connection, cookie, NULL)))
68 if(xcb_get_property_value_length(prop_r))
69 result = *(uint32_t *) xcb_get_property_value(prop_r);
71 p_delete(&prop_r);
74 return result;
77 /** Configure a window with its new geometry and border size.
78 * \param win The X window id to configure.
79 * \param geometry The new window geometry.
80 * \param border The new border size.
82 void
83 xwindow_configure(xcb_window_t win, area_t geometry, int border)
85 xcb_configure_notify_event_t ce;
87 ce.response_type = XCB_CONFIGURE_NOTIFY;
88 ce.event = win;
89 ce.window = win;
90 ce.x = geometry.x;
91 ce.y = geometry.y;
92 ce.width = geometry.width;
93 ce.height = geometry.height;
94 ce.border_width = border;
95 ce.above_sibling = XCB_NONE;
96 ce.override_redirect = false;
97 xcb_send_event(globalconf.connection, false, win, XCB_EVENT_MASK_STRUCTURE_NOTIFY, (char *) &ce);
100 /** Grab or ungrab buttons on a window.
101 * \param win The window.
102 * \param buttons The buttons to grab.
104 void
105 xwindow_buttons_grab(xcb_window_t win, button_array_t *buttons)
107 if(win == XCB_NONE)
108 return;
110 /* Ungrab everything first */
111 xcb_ungrab_button(globalconf.connection, XCB_BUTTON_INDEX_ANY, win, XCB_BUTTON_MASK_ANY);
113 foreach(b, *buttons)
114 xcb_grab_button(globalconf.connection, false, win, BUTTONMASK,
115 XCB_GRAB_MODE_SYNC, XCB_GRAB_MODE_ASYNC, XCB_NONE, XCB_NONE,
116 (*b)->button, (*b)->modifiers);
119 /** Grab key on a window.
120 * \param win The window.
121 * \param k The key.
123 static void
124 xwindow_grabkey(xcb_window_t win, keyb_t *k)
126 if(k->keycode)
127 xcb_grab_key(globalconf.connection, true, win,
128 k->modifiers, k->keycode, XCB_GRAB_MODE_ASYNC, XCB_GRAB_MODE_ASYNC);
129 else if(k->keysym)
131 xcb_keycode_t *keycodes = xcb_key_symbols_get_keycode(globalconf.keysyms, k->keysym);
132 if(keycodes)
134 for(xcb_keycode_t *kc = keycodes; *kc; kc++)
135 xcb_grab_key(globalconf.connection, true, win,
136 k->modifiers, *kc, XCB_GRAB_MODE_ASYNC, XCB_GRAB_MODE_ASYNC);
137 p_delete(&keycodes);
142 void
143 xwindow_grabkeys(xcb_window_t win, key_array_t *keys)
145 /* Ungrab everything first */
146 xcb_ungrab_key(globalconf.connection, XCB_GRAB_ANY, win, XCB_BUTTON_MASK_ANY);
148 foreach(k, *keys)
149 xwindow_grabkey(win, *k);
152 /** Send a request for a window's opacity.
153 * \param win The window
154 * \return A cookie for xwindow_get_opacity_from_reply().
156 xcb_get_property_cookie_t xwindow_get_opacity_unchecked(xcb_window_t win)
158 return xcb_get_property_unchecked(globalconf.connection, false, win,
159 _NET_WM_WINDOW_OPACITY, XCB_ATOM_CARDINAL, 0L, 1L);
162 /** Get the opacity of a window.
163 * \param win The window.
164 * \return The opacity, between 0 and 1 or -1 or no opacity set.
166 double
167 xwindow_get_opacity(xcb_window_t win)
169 xcb_get_property_cookie_t prop_c =
170 xwindow_get_opacity_unchecked(win);
171 return xwindow_get_opacity_from_cookie(prop_c);
174 /** Get the opacity of a window.
175 * \param cookie A cookie for a reply to a get property request for _NET_WM_WINDOW_OPACITY.
176 * \return The opacity, between 0 and 1.
178 double
179 xwindow_get_opacity_from_cookie(xcb_get_property_cookie_t cookie)
181 xcb_get_property_reply_t *prop_r =
182 xcb_get_property_reply(globalconf.connection, cookie, NULL);
184 if(prop_r && prop_r->value_len && prop_r->format == 32)
186 uint32_t value = *(uint32_t *) xcb_get_property_value(prop_r);
187 p_delete(&prop_r);
188 return (double) value / (double) 0xffffffff;
190 p_delete(&prop_r);
192 return -1;
195 /** Set opacity of a window.
196 * \param win The window.
197 * \param opacity Opacity of the window, between 0 and 1.
199 void
200 xwindow_set_opacity(xcb_window_t win, double opacity)
202 if(win)
204 if(opacity >= 0 && opacity <= 1)
206 uint32_t real_opacity = opacity * 0xffffffff;
207 xcb_change_property(globalconf.connection, XCB_PROP_MODE_REPLACE, win,
208 _NET_WM_WINDOW_OPACITY, XCB_ATOM_CARDINAL, 32, 1L, &real_opacity);
210 else
211 xcb_delete_property(globalconf.connection, win, _NET_WM_WINDOW_OPACITY);
215 /** Send WM_TAKE_FOCUS client message to window
216 * \param win destination window
218 void
219 xwindow_takefocus(xcb_window_t win)
221 xcb_client_message_event_t ev;
223 /* Initialize all of event's fields first */
224 p_clear(&ev, 1);
226 ev.response_type = XCB_CLIENT_MESSAGE;
227 ev.window = win;
228 ev.format = 32;
229 ev.data.data32[1] = globalconf.timestamp;
230 ev.type = WM_PROTOCOLS;
231 ev.data.data32[0] = WM_TAKE_FOCUS;
233 xcb_send_event(globalconf.connection, false, win,
234 XCB_EVENT_MASK_NO_EVENT, (char *) &ev);
237 /** Set window cursor.
238 * \param w The window.
239 * \param c The cursor.
241 void
242 xwindow_set_cursor(xcb_window_t w, xcb_cursor_t c)
244 xcb_change_window_attributes(globalconf.connection, w, XCB_CW_CURSOR,
245 (const uint32_t[]) { c });
248 /** Set a window border color.
249 * \param w The window.
250 * \param color The color.
252 void
253 xwindow_set_border_color(xcb_window_t w, color_t *color)
255 if(w)
256 xcb_change_window_attributes(globalconf.connection, w, XCB_CW_BORDER_PIXEL, &color->pixel);
259 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:textwidth=80