garbeam@5: /* garbeam@5: * (C)opyright MMVI Anselm R. Garbe garbeam@5: * See LICENSE file for license details. garbeam@5: */ garbeam@76: #include "dwm.h" garbeam@5: garbeam@5: #include garbeam@5: #include garbeam@13: #include garbeam@5: arg@146: /* static */ arg@114: arg@114: typedef struct { arg@114: unsigned long mod; arg@114: KeySym keysym; arg@114: void (*func)(Arg *arg); arg@114: Arg arg; arg@114: } Key; arg@114: arg@146: CMDS arg@146: KEYS garbeam@75: arg@146: static unsigned int valid_mask = 255 & ~(NUMLOCKMASK | LockMask); garbeam@5: garbeam@77: static void garbeam@77: movemouse(Client *c) garbeam@77: { garbeam@77: int x1, y1, ocx, ocy, di; garbeam@77: unsigned int dui; garbeam@77: Window dummy; arg@123: XEvent ev; garbeam@77: arg@115: ocx = c->x; arg@115: ocy = c->y; arg@148: if(XGrabPointer(dpy, root, False, MOUSEMASK, GrabModeAsync, GrabModeAsync, arg@123: None, cursor[CurMove], CurrentTime) != GrabSuccess) garbeam@77: return; garbeam@77: XQueryPointer(dpy, root, &dummy, &dummy, &x1, &y1, &di, &di, &dui); garbeam@77: for(;;) { arg@148: XMaskEvent(dpy, MOUSEMASK | ExposureMask, &ev); garbeam@77: switch (ev.type) { garbeam@77: default: break; garbeam@77: case Expose: garbeam@77: handler[Expose](&ev); garbeam@77: break; garbeam@77: case MotionNotify: garbeam@79: XSync(dpy, False); arg@115: c->x = ocx + (ev.xmotion.x - x1); arg@115: c->y = ocy + (ev.xmotion.y - y1); arg@99: resize(c, False, TopLeft); garbeam@77: break; garbeam@77: case ButtonRelease: garbeam@77: XUngrabPointer(dpy, CurrentTime); garbeam@77: return; garbeam@77: } garbeam@77: } garbeam@77: } garbeam@77: garbeam@77: static void garbeam@77: resizemouse(Client *c) garbeam@77: { garbeam@77: int ocx, ocy; arg@99: Corner sticky; arg@123: XEvent ev; garbeam@77: arg@115: ocx = c->x; arg@115: ocy = c->y; arg@148: if(XGrabPointer(dpy, root, False, MOUSEMASK, GrabModeAsync, GrabModeAsync, garbeam@77: None, cursor[CurResize], CurrentTime) != GrabSuccess) garbeam@77: return; arg@115: XWarpPointer(dpy, None, c->win, 0, 0, 0, 0, c->w, c->h); garbeam@77: for(;;) { arg@148: XMaskEvent(dpy, MOUSEMASK | ExposureMask, &ev); garbeam@77: switch(ev.type) { garbeam@77: default: break; garbeam@77: case Expose: garbeam@77: handler[Expose](&ev); garbeam@77: break; garbeam@77: case MotionNotify: garbeam@79: XSync(dpy, False); arg@115: c->w = abs(ocx - ev.xmotion.x); arg@115: c->h = abs(ocy - ev.xmotion.y); arg@115: c->x = (ocx <= ev.xmotion.x) ? ocx : ocx - c->w; arg@115: c->y = (ocy <= ev.xmotion.y) ? ocy : ocy - c->h; arg@105: if(ocx <= ev.xmotion.x) arg@105: sticky = (ocy <= ev.xmotion.y) ? TopLeft : BotLeft; arg@105: else arg@105: sticky = (ocy <= ev.xmotion.y) ? TopRight : BotRight; arg@99: resize(c, True, sticky); garbeam@77: break; garbeam@77: case ButtonRelease: garbeam@77: XUngrabPointer(dpy, CurrentTime); garbeam@77: return; garbeam@77: } garbeam@77: } garbeam@77: } garbeam@73: garbeam@73: static void garbeam@18: buttonpress(XEvent *e) garbeam@18: { garbeam@73: int x; garbeam@73: Arg a; arg@123: Client *c; garbeam@18: XButtonPressedEvent *ev = &e->xbutton; garbeam@18: garbeam@73: if(barwin == ev->window) { garbeam@80: switch(ev->button) { garbeam@80: default: garbeam@80: x = 0; garbeam@80: for(a.i = 0; a.i < TLast; a.i++) { garbeam@80: x += textw(tags[a.i]); garbeam@80: if(ev->x < x) { garbeam@80: view(&a); garbeam@80: break; garbeam@80: } garbeam@73: } garbeam@80: break; garbeam@80: case Button4: garbeam@80: a.i = (tsel + 1 < TLast) ? tsel + 1 : 0; garbeam@80: view(&a); garbeam@80: break; garbeam@80: case Button5: garbeam@80: a.i = (tsel - 1 >= 0) ? tsel - 1 : TLast - 1; garbeam@80: view(&a); garbeam@80: break; garbeam@73: } garbeam@73: } garbeam@58: else if((c = getclient(ev->window))) { arg@143: focus(c); garbeam@18: switch(ev->button) { garbeam@18: default: garbeam@18: break; garbeam@18: case Button1: arg@133: if(!c->ismax && (arrange == dofloat || c->isfloat)) { arg@99: higher(c); arg@99: movemouse(c); arg@99: } garbeam@18: break; garbeam@18: case Button2: garbeam@26: lower(c); garbeam@18: break; garbeam@18: case Button3: arg@133: if(!c->ismax && (arrange == dofloat || c->isfloat)) { arg@99: higher(c); arg@99: resizemouse(c); arg@99: } garbeam@18: break; garbeam@18: } garbeam@18: } garbeam@18: } garbeam@18: garbeam@18: static void garbeam@5: configurerequest(XEvent *e) garbeam@5: { arg@123: Client *c; garbeam@5: XConfigureRequestEvent *ev = &e->xconfigurerequest; garbeam@5: XWindowChanges wc; garbeam@5: garbeam@5: ev->value_mask &= ~CWSibling; garbeam@18: if((c = getclient(ev->window))) { garbeam@29: gravitate(c, True); garbeam@5: if(ev->value_mask & CWX) arg@115: c->x = ev->x; garbeam@5: if(ev->value_mask & CWY) arg@115: c->y = ev->y; garbeam@5: if(ev->value_mask & CWWidth) arg@115: c->w = ev->width; garbeam@5: if(ev->value_mask & CWHeight) arg@115: c->h = ev->height; garbeam@29: if(ev->value_mask & CWBorderWidth) garbeam@55: c->border = 1; garbeam@29: gravitate(c, False); arg@99: resize(c, True, TopLeft); garbeam@5: } garbeam@5: garbeam@5: wc.x = ev->x; garbeam@5: wc.y = ev->y; garbeam@5: wc.width = ev->width; garbeam@5: wc.height = ev->height; garbeam@25: wc.border_width = 1; garbeam@5: wc.sibling = None; garbeam@5: wc.stack_mode = Above; garbeam@5: ev->value_mask &= ~CWStackMode; garbeam@5: ev->value_mask |= CWBorderWidth; garbeam@5: XConfigureWindow(dpy, ev->window, ev->value_mask, &wc); garbeam@79: XSync(dpy, False); garbeam@5: } garbeam@5: garbeam@5: static void garbeam@5: destroynotify(XEvent *e) garbeam@5: { garbeam@5: Client *c; garbeam@5: XDestroyWindowEvent *ev = &e->xdestroywindow; garbeam@5: garbeam@11: if((c = getclient(ev->window))) garbeam@11: unmanage(c); garbeam@5: } garbeam@5: garbeam@5: static void garbeam@5: enternotify(XEvent *e) garbeam@5: { arg@123: Client *c; garbeam@5: XCrossingEvent *ev = &e->xcrossing; garbeam@5: arg@143: if(ev->detail == NotifyInferior) garbeam@5: return; garbeam@5: garbeam@13: if((c = getclient(ev->window))) garbeam@13: focus(c); garbeam@26: else if(ev->window == root) garbeam@31: issel = True; garbeam@5: } garbeam@5: garbeam@5: static void garbeam@5: expose(XEvent *e) garbeam@5: { arg@123: Client *c; garbeam@5: XExposeEvent *ev = &e->xexpose; garbeam@5: garbeam@5: if(ev->count == 0) { garbeam@70: if(barwin == ev->window) garbeam@74: drawstatus(); garbeam@75: else if((c = getctitle(ev->window))) garbeam@74: drawtitle(c); garbeam@5: } garbeam@5: } garbeam@5: garbeam@5: static void garbeam@76: keypress(XEvent *e) garbeam@76: { arg@138: static unsigned int len = sizeof(key) / sizeof(key[0]); garbeam@76: unsigned int i; garbeam@76: KeySym keysym; arg@123: XKeyEvent *ev = &e->xkey; arg@145: ev->state &= valid_mask; garbeam@76: garbeam@76: keysym = XKeycodeToKeysym(dpy, (KeyCode)ev->keycode, 0); garbeam@76: for(i = 0; i < len; i++) arg@145: if((keysym == key[i].keysym) && ((key[i].mod & valid_mask) == ev->state)) { garbeam@76: if(key[i].func) garbeam@76: key[i].func(&key[i].arg); garbeam@76: return; garbeam@76: } garbeam@76: } garbeam@76: garbeam@76: static void garbeam@76: leavenotify(XEvent *e) garbeam@76: { garbeam@76: XCrossingEvent *ev = &e->xcrossing; garbeam@76: garbeam@76: if((ev->window == root) && !ev->same_screen) garbeam@76: issel = True; garbeam@76: } garbeam@76: garbeam@76: static void garbeam@5: maprequest(XEvent *e) garbeam@5: { arg@123: static XWindowAttributes wa; garbeam@5: XMapRequestEvent *ev = &e->xmaprequest; garbeam@5: garbeam@5: if(!XGetWindowAttributes(dpy, ev->window, &wa)) garbeam@5: return; garbeam@5: garbeam@5: if(wa.override_redirect) { garbeam@5: XSelectInput(dpy, ev->window, garbeam@5: (StructureNotifyMask | PropertyChangeMask)); garbeam@5: return; garbeam@5: } garbeam@5: garbeam@10: if(!getclient(ev->window)) garbeam@10: manage(ev->window, &wa); garbeam@5: } garbeam@5: garbeam@5: static void garbeam@5: propertynotify(XEvent *e) garbeam@5: { arg@123: Client *c; arg@123: Window trans; garbeam@5: XPropertyEvent *ev = &e->xproperty; garbeam@5: garbeam@5: if(ev->state == PropertyDelete) garbeam@5: return; /* ignore */ garbeam@5: garbeam@13: if((c = getclient(ev->window))) { garbeam@77: if(ev->atom == wmatom[WMProtocols]) { garbeam@75: c->proto = getproto(c->win); garbeam@30: return; garbeam@30: } garbeam@13: switch (ev->atom) { garbeam@13: default: break; garbeam@13: case XA_WM_TRANSIENT_FOR: garbeam@53: XGetTransientForHint(dpy, c->win, &trans); garbeam@80: if(!c->isfloat && (c->isfloat = (trans != 0))) garbeam@53: arrange(NULL); garbeam@13: break; garbeam@13: case XA_WM_NORMAL_HINTS: garbeam@74: setsize(c); garbeam@13: break; garbeam@13: } garbeam@77: if(ev->atom == XA_WM_NAME || ev->atom == netatom[NetWMName]) { garbeam@74: settitle(c); garbeam@74: drawtitle(c); garbeam@13: } garbeam@13: } garbeam@5: } garbeam@5: garbeam@5: static void garbeam@5: unmapnotify(XEvent *e) garbeam@5: { garbeam@5: Client *c; garbeam@5: XUnmapEvent *ev = &e->xunmap; garbeam@5: garbeam@10: if((c = getclient(ev->window))) garbeam@10: unmanage(c); garbeam@5: } garbeam@76: garbeam@84: /* extern */ garbeam@76: garbeam@76: void (*handler[LASTEvent]) (XEvent *) = { garbeam@76: [ButtonPress] = buttonpress, garbeam@76: [ConfigureRequest] = configurerequest, garbeam@76: [DestroyNotify] = destroynotify, garbeam@76: [EnterNotify] = enternotify, garbeam@76: [LeaveNotify] = leavenotify, garbeam@76: [Expose] = expose, garbeam@76: [KeyPress] = keypress, garbeam@76: [MapRequest] = maprequest, garbeam@76: [PropertyNotify] = propertynotify, garbeam@76: [UnmapNotify] = unmapnotify garbeam@76: }; garbeam@76: garbeam@76: void garbeam@76: grabkeys() garbeam@76: { arg@138: static unsigned int len = sizeof(key) / sizeof(key[0]); garbeam@76: unsigned int i; garbeam@76: KeyCode code; garbeam@76: garbeam@76: for(i = 0; i < len; i++) { garbeam@76: code = XKeysymToKeycode(dpy, key[i].keysym); garbeam@76: XUngrabKey(dpy, code, key[i].mod, root); arg@146: XUngrabKey(dpy, code, key[i].mod | NUMLOCKMASK, root); arg@146: XUngrabKey(dpy, code, key[i].mod | NUMLOCKMASK | LockMask, root); garbeam@76: XGrabKey(dpy, code, key[i].mod, root, True, garbeam@76: GrabModeAsync, GrabModeAsync); arg@146: XGrabKey(dpy, code, key[i].mod | NUMLOCKMASK, root, True, arg@146: GrabModeAsync, GrabModeAsync); arg@146: XGrabKey(dpy, code, key[i].mod | NUMLOCKMASK | LockMask, root, True, arg@146: GrabModeAsync, GrabModeAsync); garbeam@76: } garbeam@76: }