aewl

annotate event.c @ 522:fd1275f3b205

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