2 * window.c - window handling functions
4 * Copyright © 2007-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.
23 #include <xcb/xcb_atom.h>
26 #include "common/atoms.h"
28 /** Set client state (WM_STATE) property.
29 * \param win The window to set state.
30 * \param state The state to set.
33 window_state_set(xcb_window_t win
, long state
)
35 long data
[] = { state
, XCB_NONE
};
36 xcb_change_property(globalconf
.connection
, XCB_PROP_MODE_REPLACE
, win
,
37 WM_STATE
, WM_STATE
, 32, 2, data
);
40 /** Send request to get a window state (WM_STATE).
41 * \param w A client window.
42 * \return The cookie associated with the request.
44 xcb_get_property_cookie_t
45 window_state_get_unchecked(xcb_window_t w
)
47 return xcb_get_property_unchecked(globalconf
.connection
, false, w
, WM_STATE
,
51 /** Get a window state (WM_STATE).
52 * \param cookie The cookie.
53 * \return The current state of the window, or 0 on error.
56 window_state_get_reply(xcb_get_property_cookie_t cookie
)
59 xcb_get_property_reply_t
*prop_r
;
61 if((prop_r
= xcb_get_property_reply(globalconf
.connection
, cookie
, NULL
)))
63 if(xcb_get_property_value_length(prop_r
))
64 result
= *(uint32_t *) xcb_get_property_value(prop_r
);
72 /** Configure a window with its new geometry and border size.
73 * \param win The X window id to configure.
74 * \param geometry The new window geometry.
75 * \param border The new border size.
78 window_configure(xcb_window_t win
, area_t geometry
, int border
)
80 xcb_configure_notify_event_t ce
;
82 ce
.response_type
= XCB_CONFIGURE_NOTIFY
;
87 ce
.width
= geometry
.width
;
88 ce
.height
= geometry
.height
;
89 ce
.border_width
= border
;
90 ce
.above_sibling
= XCB_NONE
;
91 ce
.override_redirect
= false;
92 xcb_send_event(globalconf
.connection
, false, win
, XCB_EVENT_MASK_STRUCTURE_NOTIFY
, (char *) &ce
);
95 /** Get the opacity of a window.
96 * \param win The window.
97 * \return The opacity, between 0 and 1 or -1 or no opacity set.
100 window_opacity_get(xcb_window_t win
)
104 xcb_get_property_cookie_t prop_c
=
105 xcb_get_property_unchecked(globalconf
.connection
, false, win
,
106 _NET_WM_WINDOW_OPACITY
, CARDINAL
, 0L, 1L);
108 xcb_get_property_reply_t
*prop_r
=
109 xcb_get_property_reply(globalconf
.connection
, prop_c
, NULL
);
111 ret
= window_opacity_get_from_reply(prop_r
);
116 /** Get the opacity of a window.
117 * \param prop_r A reply to a get property request for _NET_WM_WINDOW_OPACITY.
118 * \return The opacity, between 0 and 1.
120 double window_opacity_get_from_reply(xcb_get_property_reply_t
*prop_r
)
122 if(prop_r
&& prop_r
->value_len
&& prop_r
->format
== 32)
124 uint32_t value
= *(uint32_t *) xcb_get_property_value(prop_r
);
125 return (double) value
/ (double) 0xffffffff;
131 /** Set opacity of a window.
132 * \param win The window.
133 * \param opacity Opacity of the window, between 0 and 1.
136 window_opacity_set(xcb_window_t win
, double opacity
)
138 if(opacity
>= 0 && opacity
<= 1)
140 uint32_t real_opacity
= opacity
* 0xffffffff;
141 xcb_change_property(globalconf
.connection
, XCB_PROP_MODE_REPLACE
, win
,
142 _NET_WM_WINDOW_OPACITY
, CARDINAL
, 32, 1L, &real_opacity
);
145 xcb_delete_property(globalconf
.connection
, win
, _NET_WM_WINDOW_OPACITY
);
148 /** Send WM_TAKE_FOCUS client message to window
149 * \param win destination window
152 window_takefocus(xcb_window_t win
)
154 xcb_client_message_event_t ev
;
156 /* Initialize all of event's fields first */
159 ev
.response_type
= XCB_CLIENT_MESSAGE
;
162 ev
.data
.data32
[1] = XCB_CURRENT_TIME
;
163 ev
.type
= WM_PROTOCOLS
;
164 ev
.data
.data32
[0] = WM_TAKE_FOCUS
;
166 xcb_send_event(globalconf
.connection
, false, win
,
167 XCB_EVENT_MASK_NO_EVENT
, (char *) &ev
);
170 /** Set wibox cursor.
171 * \param w The wibox.
172 * \param c The cursor.
175 window_set_cursor(xcb_window_t w
, xcb_cursor_t c
)
177 xcb_change_window_attributes(globalconf
.connection
, w
, XCB_CW_CURSOR
,
178 (const uint32_t[]) { c
});
181 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80