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.
23 #include <xcb/xcb_atom.h>
24 #include <xcb/shape.h>
25 #include <cairo-xcb.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.
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
,
57 /** Get a window state (WM_STATE).
58 * \param cookie The cookie.
59 * \return The current state of the window, or 0 on error.
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
);
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.
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
;
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.
107 xwindow_buttons_grab(xcb_window_t win
, button_array_t
*buttons
)
112 /* Ungrab everything first */
113 xcb_ungrab_button(globalconf
.connection
, XCB_BUTTON_INDEX_ANY
, win
, XCB_BUTTON_MASK_ANY
);
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.
126 xwindow_grabkey(xcb_window_t win
, keyb_t
*k
)
129 xcb_grab_key(globalconf
.connection
, true, win
,
130 k
->modifiers
, k
->keycode
, XCB_GRAB_MODE_ASYNC
, XCB_GRAB_MODE_ASYNC
);
133 xcb_keycode_t
*keycodes
= xcb_key_symbols_get_keycode(globalconf
.keysyms
, k
->keysym
);
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
);
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
);
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.
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.
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
);
190 return (double) value
/ (double) 0xffffffff;
197 /** Set opacity of a window.
198 * \param win The window.
199 * \param opacity Opacity of the window, between 0 and 1.
202 xwindow_set_opacity(xcb_window_t win
, double opacity
)
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
);
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
221 xwindow_takefocus(xcb_window_t win
)
223 xcb_client_message_event_t ev
;
225 /* Initialize all of event's fields first */
228 ev
.response_type
= XCB_CLIENT_MESSAGE
;
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.
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.
255 xwindow_set_border_color(xcb_window_t w
, color_t
*color
)
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 */
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
;
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);
278 cairo_surface_flush(dest
);
279 cairo_surface_finish(dest
);
280 cairo_surface_destroy(dest
);
285 /** Set one of a window's shapes */
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
;
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 /** Calculate the position change that a window needs applied.
300 * \param gravity The window gravity that should be used.
301 * \param change_width_before The window width difference that will be applied.
302 * \param change_height_before The window height difference that will be applied.
303 * \param change_width_after The window width difference that will be applied.
304 * \param change_height_after The window height difference that will be applied.
305 * \param dx On return, this will be set to the amount the pixel has to be moved.
306 * \param dy On return, this will be set to the amount the pixel has to be moved.
308 void xwindow_translate_for_gravity(xcb_gravity_t gravity
, int16_t change_width_before
, int16_t change_height_before
,
309 int16_t change_width_after
, int16_t change_height_after
, int16_t *dx
, int16_t *dy
)
311 int16_t x
= 0, y
= 0;
312 int16_t change_height
= change_height_before
+ change_height_after
;
313 int16_t change_width
= change_width_before
+ change_width_after
;
316 case XCB_GRAVITY_WIN_UNMAP
:
317 case XCB_GRAVITY_NORTH_WEST
:
319 case XCB_GRAVITY_NORTH
:
320 x
= -change_width
/ 2;
322 case XCB_GRAVITY_NORTH_EAST
:
325 case XCB_GRAVITY_WEST
:
326 y
= -change_height
/ 2;
328 case XCB_GRAVITY_CENTER
:
329 x
= -change_width
/ 2;
330 y
= -change_height
/ 2;
332 case XCB_GRAVITY_EAST
:
334 y
= -change_height
/ 2;
336 case XCB_GRAVITY_SOUTH_WEST
:
339 case XCB_GRAVITY_SOUTH
:
340 x
= -change_width
/ 2;
343 case XCB_GRAVITY_SOUTH_EAST
:
347 case XCB_GRAVITY_STATIC
:
348 x
= -change_width_before
;
349 x
= -change_height_before
;
359 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:textwidth=80