all repos — dwm @ 4688ad181da14be36e034918580ec0ce5968ffdb

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,		togglemax,	{ 0 } }, 
 44	{ MODKEY,		XK_space,	togglemode,	{ 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_w,		spawn,		{ .argv = browse } },
 59	{ MODKEY|ShiftMask,	XK_Return,	spawn,		{ .argv = term } },
 60};
 61
 62/* static */
 63
 64static void
 65movemouse(Client *c)
 66{
 67	int x1, y1, ocx, ocy, di;
 68	unsigned int dui;
 69	Window dummy;
 70	XEvent ev;
 71
 72	ocx = c->x;
 73	ocy = c->y;
 74	if(XGrabPointer(dpy, root, False, MouseMask, GrabModeAsync, GrabModeAsync,
 75			None, cursor[CurMove], CurrentTime) != GrabSuccess)
 76		return;
 77	XQueryPointer(dpy, root, &dummy, &dummy, &x1, &y1, &di, &di, &dui);
 78	for(;;) {
 79		XMaskEvent(dpy, MouseMask | ExposureMask, &ev);
 80		switch (ev.type) {
 81		default: break;
 82		case Expose:
 83			handler[Expose](&ev);
 84			break;
 85		case MotionNotify:
 86			XSync(dpy, False);
 87			c->x = ocx + (ev.xmotion.x - x1);
 88			c->y = ocy + (ev.xmotion.y - y1);
 89			resize(c, False, TopLeft);
 90			break;
 91		case ButtonRelease:
 92			XUngrabPointer(dpy, CurrentTime);
 93			return;
 94		}
 95	}
 96}
 97
 98static void
 99resizemouse(Client *c)
