diff --git a/config.def.h b/config.def.h index 9efa774..17ee062 100644 --- a/config.def.h +++ b/config.def.h @@ -2,9 +2,17 @@ /* appearance */ static const unsigned int borderpx = 1; /* border pixel of windows */ +static const unsigned int gappx = 5; /* gaps between windows */ static const unsigned int snap = 32; /* snap pixel */ static const int showbar = 1; /* 0 means no bar */ static const int topbar = 1; /* 0 means bottom bar */ +/* Display modes of the tab bar: never shown, always shown, shown only in */ +/* monocle mode in the presence of several windows. */ +/* Modes after showtab_nmodes are disabled. */ +enum showtab_modes { showtab_never, showtab_auto, showtab_nmodes, showtab_always}; +static const int showtab = showtab_auto; /* Default tab bar show mode */ +static const int toptab = False; /* False means bottom tab bar */ + static const char *fonts[] = { "monospace:size=10" }; static const char dmenufont[] = "monospace:size=10"; static const char col_gray1[] = "#222222"; @@ -44,13 +52,21 @@ static const Layout layouts[] = { { "[M]", monocle }, }; +/* default layout per tag */ +/* 0 = tile, 1 = floating, 2 = monocle */ +static int def_layouts[1 + LENGTH(tags)] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; + /* key definitions */ #define MODKEY Mod1Mask +#define ALTMOD Mod4Mask #define TAGKEYS(KEY,TAG) \ { MODKEY, KEY, view, {.ui = 1 << TAG} }, \ { MODKEY|ControlMask, KEY, toggleview, {.ui = 1 << TAG} }, \ { MODKEY|ShiftMask, KEY, tag, {.ui = 1 << TAG} }, \ - { MODKEY|ControlMask|ShiftMask, KEY, toggletag, {.ui = 1 << TAG} }, + { MODKEY|ControlMask|ShiftMask, KEY, toggletag, {.ui = 1 << TAG} }, \ + { ALTMOD, KEY, focusnthmon, {.i = TAG } }, \ + { ALTMOD|ShiftMask, KEY, tagnthmon, {.i = TAG } }, +#define HOLDKEY 0 // replace 0 with the keysym to activate holdbar /* helper for spawning shell commands in the pre dwm-5.0 fashion */ #define SHCMD(cmd) { .v = (const char*[]){ "/bin/sh", "-c", cmd, NULL } } @@ -65,6 +81,7 @@ static const Key keys[] = { { MODKEY, XK_p, spawn, {.v = dmenucmd } }, { MODKEY|ShiftMask, XK_Return, spawn, {.v = termcmd } }, { MODKEY, XK_b, togglebar, {0} }, + { MODKEY, XK_w, tabmode, {-1} }, { MODKEY, XK_j, focusstack, {.i = +1 } }, { MODKEY, XK_k, focusstack, {.i = -1 } }, { MODKEY, XK_i, incnmaster, {.i = +1 } }, @@ -79,12 +96,21 @@ static const Key keys[] = { { MODKEY, XK_m, setlayout, {.v = &layouts[2]} }, { MODKEY, XK_space, setlayout, {0} }, { MODKEY|ShiftMask, XK_space, togglefloating, {0} }, + { MODKEY|ShiftMask, XK_f, togglefullscr, {0} }, { MODKEY, XK_0, view, {.ui = ~0 } }, { MODKEY|ShiftMask, XK_0, tag, {.ui = ~0 } }, { MODKEY, XK_comma, focusmon, {.i = -1 } }, { MODKEY, XK_period, focusmon, {.i = +1 } }, { MODKEY|ShiftMask, XK_comma, tagmon, {.i = -1 } }, { MODKEY|ShiftMask, XK_period, tagmon, {.i = +1 } }, + { MODKEY, XK_Right, viewnext, {0} }, + { MODKEY, XK_Left, viewprev, {0} }, + { MODKEY|ShiftMask, XK_Right, tagtonext, {0} }, + { MODKEY|ShiftMask, XK_Left, tagtoprev, {0} }, + { MODKEY|ShiftMask, XK_apostrophe, swapmon, {0} }, + { MODKEY, XK_minus, setgaps, {.i = -1 } }, + { MODKEY, XK_equal, setgaps, {.i = +1 } }, + { MODKEY|ShiftMask, XK_equal, setgaps, {.i = 0 } }, TAGKEYS( XK_1, 0) TAGKEYS( XK_2, 1) TAGKEYS( XK_3, 2) @@ -95,6 +121,7 @@ static const Key keys[] = { TAGKEYS( XK_8, 7) TAGKEYS( XK_9, 8) { MODKEY|ShiftMask, XK_q, quit, {0} }, + { 0, HOLDKEY, holdbar, {0} }, }; /* button definitions */ @@ -112,5 +139,32 @@ static const Button buttons[] = { { ClkTagBar, 0, Button3, toggleview, {0} }, { ClkTagBar, MODKEY, Button1, tag, {0} }, { ClkTagBar, MODKEY, Button3, toggletag, {0} }, + { ClkTabBar, 0, Button1, focuswin, {0} }, }; +static const char *dwmfifo = "/tmp/dwm.fifo"; +static Command commands[] = { + { "dmenu", spawn, {.v = dmenucmd} }, + { "term", spawn, {.v = termcmd} }, + { "quit", quit, {0} }, + { "togglebar", togglebar, {0} }, + { "focusstack", focusstack, .parse = parseplusminus }, + { "incnmaster", incnmaster, .parse = parseplusminus }, + { "setmfact", setmfact, .parse = parseplusminus }, + { "zoom", zoom, {0} }, + { "killclient", killclient, {0} }, + { "setlayout-tiled", setlayout, {.v = &layouts[0]} }, + { "setlayout-float", setlayout, {.v = &layouts[1]} }, + { "setlayout-mono", setlayout, {.v = &layouts[2]} }, + { "togglelayout", setlayout, {0} }, + { "togglefloating", togglefloating, {0} }, + { "viewwin", viewwin, .parse = parsexid }, + { "viewname", viewname, .parse = parsestr }, + { "viewall", view, {.ui = ~0} }, + { "focusmon", focusmon, .parse = parseplusminus }, + { "tagmon", tagmon, .parse = parseplusminus }, + { "view", view, .parse = parsetag }, + { "toggleview", toggleview, .parse = parsetag }, + { "tag", tag, .parse = parsetag }, + { "toggletag", toggletag, .parse = parsetag }, +}; diff --git a/config.mk b/config.mk index b469a2b..c57985c 100644 --- a/config.mk +++ b/config.mk @@ -23,7 +23,7 @@ FREETYPEINC = /usr/include/freetype2 # includes and libs INCS = -I${X11INC} -I${FREETYPEINC} -LIBS = -L${X11LIB} -lX11 ${XINERAMALIBS} ${FREETYPELIBS} +LIBS = -L${X11LIB} -lX11 ${XINERAMALIBS} ${FREETYPELIBS} -lXcursor # flags CPPFLAGS = -D_DEFAULT_SOURCE -D_BSD_SOURCE -D_XOPEN_SOURCE=700L -DVERSION=\"${VERSION}\" ${XINERAMAFLAGS} diff --git a/drw.c b/drw.c index c41e6af..80d2c2d 100644 --- a/drw.c +++ b/drw.c @@ -4,6 +4,7 @@ #include #include #include +#include #include "drw.h" #include "util.h" @@ -425,14 +426,14 @@ drw_font_getexts(Fnt *font, const char *text, unsigned int len, unsigned int *w, } Cur * -drw_cur_create(Drw *drw, int shape) +drw_cur_create(Drw *drw, const char *shape) { Cur *cur; if (!drw || !(cur = ecalloc(1, sizeof(Cur)))) return NULL; - cur->cursor = XCreateFontCursor(drw->dpy, shape); + cur->cursor = XcursorLibraryLoadCursor(drw->dpy, shape); return cur; } diff --git a/drw.h b/drw.h index 6471431..22295a3 100644 --- a/drw.h +++ b/drw.h @@ -43,7 +43,7 @@ void drw_clr_create(Drw *drw, Clr *dest, const char *clrname); Clr *drw_scm_create(Drw *drw, const char *clrnames[], size_t clrcount); /* Cursor abstraction */ -Cur *drw_cur_create(Drw *drw, int shape); +Cur *drw_cur_create(Drw *drw, const char *shape); void drw_cur_free(Drw *drw, Cur *cursor); /* Drawing context manipulation */ diff --git a/dwm.1 b/dwm.1 index ddc8321..5903e70 100644 --- a/dwm.1 +++ b/dwm.1 @@ -20,14 +20,22 @@ layout applied. Windows are grouped by tags. Each window can be tagged with one or multiple tags. Selecting certain tags displays all windows with these tags. .P -Each screen contains a small status bar which displays all available tags, the -layout, the title of the focused window, and the text read from the root window -name property, if the screen is focused. A floating window is indicated with an -empty square and a maximised floating window is indicated with a filled square -before the windows title. The selected tags are indicated with a different -color. The tags of the focused window are indicated with a filled square in the -top left corner. The tags which are applied to one or more windows are -indicated with an empty square in the top left corner. +Each screen contains two small status bars. +.P +One bar displays all available tags, the layout, the title of the focused +window, and the text read from the root window name property, if the screen is +focused. A floating window is indicated with an empty square and a maximised +floating window is indicated with a filled square before the windows title. The +selected tags are indicated with a different color. The tags of the focused +window are indicated with a filled square in the top left corner. The tags +which are applied to one or more windows are indicated with an empty square in +the top left corner. +.P +Another bar contains a tab for each window of the current view and allows +navigation between windows, especially in the monocle mode. The different +display modes of this bar are described under the Mod1\-w Keybord command +section. When a single tag is selected, this tag is indicated in the left corner +of the tab bar. .P dwm draws a small border around windows to indicate the focus state. .SH OPTIONS @@ -44,7 +52,8 @@ command. .TP .B Button1 click on a tag label to display all windows with that tag, click on the layout -label toggles between tiled and floating layout. +label toggles between tiled and floating layout, click on a window name in the +tab bar brings focus to that window. .TP .B Button3 click on a tag label adds/removes all windows with that tag to/from the view. @@ -110,12 +119,21 @@ Increase master area size. .B Mod1\-h Decrease master area size. .TP +.B Mod1\-w +Cycle over the tab bar display modes: never displayed, always displayed, +displayed only in monocle mode when the view contains more than one window (auto +mode). Some display modes can be disabled in the configuration, config.h. In +the default configuration only "never" and "auto" display modes are enabled. +.TP .B Mod1\-Return Zooms/cycles focused window to/from master area (tiled layouts only). .TP .B Mod1\-Shift\-c Close focused window. .TP +.B Mod1\-Shift\-f +Toggle fullscreen for focused window. +.TP .B Mod1\-Shift\-space Toggle focused window between tiled and floating state. .TP diff --git a/dwm.c b/dwm.c index 1443802..08d4517 100644 --- a/dwm.c +++ b/dwm.c @@ -21,6 +21,7 @@ * To understand everything else, start reading main(). */ #include +#include #include #include #include @@ -28,10 +29,12 @@ #include #include #include +#include +#include #include #include -#include #include +#include #include #include #include @@ -40,6 +43,7 @@ #include #endif /* XINERAMA */ #include +#include #include "drw.h" #include "util.h" @@ -63,7 +67,7 @@ enum { NetSupported, NetWMName, NetWMState, NetWMCheck, NetWMFullscreen, NetActiveWindow, NetWMWindowType, NetWMWindowTypeDialog, NetClientList, NetLast }; /* EWMH atoms */ enum { WMProtocols, WMDelete, WMState, WMTakeFocus, WMLast }; /* default atoms */ -enum { ClkTagBar, ClkLtSymbol, ClkStatusText, ClkWinTitle, +enum { ClkTagBar, ClkTabBar, ClkLtSymbol, ClkStatusText, ClkWinTitle, ClkClientWin, ClkRootWin, ClkLast }; /* clicks */ typedef union { @@ -110,25 +114,36 @@ typedef struct { void (*arrange)(Monitor *); } Layout; +#define MAXTABS 50 + +typedef struct Pertag Pertag; struct Monitor { char ltsymbol[16]; float mfact; int nmaster; int num; int by; /* bar geometry */ + int ty; /* tab bar geometry */ int mx, my, mw, mh; /* screen size */ int wx, wy, ww, wh; /* window area */ + int gappx; /* gaps between windows */ unsigned int seltags; unsigned int sellt; unsigned int tagset[2]; int showbar; + int showtab; int topbar; + int toptab; Client *clients; Client *sel; Client *stack; Monitor *next; Window barwin; + Window tabwin; + int ntabs; + int tab_widths[MAXTABS]; const Layout *lt[2]; + Pertag *pertag; }; typedef struct { @@ -140,9 +155,20 @@ typedef struct { int monitor; } Rule; +typedef struct { + int x, y, w, h; +} DwmLogo; + +typedef struct { + const char *name; + void (*func)(const Arg *arg); + const Arg arg; + int (*parse)(Arg *arg, const char *s, size_t len); +} Command; + /* function declarations */ static void applyrules(Client *c); -static int applysizehints(Client *c, int *x, int *y, int *w, int *h, int interact); +static int applysizehints(Client *c, int *x, int *y, int *w, int *h, int *bw, int interact); static void arrange(Monitor *m); static void arrangemon(Monitor *m); static void attach(Client *c); @@ -160,14 +186,20 @@ static void destroynotify(XEvent *e); static void detach(Client *c); static void detachstack(Client *c); static Monitor *dirtomon(int dir); +static void dispatchcmd(void); +static Monitor *numtomon(int num); static void drawbar(Monitor *m); static void drawbars(void); +static void drawtab(Monitor *m); +static void drawtabs(void); static void enternotify(XEvent *e); static void expose(XEvent *e); static void focus(Client *c); static void focusin(XEvent *e); static void focusmon(const Arg *arg); +static void focusnthmon(const Arg *arg); static void focusstack(const Arg *arg); +static void focuswin(const Arg* arg); static Atom getatomprop(Client *c, Atom prop); static int getrootptr(int *x, int *y); static long getstate(Window w); @@ -176,6 +208,7 @@ static void grabbuttons(Client *c, int focused); static void grabkeys(void); static void incnmaster(const Arg *arg); static void keypress(XEvent *e); +static void keyrelease(XEvent *e); static void killclient(const Arg *arg); static void manage(Window w, XWindowAttributes *wa); static void mappingnotify(XEvent *e); @@ -183,13 +216,15 @@ static void maprequest(XEvent *e); static void monocle(Monitor *m); static void motionnotify(XEvent *e); static void movemouse(const Arg *arg); +static unsigned int nexttag(void); static Client *nexttiled(Client *c); static void pop(Client *c); +static unsigned int prevtag(void); static void propertynotify(XEvent *e); static void quit(const Arg *arg); static Monitor *recttomon(int x, int y, int w, int h); -static void resize(Client *c, int x, int y, int w, int h, int interact); -static void resizeclient(Client *c, int x, int y, int w, int h); +static void resize(Client *c, int x, int y, int w, int h, int bw, int interact); +static void resizeclient(Client *c, int x, int y, int w, int h, int bw); static void resizemouse(const Arg *arg); static void restack(Monitor *m); static void run(void); @@ -199,23 +234,31 @@ static void sendmon(Client *c, Monitor *m); static void setclientstate(Client *c, long state); static void setfocus(Client *c); static void setfullscreen(Client *c, int fullscreen); +static void setgaps(const Arg *arg); static void setlayout(const Arg *arg); static void setmfact(const Arg *arg); static void setup(void); static void seturgent(Client *c, int urg); static void showhide(Client *c); static void spawn(const Arg *arg); +static void tabmode(const Arg *arg); static void tag(const Arg *arg); static void tagmon(const Arg *arg); +static void tagnthmon(const Arg *arg); +static void tagtonext(const Arg *arg); +static void tagtoprev(const Arg *arg); static void tile(Monitor *m); static void togglebar(const Arg *arg); +static void holdbar(const Arg *arg); static void togglefloating(const Arg *arg); +static void togglefullscr(const Arg *arg); static void toggletag(const Arg *arg); static void toggleview(const Arg *arg); static void unfocus(Client *c, int setfocus); static void unmanage(Client *c, int destroyed); static void unmapnotify(XEvent *e); static void updatebarpos(Monitor *m); +static void updateholdbarpos(Monitor *m); static void updatebars(void); static void updateclientlist(void); static int updategeom(void); @@ -226,12 +269,17 @@ static void updatetitle(Client *c); static void updatewindowtype(Client *c); static void updatewmhints(Client *c); static void view(const Arg *arg); +static void viewname(const Arg *arg); +static void viewwin(const Arg *arg); +static void viewnext(const Arg *arg); +static void viewprev(const Arg *arg); static Client *wintoclient(Window w); static Monitor *wintomon(Window w); static int xerror(Display *dpy, XErrorEvent *ee); static int xerrordummy(Display *dpy, XErrorEvent *ee); static int xerrorstart(Display *dpy, XErrorEvent *ee); static void zoom(const Arg *arg); +static void swapmon(const Arg *arg); /* variables */ static const char broken[] = "broken"; @@ -239,11 +287,14 @@ static char stext[256]; static int screen; static int sw, sh; /* X display screen geometry width, height */ static int bh; /* bar height */ +static int th = 0; /* tab bar geometry */ static int lrpad; /* sum of left and right padding for text */ +static int dwmlogowdth = 74; /* dwm logo width + left/right padding */ static int (*xerrorxlib)(Display *, XErrorEvent *); static unsigned int numlockmask = 0; static void (*handler[LASTEvent]) (XEvent *) = { [ButtonPress] = buttonpress, + [ButtonRelease] = keyrelease, [ClientMessage] = clientmessage, [ConfigureRequest] = configurerequest, [ConfigureNotify] = configurenotify, @@ -251,6 +302,7 @@ static void (*handler[LASTEvent]) (XEvent *) = { [EnterNotify] = enternotify, [Expose] = expose, [FocusIn] = focusin, + [KeyRelease] = keyrelease, [KeyPress] = keypress, [MappingNotify] = mappingnotify, [MapRequest] = maprequest, @@ -266,14 +318,120 @@ static Display *dpy; static Drw *drw; static Monitor *mons, *selmon; static Window root, wmcheckwin; +static int fifofd; + +static int parsetag(Arg *a, const char *s, size_t len); +static int parseplusminus(Arg *a, const char *s, size_t len); +static int parsexid(Arg *a, const char *s, size_t len); +static int parsestr(Arg *a, const char *s, size_t len); /* configuration, allows nested code to access above variables */ #include "config.h" +static int parsetag(Arg *a, const char *s, size_t len) +{ + char *end; + unsigned int rv = strtoul(s, &end, 10); + if (end == s) + a->ui = 0; + else if (rv > LENGTH(tags)) + return 0; + else if (rv == 0) + a->ui = ~0U; + else + a->ui = 1U << (rv - 1); + + return 1; +} + +static int parseplusminus(Arg *a, const char *s, size_t len) +{ + if (*s == '+') + a->i = +1; + else if (*s == '-') + a->i = -1; + else + return 0; + return 1; +} + +static int parsexid(Arg *a, const char *s, size_t len) +{ + char *end; + unsigned long long sv = strtoull(s, &end, 0); + + if (end == s) + return 0; + + a->v = (void *)(intptr_t)sv; + return 1; +} + +static int parsestr(Arg *a, const char *s, size_t len) +{ + while (*s == ' ' || *s == '\t') + s++; + a->v = s; + return 1; +} + +struct Pertag { + unsigned int curtag, prevtag; /* current and previous tag */ + int nmasters[LENGTH(tags) + 1]; /* number of windows in master area */ + float mfacts[LENGTH(tags) + 1]; /* mfacts per tag */ + unsigned int sellts[LENGTH(tags) + 1]; /* selected layouts */ + const Layout *ltidxs[LENGTH(tags) + 1][2]; /* matrix of tags and layouts indexes */ + /* int showbars[LENGTH(tags) + 1]; */ /* disabled for holdbar compat */ +}; + /* compile-time check if all tags fit into an unsigned int bit array. */ struct NumTags { char limitexceeded[LENGTH(tags) > 31 ? -1 : 1]; }; /* function implementations */ +void +holdbar(const Arg *arg) +{ + if (selmon->showbar) + return; + selmon->showbar = 2; + updateholdbarpos(selmon); + XMoveResizeWindow(dpy, selmon->barwin, selmon->wx, selmon->by, selmon->ww, bh); +} + +void +keyrelease(XEvent *e) +{ + if (XEventsQueued(dpy, QueuedAfterReading)) { + XEvent ne; + XPeekEvent(dpy, &ne); + + if (ne.type == KeyPress && ne.xkey.time == e->xkey.time && + ne.xkey.keycode == e->xkey.keycode) { + XNextEvent(dpy, &ne); + return; + } + } + if (e->xkey.keycode == XKeysymToKeycode(dpy, HOLDKEY) && selmon->showbar == 2) { + selmon->showbar = 0; + updateholdbarpos(selmon); + XMoveResizeWindow(dpy, selmon->barwin, selmon->wx, selmon->by, selmon->ww, bh); + arrange(selmon); + } +} + +void +updateholdbarpos(Monitor *m) +{ + m->wy = m->my; + m->wh = m->mh; + if (m->showbar) { + m->by = m->topbar ? m->wy : m->wy + m->wh - bh; + m->wy = m->topbar ? m->wy - bh + bh : m->wy; + } else { + m->by = -bh; + } +} + void applyrules(Client *c) { @@ -311,7 +469,7 @@ applyrules(Client *c) } int -applysizehints(Client *c, int *x, int *y, int *w, int *h, int interact) +applysizehints(Client *c, int *x, int *y, int *w, int *h, int *bw, int interact) { int baseismin; Monitor *m = c->mon; @@ -324,18 +482,18 @@ applysizehints(Client *c, int *x, int *y, int *w, int *h, int interact) *x = sw - WIDTH(c); if (*y > sh) *y = sh - HEIGHT(c); - if (*x + *w + 2 * c->bw < 0) + if (*x + *w + 2 * *bw < 0) *x = 0; - if (*y + *h + 2 * c->bw < 0) + if (*y + *h + 2 * *bw < 0) *y = 0; } else { if (*x >= m->wx + m->ww) *x = m->wx + m->ww - WIDTH(c); if (*y >= m->wy + m->wh) *y = m->wy + m->wh - HEIGHT(c); - if (*x + *w + 2 * c->bw <= m->wx) + if (*x + *w + 2 * *bw <= m->wx) *x = m->wx; - if (*y + *h + 2 * c->bw <= m->wy) + if (*y + *h + 2 * *bw <= m->wy) *y = m->wy; } if (*h < bh) @@ -375,7 +533,7 @@ applysizehints(Client *c, int *x, int *y, int *w, int *h, int interact) if (c->maxh) *h = MIN(*h, c->maxh); } - return *x != c->x || *y != c->y || *w != c->w || *h != c->h; + return *x != c->x || *y != c->y || *w != c->w || *h != c->h || *bw != c->bw; } void @@ -393,11 +551,19 @@ arrange(Monitor *m) } void -arrangemon(Monitor *m) -{ +arrangemon(Monitor *m) { + Client *c; + + updatebarpos(m); + XMoveResizeWindow(dpy, m->tabwin, m->wx, m->ty, m->ww, th); strncpy(m->ltsymbol, m->lt[m->sellt]->symbol, sizeof m->ltsymbol); if (m->lt[m->sellt]->arrange) m->lt[m->sellt]->arrange(m); + else + /* <>< floating layout; add borders */ + for (c = selmon->clients; c; c = c->next) + if (ISVISIBLE(c) && c->bw == 0) + resize(c, c->x, c->y, c->w - 2*borderpx, c->h - 2*borderpx, borderpx, 0); } void @@ -432,9 +598,16 @@ buttonpress(XEvent *e) } if (ev->window == selmon->barwin) { i = x = 0; - do + x = dwmlogowdth; /* dwm logo width */ + unsigned int occ = 0; + for(c = m->clients; c; c=c->next) + occ |= c->tags == TAGMASK ? 0 : c->tags; + do { + /* Do not reserve space for vacant tags */ + if (!(occ & 1 << i || m->tagset[m->seltags] & 1 << i)) + continue; x += TEXTW(tags[i]); - while (ev->x >= x && ++i < LENGTH(tags)); + } while (ev->x >= x && ++i < LENGTH(tags)); if (i < LENGTH(tags)) { click = ClkTagBar; arg.ui = 1 << i; @@ -444,7 +617,24 @@ buttonpress(XEvent *e) click = ClkStatusText; else click = ClkWinTitle; - } else if ((c = wintoclient(ev->window))) { + } + if(ev->window == selmon->tabwin) { + i = 0; x = 0; + for(c = selmon->clients; c; c = c->next){ + if(!ISVISIBLE(c)) continue; + x += selmon->tab_widths[i]; + if (ev->x > x) + ++i; + else + break; + if(i >= m->ntabs) break; + } + if(c) { + click = ClkTabBar; + arg.ui = i; + } + } + else if((c = wintoclient(ev->window))) { focus(c); restack(selmon); XAllowEvents(dpy, ReplayPointer, CurrentTime); @@ -452,8 +642,9 @@ buttonpress(XEvent *e) } for (i = 0; i < LENGTH(buttons); i++) if (click == buttons[i].click && buttons[i].func && buttons[i].button == ev->button - && CLEANMASK(buttons[i].mask) == CLEANMASK(ev->state)) - buttons[i].func(click == ClkTagBar && buttons[i].arg.i == 0 ? &arg : &buttons[i].arg); + && CLEANMASK(buttons[i].mask) == CLEANMASK(ev->state)){ + buttons[i].func(((click == ClkTagBar || click == ClkTabBar) && buttons[i].arg.i == 0) ? &arg : &buttons[i].arg); + } } void @@ -493,6 +684,7 @@ cleanup(void) XSync(dpy, False); XSetInputFocus(dpy, PointerRoot, RevertToPointerRoot, CurrentTime); XDeleteProperty(dpy, root, netatom[NetActiveWindow]); + close(fifofd); } void @@ -508,6 +700,8 @@ cleanupmon(Monitor *mon) } XUnmapWindow(dpy, mon->barwin); XDestroyWindow(dpy, mon->barwin); + XUnmapWindow(dpy, mon->tabwin); + XDestroyWindow(dpy, mon->tabwin); free(mon); } @@ -516,6 +710,7 @@ clientmessage(XEvent *e) { XClientMessageEvent *cme = &e->xclient; Client *c = wintoclient(cme->window); + unsigned int i; if (!c) return; @@ -525,8 +720,15 @@ clientmessage(XEvent *e) setfullscreen(c, (cme->data.l[0] == 1 /* _NET_WM_STATE_ADD */ || (cme->data.l[0] == 2 /* _NET_WM_STATE_TOGGLE */ && !c->isfullscreen))); } else if (cme->message_type == netatom[NetActiveWindow]) { - if (c != selmon->sel && !c->isurgent) - seturgent(c, 1); + for (i = 0; i < LENGTH(tags) && !((1 << i) & c->tags); i++); + if (i < LENGTH(tags)) { + const Arg a = {.ui = 1 << i}; + selmon = c->mon; + view(&a); + focus(c); + XWarpPointer(dpy, None, c->win, 0, 0, 0, 0, c->w/2, c->h/2); + restack(selmon); + } } } @@ -568,7 +770,7 @@ configurenotify(XEvent *e) for (m = mons; m; m = m->next) { for (c = m->clients; c; c = c->next) if (c->isfullscreen) - resizeclient(c, m->mx, m->my, m->mw, m->mh); + resizeclient(c, m->mx, m->my, m->mw, m->mh, 0); XMoveResizeWindow(dpy, m->barwin, m->wx, m->by, m->ww, bh); } focus(NULL); @@ -633,16 +835,35 @@ Monitor * createmon(void) { Monitor *m; + unsigned int i; m = ecalloc(1, sizeof(Monitor)); m->tagset[0] = m->tagset[1] = 1; m->mfact = mfact; m->nmaster = nmaster; m->showbar = showbar; + m->showtab = showtab; m->topbar = topbar; + m->gappx = gappx; + m->toptab = toptab; + m->ntabs = 0; m->lt[0] = &layouts[0]; m->lt[1] = &layouts[1 % LENGTH(layouts)]; strncpy(m->ltsymbol, layouts[0].symbol, sizeof m->ltsymbol); + m->pertag = ecalloc(1, sizeof(Pertag)); + m->pertag->curtag = m->pertag->prevtag = 1; + + for (i = 0; i <= LENGTH(tags); i++) { + m->pertag->nmasters[i] = m->nmaster; + m->pertag->mfacts[i] = m->mfact; + + m->pertag->ltidxs[i][0] = &layouts[def_layouts[i % LENGTH(def_layouts)] % LENGTH(layouts)]; + m->pertag->ltidxs[i][1] = m->lt[1]; + m->pertag->sellts[i] = m->sellt; + + /* m->pertag->showbars[i] = m->showbar; */ + } + return m; } @@ -694,10 +915,88 @@ dirtomon(int dir) return m; } +Monitor * +numtomon(int num) +{ + Monitor *m = NULL; + int i = 0; + + for(m = mons, i=0; m->next && i < num; m = m->next){ + i++; + } + return m; +} + +static const char * +strnprefix(const char *haystack, size_t hlen, const char *needle) +{ + while (*needle && hlen--) { + if (*haystack++ != *needle++) + return 0; + } + + if (*needle) + return NULL; + return haystack; +} + +void +dispatchcmd(void) +{ + static char buf[BUFSIZ]; + static char * const bend = 1[&buf]; + static char *bw = buf; + static int longline = 0; + ssize_t n; + char *nl; + char *pl = buf; + char *dend; + Command *i; + + n = read(fifofd, bw, bend - bw); + if (n == -1) + die("Failed to read() from DWM fifo %s:", dwmfifo); + dend = bw + n; + + if (longline) { + if (!(nl = memchr(bw, '\n', dend - bw))) + return; + bw = pl = nl + 1; + longline = 0; + } + + while ((nl = memchr(bw, '\n', dend - bw))) { + for (i = commands; i < 1[&commands]; i++) { + const char *arg; + + if (!(arg = strnprefix(pl, nl - pl, i->name))) + continue; + *nl = '\0'; + if (i->parse) { + Arg a; + if (i->parse(&a, arg, nl - arg)) + i->func(&a); + } else { + i->func(&i->arg); + } + + break; + } + bw = pl = nl + 1; + } + + memmove(buf, pl, dend - pl); + bw = dend - pl + buf; + + if (bw == bend) + bw = buf; +} + void drawbar(Monitor *m) { int x, w, tw = 0; + int stroke = 4, letterHeight = bh - 4; int boxs = drw->fonts->h / 9; int boxw = drw->fonts->h / 6 + 2; unsigned int i, occ = 0, urg = 0; @@ -714,19 +1013,43 @@ drawbar(Monitor *m) } for (c = m->clients; c; c = c->next) { - occ |= c->tags; + occ |= c->tags == TAGMASK ? 0 : c->tags; if (c->isurgent) urg |= c->tags; } - x = 0; + + /* use colored scheme for visibility */ + drw_setscheme(drw, scheme[SchemeNorm]); + + /* draw dark background for logo */ + drw_rect(drw, 0, 0, dwmlogowdth, bh, 1, 1); + + /* draw dwm logo */ + const DwmLogo dwmLogo[] = { + { 12, 9, stroke, letterHeight / 2 }, /* d: left vertical */ + { 12, 15, 35, stroke }, /* d: bottom horizontal */ + { 25, 1, stroke, letterHeight }, /* d: right vertical */ + { 12, 7, 15, stroke }, /* d: top horizontal */ + { 34, 7, stroke, letterHeight / 2 }, /* w: center vertical */ + { 43, 7, stroke, letterHeight / 2 }, /* w: right vertical */ + { 43, 7, 22, stroke }, /* m: top horizontal */ + { 52, 11, stroke, letterHeight / 2 }, /* m: center vertical */ + { 61, 11, stroke, letterHeight / 2 } /* m: right vertical */ + }; + + for (int i = 0; i < LENGTH(dwmLogo); i++) { + drw_rect(drw, dwmLogo[i].x, dwmLogo[i].y, dwmLogo[i].w, dwmLogo[i].h, 1, 0); + } + + /* start drawing tags after logo */ + x = dwmlogowdth; for (i = 0; i < LENGTH(tags); i++) { + /* Do not draw vacant tags */ + if(!(occ & 1 << i || m->tagset[m->seltags] & 1 << i)) + continue; w = TEXTW(tags[i]); drw_setscheme(drw, scheme[m->tagset[m->seltags] & 1 << i ? SchemeSel : SchemeNorm]); drw_text(drw, x, 0, w, bh, lrpad / 2, tags[i], urg & 1 << i); - if (occ & 1 << i) - drw_rect(drw, x + boxs, boxs, boxw, boxw, - m == selmon && selmon->sel && selmon->sel->tags & 1 << i, - urg & 1 << i); x += w; } w = TEXTW(m->ltsymbol); @@ -735,7 +1058,7 @@ drawbar(Monitor *m) if ((w = m->ww - tw - x) > bh) { if (m->sel) { - drw_setscheme(drw, scheme[m == selmon ? SchemeSel : SchemeNorm]); + drw_setscheme(drw, scheme[SchemeNorm]); drw_text(drw, x, 0, w, bh, lrpad / 2, m->sel->name, 0); if (m->sel->isfloating) drw_rect(drw, x + boxs, boxs, boxw, boxw, m->sel->isfixed, 0); @@ -756,6 +1079,105 @@ drawbars(void) drawbar(m); } +void +drawtabs(void) { + Monitor *m; + + for(m = mons; m; m = m->next) + drawtab(m); +} + +static int +cmpint(const void *p1, const void *p2) { + /* The actual arguments to this function are "pointers to + pointers to char", but strcmp(3) arguments are "pointers + to char", hence the following cast plus dereference */ + return *((int*) p1) > * (int*) p2; +} + + +void +drawtab(Monitor *m) { + Client *c; + int i; + int itag = -1; + char view_info[50]; + int view_info_w = 0; + int sorted_label_widths[MAXTABS]; + int tot_width; + int maxsize = bh; + int x = 0; + int w = 0; + + //view_info: indicate the tag which is displayed in the view + for(i = 0; i < LENGTH(tags); ++i){ + if((selmon->tagset[selmon->seltags] >> i) & 1) { + if(itag >=0){ //more than one tag selected + itag = -1; + break; + } + itag = i; + } + } + + if(0 <= itag && itag < LENGTH(tags)){ + snprintf(view_info, sizeof view_info, "[%s]", tags[itag]); + } else { + strncpy(view_info, "[...]", sizeof view_info); + } + view_info[sizeof(view_info) - 1 ] = 0; + view_info_w = TEXTW(view_info); + tot_width = view_info_w; + + /* Calculates number of labels and their width */ + m->ntabs = 0; + for(c = m->clients; c; c = c->next){ + if(!ISVISIBLE(c)) continue; + m->tab_widths[m->ntabs] = TEXTW(c->name); + tot_width += m->tab_widths[m->ntabs]; + ++m->ntabs; + if(m->ntabs >= MAXTABS) break; + } + + if(tot_width > m->ww){ //not enough space to display the labels, they need to be truncated + memcpy(sorted_label_widths, m->tab_widths, sizeof(int) * m->ntabs); + qsort(sorted_label_widths, m->ntabs, sizeof(int), cmpint); + tot_width = view_info_w; + for(i = 0; i < m->ntabs; ++i){ + if(tot_width + (m->ntabs - i) * sorted_label_widths[i] > m->ww) + break; + tot_width += sorted_label_widths[i]; + } + maxsize = (m->ww - tot_width) / (m->ntabs - i); + } else{ + maxsize = m->ww; + } + i = 0; + for(c = m->clients; c; c = c->next){ + if(!ISVISIBLE(c)) continue; + if(i >= m->ntabs) break; + if(m->tab_widths[i] > maxsize) m->tab_widths[i] = maxsize; + w = m->tab_widths[i]; + drw_setscheme(drw, scheme[(c == m->sel) ? SchemeSel : SchemeNorm]); + drw_text(drw, x, 0, w, th, 0, c->name, 0); + x += w; + ++i; + } + + drw_setscheme(drw, scheme[SchemeNorm]); + + /* cleans interspace between window names and current viewed tag label */ + w = m->ww - view_info_w - x; + drw_text(drw, x, 0, w, th, 0, "", 0); + + /* view info */ + x += w; + w = view_info_w; + drw_text(drw, x, 0, w, th, 0, view_info, 0); + + drw_map(drw, m->tabwin, 0, 0, m->ww, th); +} + void enternotify(XEvent *e) { @@ -781,8 +1203,10 @@ expose(XEvent *e) Monitor *m; XExposeEvent *ev = &e->xexpose; - if (ev->count == 0 && (m = wintomon(ev->window))) + if(ev->count == 0 && (m = wintomon(ev->window))){ drawbar(m); + drawtab(m); + } } void @@ -808,6 +1232,7 @@ focus(Client *c) } selmon->sel = c; drawbars(); + drawtabs(); } /* there are some broken focus acquiring clients needing extra handling */ @@ -830,6 +1255,25 @@ focusmon(const Arg *arg) if ((m = dirtomon(arg->i)) == selmon) return; unfocus(selmon->sel, 0); + XWarpPointer(dpy, None, m->barwin, 0, 0, 0, 0, m->mw / 2, m->mh / 2); + selmon = m; + focus(NULL); + if (selmon->sel) + XWarpPointer(dpy, None, selmon->sel->win, 0, 0, 0, 0, selmon->sel->w/2, selmon->sel->h/2); +} + +void +focusnthmon(const Arg *arg) +{ + Monitor *m; + + if (!mons->next) + return; + + if ((m = numtomon(arg->i)) == selmon) + return; + unfocus(selmon->sel, 0); + XWarpPointer(dpy, None, m->barwin, 0, 0, 0, 0, m->mw / 2, m->mh / 2); selmon = m; focus(NULL); } @@ -857,9 +1301,23 @@ focusstack(const Arg *arg) if (c) { focus(c); restack(selmon); + XWarpPointer(dpy, None, c->win, 0, 0, 0, 0, c->w/2, c->h/2); } } +void +focuswin(const Arg* arg){ + int iwin = arg->i; + Client* c = NULL; + for(c = selmon->clients; c && (iwin || !ISVISIBLE(c)) ; c = c->next){ + if(ISVISIBLE(c)) --iwin; + }; + if(c) { + focus(c); + restack(selmon); + } +} + Atom getatomprop(Client *c, Atom prop) { @@ -979,7 +1437,7 @@ grabkeys(void) void incnmaster(const Arg *arg) { - selmon->nmaster = MAX(selmon->nmaster + arg->i, 0); + selmon->nmaster = selmon->pertag->nmasters[selmon->pertag->curtag] = MAX(selmon->nmaster + arg->i, 0); arrange(selmon); } @@ -1067,6 +1525,8 @@ manage(Window w, XWindowAttributes *wa) updatewindowtype(c); updatesizehints(c); updatewmhints(c); + c->x = c->mon->mx + (c->mon->mw - WIDTH(c)) / 2; + c->y = c->mon->my + (c->mon->mh - HEIGHT(c)) / 2; XSelectInput(dpy, w, EnterWindowMask|FocusChangeMask|PropertyChangeMask|StructureNotifyMask); grabbuttons(c, 0); if (!c->isfloating) @@ -1084,6 +1544,8 @@ manage(Window w, XWindowAttributes *wa) c->mon->sel = c; arrange(c->mon); XMapWindow(dpy, c->win); + if (c && c->mon == selmon) + XWarpPointer(dpy, None, c->win, 0, 0, 0, 0, c->w/2, c->h/2); focus(NULL); } @@ -1121,7 +1583,7 @@ monocle(Monitor *m) if (n > 0) /* override layout symbol */ snprintf(m->ltsymbol, sizeof m->ltsymbol, "[%d]", n); for (c = nexttiled(m->clients); c; c = nexttiled(c->next)) - resize(c, m->wx, m->wy, m->ww - 2 * c->bw, m->wh - 2 * c->bw, 0); + resize(c, m->wx, m->wy, m->ww, m->wh, 0, 0); } void @@ -1189,7 +1651,7 @@ movemouse(const Arg *arg) && (abs(nx - c->x) > snap || abs(ny - c->y) > snap)) togglefloating(NULL); if (!selmon->lt[selmon->sellt]->arrange || c->isfloating) - resize(c, nx, ny, c->w, c->h, 1); + resize(c, nx, ny, c->w, c->h, c->bw, 1); break; } } while (ev.type != ButtonRelease); @@ -1201,6 +1663,29 @@ movemouse(const Arg *arg) } } +unsigned int +nexttag(void) +{ + unsigned int seltag = selmon->tagset[selmon->seltags]; + unsigned int usedtags = 0; + Client *c = selmon->clients; + + if (!c) + return seltag; + + /* skip vacant tags */ + do { + usedtags |= c->tags; + c = c->next; + } while (c); + + do { + seltag = seltag == (1 << (LENGTH(tags) - 1)) ? 1 : seltag << 1; + } while (!(seltag & usedtags)); + + return seltag; +} + Client * nexttiled(Client *c) { @@ -1217,6 +1702,28 @@ pop(Client *c) arrange(c->mon); } +unsigned int +prevtag(void) +{ + unsigned int seltag = selmon->tagset[selmon->seltags]; + unsigned int usedtags = 0; + Client *c = selmon->clients; + if (!c) + return seltag; + + /* skip vacant tags */ + do { + usedtags |= c->tags; + c = c->next; + } while (c); + + do { + seltag = seltag == 1 ? (1 << (LENGTH(tags) - 1)) : seltag >> 1; + } while (!(seltag & usedtags)); + + return seltag; +} + void propertynotify(XEvent *e) { @@ -1242,12 +1749,14 @@ propertynotify(XEvent *e) case XA_WM_HINTS: updatewmhints(c); drawbars(); + drawtabs(); break; } if (ev->atom == XA_WM_NAME || ev->atom == netatom[NetWMName]) { updatetitle(c); if (c == c->mon->sel) drawbar(c->mon); + drawtab(c->mon); } if (ev->atom == netatom[NetWMWindowType]) updatewindowtype(c); @@ -1275,14 +1784,14 @@ recttomon(int x, int y, int w, int h) } void -resize(Client *c, int x, int y, int w, int h, int interact) +resize(Client *c, int x, int y, int w, int h, int bw, int interact) { - if (applysizehints(c, &x, &y, &w, &h, interact)) - resizeclient(c, x, y, w, h); + if (applysizehints(c, &x, &y, &w, &h, &bw, interact)) + resizeclient(c, x, y, w, h, bw); } void -resizeclient(Client *c, int x, int y, int w, int h) +resizeclient(Client *c, int x, int y, int w, int h, int bw) { XWindowChanges wc; @@ -1290,7 +1799,7 @@ resizeclient(Client *c, int x, int y, int w, int h) c->oldy = c->y; c->y = wc.y = y; c->oldw = c->w; c->w = wc.width = w; c->oldh = c->h; c->h = wc.height = h; - wc.border_width = c->bw; + c->oldbw = c->bw; c->bw = wc.border_width = bw; XConfigureWindow(dpy, c->win, CWX|CWY|CWWidth|CWHeight|CWBorderWidth, &wc); configure(c); XSync(dpy, False); @@ -1339,7 +1848,7 @@ resizemouse(const Arg *arg) togglefloating(NULL); } if (!selmon->lt[selmon->sellt]->arrange || c->isfloating) - resize(c, c->x, c->y, nw, nh, 1); + resize(c, c->x, c->y, nw, nh, c->bw, 1); break; } } while (ev.type != ButtonRelease); @@ -1361,6 +1870,7 @@ restack(Monitor *m) XWindowChanges wc; drawbar(m); + drawtab(m); if (!m->sel) return; if (m->sel->isfloating || !m->lt[m->sellt]->arrange) @@ -1378,15 +1888,31 @@ restack(Monitor *m) while (XCheckMaskEvent(dpy, EnterWindowMask, &ev)); } +static Bool evpredicate() +{ + return True; +} + void run(void) { XEvent ev; + struct pollfd fds[2] = { + { .events = POLLIN }, + { .fd = fifofd, .events = POLLIN } + }; /* main event loop */ XSync(dpy, False); - while (running && !XNextEvent(dpy, &ev)) - if (handler[ev.type]) - handler[ev.type](&ev); /* call handler */ + fds[0].fd = ConnectionNumber(dpy); + while (running) { + (void)poll(fds, 1[&fds] - fds, -1); + if (fds[1].revents & POLLIN) + dispatchcmd(); + if (fds[0].revents & POLLIN) + while (XCheckIfEvent(dpy, &ev, evpredicate, NULL)) + if (handler[ev.type]) + handler[ev.type](&ev); /* call handler */ + } } void @@ -1486,33 +2012,41 @@ setfullscreen(Client *c, int fullscreen) PropModeReplace, (unsigned char*)&netatom[NetWMFullscreen], 1); c->isfullscreen = 1; c->oldstate = c->isfloating; - c->oldbw = c->bw; - c->bw = 0; c->isfloating = 1; - resizeclient(c, c->mon->mx, c->mon->my, c->mon->mw, c->mon->mh); + resizeclient(c, c->mon->mx, c->mon->my, c->mon->mw, c->mon->mh, 0); XRaiseWindow(dpy, c->win); } else if (!fullscreen && c->isfullscreen){ XChangeProperty(dpy, c->win, netatom[NetWMState], XA_ATOM, 32, PropModeReplace, (unsigned char*)0, 0); c->isfullscreen = 0; c->isfloating = c->oldstate; - c->bw = c->oldbw; c->x = c->oldx; c->y = c->oldy; c->w = c->oldw; c->h = c->oldh; - resizeclient(c, c->x, c->y, c->w, c->h); + c->bw = c->oldbw; + resizeclient(c, c->x, c->y, c->w, c->h, c->bw); arrange(c->mon); } } +void +setgaps(const Arg *arg) +{ + if ((arg->i == 0) || (selmon->gappx + arg->i < 0)) + selmon->gappx = 0; + else + selmon->gappx += arg->i; + arrange(selmon); +} + void setlayout(const Arg *arg) { if (!arg || !arg->v || arg->v != selmon->lt[selmon->sellt]) - selmon->sellt ^= 1; + selmon->sellt = selmon->pertag->sellts[selmon->pertag->curtag] ^= 1; if (arg && arg->v) - selmon->lt[selmon->sellt] = (Layout *)arg->v; + selmon->lt[selmon->sellt] = selmon->pertag->ltidxs[selmon->pertag->curtag][selmon->sellt] = (Layout *)arg->v; strncpy(selmon->ltsymbol, selmon->lt[selmon->sellt]->symbol, sizeof selmon->ltsymbol); if (selmon->sel) arrange(selmon); @@ -1531,7 +2065,7 @@ setmfact(const Arg *arg) f = arg->f < 1.0 ? arg->f + selmon->mfact : arg->f - 1.0; if (f < 0.05 || f > 0.95) return; - selmon->mfact = f; + selmon->mfact = selmon->pertag->mfacts[selmon->pertag->curtag] = f; arrange(selmon); } @@ -1562,6 +2096,7 @@ setup(void) die("no fonts could be loaded."); lrpad = drw->fonts->h; bh = drw->fonts->h + 2; + th = bh; updategeom(); /* init atoms */ utf8string = XInternAtom(dpy, "UTF8_STRING", False); @@ -1579,9 +2114,9 @@ setup(void) netatom[NetWMWindowTypeDialog] = XInternAtom(dpy, "_NET_WM_WINDOW_TYPE_DIALOG", False); netatom[NetClientList] = XInternAtom(dpy, "_NET_CLIENT_LIST", False); /* init cursors */ - cursor[CurNormal] = drw_cur_create(drw, XC_left_ptr); - cursor[CurResize] = drw_cur_create(drw, XC_sizing); - cursor[CurMove] = drw_cur_create(drw, XC_fleur); + cursor[CurNormal] = drw_cur_create(drw, "left_ptr"); + cursor[CurResize] = drw_cur_create(drw, "se-resize"); + cursor[CurMove] = drw_cur_create(drw, "fleur"); /* init appearance */ scheme = ecalloc(LENGTH(colors), sizeof(Clr *)); for (i = 0; i < LENGTH(colors); i++) @@ -1610,6 +2145,9 @@ setup(void) XSelectInput(dpy, root, wa.event_mask); grabkeys(); focus(NULL); + fifofd = open(dwmfifo, O_RDWR | O_CLOEXEC | O_NONBLOCK); + if (fifofd < 0) + die("Failed to open() DWM fifo %s:", dwmfifo); } void @@ -1634,7 +2172,7 @@ showhide(Client *c) /* show clients top down */ XMoveWindow(dpy, c->win, c->x, c->y); if ((!c->mon->lt[c->mon->sellt]->arrange || c->isfloating) && !c->isfullscreen) - resize(c, c->x, c->y, c->w, c->h, 0); + resize(c, c->x, c->y, c->w, c->h, c->bw, 0); showhide(c->snext); } else { /* hide clients bottom up */ @@ -1683,43 +2221,96 @@ tagmon(const Arg *arg) sendmon(selmon->sel, dirtomon(arg->i)); } +void +tagtonext(const Arg *arg) +{ + unsigned int tmp; + + if (selmon->sel == NULL) + return; + + if ((tmp = nexttag()) == selmon->tagset[selmon->seltags]) + return; + + tag(&(const Arg){.ui = tmp }); + view(&(const Arg){.ui = tmp }); +} + +void +tagtoprev(const Arg *arg) +{ + unsigned int tmp; + + if (selmon->sel == NULL) + return; + + if ((tmp = prevtag()) == selmon->tagset[selmon->seltags]) + return; + + tag(&(const Arg){.ui = tmp }); + view(&(const Arg){.ui = tmp }); +} + +void +tagnthmon(const Arg *arg) +{ + if (!selmon->sel || !mons->next) + return; + sendmon(selmon->sel, numtomon(arg->i)); +} + void tile(Monitor *m) { - unsigned int i, n, h, mw, my, ty; + unsigned int i, n, h, mw, my, ty, bw; Client *c; for (n = 0, c = nexttiled(m->clients); c; c = nexttiled(c->next), n++); if (n == 0) return; + if (n == 1) + bw = 0; + else + bw = borderpx; if (n > m->nmaster) mw = m->nmaster ? m->ww * m->mfact : 0; else - mw = m->ww; - for (i = my = ty = 0, c = nexttiled(m->clients); c; c = nexttiled(c->next), i++) - if (i < m->nmaster) { - h = (m->wh - my) / (MIN(n, m->nmaster) - i); - resize(c, m->wx, m->wy + my, mw - (2*c->bw), h - (2*c->bw), 0); - if (my + HEIGHT(c) < m->wh) - my += HEIGHT(c); + mw = m->ww - m->gappx; + for (i = 0, my = ty = m->gappx, c = nexttiled(m->clients); c; c = nexttiled(c->next), i++) + if (i < m->nmaster) { + h = (m->wh - my) / (MIN(n, m->nmaster) - i) - m->gappx; + resize(c, m->wx + m->gappx, m->wy + my, mw - 2*bw - m->gappx, h - 2*bw, bw, 0); + if (my + HEIGHT(c) + m->gappx < m->wh) + my += HEIGHT(c) + m->gappx; } else { - h = (m->wh - ty) / (n - i); - resize(c, m->wx + mw, m->wy + ty, m->ww - mw - (2*c->bw), h - (2*c->bw), 0); - if (ty + HEIGHT(c) < m->wh) - ty += HEIGHT(c); + h = (m->wh - ty) / (n - i) - m->gappx; + resize(c, m->wx + mw + m->gappx, m->wy + ty, m->ww - mw - 2*bw - 2*m->gappx, h - 2*bw, bw, 0); + if (ty + HEIGHT(c) + m->gappx < m->wh) + ty += HEIGHT(c) + m->gappx; } } void togglebar(const Arg *arg) { - selmon->showbar = !selmon->showbar; + selmon->showbar = /* selmon->pertag->showbars[selmon->pertag->curtag] = */ (selmon->showbar == 2 ? 1 : !selmon->showbar); updatebarpos(selmon); XMoveResizeWindow(dpy, selmon->barwin, selmon->wx, selmon->by, selmon->ww, bh); arrange(selmon); } +void +tabmode(const Arg *arg) +{ + if(arg && arg->i >= 0) + selmon->showtab = arg->ui % showtab_nmodes; + else + selmon->showtab = (selmon->showtab + 1 ) % showtab_nmodes; + arrange(selmon); +} + + void togglefloating(const Arg *arg) { @@ -1730,10 +2321,19 @@ togglefloating(const Arg *arg) selmon->sel->isfloating = !selmon->sel->isfloating || selmon->sel->isfixed; if (selmon->sel->isfloating) resize(selmon->sel, selmon->sel->x, selmon->sel->y, - selmon->sel->w, selmon->sel->h, 0); + selmon->sel->w - 2 * (borderpx - selmon->sel->bw), + selmon->sel->h - 2 * (borderpx - selmon->sel->bw), + borderpx, 0); arrange(selmon); } +void +togglefullscr(const Arg *arg) +{ + if(selmon->sel) + setfullscreen(selmon->sel, !selmon->sel->isfullscreen); +} + void toggletag(const Arg *arg) { @@ -1753,9 +2353,33 @@ void toggleview(const Arg *arg) { unsigned int newtagset = selmon->tagset[selmon->seltags] ^ (arg->ui & TAGMASK); + int i; if (newtagset) { selmon->tagset[selmon->seltags] = newtagset; + + if (newtagset == ~0) { + selmon->pertag->prevtag = selmon->pertag->curtag; + selmon->pertag->curtag = 0; + } + + /* test if the user did not select the same tag */ + if (!(newtagset & 1 << (selmon->pertag->curtag - 1))) { + selmon->pertag->prevtag = selmon->pertag->curtag; + for (i = 0; !(newtagset & 1 << i); i++) ; + selmon->pertag->curtag = i + 1; + } + + /* apply settings for this view */ + selmon->nmaster = selmon->pertag->nmasters[selmon->pertag->curtag]; + selmon->mfact = selmon->pertag->mfacts[selmon->pertag->curtag]; + selmon->sellt = selmon->pertag->sellts[selmon->pertag->curtag]; + selmon->lt[selmon->sellt] = selmon->pertag->ltidxs[selmon->pertag->curtag][selmon->sellt]; + selmon->lt[selmon->sellt^1] = selmon->pertag->ltidxs[selmon->pertag->curtag][selmon->sellt^1]; + + /* if (selmon->showbar != selmon->pertag->showbars[selmon->pertag->curtag]) */ + /* togglebar(NULL); */ + focus(NULL); arrange(selmon); } @@ -1798,6 +2422,9 @@ unmanage(Client *c, int destroyed) focus(NULL); updateclientlist(); arrange(m); + if (m == selmon && m->sel) + XWarpPointer(dpy, None, m->sel->win, 0, 0, 0, 0, + m->sel->w/2, m->sel->h/2); } void @@ -1832,6 +2459,11 @@ updatebars(void) CWOverrideRedirect|CWBackPixmap|CWEventMask, &wa); XDefineCursor(dpy, m->barwin, cursor[CurNormal]->cursor); XMapRaised(dpy, m->barwin); + m->tabwin = XCreateWindow(dpy, root, m->wx, m->ty, m->ww, th, 0, DefaultDepth(dpy, screen), + CopyFromParent, DefaultVisual(dpy, screen), + CWOverrideRedirect|CWBackPixmap|CWEventMask, &wa); + XDefineCursor(dpy, m->tabwin, cursor[CurNormal]->cursor); + XMapRaised(dpy, m->tabwin); XSetClassHint(dpy, m->barwin, &ch); } } @@ -1839,14 +2471,33 @@ updatebars(void) void updatebarpos(Monitor *m) { + Client *c; + int nvis = 0; + m->wy = m->my; m->wh = m->mh; if (m->showbar) { m->wh -= bh; m->by = m->topbar ? m->wy : m->wy + m->wh; - m->wy = m->topbar ? m->wy + bh : m->wy; - } else + if ( m->topbar ) + m->wy += bh; + } else { m->by = -bh; + } + + for(c = m->clients; c; c = c->next) { + if(ISVISIBLE(c)) ++nvis; + } + + if(m->showtab == showtab_always + || ((m->showtab == showtab_auto) && (nvis > 1) && (m->lt[m->sellt]->arrange == monocle))) { + m->wh -= th; + m->ty = m->toptab ? m->wy : m->wy + m->wh; + if ( m->toptab ) + m->wy += th; + } else { + m->ty = -th; + } } void @@ -2052,15 +2703,96 @@ updatewmhints(Client *c) void view(const Arg *arg) { + int i; + unsigned int tmptag; + if ((arg->ui & TAGMASK) == selmon->tagset[selmon->seltags]) return; selmon->seltags ^= 1; /* toggle sel tagset */ - if (arg->ui & TAGMASK) + if (arg->ui & TAGMASK) { selmon->tagset[selmon->seltags] = arg->ui & TAGMASK; + selmon->pertag->prevtag = selmon->pertag->curtag; + + if (arg->ui == ~0) + selmon->pertag->curtag = 0; + else { + for (i = 0; !(arg->ui & 1 << i); i++) ; + selmon->pertag->curtag = i + 1; + } + } else { + tmptag = selmon->pertag->prevtag; + selmon->pertag->prevtag = selmon->pertag->curtag; + selmon->pertag->curtag = tmptag; + } + + selmon->nmaster = selmon->pertag->nmasters[selmon->pertag->curtag]; + selmon->mfact = selmon->pertag->mfacts[selmon->pertag->curtag]; + selmon->sellt = selmon->pertag->sellts[selmon->pertag->curtag]; + selmon->lt[selmon->sellt] = selmon->pertag->ltidxs[selmon->pertag->curtag][selmon->sellt]; + selmon->lt[selmon->sellt^1] = selmon->pertag->ltidxs[selmon->pertag->curtag][selmon->sellt^1]; + + /* if (selmon->showbar != selmon->pertag->showbars[selmon->pertag->curtag]) */ + /* togglebar(NULL); */ + focus(NULL); arrange(selmon); } +void +viewclient(Client *c) +{ + if (!(c->tags & c->mon->tagset[c->mon->seltags])) + view(&(Arg){ .ui = c->tags }); + focus(c); +} + +void +viewwin(const Arg *arg) +{ + Client *c = wintoclient((Window)(intptr_t)arg->v); + + if (!c) + return; + + viewclient(c); +} + +Client * +pattoclient(const char *pattern) +{ + Client *c; + Monitor *m; + + for (m = mons; m; m = m->next) + for (c = m->clients; c; c = c->next) + if (!fnmatch(pattern, c->name, 0)) + return c; + return NULL; +} + +void +viewname(const Arg *arg) +{ + Client *c = pattoclient(arg->v); + + if (!c) + return; + + viewclient(c); +} + +void +viewnext(const Arg *arg) +{ + view(&(const Arg){.ui = nexttag()}); +} + +void +viewprev(const Arg *arg) +{ + view(&(const Arg){.ui = prevtag()}); +} + Client * wintoclient(Window w) { @@ -2084,7 +2816,7 @@ wintomon(Window w) if (w == root && getrootptr(&x, &y)) return recttomon(x, y, 1, 1); for (m = mons; m; m = m->next) - if (w == m->barwin) + if (w == m->barwin || w == m->tabwin) return m; if ((c = wintoclient(w))) return c->mon; @@ -2139,6 +2871,38 @@ zoom(const Arg *arg) pop(c); } +void +swapmon(const Arg *arg) +{ + if (mons->next == NULL) + return; + + Monitor *m1 = mons; + Monitor *m2 = mons->next; + + unsigned int tmp = m1->tagset[m1->seltags]; + m1->tagset[m1->seltags] = m2->tagset[m2->seltags]; + m2->tagset[m2->seltags] = tmp; + + Client *c; + for (c = m1->clients; c; c = c->next) + c->mon = m2; + for (c = m2->clients; c; c = c->next) + c->mon = m1; + + Client *tmp_clients = m1->clients; + m1->clients = m2->clients; + m2->clients = tmp_clients; + + Client *tmp_stack = m1->stack; + m1->stack = m2->stack; + m2->stack = tmp_stack; + + focus(NULL); + arrange(m1); + arrange(m2); +} + int main(int argc, char *argv[]) {