aewl

annotate event.c @ 98:f08394353588

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