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 "wm.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
40unsigned int
41discard_events(long even_mask)
42{
43 XEvent ev;
44 unsigned int n = 0;
45 while(XCheckMaskEvent(dpy, even_mask, &ev)) n++;
46 return n;
47}
48
49static void
50buttonpress(XEvent *e)
51{
52 XButtonPressedEvent *ev = &e->xbutton;
53 Client *c;
54
55 if((c = getclient(ev->window))) {
56 switch(ev->button) {
57 default:
58 break;
59 case Button1:
60 mmove(c);
61 break;
62 case Button2:
63 XLowerWindow(dpy, c->win);
64 break;
65 case Button3:
66 mresize(c);
67 break;
68 }
69 }
70}
71
72static void
73configurerequest(XEvent *e)
74{
75 XConfigureRequestEvent *ev = &e->xconfigurerequest;
76 XWindowChanges wc;
77 Client *c;
78
79 ev->value_mask &= ~CWSibling;
80 if((c = getclient(ev->window))) {
81 if(ev->value_mask & CWX)
82 c->r[RFloat].x = ev->x;
83 if(ev->value_mask & CWY)
84 c->r[RFloat].y = ev->y;
85 if(ev->value_mask & CWWidth)
86 c->r[RFloat].width = ev->width;
87 if(ev->value_mask & CWHeight)
88 c->r[RFloat].height = ev->height;
89 if(ev->value_mask & CWBorderWidth)
90 c->border = ev->border_width;
91 }
92
93 wc.x = ev->x;
94 wc.y = ev->y;
95 wc.width = ev->width;
96 wc.height = ev->height;
97 wc.border_width = 0;
98 wc.sibling = None;
99 wc.stack_mode = Above;
100 ev->value_mask &= ~CWStackMode;
101 ev->value_mask |= CWBorderWidth;
102 XConfigureWindow(dpy, ev->window, ev->value_mask, &wc);
103 XFlush(dpy);
104}
105
106static void
107destroynotify(XEvent *e)
108{
109 Client *c;
110 XDestroyWindowEvent *ev = &e->xdestroywindow;
111
112 if((c = getclient(ev->window)))
113 unmanage(c);
114}
115
116static void
117enternotify(XEvent *e)
118{
119 XCrossingEvent *ev = &e->xcrossing;
120 Client *c;
121
122 if(ev->mode != NotifyNormal || ev->detail == NotifyInferior)
123 return;
124
125 if((c = getclient(ev->window)))
126 focus(c);
127 else if(ev->window == root) {
128 sel_screen = True;
129 /*draw_frames();*/
130 }
131}
132
133static void
134leavenotify(XEvent *e)
135{
136 XCrossingEvent *ev = &e->xcrossing;
137
138 if((ev->window == root) && !ev->same_screen) {
139 sel_screen = True;
140 /*draw_frames();*/
141 }
142}
143
144static void
145expose(XEvent *e)
146{
147 XExposeEvent *ev = &e->xexpose;
148
149 if(ev->count == 0) {
150 if(ev->window == barwin)
151 draw_bar();
152 }
153}
154
155static void
156keymapnotify(XEvent *e)
157{
158 update_keys();
159}
160
161static void
162maprequest(XEvent *e)
163{
164 XMapRequestEvent *ev = &e->xmaprequest;
165 static XWindowAttributes wa;
166
167 if(!XGetWindowAttributes(dpy, ev->window, &wa))
168 return;
169
170 if(wa.override_redirect) {
171 XSelectInput(dpy, ev->window,
172 (StructureNotifyMask | PropertyChangeMask));
173 return;
174 }
175
176 if(!getclient(ev->window))
177 manage(ev->window, &wa);
178}
179
180static void
181propertynotify(XEvent *e)
182{
183 XPropertyEvent *ev = &e->xproperty;
184 long msize;
185 Client *c;
186
187 if(ev->state == PropertyDelete)
188 return; /* ignore */
189
190 if(ev->atom == wm_atom[WMProtocols]) {
191 c->proto = win_proto(c->win);
192 return;
193 }
194 if((c = getclient(ev->window))) {
195 switch (ev->atom) {
196 default: break;
197 case XA_WM_TRANSIENT_FOR:
198 XGetTransientForHint(dpy, c->win, &c->trans);
199 break;
200 case XA_WM_NORMAL_HINTS:
201 if(!XGetWMNormalHints(dpy, c->win, &c->size, &msize)
202 || !c->size.flags)
203 c->size.flags = PSize;
204 if(c->size.flags & PMinSize && c->size.flags & PMaxSize
205 && c->size.min_width == c->size.max_width
206 && c->size.min_height == c->size.max_height)
207 c->fixedsize = True;
208 else
209 c->fixedsize = False;
210 break;
211 }
212 if(ev->atom == XA_WM_NAME || ev->atom == net_atom[NetWMName]) {
213 update_name(c);
214 }
215 }
216}
217
218static void
219unmapnotify(XEvent *e)
220{
221 Client *c;
222 XUnmapEvent *ev = &e->xunmap;
223
224 if((c = getclient(ev->window)))
225 unmanage(c);
226}