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