dwm-meillo

annotate event.c @ 131:65efe27cbf3d

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