aewl

annotate event.c @ 536:d5aa5a4be560

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