JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
made barclick to select the specific tag
[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(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
73 static void
74 configurerequest(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
110 static void
111 destroynotify(XEvent *e)
112 {
113         Client *c;
114         XDestroyWindowEvent *ev = &e->xdestroywindow;
115
116         if((c = getclient(ev->window)))
117                 unmanage(c);
118 }
119
120 static void
121 enternotify(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
135 static void
136 leavenotify(XEvent *e)
137 {
138         XCrossingEvent *ev = &e->xcrossing;
139
140         if((ev->window == root) && !ev->same_screen)
141                 issel = True;
142 }
143
144 static void
145 expose(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
156 static void
157 keymapnotify(XEvent *e)
158 {
159         update_keys();
160 }
161
162 static void
163 maprequest(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
181 static void
182 propertynotify(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
214 static void
215 unmapnotify(XEvent *e)
216 {
217         Client *c;
218         XUnmapEvent *ev = &e->xunmap;
219
220         if((c = getclient(ev->window)))
221                 unmanage(c);
222 }