all repos — dwm @ 683dabe5e61ab0111ef72e0b12e69a637ebadb05

fork of suckless dynamic window manager

event.c (view raw)

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