all repos — dwm @ 7b5638f61d5c8b5a76bc3f7a5962cb7490da3b6b

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