aewl

annotate event.c @ 480:680aca428830

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