all repos — dwm @ dc5d967ee61046f899b3b49daeb9268c8161844a

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