dwm-meillo

annotate event.c @ 491:12395ef46d97

added configure(), but this doesn't really fix those frking broken SDL apps
author arg@mmvi
date Tue, 26 Sep 2006 07:40:19 +0200
parents 303d3384720e
children 5df494262dfd
rev   line source
arg@491 1 #include <stdio.h>
garbeam@5 2 /*
garbeam@5 3 * (C)opyright MMVI Anselm R. Garbe <garbeam at gmail dot com>
garbeam@5 4 * See LICENSE file for license details.
garbeam@5 5 */
garbeam@76 6 #include "dwm.h"
garbeam@5 7 #include <stdlib.h>
garbeam@5 8 #include <X11/keysym.h>
garbeam@13 9 #include <X11/Xatom.h>
garbeam@5 10
arg@146 11 /* static */
arg@114 12
arg@114 13 typedef struct {
arg@114 14 unsigned long mod;
arg@114 15 KeySym keysym;
arg@114 16 void (*func)(Arg *arg);
arg@114 17 Arg arg;
arg@114 18 } Key;
arg@114 19
arg@146 20 KEYS
garbeam@75 21
arg@291 22 #define CLEANMASK(mask) (mask & ~(numlockmask | LockMask))
garbeam@5 23
garbeam@77 24 static void
arg@461 25 movemouse(Client *c) {
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;
arg@482 36 c->ismax = False;
garbeam@77 37 XQueryPointer(dpy, root, &dummy, &dummy, &x1, &y1, &di, &di, &dui);
garbeam@77 38 for(;;) {
arg@489 39 XMaskEvent(dpy, MOUSEMASK | ExposureMask, &ev);
garbeam@77 40 switch (ev.type) {
arg@490 41 case ButtonRelease:
arg@491 42 resize(c, True, TopLeft);
arg@490 43 XUngrabPointer(dpy, CurrentTime);
arg@490 44 return;
garbeam@77 45 case Expose:
garbeam@77 46 handler[Expose](&ev);
garbeam@77 47 break;
garbeam@77 48 case MotionNotify:
garbeam@79 49 XSync(dpy, False);
arg@115 50 c->x = ocx + (ev.xmotion.x - x1);
arg@115 51 c->y = ocy + (ev.xmotion.y - y1);
arg@99 52 resize(c, False, TopLeft);
garbeam@77 53 break;
garbeam@77 54 }
garbeam@77 55 }
garbeam@77 56 }
garbeam@77 57
garbeam@77 58 static void
arg@461 59 resizemouse(Client *c) {
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@482 70 c->ismax = False;
arg@115 71 XWarpPointer(dpy, None, c->win, 0, 0, 0, 0, c->w, c->h);
garbeam@77 72 for(;;) {
arg@489 73 XMaskEvent(dpy, MOUSEMASK | ExposureMask, &ev);
garbeam@77 74 switch(ev.type) {
arg@490 75 case ButtonRelease:
arg@491 76 resize(c, True, TopLeft);
arg@490 77 XUngrabPointer(dpy, CurrentTime);
arg@490 78 return;
garbeam@77 79 case Expose:
garbeam@77 80 handler[Expose](&ev);
garbeam@77 81 break;
garbeam@77 82 case MotionNotify:
garbeam@79 83 XSync(dpy, False);
arg@268 84 if((nw = abs(ocx - ev.xmotion.x)))
arg@478 85 c->w = nw;
arg@268 86 if((nh = abs(ocy - ev.xmotion.y)))
arg@478 87 c->h = nh;
arg@115 88 c->x = (ocx <= ev.xmotion.x) ? ocx : ocx - c->w;
arg@115 89 c->y = (ocy <= ev.xmotion.y) ? ocy : ocy - c->h;
arg@105 90 if(ocx <= ev.xmotion.x)
arg@105 91 sticky = (ocy <= ev.xmotion.y) ? TopLeft : BotLeft;
arg@105 92 else
arg@105 93 sticky = (ocy <= ev.xmotion.y) ? TopRight : BotRight;
arg@99 94 resize(c, True, sticky);
garbeam@77 95 break;
garbeam@77 96 }
garbeam@77 97 }
garbeam@77 98 }
garbeam@73 99
garbeam@73 100 static void
arg@461 101 buttonpress(XEvent *e) {
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@362 108 x = 0;
arg@362 109 for(a.i = 0; a.i < ntags; a.i++) {
arg@362 110 x += textw(tags[a.i]);
arg@362 111 if(ev->x < x) {
arg@399 112 if(ev->button == Button1) {
arg@398 113 if(ev->state & MODKEY)
arg@398 114 tag(&a);
arg@398 115 else
arg@398 116 view(&a);
arg@399 117 }
arg@399 118 else if(ev->button == Button3) {
arg@398 119 if(ev->state & MODKEY)
arg@398 120 toggletag(&a);
arg@398 121 else
arg@398 122 toggleview(&a);
arg@394 123 }
arg@362 124 return;
garbeam@73 125 }
garbeam@73 126 }
arg@371 127 if(ev->x < x + bmw) {
arg@371 128 if(ev->button == Button1)
arg@371 129 togglemode(NULL);
arg@371 130 }
garbeam@73 131 }
garbeam@58 132 else if((c = getclient(ev->window))) {
arg@143 133 focus(c);
arg@473 134 if(CLEANMASK(ev->state) != MODKEY)
arg@318 135 return;
arg@400 136 if(ev->button == Button1 && (arrange == dofloat || c->isfloat)) {
arg@487 137 restack();
arg@399 138 movemouse(c);
arg@399 139 }
arg@399 140 else if(ev->button == Button2)
arg@248 141 zoom(NULL);
arg@400 142 else if(ev->button == Button3 && (arrange == dofloat || c->isfloat)) {
arg@487 143 restack();
arg@399 144 resizemouse(c);
garbeam@18 145 }
garbeam@18 146 }
garbeam@18 147 }
garbeam@18 148
garbeam@18 149 static void
arg@461 150 configurerequest(XEvent *e) {
arg@286 151 unsigned long newmask;
arg@123 152 Client *c;
garbeam@5 153 XConfigureRequestEvent *ev = &e->xconfigurerequest;
arg@489 154 XEvent synev;
garbeam@5 155 XWindowChanges wc;
garbeam@5 156
arg@491 157 fputs("configurerequest\n", stderr);
garbeam@18 158 if((c = getclient(ev->window))) {
arg@488 159 c->ismax = False;
garbeam@29 160 gravitate(c, True);
arg@195 161 if(ev->value_mask & CWX)
arg@195 162 c->x = ev->x;
arg@195 163 if(ev->value_mask & CWY)
arg@195 164 c->y = ev->y;
arg@195 165 if(ev->value_mask & CWWidth)
arg@195 166 c->w = ev->width;
arg@195 167 if(ev->value_mask & CWHeight)
arg@195 168 c->h = ev->height;
garbeam@29 169 if(ev->value_mask & CWBorderWidth)
arg@164 170 c->border = ev->border_width;
garbeam@29 171 gravitate(c, False);
arg@164 172 wc.x = c->x;
arg@164 173 wc.y = c->y;
arg@164 174 wc.width = c->w;
arg@164 175 wc.height = c->h;
arg@164 176 newmask = ev->value_mask & (~(CWSibling | CWStackMode | CWBorderWidth));
arg@164 177 if(newmask)
arg@164 178 XConfigureWindow(dpy, c->win, newmask, &wc);
arg@491 179 else
arg@491 180 configure(c);
arg@195 181 XSync(dpy, False);
arg@385 182 if(c->isfloat)
arg@385 183 resize(c, False, TopLeft);
arg@196 184 else
arg@196 185 arrange(NULL);
garbeam@5 186 }
arg@164 187 else {
arg@164 188 wc.x = ev->x;
arg@164 189 wc.y = ev->y;
arg@164 190 wc.width = ev->width;
arg@164 191 wc.height = ev->height;
arg@164 192 wc.border_width = ev->border_width;
arg@164 193 wc.sibling = ev->above;
arg@164 194 wc.stack_mode = ev->detail;
arg@164 195 XConfigureWindow(dpy, ev->window, ev->value_mask, &wc);
arg@195 196 XSync(dpy, False);
arg@164 197 }
garbeam@5 198 }
garbeam@5 199
garbeam@5 200 static void
arg@461 201 destroynotify(XEvent *e) {
garbeam@5 202 Client *c;
garbeam@5 203 XDestroyWindowEvent *ev = &e->xdestroywindow;
garbeam@5 204
garbeam@11 205 if((c = getclient(ev->window)))
garbeam@11 206 unmanage(c);
garbeam@5 207 }
garbeam@5 208
garbeam@5 209 static void
arg@461 210 enternotify(XEvent *e) {
arg@123 211 Client *c;
garbeam@5 212 XCrossingEvent *ev = &e->xcrossing;
garbeam@5 213
arg@491 214 fputs("enternotify\n", stderr);
arg@232 215 if(ev->mode != NotifyNormal || ev->detail == NotifyInferior)
garbeam@5 216 return;
garbeam@5 217
arg@466 218 if(((c = getclient(ev->window)) || (c = getctitle(ev->window))) && isvisible(c))
garbeam@13 219 focus(c);
arg@239 220 else if(ev->window == root) {
garbeam@31 221 issel = True;
arg@239 222 XSetInputFocus(dpy, root, RevertToPointerRoot, CurrentTime);
arg@239 223 drawall();
arg@239 224 }
garbeam@5 225 }
garbeam@5 226
garbeam@5 227 static void
arg@461 228 expose(XEvent *e) {
arg@123 229 Client *c;
garbeam@5 230 XExposeEvent *ev = &e->xexpose;
garbeam@5 231
garbeam@5 232 if(ev->count == 0) {
garbeam@70 233 if(barwin == ev->window)
garbeam@74 234 drawstatus();
garbeam@75 235 else if((c = getctitle(ev->window)))
garbeam@74 236 drawtitle(c);
garbeam@5 237 }
garbeam@5 238 }
garbeam@5 239
garbeam@5 240 static void
arg@461 241 keypress(XEvent *e) {
arg@138 242 static unsigned int len = sizeof(key) / sizeof(key[0]);
garbeam@76 243 unsigned int i;
garbeam@76 244 KeySym keysym;
arg@123 245 XKeyEvent *ev = &e->xkey;
garbeam@76 246
garbeam@76 247 keysym = XKeycodeToKeysym(dpy, (KeyCode)ev->keycode, 0);
arg@275 248 for(i = 0; i < len; i++) {
arg@461 249 if(keysym == key[i].keysym
arg@461 250 && CLEANMASK(key[i].mod) == CLEANMASK(ev->state))
arg@275 251 {
garbeam@76 252 if(key[i].func)
garbeam@76 253 key[i].func(&key[i].arg);
garbeam@76 254 return;
garbeam@76 255 }
arg@275 256 }
garbeam@76 257 }
garbeam@76 258
garbeam@76 259 static void
arg@461 260 leavenotify(XEvent *e) {
garbeam@76 261 XCrossingEvent *ev = &e->xcrossing;
garbeam@76 262
arg@239 263 if((ev->window == root) && !ev->same_screen) {
arg@239 264 issel = False;
arg@239 265 drawall();
arg@239 266 }
garbeam@76 267 }
garbeam@76 268
garbeam@76 269 static void
arg@461 270 mappingnotify(XEvent *e) {
arg@279 271 XMappingEvent *ev = &e->xmapping;
arg@279 272
arg@279 273 XRefreshKeyboardMapping(ev);
arg@279 274 if(ev->request == MappingKeyboard)
arg@279 275 grabkeys();
arg@279 276 }
arg@279 277
arg@279 278 static void
arg@461 279 maprequest(XEvent *e) {
arg@123 280 static XWindowAttributes wa;
garbeam@5 281 XMapRequestEvent *ev = &e->xmaprequest;
garbeam@5 282
garbeam@5 283 if(!XGetWindowAttributes(dpy, ev->window, &wa))
garbeam@5 284 return;
garbeam@5 285
garbeam@5 286 if(wa.override_redirect) {
garbeam@5 287 XSelectInput(dpy, ev->window,
garbeam@5 288 (StructureNotifyMask | PropertyChangeMask));
garbeam@5 289 return;
garbeam@5 290 }
garbeam@5 291
garbeam@10 292 if(!getclient(ev->window))
garbeam@10 293 manage(ev->window, &wa);
garbeam@5 294 }
garbeam@5 295
garbeam@5 296 static void
arg@461 297 propertynotify(XEvent *e) {
arg@123 298 Client *c;
arg@123 299 Window trans;
garbeam@5 300 XPropertyEvent *ev = &e->xproperty;
garbeam@5 301
arg@491 302 fputs("propertynotify\n", stderr);
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);
garbeam@74 324 drawtitle(c);
garbeam@13 325 }
garbeam@13 326 }
garbeam@5 327 }
garbeam@5 328
garbeam@5 329 static void
arg@461 330 unmapnotify(XEvent *e) {
garbeam@5 331 Client *c;
garbeam@5 332 XUnmapEvent *ev = &e->xunmap;
garbeam@5 333
garbeam@10 334 if((c = getclient(ev->window)))
garbeam@10 335 unmanage(c);
garbeam@5 336 }
garbeam@76 337
garbeam@84 338 /* extern */
garbeam@76 339
garbeam@76 340 void (*handler[LASTEvent]) (XEvent *) = {
garbeam@76 341 [ButtonPress] = buttonpress,
garbeam@76 342 [ConfigureRequest] = configurerequest,
garbeam@76 343 [DestroyNotify] = destroynotify,
garbeam@76 344 [EnterNotify] = enternotify,
garbeam@76 345 [LeaveNotify] = leavenotify,
garbeam@76 346 [Expose] = expose,
garbeam@76 347 [KeyPress] = keypress,
arg@279 348 [MappingNotify] = mappingnotify,
garbeam@76 349 [MapRequest] = maprequest,
garbeam@76 350 [PropertyNotify] = propertynotify,
garbeam@76 351 [UnmapNotify] = unmapnotify
garbeam@76 352 };
garbeam@76 353
garbeam@76 354 void
arg@487 355 grabkeys(void) {
arg@138 356 static unsigned int len = sizeof(key) / sizeof(key[0]);
garbeam@76 357 unsigned int i;
garbeam@76 358 KeyCode code;
garbeam@76 359
arg@279 360 XUngrabKey(dpy, AnyKey, AnyModifier, root);
garbeam@76 361 for(i = 0; i < len; i++) {
garbeam@76 362 code = XKeysymToKeycode(dpy, key[i].keysym);
garbeam@76 363 XGrabKey(dpy, code, key[i].mod, root, True,
garbeam@76 364 GrabModeAsync, GrabModeAsync);
arg@160 365 XGrabKey(dpy, code, key[i].mod | LockMask, root, True,
arg@160 366 GrabModeAsync, GrabModeAsync);
arg@291 367 XGrabKey(dpy, code, key[i].mod | numlockmask, root, True,
arg@146 368 GrabModeAsync, GrabModeAsync);
arg@291 369 XGrabKey(dpy, code, key[i].mod | numlockmask | LockMask, root, True,
arg@146 370 GrabModeAsync, GrabModeAsync);
garbeam@76 371 }
garbeam@76 372 }
arg@292 373
arg@292 374 void
arg@487 375 procevent(void) {
arg@292 376 XEvent ev;
arg@292 377
arg@292 378 while(XPending(dpy)) {
arg@292 379 XNextEvent(dpy, &ev);
arg@292 380 if(handler[ev.type])
arg@292 381 (handler[ev.type])(&ev); /* call handler */
arg@292 382 }
arg@292 383 }