all repos — dwm @ d2a4952956aa21a48ac40d7f650036682cb9d97d

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[NFUNCS])(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, j;
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			for(j = 0; j < NFUNCS; j++)
258				if(key[i].func[j])
259					key[i].func[j](&key[i].arg);
260			return;
261		}
262	}
263}
264
265static void
266leavenotify(XEvent *e) {
267	XCrossingEvent *ev = &e->xcrossing;
268
269	if((ev->window == root) && !ev->same_screen) {
270		issel = False;
271		drawall();
272	}
273}
274
275static void
276mappingnotify(XEvent *e) {
277	XMappingEvent *ev = &e->xmapping;
278
279	XRefreshKeyboardMapping(ev);
280	if(ev->request == MappingKeyboard)
281		grabkeys();
282}
283
284static void
285maprequest(XEvent *e) {
286	static XWindowAttributes wa;
287	XMapRequestEvent *ev = &e->xmaprequest;
288
289	if(!XGetWindowAttributes(dpy, ev->window, &wa))
290		return;
291	if(wa.override_redirect) {
292		XSelectInput(dpy, ev->window,
293				(StructureNotifyMask | PropertyChangeMask));
294		return;
295	}
296	if(!getclient(ev->window))
297		manage(ev->window, &wa);
298}
299
300static void
301propertynotify(XEvent *e) {
302	Client *c;
303	Window trans;
304	XPropertyEvent *ev = &e->xproperty;
305
306	if(ev->state == PropertyDelete)
307		return; /* ignore */
308	if((c = getclient(ev->window))) {
309		if(ev->atom == wmatom[WMProtocols]) {
310			c->proto = getproto(c->win);
311			return;
312		}
313		switch (ev->atom) {
314			default: break;
315			case XA_WM_TRANSIENT_FOR:
316				XGetTransientForHint(dpy, c->win, &trans);
317				if(!c->isfloat && (c->isfloat = (trans != 0)))
318					arrange();
319				break;
320			case XA_WM_NORMAL_HINTS:
321				updatesize(c);
322				break;
323		}
324		if(ev->atom == XA_WM_NAME || ev->atom == netatom[NetWMName]) {
325			updatetitle(c);
326			resizetitle(c);
327			drawtitle(c);
328		}
329	}
330}
331
332static void
333unmapnotify(XEvent *e) {
334	Client *c;
335	XUnmapEvent *ev = &e->xunmap;
336
337	if((c = getclient(ev->window)))
338		unmanage(c);
339}
340
341/* extern */
342
343void (*handler[LASTEvent]) (XEvent *) = {
344	[ButtonPress] = buttonpress,
345	[ConfigureRequest] = configurerequest,
346	[DestroyNotify] = destroynotify,
347	[EnterNotify] = enternotify,
348	[LeaveNotify] = leavenotify,
349	[Expose] = expose,
350	[KeyPress] = keypress,
351	[MappingNotify] = mappingnotify,
352	[MapRequest] = maprequest,
353	[PropertyNotify] = propertynotify,
354	[UnmapNotify] = unmapnotify
355};
356
357void
358grabkeys(void) {
359	static unsigned int len = sizeof key / sizeof key[0];
360	unsigned int i;
361	KeyCode code;
362
363	XUngrabKey(dpy, AnyKey, AnyModifier, root);
364	for(i = 0; i < len; i++) {
365		code = XKeysymToKeycode(dpy, key[i].keysym);
366		XGrabKey(dpy, code, key[i].mod, root, True,
367				GrabModeAsync, GrabModeAsync);
368		XGrabKey(dpy, code, key[i].mod | LockMask, root, True,
369				GrabModeAsync, GrabModeAsync);
370		XGrabKey(dpy, code, key[i].mod | numlockmask, root, True,
371				GrabModeAsync, GrabModeAsync);
372		XGrabKey(dpy, code, key[i].mod | numlockmask | LockMask, root, True,
373				GrabModeAsync, GrabModeAsync);
374	}
375}
376
377void
378procevent(void) {
379	XEvent ev;
380
381	while(XPending(dpy)) {
382		XNextEvent(dpy, &ev);
383		if(handler[ev.type])
384			(handler[ev.type])(&ev); /* call handler */
385	}
386}