change codename
[awesome.git] / xwindow.c
blobab3b80a1718609c7e10d9667fa74cf2c3589e274
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>
24 #include <xcb/shape.h>
25 #include <cairo-xcb.h>
27 #include "xwindow.h"
28 #include "objects/button.h"
29 #include "common/atoms.h"
31 /** Mask shorthands */
32 #define BUTTONMASK (XCB_EVENT_MASK_BUTTON_PRESS | XCB_EVENT_MASK_BUTTON_RELEASE)
34 /** Set client state (WM_STATE) property.
35 * \param win The window to set state.
36 * \param state The state to set.
38 void
39 xwindow_set_state(xcb_window_t win, uint32_t state)
41 uint32_t data[] = { state, XCB_NONE };
42 xcb_change_property(globalconf.connection, XCB_PROP_MODE_REPLACE, win,
43 WM_STATE, WM_STATE, 32, 2, data);
46 /** Send request to get a window state (WM_STATE).
47 * \param w A client window.
48 * \return The cookie associated with the request.
50 xcb_get_property_cookie_t
51 xwindow_get_state_unchecked(xcb_window_t w)
53 return xcb_get_property_unchecked(globalconf.connection, false, w, WM_STATE,
54 WM_STATE, 0L, 2L);
57 /** Get a window state (WM_STATE).
58 * \param cookie The cookie.
59 * \return The current state of the window, or 0 on error.
61 uint32_t
62 xwindow_get_state_reply(xcb_get_property_cookie_t cookie)
64 /* If no property is set, we just assume a sane default. */
65 uint32_t result = XCB_ICCCM_WM_STATE_NORMAL;
66 xcb_get_property_reply_t *prop_r;
68 if((prop_r = xcb_get_property_reply(globalconf.connection, cookie, NULL)))
70 if(xcb_get_property_value_length(prop_r))
71 result = *(uint32_t *) xcb_get_property_value(prop_r);
73 p_delete(&prop_r);
76 return result;
79 /** Configure a window with its new geometry and border size.
80 * \param win The X window id to configure.
81 * \param geometry The new window geometry.
82 * \param border The new border size.
84 void
85 xwindow_configure(xcb_window_t win, area_t geometry, int border)
87 xcb_configure_notify_event_t ce;
89 ce.response_type = XCB_CONFIGURE_NOTIFY;
90 ce.event = win;
91 ce.window = win;
92 ce.x = geometry.x;
93 ce.y = geometry.y;
94 ce.width = geometry.width;
95 ce.height = geometry.height;
96 ce.border_width = border;
97 ce.above_sibling = XCB_NONE;
98 ce.override_redirect = false;
99 xcb_send_event(globalconf.connection, false, win, XCB_EVENT_MASK_STRUCTURE_NOTIFY, (char *) &ce);
102 /** Grab or ungrab buttons on a window.
103 * \param win The window.
104 * \param buttons The buttons to grab.
106 void
107 xwindow_buttons_grab(xcb_window_t win, button_array_t *buttons)
109 if(win == XCB_NONE)
110 return;
112 /* Ungrab everything first */
113 xcb_ungrab_button(globalconf.connection, XCB_BUTTON_INDEX_ANY, win, XCB_BUTTON_MASK_ANY);
115 foreach(b, *buttons)
116 xcb_grab_button(globalconf.connection, false, win, BUTTONMASK,
117 XCB_GRAB_MODE_SYNC, XCB_GRAB_MODE_ASYNC, XCB_NONE, XCB_NONE,
118 (*b)->button, (*b)->modifiers);
121 /** Grab key on a window.
122 * \param win The window.
123 * \param k The key.
125 static void
126 xwindow_grabkey(xcb_window_t win, keyb_t *k)
128 if(k->keycode)
129 xcb_grab_key(globalconf.connection, true, win,
130 k->modifiers, k->keycode, XCB_GRAB_MODE_ASYNC, XCB_GRAB_MODE_ASYNC);
131 else if(k->keysym)
133 xcb_keycode_t *keycodes = xcb_key_symbols_get_keycode(globalconf.keysyms, k->keysym);
134 if(keycodes)
136 for(xcb_keycode_t *kc = keycodes; *kc; kc++)
137 xcb_grab_key(globalconf.connection, true, win,
138 k->modifiers, *kc, XCB_GRAB_MODE_ASYNC, XCB_GRAB_MODE_ASYNC);
139 p_delete(&keycodes);
144 void
145 xwindow_grabkeys(xcb_window_t win, key_array_t *keys)
147 /* Ungrab everything first */
148 xcb_ungrab_key(globalconf.connection, XCB_GRAB_ANY, win, XCB_BUTTON_MASK_ANY);
150 foreach(k, *keys)
151 xwindow_grabkey(win, *k);
154 /** Send a request for a window's opacity.
155 * \param win The window
156 * \return A cookie for xwindow_get_opacity_from_reply().
158 xcb_get_property_cookie_t xwindow_get_opacity_unchecked(xcb_window_t win)
160 return xcb_get_property_unchecked(globalconf.connection, false, win,
161 _NET_WM_WINDOW_OPACITY, XCB_ATOM_CARDINAL, 0L, 1L);
164 /** Get the opacity of a window.
165 * \param win The window.
166 * \return The opacity, between 0 and 1 or -1 or no opacity set.
168 double
169 xwindow_get_opacity(xcb_window_t win)
171 xcb_get_property_cookie_t prop_c =
172 xwindow_get_opacity_unchecked(win);
173 return xwindow_get_opacity_from_cookie(prop_c);
176 /** Get the opacity of a window.
177 * \param cookie A cookie for a reply to a get property request for _NET_WM_WINDOW_OPACITY.
178 * \return The opacity, between 0 and 1.
180 double
181 xwindow_get_opacity_from_cookie(xcb_get_property_cookie_t cookie)
183 xcb_get_property_reply_t *prop_r =
184 xcb_get_property_reply(globalconf.connection, cookie, NULL);
186 if(prop_r && prop_r->value_len && prop_r->format == 32)
188 uint32_t value = *(uint32_t *) xcb_get_property_value(prop_r);
189 p_delete(&prop_r);
190 return (double) value / (double) 0xffffffff;
192 p_delete(&prop_r);
194 return -1;
197 /** Set opacity of a window.
198 * \param win The window.
199 * \param opacity Opacity of the window, between 0 and 1.
201 void
202 xwindow_set_opacity(xcb_window_t win, double opacity)
204 if(win)
206 if(opacity >= 0 && opacity <= 1)
208 uint32_t real_opacity = opacity * 0xffffffff;
209 xcb_change_property(globalconf.connection, XCB_PROP_MODE_REPLACE, win,
210 _NET_WM_WINDOW_OPACITY, XCB_ATOM_CARDINAL, 32, 1L, &real_opacity);
212 else
213 xcb_delete_property(globalconf.connection, win, _NET_WM_WINDOW_OPACITY);
217 /** Send WM_TAKE_FOCUS client message to window
218 * \param win destination window
220 void
221 xwindow_takefocus(xcb_window_t win)
223 xcb_client_message_event_t ev;
225 /* Initialize all of event's fields first */
226 p_clear(&ev, 1);
228 ev.response_type = XCB_CLIENT_MESSAGE;
229 ev.window = win;
230 ev.format = 32;
231 ev.data.data32[1] = globalconf.timestamp;
232 ev.type = WM_PROTOCOLS;
233 ev.data.data32[0] = WM_TAKE_FOCUS;
235 xcb_send_event(globalconf.connection, false, win,
236 XCB_EVENT_MASK_NO_EVENT, (char *) &ev);
239 /** Set window cursor.
240 * \param w The window.
241 * \param c The cursor.
243 void
244 xwindow_set_cursor(xcb_window_t w, xcb_cursor_t c)
246 xcb_change_window_attributes(globalconf.connection, w, XCB_CW_CURSOR,
247 (const uint32_t[]) { c });
250 /** Set a window border color.
251 * \param w The window.
252 * \param color The color.
254 void
255 xwindow_set_border_color(xcb_window_t w, color_t *color)
257 if(w)
258 xcb_change_window_attributes(globalconf.connection, w, XCB_CW_BORDER_PIXEL, &color->pixel);
261 /** Turn a cairo surface into a pixmap with depth 1 */
262 static xcb_pixmap_t
263 xwindow_shape_pixmap(int width, int height, cairo_surface_t *surf)
265 xcb_pixmap_t pixmap = xcb_generate_id(globalconf.connection);
266 cairo_surface_t *dest;
267 cairo_t *cr;
269 xcb_create_pixmap(globalconf.connection, 1, pixmap, globalconf.screen->root, width, height);
270 dest = cairo_xcb_surface_create_for_bitmap(globalconf.connection, globalconf.screen, pixmap, width, height);
272 cr = cairo_create(dest);
273 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
274 cairo_set_source_surface(cr, surf, 0, 0);
275 cairo_paint(cr);
277 cairo_destroy(cr);
278 cairo_surface_flush(dest);
279 cairo_surface_finish(dest);
280 cairo_surface_destroy(dest);
282 return pixmap;
285 /** Set one of a window's shapes */
286 void
287 xwindow_set_shape(xcb_window_t win, int width, int height, enum xcb_shape_sk_t kind, cairo_surface_t *surf, int offset)
289 xcb_pixmap_t pixmap = XCB_NONE;
290 if (surf)
291 pixmap = xwindow_shape_pixmap(width, height, surf);
293 xcb_shape_mask(globalconf.connection, XCB_SHAPE_SO_SET, kind, win, offset, offset, pixmap);
295 if (pixmap != XCB_NONE)
296 xcb_free_pixmap(globalconf.connection, pixmap);
299 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:textwidth=80