dwm-meillo

annotate event.c @ 489:d34374cbe926

simplified mouse resizals
author arg@mmvi
date Mon, 25 Sep 2006 21:19:18 +0200
parents 0d2559f46b9e
children 303d3384720e
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 #include <stdlib.h>
garbeam@5 7 #include <X11/keysym.h>
garbeam@13 8 #include <X11/Xatom.h>
garbeam@5 9
arg@146 10 /* static */
arg@114 11
arg@114 12 typedef struct {
arg@114 13 unsigned long mod;
arg@114 14 KeySym keysym;
arg@114 15 void (*func)(Arg *arg);
arg@114 16 Arg arg;
arg@114 17 } Key;
arg@114 18
arg@146 19 KEYS
garbeam@75 20
arg@291 21 #define CLEANMASK(mask) (mask & ~(numlockmask | LockMask))
garbeam@5 22
garbeam@77 23 static void
arg@461 24 movemouse(Client *c) {
garbeam@77 25 int x1, y1, ocx, ocy, di;
garbeam@77 26 unsigned int dui;
garbeam@77 27 Window dummy;
arg@123 28 XEvent ev;
garbeam@77 29
arg@115 30 ocx = c->x;
arg@115 31 ocy = c->y;
arg@148 32 if(XGrabPointer(dpy, root, False, MOUSEMASK, GrabModeAsync, GrabModeAsync,
arg@123 33 None, cursor[CurMove], CurrentTime) != GrabSuccess)
garbeam@77 34 return;
arg@482 35 c->ismax = False;
garbeam@77 36 XQueryPointer(dpy, root, &dummy, &dummy, &x1, &y1, &di, &di, &dui);
garbeam@77 37 for(;;) {
arg@489 38 XMaskEvent(dpy, MOUSEMASK | ExposureMask, &ev);
garbeam@77 39 switch (ev.type) {
garbeam@77 40 case Expose:
garbeam@77 41 handler[Expose](&ev);
garbeam@77 42 break;
garbeam@77 43 case MotionNotify:
garbeam@79 44 XSync(dpy, False);
arg@115 45 c->x = ocx + (ev.xmotion.x - x1);
arg@115 46 c->y = ocy + (ev.xmotion.y - y1);
arg@99 47 resize(c, False, TopLeft);
garbeam@77 48 break;
garbeam@77 49 case ButtonRelease:
garbeam@77 50 XUngrabPointer(dpy, CurrentTime);
garbeam@77 51 return;
garbeam@77 52 }
garbeam@77 53 }
garbeam@77 54 }
garbeam@77 55
garbeam@77 56 static void
arg@461 57 resizemouse(Client *c) {
garbeam@77 58 int ocx, ocy;
arg@268 59 int nw, nh;
arg@99 60 Corner sticky;
arg@123 61 XEvent ev;
garbeam@77 62
arg@115 63 ocx = c->x;
arg@115 64 ocy = c->y;
arg@148 65 if(XGrabPointer(dpy, root, False, MOUSEMASK, GrabModeAsync, GrabModeAsync,
garbeam@77 66 None, cursor[CurResize], CurrentTime) != GrabSuccess)
garbeam@77 67 return;
arg@482 68 c->ismax = False;
arg@115 69 XWarpPointer(dpy, None, c->win, 0, 0, 0, 0, c->w, c->h);
garbeam@77 70 for(;;) {
arg@489 71 XMaskEvent(dpy, MOUSEMASK | ExposureMask, &ev);
garbeam@77 72 switch(ev.type) {
garbeam@77 73 case Expose:
garbeam@77 74 handler[Expose](&ev);
garbeam@77 75 break;
garbeam@77 76 case MotionNotify:
garbeam@79 77 XSync(dpy, False);
arg@268 78 if((nw = abs(ocx - ev.xmotion.x)))
arg@478 79 c->w = nw;
arg@268 80 if((nh = abs(ocy - ev.xmotion.y)))
arg@478 81 c->h = nh;
arg@115 82 c->x = (ocx <= ev.xmotion.x) ? ocx : ocx - c->w;
arg@115 83 c->y = (ocy <= ev.xmotion.y) ? ocy : ocy - c->h;
arg@105 84 if(ocx <= ev.xmotion.x)
arg@105 85 sticky = (ocy <= ev.xmotion.y) ? TopLeft : BotLeft;
arg@105 86 else
arg@105 87 sticky = (ocy <= ev.xmotion.y) ? TopRight : BotRight;
arg@99 88 resize(c, True, sticky);
garbeam@77 89 break;
garbeam@77 90 case ButtonRelease:
garbeam@77 91 XUngrabPointer(dpy, CurrentTime);
garbeam@77 92 return;
garbeam@77 93 }
garbeam@77 94 }
garbeam@77 95 }
garbeam@73 96
garbeam@73 97 static void
arg@461 98 buttonpress(XEvent *e) {
garbeam@73 99 int x;
garbeam@73 100 Arg a;
arg@123 101 Client *c;
garbeam@18 102 XButtonPressedEvent *ev = &e->xbutton;
garbeam@18 103
garbeam@73 104 if(barwin == ev->window) {
arg@362 105 x = 0;
arg@362 106 for(a.i = 0; a.i < ntags; a.i++) {
arg@362 107 x += textw(tags[a.i]);
arg@362 108 if(ev->x < x) {
arg@399 109 if(ev->button == Button1) {
arg@398 110 if(ev->state & MODKEY)
arg@398 111 tag(&a);
arg@398 112 else
arg@398 113 view(&a);
arg@399 114 }
arg@399 115 else if(ev->button == Button3) {
arg@398 116 if(ev->state & MODKEY)
arg@398 117 toggletag(&a);
arg@398 118 else
arg@398 119 toggleview(&a);
arg@394 120 }
arg@362 121 return;
garbeam@73 122 }
garbeam@73 123 }
arg@371 124 if(ev->x < x + bmw) {
arg@371 125 if(ev->button == Button1)
arg@371 126 togglemode(NULL);
arg@371 127 }
garbeam@73 128 }
garbeam@58 129 else if((c = getclient(ev->window))) {
arg@143 130 focus(c);
arg@473 131 if(CLEANMASK(ev->state) != MODKEY)
arg@318 132 return;
arg@400 133 if(ev->button == Button1 && (arrange == dofloat || c->isfloat)) {
arg@487 134 restack();
arg@399 135 movemouse(c);
arg@399 136 }
arg@399 137 else if(ev->button == Button2)
arg@248 138 zoom(NULL);
arg@400 139 else if(ev->button == Button3 && (arrange == dofloat || c->isfloat)) {
arg@487 140 restack();
arg@399 141 resizemouse(c);
garbeam@18 142 }
garbeam@18 143 }
garbeam@18 144 }
garbeam@18 145
garbeam@18 146 static void
arg@461 147 configurerequest(XEvent *e) {
arg@286 148 unsigned long newmask;
arg@123 149 Client *c;
garbeam@5 150 XConfigureRequestEvent *ev = &e->xconfigurerequest;
arg@489 151 XEvent synev;
garbeam@5 152 XWindowChanges wc;
garbeam@5 153
garbeam@18 154 if((c = getclient(ev->window))) {
arg@488 155 c->ismax = False;
garbeam@29 156 gravitate(c, True);
arg@195 157 if(ev->value_mask & CWX)
arg@195 158 c->x = ev->x;
arg@195 159 if(ev->value_mask & CWY)
arg@195 160 c->y = ev->y;
arg@195 161 if(ev->value_mask & CWWidth)
arg@195 162 c->w = ev->width;
arg@195 163 if(ev->value_mask & CWHeight)
arg@195 164 c->h = ev->height;
garbeam@29 165 if(ev->value_mask & CWBorderWidth)
arg@164 166 c->border = ev->border_width;
garbeam@29 167 gravitate(c, False);
arg@164 168 wc.x = c->x;
arg@164 169 wc.y = c->y;
arg@164 170 wc.width = c->w;
arg@164 171 wc.height = c->h;
arg@164 172 newmask = ev->value_mask & (~(CWSibling | CWStackMode | CWBorderWidth));
arg@164 173 if(newmask)
arg@164 174 XConfigureWindow(dpy, c->win, newmask, &wc);
arg@489 175 else {
arg@489 176 synev.type = ConfigureNotify;
arg@489 177 synev.xconfigure.display = dpy;
arg@489 178 synev.xconfigure.event = c->win;
arg@489 179 synev.xconfigure.window = c->win;
arg@489 180 synev.xconfigure.x = c->x;
arg@489 181 synev.xconfigure.y = c->y;
arg@489 182 synev.xconfigure.width = c->w;
arg@489 183 synev.xconfigure.height = c->h;
arg@489 184 synev.xconfigure.border_width = c->border;
arg@489 185 synev.xconfigure.above = None;
arg@489 186 XSendEvent(dpy, c->win, True, NoEventMask, &synev);
arg@489 187 }
arg@195 188 XSync(dpy, False);
arg@385 189 if(c->isfloat)
arg@385 190 resize(c, False, TopLeft);
arg@196 191 else
arg@196 192 arrange(NULL);
garbeam@5 193 }
arg@164 194 else {
arg@164 195 wc.x = ev->x;
arg@164 196 wc.y = ev->y;
arg@164 197 wc.width = ev->width;
arg@164 198 wc.height = ev->height;
arg@164 199 wc.border_width = ev->border_width;
arg@164 200 wc.sibling = ev->above;
arg@164 201 wc.stack_mode = ev->detail;
arg@164 202 XConfigureWindow(dpy, ev->window, ev->value_mask, &wc);
arg@195 203 XSync(dpy, False);
arg@164 204 }
garbeam@5 205 }
garbeam@5 206
garbeam@5 207 static void
arg@461 208 destroynotify(XEvent *e) {
garbeam@5 209 Client *c;
garbeam@5 210 XDestroyWindowEvent *ev = &e->xdestroywindow;
garbeam@5 211
garbeam@11 212 if((c = getclient(ev->window)))
garbeam@11 213 unmanage(c);
garbeam@5 214 }
garbeam@5 215
garbeam@5 216 static void
arg@461 217 enternotify(XEvent *e) {
arg@123 218 Client *c;
garbeam@5 219 XCrossingEvent *ev = &e->xcrossing;
garbeam@5 220
arg@232 221 if(ev->mode != NotifyNormal || ev->detail == NotifyInferior)
garbeam@5 222 return;
garbeam@5 223
arg@466 224 if(((c = getclient(ev->window)) || (c = getctitle(ev->window))) && isvisible(c))
garbeam@13 225 focus(c);
arg@239 226 else if(ev->window == root) {
garbeam@31 227 issel = True;
arg@239 228 XSetInputFocus(dpy, root, RevertToPointerRoot, CurrentTime);
arg@239 229 drawall();
arg@239 230 }
garbeam@5 231 }
garbeam@5 232
garbeam@5 233 static void
arg@461 234 expose(XEvent *e) {
arg@123 235 Client *c;
garbeam@5 236 XExposeEvent *ev = &e->xexpose;
garbeam@5 237
garbeam@5 238 if(ev->count == 0) {
garbeam@70 239 if(barwin == ev->window)
garbeam@74 240 drawstatus();
garbeam@75 241 else if((c = getctitle(ev->window)))
garbeam@74 242 drawtitle(c);
garbeam@5 243 }
garbeam@5 244 }
garbeam@5 245
garbeam@5 246 static void
arg@461 247 keypress(XEvent *e) {
arg@138 248 static unsigned int len = sizeof(key) / sizeof(key[0]);
garbeam@76 249 unsigned int i;
garbeam@76 250 KeySym keysym;
arg@123 251 XKeyEvent *ev = &e->xkey;
garbeam@76 252
garbeam@76 253 keysym = XKeycodeToKeysym(dpy, (KeyCode)ev->keycode, 0);
arg@275 254 for(i = 0; i < len; i++) {
arg@461 255 if(keysym == key[i].keysym
arg@461 256 && CLEANMASK(key[i].mod) == CLEANMASK(ev->state))
arg@275 257 {
garbeam@76 258 if(key[i].func)
garbeam@76 259 key[i].func(&key[i].arg);
garbeam@76 260 return;
garbeam@76 261 }
arg@275 262 }
garbeam@76 263 }
garbeam@76 264
garbeam@76 265 static void
arg@461 266 leavenotify(XEvent *e) {
garbeam@76 267 XCrossingEvent *ev = &e->xcrossing;
garbeam@76 268
arg@239 269 if((ev->window == root) && !ev->same_screen) {
arg@239 270 issel = False;
arg@239 271 drawall();
arg@239 272 }
garbeam@76 273 }
garbeam@76 274
garbeam@76 275 static void
arg@461 276 mappingnotify(XEvent *e) {
arg@279 277 XMappingEvent *ev = &e->xmapping;
arg@279 278
arg@279 279 XRefreshKeyboardMapping(ev);
arg@279 280 if(ev->request == MappingKeyboard)
arg@279 281 grabkeys();
arg@279 282 }
arg@279 283
arg@279 284 static void
arg@461 285 maprequest(XEvent *e) {
arg@123 286 static XWindowAttributes wa;
garbeam@5 287 XMapRequestEvent *ev = &e->xmaprequest;
garbeam@5 288
garbeam@5 289 if(!XGetWindowAttributes(dpy, ev->window, &wa))
garbeam@5 290 return;
garbeam@5 291
garbeam@5 292 if(wa.override_redirect) {
garbeam@5 293 XSelectInput(dpy, ev->window,
garbeam@5 294 (StructureNotifyMask | PropertyChangeMask));
garbeam@5 295 return;
garbeam@5 296 }
garbeam@5 297
garbeam@10 298 if(!getclient(ev->window))
garbeam@10 299 manage(ev->window, &wa);
garbeam@5 300 }
garbeam@5 301
garbeam@5 302 static void
arg@461 303 propertynotify(XEvent *e) {
arg@123 304 Client *c;
arg@123 305 Window trans;
garbeam@5 306 XPropertyEvent *ev = &e->xproperty;
garbeam@5 307
garbeam@5 308 if(ev->state == PropertyDelete)
garbeam@5 309 return; /* ignore */
garbeam@5 310
garbeam@13 311 if((c = getclient(ev->window))) {
garbeam@77 312 if(ev->atom == wmatom[WMProtocols]) {
garbeam@75 313 c->proto = getproto(c->win);
garbeam@30 314 return;
garbeam@30 315 }
garbeam@13 316 switch (ev->atom) {
garbeam@13 317 default: break;
garbeam@13 318 case XA_WM_TRANSIENT_FOR:
garbeam@53 319 XGetTransientForHint(dpy, c->win, &trans);
garbeam@80 320 if(!c->isfloat && (c->isfloat = (trans != 0)))
garbeam@53 321 arrange(NULL);
garbeam@13 322 break;
garbeam@13 323 case XA_WM_NORMAL_HINTS:
arg@454 324 updatesize(c);
garbeam@13 325 break;
garbeam@13 326 }
garbeam@77 327 if(ev->atom == XA_WM_NAME || ev->atom == netatom[NetWMName]) {
arg@454 328 updatetitle(c);
garbeam@74 329 drawtitle(c);
garbeam@13 330 }
garbeam@13 331 }
garbeam@5 332 }
garbeam@5 333
garbeam@5 334 static void
arg@461 335 unmapnotify(XEvent *e) {
garbeam@5 336 Client *c;
garbeam@5 337 XUnmapEvent *ev = &e->xunmap;
garbeam@5 338
garbeam@10 339 if((c = getclient(ev->window)))
garbeam@10 340 unmanage(c);
garbeam@5 341 }
garbeam@76 342
garbeam@84 343 /* extern */
garbeam@76 344
garbeam@76 345 void (*handler[LASTEvent]) (XEvent *) = {
garbeam@76 346 [ButtonPress] = buttonpress,
garbeam@76 347 [ConfigureRequest] = configurerequest,
garbeam@76 348 [DestroyNotify] = destroynotify,
garbeam@76 349 [EnterNotify] = enternotify,
garbeam@76 350 [LeaveNotify] = leavenotify,
garbeam@76 351 [Expose] = expose,
garbeam@76 352 [KeyPress] = keypress,
arg@279 353 [MappingNotify] = mappingnotify,
garbeam@76 354 [MapRequest] = maprequest,
garbeam@76 355 [PropertyNotify] = propertynotify,
garbeam@76 356 [UnmapNotify] = unmapnotify
garbeam@76 357 };
garbeam@76 358
garbeam@76 359 void
arg@487 360 grabkeys(void) {
arg@138 361 static unsigned int len = sizeof(key) / sizeof(key[0]);
garbeam@76 362 unsigned int i;
garbeam@76 363 KeyCode code;
garbeam@76 364
arg@279 365 XUngrabKey(dpy, AnyKey, AnyModifier, root);
garbeam@76 366 for(i = 0; i < len; i++) {
garbeam@76 367 code = XKeysymToKeycode(dpy, key[i].keysym);
garbeam@76 368 XGrabKey(dpy, code, key[i].mod, root, True,
garbeam@76 369 GrabModeAsync, GrabModeAsync);
arg@160 370 XGrabKey(dpy, code, key[i].mod | LockMask, root, True,
arg@160 371 GrabModeAsync, GrabModeAsync);
arg@291 372 XGrabKey(dpy, code, key[i].mod | numlockmask, root, True,
arg@146 373 GrabModeAsync, GrabModeAsync);
arg@291 374 XGrabKey(dpy, code, key[i].mod | numlockmask | LockMask, root, True,
arg@146 375 GrabModeAsync, GrabModeAsync);
garbeam@76 376 }
garbeam@76 377 }
arg@292 378
arg@292 379 void
arg@487 380 procevent(void) {
arg@292 381 XEvent ev;
arg@292 382
arg@292 383 while(XPending(dpy)) {
arg@292 384 XNextEvent(dpy, &ev);
arg@292 385 if(handler[ev.type])
arg@292 386 (handler[ev.type])(&ev); /* call handler */
arg@292 387 }
arg@292 388 }