aewl

annotate event.c @ 325:58c09c533d3f

removed badmalloc (thx for the pointer to Uriel)
author Anselm R. Garbe <arg@10kloc.org>
date Tue, 22 Aug 2006 16:06:11 +0200
parents 4aa632b6ba66
children 8a06efe5b563
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@267 108 x = 0;
arg@267 109 for(a.i = 0; a.i < ntags; a.i++) {
arg@267 110 x += textw(tags[a.i]);
arg@267 111 if(ev->x < x) {
arg@288 112 if(ev->button == Button1)
arg@288 113 view(&a);
arg@288 114 else if(ev->button == Button3)
arg@267 115 toggleview(&a);
arg@267 116 return;
garbeam@73 117 }
garbeam@73 118 }
garbeam@73 119 }
garbeam@58 120 else if((c = getclient(ev->window))) {
arg@143 121 focus(c);
arg@318 122 if(CLEANMASK(ev->state) == 0)
arg@318 123 return;
garbeam@18 124 switch(ev->button) {
garbeam@18 125 default:
garbeam@18 126 break;
garbeam@18 127 case Button1:
arg@270 128 if(!c->ismax && (arrange == dofloat || c->isfloat)) {
arg@270 129 restack(c);
arg@238 130 movemouse(c);
arg@270 131 }
arg@238 132 break;
arg@238 133 case Button2:
arg@248 134 zoom(NULL);
garbeam@18 135 break;
garbeam@18 136 case Button3:
arg@270 137 if(!c->ismax && (arrange == dofloat || c->isfloat)) {
arg@270 138 restack(c);
arg@99 139 resizemouse(c);
arg@270 140 }
garbeam@18 141 break;
garbeam@18 142 }
garbeam@18 143 }
garbeam@18 144 }
garbeam@18 145
garbeam@18 146 static void
garbeam@5 147 configurerequest(XEvent *e)
garbeam@5 148 {
arg@286 149 unsigned long newmask;
arg@123 150 Client *c;
garbeam@5 151 XConfigureRequestEvent *ev = &e->xconfigurerequest;
arg@164 152 XEvent synev;
garbeam@5 153 XWindowChanges wc;
garbeam@5 154
garbeam@18 155 if((c = getclient(ev->window))) {
garbeam@29 156 gravitate(c, True);
arg@195 157 if(ev->value_mask & CWX)
arg@195 158 c->x = ev->x;
arg@195 159 if(ev->value_mask & CWY)
arg@195 160 c->y = ev->y;
arg@195 161 if(ev->value_mask & CWWidth)
arg@195 162 c->w = ev->width;
arg@195 163 if(ev->value_mask & CWHeight)
arg@195 164 c->h = ev->height;
garbeam@29 165 if(ev->value_mask & CWBorderWidth)
arg@164 166 c->border = ev->border_width;
garbeam@29 167 gravitate(c, False);
arg@164 168 wc.x = c->x;
arg@164 169 wc.y = c->y;
arg@164 170 wc.width = c->w;
arg@164 171 wc.height = c->h;
arg@164 172 newmask = ev->value_mask & (~(CWSibling | CWStackMode | CWBorderWidth));
arg@164 173 if(newmask)
arg@164 174 XConfigureWindow(dpy, c->win, newmask, &wc);
arg@164 175 else {
arg@164 176 synev.type = ConfigureNotify;
arg@164 177 synev.xconfigure.display = dpy;
arg@164 178 synev.xconfigure.event = c->win;
arg@164 179 synev.xconfigure.window = c->win;
arg@164 180 synev.xconfigure.x = c->x;
arg@164 181 synev.xconfigure.y = c->y;
arg@164 182 synev.xconfigure.width = c->w;
arg@164 183 synev.xconfigure.height = c->h;
arg@164 184 synev.xconfigure.border_width = c->border;
arg@164 185 synev.xconfigure.above = None;
arg@164 186 /* Send synthetic ConfigureNotify */
arg@164 187 XSendEvent(dpy, c->win, True, NoEventMask, &synev);
arg@164 188 }
arg@195 189 XSync(dpy, False);
arg@196 190 if(c->isfloat)
arg@196 191 resize(c, False, TopLeft);
arg@196 192 else
arg@196 193 arrange(NULL);
garbeam@5 194 }
arg@164 195 else {
arg@164 196 wc.x = ev->x;
arg@164 197 wc.y = ev->y;
arg@164 198 wc.width = ev->width;
arg@164 199 wc.height = ev->height;
arg@164 200 wc.border_width = ev->border_width;
arg@164 201 wc.sibling = ev->above;
arg@164 202 wc.stack_mode = ev->detail;
arg@164 203 XConfigureWindow(dpy, ev->window, ev->value_mask, &wc);
arg@195 204 XSync(dpy, False);
arg@164 205 }
garbeam@5 206 }
garbeam@5 207
garbeam@5 208 static void
garbeam@5 209 destroynotify(XEvent *e)
garbeam@5 210 {
garbeam@5 211 Client *c;
garbeam@5 212 XDestroyWindowEvent *ev = &e->xdestroywindow;
garbeam@5 213
garbeam@11 214 if((c = getclient(ev->window)))
garbeam@11 215 unmanage(c);
garbeam@5 216 }
garbeam@5 217
garbeam@5 218 static void
garbeam@5 219 enternotify(XEvent *e)
garbeam@5 220 {
arg@123 221 Client *c;
garbeam@5 222 XCrossingEvent *ev = &e->xcrossing;
garbeam@5 223
arg@232 224 if(ev->mode != NotifyNormal || ev->detail == NotifyInferior)
garbeam@5 225 return;
garbeam@5 226
arg@161 227 if((c = getclient(ev->window)) || (c = getctitle(ev->window)))
garbeam@13 228 focus(c);
arg@239 229 else if(ev->window == root) {
garbeam@31 230 issel = True;
arg@239 231 XSetInputFocus(dpy, root, RevertToPointerRoot, CurrentTime);
arg@239 232 drawall();
arg@239 233 }
garbeam@5 234 }
garbeam@5 235
garbeam@5 236 static void
garbeam@5 237 expose(XEvent *e)
garbeam@5 238 {
arg@123 239 Client *c;
garbeam@5 240 XExposeEvent *ev = &e->xexpose;
garbeam@5 241
garbeam@5 242 if(ev->count == 0) {
garbeam@70 243 if(barwin == ev->window)
garbeam@74 244 drawstatus();
garbeam@75 245 else if((c = getctitle(ev->window)))
garbeam@74 246 drawtitle(c);
garbeam@5 247 }
garbeam@5 248 }
garbeam@5 249
garbeam@5 250 static void
garbeam@76 251 keypress(XEvent *e)
garbeam@76 252 {
arg@138 253 static unsigned int len = sizeof(key) / sizeof(key[0]);
garbeam@76 254 unsigned int i;
garbeam@76 255 KeySym keysym;
arg@123 256 XKeyEvent *ev = &e->xkey;
garbeam@76 257
garbeam@76 258 keysym = XKeycodeToKeysym(dpy, (KeyCode)ev->keycode, 0);
arg@275 259 for(i = 0; i < len; i++) {
arg@160 260 if(keysym == key[i].keysym &&
arg@275 261 CLEANMASK(key[i].mod) == CLEANMASK(ev->state))
arg@275 262 {
garbeam@76 263 if(key[i].func)
garbeam@76 264 key[i].func(&key[i].arg);
garbeam@76 265 return;
garbeam@76 266 }
arg@275 267 }
garbeam@76 268 }
garbeam@76 269
garbeam@76 270 static void
garbeam@76 271 leavenotify(XEvent *e)
garbeam@76 272 {
garbeam@76 273 XCrossingEvent *ev = &e->xcrossing;
garbeam@76 274
arg@239 275 if((ev->window == root) && !ev->same_screen) {
arg@239 276 issel = False;
arg@239 277 drawall();
arg@239 278 }
garbeam@76 279 }
garbeam@76 280
garbeam@76 281 static void
arg@279 282 mappingnotify(XEvent *e)
arg@279 283 {
arg@279 284 XMappingEvent *ev = &e->xmapping;
arg@279 285
arg@279 286 XRefreshKeyboardMapping(ev);
arg@279 287 if(ev->request == MappingKeyboard)
arg@279 288 grabkeys();
arg@279 289 }
arg@279 290
arg@279 291 static void
garbeam@5 292 maprequest(XEvent *e)
garbeam@5 293 {
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
garbeam@5 300 if(wa.override_redirect) {
garbeam@5 301 XSelectInput(dpy, ev->window,
garbeam@5 302 (StructureNotifyMask | PropertyChangeMask));
garbeam@5 303 return;
garbeam@5 304 }
garbeam@5 305
garbeam@10 306 if(!getclient(ev->window))
garbeam@10 307 manage(ev->window, &wa);
garbeam@5 308 }
garbeam@5 309
garbeam@5 310 static void
garbeam@5 311 propertynotify(XEvent *e)
garbeam@5 312 {
arg@123 313 Client *c;
arg@123 314 Window trans;
garbeam@5 315 XPropertyEvent *ev = &e->xproperty;
garbeam@5 316
garbeam@5 317 if(ev->state == PropertyDelete)
garbeam@5 318 return; /* ignore */
garbeam@5 319
garbeam@13 320 if((c = getclient(ev->window))) {
garbeam@77 321 if(ev->atom == wmatom[WMProtocols]) {
garbeam@75 322 c->proto = getproto(c->win);
garbeam@30 323 return;
garbeam@30 324 }
garbeam@13 325 switch (ev->atom) {
garbeam@13 326 default: break;
garbeam@13 327 case XA_WM_TRANSIENT_FOR:
garbeam@53 328 XGetTransientForHint(dpy, c->win, &trans);
garbeam@80 329 if(!c->isfloat && (c->isfloat = (trans != 0)))
garbeam@53 330 arrange(NULL);
garbeam@13 331 break;
garbeam@13 332 case XA_WM_NORMAL_HINTS:
garbeam@74 333 setsize(c);
garbeam@13 334 break;
garbeam@13 335 }
garbeam@77 336 if(ev->atom == XA_WM_NAME || ev->atom == netatom[NetWMName]) {
garbeam@74 337 settitle(c);
garbeam@74 338 drawtitle(c);
garbeam@13 339 }
garbeam@13 340 }
garbeam@5 341 }
garbeam@5 342
garbeam@5 343 static void
garbeam@5 344 unmapnotify(XEvent *e)
garbeam@5 345 {
garbeam@5 346 Client *c;
garbeam@5 347 XUnmapEvent *ev = &e->xunmap;
garbeam@5 348
garbeam@10 349 if((c = getclient(ev->window)))
garbeam@10 350 unmanage(c);
garbeam@5 351 }
garbeam@76 352
garbeam@84 353 /* extern */
garbeam@76 354
garbeam@76 355 void (*handler[LASTEvent]) (XEvent *) = {
garbeam@76 356 [ButtonPress] = buttonpress,
garbeam@76 357 [ConfigureRequest] = configurerequest,
garbeam@76 358 [DestroyNotify] = destroynotify,
garbeam@76 359 [EnterNotify] = enternotify,
garbeam@76 360 [LeaveNotify] = leavenotify,
garbeam@76 361 [Expose] = expose,
garbeam@76 362 [KeyPress] = keypress,
arg@279 363 [MappingNotify] = mappingnotify,
garbeam@76 364 [MapRequest] = maprequest,
garbeam@76 365 [PropertyNotify] = propertynotify,
garbeam@76 366 [UnmapNotify] = unmapnotify
garbeam@76 367 };
garbeam@76 368
garbeam@76 369 void
garbeam@76 370 grabkeys()
garbeam@76 371 {
arg@138 372 static unsigned int len = sizeof(key) / sizeof(key[0]);
garbeam@76 373 unsigned int i;
garbeam@76 374 KeyCode code;
garbeam@76 375
arg@279 376 XUngrabKey(dpy, AnyKey, AnyModifier, root);
garbeam@76 377 for(i = 0; i < len; i++) {
garbeam@76 378 code = XKeysymToKeycode(dpy, key[i].keysym);
garbeam@76 379 XGrabKey(dpy, code, key[i].mod, root, True,
garbeam@76 380 GrabModeAsync, GrabModeAsync);
arg@160 381 XGrabKey(dpy, code, key[i].mod | LockMask, root, True,
arg@160 382 GrabModeAsync, GrabModeAsync);
arg@291 383 XGrabKey(dpy, code, key[i].mod | numlockmask, root, True,
arg@146 384 GrabModeAsync, GrabModeAsync);
arg@291 385 XGrabKey(dpy, code, key[i].mod | numlockmask | LockMask, root, True,
arg@146 386 GrabModeAsync, GrabModeAsync);
garbeam@76 387 }
garbeam@76 388 }
arg@292 389
arg@292 390 void
arg@292 391 procevent()
arg@292 392 {
arg@292 393 XEvent ev;
arg@292 394
arg@292 395 while(XPending(dpy)) {
arg@292 396 XNextEvent(dpy, &ev);
arg@292 397 if(handler[ev.type])
arg@292 398 (handler[ev.type])(&ev); /* call handler */
arg@292 399 }
arg@292 400 }
arg@292 401