dwm-meillo
view event.c @ 262:d659a2dce2b5
implemented viewextend and added M-S-C-n shortcuts for extending the current view... updated man page (works great!) nice feature
author | Anselm R.Garbe <arg@10ksloc.org> |
---|---|
date | Fri, 11 Aug 2006 19:26:12 +0200 |
parents | d6fd632d861c |
children | 118d3e010e5e |
line source
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 #include <stdlib.h>
7 #include <X11/keysym.h>
8 #include <X11/Xatom.h>
10 /* static */
12 typedef struct {
13 unsigned long mod;
14 KeySym keysym;
15 void (*func)(Arg *arg);
16 Arg arg;
17 } Key;
19 KEYS
21 #define CLEANMASK(mask) (mask & ~(NUMLOCKMASK | LockMask))
23 static void
24 movemouse(Client *c)
25 {
26 int x1, y1, ocx, ocy, di;
27 unsigned int dui;
28 Window dummy;
29 XEvent ev;
31 ocx = c->x;
32 ocy = c->y;
33 if(XGrabPointer(dpy, root, False, MOUSEMASK, GrabModeAsync, GrabModeAsync,
34 None, cursor[CurMove], CurrentTime) != GrabSuccess)
35 return;
36 XQueryPointer(dpy, root, &dummy, &dummy, &x1, &y1, &di, &di, &dui);
37 for(;;) {
38 XMaskEvent(dpy, MOUSEMASK | ExposureMask, &ev);
39 switch (ev.type) {
40 default: break;
41 case Expose:
42 handler[Expose](&ev);
43 break;
44 case MotionNotify:
45 XSync(dpy, False);
46 c->x = ocx + (ev.xmotion.x - x1);
47 c->y = ocy + (ev.xmotion.y - y1);
48 resize(c, False, TopLeft);
49 break;
50 case ButtonRelease:
51 XUngrabPointer(dpy, CurrentTime);
52 return;
53 }
54 }
55 }
57 static void
58 resizemouse(Client *c)
59 {
60 int ocx, ocy;
61 Corner sticky;
62 XEvent ev;
64 ocx = c->x;
65 ocy = c->y;
66 if(XGrabPointer(dpy, root, False, MOUSEMASK, GrabModeAsync, GrabModeAsync,
67 None, cursor[CurResize], CurrentTime) != GrabSuccess)
68 return;
69 XWarpPointer(dpy, None, c->win, 0, 0, 0, 0, c->w, c->h);
70 for(;;) {
71 XMaskEvent(dpy, MOUSEMASK | ExposureMask, &ev);
72 switch(ev.type) {
73 default: break;
74 case Expose:
75 handler[Expose](&ev);
76 break;
77 case MotionNotify:
78 XSync(dpy, False);
79 c->w = abs(ocx - ev.xmotion.x);
80 c->h = abs(ocy - ev.xmotion.y);
81 c->x = (ocx <= ev.xmotion.x) ? ocx : ocx - c->w;
82 c->y = (ocy <= ev.xmotion.y) ? ocy : ocy - c->h;
83 if(ocx <= ev.xmotion.x)
84 sticky = (ocy <= ev.xmotion.y) ? TopLeft : BotLeft;
85 else
86 sticky = (ocy <= ev.xmotion.y) ? TopRight : BotRight;
87 resize(c, True, sticky);
88 break;
89 case ButtonRelease:
90 XUngrabPointer(dpy, CurrentTime);
91 return;
92 }
93 }
94 }
96 static void
97 buttonpress(XEvent *e)
98 {
99 int x;
100 Arg a;
101 Client *c;
102 XButtonPressedEvent *ev = &e->xbutton;
104 if(barwin == ev->window) {
105 switch(ev->button) {
106 default:
107 x = 0;
108 for(a.i = 0; a.i < ntags; a.i++) {
109 x += textw(tags[a.i]);
110 if(ev->x < x) {
111 if(ev->button == Button3)
112 viewextend(&a);
113 else
114 view(&a);
115 return;
116 }
117 }
118 if(ev->button == Button1)
119 viewprev(&a);
120 else if(ev->button == Button3)
121 viewnext(&a);
122 break;
123 case Button4:
124 viewprev(&a);
125 break;
126 case Button5:
127 viewnext(&a);
128 break;
129 }
130 }
131 else if((c = getclient(ev->window))) {
132 higher(c);
133 focus(c);
134 switch(ev->button) {
135 default:
136 break;
137 case Button1:
138 if(!c->ismax && (arrange == dofloat || c->isfloat))
139 movemouse(c);
140 break;
141 case Button2:
142 zoom(NULL);
143 break;
144 case Button3:
145 if(!c->ismax && (arrange == dofloat || c->isfloat))
146 resizemouse(c);
147 break;
148 }
149 }
150 }
152 static void
153 configurerequest(XEvent *e)
154 {
155 Client *c;
156 XConfigureRequestEvent *ev = &e->xconfigurerequest;
157 XEvent synev;
158 XWindowChanges wc;
159 unsigned long newmask;
161 if((c = getclient(ev->window))) {
162 gravitate(c, True);
163 if(ev->value_mask & CWX)
164 c->x = ev->x;
165 if(ev->value_mask & CWY)
166 c->y = ev->y;
167 if(ev->value_mask & CWWidth)
168 c->w = ev->width;
169 if(ev->value_mask & CWHeight)
170 c->h = ev->height;
171 if(ev->value_mask & CWBorderWidth)
172 c->border = ev->border_width;
173 gravitate(c, False);
174 wc.x = c->x;
175 wc.y = c->y;
176 wc.width = c->w;
177 wc.height = c->h;
178 newmask = ev->value_mask & (~(CWSibling | CWStackMode | CWBorderWidth));
179 if(newmask)
180 XConfigureWindow(dpy, c->win, newmask, &wc);
181 else {
182 synev.type = ConfigureNotify;
183 synev.xconfigure.display = dpy;
184 synev.xconfigure.event = c->win;
185 synev.xconfigure.window = c->win;
186 synev.xconfigure.x = c->x;
187 synev.xconfigure.y = c->y;
188 synev.xconfigure.width = c->w;
189 synev.xconfigure.height = c->h;
190 synev.xconfigure.border_width = c->border;
191 synev.xconfigure.above = None;
192 /* Send synthetic ConfigureNotify */
193 XSendEvent(dpy, c->win, True, NoEventMask, &synev);
194 }
195 XSync(dpy, False);
196 if(c->isfloat)
197 resize(c, False, TopLeft);
198 else
199 arrange(NULL);
200 }
201 else {
202 wc.x = ev->x;
203 wc.y = ev->y;
204 wc.width = ev->width;
205 wc.height = ev->height;
206 wc.border_width = ev->border_width;
207 wc.sibling = ev->above;
208 wc.stack_mode = ev->detail;
209 XConfigureWindow(dpy, ev->window, ev->value_mask, &wc);
210 XSync(dpy, False);
211 }
212 }
214 static void
215 destroynotify(XEvent *e)
216 {
217 Client *c;
218 XDestroyWindowEvent *ev = &e->xdestroywindow;
220 if((c = getclient(ev->window)))
221 unmanage(c);
222 }
224 static void
225 enternotify(XEvent *e)
226 {
227 Client *c;
228 XCrossingEvent *ev = &e->xcrossing;
230 if(ev->mode != NotifyNormal || ev->detail == NotifyInferior)
231 return;
233 if((c = getclient(ev->window)) || (c = getctitle(ev->window)))
234 focus(c);
235 else if(ev->window == root) {
236 issel = True;
237 XSetInputFocus(dpy, root, RevertToPointerRoot, CurrentTime);
238 drawall();
239 }
240 }
242 static void
243 expose(XEvent *e)
244 {
245 Client *c;
246 XExposeEvent *ev = &e->xexpose;
248 if(ev->count == 0) {
249 if(barwin == ev->window)
250 drawstatus();
251 else if((c = getctitle(ev->window)))
252 drawtitle(c);
253 }
254 }
256 static void
257 keypress(XEvent *e)
258 {
259 static unsigned int len = sizeof(key) / sizeof(key[0]);
260 unsigned int i;
261 KeySym keysym;
262 XKeyEvent *ev = &e->xkey;
264 keysym = XKeycodeToKeysym(dpy, (KeyCode)ev->keycode, 0);
265 for(i = 0; i < len; i++)
266 if(keysym == key[i].keysym &&
267 CLEANMASK(key[i].mod) == CLEANMASK(ev->state)) {
268 if(key[i].func)
269 key[i].func(&key[i].arg);
270 return;
271 }
272 }
274 static void
275 leavenotify(XEvent *e)
276 {
277 XCrossingEvent *ev = &e->xcrossing;
279 if((ev->window == root) && !ev->same_screen) {
280 issel = False;
281 drawall();
282 }
283 }
285 static void
286 maprequest(XEvent *e)
287 {
288 static XWindowAttributes wa;
289 XMapRequestEvent *ev = &e->xmaprequest;
291 if(!XGetWindowAttributes(dpy, ev->window, &wa))
292 return;
294 if(wa.override_redirect) {
295 XSelectInput(dpy, ev->window,
296 (StructureNotifyMask | PropertyChangeMask));
297 return;
298 }
300 if(!getclient(ev->window))
301 manage(ev->window, &wa);
302 }
304 static void
305 propertynotify(XEvent *e)
306 {
307 Client *c;
308 Window trans;
309 XPropertyEvent *ev = &e->xproperty;
311 if(ev->state == PropertyDelete)
312 return; /* ignore */
314 if((c = getclient(ev->window))) {
315 if(ev->atom == wmatom[WMProtocols]) {
316 c->proto = getproto(c->win);
317 return;
318 }
319 switch (ev->atom) {
320 default: break;
321 case XA_WM_TRANSIENT_FOR:
322 XGetTransientForHint(dpy, c->win, &trans);
323 if(!c->isfloat && (c->isfloat = (trans != 0)))
324 arrange(NULL);
325 break;
326 case XA_WM_NORMAL_HINTS:
327 setsize(c);
328 break;
329 }
330 if(ev->atom == XA_WM_NAME || ev->atom == netatom[NetWMName]) {
331 settitle(c);
332 drawtitle(c);
333 }
334 }
335 }
337 static void
338 unmapnotify(XEvent *e)
339 {
340 Client *c;
341 XUnmapEvent *ev = &e->xunmap;
343 if((c = getclient(ev->window)))
344 unmanage(c);
345 }
347 /* extern */
349 void (*handler[LASTEvent]) (XEvent *) = {
350 [ButtonPress] = buttonpress,
351 [ConfigureRequest] = configurerequest,
352 [DestroyNotify] = destroynotify,
353 [EnterNotify] = enternotify,
354 [LeaveNotify] = leavenotify,
355 [Expose] = expose,
356 [KeyPress] = keypress,
357 [MapRequest] = maprequest,
358 [PropertyNotify] = propertynotify,
359 [UnmapNotify] = unmapnotify
360 };
362 void
363 grabkeys()
364 {
365 static unsigned int len = sizeof(key) / sizeof(key[0]);
366 unsigned int i;
367 KeyCode code;
369 for(i = 0; i < len; i++) {
370 code = XKeysymToKeycode(dpy, key[i].keysym);
371 XGrabKey(dpy, code, key[i].mod, root, True,
372 GrabModeAsync, GrabModeAsync);
373 XGrabKey(dpy, code, key[i].mod | LockMask, root, True,
374 GrabModeAsync, GrabModeAsync);
375 XGrabKey(dpy, code, key[i].mod | NUMLOCKMASK, root, True,
376 GrabModeAsync, GrabModeAsync);
377 XGrabKey(dpy, code, key[i].mod | NUMLOCKMASK | LockMask, root, True,
378 GrabModeAsync, GrabModeAsync);
379 }
380 }