aewl

annotate event.c @ 268:a47b3b0d7bf4

applied Sanders LD and resize patches
author Anselm R.Garbe <arg@10ksloc.org>
date Mon, 14 Aug 2006 08:43:19 +0200
parents 8a8ea74e1b87
children dacd3f3c5823
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@160 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@267 108 x = 0;
arg@267 109 for(a.i = 0; a.i < ntags; a.i++) {
arg@267 110 x += textw(tags[a.i]);
arg@267 111 if(ev->x < x) {
arg@267 112 if(ev->button == Button3)
arg@267 113 toggleview(&a);
arg@267 114 else
arg@267 115 view(&a);
arg@267 116 return;
garbeam@73 117 }
garbeam@73 118 }
garbeam@73 119 }
garbeam@58 120 else if((c = getclient(ev->window))) {
arg@238 121 higher(c);
arg@143 122 focus(c);
garbeam@18 123 switch(ev->button) {
garbeam@18 124 default:
garbeam@18 125 break;
garbeam@18 126 case Button1:
arg@238 127 if(!c->ismax && (arrange == dofloat || c->isfloat))
arg@238 128 movemouse(c);
arg@238 129 break;
arg@238 130 case Button2:
arg@248 131 zoom(NULL);
garbeam@18 132 break;
garbeam@18 133 case Button3:
arg@238 134 if(!c->ismax && (arrange == dofloat || c->isfloat))
arg@99 135 resizemouse(c);
garbeam@18 136 break;
garbeam@18 137 }
garbeam@18 138 }
garbeam@18 139 }
garbeam@18 140
garbeam@18 141 static void
garbeam@5 142 configurerequest(XEvent *e)
garbeam@5 143 {
arg@123 144 Client *c;
garbeam@5 145 XConfigureRequestEvent *ev = &e->xconfigurerequest;
arg@164 146 XEvent synev;
garbeam@5 147 XWindowChanges wc;
arg@164 148 unsigned long newmask;
garbeam@5 149
garbeam@18 150 if((c = getclient(ev->window))) {
garbeam@29 151 gravitate(c, True);
arg@195 152 if(ev->value_mask & CWX)
arg@195 153 c->x = ev->x;
arg@195 154 if(ev->value_mask & CWY)
arg@195 155 c->y = ev->y;
arg@195 156 if(ev->value_mask & CWWidth)
arg@195 157 c->w = ev->width;
arg@195 158 if(ev->value_mask & CWHeight)
arg@195 159 c->h = ev->height;
garbeam@29 160 if(ev->value_mask & CWBorderWidth)
arg@164 161 c->border = ev->border_width;
garbeam@29 162 gravitate(c, False);
arg@164 163 wc.x = c->x;
arg@164 164 wc.y = c->y;
arg@164 165 wc.width = c->w;
arg@164 166 wc.height = c->h;
arg@164 167 newmask = ev->value_mask & (~(CWSibling | CWStackMode | CWBorderWidth));
arg@164 168 if(newmask)
arg@164 169 XConfigureWindow(dpy, c->win, newmask, &wc);
arg@164 170 else {
arg@164 171 synev.type = ConfigureNotify;
arg@164 172 synev.xconfigure.display = dpy;
arg@164 173 synev.xconfigure.event = c->win;
arg@164 174 synev.xconfigure.window = c->win;
arg@164 175 synev.xconfigure.x = c->x;
arg@164 176 synev.xconfigure.y = c->y;
arg@164 177 synev.xconfigure.width = c->w;
arg@164 178 synev.xconfigure.height = c->h;
arg@164 179 synev.xconfigure.border_width = c->border;
arg@164 180 synev.xconfigure.above = None;
arg@164 181 /* Send synthetic ConfigureNotify */
arg@164 182 XSendEvent(dpy, c->win, True, NoEventMask, &synev);
arg@164 183 }
arg@195 184 XSync(dpy, False);
arg@196 185 if(c->isfloat)
arg@196 186 resize(c, False, TopLeft);
arg@196 187 else
arg@196 188 arrange(NULL);
garbeam@5 189 }
arg@164 190 else {
arg@164 191 wc.x = ev->x;
arg@164 192 wc.y = ev->y;
arg@164 193 wc.width = ev->width;
arg@164 194 wc.height = ev->height;
arg@164 195 wc.border_width = ev->border_width;
arg@164 196 wc.sibling = ev->above;
arg@164 197 wc.stack_mode = ev->detail;
arg@164 198 XConfigureWindow(dpy, ev->window, ev->value_mask, &wc);
arg@195 199 XSync(dpy, False);
arg@164 200 }
garbeam@5 201 }
garbeam@5 202
garbeam@5 203 static void
garbeam@5 204 destroynotify(XEvent *e)
garbeam@5 205 {
garbeam@5 206 Client *c;
garbeam@5 207 XDestroyWindowEvent *ev = &e->xdestroywindow;
garbeam@5 208
garbeam@11 209 if((c = getclient(ev->window)))
garbeam@11 210 unmanage(c);
garbeam@5 211 }
garbeam@5 212
garbeam@5 213 static void
garbeam@5 214 enternotify(XEvent *e)
garbeam@5 215 {
arg@123 216 Client *c;
garbeam@5 217 XCrossingEvent *ev = &e->xcrossing;
garbeam@5 218
arg@232 219 if(ev->mode != NotifyNormal || ev->detail == NotifyInferior)
garbeam@5 220 return;
garbeam@5 221
arg@161 222 if((c = getclient(ev->window)) || (c = getctitle(ev->window)))
garbeam@13 223 focus(c);
arg@239 224 else if(ev->window == root) {
garbeam@31 225 issel = True;
arg@239 226 XSetInputFocus(dpy, root, RevertToPointerRoot, CurrentTime);
arg@239 227 drawall();
arg@239 228 }
garbeam@5 229 }
garbeam@5 230
garbeam@5 231 static void
garbeam@5 232 expose(XEvent *e)
garbeam@5 233 {
arg@123 234 Client *c;
garbeam@5 235 XExposeEvent *ev = &e->xexpose;
garbeam@5 236
garbeam@5 237 if(ev->count == 0) {
garbeam@70 238 if(barwin == ev->window)
garbeam@74 239 drawstatus();
garbeam@75 240 else if((c = getctitle(ev->window)))
garbeam@74 241 drawtitle(c);
garbeam@5 242 }
garbeam@5 243 }
garbeam@5 244
garbeam@5 245 static void
garbeam@76 246 keypress(XEvent *e)
garbeam@76 247 {
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);
garbeam@76 254 for(i = 0; i < len; i++)
arg@160 255 if(keysym == key[i].keysym &&
arg@160 256 CLEANMASK(key[i].mod) == CLEANMASK(ev->state)) {
garbeam@76 257 if(key[i].func)
garbeam@76 258 key[i].func(&key[i].arg);
garbeam@76 259 return;
garbeam@76 260 }
garbeam@76 261 }
garbeam@76 262
garbeam@76 263 static void
garbeam@76 264 leavenotify(XEvent *e)
garbeam@76 265 {
garbeam@76 266 XCrossingEvent *ev = &e->xcrossing;
garbeam@76 267
arg@239 268 if((ev->window == root) && !ev->same_screen) {
arg@239 269 issel = False;
arg@239 270 drawall();
arg@239 271 }
garbeam@76 272 }
garbeam@76 273
garbeam@76 274 static void
garbeam@5 275 maprequest(XEvent *e)
garbeam@5 276 {
arg@123 277 static XWindowAttributes wa;
garbeam@5 278 XMapRequestEvent *ev = &e->xmaprequest;
garbeam@5 279
garbeam@5 280 if(!XGetWindowAttributes(dpy, ev->window, &wa))
garbeam@5 281 return;
garbeam@5 282
garbeam@5 283 if(wa.override_redirect) {
garbeam@5 284 XSelectInput(dpy, ev->window,
garbeam@5 285 (StructureNotifyMask | PropertyChangeMask));
garbeam@5 286 return;
garbeam@5 287 }
garbeam@5 288
garbeam@10 289 if(!getclient(ev->window))
garbeam@10 290 manage(ev->window, &wa);
garbeam@5 291 }
garbeam@5 292
garbeam@5 293 static void
garbeam@5 294 propertynotify(XEvent *e)
garbeam@5 295 {
arg@123 296 Client *c;
arg@123 297 Window trans;
garbeam@5 298 XPropertyEvent *ev = &e->xproperty;
garbeam@5 299
garbeam@5 300 if(ev->state == PropertyDelete)
garbeam@5 301 return; /* ignore */
garbeam@5 302
garbeam@13 303 if((c = getclient(ev->window))) {
garbeam@77 304 if(ev->atom == wmatom[WMProtocols]) {
garbeam@75 305 c->proto = getproto(c->win);
garbeam@30 306 return;
garbeam@30 307 }
garbeam@13 308 switch (ev->atom) {
garbeam@13 309 default: break;
garbeam@13 310 case XA_WM_TRANSIENT_FOR:
garbeam@53 311 XGetTransientForHint(dpy, c->win, &trans);
garbeam@80 312 if(!c->isfloat && (c->isfloat = (trans != 0)))
garbeam@53 313 arrange(NULL);
garbeam@13 314 break;
garbeam@13 315 case XA_WM_NORMAL_HINTS:
garbeam@74 316 setsize(c);
garbeam@13 317 break;
garbeam@13 318 }
garbeam@77 319 if(ev->atom == XA_WM_NAME || ev->atom == netatom[NetWMName]) {
garbeam@74 320 settitle(c);
garbeam@74 321 drawtitle(c);
garbeam@13 322 }
garbeam@13 323 }
garbeam@5 324 }
garbeam@5 325
garbeam@5 326 static void
garbeam@5 327 unmapnotify(XEvent *e)
garbeam@5 328 {
garbeam@5 329 Client *c;
garbeam@5 330 XUnmapEvent *ev = &e->xunmap;
garbeam@5 331
garbeam@10 332 if((c = getclient(ev->window)))
garbeam@10 333 unmanage(c);
garbeam@5 334 }
garbeam@76 335
garbeam@84 336 /* extern */
garbeam@76 337
garbeam@76 338 void (*handler[LASTEvent]) (XEvent *) = {
garbeam@76 339 [ButtonPress] = buttonpress,
garbeam@76 340 [ConfigureRequest] = configurerequest,
garbeam@76 341 [DestroyNotify] = destroynotify,
garbeam@76 342 [EnterNotify] = enternotify,
garbeam@76 343 [LeaveNotify] = leavenotify,
garbeam@76 344 [Expose] = expose,
garbeam@76 345 [KeyPress] = keypress,
garbeam@76 346 [MapRequest] = maprequest,
garbeam@76 347 [PropertyNotify] = propertynotify,
garbeam@76 348 [UnmapNotify] = unmapnotify
garbeam@76 349 };
garbeam@76 350
garbeam@76 351 void
garbeam@76 352 grabkeys()
garbeam@76 353 {
arg@138 354 static unsigned int len = sizeof(key) / sizeof(key[0]);
garbeam@76 355 unsigned int i;
garbeam@76 356 KeyCode code;
garbeam@76 357
garbeam@76 358 for(i = 0; i < len; i++) {
garbeam@76 359 code = XKeysymToKeycode(dpy, key[i].keysym);
garbeam@76 360 XGrabKey(dpy, code, key[i].mod, root, True,
garbeam@76 361 GrabModeAsync, GrabModeAsync);
arg@160 362 XGrabKey(dpy, code, key[i].mod | LockMask, root, True,
arg@160 363 GrabModeAsync, GrabModeAsync);
arg@146 364 XGrabKey(dpy, code, key[i].mod | NUMLOCKMASK, root, True,
arg@146 365 GrabModeAsync, GrabModeAsync);
arg@146 366 XGrabKey(dpy, code, key[i].mod | NUMLOCKMASK | LockMask, root, True,
arg@146 367 GrabModeAsync, GrabModeAsync);
garbeam@76 368 }
garbeam@76 369 }