all repos — dwm @ 7d7cde0fd6ceae2f7d759b924afd8df279a4ddde

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