dwm-meillo

annotate event.c @ 483:0db173d7e59c

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