aewl

annotate event.c @ 386:58288246d16c

now dwm enforces max screen size also in tiled mode on non-floating clients which are maximized
author Anselm R. Garbe <arg@10kloc.org>
date Tue, 29 Aug 2006 18:05:02 +0200
parents 37706ba7d492
children dca6aca09653
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
garbeam@77 24 movemouse(Client *c)
garbeam@77 25 {
garbeam@77 26 int x1, y1, ocx, ocy, di;
garbeam@77 27 unsigned int dui;
garbeam@77 28 Window dummy;
arg@123 29 XEvent ev;
garbeam@77 30
arg@115 31 ocx = c->x;
arg@115 32 ocy = c->y;
arg@148 33 if(XGrabPointer(dpy, root, False, MOUSEMASK, GrabModeAsync, GrabModeAsync,
arg@123 34 None, cursor[CurMove], CurrentTime) != GrabSuccess)
garbeam@77 35 return;
garbeam@77 36 XQueryPointer(dpy, root, &dummy, &dummy, &x1, &y1, &di, &di, &dui);
garbeam@77 37 for(;;) {
arg@148 38 XMaskEvent(dpy, MOUSEMASK | ExposureMask, &ev);
garbeam@77 39 switch (ev.type) {
garbeam@77 40 default: 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
garbeam@77 58 resizemouse(Client *c)
garbeam@77 59 {
garbeam@77 60 int ocx, ocy;
arg@268 61 int nw, nh;
arg@99 62 Corner sticky;
arg@123 63 XEvent ev;
garbeam@77 64
arg@115 65 ocx = c->x;
arg@115 66 ocy = c->y;
arg@148 67 if(XGrabPointer(dpy, root, False, MOUSEMASK, GrabModeAsync, GrabModeAsync,
garbeam@77 68 None, cursor[CurResize], CurrentTime) != GrabSuccess)
garbeam@77 69 return;
arg@115 70 XWarpPointer(dpy, None, c->win, 0, 0, 0, 0, c->w, c->h);
garbeam@77 71 for(;;) {
arg@148 72 XMaskEvent(dpy, MOUSEMASK | ExposureMask, &ev);
garbeam@77 73 switch(ev.type) {
garbeam@77 74 default: 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
garbeam@18 100 buttonpress(XEvent *e)
garbeam@18 101 {
garbeam@73 102 int x;
garbeam@73 103 Arg a;
arg@123 104 Client *c;
garbeam@18 105 XButtonPressedEvent *ev = &e->xbutton;
garbeam@18 106
garbeam@73 107 if(barwin == ev->window) {
arg@362 108 x = 0;
arg@362 109 for(a.i = 0; a.i < ntags; a.i++) {
arg@362 110 x += textw(tags[a.i]);
arg@362 111 if(ev->x < x) {
arg@362 112 if(ev->button == Button1)
arg@362 113 view(&a);
arg@362 114 else if(ev->button == Button3)
arg@362 115 toggleview(&a);
arg@362 116 return;
garbeam@73 117 }
garbeam@73 118 }
arg@371 119 if(ev->x < x + bmw) {
arg@371 120 if(ev->button == Button1)
arg@371 121 togglemode(NULL);
arg@371 122 }
garbeam@73 123 }
garbeam@58 124 else if((c = getclient(ev->window))) {
arg@143 125 focus(c);
arg@372 126 if(CLEANMASK(ev->state) != MODKEY)
arg@318 127 return;
garbeam@18 128 switch(ev->button) {
garbeam@18 129 default:
garbeam@18 130 break;
garbeam@18 131 case Button1:
arg@270 132 if(!c->ismax && (arrange == dofloat || c->isfloat)) {
arg@270 133 restack(c);
arg@238 134 movemouse(c);
arg@270 135 }
arg@238 136 break;
arg@238 137 case Button2:
arg@248 138 zoom(NULL);
garbeam@18 139 break;
garbeam@18 140 case Button3:
arg@270 141 if(!c->ismax && (arrange == dofloat || c->isfloat)) {
arg@270 142 restack(c);
arg@99 143 resizemouse(c);
arg@270 144 }
garbeam@18 145 break;
garbeam@18 146 }
garbeam@18 147 }
garbeam@18 148 }
garbeam@18 149
garbeam@18 150 static void
garbeam@5 151 configurerequest(XEvent *e)
garbeam@5 152 {
arg@286 153 unsigned long newmask;
arg@123 154 Client *c;
garbeam@5 155 XConfigureRequestEvent *ev = &e->xconfigurerequest;
arg@164 156 XEvent synev;
garbeam@5 157 XWindowChanges wc;
garbeam@5 158
garbeam@18 159 if((c = getclient(ev->window))) {
arg@386 160 if(!c->isfloat && (arrange != dofloat) && c->ismax) {
arg@386 161 synev.type = ConfigureNotify;
arg@386 162 synev.xconfigure.display = dpy;
arg@386 163 synev.xconfigure.event = c->win;
arg@386 164 synev.xconfigure.window = c->win;
arg@386 165 synev.xconfigure.x = sx;
arg@386 166 synev.xconfigure.y = sy + bh;
arg@386 167 synev.xconfigure.width = sw - 2;
arg@386 168 synev.xconfigure.height = sw - 2 - bh;
arg@386 169 synev.xconfigure.border_width = ev->border_width;
arg@386 170 synev.xconfigure.above = None;
arg@386 171 XSendEvent(dpy, c->win, True, NoEventMask, &synev);
arg@386 172 return;
arg@386 173 }
garbeam@29 174 gravitate(c, True);
arg@195 175 if(ev->value_mask & CWX)
arg@195 176 c->x = ev->x;
arg@195 177 if(ev->value_mask & CWY)
arg@195 178 c->y = ev->y;
arg@195 179 if(ev->value_mask & CWWidth)
arg@195 180 c->w = ev->width;
arg@195 181 if(ev->value_mask & CWHeight)
arg@195 182 c->h = ev->height;
garbeam@29 183 if(ev->value_mask & CWBorderWidth)
arg@164 184 c->border = ev->border_width;
garbeam@29 185 gravitate(c, False);
arg@164 186 wc.x = c->x;
arg@164 187 wc.y = c->y;
arg@164 188 wc.width = c->w;
arg@164 189 wc.height = c->h;
arg@164 190 newmask = ev->value_mask & (~(CWSibling | CWStackMode | CWBorderWidth));
arg@164 191 if(newmask)
arg@164 192 XConfigureWindow(dpy, c->win, newmask, &wc);
arg@164 193 else {
arg@164 194 synev.type = ConfigureNotify;
arg@164 195 synev.xconfigure.display = dpy;
arg@164 196 synev.xconfigure.event = c->win;
arg@164 197 synev.xconfigure.window = c->win;
arg@164 198 synev.xconfigure.x = c->x;
arg@164 199 synev.xconfigure.y = c->y;
arg@164 200 synev.xconfigure.width = c->w;
arg@164 201 synev.xconfigure.height = c->h;
arg@164 202 synev.xconfigure.border_width = c->border;
arg@164 203 synev.xconfigure.above = None;
arg@164 204 XSendEvent(dpy, c->win, True, NoEventMask, &synev);
arg@164 205 }
arg@195 206 XSync(dpy, False);
arg@385 207 if(c->isfloat)
arg@385 208 resize(c, False, TopLeft);
arg@196 209 else
arg@196 210 arrange(NULL);
garbeam@5 211 }
arg@164 212 else {
arg@164 213 wc.x = ev->x;
arg@164 214 wc.y = ev->y;
arg@164 215 wc.width = ev->width;
arg@164 216 wc.height = ev->height;
arg@164 217 wc.border_width = ev->border_width;
arg@164 218 wc.sibling = ev->above;
arg@164 219 wc.stack_mode = ev->detail;
arg@164 220 XConfigureWindow(dpy, ev->window, ev->value_mask, &wc);
arg@195 221 XSync(dpy, False);
arg@164 222 }
garbeam@5 223 }
garbeam@5 224
garbeam@5 225 static void
garbeam@5 226 destroynotify(XEvent *e)
garbeam@5 227 {
garbeam@5 228 Client *c;
garbeam@5 229 XDestroyWindowEvent *ev = &e->xdestroywindow;
garbeam@5 230
garbeam@11 231 if((c = getclient(ev->window)))
garbeam@11 232 unmanage(c);
garbeam@5 233 }
garbeam@5 234
garbeam@5 235 static void
garbeam@5 236 enternotify(XEvent *e)
garbeam@5 237 {
arg@123 238 Client *c;
garbeam@5 239 XCrossingEvent *ev = &e->xcrossing;
garbeam@5 240
arg@232 241 if(ev->mode != NotifyNormal || ev->detail == NotifyInferior)
garbeam@5 242 return;
garbeam@5 243
arg@161 244 if((c = getclient(ev->window)) || (c = getctitle(ev->window)))
garbeam@13 245 focus(c);
arg@239 246 else if(ev->window == root) {
garbeam@31 247 issel = True;
arg@239 248 XSetInputFocus(dpy, root, RevertToPointerRoot, CurrentTime);
arg@239 249 drawall();
arg@239 250 }
garbeam@5 251 }
garbeam@5 252
garbeam@5 253 static void
garbeam@5 254 expose(XEvent *e)
garbeam@5 255 {
arg@123 256 Client *c;
garbeam@5 257 XExposeEvent *ev = &e->xexpose;
garbeam@5 258
garbeam@5 259 if(ev->count == 0) {
garbeam@70 260 if(barwin == ev->window)
garbeam@74 261 drawstatus();
garbeam@75 262 else if((c = getctitle(ev->window)))
garbeam@74 263 drawtitle(c);
garbeam@5 264 }
garbeam@5 265 }
garbeam@5 266
garbeam@5 267 static void
garbeam@76 268 keypress(XEvent *e)
garbeam@76 269 {
arg@138 270 static unsigned int len = sizeof(key) / sizeof(key[0]);
garbeam@76 271 unsigned int i;
garbeam@76 272 KeySym keysym;
arg@123 273 XKeyEvent *ev = &e->xkey;
garbeam@76 274
garbeam@76 275 keysym = XKeycodeToKeysym(dpy, (KeyCode)ev->keycode, 0);
arg@275 276 for(i = 0; i < len; i++) {
arg@160 277 if(keysym == key[i].keysym &&
arg@275 278 CLEANMASK(key[i].mod) == CLEANMASK(ev->state))
arg@275 279 {
garbeam@76 280 if(key[i].func)
garbeam@76 281 key[i].func(&key[i].arg);
garbeam@76 282 return;
garbeam@76 283 }
arg@275 284 }
garbeam@76 285 }
garbeam@76 286
garbeam@76 287 static void
garbeam@76 288 leavenotify(XEvent *e)
garbeam@76 289 {
garbeam@76 290 XCrossingEvent *ev = &e->xcrossing;
garbeam@76 291
arg@239 292 if((ev->window == root) && !ev->same_screen) {
arg@239 293 issel = False;
arg@239 294 drawall();
arg@239 295 }
garbeam@76 296 }
garbeam@76 297
garbeam@76 298 static void
arg@279 299 mappingnotify(XEvent *e)
arg@279 300 {
arg@279 301 XMappingEvent *ev = &e->xmapping;
arg@279 302
arg@279 303 XRefreshKeyboardMapping(ev);
arg@279 304 if(ev->request == MappingKeyboard)
arg@279 305 grabkeys();
arg@279 306 }
arg@279 307
arg@279 308 static void
garbeam@5 309 maprequest(XEvent *e)
garbeam@5 310 {
arg@123 311 static XWindowAttributes wa;
garbeam@5 312 XMapRequestEvent *ev = &e->xmaprequest;
garbeam@5 313
garbeam@5 314 if(!XGetWindowAttributes(dpy, ev->window, &wa))
garbeam@5 315 return;
garbeam@5 316
garbeam@5 317 if(wa.override_redirect) {
garbeam@5 318 XSelectInput(dpy, ev->window,
garbeam@5 319 (StructureNotifyMask | PropertyChangeMask));
garbeam@5 320 return;
garbeam@5 321 }
garbeam@5 322
garbeam@10 323 if(!getclient(ev->window))
garbeam@10 324 manage(ev->window, &wa);
garbeam@5 325 }
garbeam@5 326
garbeam@5 327 static void
garbeam@5 328 propertynotify(XEvent *e)
garbeam@5 329 {
arg@123 330 Client *c;
arg@123 331 Window trans;
garbeam@5 332 XPropertyEvent *ev = &e->xproperty;
garbeam@5 333
garbeam@5 334 if(ev->state == PropertyDelete)
garbeam@5 335 return; /* ignore */
garbeam@5 336
garbeam@13 337 if((c = getclient(ev->window))) {
garbeam@77 338 if(ev->atom == wmatom[WMProtocols]) {
garbeam@75 339 c->proto = getproto(c->win);
garbeam@30 340 return;
garbeam@30 341 }
garbeam@13 342 switch (ev->atom) {
garbeam@13 343 default: break;
garbeam@13 344 case XA_WM_TRANSIENT_FOR:
garbeam@53 345 XGetTransientForHint(dpy, c->win, &trans);
garbeam@80 346 if(!c->isfloat && (c->isfloat = (trans != 0)))
garbeam@53 347 arrange(NULL);
garbeam@13 348 break;
garbeam@13 349 case XA_WM_NORMAL_HINTS:
garbeam@74 350 setsize(c);
garbeam@13 351 break;
garbeam@13 352 }
garbeam@77 353 if(ev->atom == XA_WM_NAME || ev->atom == netatom[NetWMName]) {
garbeam@74 354 settitle(c);
garbeam@74 355 drawtitle(c);
garbeam@13 356 }
garbeam@13 357 }
garbeam@5 358 }
garbeam@5 359
garbeam@5 360 static void
garbeam@5 361 unmapnotify(XEvent *e)
garbeam@5 362 {
garbeam@5 363 Client *c;
garbeam@5 364 XUnmapEvent *ev = &e->xunmap;
garbeam@5 365
garbeam@10 366 if((c = getclient(ev->window)))
garbeam@10 367 unmanage(c);
garbeam@5 368 }
garbeam@76 369
garbeam@84 370 /* extern */
garbeam@76 371
garbeam@76 372 void (*handler[LASTEvent]) (XEvent *) = {
garbeam@76 373 [ButtonPress] = buttonpress,
garbeam@76 374 [ConfigureRequest] = configurerequest,
garbeam@76 375 [DestroyNotify] = destroynotify,
garbeam@76 376 [EnterNotify] = enternotify,
garbeam@76 377 [LeaveNotify] = leavenotify,
garbeam@76 378 [Expose] = expose,
garbeam@76 379 [KeyPress] = keypress,
arg@279 380 [MappingNotify] = mappingnotify,
garbeam@76 381 [MapRequest] = maprequest,
garbeam@76 382 [PropertyNotify] = propertynotify,
garbeam@76 383 [UnmapNotify] = unmapnotify
garbeam@76 384 };
garbeam@76 385
garbeam@76 386 void
garbeam@76 387 grabkeys()
garbeam@76 388 {
arg@138 389 static unsigned int len = sizeof(key) / sizeof(key[0]);
garbeam@76 390 unsigned int i;
garbeam@76 391 KeyCode code;
garbeam@76 392
arg@279 393 XUngrabKey(dpy, AnyKey, AnyModifier, root);
garbeam@76 394 for(i = 0; i < len; i++) {
garbeam@76 395 code = XKeysymToKeycode(dpy, key[i].keysym);
garbeam@76 396 XGrabKey(dpy, code, key[i].mod, root, True,
garbeam@76 397 GrabModeAsync, GrabModeAsync);
arg@160 398 XGrabKey(dpy, code, key[i].mod | LockMask, root, True,
arg@160 399 GrabModeAsync, GrabModeAsync);
arg@291 400 XGrabKey(dpy, code, key[i].mod | numlockmask, root, True,
arg@146 401 GrabModeAsync, GrabModeAsync);
arg@291 402 XGrabKey(dpy, code, key[i].mod | numlockmask | LockMask, root, True,
arg@146 403 GrabModeAsync, GrabModeAsync);
garbeam@76 404 }
garbeam@76 405 }
arg@292 406
arg@292 407 void
arg@292 408 procevent()
arg@292 409 {
arg@292 410 XEvent ev;
arg@292 411
arg@292 412 while(XPending(dpy)) {
arg@292 413 XNextEvent(dpy, &ev);
arg@292 414 if(handler[ev.type])
arg@292 415 (handler[ev.type])(&ev); /* call handler */
arg@292 416 }
arg@292 417 }
arg@292 418