Only set client's urgent after startup
[awesome.git] / xwindow.c
blob6556ce7268ae939695e056acca0bf9d8a316d2ab
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 + border;
93 ce.y = geometry.y + border;
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 /** Get one of a window's shapes as a cairo surface */
262 cairo_surface_t *
263 xwindow_get_shape(xcb_window_t win, enum xcb_shape_sk_t kind)
265 if (!globalconf.have_shape)
266 return NULL;
268 xcb_shape_query_extents_cookie_t ecookie = xcb_shape_query_extents(globalconf.connection, win);
269 xcb_shape_get_rectangles_cookie_t rcookie = xcb_shape_get_rectangles(globalconf.connection, win, kind);
270 xcb_shape_query_extents_reply_t *extents = xcb_shape_query_extents_reply(globalconf.connection, ecookie, NULL);
271 xcb_shape_get_rectangles_reply_t *rects_reply = xcb_shape_get_rectangles_reply(globalconf.connection, rcookie, NULL);
273 if (!extents || !rects_reply)
275 free(extents);
276 free(rects_reply);
277 /* Create a cairo surface in an error state */
278 return cairo_image_surface_create(CAIRO_FORMAT_INVALID, -1, -1);
281 int16_t x, y;
282 uint16_t width, height;
283 bool shaped;
284 if (kind == XCB_SHAPE_SK_BOUNDING)
286 x = extents->bounding_shape_extents_x;
287 y = extents->bounding_shape_extents_y;
288 width = extents->bounding_shape_extents_width;
289 height = extents->bounding_shape_extents_height;
290 shaped = extents->bounding_shaped;
291 } else {
292 assert(kind == XCB_SHAPE_SK_CLIP);
293 x = extents->clip_shape_extents_x;
294 y = extents->clip_shape_extents_y;
295 width = extents->clip_shape_extents_width;
296 height = extents->clip_shape_extents_height;
297 shaped = extents->clip_shaped;
300 if (!shaped)
302 free(extents);
303 free(rects_reply);
304 return NULL;
307 cairo_surface_t *surface = cairo_image_surface_create(CAIRO_FORMAT_A1, width, height);
308 cairo_t *cr = cairo_create(surface);
309 int num_rects = xcb_shape_get_rectangles_rectangles_length(rects_reply);
310 xcb_rectangle_t *rects = xcb_shape_get_rectangles_rectangles(rects_reply);
312 cairo_surface_set_device_offset(surface, -x, -y);
313 cairo_set_fill_rule(cr, CAIRO_FILL_RULE_WINDING);
315 for (int i = 0; i < num_rects; i++)
316 cairo_rectangle(cr, rects[i].x, rects[i].y, rects[i].width, rects[i].height);
317 cairo_fill(cr);
319 cairo_destroy(cr);
320 free(extents);
321 free(rects_reply);
322 return surface;
325 /** Turn a cairo surface into a pixmap with depth 1 */
326 static xcb_pixmap_t
327 xwindow_shape_pixmap(int width, int height, cairo_surface_t *surf)
329 xcb_pixmap_t pixmap = xcb_generate_id(globalconf.connection);
330 cairo_surface_t *dest;
331 cairo_t *cr;
333 xcb_create_pixmap(globalconf.connection, 1, pixmap, globalconf.screen->root, width, height);
334 dest = cairo_xcb_surface_create_for_bitmap(globalconf.connection, globalconf.screen, pixmap, width, height);
336 cr = cairo_create(dest);
337 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
338 cairo_set_source_surface(cr, surf, 0, 0);
339 cairo_paint(cr);
341 cairo_destroy(cr);
342 cairo_surface_flush(dest);
343 cairo_surface_finish(dest);
344 cairo_surface_destroy(dest);
346 return pixmap;
349 /** Set one of a window's shapes */
350 void
351 xwindow_set_shape(xcb_window_t win, int width, int height, enum xcb_shape_sk_t kind, cairo_surface_t *surf, int offset)
353 if (!globalconf.have_shape)
354 return;
356 xcb_pixmap_t pixmap = XCB_NONE;
357 if (surf)
358 pixmap = xwindow_shape_pixmap(width, height, surf);
360 xcb_shape_mask(globalconf.connection, XCB_SHAPE_SO_SET, kind, win, offset, offset, pixmap);
362 if (pixmap != XCB_NONE)
363 xcb_free_pixmap(globalconf.connection, pixmap);
366 /** Calculate the position change that a window needs applied.
367 * \param gravity The window gravity that should be used.
368 * \param change_width_before The window width difference that will be applied.
369 * \param change_height_before The window height difference that will be applied.
370 * \param change_width_after The window width difference that will be applied.
371 * \param change_height_after The window height difference that will be applied.
372 * \param dx On return, this will be set to the amount the pixel has to be moved.
373 * \param dy On return, this will be set to the amount the pixel has to be moved.
375 void xwindow_translate_for_gravity(xcb_gravity_t gravity, int16_t change_width_before, int16_t change_height_before,
376 int16_t change_width_after, int16_t change_height_after, int16_t *dx, int16_t *dy)
378 int16_t x = 0, y = 0;
379 int16_t change_height = change_height_before + change_height_after;
380 int16_t change_width = change_width_before + change_width_after;
382 switch (gravity) {
383 case XCB_GRAVITY_WIN_UNMAP:
384 case XCB_GRAVITY_NORTH_WEST:
385 break;
386 case XCB_GRAVITY_NORTH:
387 x = -change_width / 2;
388 break;
389 case XCB_GRAVITY_NORTH_EAST:
390 x = -change_width;
391 break;
392 case XCB_GRAVITY_WEST:
393 y = -change_height / 2;
394 break;
395 case XCB_GRAVITY_CENTER:
396 x = -change_width / 2;
397 y = -change_height / 2;
398 break;
399 case XCB_GRAVITY_EAST:
400 x = -change_width;
401 y = -change_height / 2;
402 break;
403 case XCB_GRAVITY_SOUTH_WEST:
404 y = -change_height;
405 break;
406 case XCB_GRAVITY_SOUTH:
407 x = -change_width / 2;
408 y = -change_height;
409 break;
410 case XCB_GRAVITY_SOUTH_EAST:
411 x = -change_width;
412 y = -change_height;
413 break;
414 case XCB_GRAVITY_STATIC:
415 x = -change_width_before;
416 x = -change_height_before;
417 break;
420 if (dx)
421 *dx = x;
422 if (dy)
423 *dy = y;
426 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:textwidth=80