all repos — dwm @ 6cca3999c851770658d9223122f3789af1e2f8cd

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
 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			resize(c, False, TopLeft);
 52			break;
 53		}
 54	}
 55}
 56
 57static void
 58resizemouse(Client *c) {
 59	int ocx, ocy;
 60	int nw, nh;
 61	Corner sticky;
 62	XEvent ev;
 63
 64	ocx = c->x;
 65	ocy = c->y;
 66	if(XGrabPointer(dpy, root, False, MOUSEMASK, GrabModeAsync, GrabModeAsync,
 67				None, cursor[CurResize], CurrentTime) != GrabSuccess)
 68		return;
 69	c->ismax = False;
 70	XWarpPointer(dpy, None, c->win, 0, 0, 0, 0, c->w, c->h);
 71	for(;;) {
 72		XMaskEvent(dpy, MOUSEMASK | ExposureMask, &ev);
 73		switch(ev.type) {
 74		case ButtonRelease:
 75			resize(c, True, TopLeft);
 76			XUngrabPointer(dpy, CurrentTime);
 77			return;
 78		case Expose:
 79			handler[Expose](&ev);
 80			break;
 81		case MotionNotify:
 82			XSync(dpy, False);
 83			if((nw = abs(ocx - ev.xmotion.x)))
 84				c->w = nw;
 85			if((nh = abs(ocy - ev.xmotion.y)))
 86				c->h = nh;
 87			c->x = (ocx <= ev.xmotion.x) ? ocx : ocx - c->w;
 88			c->y = (ocy <= ev.xmotion.y) ? ocy : ocy - c->h;
 89			if(ocx <= ev.xmotion.x)
 90				sticky = (ocy <= ev.xmotion.y) ? TopLeft : BotLeft;
 91			else
 92				sticky = (ocy <= ev.xmotion.y) ? TopRight : BotRight;
 93			resize(c, True, sticky);
 94			break;
 95		}
 96	}
 97}
 98
 99static void
100buttonpress(XEvent *e) {
101	int x;
102	Arg a;
103	Client *c;
104	XButtonPressedEvent *ev = &e->xbutton;
105
106	if(barwin == ev->window) {
107		x = 0;
108		for(a.i = 0; a.i < ntags; a.i++) {
109			x += textw(tags[a.i]);
110			if(ev->x < x) {
111				if(ev->button == Button1) {
112					if(ev->state & MODKEY)
113						tag(&a);
114					else
115						view(&a);
116				}
117				else if(ev->button == Button3) {
118					if(ev->state & MODKEY)
119						toggletag(&a);
120					else
121						toggleview(&a);
122				}
123				return;
124			}
125		}
126		if(ev->x < x + bmw) {
127			if(ev->button == Button1)
128				togglemode(NULL);
129			else if(ev->button == Button3)
130				togglestackpos(NULL);
131		}
132	}
133	else if((c = getclient(ev->window))) {
134		focus(c);
135		if(CLEANMASK(ev->state) != MODKEY)
136			return;
137		if(ev->button == Button1 && (arrange == dofloat || c->isfloat)) {
138			restack();
139			movemouse(c);
140		}
141		else if(ev->button == Button2)
142			zoom(NULL);
143		else if(ev->button == Button3 && (arrange == dofloat || c->isfloat)) {
144			restack();
145			resizemouse(c);
146		}
147	}
148}
149
150static void
151configurerequest(XEvent *e) {
152	unsigned long newmask;
153	Client *c;
154	XConfigureRequestEvent *ev = &e->xconfigurerequest;
155	XWindowChanges wc;
156
157	if((c = getclient(ev->window))) {
158		c->ismax = False;
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 = ev->border_width;
170		gravitate(c, False);
171		wc.x = c->x;
172		wc.y = c->y;
173		wc.width = c->w;
174		wc.height = c->h;
175		newmask = ev->value_mask & (~(CWSibling | CWStackMode | CWBorderWidth));
176		if(newmask)
177			XConfigureWindow(dpy, c->win, newmask, &wc);
178		else
179			configure(c);
180		XSync(dpy, False);
181		if(c->isfloat) {
182			resize(c, False, TopLeft);
183			if(!isvisible(c))
184				ban(c);
185		}
186		else
187			arrange(NULL);
188	}
189	else {
190		wc.x = ev->x;
191		wc.y = ev->y;
192		wc.width = ev->width;
193		wc.height = ev->height;
194		wc.border_width = ev->border_width;
195		wc.sibling = ev->above;
196		wc.stack_mode = ev->detail;
197		XConfigureWindow(dpy, ev->window, ev->value_mask, &wc);
198		XSync(dpy, False);
199	}
200}
201
202static void
203destroynotify(XEvent *e) {
204	Client *c;
205	XDestroyWindowEvent *ev = &e->xdestroywindow;
206
207	if((c = getclient(ev->window)))
208		unmanage(c);
209}
210
211static void
212enternotify(XEvent *e) {
213	Client *c;
214	XCrossingEvent *ev = &e->xcrossing;
215
216	if(ev->mode != NotifyNormal || ev->detail == NotifyInferior)
217		return;
218
219	if(((c = getclient(ev->window)) || (c = getctitle(ev->window))) && isvisible(c))
220		focus(c);
221	else if(ev->window == root) {
222		issel = True;
223		XSetInputFocus(dpy, root, RevertToPointerRoot, CurrentTime);
224		drawall();
225	}
226}
227
228static void
229expose(XEvent *e) {
230	Client *c;
231	XExposeEvent *ev = &e->xexpose;
232
233	if(ev->count == 0) {
234		if(barwin == ev->window)
235			drawstatus();
236		else if((c = getctitle(ev->window)))
237			drawtitle(c);
238	}
239}
240
241static void
242keypress(XEvent *e) {
243	static unsigned int len = sizeof(key) / sizeof(key[0]);
244	unsigned int i;
245	KeySym keysym;
246	XKeyEvent *ev = &e->xkey;
247
248	keysym = XKeycodeToKeysym(dpy, (KeyCode)ev->keycode, 0);
249	for(i = 0; i < len; i++) {
250		if(keysym == key[i].keysym
251			&& CLEANMASK(key[i].mod) == CLEANMASK(ev->state))
252		{
253			if(key[i].func)
254				key[i].func(&key[i].arg);
255			return;
256		}
257	}
258}
259
260static void
261leavenotify(XEvent *e) {
262	XCrossingEvent *ev = &e->xcrossing;
263
264	if((ev->window == root) && !ev->same_screen) {
265		issel = False;
266		drawall();
267	}
268}
269
270static void
271mappingnotify(XEvent *e) {
272	XMappingEvent *ev = &e->xmapping;
273
274	XRefreshKeyboardMapping(ev);
275	if(ev->request == MappingKeyboard)
276		grabkeys();
277}
278
279static void
280maprequest(XEvent *e) {
281	static XWindowAttributes wa;
282	XMapRequestEvent *ev = &e->xmaprequest;
283
284	if(!XGetWindowAttributes(dpy, ev->window, &wa))
285		return;
286
287	if(wa.override_redirect) {
288		XSelectInput(dpy, ev->window,
289				(StructureNotifyMask | PropertyChangeMask));
290		return;
291	}
292
293	if(!getclient(ev->window))
294		manage(ev->window, &wa);
295}
296
297static void
298propertynotify(XEvent *e) {
299	Client *c;
300	Window trans;
301	XPropertyEvent *ev = &e->xproperty;
302
303	if(ev->state == PropertyDelete)
304		return; /* ignore */
305
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(NULL);
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}