all repos — dwm @ b38905b0048ded1cdd20b0edec64d56450c47551

fork of suckless dynamic window manager

event.c (view raw)

  1/*
  2 * (C)opyright MMVI Anselm R. Garbe <garbeam at gmail dot com>
  3 * See LICENSE file for license details.
  4 */
  5#include "dwm.h"
  6
  7#include <stdlib.h>
  8#include <X11/keysym.h>
  9#include <X11/Xatom.h>
 10
 11/* static */
 12
 13typedef struct {
 14	unsigned long mod;
 15	KeySym keysym;
 16	void (*func)(Arg *arg);
 17	Arg arg;
 18} Key;
 19
 20CMDS
 21KEYS
 22
 23static unsigned int valid_mask =  255 &  ~(NUMLOCKMASK | LockMask);
 24
 25static void
 26movemouse(Client *c)
 27{
 28	int x1, y1, ocx, ocy, di;
 29	unsigned int dui;
 30	Window dummy;
 31	XEvent ev;
 32
 33	ocx = c->x;
 34	ocy = c->y;
 35	if(XGrabPointer(dpy, root, False, MOUSEMASK, GrabModeAsync, GrabModeAsync,
 36			None, cursor[CurMove], CurrentTime) != GrabSuccess)
 37		return;
 38	XQueryPointer(dpy, root, &dummy, &dummy, &x1, &y1, &di, &di, &dui);
 39	for(;;) {
 40		XMaskEvent(dpy, MOUSEMASK | ExposureMask, &ev);
 41		switch (ev.type) {
 42		default: break;
 43		case Expose:
 44			handler[Expose](&ev);
 45			break;
 46		case MotionNotify:
 47			XSync(dpy, False);
 48			c->x = ocx + (ev.xmotion.x - x1);
 49			c->y = ocy + (ev.xmotion.y - y1);
 50			resize(c, False, TopLeft);
 51			break;
 52		case ButtonRelease:
 53			XUngrabPointer(dpy, CurrentTime);
 54			return;
 55		}
 56	}
 57}
 58
 59static void
 60resizemouse(Client *c)
 61{
 62	int ocx, ocy;
 63	Corner sticky;
 64	XEvent ev;
 65
 66	ocx = c->x;
 67	ocy = c->y;
 68	if(XGrabPointer(dpy, root, False, MOUSEMASK, GrabModeAsync, GrabModeAsync,
 69				None, cursor[CurResize], CurrentTime) != GrabSuccess)
 70		return;
 71	XWarpPointer(dpy, None, c->win, 0, 0, 0, 0, c->w, c->h);
 72	for(;;) {
 73		XMaskEvent(dpy, MOUSEMASK | ExposureMask, &ev);
 74		switch(ev.type) {
 75		default: break;
 76		case Expose:
 77			handler[Expose](&ev);
 78			break;
 79		case MotionNotify:
 80			XSync(dpy, False);
 81			c->w = abs(ocx - ev.xmotion.x);
 82			c->h = abs(ocy - ev.xmotion.y);
 83			c->x = (ocx <= ev.xmotion.x) ? ocx : ocx - c->w;
 84			c->y = (ocy <= ev.xmotion.y) ? ocy : ocy - c->h;
 85			if(ocx <= ev.xmotion.x)
 86				sticky = (ocy <= ev.xmotion.y) ? TopLeft : BotLeft;
 87			else
 88				sticky = (ocy <= ev.xmotion.y) ? TopRight : BotRight;
 89			resize(c, True, sticky);
 90			break;
 91		case ButtonRelease:
 92			XUngrabPointer(dpy, CurrentTime);
 93			return;
 94		}
 95	}
 96}
 97
 98static void
 99buttonpress(XEvent *e)
