aewl

annotate event.c @ 276:9a0a351dd910

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