dwm-meillo

annotate event.c @ 683:1fceacfc5e3a

Added tag 3.0 for changeset 76b58d21ea98
author Anselm R. Garbe <arg@suckless.org>
date Fri, 12 Jan 2007 12:37:12 +0100
parents fd2ea58b0b94
children a76799907854
rev   line source
arg@644 1 /* (C)opyright MMVI-MMVII 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@589 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@565 51 if(abs(wax + c->x) < SNAP)
arg@565 52 c->x = wax;
arg@565 53 else if(abs((wax + waw) - (c->x + c->w)) < SNAP)
arg@565 54 c->x = wax + waw - c->w - 2 * BORDERPX;
arg@565 55 if(abs(way - c->y) < SNAP)
arg@565 56 c->y = way;
arg@565 57 else if(abs((way + wah) - (c->y + c->h)) < SNAP)
arg@565 58 c->y = way + wah - 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@676 134 if(ev->x < x + bmw)
arg@676 135 switch(ev->button) {
arg@676 136 case Button1:
arg@676 137 togglemode(NULL);
arg@676 138 break;
arg@676 139 case Button4:
arg@676 140 a.i = 1;
arg@676 141 incnmaster(&a);
arg@676 142 break;
arg@676 143 case Button5:
arg@676 144 a.i = -1;
arg@676 145 incnmaster(&a);
arg@676 146 break;
arg@676 147 }
garbeam@73 148 }
garbeam@58 149 else if((c = getclient(ev->window))) {
arg@143 150 focus(c);
arg@473 151 if(CLEANMASK(ev->state) != MODKEY)
arg@318 152 return;
arg@400 153 if(ev->button == Button1 && (arrange == dofloat || c->isfloat)) {
arg@487 154 restack();
arg@399 155 movemouse(c);
arg@399 156 }
arg@399 157 else if(ev->button == Button2)
arg@248 158 zoom(NULL);
arg@550 159 else if(ev->button == Button3 && (arrange == dofloat || c->isfloat) &&
arg@550 160 !c->isfixed) {
arg@487 161 restack();
arg@399 162 resizemouse(c);
garbeam@18 163 }
garbeam@18 164 }
garbeam@18 165 }
garbeam@18 166
garbeam@18 167 static void
arg@461 168 configurerequest(XEvent *e) {
arg@286 169 unsigned long newmask;
arg@123 170 Client *c;
garbeam@5 171 XConfigureRequestEvent *ev = &e->xconfigurerequest;
garbeam@5 172 XWindowChanges wc;
garbeam@5 173
garbeam@18 174 if((c = getclient(ev->window))) {
arg@488 175 c->ismax = False;
arg@195 176 if(ev->value_mask & CWX)
arg@195 177 c->x = ev->x;
arg@195 178 if(ev->value_mask & CWY)
arg@195 179 c->y = ev->y;
arg@195 180 if(ev->value_mask & CWWidth)
arg@195 181 c->w = ev->width;
arg@195 182 if(ev->value_mask & CWHeight)
arg@195 183 c->h = ev->height;
garbeam@29 184 if(ev->value_mask & CWBorderWidth)
arg@164 185 c->border = ev->border_width;
arg@164 186 wc.x = c->x;
arg@164 187 wc.y = c->y;
arg@164 188 wc.width = c->w;
arg@164 189 wc.height = c->h;
arg@164 190 newmask = ev->value_mask & (~(CWSibling | CWStackMode | CWBorderWidth));
arg@164 191 if(newmask)
arg@164 192 XConfigureWindow(dpy, c->win, newmask, &wc);
arg@491 193 else
arg@491 194 configure(c);
arg@195 195 XSync(dpy, False);
arg@505 196 if(c->isfloat) {
arg@508 197 resize(c, False, TopLeft);
arg@508 198 if(!isvisible(c))
arg@508 199 ban(c);
arg@505 200 }
arg@196 201 else
arg@533 202 arrange();
garbeam@5 203 }
arg@164 204 else {
arg@164 205 wc.x = ev->x;
arg@164 206 wc.y = ev->y;
arg@164 207 wc.width = ev->width;
arg@164 208 wc.height = ev->height;
arg@164 209 wc.border_width = ev->border_width;
arg@164 210 wc.sibling = ev->above;
arg@164 211 wc.stack_mode = ev->detail;
arg@164 212 XConfigureWindow(dpy, ev->window, ev->value_mask, &wc);
arg@195 213 XSync(dpy, False);
arg@164 214 }
garbeam@5 215 }
garbeam@5 216
garbeam@5 217 static void
arg@461 218 destroynotify(XEvent *e) {
garbeam@5 219 Client *c;
garbeam@5 220 XDestroyWindowEvent *ev = &e->xdestroywindow;
garbeam@5 221
garbeam@11 222 if((c = getclient(ev->window)))
garbeam@11 223 unmanage(c);
garbeam@5 224 }
garbeam@5 225
garbeam@5 226 static void
arg@461 227 enternotify(XEvent *e) {
arg@123 228 Client *c;
garbeam@5 229 XCrossingEvent *ev = &e->xcrossing;
garbeam@5 230
arg@232 231 if(ev->mode != NotifyNormal || ev->detail == NotifyInferior)
garbeam@5 232 return;
arg@466 233 if(((c = getclient(ev->window)) || (c = getctitle(ev->window))) && isvisible(c))
garbeam@13 234 focus(c);
arg@239 235 else if(ev->window == root) {
garbeam@31 236 issel = True;
arg@239 237 XSetInputFocus(dpy, root, RevertToPointerRoot, CurrentTime);
arg@239 238 drawall();
arg@239 239 }
garbeam@5 240 }
garbeam@5 241
garbeam@5 242 static void
arg@461 243 expose(XEvent *e) {
arg@123 244 Client *c;
garbeam@5 245 XExposeEvent *ev = &e->xexpose;
garbeam@5 246
garbeam@5 247 if(ev->count == 0) {
garbeam@70 248 if(barwin == ev->window)
garbeam@74 249 drawstatus();
garbeam@75 250 else if((c = getctitle(ev->window)))
arg@647 251 drawclient(c);
garbeam@5 252 }
garbeam@5 253 }
garbeam@5 254
garbeam@5 255 static void
arg@461 256 keypress(XEvent *e) {
arg@581 257 static unsigned int len = sizeof key / sizeof key[0];
arg@589 258 unsigned int i;
garbeam@76 259 KeySym keysym;
arg@123 260 XKeyEvent *ev = &e->xkey;
garbeam@76 261
garbeam@76 262 keysym = XKeycodeToKeysym(dpy, (KeyCode)ev->keycode, 0);
arg@275 263 for(i = 0; i < len; i++) {
arg@461 264 if(keysym == key[i].keysym
arg@461 265 && CLEANMASK(key[i].mod) == CLEANMASK(ev->state))
arg@275 266 {
arg@589 267 if(key[i].func)
arg@589 268 key[i].func(&key[i].arg);
garbeam@76 269 }
arg@275 270 }
garbeam@76 271 }
garbeam@76 272
garbeam@76 273 static void
arg@461 274 leavenotify(XEvent *e) {
garbeam@76 275 XCrossingEvent *ev = &e->xcrossing;
garbeam@76 276
arg@239 277 if((ev->window == root) && !ev->same_screen) {
arg@239 278 issel = False;
arg@239 279 drawall();
arg@239 280 }
garbeam@76 281 }
garbeam@76 282
garbeam@76 283 static void
arg@461 284 mappingnotify(XEvent *e) {
arg@279 285 XMappingEvent *ev = &e->xmapping;
arg@279 286
arg@279 287 XRefreshKeyboardMapping(ev);
arg@279 288 if(ev->request == MappingKeyboard)
arg@279 289 grabkeys();
arg@279 290 }
arg@279 291
arg@279 292 static void
arg@461 293 maprequest(XEvent *e) {
arg@123 294 static XWindowAttributes wa;
garbeam@5 295 XMapRequestEvent *ev = &e->xmaprequest;
garbeam@5 296
garbeam@5 297 if(!XGetWindowAttributes(dpy, ev->window, &wa))
garbeam@5 298 return;
garbeam@5 299 if(wa.override_redirect) {
garbeam@5 300 XSelectInput(dpy, ev->window,
garbeam@5 301 (StructureNotifyMask | PropertyChangeMask));
garbeam@5 302 return;
garbeam@5 303 }
garbeam@10 304 if(!getclient(ev->window))
garbeam@10 305 manage(ev->window, &wa);
garbeam@5 306 }
garbeam@5 307
garbeam@5 308 static void
arg@461 309 propertynotify(XEvent *e) {
arg@123 310 Client *c;
arg@123 311 Window trans;
garbeam@5 312 XPropertyEvent *ev = &e->xproperty;
garbeam@5 313
garbeam@5 314 if(ev->state == PropertyDelete)
garbeam@5 315 return; /* ignore */
garbeam@13 316 if((c = getclient(ev->window))) {
garbeam@77 317 if(ev->atom == wmatom[WMProtocols]) {
garbeam@75 318 c->proto = getproto(c->win);
garbeam@30 319 return;
garbeam@30 320 }
garbeam@13 321 switch (ev->atom) {
garbeam@13 322 default: break;
garbeam@13 323 case XA_WM_TRANSIENT_FOR:
garbeam@53 324 XGetTransientForHint(dpy, c->win, &trans);
garbeam@80 325 if(!c->isfloat && (c->isfloat = (trans != 0)))
arg@533 326 arrange();
garbeam@13 327 break;
garbeam@13 328 case XA_WM_NORMAL_HINTS:
arg@639 329 updatesizehints(c);
garbeam@13 330 break;
garbeam@13 331 }
garbeam@77 332 if(ev->atom == XA_WM_NAME || ev->atom == netatom[NetWMName]) {
arg@454 333 updatetitle(c);
arg@500 334 resizetitle(c);
arg@647 335 drawclient(c);
garbeam@13 336 }
garbeam@13 337 }
garbeam@5 338 }
garbeam@5 339
garbeam@5 340 static void
arg@461 341 unmapnotify(XEvent *e) {
garbeam@5 342 Client *c;
garbeam@5 343 XUnmapEvent *ev = &e->xunmap;
garbeam@5 344
garbeam@10 345 if((c = getclient(ev->window)))
garbeam@10 346 unmanage(c);
garbeam@5 347 }
garbeam@76 348
garbeam@84 349 /* extern */
garbeam@76 350
garbeam@76 351 void (*handler[LASTEvent]) (XEvent *) = {
garbeam@76 352 [ButtonPress] = buttonpress,
garbeam@76 353 [ConfigureRequest] = configurerequest,
garbeam@76 354 [DestroyNotify] = destroynotify,
garbeam@76 355 [EnterNotify] = enternotify,
garbeam@76 356 [LeaveNotify] = leavenotify,
garbeam@76 357 [Expose] = expose,
garbeam@76 358 [KeyPress] = keypress,
arg@279 359 [MappingNotify] = mappingnotify,
garbeam@76 360 [MapRequest] = maprequest,
garbeam@76 361 [PropertyNotify] = propertynotify,
garbeam@76 362 [UnmapNotify] = unmapnotify
garbeam@76 363 };
garbeam@76 364
garbeam@76 365 void
arg@487 366 grabkeys(void) {
arg@581 367 static unsigned int len = sizeof key / sizeof key[0];
garbeam@76 368 unsigned int i;
garbeam@76 369 KeyCode code;
garbeam@76 370
arg@279 371 XUngrabKey(dpy, AnyKey, AnyModifier, root);
garbeam@76 372 for(i = 0; i < len; i++) {
garbeam@76 373 code = XKeysymToKeycode(dpy, key[i].keysym);
garbeam@76 374 XGrabKey(dpy, code, key[i].mod, root, True,
garbeam@76 375 GrabModeAsync, GrabModeAsync);
arg@160 376 XGrabKey(dpy, code, key[i].mod | LockMask, root, True,
arg@160 377 GrabModeAsync, GrabModeAsync);
arg@291 378 XGrabKey(dpy, code, key[i].mod | numlockmask, root, True,
arg@146 379 GrabModeAsync, GrabModeAsync);
arg@291 380 XGrabKey(dpy, code, key[i].mod | numlockmask | LockMask, root, True,
arg@146 381 GrabModeAsync, GrabModeAsync);
garbeam@76 382 }
garbeam@76 383 }
arg@292 384
arg@292 385 void
arg@487 386 procevent(void) {
arg@292 387 XEvent ev;
arg@292 388
arg@292 389 while(XPending(dpy)) {
arg@292 390 XNextEvent(dpy, &ev);
arg@292 391 if(handler[ev.type])
arg@292 392 (handler[ev.type])(&ev); /* call handler */
arg@292 393 }
arg@292 394 }