X-Git-Url: https://jasonwoof.com/gitweb/?a=blobdiff_plain;f=event.c;h=58df281687e91d4e99dc1c6137cdaabe234021f9;hb=ac24f132dba7e59d9d9ff98d984f5e0b0d20fd09;hp=d1687874f7de33dd867188cc0a7c9866214a2a3d;hpb=26e134b8a7dae21a699822009674b3131de6e250;p=dwm.git diff --git a/event.c b/event.c index d168787..0043ed2 100644 --- a/event.c +++ b/event.c @@ -1,86 +1,209 @@ -/* - * (C)opyright MMVI Anselm R. Garbe +/* (C)opyright MMVI-MMVII Anselm R. Garbe * See LICENSE file for license details. */ - -#include +#include "dwm.h" #include -#include #include +#include -#include "wm.h" +/* static */ -/* local functions */ -static void configurerequest(XEvent *e); -static void destroynotify(XEvent *e); -static void enternotify(XEvent *e); -static void leavenotify(XEvent *e); -static void expose(XEvent *e); -static void keymapnotify(XEvent *e); -static void maprequest(XEvent *e); -static void propertynotify(XEvent *e); -static void unmapnotify(XEvent *e); +typedef struct { + unsigned long mod; + KeySym keysym; + void (*func)(Arg *arg); + Arg arg; +} Key; -void (*handler[LASTEvent]) (XEvent *) = { - [ConfigureRequest] = configurerequest, - [DestroyNotify] = destroynotify, - [EnterNotify] = enternotify, - [LeaveNotify] = leavenotify, - [Expose] = expose, - [KeyPress] = keypress, - [KeymapNotify] = keymapnotify, - [MapRequest] = maprequest, - [PropertyNotify] = propertynotify, - [UnmapNotify] = unmapnotify -}; +KEYS + +#define CLEANMASK(mask) (mask & ~(numlockmask | LockMask)) +#define MOUSEMASK (BUTTONMASK | PointerMotionMask) + +static void +movemouse(Client *c) { + int x1, y1, ocx, ocy, di; + unsigned int dui; + Window dummy; + XEvent ev; -unsigned int -flush_masked_events(long even_mask) -{ + ocx = c->x; + ocy = c->y; + if(XGrabPointer(dpy, root, False, MOUSEMASK, GrabModeAsync, GrabModeAsync, + None, cursor[CurMove], CurrentTime) != GrabSuccess) + return; + c->ismax = False; + XQueryPointer(dpy, root, &dummy, &dummy, &x1, &y1, &di, &di, &dui); + for(;;) { + XMaskEvent(dpy, MOUSEMASK | ExposureMask, &ev); + switch (ev.type) { + case ButtonRelease: + resize(c, True, TopLeft); + XUngrabPointer(dpy, CurrentTime); + return; + case Expose: + handler[Expose](&ev); + break; + case MotionNotify: + XSync(dpy, False); + c->x = ocx + (ev.xmotion.x - x1); + c->y = ocy + (ev.xmotion.y - y1); + if(abs(wax + c->x) < SNAP) + c->x = wax; + else if(abs((wax + waw) - (c->x + c->w)) < SNAP) + c->x = wax + waw - c->w - 2 * BORDERPX; + if(abs(way - c->y) < SNAP) + c->y = way; + else if(abs((way + wah) - (c->y + c->h)) < SNAP) + c->y = way + wah - c->h - 2 * BORDERPX; + resize(c, False, TopLeft); + break; + } + } +} + +static void +resizemouse(Client *c) { + int ocx, ocy; + int nw, nh; + Corner sticky; XEvent ev; - unsigned int n = 0; - while(XCheckMaskEvent(dpy, even_mask, &ev)) n++; - return n; + + ocx = c->x; + ocy = c->y; + if(XGrabPointer(dpy, root, False, MOUSEMASK, GrabModeAsync, GrabModeAsync, + None, cursor[CurResize], CurrentTime) != GrabSuccess) + return; + c->ismax = False; + XWarpPointer(dpy, None, c->win, 0, 0, 0, 0, c->w, c->h); + for(;;) { + XMaskEvent(dpy, MOUSEMASK | ExposureMask, &ev); + switch(ev.type) { + case ButtonRelease: + resize(c, True, TopLeft); + XUngrabPointer(dpy, CurrentTime); + return; + case Expose: + handler[Expose](&ev); + break; + case MotionNotify: + XSync(dpy, False); + if((nw = abs(ocx - ev.xmotion.x))) + c->w = nw; + if((nh = abs(ocy - ev.xmotion.y))) + c->h = nh; + c->x = (ocx <= ev.xmotion.x) ? ocx : ocx - c->w; + c->y = (ocy <= ev.xmotion.y) ? ocy : ocy - c->h; + if(ocx <= ev.xmotion.x) + sticky = (ocy <= ev.xmotion.y) ? TopLeft : BotLeft; + else + sticky = (ocy <= ev.xmotion.y) ? TopRight : BotRight; + resize(c, True, sticky); + break; + } + } } static void -configurerequest(XEvent *e) -{ +buttonpress(XEvent *e) { + int x; + Arg a; + Client *c; + XButtonPressedEvent *ev = &e->xbutton; + + if(barwin == ev->window) { + x = 0; + for(a.i = 0; a.i < ntags; a.i++) { + x += textw(tags[a.i]); + if(ev->x < x) { + if(ev->button == Button1) { + if(ev->state & MODKEY) + tag(&a); + else + view(&a); + } + else if(ev->button == Button3) { + if(ev->state & MODKEY) + toggletag(&a); + else + toggleview(&a); + } + return; + } + } + if((ev->x < x + bmw) && (ev->button == Button1)) + togglemode(NULL); + } + else if((c = getclient(ev->window))) { + focus(c); + if(CLEANMASK(ev->state) != MODKEY) + return; + if(ev->button == Button1 && (arrange == dofloat || c->isfloat)) { + restack(); + movemouse(c); + } + else if(ev->button == Button2) + zoom(NULL); + else if(ev->button == Button3 && (arrange == dofloat || c->isfloat) && + !c->isfixed) { + restack(); + resizemouse(c); + } + } +} + +static void +configurerequest(XEvent *e) { + unsigned long newmask; + Client *c; XConfigureRequestEvent *ev = &e->xconfigurerequest; XWindowChanges wc; - Client *c; - c = getclient(ev->window); - ev->value_mask &= ~CWSibling; - if(c) { + if((c = getclient(ev->window))) { + c->ismax = False; if(ev->value_mask & CWX) - c->r[RFloat].x = ev->x; + c->x = ev->x; if(ev->value_mask & CWY) - c->r[RFloat].y = ev->y; + c->y = ev->y; if(ev->value_mask & CWWidth) - c->r[RFloat].width = ev->width; + c->w = ev->width; if(ev->value_mask & CWHeight) - c->r[RFloat].height = ev->height; + c->h = ev->height; if(ev->value_mask & CWBorderWidth) c->border = ev->border_width; + wc.x = c->x; + wc.y = c->y; + wc.width = c->w; + wc.height = c->h; + newmask = ev->value_mask & (~(CWSibling | CWStackMode | CWBorderWidth)); + if(newmask) + XConfigureWindow(dpy, c->win, newmask, &wc); + else + configure(c); + XSync(dpy, False); + if(c->isfloat) { + resize(c, False, TopLeft); + if(!isvisible(c)) + ban(c); + } + else + arrange(); + } + else { + wc.x = ev->x; + wc.y = ev->y; + wc.width = ev->width; + wc.height = ev->height; + wc.border_width = ev->border_width; + wc.sibling = ev->above; + wc.stack_mode = ev->detail; + XConfigureWindow(dpy, ev->window, ev->value_mask, &wc); + XSync(dpy, False); } - - wc.x = ev->x; - wc.y = ev->y; - wc.width = ev->width; - wc.height = ev->height; - wc.border_width = 0; - wc.sibling = None; - wc.stack_mode = Above; - ev->value_mask &= ~CWStackMode; - ev->value_mask |= CWBorderWidth; - XConfigureWindow(dpy, ev->window, ev->value_mask, &wc); - XFlush(dpy); } static void -destroynotify(XEvent *e) -{ +destroynotify(XEvent *e) { Client *c; XDestroyWindowEvent *ev = &e->xdestroywindow; @@ -89,99 +212,171 @@ destroynotify(XEvent *e) } static void -enternotify(XEvent *e) -{ -#if 0 - XCrossingEvent *ev = &e->xcrossing; +enternotify(XEvent *e) { Client *c; + XCrossingEvent *ev = &e->xcrossing; if(ev->mode != NotifyNormal || ev->detail == NotifyInferior) return; - - if((c = client_of_win(ev->window))) { - Frame *f = c->sel; - Area *a = f->area; - if(a->mode == Colmax) - c = a->sel->client; - focus(c, False); - } + if(((c = getclient(ev->window)) || (c = getctitle(ev->window))) && isvisible(c)) + focus(c); else if(ev->window == root) { - sel_screen = True; - draw_frames(); + issel = True; + XSetInputFocus(dpy, root, RevertToPointerRoot, CurrentTime); + drawall(); } -#endif } static void -leavenotify(XEvent *e) -{ - XCrossingEvent *ev = &e->xcrossing; +expose(XEvent *e) { + Client *c; + XExposeEvent *ev = &e->xexpose; - if((ev->window == root) && !ev->same_screen) { - sel_screen = True; - /*draw_frames();*/ + if(ev->count == 0) { + if(barwin == ev->window) + drawstatus(); + else if((c = getctitle(ev->window))) + drawtitle(c); } } static void -expose(XEvent *e) -{ - XExposeEvent *ev = &e->xexpose; +keypress(XEvent *e) { + static unsigned int len = sizeof key / sizeof key[0]; + unsigned int i; + KeySym keysym; + XKeyEvent *ev = &e->xkey; - if(ev->count == 0) { - if(ev->window == barwin) - draw_bar(); + keysym = XKeycodeToKeysym(dpy, (KeyCode)ev->keycode, 0); + for(i = 0; i < len; i++) { + if(keysym == key[i].keysym + && CLEANMASK(key[i].mod) == CLEANMASK(ev->state)) + { + if(key[i].func) + key[i].func(&key[i].arg); + } } } static void -keymapnotify(XEvent *e) -{ -#if 0 - update_keys(); -#endif +leavenotify(XEvent *e) { + XCrossingEvent *ev = &e->xcrossing; + + if((ev->window == root) && !ev->same_screen) { + issel = False; + drawall(); + } } static void -maprequest(XEvent *e) -{ - XMapRequestEvent *ev = &e->xmaprequest; +mappingnotify(XEvent *e) { + XMappingEvent *ev = &e->xmapping; + + XRefreshKeyboardMapping(ev); + if(ev->request == MappingKeyboard) + grabkeys(); +} + +static void +maprequest(XEvent *e) { static XWindowAttributes wa; + XMapRequestEvent *ev = &e->xmaprequest; if(!XGetWindowAttributes(dpy, ev->window, &wa)) return; - if(wa.override_redirect) { XSelectInput(dpy, ev->window, (StructureNotifyMask | PropertyChangeMask)); return; } - if(!getclient(ev->window)) manage(ev->window, &wa); } static void -propertynotify(XEvent *e) -{ -#if 0 - XPropertyEvent *ev = &e->xproperty; +propertynotify(XEvent *e) { Client *c; + Window trans; + XPropertyEvent *ev = &e->xproperty; if(ev->state == PropertyDelete) return; /* ignore */ - - if((c = client_of_win(ev->window))) - prop_client(c, ev); -#endif + if((c = getclient(ev->window))) { + if(ev->atom == wmatom[WMProtocols]) { + c->proto = getproto(c->win); + return; + } + switch (ev->atom) { + default: break; + case XA_WM_TRANSIENT_FOR: + XGetTransientForHint(dpy, c->win, &trans); + if(!c->isfloat && (c->isfloat = (trans != 0))) + arrange(); + break; + case XA_WM_NORMAL_HINTS: + updatesizehints(c); + break; + } + if(ev->atom == XA_WM_NAME || ev->atom == netatom[NetWMName]) { + updatetitle(c); + resizetitle(c); + drawtitle(c); + } + } } static void -unmapnotify(XEvent *e) -{ +unmapnotify(XEvent *e) { Client *c; XUnmapEvent *ev = &e->xunmap; if((c = getclient(ev->window))) unmanage(c); } + +/* extern */ + +void (*handler[LASTEvent]) (XEvent *) = { + [ButtonPress] = buttonpress, + [ConfigureRequest] = configurerequest, + [DestroyNotify] = destroynotify, + [EnterNotify] = enternotify, + [LeaveNotify] = leavenotify, + [Expose] = expose, + [KeyPress] = keypress, + [MappingNotify] = mappingnotify, + [MapRequest] = maprequest, + [PropertyNotify] = propertynotify, + [UnmapNotify] = unmapnotify +}; + +void +grabkeys(void) { + static unsigned int len = sizeof key / sizeof key[0]; + unsigned int i; + KeyCode code; + + XUngrabKey(dpy, AnyKey, AnyModifier, root); + for(i = 0; i < len; i++) { + code = XKeysymToKeycode(dpy, key[i].keysym); + XGrabKey(dpy, code, key[i].mod, root, True, + GrabModeAsync, GrabModeAsync); + XGrabKey(dpy, code, key[i].mod | LockMask, root, True, + GrabModeAsync, GrabModeAsync); + XGrabKey(dpy, code, key[i].mod | numlockmask, root, True, + GrabModeAsync, GrabModeAsync); + XGrabKey(dpy, code, key[i].mod | numlockmask | LockMask, root, True, + GrabModeAsync, GrabModeAsync); + } +} + +void +procevent(void) { + XEvent ev; + + while(XPending(dpy)) { + XNextEvent(dpy, &ev); + if(handler[ev.type]) + (handler[ev.type])(&ev); /* call handler */ + } +}