all repos — dwm @ 79cd408844c62963aa0eec45bb0414ed66f06f6f

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
  7#include <stdlib.h>
  8#include <X11/keysym.h>
  9#include <X11/Xatom.h>
 10
 11#define ButtonMask      (ButtonPressMask | ButtonReleaseMask)
 12#define MouseMask       (ButtonMask | PointerMotionMask)
 13
 14/* CUSTOMIZE */
 15const char *browse[] = { "firefox", NULL };
 16const char *gimp[] = { "gimp", NULL };
 17const char *term[] = { 
 18	"urxvtc", "-tr", "+sb", "-bg", "black", "-fg", "white", "-cr", "white",
 19	"-fn", "-*-terminus-medium-*-*-*-13-*-*-*-*-*-iso10646-*", NULL
 20};
 21const char *xlock[] = { "xlock", NULL };
 22
 23Key key[] = {
 24	/* modifier				key			function	arguments */
 25	{ ControlMask,			XK_0,		appendtag,	{ .i = Tscratch } }, 
 26	{ ControlMask,			XK_1,		appendtag,	{ .i = Tdev } }, 
 27	{ ControlMask,			XK_2,		appendtag,	{ .i = Twww } }, 
 28	{ ControlMask,			XK_3,		appendtag,	{ .i = Twork } }, 
 29	{ Mod1Mask,				XK_0,		view,		{ .i = Tscratch } }, 
 30	{ Mod1Mask,				XK_1,		view,		{ .i = Tdev } }, 
 31	{ Mod1Mask,				XK_2,		view,		{ .i = Twww } }, 
 32	{ Mod1Mask,				XK_3,		view,		{ .i = Twork } }, 
 33	{ Mod1Mask,				XK_j,		focusnext,		{ 0 } }, 
 34	{ Mod1Mask,				XK_k,		focusprev,		{ 0 } },
 35	{ Mod1Mask,				XK_m,		maximize,		{ 0 } }, 
 36	{ Mod1Mask,				XK_space,	dotile,		{ 0 } }, 
 37	{ Mod1Mask,				XK_Return,	zoom,		{ 0 } },
 38	{ ControlMask|ShiftMask,XK_0,		heretag,	{ .i = Tscratch } }, 
 39	{ ControlMask|ShiftMask,XK_1,		heretag,	{ .i = Tdev } }, 
 40	{ ControlMask|ShiftMask,XK_2,		heretag,	{ .i = Twww } }, 
 41	{ ControlMask|ShiftMask,XK_3,		heretag,	{ .i = Twork } }, 
 42	{ Mod1Mask|ShiftMask,	XK_0,		replacetag,		{ .i = Tscratch } }, 
 43	{ Mod1Mask|ShiftMask,	XK_1,		replacetag,		{ .i = Tdev } }, 
 44	{ Mod1Mask|ShiftMask,	XK_2,		replacetag,		{ .i = Twww } }, 
 45	{ Mod1Mask|ShiftMask,	XK_3,		replacetag,		{ .i = Twork } }, 
 46	{ Mod1Mask|ShiftMask,	XK_c,		killclient,		{ 0 } }, 
 47	{ Mod1Mask|ShiftMask,	XK_g,		spawn,		{ .argv = gimp } },
 48	{ Mod1Mask|ShiftMask,	XK_l,		spawn,		{ .argv = xlock } },
 49	{ Mod1Mask|ShiftMask,	XK_q,		quit,		{ 0 } },
 50	{ Mod1Mask|ShiftMask,	XK_space,	dofloat,	{ 0 } }, 
 51	{ Mod1Mask|ShiftMask,	XK_w,		spawn,		{ .argv = browse } },
 52	{ Mod1Mask|ShiftMask,	XK_Return,	spawn,		{ .argv = term } },
 53};
 54
 55/* static */
 56
 57static void
 58movemouse(Client *c)
 59{
 60	XEvent ev;
 61	int x1, y1, ocx, ocy, di;
 62	unsigned int dui;
 63	Window dummy;
 64
 65	ocx = *c->x;
 66	ocy = *c->y;
 67	if(XGrabPointer(dpy, root, False, MouseMask, GrabModeAsync, GrabModeAsync,
 68				None, cursor[CurMove], CurrentTime) != GrabSuccess)
 69		return;
 70	XQueryPointer(dpy, root, &dummy, &dummy, &x1, &y1, &di, &di, &dui);
 71	for(;;) {
 72		XMaskEvent(dpy, MouseMask | ExposureMask, &ev);
 73		switch (ev.type) {
 74		default: break;
 75		case Expose:
 76			handler[Expose](&ev);
 77			break;
 78		case MotionNotify:
 79			XSync(dpy, False);
 80			*c->x = ocx + (ev.xmotion.x - x1);
 81			*c->y = ocy + (ev.xmotion.y - y1);
 82			resize(c, False, TopLeft);
 83			break;
 84		case ButtonRelease:
 85			XUngrabPointer(dpy, CurrentTime);
 86			return;
 87		}
 88	}
 89}
 90
 91static void
 92resizemouse(Client *c)
 93{
 94	XEvent ev;
 95	int ocx, ocy;
 96	Corner sticky;
 97
 98	ocx = *c->x;
 99	ocy = *c->y;
100	if(XGrabPointer(dpy, root, False, MouseMask, GrabModeAsync, GrabModeAsync,
101				None, cursor[CurResize], CurrentTime) != GrabSuccess)
102		return;
103	XWarpPointer(dpy, None, c->win, 0, 0, 0, 0, *c->w, *c->h);
104	for(;;) {
105		XMaskEvent(dpy, MouseMask | ExposureMask, &ev);
106		switch(ev.type) {
107		default: break;
108		case Expose:
109			handler[Expose](&ev);
110			break;
111		case MotionNotify:
112			XSync(dpy, False);
113			*c->w = abs(ocx - ev.xmotion.x);
114			*c->h = abs(ocy - ev.xmotion.y);
115			*c->x = (ocx <= ev.xmotion.x) ? ocx : ocx - *c->w;
116			*c->y = (ocy <= ev.xmotion.y) ? ocy : ocy - *c->h;
117			if(ocx <= ev.xmotion.x) {
118				if(ocy <= ev.xmotion.y)
119					sticky = TopLeft;
120				else
121					sticky = BottomLeft;
122			} else {
123				if(ocy <= ev.xmotion.y)
124					sticky = TopRight;
125				else
126					sticky = BottomRight;
127			}
128			resize(c, True, sticky);
129			break;
130		case ButtonRelease:
131			XUngrabPointer(dpy, CurrentTime);
132			return;
133		}
134	}
135}
136
137static void
138buttonpress(XEvent *e)
139{
140	int x;
141	Arg a;
142	XButtonPressedEvent *ev = &e->xbutton;
143	Client *c;
144
145	if(barwin == ev->window) {
146		switch(ev->button) {
147		default:
148			x = 0;
149			for(a.i = 0; a.i < TLast; a.i++) {
150				x += textw(tags[a.i]);
151				if(ev->x < x) {
152					view(&a);
153					break;
154				}
155			}
156			break;
157		case Button4:
158			a.i = (tsel + 1 < TLast) ? tsel + 1 : 0;
159			view(&a);
160			break;
161		case Button5:
162			a.i = (tsel - 1 >= 0) ? tsel - 1 : TLast - 1;
163			view(&a);
164			break;
165		}
166	}
167	else if((c = getclient(ev->window))) {
168		switch(ev->button) {
169		default:
170			break;
171		case Button1:
172			if(arrange == dotile && !c->isfloat) {
173				if((ev->state & ControlMask) && (ev->button == Button1))
174					zoom(NULL);
175			}
176			else {
177				higher(c);
178				movemouse(c);
179			}
180			break;
181		case Button2:
182			lower(c);
183			break;
184		case Button3:
185			if(arrange == dofloat || c->isfloat) {
186				higher(c);
187				resizemouse(c);
188			}
189			break;
190		}
191	}
192}
193
194static void
195configurerequest(XEvent *e)
196{
197	XConfigureRequestEvent *ev = &e->xconfigurerequest;
198	XWindowChanges wc;
199	Client *c;
200
201	ev->value_mask &= ~CWSibling;
202	if((c = getclient(ev->window))) {
203		gravitate(c, True);
204		if(ev->value_mask & CWX)
205			*c->x = ev->x;
206		if(ev->value_mask & CWY)
207			*c->y = ev->y;
208		if(ev->value_mask & CWWidth)
209			*c->w = ev->width;
210		if(ev->value_mask & CWHeight)
211			*c->h = ev->height;
212		if(ev->value_mask & CWBorderWidth)
213			c->border = 1;
214		gravitate(c, False);
215		resize(c, True, TopLeft);
216	}
217
218	wc.x = ev->x;
219	wc.y = ev->y;
220	wc.width = ev->width;
221	wc.height = ev->height;
222	wc.border_width = 1;
223	wc.sibling = None;
224	wc.stack_mode = Above;
225	ev->value_mask &= ~CWStackMode;
226	ev->value_mask |= CWBorderWidth;
227	XConfigureWindow(dpy, ev->window, ev->value_mask, &wc);
228	XSync(dpy, False);
229}
230
231static void
232destroynotify(XEvent *e)
233{
234	Client *c;
235	XDestroyWindowEvent *ev = &e->xdestroywindow;
236
237	if((c = getclient(ev->window)))
238		unmanage(c);
239}
240
241static void
242enternotify(XEvent *e)
243{
244	XCrossingEvent *ev = &e->xcrossing;
245	Client *c;
246
247	if(ev->mode != NotifyNormal || ev->detail == NotifyInferior)
248		return;
249
250	if((c = getclient(ev->window)))
251		focus(c);
252	else if(ev->window == root)
253		issel = True;
254}
255
256static void
257expose(XEvent *e)
258{
259	XExposeEvent *ev = &e->xexpose;
260	Client *c;
261
262	if(ev->count == 0) {
263		if(barwin == ev->window)
264			drawstatus();
265		else if((c = getctitle(ev->window)))
266			drawtitle(c);
267	}
268}
269
270static void
271keypress(XEvent *e)
272{
273	XKeyEvent *ev = &e->xkey;
274	static unsigned int len = key ? sizeof(key) / sizeof(key[0]) : 0;
275	unsigned int i;
276	KeySym keysym;
277
278	keysym = XKeycodeToKeysym(dpy, (KeyCode)ev->keycode, 0);
279	for(i = 0; i < len; i++)
280		if((keysym == key[i].keysym) && (key[i].mod == ev->state)) {
281			if(key[i].func)
282				key[i].func(&key[i].arg);
283			return;
284		}
285}
286
287static void
288leavenotify(XEvent *e)
289{
290	XCrossingEvent *ev = &e->xcrossing;
291
292	if((ev->window == root) && !ev->same_screen)
293		issel = True;
294}
295
296static void
297maprequest(XEvent *e)
298{
299	XMapRequestEvent *ev = &e->xmaprequest;
300	static XWindowAttributes wa;
301
302	if(!XGetWindowAttributes(dpy, ev->window, &wa))
303		return;
304
305	if(wa.override_redirect) {
306		XSelectInput(dpy, ev->window,
307				(StructureNotifyMask | PropertyChangeMask));
308		return;
309	}
310
311	if(!getclient(ev->window))
312		manage(ev->window, &wa);
313}
314
315static void
316propertynotify(XEvent *e)
317{
318	XPropertyEvent *ev = &e->xproperty;
319	Window trans;
320	Client *c;
321
322	if(ev->state == PropertyDelete)
323		return; /* ignore */
324
325	if((c = getclient(ev->window))) {
326		if(ev->atom == wmatom[WMProtocols]) {
327			c->proto = getproto(c->win);
328			return;
329		}
330		switch (ev->atom) {
331			default: break;
332			case XA_WM_TRANSIENT_FOR:
333				XGetTransientForHint(dpy, c->win, &trans);
334				if(!c->isfloat && (c->isfloat = (trans != 0)))
335					arrange(NULL);
336				break;
337			case XA_WM_NORMAL_HINTS:
338				setsize(c);
339				break;
340		}
341		if(ev->atom == XA_WM_NAME || ev->atom == netatom[NetWMName]) {
342			settitle(c);
343			drawtitle(c);
344		}
345	}
346}
347
348static void
349unmapnotify(XEvent *e)
350{
351	Client *c;
352	XUnmapEvent *ev = &e->xunmap;
353
354	if((c = getclient(ev->window)))
355		unmanage(c);
356}
357
358/* extern */
359
360void (*handler[LASTEvent]) (XEvent *) = {
361	[ButtonPress] = buttonpress,
362	[ConfigureRequest] = configurerequest,
363	[DestroyNotify] = destroynotify,
364	[EnterNotify] = enternotify,
365	[LeaveNotify] = leavenotify,
366	[Expose] = expose,
367	[KeyPress] = keypress,
368	[MapRequest] = maprequest,
369	[PropertyNotify] = propertynotify,
370	[UnmapNotify] = unmapnotify
371};
372
373void
374grabkeys()
375{
376	static unsigned int len = key ? sizeof(key) / sizeof(key[0]) : 0;
377	unsigned int i;
378	KeyCode code;
379
380	for(i = 0; i < len; i++) {
381		code = XKeysymToKeycode(dpy, key[i].keysym);
382		XUngrabKey(dpy, code, key[i].mod, root);
383		XGrabKey(dpy, code, key[i].mod, root, True,
384				GrabModeAsync, GrabModeAsync);
385	}
386}