all repos — dwm @ 0e5c8198bc5a69e87b0114b81d6569188828edfa

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
  6#include <fcntl.h>
  7#include <stdlib.h>
  8#include <string.h>
  9#include <X11/keysym.h>
 10#include <X11/Xatom.h>
 11
 12#include "dwm.h"
 13
 14/* local functions */
 15static void buttonpress(XEvent *e);
 16static void configurerequest(XEvent *e);
 17static void destroynotify(XEvent *e);
 18static void enternotify(XEvent *e);
 19static void leavenotify(XEvent *e);
 20static void expose(XEvent *e);
 21static void keymapnotify(XEvent *e);
 22static void maprequest(XEvent *e);
 23static void propertynotify(XEvent *e);
 24static void unmapnotify(XEvent *e);
 25
 26void (*handler[LASTEvent]) (XEvent *) = {
 27	[ButtonPress] = buttonpress,
 28	[ConfigureRequest] = configurerequest,
 29	[DestroyNotify] = destroynotify,
 30	[EnterNotify] = enternotify,
 31	[LeaveNotify] = leavenotify,
 32	[Expose] = expose,
 33	[KeyPress] = keypress,
 34	[KeymapNotify] = keymapnotify,
 35	[MapRequest] = maprequest,
 36	[PropertyNotify] = propertynotify,
 37	[UnmapNotify] = unmapnotify
 38};
 39
 40void
 41discard_events(long even_mask)
 42{
 43	XEvent ev;
 44	while(XCheckMaskEvent(dpy, even_mask, &ev));
 45}
 46
 47static void
 48buttonpress(XEvent *e)
 49{
 50	XButtonPressedEvent *ev = &e->xbutton;
 51	Client *c;
 52
 53	if(barwin == ev->window)
 54		barclick(ev);
 55	else if((c = getclient(ev->window))) {
 56		craise(c);
 57		switch(ev->button) {
 58		default:
 59			break;
 60		case Button1:
 61			mmove(c);
 62			break;
 63		case Button2:
 64			lower(c);
 65			break;
 66		case Button3:
 67			mresize(c);
 68			break;
 69		}
 70	}
 71}
 72
 73static void
 74configurerequest(XEvent *e)
 75{
 76	XConfigureRequestEvent *ev = &e->xconfigurerequest;
 77	XWindowChanges wc;
 78	Client *c;
 79
 80	ev->value_mask &= ~CWSibling;
 81	if((c = getclient(ev->window))) {
 82		gravitate(c, True);
 83		if(ev->value_mask & CWX)
 84			c->x = ev->x;
 85		if(ev->value_mask & CWY)
 86			c->y = ev->y;
 87		if(ev->value_mask & CWWidth)
 88			c->w = ev->width;
 89		if(ev->value_mask & CWHeight)
 90			c->h = ev->height;
 91		if(ev->value_mask & CWBorderWidth)
 92			c->border = 1;
 93		gravitate(c, False);
 94		resize(c, True);
 95	}
 96
 97	wc.x = ev->x;
 98	wc.y = ev->y;
 99	wc.width = ev->width;
100	wc.height = ev->height;
101	wc.border_width = 1;
102	wc.sibling = None;
103	wc.stack_mode = Above;
104	ev->value_mask &= ~CWStackMode;
105	ev->value_mask |= CWBorderWidth;
106	XConfigureWindow(dpy, ev->window, ev->value_mask, &wc);
107	XFlush(dpy);
108}
109
110static void
111destroynotify(XEvent *e)
112{
113	Client *c;
114	XDestroyWindowEvent *ev = &e->xdestroywindow;
115
116	if((c = getclient(ev->window)))
117		unmanage(c);
118}
119
120static void
121enternotify(XEvent *e)
122{
123	XCrossingEvent *ev = &e->xcrossing;
124	Client *c;
125
126	if(ev->mode != NotifyNormal || ev->detail == NotifyInferior)
127		return;
128
129	if((c = getclient(ev->window)))
130		focus(c);
131	else if(ev->window == root)
132		issel = True;
133}
134
135static void
136leavenotify(XEvent *e)
137{
138	XCrossingEvent *ev = &e->xcrossing;
139
140	if((ev->window == root) && !ev->same_screen)
141		issel = True;
142}
143
144static void
145expose(XEvent *e)
146{
147	XExposeEvent *ev = &e->xexpose;
148	Client *c;
149
150	if(ev->count == 0) {
151		if((c = gettitle(ev->window)))
152			draw_client(c);
153	}
154}
155
156static void
157keymapnotify(XEvent *e)
158{
159	update_keys();
160}
161
162static void
163maprequest(XEvent *e)
164{
165	XMapRequestEvent *ev = &e->xmaprequest;
166	static XWindowAttributes wa;
167
168	if(!XGetWindowAttributes(dpy, ev->window, &wa))
169		return;
170
171	if(wa.override_redirect) {
172		XSelectInput(dpy, ev->window,
173				(StructureNotifyMask | PropertyChangeMask));
174		return;
175	}
176
177	if(!getclient(ev->window))
178		manage(ev->window, &wa);
179}
180
181static void
182propertynotify(XEvent *e)
183{
184	XPropertyEvent *ev = &e->xproperty;
185	Window trans;
186	Client *c;
187
188	if(ev->state == PropertyDelete)
189		return; /* ignore */
190
191	if((c = getclient(ev->window))) {
192		if(ev->atom == wm_atom[WMProtocols]) {
193			c->proto = win_proto(c->win);
194			return;
195		}
196		switch (ev->atom) {
197			default: break;
198			case XA_WM_TRANSIENT_FOR:
199				XGetTransientForHint(dpy, c->win, &trans);
200				if(!c->floating && (c->floating = (trans != 0)))
201					arrange(NULL);
202				break;
203			case XA_WM_NORMAL_HINTS:
204				update_size(c);
205				break;
206		}
207		if(ev->atom == XA_WM_NAME || ev->atom == net_atom[NetWMName]) {
208			update_name(c);
209			draw_client(c);
210		}
211	}
212}
213
214static void
215unmapnotify(XEvent *e)
216{
217	Client *c;
218	XUnmapEvent *ev = &e->xunmap;
219
220	if((c = getclient(ev->window)))
221		unmanage(c);
222}