aewl

annotate event.c @ 110:268797d63de8

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