aewl

annotate event.c @ 562:def7247888d4

next release will be 2.2
author arg@mig29
date Fri, 03 Nov 2006 09:22:40 +0100
parents 1734939bceba
children fe766305eed1
rev   line source
arg@532 1 /* (C)opyright MMVI Anselm R. Garbe <garbeam at gmail dot com>
garbeam@5 2 * See LICENSE file for license details.
garbeam@5 3 */
garbeam@76 4 #include "dwm.h"
garbeam@5 5 #include <stdlib.h>
garbeam@5 6 #include <X11/keysym.h>
garbeam@13 7 #include <X11/Xatom.h>
garbeam@5 8
arg@146 9 /* static */
arg@114 10
arg@114 11 typedef struct {
arg@114 12 unsigned long mod;
arg@114 13 KeySym keysym;
arg@114 14 void (*func)(Arg *arg);
arg@114 15 Arg arg;
arg@114 16 } Key;
arg@114 17
arg@146 18 KEYS
garbeam@75 19
arg@291 20 #define CLEANMASK(mask) (mask & ~(numlockmask | LockMask))
arg@538 21 #define MOUSEMASK (BUTTONMASK | PointerMotionMask)
garbeam@5 22
garbeam@77 23 static void
arg@461 24 movemouse(Client *c) {
garbeam@77 25 int x1, y1, ocx, ocy, di;
garbeam@77 26 unsigned int dui;
garbeam@77 27 Window dummy;
arg@123 28 XEvent ev;
garbeam@77 29
arg@115 30 ocx = c->x;
arg@115 31 ocy = c->y;
arg@148 32 if(XGrabPointer(dpy, root, False, MOUSEMASK, GrabModeAsync, GrabModeAsync,
arg@123 33 None, cursor[CurMove], CurrentTime) != GrabSuccess)
garbeam@77 34 return;
arg@482 35 c->ismax = False;
garbeam@77 36 XQueryPointer(dpy, root, &dummy, &dummy, &x1, &y1, &di, &di, &dui);
garbeam@77 37 for(;;) {
arg@489 38 XMaskEvent(dpy, MOUSEMASK | ExposureMask, &ev);
garbeam@77 39 switch (ev.type) {
arg@490 40 case ButtonRelease:
arg@491 41 resize(c, True, TopLeft);
arg@490 42 XUngrabPointer(dpy, CurrentTime);
arg@490 43 return;
garbeam@77 44 case Expose:
garbeam@77 45 handler[Expose](&ev);
garbeam@77 46 break;
garbeam@77 47 case MotionNotify:
garbeam@79 48 XSync(dpy, False);
arg@115 49 c->x = ocx + (ev.xmotion.x - x1);
arg@115 50 c->y = ocy + (ev.xmotion.y - y1);
arg@556 51 if(abs(sx + c->x) < SNAP)
arg@551 52 c->x = sx;
arg@554 53 else if(abs((sx + sw) - (c->x + c->w)) < SNAP)
arg@553 54 c->x = sw - c->w - 2 * BORDERPX;
arg@554 55 if(abs((sy + bh) - c->y) < SNAP)
arg@551 56 c->y = sy + bh;
arg@554 57 else if(abs((sy + sh) - (c->y + c->h)) < SNAP)
arg@551 58 c->y = sh - c->h - 2 * BORDERPX;
arg@99 59 resize(c, False, TopLeft);
garbeam@77 60 break;
garbeam@77 61 }
garbeam@77 62 }
garbeam@77 63 }
garbeam@77 64
garbeam@77 65 static void
arg@461 66 resizemouse(Client *c) {
garbeam@77 67 int ocx, ocy;
arg@268 68 int nw, nh;
arg@99 69 Corner sticky;
arg@123 70 XEvent ev;
garbeam@77 71
arg@115 72 ocx = c->x;
arg@115 73 ocy = c->y;
arg@148 74 if(XGrabPointer(dpy, root, False, MOUSEMASK, GrabModeAsync, GrabModeAsync,
arg@532 75 None, cursor[CurResize], CurrentTime) != GrabSuccess)
garbeam@77 76 return;
arg@482 77 c->ismax = False;
arg@115 78 XWarpPointer(dpy, None, c->win, 0, 0, 0, 0, c->w, c->h);
garbeam@77 79 for(;;) {
arg@489 80 XMaskEvent(dpy, MOUSEMASK | ExposureMask, &ev);
garbeam@77 81 switch(ev.type) {
arg@490 82 case ButtonRelease:
arg@491 83 resize(c, True, TopLeft);
arg@490 84 XUngrabPointer(dpy, CurrentTime);
arg@490 85 return;
garbeam@77 86 case Expose:
garbeam@77 87 handler[Expose](&ev);
garbeam@77 88 break;
garbeam@77 89 case MotionNotify:
garbeam@79 90 XSync(dpy, False);
arg@268 91 if((nw = abs(ocx - ev.xmotion.x)))
arg@478 92 c->w = nw;
arg@268 93 if((nh = abs(ocy - ev.xmotion.y)))
arg@478 94 c->h = nh;
arg@115 95 c->x = (ocx <= ev.xmotion.x) ? ocx : ocx - c->w;
arg@115 96 c->y = (ocy <= ev.xmotion.y) ? ocy : ocy - c->h;
arg@105 97 if(ocx <= ev.xmotion.x)
arg@105 98 sticky = (ocy <= ev.xmotion.y) ? TopLeft : BotLeft;
arg@105 99 else
arg@105 100 sticky = (ocy <= ev.xmotion.y) ? TopRight : BotRight;
arg@99 101 resize(c, True, sticky);
garbeam@77 102 break;
garbeam@77 103 }
garbeam@77 104 }
garbeam@77 105 }
garbeam@73 106
garbeam@73 107 static void
arg@461 108 buttonpress(XEvent *e) {
garbeam@73 109 int x;
garbeam@73 110 Arg a;
arg@123 111 Client *c;
garbeam@18 112 XButtonPressedEvent *ev = &e->xbutton;
garbeam@18 113
garbeam@73 114 if(barwin == ev->window) {
arg@362 115 x = 0;
arg@362 116 for(a.i = 0; a.i < ntags; a.i++) {
arg@362 117 x += textw(tags[a.i]);
arg@362 118 if(ev->x < x) {
arg@399 119 if(ev->button == Button1) {
arg@398 120 if(ev->state & MODKEY)
arg@398 121 tag(&a);
arg@398 122 else
arg@398 123 view(&a);
arg@399 124 }
arg@399 125 else if(ev->button == Button3) {
arg@398 126 if(ev->state & MODKEY)
arg@398 127 toggletag(&a);
arg@398 128 else
arg@398 129 toggleview(&a);
arg@394 130 }
arg@362 131 return;
garbeam@73 132 }
garbeam@73 133 }
arg@530 134 if((ev->x < x + bmw) && (ev->button == Button1))
arg@530 135 togglemode(NULL);
garbeam@73 136 }
garbeam@58 137 else if((c = getclient(ev->window))) {
arg@143 138 focus(c);
arg@473 139 if(CLEANMASK(ev->state) != MODKEY)
arg@318 140 return;
arg@400 141 if(ev->button == Button1 && (arrange == dofloat || c->isfloat)) {
arg@487 142 restack();
arg@399 143 movemouse(c);
arg@399 144 }
arg@399 145 else if(ev->button == Button2)
arg@248 146 zoom(NULL);
arg@550 147 else if(ev->button == Button3 && (arrange == dofloat || c->isfloat) &&
arg@550 148 !c->isfixed) {
arg@487 149 restack();
arg@399 150 resizemouse(c);
garbeam@18 151 }
garbeam@18 152 }
garbeam@18 153 }
garbeam@18 154
garbeam@18 155 static void
arg@461 156 configurerequest(XEvent *e) {
arg@286 157 unsigned long newmask;
arg@123 158 Client *c;
garbeam@5 159 XConfigureRequestEvent *ev = &e->xconfigurerequest;
garbeam@5 160 XWindowChanges wc;
garbeam@5 161
garbeam@18 162 if((c = getclient(ev->window))) {
arg@488 163 c->ismax = False;
garbeam@29 164 gravitate(c, True);
arg@195 165 if(ev->value_mask & CWX)
arg@195 166 c->x = ev->x;
arg@195 167 if(ev->value_mask & CWY)
arg@195 168 c->y = ev->y;
arg@195 169 if(ev->value_mask & CWWidth)
arg@195 170 c->w = ev->width;
arg@195 171 if(ev->value_mask & CWHeight)
arg@195 172 c->h = ev->height;
garbeam@29 173 if(ev->value_mask & CWBorderWidth)
arg@164 174 c->border = ev->border_width;
garbeam@29 175 gravitate(c, False);
arg@164 176 wc.x = c->x;
arg@164 177 wc.y = c->y;
arg@164 178 wc.width = c->w;
arg@164 179 wc.height = c->h;
arg@164 180 newmask = ev->value_mask & (~(CWSibling | CWStackMode | CWBorderWidth));
arg@164 181 if(newmask)
arg@164 182 XConfigureWindow(dpy, c->win, newmask, &wc);
arg@491 183 else
arg@491 184 configure(c);
arg@195 185 XSync(dpy, False);
arg@505 186 if(c->isfloat) {
arg@508 187 resize(c, False, TopLeft);
arg@508 188 if(!isvisible(c))
arg@508 189 ban(c);
arg@505 190 }
arg@196 191 else
arg@533 192 arrange();
garbeam@5 193 }
arg@164 194 else {
arg@164 195 wc.x = ev->x;
arg@164 196 wc.y = ev->y;
arg@164 197 wc.width = ev->width;
arg@164 198 wc.height = ev->height;
arg@164 199 wc.border_width = ev->border_width;
arg@164 200 wc.sibling = ev->above;
arg@164 201 wc.stack_mode = ev->detail;
arg@164 202 XConfigureWindow(dpy, ev->window, ev->value_mask, &wc);
arg@195 203 XSync(dpy, False);
arg@164 204 }
garbeam@5 205 }
garbeam@5 206
garbeam@5 207 static void
arg@461 208 destroynotify(XEvent *e) {
garbeam@5 209 Client *c;
garbeam@5 210 XDestroyWindowEvent *ev = &e->xdestroywindow;
garbeam@5 211
garbeam@11 212 if((c = getclient(ev->window)))
garbeam@11 213 unmanage(c);
garbeam@5 214 }
garbeam@5 215
garbeam@5 216 static void
arg@461 217 enternotify(XEvent *e) {
arg@123 218 Client *c;
garbeam@5 219 XCrossingEvent *ev = &e->xcrossing;
garbeam@5 220
arg@232 221 if(ev->mode != NotifyNormal || ev->detail == NotifyInferior)
garbeam@5 222 return;
arg@466 223 if(((c = getclient(ev->window)) || (c = getctitle(ev->window))) && isvisible(c))
garbeam@13 224 focus(c);
arg@239 225 else if(ev->window == root) {
garbeam@31 226 issel = True;
arg@239 227 XSetInputFocus(dpy, root, RevertToPointerRoot, CurrentTime);
arg@239 228 drawall();
arg@239 229 }
garbeam@5 230 }
garbeam@5 231
garbeam@5 232 static void
arg@461 233 expose(XEvent *e) {
arg@123 234 Client *c;
garbeam@5 235 XExposeEvent *ev = &e->xexpose;
garbeam@5 236
garbeam@5 237 if(ev->count == 0) {
garbeam@70 238 if(barwin == ev->window)
garbeam@74 239 drawstatus();
garbeam@75 240 else if((c = getctitle(ev->window)))
garbeam@74 241 drawtitle(c);
garbeam@5 242 }
garbeam@5 243 }
garbeam@5 244
garbeam@5 245 static void
arg@461 246 keypress(XEvent *e) {
arg@138 247 static unsigned int len = sizeof(key) / sizeof(key[0]);
garbeam@76 248 unsigned int i;
garbeam@76 249 KeySym keysym;
arg@123 250 XKeyEvent *ev = &e->xkey;
garbeam@76 251
garbeam@76 252 keysym = XKeycodeToKeysym(dpy, (KeyCode)ev->keycode, 0);
arg@275 253 for(i = 0; i < len; i++) {
arg@461 254 if(keysym == key[i].keysym
arg@461 255 && CLEANMASK(key[i].mod) == CLEANMASK(ev->state))
arg@275 256 {
garbeam@76 257 if(key[i].func)
garbeam@76 258 key[i].func(&key[i].arg);
garbeam@76 259 return;
garbeam@76 260 }
arg@275 261 }
garbeam@76 262 }
garbeam@76 263
garbeam@76 264 static void
arg@461 265 leavenotify(XEvent *e) {
garbeam@76 266 XCrossingEvent *ev = &e->xcrossing;
garbeam@76 267
arg@239 268 if((ev->window == root) && !ev->same_screen) {
arg@239 269 issel = False;
arg@239 270 drawall();
arg@239 271 }
garbeam@76 272 }
garbeam@76 273
garbeam@76 274 static void
arg@461 275 mappingnotify(XEvent *e) {
arg@279 276 XMappingEvent *ev = &e->xmapping;
arg@279 277
arg@279 278 XRefreshKeyboardMapping(ev);
arg@279 279 if(ev->request == MappingKeyboard)
arg@279 280 grabkeys();
arg@279 281 }
arg@279 282
arg@279 283 static void
arg@461 284 maprequest(XEvent *e) {
arg@123 285 static XWindowAttributes wa;
garbeam@5 286 XMapRequestEvent *ev = &e->xmaprequest;
garbeam@5 287
garbeam@5 288 if(!XGetWindowAttributes(dpy, ev->window, &wa))
garbeam@5 289 return;
garbeam@5 290 if(wa.override_redirect) {
garbeam@5 291 XSelectInput(dpy, ev->window,
garbeam@5 292 (StructureNotifyMask | PropertyChangeMask));
garbeam@5 293 return;
garbeam@5 294 }
garbeam@10 295 if(!getclient(ev->window))
garbeam@10 296 manage(ev->window, &wa);
garbeam@5 297 }
garbeam@5 298
garbeam@5 299 static void
arg@461 300 propertynotify(XEvent *e) {
arg@123 301 Client *c;
arg@123 302 Window trans;
garbeam@5 303 XPropertyEvent *ev = &e->xproperty;
garbeam@5 304
garbeam@5 305 if(ev->state == PropertyDelete)
garbeam@5 306 return; /* ignore */
garbeam@13 307 if((c = getclient(ev->window))) {
garbeam@77 308 if(ev->atom == wmatom[WMProtocols]) {
garbeam@75 309 c->proto = getproto(c->win);
garbeam@30 310 return;
garbeam@30 311 }
garbeam@13 312 switch (ev->atom) {
garbeam@13 313 default: break;
garbeam@13 314 case XA_WM_TRANSIENT_FOR:
garbeam@53 315 XGetTransientForHint(dpy, c->win, &trans);
garbeam@80 316 if(!c->isfloat && (c->isfloat = (trans != 0)))
arg@533 317 arrange();
garbeam@13 318 break;
garbeam@13 319 case XA_WM_NORMAL_HINTS:
arg@454 320 updatesize(c);
garbeam@13 321 break;
garbeam@13 322 }
garbeam@77 323 if(ev->atom == XA_WM_NAME || ev->atom == netatom[NetWMName]) {
arg@454 324 updatetitle(c);
arg@500 325 resizetitle(c);
garbeam@74 326 drawtitle(c);
garbeam@13 327 }
garbeam@13 328 }
garbeam@5 329 }
garbeam@5 330
garbeam@5 331 static void
arg@461 332 unmapnotify(XEvent *e) {
garbeam@5 333 Client *c;
garbeam@5 334 XUnmapEvent *ev = &e->xunmap;
garbeam@5 335
garbeam@10 336 if((c = getclient(ev->window)))
garbeam@10 337 unmanage(c);
garbeam@5 338 }
garbeam@76 339
garbeam@84 340 /* extern */
garbeam@76 341
garbeam@76 342 void (*handler[LASTEvent]) (XEvent *) = {
garbeam@76 343 [ButtonPress] = buttonpress,
garbeam@76 344 [ConfigureRequest] = configurerequest,
garbeam@76 345 [DestroyNotify] = destroynotify,
garbeam@76 346 [EnterNotify] = enternotify,
garbeam@76 347 [LeaveNotify] = leavenotify,
garbeam@76 348 [Expose] = expose,
garbeam@76 349 [KeyPress] = keypress,
arg@279 350 [MappingNotify] = mappingnotify,
garbeam@76 351 [MapRequest] = maprequest,
garbeam@76 352 [PropertyNotify] = propertynotify,
garbeam@76 353 [UnmapNotify] = unmapnotify
garbeam@76 354 };
garbeam@76 355
garbeam@76 356 void
arg@487 357 grabkeys(void) {
arg@138 358 static unsigned int len = sizeof(key) / sizeof(key[0]);
garbeam@76 359 unsigned int i;
garbeam@76 360 KeyCode code;
garbeam@76 361
arg@279 362 XUngrabKey(dpy, AnyKey, AnyModifier, root);
garbeam@76 363 for(i = 0; i < len; i++) {
garbeam@76 364 code = XKeysymToKeycode(dpy, key[i].keysym);
garbeam@76 365 XGrabKey(dpy, code, key[i].mod, root, True,
garbeam@76 366 GrabModeAsync, GrabModeAsync);
arg@160 367 XGrabKey(dpy, code, key[i].mod | LockMask, root, True,
arg@160 368 GrabModeAsync, GrabModeAsync);
arg@291 369 XGrabKey(dpy, code, key[i].mod | numlockmask, root, True,
arg@146 370 GrabModeAsync, GrabModeAsync);
arg@291 371 XGrabKey(dpy, code, key[i].mod | numlockmask | LockMask, root, True,
arg@146 372 GrabModeAsync, GrabModeAsync);
garbeam@76 373 }
garbeam@76 374 }
arg@292 375
arg@292 376 void
arg@487 377 procevent(void) {
arg@292 378 XEvent ev;
arg@292 379
arg@292 380 while(XPending(dpy)) {
arg@292 381 XNextEvent(dpy, &ev);
arg@292 382 if(handler[ev.type])
arg@292 383 (handler[ev.type])(&ev); /* call handler */
arg@292 384 }
arg@292 385 }