dwm-meillo

annotate event.c @ 124:75576e44c1d8

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