client: rework and document opacity field
[awesome.git] / window.c
blobe34554e3cb5f72dee059d1aaa9dcdbae55c5d97f
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_t *buttons)
110 button_t *b;
112 for(b = buttons; b; b = b->next)
114 xcb_grab_button(globalconf.connection, false, win, BUTTONMASK,
115 XCB_GRAB_MODE_SYNC, XCB_GRAB_MODE_ASYNC, XCB_NONE, XCB_NONE,
116 b->button, b->mod);
117 xcb_grab_button(globalconf.connection, false, win, BUTTONMASK,
118 XCB_GRAB_MODE_SYNC, XCB_GRAB_MODE_ASYNC, XCB_NONE, XCB_NONE,
119 b->button, b->mod | XCB_MOD_MASK_LOCK);
120 xcb_grab_button(globalconf.connection, false, win, BUTTONMASK,
121 XCB_GRAB_MODE_SYNC, XCB_GRAB_MODE_ASYNC, XCB_NONE, XCB_NONE,
122 b->button, b->mod | globalconf.numlockmask);
123 xcb_grab_button(globalconf.connection, false, win, BUTTONMASK,
124 XCB_GRAB_MODE_SYNC, XCB_GRAB_MODE_ASYNC, XCB_NONE, XCB_NONE,
125 b->button, b->mod | globalconf.numlockmask | XCB_MOD_MASK_LOCK);
128 xcb_ungrab_button(globalconf.connection, XCB_BUTTON_INDEX_ANY, root, XUTIL_ANY_MODIFIER);
131 /** Grab all buttons on the root window.
132 * \param root The root window.
134 void
135 window_root_buttons_grab(xcb_window_t root)
137 button_t *b;
139 for(b = globalconf.buttons.root; b; b = b->next)
141 xcb_grab_button(globalconf.connection, false, root, BUTTONMASK,
142 XCB_GRAB_MODE_ASYNC, XCB_GRAB_MODE_SYNC, XCB_NONE, XCB_NONE,
143 b->button, b->mod);
144 xcb_grab_button(globalconf.connection, false, root, BUTTONMASK,
145 XCB_GRAB_MODE_ASYNC, XCB_GRAB_MODE_SYNC, XCB_NONE, XCB_NONE,
146 b->button, b->mod | XCB_MOD_MASK_LOCK);
147 xcb_grab_button(globalconf.connection, false, root, BUTTONMASK,
148 XCB_GRAB_MODE_ASYNC, XCB_GRAB_MODE_SYNC, XCB_NONE, XCB_NONE,
149 b->button, b->mod | globalconf.numlockmask);
150 xcb_grab_button(globalconf.connection, false, root, BUTTONMASK,
151 XCB_GRAB_MODE_ASYNC, XCB_GRAB_MODE_SYNC, XCB_NONE, XCB_NONE,
152 b->button, b->mod | globalconf.numlockmask | XCB_MOD_MASK_LOCK);
156 /** Get the opacity of a window.
157 * \param The window.
158 * \return The opacity, between 0 and 1 or -1 or no opacity set.
160 double
161 window_opacity_get(xcb_window_t win)
163 xcb_get_property_cookie_t prop_c;
164 xcb_get_property_reply_t *prop_r;
166 prop_c = xcb_get_property_unchecked(globalconf.connection, false, win,
167 _NET_WM_WINDOW_OPACITY, CARDINAL, 0L, 1L);
169 prop_r = xcb_get_property_reply(globalconf.connection, prop_c, NULL);
171 if(!prop_r || !prop_r->value_len || prop_r->format != 32)
172 goto bailout;
174 if(prop_r->value_len)
176 unsigned int *data = xcb_get_property_value(prop_r);
177 printf("result %d\n", *data);
178 return (double) *data / (double) 0xffffffff;
181 bailout:
182 p_delete(&prop_r);
183 return -1;
186 /** Set opacity of a window.
187 * \param win The window.
188 * \param opacity Opacity of the window, between 0 and 1.
190 void
191 window_opacity_set(xcb_window_t win, double opacity)
193 unsigned int real_opacity = 0xffffffff;
195 if(opacity >= 0 && opacity <= 1)
197 real_opacity = opacity * 0xffffffff;
198 xcb_change_property(globalconf.connection, XCB_PROP_MODE_REPLACE, win,
199 _NET_WM_WINDOW_OPACITY, CARDINAL, 32, 1L, &real_opacity);
201 else
202 xcb_delete_property(globalconf.connection, win, _NET_WM_WINDOW_OPACITY);
205 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80