always grab Button1
[awesome.git] / window.c
blobc267385403439ed93709f666590035be1dae22ad
1 /*
2 * window.c - window handling functions
4 * Copyright © 2007 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 <X11/Xatom.h>
23 #include <X11/extensions/shape.h>
25 #include "window.h"
26 #include "common/util.h"
28 extern AwesomeConf globalconf;
30 /** Mask shorthands, used in event.c and window.c */
31 #define BUTTONMASK (ButtonPressMask | ButtonReleaseMask)
33 /** Set client WM_STATE property
34 * \param win Window
35 * \param state state
36 * \return XChangeProperty result
38 int
39 window_setstate(Window win, long state)
41 long data[] = { state, None };
43 return XChangeProperty(globalconf.display, win, XInternAtom(globalconf.display, "WM_STATE", False),
44 XInternAtom(globalconf.display, "WM_STATE", False), 32,
45 PropModeReplace, (unsigned char *) data, 2);
48 /** Get a window state (WM_STATE)
49 * \param w Client window
50 * \return state
52 long
53 window_getstate(Window w)
55 int format;
56 long result = -1;
57 unsigned char *p = NULL;
58 unsigned long n, extra;
59 Atom real;
60 if(XGetWindowProperty(globalconf.display, w, XInternAtom(globalconf.display, "WM_STATE", False),
61 0L, 2L, False, XInternAtom(globalconf.display, "WM_STATE", False),
62 &real, &format, &n, &extra, (unsigned char **) &p) != Success)
63 return -1;
64 if(n != 0)
65 result = *p;
66 p_delete(&p);
67 return result;
70 Status
71 window_configure(Window win, Area geometry, int border)
73 XConfigureEvent ce;
75 ce.type = ConfigureNotify;
76 ce.display = globalconf.display;
77 ce.event = win;
78 ce.window = win;
79 ce.x = geometry.x;
80 ce.y = geometry.y;
81 ce.width = geometry.width;
82 ce.height = geometry.height;
83 ce.border_width = border;
84 ce.above = None;
85 ce.override_redirect = False;
86 return XSendEvent(globalconf.display, win, False, StructureNotifyMask, (XEvent *) & ce);
89 /** Grab or ungrab buttons on a window
90 * \param screen The screen
91 * \param win The window
93 void
94 window_grabbuttons(int screen, Window win)
96 Button *b;
98 XGrabButton(globalconf.display, Button1, NoSymbol,
99 win, False, BUTTONMASK, GrabModeSync, GrabModeAsync, None, None);
100 XGrabButton(globalconf.display, Button1, NoSymbol | LockMask,
101 win, False, BUTTONMASK, GrabModeSync, GrabModeAsync, None, None);
102 XGrabButton(globalconf.display, Button1, NoSymbol | globalconf.numlockmask,
103 win, False, BUTTONMASK, GrabModeSync, GrabModeAsync, None, None);
104 XGrabButton(globalconf.display, Button1, NoSymbol | globalconf.numlockmask | LockMask,
105 win, False, BUTTONMASK, GrabModeSync, GrabModeAsync, None, None);
107 for(b = globalconf.buttons.client; b; b = b->next)
109 XGrabButton(globalconf.display, b->button, b->mod,
110 win, False, BUTTONMASK, GrabModeAsync, GrabModeSync, None, None);
111 XGrabButton(globalconf.display, b->button, b->mod | LockMask,
112 win, False, BUTTONMASK, GrabModeAsync, GrabModeSync, None, None);
113 XGrabButton(globalconf.display, b->button, b->mod | globalconf.numlockmask,
114 win, False, BUTTONMASK, GrabModeAsync, GrabModeSync, None, None);
115 XGrabButton(globalconf.display, b->button, b->mod | globalconf.numlockmask | LockMask,
116 win, False, BUTTONMASK, GrabModeAsync, GrabModeSync, None, None);
119 XUngrabButton(globalconf.display, AnyButton, AnyModifier, RootWindow(globalconf.display, screen));
122 void
123 window_root_grabbuttons(int screen)
125 Button *b;
127 for(b = globalconf.buttons.root; b; b = b->next)
129 XGrabButton(globalconf.display, b->button, b->mod,
130 RootWindow(globalconf.display, screen), False, BUTTONMASK,
131 GrabModeAsync, GrabModeSync, None, None);
132 XGrabButton(globalconf.display, b->button, b->mod | LockMask,
133 RootWindow(globalconf.display, screen), False, BUTTONMASK,
134 GrabModeAsync, GrabModeSync, None, None);
135 XGrabButton(globalconf.display, b->button, b->mod | globalconf.numlockmask,
136 RootWindow(globalconf.display, screen), False, BUTTONMASK,
137 GrabModeAsync, GrabModeSync, None, None);
138 XGrabButton(globalconf.display, b->button, b->mod | globalconf.numlockmask | LockMask,
139 RootWindow(globalconf.display, screen), False, BUTTONMASK,
140 GrabModeAsync, GrabModeSync, None, None);
144 void
145 window_setshape(int screen, Window win)
147 int bounding_shaped;
148 int i, b; unsigned int u; /* dummies */
149 /* Logic to decide if we have a shaped window cribbed from fvwm-2.5.10. */
150 if(XShapeQueryExtents(globalconf.display, win, &bounding_shaped, &i, &i,
151 &u, &u, &b, &i, &i, &u, &u) && bounding_shaped)
152 XShapeCombineShape(globalconf.display, RootWindow(globalconf.display, screen), ShapeBounding, 0, 0, win, ShapeBounding, ShapeSet);
156 window_settrans(Window win, double opacity)
158 int status;
159 unsigned int real_opacity = 0xffffffff;
161 if(opacity >= 0 && opacity <= 100)
163 real_opacity = ((opacity / 100.0) * 0xffffffff);
164 status = XChangeProperty(globalconf.display, win,
165 XInternAtom(globalconf.display, "_NET_WM_WINDOW_OPACITY", False),
166 XA_CARDINAL, 32, PropModeReplace, (unsigned char *) &real_opacity, 1L);
168 else
169 status = XDeleteProperty(globalconf.display, win,
170 XInternAtom(globalconf.display, "_NET_WM_WINDOW_OPACITY", False));
172 return status;
175 SimpleWindow *
176 simplewindow_new(int phys_screen, int x, int y, unsigned int w, unsigned int h,
177 unsigned int border_width)
179 XSetWindowAttributes wa;
180 SimpleWindow *sw;
182 sw = p_new(SimpleWindow, 1);
184 sw->geometry.x = x;
185 sw->geometry.y = y;
186 sw->geometry.width = w;
187 sw->geometry.height = h;
189 wa.event_mask = SubstructureRedirectMask | SubstructureNotifyMask
190 | EnterWindowMask | LeaveWindowMask | StructureNotifyMask;
191 wa.cursor = globalconf.cursor[CurNormal];
192 wa.override_redirect = 1;
193 wa.background_pixmap = ParentRelative;
194 wa.event_mask = ButtonPressMask | ExposureMask;
195 sw->window = XCreateWindow(globalconf.display,
196 RootWindow(globalconf.display, phys_screen),
197 x, y, w, h,
198 border_width,
199 DefaultDepth(globalconf.display, phys_screen),
200 CopyFromParent,
201 DefaultVisual(globalconf.display, phys_screen),
202 CWOverrideRedirect | CWBackPixmap | CWEventMask,
203 &wa);
205 sw->drawable = XCreatePixmap(globalconf.display,
206 RootWindow(globalconf.display, phys_screen),
207 w, h,
208 DefaultDepth(globalconf.display, phys_screen));
210 XDefineCursor(globalconf.display,
211 sw->window,
212 globalconf.cursor[CurNormal]);
214 return sw;
217 void
218 simplewindow_delete(SimpleWindow *sw)
220 XDestroyWindow(globalconf.display, sw->window);
221 XFreePixmap(globalconf.display, sw->drawable);
222 p_delete(&sw);
226 simplewindow_move(SimpleWindow *sw, int x, int y)
228 sw->geometry.x = x;
229 sw->geometry.y = y;
230 return XMoveWindow(globalconf.display, sw->window, x, y);
234 simplewindow_refresh_drawable(SimpleWindow *sw, int phys_screen)
236 return XCopyArea(globalconf.display, sw->drawable,
237 sw->window,
238 DefaultGC(globalconf.display, phys_screen), 0, 0,
239 sw->geometry.width,
240 sw->geometry.height,
241 0, 0);
244 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80