aewl

annotate event.c @ 153:02545d36e9d3

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