dwm-meillo

annotate event.c @ 80:8125f908c80c

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