all repos — dwm @ 91e569ca37ac441930f041057d2934f02bf263a2

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