dwm-meillo

annotate event.c @ 267:8a8ea74e1b87

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