dwm-meillo

annotate event.c @ 121:eb3165734f00

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