all repos — dwm @ 1802fad2f9b2a432b04f642b9e954159d1f2554f

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		}
130	}
131	else if((c = getclient(ev->window))) {
132		focus(c);
133		if(CLEANMASK(ev->state) != MODKEY)
134			return;
135		if(ev->button == Button1 && (arrange == dofloat || c->isfloat)) {
136			restack();
137			movemouse(c);
138		}
139		else if(ev->button == Button2)
140			zoom(NULL);
141		else if(ev->button == Button3 && (arrange == dofloat || c->isfloat)) {
142			restack();
143			resizemouse(c);
144		}
145	}
146}
147
148static void
149configurerequest(XEvent *e) {
150	unsigned long newmask;
151	Client *c;
152	XConfigureRequestEvent *ev = &e->xconfigurerequest;
153	XWindowChanges wc;
154
155	if((c = getclient(ev->window))) {
156		c->ismax = False;
157		gravitate(c, True);
158		if(ev->value_mask & CWX)
159			c->x = ev->x;
160		if(ev->value_mask & CWY)
161			c->y = ev->y;
162		if(ev->value_mask & CWWidth)
163			c->w = ev->width;
164		if(ev->value_mask & CWHeight)
165			c->h = ev->height;
166		if(ev->value_mask & CWBorderWidth)
167			c->border = ev->border_width;
168		gravitate(c, False);
169		wc.x = c->x;
170		wc.y = c->y;
171		wc.width = c->w;
172		wc.height = c->h;
173		newmask = ev->value_mask & (~(CWSibling | CWStackMode | CWBorderWidth));
174		if(newmask)
175			XConfigureWindow(dpy, c->win, newmask, &wc);
176		else
177			configure(c);
178		XSync(dpy, False);
179		if(c->isfloat) {
180			resize(c, False, TopLeft);
181			if(!isvisible(c))
182				ban(c);
183		}
184		else
185			arrange(NULL);
186	}
187	else {
188		wc.x = ev->x;
189		wc.y = ev->y;
190		wc.width = ev->width;
191		wc.height = ev->height;
192		wc.border_width = ev->border_width;
193		wc.sibling = ev->above;
194		wc.stack_mode = ev->detail;
195		XConfigureWindow(dpy, ev->window, ev->value_mask, &wc);
196		XSync(dpy, False);
197	}
198}
199
200static void
201destroynotify(XEvent *e) {
202	Client *c;
203	XDestroyWindowEvent *ev = &e->xdestroywindow;
204
205	if((c = getclient(ev->window)))
206		unmanage(c);
207}
208
209static void
210enternotify(XEvent *e) {
211	Client *c;
212	XCrossingEvent *ev = &e->xcrossing;
213
214	if(ev->mode != NotifyNormal || ev->detail == NotifyInferior)
215		return;
216
217	if(((c = getclient(ev->window)) || (c = getctitle(ev->window))) && isvisible(c))
218		focus(c);
219	else if(ev->window == root) {
220		issel = True;
221		XSetInputFocus(dpy, root, RevertToPointerRoot, CurrentTime);
222		drawall();
223	}
224}
225
226static void
227expose(XEvent *e) {
228	Client *c;
229	XExposeEvent *ev = &e->xexpose;
230
231	if(ev->count == 0) {
232		if(barwin == ev->window)
233			drawstatus();
234		else if((c = getctitle(ev->window)))
235			drawtitle(c);
236	}
237}
238
239static void
240keypress(XEvent *e) {
241	static unsigned int len = sizeof(key) / sizeof(key[0]);
242	unsigned int i;
243	KeySym keysym;
244	XKeyEvent *ev = &e->xkey;
245
246	keysym = XKeycodeToKeysym(dpy, (KeyCode)ev->keycode, 0);
247	for(i = 0; i < len; i++) {
248		if(keysym == key[i].keysym
249			&& CLEANMASK(key[i].mod) == CLEANMASK(ev->state))
250		{
251			if(key[i].func)
252				key[i].func(&key[i].arg);
253			return;
254		}
255	}
256}
257
258static void
259leavenotify(XEvent *e) {
260	XCrossingEvent *ev = &e->xcrossing;
261
262	if((ev->window == root) && !ev->same_screen) {
263		issel = False;
264		drawall();
265	}
266}
267
268static void
269mappingnotify(XEvent *e) {
270	XMappingEvent *ev = &e->xmapping;
271
272	XRefreshKeyboardMapping(ev);
273	if(ev->request == MappingKeyboard)
274		grabkeys();
275}
276
277static void
278maprequest(XEvent *e) {
279	static XWindowAttributes wa;
280	XMapRequestEvent *ev = &e->xmaprequest;
281
282	if(!XGetWindowAttributes(dpy, ev->window, &wa))
283		return;
284
285	if(wa.override_redirect) {
286		XSelectInput(dpy, ev->window,
287				(StructureNotifyMask | PropertyChangeMask));
288		return;
289	}
290
291	if(!getclient(ev->window))
292		manage(ev->window, &wa);
293}
294
295static void
296propertynotify(XEvent *e) {
297	Client *c;
298	Window trans;
299	XPropertyEvent *ev = &e->xproperty;
300
301	if(ev->state == PropertyDelete)
302		return; /* ignore */
303
304	if((c = getclient(ev->window))) {
305		if(ev->atom == wmatom[WMProtocols]) {
306			c->proto = getproto(c->win);
307			return;
308		}
309		switch (ev->atom) {
310			default: break;
311			case XA_WM_TRANSIENT_FOR:
312				XGetTransientForHint(dpy, c->win, &trans);
313				if(!c->isfloat && (c->isfloat = (trans != 0)))
314					arrange(NULL);
315				break;
316			case XA_WM_NORMAL_HINTS:
317				updatesize(c);
318				break;
319		}
320		if(ev->atom == XA_WM_NAME || ev->atom == netatom[NetWMName]) {
321			updatetitle(c);
322			resizetitle(c);
323			drawtitle(c);
324		}
325	}
326}
327
328static void
329unmapnotify(XEvent *e) {
330	Client *c;
331	XUnmapEvent *ev = &e->xunmap;
332
333	if((c = getclient(ev->window)))
334		unmanage(c);
335}
336
337/* extern */
338
339void (*handler[LASTEvent]) (XEvent *) = {
340	[ButtonPress] = buttonpress,
341	[ConfigureRequest] = configurerequest,
342	[DestroyNotify] = destroynotify,
343	[EnterNotify] = enternotify,
344	[LeaveNotify] = leavenotify,
345	[Expose] = expose,
346	[KeyPress] = keypress,
347	[MappingNotify] = mappingnotify,
348	[MapRequest] = maprequest,
349	[PropertyNotify] = propertynotify,
350	[UnmapNotify] = unmapnotify
351};
352
353void
354grabkeys(void) {
355	static unsigned int len = sizeof(key) / sizeof(key[0]);
356	unsigned int i;
357	KeyCode code;
358
359	XUngrabKey(dpy, AnyKey, AnyModifier, root);
360	for(i = 0; i < len; i++) {
361		code = XKeysymToKeycode(dpy, key[i].keysym);
362		XGrabKey(dpy, code, key[i].mod, root, True,
363				GrabModeAsync, GrabModeAsync);
364		XGrabKey(dpy, code, key[i].mod | LockMask, root, True,
365				GrabModeAsync, GrabModeAsync);
366		XGrabKey(dpy, code, key[i].mod | numlockmask, root, True,
367				GrabModeAsync, GrabModeAsync);
368		XGrabKey(dpy, code, key[i].mod | numlockmask | LockMask, root, True,
369				GrabModeAsync, GrabModeAsync);
370	}
371}
372
373void
374procevent(void) {
375	XEvent ev;
376
377	while(XPending(dpy)) {
378		XNextEvent(dpy, &ev);
379		if(handler[ev.type])
380			(handler[ev.type])(&ev); /* call handler */
381	}
382}