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: garbeam@73: #define ButtonMask (ButtonPressMask | ButtonReleaseMask) garbeam@73: #define MouseMask (ButtonMask | PointerMotionMask) garbeam@73: garbeam@75: /********** CUSTOMIZE **********/ garbeam@75: garbeam@75: const char *term[] = { garbeam@75: "urxvtc", "-tr", "+sb", "-bg", "black", "-fg", "white", "-fn", garbeam@75: "-*-terminus-medium-*-*-*-13-*-*-*-*-*-iso10646-*",NULL garbeam@75: }; garbeam@75: const char *browse[] = { "firefox", NULL }; garbeam@75: const char *xlock[] = { "xlock", NULL }; garbeam@75: garbeam@75: Key key[] = { garbeam@75: /* modifier key function arguments */ garbeam@75: { Mod1Mask, XK_Return, zoom, { 0 } }, garbeam@75: { Mod1Mask, XK_k, focusprev, { 0 } }, garbeam@75: { Mod1Mask, XK_j, focusnext, { 0 } }, garbeam@75: { Mod1Mask, XK_m, maximize, { 0 } }, garbeam@75: { Mod1Mask, XK_0, view, { .i = Tscratch } }, garbeam@75: { Mod1Mask, XK_1, view, { .i = Tdev } }, garbeam@75: { Mod1Mask, XK_2, view, { .i = Twww } }, garbeam@75: { Mod1Mask, XK_3, view, { .i = Twork } }, garbeam@75: { Mod1Mask, XK_space, dotile, { 0 } }, garbeam@75: { Mod1Mask|ShiftMask, XK_space, dofloat, { 0 } }, garbeam@75: { Mod1Mask|ShiftMask, XK_0, replacetag, { .i = Tscratch } }, garbeam@75: { Mod1Mask|ShiftMask, XK_1, replacetag, { .i = Tdev } }, garbeam@75: { Mod1Mask|ShiftMask, XK_2, replacetag, { .i = Twww } }, garbeam@75: { Mod1Mask|ShiftMask, XK_3, replacetag, { .i = Twork } }, garbeam@75: { Mod1Mask|ShiftMask, XK_c, killclient, { 0 } }, garbeam@75: { Mod1Mask|ShiftMask, XK_q, quit, { 0 } }, garbeam@75: { Mod1Mask|ShiftMask, XK_Return, spawn, { .argv = term } }, garbeam@75: { Mod1Mask|ShiftMask, XK_w, spawn, { .argv = browse } }, garbeam@75: { Mod1Mask|ShiftMask, XK_l, spawn, { .argv = xlock } }, garbeam@75: { ControlMask, XK_0, appendtag, { .i = Tscratch } }, garbeam@75: { ControlMask, XK_1, appendtag, { .i = Tdev } }, garbeam@75: { ControlMask, XK_2, appendtag, { .i = Twww } }, garbeam@75: { ControlMask, XK_3, appendtag, { .i = Twork } }, garbeam@75: }; garbeam@75: garbeam@75: /********** CUSTOMIZE **********/ garbeam@75: garbeam@76: /* static functions */ garbeam@5: garbeam@77: static void garbeam@77: movemouse(Client *c) garbeam@77: { garbeam@77: XEvent ev; garbeam@77: int x1, y1, ocx, ocy, di; garbeam@77: unsigned int dui; garbeam@77: Window dummy; garbeam@77: garbeam@77: ocx = c->x; garbeam@77: ocy = c->y; garbeam@77: if(XGrabPointer(dpy, root, False, MouseMask, GrabModeAsync, GrabModeAsync, garbeam@77: None, cursor[CurMove], CurrentTime) != GrabSuccess) garbeam@77: return; garbeam@77: XQueryPointer(dpy, root, &dummy, &dummy, &x1, &y1, &di, &di, &dui); garbeam@77: for(;;) { garbeam@77: 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@77: XFlush(dpy); garbeam@77: c->x = ocx + (ev.xmotion.x - x1); garbeam@77: c->y = ocy + (ev.xmotion.y - y1); garbeam@77: resize(c, False); 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: XEvent ev; garbeam@77: int ocx, ocy; garbeam@77: garbeam@77: ocx = c->x; garbeam@77: ocy = c->y; garbeam@77: if(XGrabPointer(dpy, root, False, MouseMask, GrabModeAsync, GrabModeAsync, garbeam@77: None, cursor[CurResize], CurrentTime) != GrabSuccess) garbeam@77: return; garbeam@77: XWarpPointer(dpy, None, c->win, 0, 0, 0, 0, c->w, c->h); garbeam@77: for(;;) { garbeam@77: 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@77: XFlush(dpy); garbeam@77: c->w = abs(ocx - ev.xmotion.x); garbeam@77: c->h = abs(ocy - ev.xmotion.y); garbeam@77: c->x = (ocx <= ev.xmotion.x) ? ocx : ocx - c->w; garbeam@77: c->y = (ocy <= ev.xmotion.y) ? ocy : ocy - c->h; garbeam@77: resize(c, True); 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; garbeam@18: XButtonPressedEvent *ev = &e->xbutton; garbeam@18: Client *c; garbeam@18: garbeam@73: if(barwin == ev->window) { garbeam@75: x = (arrange == dofloat) ? textw("~") : 0; garbeam@73: for(a.i = 0; a.i < TLast; a.i++) { garbeam@73: x += textw(tags[a.i]); garbeam@73: if(ev->x < x) { garbeam@73: view(&a); garbeam@73: break; garbeam@73: } garbeam@73: } garbeam@73: } garbeam@58: else if((c = getclient(ev->window))) { garbeam@75: if(arrange == dotile && !c->dofloat) garbeam@73: return; garbeam@74: higher(c); garbeam@18: switch(ev->button) { garbeam@18: default: garbeam@18: break; garbeam@18: case Button1: garbeam@75: movemouse(c); garbeam@18: break; garbeam@18: case Button2: garbeam@26: lower(c); garbeam@18: break; garbeam@18: case Button3: garbeam@75: resizemouse(c); garbeam@18: break; garbeam@18: } garbeam@18: } garbeam@18: } garbeam@18: garbeam@18: static void garbeam@5: configurerequest(XEvent *e) garbeam@5: { garbeam@5: XConfigureRequestEvent *ev = &e->xconfigurerequest; garbeam@5: XWindowChanges wc; garbeam@5: Client *c; 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) garbeam@20: c->x = ev->x; garbeam@5: if(ev->value_mask & CWY) garbeam@20: c->y = ev->y; garbeam@5: if(ev->value_mask & CWWidth) garbeam@20: c->w = ev->width; garbeam@5: if(ev->value_mask & CWHeight) garbeam@20: c->h = ev->height; garbeam@29: if(ev->value_mask & CWBorderWidth) garbeam@55: c->border = 1; garbeam@29: gravitate(c, False); garbeam@53: resize(c, True); 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@5: XFlush(dpy); 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: { garbeam@5: XCrossingEvent *ev = &e->xcrossing; garbeam@5: Client *c; garbeam@5: garbeam@5: if(ev->mode != NotifyNormal || 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: { garbeam@5: XExposeEvent *ev = &e->xexpose; garbeam@21: Client *c; 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: { garbeam@76: XKeyEvent *ev = &e->xkey; garbeam@76: static unsigned int len = key ? sizeof(key) / sizeof(key[0]) : 0; garbeam@76: unsigned int i; garbeam@76: KeySym keysym; garbeam@76: garbeam@76: keysym = XKeycodeToKeysym(dpy, (KeyCode)ev->keycode, 0); garbeam@76: for(i = 0; i < len; i++) garbeam@76: if((keysym == key[i].keysym) && (key[i].mod == 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: { garbeam@5: XMapRequestEvent *ev = &e->xmaprequest; garbeam@5: static XWindowAttributes wa; 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: { garbeam@5: XPropertyEvent *ev = &e->xproperty; garbeam@53: Window trans; garbeam@5: Client *c; 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@75: if(!c->dofloat && (c->dofloat = (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@76: /* extern functions */ 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: { garbeam@76: static unsigned int len = key ? sizeof(key) / sizeof(key[0]) : 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); garbeam@76: XGrabKey(dpy, code, key[i].mod, root, True, garbeam@76: GrabModeAsync, GrabModeAsync); garbeam@76: } garbeam@76: }