dwm-meillo

annotate event.c @ 152:4a64fd270e47

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