aewl

annotate event.c @ 398:9c703e528e58

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