client: make `ontop' windows under fullscreen
[awesome.git] / window.c
blob0e05598240abadf1f402be80968dda378ba96001
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 "mouse.h"
27 #include "common/atoms.h"
29 extern awesome_t globalconf;
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 window_state_set(xcb_window_t win, long state)
41 long 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 window_state_get_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 -1 on error.
61 long
62 window_state_get_reply(xcb_get_property_cookie_t cookie)
64 long result = -1;
65 unsigned char *p = NULL;
66 xcb_get_property_reply_t *prop_r;
68 if(!(prop_r = xcb_get_property_reply(globalconf.connection, cookie, NULL)))
69 return -1;
71 p = xcb_get_property_value(prop_r);
72 if(xcb_get_property_value_length(prop_r) != 0)
73 result = *p;
75 p_delete(&prop_r);
77 return result;
80 /** Configure a window with its new geometry and border size.
81 * \param win The X window id to configure.
82 * \param geometry The new window geometry.
83 * \param border The new border size.
85 void
86 window_configure(xcb_window_t win, area_t geometry, int border)
88 xcb_configure_notify_event_t ce;
90 ce.response_type = XCB_CONFIGURE_NOTIFY;
91 ce.event = win;
92 ce.window = win;
93 ce.x = geometry.x;
94 ce.y = geometry.y;
95 ce.width = geometry.width;
96 ce.height = geometry.height;
97 ce.border_width = border;
98 ce.above_sibling = XCB_NONE;
99 ce.override_redirect = false;
100 xcb_send_event(globalconf.connection, false, win, XCB_EVENT_MASK_STRUCTURE_NOTIFY, (char *) &ce);
103 /** Grab or ungrab buttons on a window.
104 * \param win The window.
105 * \param root The root window.
106 * \param buttons The buttons to grab.
108 void
109 window_buttons_grab(xcb_window_t win, xcb_window_t root, button_array_t *buttons)
111 for(int i = 0; i < buttons->len; i++)
113 xcb_grab_button(globalconf.connection, false, win, BUTTONMASK,
114 XCB_GRAB_MODE_SYNC, XCB_GRAB_MODE_ASYNC, XCB_NONE, XCB_NONE,
115 buttons->tab[i]->button, buttons->tab[i]->mod);
116 xcb_grab_button(globalconf.connection, false, win, BUTTONMASK,
117 XCB_GRAB_MODE_SYNC, XCB_GRAB_MODE_ASYNC, XCB_NONE, XCB_NONE,
118 buttons->tab[i]->button, buttons->tab[i]->mod | XCB_MOD_MASK_LOCK);
119 xcb_grab_button(globalconf.connection, false, win, BUTTONMASK,
120 XCB_GRAB_MODE_SYNC, XCB_GRAB_MODE_ASYNC, XCB_NONE, XCB_NONE,
121 buttons->tab[i]->button, buttons->tab[i]->mod | globalconf.numlockmask);
122 xcb_grab_button(globalconf.connection, false, win, BUTTONMASK,
123 XCB_GRAB_MODE_SYNC, XCB_GRAB_MODE_ASYNC, XCB_NONE, XCB_NONE,
124 buttons->tab[i]->button, buttons->tab[i]->mod | globalconf.numlockmask | XCB_MOD_MASK_LOCK);
127 xcb_ungrab_button(globalconf.connection, XCB_BUTTON_INDEX_ANY, root, XCB_BUTTON_MASK_ANY);
130 /** Grab all buttons on the root window.
131 * \param root The root window.
133 void
134 window_root_buttons_grab(xcb_window_t root)
136 button_array_t *barr = &globalconf.buttons;
138 for(int i = 0; i < barr->len; i++)
140 xcb_grab_button(globalconf.connection, false, root, BUTTONMASK,
141 XCB_GRAB_MODE_ASYNC, XCB_GRAB_MODE_SYNC, XCB_NONE, XCB_NONE,
142 barr->tab[i]->button, barr->tab[i]->mod);
143 xcb_grab_button(globalconf.connection, false, root, BUTTONMASK,
144 XCB_GRAB_MODE_ASYNC, XCB_GRAB_MODE_SYNC, XCB_NONE, XCB_NONE,
145 barr->tab[i]->button, barr->tab[i]->mod | XCB_MOD_MASK_LOCK);
146 xcb_grab_button(globalconf.connection, false, root, BUTTONMASK,
147 XCB_GRAB_MODE_ASYNC, XCB_GRAB_MODE_SYNC, XCB_NONE, XCB_NONE,
148 barr->tab[i]->button, barr->tab[i]->mod | globalconf.numlockmask);
149 xcb_grab_button(globalconf.connection, false, root, BUTTONMASK,
150 XCB_GRAB_MODE_ASYNC, XCB_GRAB_MODE_SYNC, XCB_NONE, XCB_NONE,
151 barr->tab[i]->button, barr->tab[i]->mod | globalconf.numlockmask | XCB_MOD_MASK_LOCK);
155 /** Get the opacity of a window.
156 * \param The window.
157 * \return The opacity, between 0 and 1 or -1 or no opacity set.
159 double
160 window_opacity_get(xcb_window_t win)
162 xcb_get_property_cookie_t prop_c;
163 xcb_get_property_reply_t *prop_r;
165 prop_c = xcb_get_property_unchecked(globalconf.connection, false, win,
166 _NET_WM_WINDOW_OPACITY, CARDINAL, 0L, 1L);
168 prop_r = xcb_get_property_reply(globalconf.connection, prop_c, NULL);
170 if(!prop_r || !prop_r->value_len || prop_r->format != 32)
171 goto bailout;
173 if(prop_r->value_len)
175 unsigned int *data = xcb_get_property_value(prop_r);
176 return (double) *data / (double) 0xffffffff;
179 bailout:
180 p_delete(&prop_r);
181 return -1;
184 /** Set opacity of a window.
185 * \param win The window.
186 * \param opacity Opacity of the window, between 0 and 1.
188 void
189 window_opacity_set(xcb_window_t win, double opacity)
191 unsigned int real_opacity = 0xffffffff;
193 if(opacity >= 0 && opacity <= 1)
195 real_opacity = opacity * 0xffffffff;
196 xcb_change_property(globalconf.connection, XCB_PROP_MODE_REPLACE, win,
197 _NET_WM_WINDOW_OPACITY, CARDINAL, 32, 1L, &real_opacity);
199 else
200 xcb_delete_property(globalconf.connection, win, _NET_WM_WINDOW_OPACITY);
203 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80