image: Remove some code duplication
[awesome.git] / window.c
blob1d21ec40bdf9dade8253e62acdd3c33679bf3ab2
1 /*
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.
22 #include <xcb/xcb.h>
23 #include <xcb/xcb_atom.h>
25 #include "window.h"
26 #include "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 window_state_set(xcb_window_t win, long state)
39 long 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 window_state_get_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 window_state_get_reply(xcb_get_property_cookie_t cookie)
62 uint32_t result = 0;
63 xcb_get_property_reply_t *prop_r;
65 if((prop_r = xcb_get_property_reply(globalconf.connection, cookie, NULL)))
67 if(xcb_get_property_value_length(prop_r))
68 result = *(uint32_t *) xcb_get_property_value(prop_r);
70 p_delete(&prop_r);
73 return result;
76 /** Configure a window with its new geometry and border size.
77 * \param win The X window id to configure.
78 * \param geometry The new window geometry.
79 * \param border The new border size.
81 void
82 window_configure(xcb_window_t win, area_t geometry, int border)
84 xcb_configure_notify_event_t ce;
86 ce.response_type = XCB_CONFIGURE_NOTIFY;
87 ce.event = win;
88 ce.window = win;
89 ce.x = geometry.x;
90 ce.y = geometry.y;
91 ce.width = geometry.width;
92 ce.height = geometry.height;
93 ce.border_width = border;
94 ce.above_sibling = XCB_NONE;
95 ce.override_redirect = false;
96 xcb_send_event(globalconf.connection, false, win, XCB_EVENT_MASK_STRUCTURE_NOTIFY, (char *) &ce);
99 /** Grab or ungrab buttons on a window.
100 * \param win The window.
101 * \param buttons The buttons to grab.
103 void
104 window_buttons_grab(xcb_window_t win, button_array_t *buttons)
106 foreach(b, *buttons)
107 xcb_grab_button(globalconf.connection, false, win, BUTTONMASK,
108 XCB_GRAB_MODE_SYNC, XCB_GRAB_MODE_ASYNC, XCB_NONE, XCB_NONE,
109 (*b)->button, (*b)->mod);
112 /** Get the opacity of a window.
113 * \param The window.
114 * \return The opacity, between 0 and 1 or -1 or no opacity set.
116 double
117 window_opacity_get(xcb_window_t win)
119 double ret;
121 xcb_get_property_cookie_t prop_c =
122 xcb_get_property_unchecked(globalconf.connection, false, win,
123 _NET_WM_WINDOW_OPACITY, CARDINAL, 0L, 1L);
125 xcb_get_property_reply_t *prop_r =
126 xcb_get_property_reply(globalconf.connection, prop_c, NULL);
128 ret = window_opacity_get_from_reply(prop_r);
129 p_delete(&prop_r);
130 return ret;
133 /** Get the opacity of a window.
134 * \param A reply to a get property request for _NET_WM_WINDOW_OPACITY.
135 * \return The opacity, between 0 and 1.
137 double window_opacity_get_from_reply(xcb_get_property_reply_t *prop_r)
139 if(prop_r && prop_r->value_len && prop_r->format == 32)
141 uint32_t value = *(uint32_t *) xcb_get_property_value(prop_r);
142 return (double) value / (double) 0xffffffff;
145 return -1;
148 /** Set opacity of a window.
149 * \param win The window.
150 * \param opacity Opacity of the window, between 0 and 1.
152 void
153 window_opacity_set(xcb_window_t win, double opacity)
155 if(opacity >= 0 && opacity <= 1)
157 uint32_t real_opacity = opacity * 0xffffffff;
158 xcb_change_property(globalconf.connection, XCB_PROP_MODE_REPLACE, win,
159 _NET_WM_WINDOW_OPACITY, CARDINAL, 32, 1L, &real_opacity);
161 else
162 xcb_delete_property(globalconf.connection, win, _NET_WM_WINDOW_OPACITY);
165 /** Check if client supports protocol a protocole in WM_PROTOCOL.
166 * \param win The window.
167 * \return True if client has the atom in protocol, false otherwise.
169 bool
170 window_hasproto(xcb_window_t win, xcb_atom_t atom)
172 uint32_t i;
173 xcb_get_wm_protocols_reply_t protocols;
174 bool ret = false;
176 if(xcb_get_wm_protocols_reply(globalconf.connection,
177 xcb_get_wm_protocols_unchecked(globalconf.connection,
178 win, WM_PROTOCOLS),
179 &protocols, NULL))
181 for(i = 0; !ret && i < protocols.atoms_len; i++)
182 if(protocols.atoms[i] == atom)
183 ret = true;
184 xcb_get_wm_protocols_reply_wipe(&protocols);
186 return ret;
189 /** Send WM_TAKE_FOCUS client message to window
190 * \param win destination window
192 void
193 window_takefocus(xcb_window_t win)
195 xcb_client_message_event_t ev;
197 /* Initialize all of event's fields first */
198 p_clear(&ev, 1);
200 ev.response_type = XCB_CLIENT_MESSAGE;
201 ev.window = win;
202 ev.format = 32;
203 ev.data.data32[1] = XCB_CURRENT_TIME;
204 ev.type = WM_PROTOCOLS;
205 ev.data.data32[0] = WM_TAKE_FOCUS;
207 xcb_send_event(globalconf.connection, false, win,
208 XCB_EVENT_MASK_NO_EVENT, (char *) &ev);
211 /** Sets focus on window - using xcb_set_input_focus or WM_TAKE_FOCUS
212 * \param w Window that should get focus
213 * \param set_input_focus Should we call xcb_set_input_focus
215 void
216 window_setfocus(xcb_window_t w, bool set_input_focus)
218 bool takefocus = window_hasproto(w, WM_TAKE_FOCUS);
219 if(set_input_focus)
220 xcb_set_input_focus(globalconf.connection, XCB_INPUT_FOCUS_PARENT,
221 w, XCB_CURRENT_TIME);
222 if(takefocus)
223 window_takefocus(w);
226 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80