mouse: fix button delete
[awesome.git] / window.c
blobf091e618ca3a3b0cca85de982b0df6211f0f7dcc
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 "common/atoms.h"
28 extern awesome_t globalconf;
30 /** Mask shorthands */
31 #define BUTTONMASK (XCB_EVENT_MASK_BUTTON_PRESS | XCB_EVENT_MASK_BUTTON_RELEASE)
33 /** Set client state (WM_STATE) property.
34 * \param win The window to set state.
35 * \param state The state to set.
37 void
38 window_state_set(xcb_window_t win, long state)
40 long data[] = { state, XCB_NONE };
41 xcb_change_property(globalconf.connection, XCB_PROP_MODE_REPLACE, win,
42 WM_STATE, WM_STATE, 32, 2, data);
45 /** Send request to get a window state (WM_STATE).
46 * \param w A client window.
47 * \return The cookie associated with the request.
49 xcb_get_property_cookie_t
50 window_state_get_unchecked(xcb_window_t w)
52 return xcb_get_property_unchecked(globalconf.connection, false, w, WM_STATE,
53 WM_STATE, 0L, 2L);
56 /** Get a window state (WM_STATE).
57 * \param cookie The cookie.
58 * \return The current state of the window, or -1 on error.
60 long
61 window_state_get_reply(xcb_get_property_cookie_t cookie)
63 long result = -1;
64 unsigned char *p = NULL;
65 xcb_get_property_reply_t *prop_r;
67 if(!(prop_r = xcb_get_property_reply(globalconf.connection, cookie, NULL)))
68 return -1;
70 p = xcb_get_property_value(prop_r);
71 if(xcb_get_property_value_length(prop_r) != 0)
72 result = *p;
74 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 window_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;
93 ce.y = geometry.y;
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 root The root window.
105 * \param buttons The buttons to grab.
107 void
108 window_buttons_grab(xcb_window_t win, xcb_window_t root, button_array_t *buttons)
110 for(int i = 0; i < buttons->len; i++)
112 xcb_grab_button(globalconf.connection, false, win, BUTTONMASK,
113 XCB_GRAB_MODE_SYNC, XCB_GRAB_MODE_ASYNC, XCB_NONE, XCB_NONE,
114 buttons->tab[i]->button, buttons->tab[i]->mod);
115 xcb_grab_button(globalconf.connection, false, win, BUTTONMASK,
116 XCB_GRAB_MODE_SYNC, XCB_GRAB_MODE_ASYNC, XCB_NONE, XCB_NONE,
117 buttons->tab[i]->button, buttons->tab[i]->mod | XCB_MOD_MASK_LOCK);
118 xcb_grab_button(globalconf.connection, false, win, BUTTONMASK,
119 XCB_GRAB_MODE_SYNC, XCB_GRAB_MODE_ASYNC, XCB_NONE, XCB_NONE,
120 buttons->tab[i]->button, buttons->tab[i]->mod | globalconf.numlockmask);
121 xcb_grab_button(globalconf.connection, false, win, BUTTONMASK,
122 XCB_GRAB_MODE_SYNC, XCB_GRAB_MODE_ASYNC, XCB_NONE, XCB_NONE,
123 buttons->tab[i]->button, buttons->tab[i]->mod | globalconf.numlockmask | XCB_MOD_MASK_LOCK);
126 xcb_ungrab_button(globalconf.connection, XCB_BUTTON_INDEX_ANY, root, XCB_BUTTON_MASK_ANY);
129 /** Grab all buttons on the root window.
130 * \param root The root window.
132 void
133 window_root_buttons_grab(xcb_window_t root)
135 button_array_t *barr = &globalconf.buttons;
137 for(int i = 0; i < barr->len; i++)
139 xcb_grab_button(globalconf.connection, false, root, BUTTONMASK,
140 XCB_GRAB_MODE_ASYNC, XCB_GRAB_MODE_SYNC, XCB_NONE, XCB_NONE,
141 barr->tab[i]->button, barr->tab[i]->mod);
142 xcb_grab_button(globalconf.connection, false, root, BUTTONMASK,
143 XCB_GRAB_MODE_ASYNC, XCB_GRAB_MODE_SYNC, XCB_NONE, XCB_NONE,
144 barr->tab[i]->button, barr->tab[i]->mod | XCB_MOD_MASK_LOCK);
145 xcb_grab_button(globalconf.connection, false, root, BUTTONMASK,
146 XCB_GRAB_MODE_ASYNC, XCB_GRAB_MODE_SYNC, XCB_NONE, XCB_NONE,
147 barr->tab[i]->button, barr->tab[i]->mod | globalconf.numlockmask);
148 xcb_grab_button(globalconf.connection, false, root, BUTTONMASK,
149 XCB_GRAB_MODE_ASYNC, XCB_GRAB_MODE_SYNC, XCB_NONE, XCB_NONE,
150 barr->tab[i]->button, barr->tab[i]->mod | globalconf.numlockmask | XCB_MOD_MASK_LOCK);
154 /** Get the opacity of a window.
155 * \param The window.
156 * \return The opacity, between 0 and 1 or -1 or no opacity set.
158 double
159 window_opacity_get(xcb_window_t win)
161 xcb_get_property_cookie_t prop_c;
162 xcb_get_property_reply_t *prop_r;
164 prop_c = xcb_get_property_unchecked(globalconf.connection, false, win,
165 _NET_WM_WINDOW_OPACITY, CARDINAL, 0L, 1L);
167 prop_r = xcb_get_property_reply(globalconf.connection, prop_c, NULL);
169 if(!prop_r || !prop_r->value_len || prop_r->format != 32)
170 goto bailout;
172 if(prop_r->value_len)
174 unsigned int *data = xcb_get_property_value(prop_r);
175 return (double) *data / (double) 0xffffffff;
178 bailout:
179 p_delete(&prop_r);
180 return -1;
183 /** Set opacity of a window.
184 * \param win The window.
185 * \param opacity Opacity of the window, between 0 and 1.
187 void
188 window_opacity_set(xcb_window_t win, double opacity)
190 unsigned int real_opacity = 0xffffffff;
192 if(opacity >= 0 && opacity <= 1)
194 real_opacity = opacity * 0xffffffff;
195 xcb_change_property(globalconf.connection, XCB_PROP_MODE_REPLACE, win,
196 _NET_WM_WINDOW_OPACITY, CARDINAL, 32, 1L, &real_opacity);
198 else
199 xcb_delete_property(globalconf.connection, win, _NET_WM_WINDOW_OPACITY);
202 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80