dwm-meillo

annotate event.c @ 127:1480e19f6377

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