dwm-meillo

annotate event.c @ 101:17c526ee321c

fixed a typo
author arg@10ksloc.org
date Wed, 19 Jul 2006 13:24:58 +0200
parents 5d88952cbf96
children 3e74cc981e9b
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);
arg@99 82 resize(c, False, TopLeft);
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;
arg@99 96 Corner sticky;
garbeam@77 97
garbeam@95 98 ocx = *c->x;
garbeam@95 99 ocy = *c->y;
garbeam@77 100 if(XGrabPointer(dpy, root, False, MouseMask, GrabModeAsync, GrabModeAsync,
garbeam@77 101 None, cursor[CurResize], CurrentTime) != GrabSuccess)
garbeam@77 102 return;
garbeam@95 103 XWarpPointer(dpy, None, c->win, 0, 0, 0, 0, *c->w, *c->h);
garbeam@77 104 for(;;) {
garbeam@77 105 XMaskEvent(dpy, MouseMask | ExposureMask, &ev);
garbeam@77 106 switch(ev.type) {
garbeam@77 107 default: break;
garbeam@77 108 case Expose:
garbeam@77 109 handler[Expose](&ev);
garbeam@77 110 break;
garbeam@77 111 case MotionNotify:
garbeam@79 112 XSync(dpy, False);
garbeam@95 113 *c->w = abs(ocx - ev.xmotion.x);
garbeam@95 114 *c->h = abs(ocy - ev.xmotion.y);
garbeam@95 115 *c->x = (ocx <= ev.xmotion.x) ? ocx : ocx - *c->w;
garbeam@95 116 *c->y = (ocy <= ev.xmotion.y) ? ocy : ocy - *c->h;
arg@99 117 if(ocx <= ev.xmotion.x) {
arg@99 118 if(ocy <= ev.xmotion.y)
arg@99 119 sticky = TopLeft;
arg@99 120 else
arg@99 121 sticky = BottomLeft;
arg@99 122 } else {
arg@99 123 if(ocy <= ev.xmotion.y)
arg@99 124 sticky = TopRight;
arg@99 125 else
arg@99 126 sticky = BottomRight;
arg@99 127 }
arg@99 128 resize(c, True, sticky);
garbeam@77 129 break;
garbeam@77 130 case ButtonRelease:
garbeam@77 131 XUngrabPointer(dpy, CurrentTime);
garbeam@77 132 return;
garbeam@77 133 }
garbeam@77 134 }
garbeam@77 135 }
garbeam@73 136
garbeam@73 137 static void
garbeam@18 138 buttonpress(XEvent *e)
garbeam@18 139 {
garbeam@73 140 int x;
garbeam@73 141 Arg a;
garbeam@18 142 XButtonPressedEvent *ev = &e->xbutton;
garbeam@18 143 Client *c;
garbeam@18 144
garbeam@73 145 if(barwin == ev->window) {
garbeam@80 146 switch(ev->button) {
garbeam@80 147 default:
garbeam@80 148 x = 0;
garbeam@80 149 for(a.i = 0; a.i < TLast; a.i++) {
garbeam@80 150 x += textw(tags[a.i]);
garbeam@80 151 if(ev->x < x) {
garbeam@80 152 view(&a);
garbeam@80 153 break;
garbeam@80 154 }
garbeam@73 155 }
garbeam@80 156 break;
garbeam@80 157 case Button4:
garbeam@80 158 a.i = (tsel + 1 < TLast) ? tsel + 1 : 0;
garbeam@80 159 view(&a);
garbeam@80 160 break;
garbeam@80 161 case Button5:
garbeam@80 162 a.i = (tsel - 1 >= 0) ? tsel - 1 : TLast - 1;
garbeam@80 163 view(&a);
garbeam@80 164 break;
garbeam@73 165 }
garbeam@73 166 }
garbeam@58 167 else if((c = getclient(ev->window))) {
garbeam@18 168 switch(ev->button) {
garbeam@18 169 default:
garbeam@18 170 break;
garbeam@18 171 case Button1:
arg@99 172 if(arrange == dotile && !c->isfloat) {
arg@99 173 if((ev->state & ControlMask) && (ev->button == Button1))
arg@99 174 zoom(NULL);
arg@99 175 }
arg@99 176 else {
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@99 185 if(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 {
garbeam@5 197 XConfigureRequestEvent *ev = &e->xconfigurerequest;
garbeam@5 198 XWindowChanges wc;
garbeam@5 199 Client *c;
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)
garbeam@95 205 *c->x = ev->x;
garbeam@5 206 if(ev->value_mask & CWY)
garbeam@95 207 *c->y = ev->y;
garbeam@5 208 if(ev->value_mask & CWWidth)
garbeam@95 209 *c->w = ev->width;
garbeam@5 210 if(ev->value_mask & CWHeight)
garbeam@95 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 {
garbeam@5 244 XCrossingEvent *ev = &e->xcrossing;
garbeam@5 245 Client *c;
garbeam@5 246
garbeam@5 247 if(ev->mode != NotifyNormal || 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 {
garbeam@5 259 XExposeEvent *ev = &e->xexpose;
garbeam@21 260 Client *c;
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 {
garbeam@76 273 XKeyEvent *ev = &e->xkey;
garbeam@76 274 static unsigned int len = key ? sizeof(key) / sizeof(key[0]) : 0;
garbeam@76 275 unsigned int i;
garbeam@76 276 KeySym keysym;
garbeam@76 277
garbeam@76 278 keysym = XKeycodeToKeysym(dpy, (KeyCode)ev->keycode, 0);
garbeam@76 279 for(i = 0; i < len; i++)
garbeam@76 280 if((keysym == key[i].keysym) && (key[i].mod == ev->state)) {
garbeam@76 281 if(key[i].func)
garbeam@76 282 key[i].func(&key[i].arg);
garbeam@76 283 return;
garbeam@76 284 }
garbeam@76 285 }
garbeam@76 286
garbeam@76 287 static void
garbeam@76 288 leavenotify(XEvent *e)
garbeam@76 289 {
garbeam@76 290 XCrossingEvent *ev = &e->xcrossing;
garbeam@76 291
garbeam@76 292 if((ev->window == root) && !ev->same_screen)
garbeam@76 293 issel = True;
garbeam@76 294 }
garbeam@76 295
garbeam@76 296 static void
garbeam@5 297 maprequest(XEvent *e)
garbeam@5 298 {
garbeam@5 299 XMapRequestEvent *ev = &e->xmaprequest;
garbeam@5 300 static XWindowAttributes wa;
garbeam@5 301
garbeam@5 302 if(!XGetWindowAttributes(dpy, ev->window, &wa))
garbeam@5 303 return;
garbeam@5 304
garbeam@5 305 if(wa.override_redirect) {
garbeam@5 306 XSelectInput(dpy, ev->window,
garbeam@5 307 (StructureNotifyMask | PropertyChangeMask));
garbeam@5 308 return;
garbeam@5 309 }
garbeam@5 310
garbeam@10 311 if(!getclient(ev->window))
garbeam@10 312 manage(ev->window, &wa);
garbeam@5 313 }
garbeam@5 314
garbeam@5 315 static void
garbeam@5 316 propertynotify(XEvent *e)
garbeam@5 317 {
garbeam@5 318 XPropertyEvent *ev = &e->xproperty;
garbeam@53 319 Window trans;
garbeam@5 320 Client *c;
garbeam@5 321
garbeam@5 322 if(ev->state == PropertyDelete)
garbeam@5 323 return; /* ignore */
garbeam@5 324
garbeam@13 325 if((c = getclient(ev->window))) {
garbeam@77 326 if(ev->atom == wmatom[WMProtocols]) {
garbeam@75 327 c->proto = getproto(c->win);
garbeam@30 328 return;
garbeam@30 329 }
garbeam@13 330 switch (ev->atom) {
garbeam@13 331 default: break;
garbeam@13 332 case XA_WM_TRANSIENT_FOR:
garbeam@53 333 XGetTransientForHint(dpy, c->win, &trans);
garbeam@80 334 if(!c->isfloat && (c->isfloat = (trans != 0)))
garbeam@53 335 arrange(NULL);
garbeam@13 336 break;
garbeam@13 337 case XA_WM_NORMAL_HINTS:
garbeam@74 338 setsize(c);
garbeam@13 339 break;
garbeam@13 340 }
garbeam@77 341 if(ev->atom == XA_WM_NAME || ev->atom == netatom[NetWMName]) {
garbeam@74 342 settitle(c);
garbeam@74 343 drawtitle(c);
garbeam@13 344 }
garbeam@13 345 }
garbeam@5 346 }
garbeam@5 347
garbeam@5 348 static void
garbeam@5 349 unmapnotify(XEvent *e)
garbeam@5 350 {
garbeam@5 351 Client *c;
garbeam@5 352 XUnmapEvent *ev = &e->xunmap;
garbeam@5 353
garbeam@10 354 if((c = getclient(ev->window)))
garbeam@10 355 unmanage(c);
garbeam@5 356 }
garbeam@76 357
garbeam@84 358 /* extern */
garbeam@76 359
garbeam@76 360 void (*handler[LASTEvent]) (XEvent *) = {
garbeam@76 361 [ButtonPress] = buttonpress,
garbeam@76 362 [ConfigureRequest] = configurerequest,
garbeam@76 363 [DestroyNotify] = destroynotify,
garbeam@76 364 [EnterNotify] = enternotify,
garbeam@76 365 [LeaveNotify] = leavenotify,
garbeam@76 366 [Expose] = expose,
garbeam@76 367 [KeyPress] = keypress,
garbeam@76 368 [MapRequest] = maprequest,
garbeam@76 369 [PropertyNotify] = propertynotify,
garbeam@76 370 [UnmapNotify] = unmapnotify
garbeam@76 371 };
garbeam@76 372
garbeam@76 373 void
garbeam@76 374 grabkeys()
garbeam@76 375 {
garbeam@76 376 static unsigned int len = key ? sizeof(key) / sizeof(key[0]) : 0;
garbeam@76 377 unsigned int i;
garbeam@76 378 KeyCode code;
garbeam@76 379
garbeam@76 380 for(i = 0; i < len; i++) {
garbeam@76 381 code = XKeysymToKeycode(dpy, key[i].keysym);
garbeam@76 382 XUngrabKey(dpy, code, key[i].mod, root);
garbeam@76 383 XGrabKey(dpy, code, key[i].mod, root, True,
garbeam@76 384 GrabModeAsync, GrabModeAsync);
garbeam@76 385 }
garbeam@76 386 }