100{
101	int x;
102	Arg a;
103	Client *c;
104	XButtonPressedEvent *ev = &e->xbutton;
105
106	if(barwin == ev->window) {
107		switch(ev->button) {
108		default:
109			x = 0;
110			for(a.i = 0; a.i < TLast; a.i++) {
111				x += textw(tags[a.i]);
112				if(ev->x < x) {
113					view(&a);
114					break;
115				}
116			}
117			break;
118		case Button4:
119			viewnext(&a);
120			break;
121		case Button5:
122			viewprev(&a);
123			break;
124		}
125	}
126	else if((c = getclient(ev->window))) {
127		focus(c);
128		switch(ev->button) {
129		default:
130			break;
131		case Button1:
132			if(!c->ismax && (arrange == dofloat || c->isfloat)) {
133				higher(c);
134				movemouse(c);
135			}
136			break;
137		case Button2:
138			lower(c);
139			break;
140		case Button3:
141			if(!c->ismax && (arrange == dofloat || c->isfloat)) {
142				higher(c);
143				resizemouse(c);
144			}
145			break;
146		}
147	}
148}
149
150static void
151configurerequest(XEvent *e)
152{
153	Client *c;
154	XConfigureRequestEvent *ev = &e->xconfigurerequest;
155	XWindowChanges wc;
156
157	ev->value_mask &= ~CWSibling;
158	if((c = getclient(ev->window))) {
159		gravitate(c, True);
160		if(ev->value_mask & CWX)
161			c->x = ev->x;
162		if(ev->value_mask & CWY)
163			c->y = ev->y;
164		if(ev->value_mask & CWWidth)
165			c->w = ev->width;
166		if(ev->value_mask & CWHeight)
167			c->h = ev->height;
168		if(ev->value_mask & CWBorderWidth)
169			c->border = 1;
170		gravitate(c, False);
171		resize(c, True, TopLeft);
172	}
173
174	wc.x = ev->x;
175	wc.y = ev->y;
176	wc.width = ev->width;
177	wc.height = ev->height;
178	wc.border_width = 1;
179	wc.sibling = None;
180	wc.stack_mode = Above;
181	ev->value_mask &= ~CWStackMode;
182	ev->value_mask |= CWBorderWidth;
183	XConfigureWindow(dpy, ev->window, ev->value_mask, &wc);
184	XSync(dpy, False);
185}
186
187static void
188destroynotify(XEvent *e)
189{
190	Client *c;
191	XDestroyWindowEvent *ev = &e->xdestroywindow;
192
193	if((c = getclient(ev->window)))
194		unmanage(c);
195}
196
197static void
198enternotify(XEvent *e)
199{
200	Client *c;
201	XCrossingEvent *ev = &e->xcrossing;
202
203	if(ev->detail == NotifyInferior)
204		return;
205
206	if((c = getclient(ev->window)))
207		focus(c);
208	else if(ev->window == root)
209		issel = True;
210}
211
212static void
213expose(XEvent *e)
214{
215	Client *c;
216	XExposeEvent *ev = &e->xexpose;
217
218	if(ev->count == 0) {
219		if(barwin == ev->window)
220			drawstatus();
221		else if((c = getctitle(ev->window)))
222			drawtitle(c);
223	}
224}
225
226static void
227keypress(XEvent *e)
228{
229	static unsigned int len = sizeof(key) / sizeof(key[0]);
230	unsigned int i;
231	KeySym keysym;
232	XKeyEvent *ev = &e->xkey;
233	ev->state &= valid_mask;
234
235	keysym = XKeycodeToKeysym(dpy, (KeyCode)ev->keycode, 0);
236	for(i = 0; i < len; i++)
237		if((keysym == key[i].keysym) && ((key[i].mod & valid_mask) == ev->state)) {
238			if(key[i].func)
239				key[i].func(&key[i].arg);
240			return;
241		}
242}
243
244static void
245leavenotify(XEvent *e)
246{
247	XCrossingEvent *ev = &e->xcrossing;
248
249	if((ev->window == root) && !ev->same_screen)
250		issel = True;
251}
252
253static void
254maprequest(XEvent *e)
255{
256	static XWindowAttributes wa;
257	XMapRequestEvent *ev = &e->xmaprequest;
258
259	if(!XGetWindowAttributes(dpy, ev->window, &wa))
260		return;
261
262	if(wa.override_redirect) {
263		XSelectInput(dpy, ev->window,
264				(StructureNotifyMask | PropertyChangeMask));
265		return;
266	}
267
268	if(!getclient(ev->window))
269		manage(ev->window, &wa);
270}
271
272static void
273propertynotify(XEvent *e)
274{
275	Client *c;
276	Window trans;
277	XPropertyEvent *ev = &e->xproperty;
278
279	if(ev->state == PropertyDelete)
280		return; /* ignore */
281
282	if((c = getclient(ev->window))) {
283		if(ev->atom == wmatom[WMProtocols]) {
284			c->proto = getproto(c->win);
285			return;
286		}
287		switch (ev->atom) {
288			default: break;
289			case XA_WM_TRANSIENT_FOR:
290				XGetTransientForHint(dpy, c->win, &trans);
291				if(!c->isfloat && (c->isfloat = (trans != 0)))
292					arrange(NULL);
293				break;
294			case XA_WM_NORMAL_HINTS:
295				setsize(c);
296				break;
297		}
298		if(ev->atom == XA_WM_NAME || ev->atom == netatom[NetWMName]) {
299			settitle(c);
300			drawtitle(c);
301		}
302	}
303}
304
305static void
306unmapnotify(XEvent *e)
307{
308	Client *c;
309	XUnmapEvent *ev = &e->xunmap;
310
311	if((c = getclient(ev->window)))
312		unmanage(c);
313}
314
315/* extern */
316
317void (*handler[LASTEvent]) (XEvent *) = {
318	[ButtonPress] = buttonpress,
319	[ConfigureRequest] = configurerequest,
320	[DestroyNotify] = destroynotify,
321	[EnterNotify] = enternotify,
322	[LeaveNotify] = leavenotify,
323	[Expose] = expose,
324	[KeyPress] = keypress,
325	[MapRequest] = maprequest,
326	[PropertyNotify] = propertynotify,
327	[UnmapNotify] = unmapnotify
328};
329
330void
331grabkeys()
332{
333	static unsigned int len = sizeof(key) / sizeof(key[0]);
334	unsigned int i;
335	KeyCode code;
336
337	for(i = 0; i < len; i++) {
338		code = XKeysymToKeycode(dpy, key[i].keysym);
339		XUngrabKey(dpy, code, key[i].mod, root);
340		XUngrabKey(dpy, code, key[i].mod | NUMLOCKMASK, root);
341		XUngrabKey(dpy, code, key[i].mod | NUMLOCKMASK | LockMask, root);
342		XGrabKey(dpy, code, key[i].mod, root, True,
343				GrabModeAsync, GrabModeAsync);
344		XGrabKey(dpy, code, key[i].mod | NUMLOCKMASK, root, True,
345				GrabModeAsync, GrabModeAsync);
346		XGrabKey(dpy, code, key[i].mod | NUMLOCKMASK | LockMask, root, True,
347				GrabModeAsync, GrabModeAsync);
348	}
349}