aewl

annotate event.c @ 145:774754477c35

applied Cedric's NumLock patch
author arg@10ksloc.org
date Tue, 01 Aug 2006 12:41:38 +0200
parents e61447a7f249
children f328ce9c558c
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
garbeam@5 7 #include <stdlib.h>
garbeam@5 8 #include <X11/keysym.h>
garbeam@13 9 #include <X11/Xatom.h>
garbeam@5 10
garbeam@84 11 /* CUSTOMIZE */
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@111 20 const char *browse[] = { "firefox", NULL };
arg@111 21 const char *gimp[] = { "gimp", NULL };
arg@143 22 const char *term[] = { /*"xterm", NULL };*/
arg@137 23 "urxvt", "-tr", "+sb", "-bg", "black", "-fg", "white", "-cr", "white",
arg@111 24 "-fn", "-*-terminus-medium-*-*-*-13-*-*-*-*-*-iso10646-*", NULL
arg@111 25 };
arg@123 26 const char *xlock[] = { "xlock", NULL };
garbeam@75 27
arg@114 28 static Key key[] = {
arg@123 29 /* modifier key function arguments */
arg@130 30 { MODKEY, XK_0, view, { .i = Tfnord } },
arg@123 31 { MODKEY, XK_1, view, { .i = Tdev } },
arg@130 32 { MODKEY, XK_2, view, { .i = Tnet } },
arg@123 33 { MODKEY, XK_3, view, { .i = Twork } },
arg@130 34 { MODKEY, XK_4, view, { .i = Tmisc} },
arg@144 35 { MODKEY, XK_h, viewprev, { 0 } },
arg@123 36 { MODKEY, XK_j, focusnext, { 0 } },
arg@123 37 { MODKEY, XK_k, focusprev, { 0 } },
arg@144 38 { MODKEY, XK_l, viewnext, { 0 } },
arg@124 39 { MODKEY, XK_m, togglemax, { 0 } },
arg@124 40 { MODKEY, XK_space, togglemode, { 0 } },
arg@123 41 { MODKEY, XK_Return, zoom, { 0 } },
arg@130 42 { MODKEY|ControlMask, XK_0, appendtag, { .i = Tfnord } },
arg@130 43 { MODKEY|ControlMask, XK_1, appendtag, { .i = Tdev } },
arg@130 44 { MODKEY|ControlMask, XK_2, appendtag, { .i = Tnet } },
arg@130 45 { MODKEY|ControlMask, XK_3, appendtag, { .i = Twork } },
arg@130 46 { MODKEY|ControlMask, XK_4, appendtag, { .i = Tmisc } },
arg@130 47 { MODKEY|ShiftMask, XK_0, replacetag, { .i = Tfnord } },
arg@123 48 { MODKEY|ShiftMask, XK_1, replacetag, { .i = Tdev } },
arg@130 49 { MODKEY|ShiftMask, XK_2, replacetag, { .i = Tnet } },
arg@123 50 { MODKEY|ShiftMask, XK_3, replacetag, { .i = Twork } },
arg@130 51 { MODKEY|ShiftMask, XK_4, replacetag, { .i = Tmisc } },
arg@123 52 { MODKEY|ShiftMask, XK_c, killclient, { 0 } },
arg@139 53 { MODKEY|ShiftMask, XK_q, quit, { 0 } },
arg@139 54 { MODKEY|ShiftMask, XK_Return, spawn, { .argv = term } },
arg@123 55 { MODKEY|ShiftMask, XK_g, spawn, { .argv = gimp } },
arg@123 56 { MODKEY|ShiftMask, XK_l, spawn, { .argv = xlock } },
arg@123 57 { MODKEY|ShiftMask, XK_w, spawn, { .argv = browse } },
garbeam@75 58 };
garbeam@75 59
arg@145 60 #define NumLockMask Mod2Mask
arg@145 61 unsigned int valid_mask = 255 & ~( NumLockMask | LockMask);
arg@145 62
arg@125 63 /* END CUSTOMIZE */
arg@125 64
garbeam@84 65 /* static */
garbeam@5 66
garbeam@77 67 static void
garbeam@77 68 movemouse(Client *c)
garbeam@77 69 {
garbeam@77 70 int x1, y1, ocx, ocy, di;
garbeam@77 71 unsigned int dui;
garbeam@77 72 Window dummy;
arg@123 73 XEvent ev;
garbeam@77 74
arg@115 75 ocx = c->x;
arg@115 76 ocy = c->y;
garbeam@77 77 if(XGrabPointer(dpy, root, False, MouseMask, GrabModeAsync, GrabModeAsync,
arg@123 78 None, cursor[CurMove], CurrentTime) != GrabSuccess)
garbeam@77 79 return;
garbeam@77 80 XQueryPointer(dpy, root, &dummy, &dummy, &x1, &y1, &di, &di, &dui);
garbeam@77 81 for(;;) {
garbeam@77 82 XMaskEvent(dpy, MouseMask | ExposureMask, &ev);
garbeam@77 83 switch (ev.type) {
garbeam@77 84 default: break;
garbeam@77 85 case Expose:
garbeam@77 86 handler[Expose](&ev);
garbeam@77 87 break;
garbeam@77 88 case MotionNotify:
garbeam@79 89 XSync(dpy, False);
arg@115 90 c->x = ocx + (ev.xmotion.x - x1);
arg@115 91 c->y = ocy + (ev.xmotion.y - y1);
arg@99 92 resize(c, False, TopLeft);
garbeam@77 93 break;
garbeam@77 94 case ButtonRelease:
garbeam@77 95 XUngrabPointer(dpy, CurrentTime);
garbeam@77 96 return;
garbeam@77 97 }
garbeam@77 98 }
garbeam@77 99 }
garbeam@77 100
garbeam@77 101 static void
garbeam@77 102 resizemouse(Client *c)
garbeam@77 103 {
garbeam@77 104 int ocx, ocy;
arg@99 105 Corner sticky;
arg@123 106 XEvent ev;
garbeam@77 107
arg@115 108 ocx = c->x;
arg@115 109 ocy = c->y;
garbeam@77 110 if(XGrabPointer(dpy, root, False, MouseMask, GrabModeAsync, GrabModeAsync,
garbeam@77 111 None, cursor[CurResize], CurrentTime) != GrabSuccess)
garbeam@77 112 return;
arg@115 113 XWarpPointer(dpy, None, c->win, 0, 0, 0, 0, c->w, c->h);
garbeam@77 114 for(;;) {
garbeam@77 115 XMaskEvent(dpy, MouseMask | ExposureMask, &ev);
garbeam@77 116 switch(ev.type) {
garbeam@77 117 default: break;
garbeam@77 118 case Expose:
garbeam@77 119 handler[Expose](&ev);
garbeam@77 120 break;
garbeam@77 121 case MotionNotify:
garbeam@79 122 XSync(dpy, False);
arg@115 123 c->w = abs(ocx - ev.xmotion.x);
arg@115 124 c->h = abs(ocy - ev.xmotion.y);
arg@115 125 c->x = (ocx <= ev.xmotion.x) ? ocx : ocx - c->w;
arg@115 126 c->y = (ocy <= ev.xmotion.y) ? ocy : ocy - c->h;
arg@105 127 if(ocx <= ev.xmotion.x)
arg@105 128 sticky = (ocy <= ev.xmotion.y) ? TopLeft : BotLeft;
arg@105 129 else
arg@105 130 sticky = (ocy <= ev.xmotion.y) ? TopRight : BotRight;
arg@99 131 resize(c, True, sticky);
garbeam@77 132 break;
garbeam@77 133 case ButtonRelease:
garbeam@77 134 XUngrabPointer(dpy, CurrentTime);
garbeam@77 135 return;
garbeam@77 136 }
garbeam@77 137 }
garbeam@77 138 }
garbeam@73 139
garbeam@73 140 static void
garbeam@18 141 buttonpress(XEvent *e)
garbeam@18 142 {
garbeam@73 143 int x;
garbeam@73 144 Arg a;
arg@123 145 Client *c;
garbeam@18 146 XButtonPressedEvent *ev = &e->xbutton;
garbeam@18 147
garbeam@73 148 if(barwin == ev->window) {
garbeam@80 149 switch(ev->button) {
garbeam@80 150 default:
garbeam@80 151 x = 0;
garbeam@80 152 for(a.i = 0; a.i < TLast; a.i++) {
garbeam@80 153 x += textw(tags[a.i]);
garbeam@80 154 if(ev->x < x) {
garbeam@80 155 view(&a);
garbeam@80 156 break;
garbeam@80 157 }
garbeam@73 158 }
garbeam@80 159 break;
garbeam@80 160 case Button4:
garbeam@80 161 a.i = (tsel + 1 < TLast) ? tsel + 1 : 0;
garbeam@80 162 view(&a);
garbeam@80 163 break;
garbeam@80 164 case Button5:
garbeam@80 165 a.i = (tsel - 1 >= 0) ? tsel - 1 : TLast - 1;
garbeam@80 166 view(&a);
garbeam@80 167 break;
garbeam@73 168 }
garbeam@73 169 }
garbeam@58 170 else if((c = getclient(ev->window))) {
arg@143 171 focus(c);
garbeam@18 172 switch(ev->button) {
garbeam@18 173 default:
garbeam@18 174 break;
garbeam@18 175 case Button1:
arg@133 176 if(!c->ismax && (arrange == dofloat || c->isfloat)) {
arg@99 177 higher(c);
arg@99 178 movemouse(c);
arg@99 179 }
garbeam@18 180 break;
garbeam@18 181 case Button2:
garbeam@26 182 lower(c);
garbeam@18 183 break;
garbeam@18 184 case Button3:
arg@133 185 if(!c->ismax && (arrange == dofloat || c->isfloat)) {
arg@99 186 higher(c);
arg@99 187 resizemouse(c);
arg@99 188 }
garbeam@18 189 break;
garbeam@18 190 }
garbeam@18 191 }
garbeam@18 192 }
garbeam@18 193
garbeam@18 194 static void
garbeam@5 195 configurerequest(XEvent *e)
garbeam@5 196 {
arg@123 197 Client *c;
garbeam@5 198 XConfigureRequestEvent *ev = &e->xconfigurerequest;
garbeam@5 199 XWindowChanges wc;
garbeam@5 200
garbeam@5 201 ev->value_mask &= ~CWSibling;
garbeam@18 202 if((c = getclient(ev->window))) {
garbeam@29 203 gravitate(c, True);
garbeam@5 204 if(ev->value_mask & CWX)
arg@115 205 c->x = ev->x;
garbeam@5 206 if(ev->value_mask & CWY)
arg@115 207 c->y = ev->y;
garbeam@5 208 if(ev->value_mask & CWWidth)
arg@115 209 c->w = ev->width;
garbeam@5 210 if(ev->value_mask & CWHeight)
arg@115 211 c->h = ev->height;
garbeam@29 212 if(ev->value_mask & CWBorderWidth)
garbeam@55 213 c->border = 1;
garbeam@29 214 gravitate(c, False);
arg@99 215 resize(c, True, TopLeft);
garbeam@5 216 }
garbeam@5 217
garbeam@5 218 wc.x = ev->x;
garbeam@5 219 wc.y = ev->y;
garbeam@5 220 wc.width = ev->width;
garbeam@5 221 wc.height = ev->height;
garbeam@25 222 wc.border_width = 1;
garbeam@5 223 wc.sibling = None;
garbeam@5 224 wc.stack_mode = Above;
garbeam@5 225 ev->value_mask &= ~CWStackMode;
garbeam@5 226 ev->value_mask |= CWBorderWidth;
garbeam@5 227 XConfigureWindow(dpy, ev->window, ev->value_mask, &wc);
garbeam@79 228 XSync(dpy, False);
garbeam@5 229 }
garbeam@5 230
garbeam@5 231 static void
garbeam@5 232 destroynotify(XEvent *e)
garbeam@5 233 {
garbeam@5 234 Client *c;
garbeam@5 235 XDestroyWindowEvent *ev = &e->xdestroywindow;
garbeam@5 236
garbeam@11 237 if((c = getclient(ev->window)))
garbeam@11 238 unmanage(c);
garbeam@5 239 }
garbeam@5 240
garbeam@5 241 static void
garbeam@5 242 enternotify(XEvent *e)
garbeam@5 243 {
arg@123 244 Client *c;
garbeam@5 245 XCrossingEvent *ev = &e->xcrossing;
garbeam@5 246
arg@143 247 if(ev->detail == NotifyInferior)
garbeam@5 248 return;
garbeam@5 249
garbeam@13 250 if((c = getclient(ev->window)))
garbeam@13 251 focus(c);
garbeam@26 252 else if(ev->window == root)
garbeam@31 253 issel = True;
garbeam@5 254 }
garbeam@5 255
garbeam@5 256 static void
garbeam@5 257 expose(XEvent *e)
garbeam@5 258 {
arg@123 259 Client *c;
garbeam@5 260 XExposeEvent *ev = &e->xexpose;
garbeam@5 261
garbeam@5 262 if(ev->count == 0) {
garbeam@70 263 if(barwin == ev->window)
garbeam@74 264 drawstatus();
garbeam@75 265 else if((c = getctitle(ev->window)))
garbeam@74 266 drawtitle(c);
garbeam@5 267 }
garbeam@5 268 }
garbeam@5 269
garbeam@5 270 static void
garbeam@76 271 keypress(XEvent *e)
garbeam@76 272 {
arg@138 273 static unsigned int len = sizeof(key) / sizeof(key[0]);
garbeam@76 274 unsigned int i;
garbeam@76 275 KeySym keysym;
arg@123 276 XKeyEvent *ev = &e->xkey;
arg@145 277 ev->state &= valid_mask;
garbeam@76 278
garbeam@76 279 keysym = XKeycodeToKeysym(dpy, (KeyCode)ev->keycode, 0);
garbeam@76 280 for(i = 0; i < len; i++)
arg@145 281 if((keysym == key[i].keysym) && ((key[i].mod & valid_mask) == ev->state)) {
garbeam@76 282 if(key[i].func)
garbeam@76 283 key[i].func(&key[i].arg);
garbeam@76 284 return;
garbeam@76 285 }
garbeam@76 286 }
garbeam@76 287
garbeam@76 288 static void
garbeam@76 289 leavenotify(XEvent *e)
garbeam@76 290 {
garbeam@76 291 XCrossingEvent *ev = &e->xcrossing;
garbeam@76 292
garbeam@76 293 if((ev->window == root) && !ev->same_screen)
garbeam@76 294 issel = True;
garbeam@76 295 }
garbeam@76 296
garbeam@76 297 static void
garbeam@5 298 maprequest(XEvent *e)
garbeam@5 299 {
arg@123 300 static XWindowAttributes wa;
garbeam@5 301 XMapRequestEvent *ev = &e->xmaprequest;
garbeam@5 302
garbeam@5 303 if(!XGetWindowAttributes(dpy, ev->window, &wa))
garbeam@5 304 return;
garbeam@5 305
garbeam@5 306 if(wa.override_redirect) {
garbeam@5 307 XSelectInput(dpy, ev->window,
garbeam@5 308 (StructureNotifyMask | PropertyChangeMask));
garbeam@5 309 return;
garbeam@5 310 }
garbeam@5 311
garbeam@10 312 if(!getclient(ev->window))
garbeam@10 313 manage(ev->window, &wa);
garbeam@5 314 }
garbeam@5 315
garbeam@5 316 static void
garbeam@5 317 propertynotify(XEvent *e)
garbeam@5 318 {
arg@123 319 Client *c;
arg@123 320 Window trans;
garbeam@5 321 XPropertyEvent *ev = &e->xproperty;
garbeam@5 322
garbeam@5 323 if(ev->state == PropertyDelete)
garbeam@5 324 return; /* ignore */
garbeam@5 325
garbeam@13 326 if((c = getclient(ev->window))) {
garbeam@77 327 if(ev->atom == wmatom[WMProtocols]) {
garbeam@75 328 c->proto = getproto(c->win);
garbeam@30 329 return;
garbeam@30 330 }
garbeam@13 331 switch (ev->atom) {
garbeam@13 332 default: break;
garbeam@13 333 case XA_WM_TRANSIENT_FOR:
garbeam@53 334 XGetTransientForHint(dpy, c->win, &trans);
garbeam@80 335 if(!c->isfloat && (c->isfloat = (trans != 0)))
garbeam@53 336 arrange(NULL);
garbeam@13 337 break;
garbeam@13 338 case XA_WM_NORMAL_HINTS:
garbeam@74 339 setsize(c);
garbeam@13 340 break;
garbeam@13 341 }
garbeam@77 342 if(ev->atom == XA_WM_NAME || ev->atom == netatom[NetWMName]) {
garbeam@74 343 settitle(c);
garbeam@74 344 drawtitle(c);
garbeam@13 345 }
garbeam@13 346 }
garbeam@5 347 }
garbeam@5 348
garbeam@5 349 static void
garbeam@5 350 unmapnotify(XEvent *e)
garbeam@5 351 {
garbeam@5 352 Client *c;
garbeam@5 353 XUnmapEvent *ev = &e->xunmap;
garbeam@5 354
garbeam@10 355 if((c = getclient(ev->window)))
garbeam@10 356 unmanage(c);
garbeam@5 357 }
garbeam@76 358
garbeam@84 359 /* extern */
garbeam@76 360
garbeam@76 361 void (*handler[LASTEvent]) (XEvent *) = {
garbeam@76 362 [ButtonPress] = buttonpress,
garbeam@76 363 [ConfigureRequest] = configurerequest,
garbeam@76 364 [DestroyNotify] = destroynotify,
garbeam@76 365 [EnterNotify] = enternotify,
garbeam@76 366 [LeaveNotify] = leavenotify,
garbeam@76 367 [Expose] = expose,
garbeam@76 368 [KeyPress] = keypress,
garbeam@76 369 [MapRequest] = maprequest,
garbeam@76 370 [PropertyNotify] = propertynotify,
garbeam@76 371 [UnmapNotify] = unmapnotify
garbeam@76 372 };
garbeam@76 373
garbeam@76 374 void
garbeam@76 375 grabkeys()
garbeam@76 376 {
arg@138 377 static unsigned int len = sizeof(key) / sizeof(key[0]);
garbeam@76 378 unsigned int i;
garbeam@76 379 KeyCode code;
garbeam@76 380
garbeam@76 381 for(i = 0; i < len; i++) {
garbeam@76 382 code = XKeysymToKeycode(dpy, key[i].keysym);
garbeam@76 383 XUngrabKey(dpy, code, key[i].mod, root);
arg@145 384 if (NumLockMask)
arg@145 385 {
arg@145 386 XUngrabKey(dpy, code, key[i].mod | NumLockMask, root);
arg@145 387 XUngrabKey(dpy, code, key[i].mod | NumLockMask | LockMask, root);
arg@145 388 }
garbeam@76 389 XGrabKey(dpy, code, key[i].mod, root, True,
garbeam@76 390 GrabModeAsync, GrabModeAsync);
arg@145 391 if (NumLockMask)
arg@145 392 {
arg@145 393 XGrabKey(dpy, code, key[i].mod | NumLockMask, root, True,
arg@145 394 GrabModeAsync, GrabModeAsync);
arg@145 395 XGrabKey(dpy, code, key[i].mod | NumLockMask | LockMask, root, True,
arg@145 396 GrabModeAsync, GrabModeAsync);
arg@145 397 }
garbeam@76 398 }
garbeam@76 399 }