dwm-meillo

annotate event.c @ 146:f328ce9c558c

centralized/externalized configuration to config.h
author arg@10ksloc.org
date Tue, 01 Aug 2006 13:59:13 +0200
parents 774754477c35
children 5267e1204367
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
garbeam@5 7 #include <stdlib.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 CMDS
arg@146 21 KEYS
garbeam@75 22
arg@146 23 static unsigned int valid_mask = 255 & ~(NUMLOCKMASK | LockMask);
garbeam@5 24
garbeam@77 25 static void
garbeam@77 26 movemouse(Client *c)
garbeam@77 27 {
garbeam@77 28 int x1, y1, ocx, ocy, di;
garbeam@77 29 unsigned int dui;
garbeam@77 30 Window dummy;
arg@123 31 XEvent ev;
garbeam@77 32
arg@115 33 ocx = c->x;
arg@115 34 ocy = c->y;
garbeam@77 35 if(XGrabPointer(dpy, root, False, MouseMask, GrabModeAsync, GrabModeAsync,
arg@123 36 None, cursor[CurMove], CurrentTime) != GrabSuccess)
garbeam@77 37 return;
garbeam@77 38 XQueryPointer(dpy, root, &dummy, &dummy, &x1, &y1, &di, &di, &dui);
garbeam@77 39 for(;;) {
garbeam@77 40 XMaskEvent(dpy, MouseMask | ExposureMask, &ev);
garbeam@77 41 switch (ev.type) {
garbeam@77 42 default: break;
garbeam@77 43 case Expose:
garbeam@77 44 handler[Expose](&ev);
garbeam@77 45 break;
garbeam@77 46 case MotionNotify:
garbeam@79 47 XSync(dpy, False);
arg@115 48 c->x = ocx + (ev.xmotion.x - x1);
arg@115 49 c->y = ocy + (ev.xmotion.y - y1);
arg@99 50 resize(c, False, TopLeft);
garbeam@77 51 break;
garbeam@77 52 case ButtonRelease:
garbeam@77 53 XUngrabPointer(dpy, CurrentTime);
garbeam@77 54 return;
garbeam@77 55 }
garbeam@77 56 }
garbeam@77 57 }
garbeam@77 58
garbeam@77 59 static void
garbeam@77 60 resizemouse(Client *c)
garbeam@77 61 {
garbeam@77 62 int ocx, ocy;
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;
garbeam@77 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(;;) {
garbeam@77 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@115 81 c->w = abs(ocx - ev.xmotion.x);
arg@115 82 c->h = abs(ocy - ev.xmotion.y);
arg@115 83 c->x = (ocx <= ev.xmotion.x) ? ocx : ocx - c->w;
arg@115 84 c->y = (ocy <= ev.xmotion.y) ? ocy : ocy - c->h;
arg@105 85 if(ocx <= ev.xmotion.x)
arg@105 86 sticky = (ocy <= ev.xmotion.y) ? TopLeft : BotLeft;
arg@105 87 else
arg@105 88 sticky = (ocy <= ev.xmotion.y) ? TopRight : BotRight;
arg@99 89 resize(c, True, sticky);
garbeam@77 90 break;
garbeam@77 91 case ButtonRelease:
garbeam@77 92 XUngrabPointer(dpy, CurrentTime);
garbeam@77 93 return;
garbeam@77 94 }
garbeam@77 95 }
garbeam@77 96 }
garbeam@73 97
garbeam@73 98 static void
garbeam@18 99 buttonpress(XEvent *e)
garbeam@18 100 {
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) {
garbeam@80 107 switch(ev->button) {
garbeam@80 108 default:
garbeam@80 109 x = 0;
garbeam@80 110 for(a.i = 0; a.i < TLast; a.i++) {
garbeam@80 111 x += textw(tags[a.i]);
garbeam@80 112 if(ev->x < x) {
garbeam@80 113 view(&a);
garbeam@80 114 break;
garbeam@80 115 }
garbeam@73 116 }
garbeam@80 117 break;
garbeam@80 118 case Button4:
garbeam@80 119 a.i = (tsel + 1 < TLast) ? tsel + 1 : 0;
garbeam@80 120 view(&a);
garbeam@80 121 break;
garbeam@80 122 case Button5:
garbeam@80 123 a.i = (tsel - 1 >= 0) ? tsel - 1 : TLast - 1;
garbeam@80 124 view(&a);
garbeam@80 125 break;
garbeam@73 126 }
garbeam@73 127 }
garbeam@58 128 else if((c = getclient(ev->window))) {
arg@143 129 focus(c);
garbeam@18 130 switch(ev->button) {
garbeam@18 131 default:
garbeam@18 132 break;
garbeam@18 133 case Button1:
arg@133 134 if(!c->ismax && (arrange == dofloat || c->isfloat)) {
arg@99 135 higher(c);
arg@99 136 movemouse(c);
arg@99 137 }
garbeam@18 138 break;
garbeam@18 139 case Button2:
garbeam@26 140 lower(c);
garbeam@18 141 break;
garbeam@18 142 case Button3:
arg@133 143 if(!c->ismax && (arrange == dofloat || c->isfloat)) {
arg@99 144 higher(c);
arg@99 145 resizemouse(c);
arg@99 146 }
garbeam@18 147 break;
garbeam@18 148 }
garbeam@18 149 }
garbeam@18 150 }
garbeam@18 151
garbeam@18 152 static void
garbeam@5 153 configurerequest(XEvent *e)
garbeam@5 154 {
arg@123 155 Client *c;
garbeam@5 156 XConfigureRequestEvent *ev = &e->xconfigurerequest;
garbeam@5 157 XWindowChanges wc;
garbeam@5 158
garbeam@5 159 ev->value_mask &= ~CWSibling;
garbeam@18 160 if((c = getclient(ev->window))) {
garbeam@29 161 gravitate(c, True);
garbeam@5 162 if(ev->value_mask & CWX)
arg@115 163 c->x = ev->x;
garbeam@5 164 if(ev->value_mask & CWY)
arg@115 165 c->y = ev->y;
garbeam@5 166 if(ev->value_mask & CWWidth)
arg@115 167 c->w = ev->width;
garbeam@5 168 if(ev->value_mask & CWHeight)
arg@115 169 c->h = ev->height;
garbeam@29 170 if(ev->value_mask & CWBorderWidth)
garbeam@55 171 c->border = 1;
garbeam@29 172 gravitate(c, False);
arg@99 173 resize(c, True, TopLeft);
garbeam@5 174 }
garbeam@5 175
garbeam@5 176 wc.x = ev->x;
garbeam@5 177 wc.y = ev->y;
garbeam@5 178 wc.width = ev->width;
garbeam@5 179 wc.height = ev->height;
garbeam@25 180 wc.border_width = 1;
garbeam@5 181 wc.sibling = None;
garbeam@5 182 wc.stack_mode = Above;
garbeam@5 183 ev->value_mask &= ~CWStackMode;
garbeam@5 184 ev->value_mask |= CWBorderWidth;
garbeam@5 185 XConfigureWindow(dpy, ev->window, ev->value_mask, &wc);
garbeam@79 186 XSync(dpy, False);
garbeam@5 187 }
garbeam@5 188
garbeam@5 189 static void
garbeam@5 190 destroynotify(XEvent *e)
garbeam@5 191 {
garbeam@5 192 Client *c;
garbeam@5 193 XDestroyWindowEvent *ev = &e->xdestroywindow;
garbeam@5 194
garbeam@11 195 if((c = getclient(ev->window)))
garbeam@11 196 unmanage(c);
garbeam@5 197 }
garbeam@5 198
garbeam@5 199 static void
garbeam@5 200 enternotify(XEvent *e)
garbeam@5 201 {
arg@123 202 Client *c;
garbeam@5 203 XCrossingEvent *ev = &e->xcrossing;
garbeam@5 204
arg@143 205 if(ev->detail == NotifyInferior)
garbeam@5 206 return;
garbeam@5 207
garbeam@13 208 if((c = getclient(ev->window)))
garbeam@13 209 focus(c);
garbeam@26 210 else if(ev->window == root)
garbeam@31 211 issel = True;
garbeam@5 212 }
garbeam@5 213
garbeam@5 214 static void
garbeam@5 215 expose(XEvent *e)
garbeam@5 216 {
arg@123 217 Client *c;
garbeam@5 218 XExposeEvent *ev = &e->xexpose;
garbeam@5 219
garbeam@5 220 if(ev->count == 0) {
garbeam@70 221 if(barwin == ev->window)
garbeam@74 222 drawstatus();
garbeam@75 223 else if((c = getctitle(ev->window)))
garbeam@74 224 drawtitle(c);
garbeam@5 225 }
garbeam@5 226 }
garbeam@5 227
garbeam@5 228 static void
garbeam@76 229 keypress(XEvent *e)
garbeam@76 230 {
arg@138 231 static unsigned int len = sizeof(key) / sizeof(key[0]);
garbeam@76 232 unsigned int i;
garbeam@76 233 KeySym keysym;
arg@123 234 XKeyEvent *ev = &e->xkey;
arg@145 235 ev->state &= valid_mask;
garbeam@76 236
garbeam@76 237 keysym = XKeycodeToKeysym(dpy, (KeyCode)ev->keycode, 0);
garbeam@76 238 for(i = 0; i < len; i++)
arg@145 239 if((keysym == key[i].keysym) && ((key[i].mod & valid_mask) == ev->state)) {
garbeam@76 240 if(key[i].func)
garbeam@76 241 key[i].func(&key[i].arg);
garbeam@76 242 return;
garbeam@76 243 }
garbeam@76 244 }
garbeam@76 245
garbeam@76 246 static void
garbeam@76 247 leavenotify(XEvent *e)
garbeam@76 248 {
garbeam@76 249 XCrossingEvent *ev = &e->xcrossing;
garbeam@76 250
garbeam@76 251 if((ev->window == root) && !ev->same_screen)
garbeam@76 252 issel = True;
garbeam@76 253 }
garbeam@76 254
garbeam@76 255 static void
garbeam@5 256 maprequest(XEvent *e)
garbeam@5 257 {
arg@123 258 static XWindowAttributes wa;
garbeam@5 259 XMapRequestEvent *ev = &e->xmaprequest;
garbeam@5 260
garbeam@5 261 if(!XGetWindowAttributes(dpy, ev->window, &wa))
garbeam@5 262 return;
garbeam@5 263
garbeam@5 264 if(wa.override_redirect) {
garbeam@5 265 XSelectInput(dpy, ev->window,
garbeam@5 266 (StructureNotifyMask | PropertyChangeMask));
garbeam@5 267 return;
garbeam@5 268 }
garbeam@5 269
garbeam@10 270 if(!getclient(ev->window))
garbeam@10 271 manage(ev->window, &wa);
garbeam@5 272 }
garbeam@5 273
garbeam@5 274 static void
garbeam@5 275 propertynotify(XEvent *e)
garbeam@5 276 {
arg@123 277 Client *c;
arg@123 278 Window trans;
garbeam@5 279 XPropertyEvent *ev = &e->xproperty;
garbeam@5 280
garbeam@5 281 if(ev->state == PropertyDelete)
garbeam@5 282 return; /* ignore */
garbeam@5 283
garbeam@13 284 if((c = getclient(ev->window))) {
garbeam@77 285 if(ev->atom == wmatom[WMProtocols]) {
garbeam@75 286 c->proto = getproto(c->win);
garbeam@30 287 return;
garbeam@30 288 }
garbeam@13 289 switch (ev->atom) {
garbeam@13 290 default: break;
garbeam@13 291 case XA_WM_TRANSIENT_FOR:
garbeam@53 292 XGetTransientForHint(dpy, c->win, &trans);
garbeam@80 293 if(!c->isfloat && (c->isfloat = (trans != 0)))
garbeam@53 294 arrange(NULL);
garbeam@13 295 break;
garbeam@13 296 case XA_WM_NORMAL_HINTS:
garbeam@74 297 setsize(c);
garbeam@13 298 break;
garbeam@13 299 }
garbeam@77 300 if(ev->atom == XA_WM_NAME || ev->atom == netatom[NetWMName]) {
garbeam@74 301 settitle(c);
garbeam@74 302 drawtitle(c);
garbeam@13 303 }
garbeam@13 304 }
garbeam@5 305 }
garbeam@5 306
garbeam@5 307 static void
garbeam@5 308 unmapnotify(XEvent *e)
garbeam@5 309 {
garbeam@5 310 Client *c;
garbeam@5 311 XUnmapEvent *ev = &e->xunmap;
garbeam@5 312
garbeam@10 313 if((c = getclient(ev->window)))
garbeam@10 314 unmanage(c);
garbeam@5 315 }
garbeam@76 316
garbeam@84 317 /* extern */
garbeam@76 318
garbeam@76 319 void (*handler[LASTEvent]) (XEvent *) = {
garbeam@76 320 [ButtonPress] = buttonpress,
garbeam@76 321 [ConfigureRequest] = configurerequest,
garbeam@76 322 [DestroyNotify] = destroynotify,
garbeam@76 323 [EnterNotify] = enternotify,
garbeam@76 324 [LeaveNotify] = leavenotify,
garbeam@76 325 [Expose] = expose,
garbeam@76 326 [KeyPress] = keypress,
garbeam@76 327 [MapRequest] = maprequest,
garbeam@76 328 [PropertyNotify] = propertynotify,
garbeam@76 329 [UnmapNotify] = unmapnotify
garbeam@76 330 };
garbeam@76 331
garbeam@76 332 void
garbeam@76 333 grabkeys()
garbeam@76 334 {
arg@138 335 static unsigned int len = sizeof(key) / sizeof(key[0]);
garbeam@76 336 unsigned int i;
garbeam@76 337 KeyCode code;
garbeam@76 338
garbeam@76 339 for(i = 0; i < len; i++) {
garbeam@76 340 code = XKeysymToKeycode(dpy, key[i].keysym);
garbeam@76 341 XUngrabKey(dpy, code, key[i].mod, root);
arg@146 342 XUngrabKey(dpy, code, key[i].mod | NUMLOCKMASK, root);
arg@146 343 XUngrabKey(dpy, code, key[i].mod | NUMLOCKMASK | LockMask, root);
garbeam@76 344 XGrabKey(dpy, code, key[i].mod, root, True,
garbeam@76 345 GrabModeAsync, GrabModeAsync);
arg@146 346 XGrabKey(dpy, code, key[i].mod | NUMLOCKMASK, root, True,
arg@146 347 GrabModeAsync, GrabModeAsync);
arg@146 348 XGrabKey(dpy, code, key[i].mod | NUMLOCKMASK | LockMask, root, True,
arg@146 349 GrabModeAsync, GrabModeAsync);
garbeam@76 350 }
garbeam@76 351 }