dwm-meillo

annotate event.c @ 139:22213b9a2114

prepared 0.5
author arg@10ksloc.org
date Fri, 21 Jul 2006 21:15:08 +0200
parents c1185dc7a36e
children 36cabfe408cd
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@139 23 /*
arg@111 24 const char *browse[] = { "firefox", NULL };
arg@111 25 const char *gimp[] = { "gimp", NULL };
arg@139 26 */
arg@139 27 const char *term[] = { "xterm", NULL };
arg@139 28 /*
arg@137 29 "urxvt", "-tr", "+sb", "-bg", "black", "-fg", "white", "-cr", "white",
arg@111 30 "-fn", "-*-terminus-medium-*-*-*-13-*-*-*-*-*-iso10646-*", NULL
arg@111 31 };
arg@123 32 const char *xlock[] = { "xlock", NULL };
arg@139 33 */
garbeam@75 34
arg@114 35 static Key key[] = {
arg@123 36 /* modifier key function arguments */
arg@130 37 { MODKEY, XK_0, view, { .i = Tfnord } },
arg@123 38 { MODKEY, XK_1, view, { .i = Tdev } },
arg@130 39 { MODKEY, XK_2, view, { .i = Tnet } },
arg@123 40 { MODKEY, XK_3, view, { .i = Twork } },
arg@130 41 { MODKEY, XK_4, view, { .i = Tmisc} },
arg@123 42 { MODKEY, XK_j, focusnext, { 0 } },
arg@123 43 { MODKEY, XK_k, focusprev, { 0 } },
arg@124 44 { MODKEY, XK_m, togglemax, { 0 } },
arg@124 45 { MODKEY, XK_space, togglemode, { 0 } },
arg@123 46 { MODKEY, XK_Return, zoom, { 0 } },
arg@130 47 { MODKEY|ControlMask, XK_0, appendtag, { .i = Tfnord } },
arg@130 48 { MODKEY|ControlMask, XK_1, appendtag, { .i = Tdev } },
arg@130 49 { MODKEY|ControlMask, XK_2, appendtag, { .i = Tnet } },
arg@130 50 { MODKEY|ControlMask, XK_3, appendtag, { .i = Twork } },
arg@130 51 { MODKEY|ControlMask, XK_4, appendtag, { .i = Tmisc } },
arg@130 52 { MODKEY|ShiftMask, XK_0, replacetag, { .i = Tfnord } },
arg@123 53 { MODKEY|ShiftMask, XK_1, replacetag, { .i = Tdev } },
arg@130 54 { MODKEY|ShiftMask, XK_2, replacetag, { .i = Tnet } },
arg@123 55 { MODKEY|ShiftMask, XK_3, replacetag, { .i = Twork } },
arg@130 56 { MODKEY|ShiftMask, XK_4, replacetag, { .i = Tmisc } },
arg@123 57 { MODKEY|ShiftMask, XK_c, killclient, { 0 } },
arg@139 58 { MODKEY|ShiftMask, XK_q, quit, { 0 } },
arg@139 59 { MODKEY|ShiftMask, XK_Return, spawn, { .argv = term } },
arg@139 60 /*
arg@123 61 { MODKEY|ShiftMask, XK_g, spawn, { .argv = gimp } },
arg@123 62 { MODKEY|ShiftMask, XK_l, spawn, { .argv = xlock } },
arg@123 63 { MODKEY|ShiftMask, XK_w, spawn, { .argv = browse } },
arg@139 64 */
garbeam@75 65 };
garbeam@75 66
arg@125 67 /* END CUSTOMIZE */
arg@125 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 int x1, y1, ocx, ocy, di;
garbeam@77 75 unsigned int dui;
garbeam@77 76 Window dummy;
arg@123 77 XEvent ev;
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,
arg@123 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 int ocx, ocy;
arg@99 109 Corner sticky;
arg@123 110 XEvent ev;
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;
arg@123 149 Client *c;
garbeam@18 150 XButtonPressedEvent *ev = &e->xbutton;
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@133 179 if(!c->ismax && (arrange == dofloat || c->isfloat)) {
arg@99 180 higher(c);
arg@99 181 movemouse(c);
arg@99 182 }
garbeam@18 183 break;
garbeam@18 184 case Button2:
garbeam@26 185 lower(c);
garbeam@18 186 break;
garbeam@18 187 case Button3:
arg@133 188 if(!c->ismax && (arrange == dofloat || c->isfloat)) {
arg@99 189 higher(c);
arg@99 190 resizemouse(c);
arg@99 191 }
garbeam@18 192 break;
garbeam@18 193 }
garbeam@18 194 }
garbeam@18 195 }
garbeam@18 196
garbeam@18 197 static void
garbeam@5 198 configurerequest(XEvent *e)
garbeam@5 199 {
arg@123 200 Client *c;
garbeam@5 201 XConfigureRequestEvent *ev = &e->xconfigurerequest;
garbeam@5 202 XWindowChanges wc;
garbeam@5 203
garbeam@5 204 ev->value_mask &= ~CWSibling;
garbeam@18 205 if((c = getclient(ev->window))) {
garbeam@29 206 gravitate(c, True);
garbeam@5 207 if(ev->value_mask & CWX)
arg@115 208 c->x = ev->x;
garbeam@5 209 if(ev->value_mask & CWY)
arg@115 210 c->y = ev->y;
garbeam@5 211 if(ev->value_mask & CWWidth)
arg@115 212 c->w = ev->width;
garbeam@5 213 if(ev->value_mask & CWHeight)
arg@115 214 c->h = ev->height;
garbeam@29 215 if(ev->value_mask & CWBorderWidth)
garbeam@55 216 c->border = 1;
garbeam@29 217 gravitate(c, False);
arg@99 218 resize(c, True, TopLeft);
garbeam@5 219 }
garbeam@5 220
garbeam@5 221 wc.x = ev->x;
garbeam@5 222 wc.y = ev->y;
garbeam@5 223 wc.width = ev->width;
garbeam@5 224 wc.height = ev->height;
garbeam@25 225 wc.border_width = 1;
garbeam@5 226 wc.sibling = None;
garbeam@5 227 wc.stack_mode = Above;
garbeam@5 228 ev->value_mask &= ~CWStackMode;
garbeam@5 229 ev->value_mask |= CWBorderWidth;
garbeam@5 230 XConfigureWindow(dpy, ev->window, ev->value_mask, &wc);
garbeam@79 231 XSync(dpy, False);
garbeam@5 232 }
garbeam@5 233
garbeam@5 234 static void
garbeam@5 235 destroynotify(XEvent *e)
garbeam@5 236 {
garbeam@5 237 Client *c;
garbeam@5 238 XDestroyWindowEvent *ev = &e->xdestroywindow;
garbeam@5 239
garbeam@11 240 if((c = getclient(ev->window)))
garbeam@11 241 unmanage(c);
garbeam@5 242 }
garbeam@5 243
garbeam@5 244 static void
garbeam@5 245 enternotify(XEvent *e)
garbeam@5 246 {
arg@123 247 Client *c;
garbeam@5 248 XCrossingEvent *ev = &e->xcrossing;
garbeam@5 249
garbeam@5 250 if(ev->mode != NotifyNormal || ev->detail == NotifyInferior)
garbeam@5 251 return;
garbeam@5 252
garbeam@13 253 if((c = getclient(ev->window)))
garbeam@13 254 focus(c);
garbeam@26 255 else if(ev->window == root)
garbeam@31 256 issel = True;
garbeam@5 257 }
garbeam@5 258
garbeam@5 259 static void
garbeam@5 260 expose(XEvent *e)
garbeam@5 261 {
arg@123 262 Client *c;
garbeam@5 263 XExposeEvent *ev = &e->xexpose;
garbeam@5 264
garbeam@5 265 if(ev->count == 0) {
garbeam@70 266 if(barwin == ev->window)
garbeam@74 267 drawstatus();
garbeam@75 268 else if((c = getctitle(ev->window)))
garbeam@74 269 drawtitle(c);
garbeam@5 270 }
garbeam@5 271 }
garbeam@5 272
garbeam@5 273 static void
garbeam@76 274 keypress(XEvent *e)
garbeam@76 275 {
arg@138 276 static unsigned int len = sizeof(key) / sizeof(key[0]);
garbeam@76 277 unsigned int i;
garbeam@76 278 KeySym keysym;
arg@123 279 XKeyEvent *ev = &e->xkey;
garbeam@76 280
garbeam@76 281 keysym = XKeycodeToKeysym(dpy, (KeyCode)ev->keycode, 0);
garbeam@76 282 for(i = 0; i < len; i++)
garbeam@76 283 if((keysym == key[i].keysym) && (key[i].mod == ev->state)) {
garbeam@76 284 if(key[i].func)
garbeam@76 285 key[i].func(&key[i].arg);
garbeam@76 286 return;
garbeam@76 287 }
garbeam@76 288 }
garbeam@76 289
garbeam@76 290 static void
garbeam@76 291 leavenotify(XEvent *e)
garbeam@76 292 {
garbeam@76 293 XCrossingEvent *ev = &e->xcrossing;
garbeam@76 294
garbeam@76 295 if((ev->window == root) && !ev->same_screen)
garbeam@76 296 issel = True;
garbeam@76 297 }
garbeam@76 298
garbeam@76 299 static void
garbeam@5 300 maprequest(XEvent *e)
garbeam@5 301 {
arg@123 302 static XWindowAttributes wa;
garbeam@5 303 XMapRequestEvent *ev = &e->xmaprequest;
garbeam@5 304
garbeam@5 305 if(!XGetWindowAttributes(dpy, ev->window, &wa))
garbeam@5 306 return;
garbeam@5 307
garbeam@5 308 if(wa.override_redirect) {
garbeam@5 309 XSelectInput(dpy, ev->window,
garbeam@5 310 (StructureNotifyMask | PropertyChangeMask));
garbeam@5 311 return;
garbeam@5 312 }
garbeam@5 313
garbeam@10 314 if(!getclient(ev->window))
garbeam@10 315 manage(ev->window, &wa);
garbeam@5 316 }
garbeam@5 317
garbeam@5 318 static void
garbeam@5 319 propertynotify(XEvent *e)
garbeam@5 320 {
arg@123 321 Client *c;
arg@123 322 Window trans;
garbeam@5 323 XPropertyEvent *ev = &e->xproperty;
garbeam@5 324
garbeam@5 325 if(ev->state == PropertyDelete)
garbeam@5 326 return; /* ignore */
garbeam@5 327
garbeam@13 328 if((c = getclient(ev->window))) {
garbeam@77 329 if(ev->atom == wmatom[WMProtocols]) {
garbeam@75 330 c->proto = getproto(c->win);
garbeam@30 331 return;
garbeam@30 332 }
garbeam@13 333 switch (ev->atom) {
garbeam@13 334 default: break;
garbeam@13 335 case XA_WM_TRANSIENT_FOR:
garbeam@53 336 XGetTransientForHint(dpy, c->win, &trans);
garbeam@80 337 if(!c->isfloat && (c->isfloat = (trans != 0)))
garbeam@53 338 arrange(NULL);
garbeam@13 339 break;
garbeam@13 340 case XA_WM_NORMAL_HINTS:
garbeam@74 341 setsize(c);
garbeam@13 342 break;
garbeam@13 343 }
garbeam@77 344 if(ev->atom == XA_WM_NAME || ev->atom == netatom[NetWMName]) {
garbeam@74 345 settitle(c);
garbeam@74 346 drawtitle(c);
garbeam@13 347 }
garbeam@13 348 }
garbeam@5 349 }
garbeam@5 350
garbeam@5 351 static void
garbeam@5 352 unmapnotify(XEvent *e)
garbeam@5 353 {
garbeam@5 354 Client *c;
garbeam@5 355 XUnmapEvent *ev = &e->xunmap;
garbeam@5 356
garbeam@10 357 if((c = getclient(ev->window)))
garbeam@10 358 unmanage(c);
garbeam@5 359 }
garbeam@76 360
garbeam@84 361 /* extern */
garbeam@76 362
garbeam@76 363 void (*handler[LASTEvent]) (XEvent *) = {
garbeam@76 364 [ButtonPress] = buttonpress,
garbeam@76 365 [ConfigureRequest] = configurerequest,
garbeam@76 366 [DestroyNotify] = destroynotify,
garbeam@76 367 [EnterNotify] = enternotify,
garbeam@76 368 [LeaveNotify] = leavenotify,
garbeam@76 369 [Expose] = expose,
garbeam@76 370 [KeyPress] = keypress,
garbeam@76 371 [MapRequest] = maprequest,
garbeam@76 372 [PropertyNotify] = propertynotify,
garbeam@76 373 [UnmapNotify] = unmapnotify
garbeam@76 374 };
garbeam@76 375
garbeam@76 376 void
garbeam@76 377 grabkeys()
garbeam@76 378 {
arg@138 379 static unsigned int len = sizeof(key) / sizeof(key[0]);
garbeam@76 380 unsigned int i;
garbeam@76 381 KeyCode code;
garbeam@76 382
garbeam@76 383 for(i = 0; i < len; i++) {
garbeam@76 384 code = XKeysymToKeycode(dpy, key[i].keysym);
garbeam@76 385 XUngrabKey(dpy, code, key[i].mod, root);
garbeam@76 386 XGrabKey(dpy, code, key[i].mod, root, True,
garbeam@76 387 GrabModeAsync, GrabModeAsync);
garbeam@76 388 }
garbeam@76 389 }