aewl

annotate event.c @ 473:2d8af0d7920d

implemented the maximization as I described on the mailinglist, this feels better to me.
author arg@mmvi
date Fri, 22 Sep 2006 07:37:56 +0200
parents ad3fa2d18542
children 3e4596240abb
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;
garbeam@77 35 XQueryPointer(dpy, root, &dummy, &dummy, &x1, &y1, &di, &di, &dui);
garbeam@77 36 for(;;) {
arg@148 37 XMaskEvent(dpy, MOUSEMASK | ExposureMask, &ev);
garbeam@77 38 switch (ev.type) {
arg@461 39 default:
arg@461 40 break;
garbeam@77 41 case Expose:
garbeam@77 42 handler[Expose](&ev);
garbeam@77 43 break;
garbeam@77 44 case MotionNotify:
garbeam@79 45 XSync(dpy, False);
arg@115 46 c->x = ocx + (ev.xmotion.x - x1);
arg@115 47 c->y = ocy + (ev.xmotion.y - y1);
arg@99 48 resize(c, False, TopLeft);
garbeam@77 49 break;
garbeam@77 50 case ButtonRelease:
garbeam@77 51 XUngrabPointer(dpy, CurrentTime);
garbeam@77 52 return;
garbeam@77 53 }
garbeam@77 54 }
garbeam@77 55 }
garbeam@77 56
garbeam@77 57 static void
arg@461 58 resizemouse(Client *c) {
garbeam@77 59 int ocx, ocy;
arg@268 60 int nw, nh;
arg@99 61 Corner sticky;
arg@123 62 XEvent ev;
garbeam@77 63
arg@115 64 ocx = c->x;
arg@115 65 ocy = c->y;
arg@148 66 if(XGrabPointer(dpy, root, False, MOUSEMASK, GrabModeAsync, GrabModeAsync,
garbeam@77 67 None, cursor[CurResize], CurrentTime) != GrabSuccess)
garbeam@77 68 return;
arg@115 69 XWarpPointer(dpy, None, c->win, 0, 0, 0, 0, c->w, c->h);
garbeam@77 70 for(;;) {
arg@148 71 XMaskEvent(dpy, MOUSEMASK | ExposureMask, &ev);
garbeam@77 72 switch(ev.type) {
arg@461 73 default:
arg@461 74 break;
garbeam@77 75 case Expose:
garbeam@77 76 handler[Expose](&ev);
garbeam@77 77 break;
garbeam@77 78 case MotionNotify:
garbeam@79 79 XSync(dpy, False);
arg@268 80 if((nw = abs(ocx - ev.xmotion.x)))
arg@268 81 c->w = abs(ocx - ev.xmotion.x);
arg@268 82 if((nh = abs(ocy - ev.xmotion.y)))
arg@268 83 c->h = abs(ocy - ev.xmotion.y);
arg@115 84 c->x = (ocx <= ev.xmotion.x) ? ocx : ocx - c->w;
arg@115 85 c->y = (ocy <= ev.xmotion.y) ? ocy : ocy - c->h;
arg@105 86 if(ocx <= ev.xmotion.x)
arg@105 87 sticky = (ocy <= ev.xmotion.y) ? TopLeft : BotLeft;
arg@105 88 else
arg@105 89 sticky = (ocy <= ev.xmotion.y) ? TopRight : BotRight;
arg@99 90 resize(c, True, sticky);
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@73 98
garbeam@73 99 static void
arg@461 100 buttonpress(XEvent *e) {
garbeam@73 101 int x;
garbeam@73 102 Arg a;
arg@123 103 Client *c;
garbeam@18 104 XButtonPressedEvent *ev = &e->xbutton;
garbeam@18 105
garbeam@73 106 if(barwin == ev->window) {
arg@362 107 x = 0;
arg@362 108 for(a.i = 0; a.i < ntags; a.i++) {
arg@362 109 x += textw(tags[a.i]);
arg@362 110 if(ev->x < x) {
arg@399 111 if(ev->button == Button1) {
arg@398 112 if(ev->state & MODKEY)
arg@398 113 tag(&a);
arg@398 114 else
arg@398 115 view(&a);
arg@399 116 }
arg@399 117 else if(ev->button == Button3) {
arg@398 118 if(ev->state & MODKEY)
arg@398 119 toggletag(&a);
arg@398 120 else
arg@398 121 toggleview(&a);
arg@394 122 }
arg@362 123 return;
garbeam@73 124 }
garbeam@73 125 }
arg@371 126 if(ev->x < x + bmw) {
arg@371 127 if(ev->button == Button1)
arg@371 128 togglemode(NULL);
arg@371 129 }
garbeam@73 130 }
garbeam@58 131 else if((c = getclient(ev->window))) {
arg@143 132 focus(c);
arg@473 133 if(CLEANMASK(ev->state) != MODKEY)
arg@318 134 return;
arg@400 135 if(ev->button == Button1 && (arrange == dofloat || c->isfloat)) {
arg@399 136 restack(c);
arg@399 137 movemouse(c);
arg@399 138 }
arg@399 139 else if(ev->button == Button2)
arg@248 140 zoom(NULL);
arg@400 141 else if(ev->button == Button3 && (arrange == dofloat || c->isfloat)) {
arg@399 142 restack(c);
arg@399 143 resizemouse(c);
garbeam@18 144 }
garbeam@18 145 }
garbeam@18 146 }
garbeam@18 147
garbeam@18 148 static void
arg@461 149 synconfig(Client *c, int x, int y, int w, int h, unsigned int border) {
arg@387 150 XEvent synev;
arg@387 151
arg@387 152 synev.type = ConfigureNotify;
arg@387 153 synev.xconfigure.display = dpy;
arg@387 154 synev.xconfigure.event = c->win;
arg@387 155 synev.xconfigure.window = c->win;
arg@387 156 synev.xconfigure.x = x;
arg@387 157 synev.xconfigure.y = y;
arg@387 158 synev.xconfigure.width = w;
arg@387 159 synev.xconfigure.height = h;
arg@387 160 synev.xconfigure.border_width = border;
arg@387 161 synev.xconfigure.above = None;
arg@387 162 XSendEvent(dpy, c->win, True, NoEventMask, &synev);
arg@387 163 }
arg@387 164
arg@387 165 static void
arg@461 166 configurerequest(XEvent *e) {
arg@286 167 unsigned long newmask;
arg@123 168 Client *c;
garbeam@5 169 XConfigureRequestEvent *ev = &e->xconfigurerequest;
garbeam@5 170 XWindowChanges wc;
garbeam@5 171
garbeam@18 172 if((c = getclient(ev->window))) {
arg@473 173 if((c == sel) && !c->isfloat && (arrange != dofloat)) {
arg@387 174 synconfig(c, sx, sy + bh, sw - 2, sh - 2 - bh, ev->border_width);
arg@387 175 XSync(dpy, False);
arg@386 176 return;
arg@386 177 }
garbeam@29 178 gravitate(c, True);
arg@195 179 if(ev->value_mask & CWX)
arg@195 180 c->x = ev->x;
arg@195 181 if(ev->value_mask & CWY)
arg@195 182 c->y = ev->y;
arg@195 183 if(ev->value_mask & CWWidth)
arg@195 184 c->w = ev->width;
arg@195 185 if(ev->value_mask & CWHeight)
arg@195 186 c->h = ev->height;
garbeam@29 187 if(ev->value_mask & CWBorderWidth)
arg@164 188 c->border = ev->border_width;
garbeam@29 189 gravitate(c, False);
arg@164 190 wc.x = c->x;
arg@164 191 wc.y = c->y;
arg@164 192 wc.width = c->w;
arg@164 193 wc.height = c->h;
arg@164 194 newmask = ev->value_mask & (~(CWSibling | CWStackMode | CWBorderWidth));
arg@164 195 if(newmask)
arg@164 196 XConfigureWindow(dpy, c->win, newmask, &wc);
arg@387 197 else
arg@387 198 synconfig(c, c->x, c->y, c->w, c->h, c->border);
arg@195 199 XSync(dpy, False);
arg@385 200 if(c->isfloat)
arg@385 201 resize(c, False, TopLeft);
arg@196 202 else
arg@196 203 arrange(NULL);
garbeam@5 204 }
arg@164 205 else {
arg@164 206 wc.x = ev->x;
arg@164 207 wc.y = ev->y;
arg@164 208 wc.width = ev->width;
arg@164 209 wc.height = ev->height;
arg@164 210 wc.border_width = ev->border_width;
arg@164 211 wc.sibling = ev->above;
arg@164 212 wc.stack_mode = ev->detail;
arg@164 213 XConfigureWindow(dpy, ev->window, ev->value_mask, &wc);
arg@195 214 XSync(dpy, False);
arg@164 215 }
garbeam@5 216 }
garbeam@5 217
garbeam@5 218 static void
arg@461 219 destroynotify(XEvent *e) {
garbeam@5 220 Client *c;
garbeam@5 221 XDestroyWindowEvent *ev = &e->xdestroywindow;
garbeam@5 222
garbeam@11 223 if((c = getclient(ev->window)))
garbeam@11 224 unmanage(c);
garbeam@5 225 }
garbeam@5 226
garbeam@5 227 static void
arg@461 228 enternotify(XEvent *e) {
arg@123 229 Client *c;
garbeam@5 230 XCrossingEvent *ev = &e->xcrossing;
garbeam@5 231
arg@232 232 if(ev->mode != NotifyNormal || ev->detail == NotifyInferior)
garbeam@5 233 return;
garbeam@5 234
arg@466 235 if(((c = getclient(ev->window)) || (c = getctitle(ev->window))) && isvisible(c))
garbeam@13 236 focus(c);
arg@239 237 else if(ev->window == root) {
garbeam@31 238 issel = True;
arg@239 239 XSetInputFocus(dpy, root, RevertToPointerRoot, CurrentTime);
arg@239 240 drawall();
arg@239 241 }
garbeam@5 242 }
garbeam@5 243
garbeam@5 244 static void
arg@461 245 expose(XEvent *e) {
arg@123 246 Client *c;
garbeam@5 247 XExposeEvent *ev = &e->xexpose;
garbeam@5 248
garbeam@5 249 if(ev->count == 0) {
garbeam@70 250 if(barwin == ev->window)
garbeam@74 251 drawstatus();
garbeam@75 252 else if((c = getctitle(ev->window)))
garbeam@74 253 drawtitle(c);
garbeam@5 254 }
garbeam@5 255 }
garbeam@5 256
garbeam@5 257 static void
arg@461 258 keypress(XEvent *e) {
arg@138 259 static unsigned int len = sizeof(key) / sizeof(key[0]);
garbeam@76 260 unsigned int i;
garbeam@76 261 KeySym keysym;
arg@123 262 XKeyEvent *ev = &e->xkey;
garbeam@76 263
garbeam@76 264 keysym = XKeycodeToKeysym(dpy, (KeyCode)ev->keycode, 0);
arg@275 265 for(i = 0; i < len; i++) {
arg@461 266 if(keysym == key[i].keysym
arg@461 267 && CLEANMASK(key[i].mod) == CLEANMASK(ev->state))
arg@275 268 {
garbeam@76 269 if(key[i].func)
garbeam@76 270 key[i].func(&key[i].arg);
garbeam@76 271 return;
garbeam@76 272 }
arg@275 273 }
garbeam@76 274 }
garbeam@76 275
garbeam@76 276 static void
arg@461 277 leavenotify(XEvent *e) {
garbeam@76 278 XCrossingEvent *ev = &e->xcrossing;
garbeam@76 279
arg@239 280 if((ev->window == root) && !ev->same_screen) {
arg@239 281 issel = False;
arg@239 282 drawall();
arg@239 283 }
garbeam@76 284 }
garbeam@76 285
garbeam@76 286 static void
arg@461 287 mappingnotify(XEvent *e) {
arg@279 288 XMappingEvent *ev = &e->xmapping;
arg@279 289
arg@279 290 XRefreshKeyboardMapping(ev);
arg@279 291 if(ev->request == MappingKeyboard)
arg@279 292 grabkeys();
arg@279 293 }
arg@279 294
arg@279 295 static void
arg@461 296 maprequest(XEvent *e) {
arg@123 297 static XWindowAttributes wa;
garbeam@5 298 XMapRequestEvent *ev = &e->xmaprequest;
garbeam@5 299
garbeam@5 300 if(!XGetWindowAttributes(dpy, ev->window, &wa))
garbeam@5 301 return;
garbeam@5 302
garbeam@5 303 if(wa.override_redirect) {
garbeam@5 304 XSelectInput(dpy, ev->window,
garbeam@5 305 (StructureNotifyMask | PropertyChangeMask));
garbeam@5 306 return;
garbeam@5 307 }
garbeam@5 308
garbeam@10 309 if(!getclient(ev->window))
garbeam@10 310 manage(ev->window, &wa);
garbeam@5 311 }
garbeam@5 312
garbeam@5 313 static void
arg@461 314 propertynotify(XEvent *e) {
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:
arg@454 335 updatesize(c);
garbeam@13 336 break;
garbeam@13 337 }
garbeam@77 338 if(ev->atom == XA_WM_NAME || ev->atom == netatom[NetWMName]) {
arg@454 339 updatetitle(c);
garbeam@74 340 drawtitle(c);
garbeam@13 341 }
garbeam@13 342 }
garbeam@5 343 }
garbeam@5 344
garbeam@5 345 static void
arg@461 346 unmapnotify(XEvent *e) {
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,
arg@279 364 [MappingNotify] = mappingnotify,
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
arg@461 371 grabkeys() {
arg@138 372 static unsigned int len = sizeof(key) / sizeof(key[0]);
garbeam@76 373 unsigned int i;
garbeam@76 374 KeyCode code;
garbeam@76 375
arg@279 376 XUngrabKey(dpy, AnyKey, AnyModifier, root);
garbeam@76 377 for(i = 0; i < len; i++) {
garbeam@76 378 code = XKeysymToKeycode(dpy, key[i].keysym);
garbeam@76 379 XGrabKey(dpy, code, key[i].mod, root, True,
garbeam@76 380 GrabModeAsync, GrabModeAsync);
arg@160 381 XGrabKey(dpy, code, key[i].mod | LockMask, root, True,
arg@160 382 GrabModeAsync, GrabModeAsync);
arg@291 383 XGrabKey(dpy, code, key[i].mod | numlockmask, root, True,
arg@146 384 GrabModeAsync, GrabModeAsync);
arg@291 385 XGrabKey(dpy, code, key[i].mod | numlockmask | LockMask, root, True,
arg@146 386 GrabModeAsync, GrabModeAsync);
garbeam@76 387 }
garbeam@76 388 }
arg@292 389
arg@292 390 void
arg@461 391 procevent() {
arg@292 392 XEvent ev;
arg@292 393
arg@292 394 while(XPending(dpy)) {
arg@292 395 XNextEvent(dpy, &ev);
arg@292 396 if(handler[ev.type])
arg@292 397 (handler[ev.type])(&ev); /* call handler */
arg@292 398 }
arg@292 399 }