100{
101	int ocx, ocy;
102	Corner sticky;
103	XEvent ev;
104
105	ocx = c->x;
106	ocy = c->y;
107	if(XGrabPointer(dpy, root, False, MouseMask, GrabModeAsync, GrabModeAsync,
108				None, cursor[CurResize], CurrentTime) != GrabSuccess)
109		return;
110	XWarpPointer(dpy, None, c->win, 0, 0, 0, 0, c->w, c->h);
111	for(;;) {
112		XMaskEvent(dpy, MouseMask | ExposureMask, &ev);
113		switch(ev.type) {
114		default: break;
115		case Expose:
116			handler[Expose](&ev);
117			break;
118		case MotionNotify:
119			XSync(dpy, False);
120			c->w = abs(ocx - ev.xmotion.x);
121			c->h = abs(ocy - ev.xmotion.y);
122			c->x = (ocx <= ev.xmotion.x) ? ocx : ocx - c->w;
123			c->y = (ocy <= ev.xmotion.y) ? ocy : ocy - c->h;
124			if(ocx <= ev.xmotion.x)
125				sticky = (ocy <= ev.xmotion.y) ? TopLeft : BotLeft;
126			else
127				sticky = (ocy <= ev.xmotion.y) ? TopRight : BotRight;
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	Client *c;
143	XButtonPressedEvent *ev = &e->xbutton;
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 == dofloat || c->isfloat) {
173				higher(c);
174				movemouse(c);
175			}
176			break;
177		case Button2:
178			lower(c);
179			break;
180		case Button3:
181			if(arrange == dofloat || c->isfloat) {
182				higher(c);
183				resizemouse(c);
184			}
185			break;
186		}
187	}
188}
189
190static void
191configurerequest(XEvent *e)
192{
193	Client *c;
194	XConfigureRequestEvent *ev = &e->xconfigurerequest;
195	XWindowChanges wc;
196
197	ev->value_mask &= ~CWSibling;
198	if((c = getclient(ev->window))) {
199		gravitate(c, True);
200		if(ev->value_mask & CWX)
201			c->x = ev->x;
202		if(ev->value_mask & CWY)
203			c->y = ev->y;
204		if(ev->value_mask & CWWidth)
205			c->w = ev->width;
206		if(ev->value_mask & CWHeight)
207			c->h = ev->height;
208		if(ev->value_mask & CWBorderWidth)
209			c->border = 1;
210		gravitate(c, False);
211		resize(c, True, TopLeft);
212	}
213
214	wc.x = ev->x;
215	wc.y = ev->y;
216	wc.width = ev->width;
217	wc.height = ev->height;
218	wc.border_width = 1;
219	wc.sibling = None;
220	wc.stack_mode = Above;
221	ev->value_mask &= ~CWStackMode;
222	ev->value_mask |= CWBorderWidth;
223	XConfigureWindow(dpy, ev->window, ev->value_mask, &wc);
224	XSync(dpy, False);
225}
226
227static void
228destroynotify(XEvent *e)
229{
230	Client *c;
231	XDestroyWindowEvent *ev = &e->xdestroywindow;
232
233	if((c = getclient(ev->window)))
234		unmanage(c);
235}
236
237static void
238enternotify(XEvent *e)
239{
240	Client *c;
241	XCrossingEvent *ev = &e->xcrossing;
242
243	if(ev->mode != NotifyNormal || ev->detail == NotifyInferior)
244		return;
245
246	if((c = getclient(ev->window)))
247		focus(c);
248	else if(ev->window == root)
249		issel = True;
250}
251
252static void
253expose(XEvent *e)
254{
255	Client *c;
256	XExposeEvent *ev = &e->xexpose;
257
258	if(ev->count == 0) {
259		if(barwin == ev->window)
260			drawstatus();
261		else if((c = getctitle(ev->window)))
262			drawtitle(c);
263	}
264}
265
266static void
267keypress(XEvent *e)
268{
269	static unsigned int len = key ? sizeof(key) / sizeof(key[0]) : 0;
270	unsigned int i;
271	KeySym keysym;
272	XKeyEvent *ev = &e->xkey;
273
274	keysym = XKeycodeToKeysym(dpy, (KeyCode)ev->keycode, 0);
275	for(i = 0; i < len; i++)
276		if((keysym == key[i].keysym) && (key[i].mod == ev->state)) {
277			if(key[i].func)
278				key[i].func(&key[i].arg);
279			return;
280		}
281}
282
283static void
284leavenotify(XEvent *e)
285{
286	XCrossingEvent *ev = &e->xcrossing;
287
288	if((ev->window == root) && !ev->same_screen)
289		issel = True;
290}
291
292static void
293maprequest(XEvent *e)
294{
295	static XWindowAttributes wa;
296	XMapRequestEvent *ev = &e->xmaprequest;
297
298	if(!XGetWindowAttributes(dpy, ev->window, &wa))
299		return;
300
301	if(wa.override_redirect) {
302		XSelectInput(dpy, ev->window,
303				(StructureNotifyMask | PropertyChangeMask));
304		return;
305	}
306
307	if(!getclient(ev->window))
308		manage(ev->window, &wa);
309}
310
311static void
312propertynotify(XEvent *e)
313{
314	Client *c;
315	Window trans;
316	XPropertyEvent *ev = &e->xproperty;
317
318	if(ev->state == PropertyDelete)
319		return; /* ignore */
320
321	if((c = getclient(ev->window))) {
322		if(ev->atom == wmatom[WMProtocols]) {
323			c->proto = getproto(c->win);
324			return;
325		}
326		switch (ev->atom) {
327			default: break;
328			case XA_WM_TRANSIENT_FOR:
329				XGetTransientForHint(dpy, c->win, &trans);
330				if(!c->isfloat && (c->isfloat = (trans != 0)))
331					arrange(NULL);
332				break;
333			case XA_WM_NORMAL_HINTS:
334				setsize(c);
335				break;
336		}
337		if(ev->atom == XA_WM_NAME || ev->atom == netatom[NetWMName]) {
338			settitle(c);
339			drawtitle(c);
340		}
341	}
342}
343
344static void
345unmapnotify(XEvent *e)
346{
347	Client *c;
348	XUnmapEvent *ev = &e->xunmap;
349
350	if((c = getclient(ev->window)))
351		unmanage(c);
352}
353
354/* extern */
355
356void (*handler[LASTEvent]) (XEvent *) = {
357	[ButtonPress] = buttonpress,
358	[ConfigureRequest] = configurerequest,
359	[DestroyNotify] = destroynotify,
360	[EnterNotify] = enternotify,
361	[LeaveNotify] = leavenotify,
362	[Expose] = expose,
363	[KeyPress] = keypress,
364	[MapRequest] = maprequest,
365	[PropertyNotify] = propertynotify,
366	[UnmapNotify] = unmapnotify
367};
368
369void
370grabkeys()
371{
372	static unsigned int len = key ? sizeof(key) / sizeof(key[0]) : 0;
373	unsigned int i;
374	KeyCode code;
375
376	for(i = 0; i < len; i++) {
377		code = XKeysymToKeycode(dpy, key[i].keysym);
378		XUngrabKey(dpy, code, key[i].mod, root);
379		XGrabKey(dpy, code, key[i].mod, root, True,
380				GrabModeAsync, GrabModeAsync);
381	}
382}