all repos — dwm @ 77f8c075c48e510e064b8f0b7b823a7e1f9f44b7

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