From 1ec0175dee62d4ac2d2a937922ecc010a193edc3 Mon Sep 17 00:00:00 2001
From: = <=>
Date: Wed, 10 Feb 2021 19:47:02 -0500
Subject: [PATCH] Complete working outer gaps

---
 config.h |  2 ++
 dwm.c    | 61 +++++++++++++++++++++++++++++++++++++++++++++++++-------
 2 files changed, 56 insertions(+), 7 deletions(-)

diff --git a/config.h b/config.h
index ed02b7c..814f7d8 100644
--- a/config.h
+++ b/config.h
@@ -107,6 +107,8 @@ static Key keys[] = {
 	{ MODKEY,                       XK_equal,  setgaps,        {.i = +5 } },
 	{ MODKEY|ShiftMask,             XK_minus,  setgaps,        {.i = GAP_RESET } },
 	{ MODKEY|ShiftMask,             XK_equal,  setgaps,        {.i = GAP_TOGGLE} },
+	{ MODKEY,             XK_bracketleft,	setoutergaps,        {.i = -5 } },
+	{ MODKEY,             XK_bracketright,  setoutergaps,        {.i = +5 } },
 	TAGKEYS(                        XK_1,                      0)
 	TAGKEYS(                        XK_2,                      1)
 	TAGKEYS(                        XK_3,                      2)
diff --git a/dwm.c b/dwm.c
index 4915a9c..0d67216 100644
--- a/dwm.c
+++ b/dwm.c
@@ -226,6 +226,7 @@ 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 setoutergaps(const Arg *arg);
 static void setlayout(const Arg *arg);
 static void setmfact(const Arg *arg);
 static void setup(void);
@@ -1733,6 +1734,7 @@ gap_copy(Gap *to, const Gap *from)
 	to->isgap   = from->isgap;
 	to->realgap = from->realgap;
 	to->gappx   = from->gappx;
+	to->ogap   = from->ogap;
 }
 
 void
@@ -1756,6 +1758,26 @@ setgaps(const Arg *arg)
 	arrange(selmon);
 }
 
+void
+setoutergaps(const Arg *arg)
+{
+	Gap *p = selmon->gap;
+	switch(arg->i)
+	{
+		case GAP_TOGGLE:
+			p->isgap = 1 - p->isgap;
+			break;
+		case GAP_RESET:
+			gap_copy(p, &default_gap);
+			break;
+		default:
+			p->ogap += arg->i;
+			p->isgap = 1;
+	}
+	p->realgap = MAX(p->realgap, 0);
+	p->ogap = p->ogap * p->isgap;
+	arrange(selmon);
+}
 void
 setlayout(const Arg *arg)
 {
@@ -1928,7 +1950,7 @@ tagmon(const Arg *arg)
 void
 tile(Monitor *m)
 {
-	unsigned int i, n, h, mw, my, ty;
+	unsigned int i, n, h, mw, my, ty, xposition, yposition, xlength, ylength;
 	Client *c;
 
 	for (n = 0, c = nexttiled(m->clients); c; c = nexttiled(c->next), n++);
@@ -1938,16 +1960,41 @@ tile(Monitor *m)
 	if (n > m->nmaster)
 		mw = m->nmaster ? m->ww * m->mfact : 0;
 	else
-		mw = m->ww - m->gap->gappx - 2*m->gap->ogap;
-	for (i = 0, my = ty = m->gap->ogap, c = nexttiled(m->clients); c; c = nexttiled(c->next), i++) //May need to change ogap to gappx
+		mw = m->ww - m->gap->gappx;
+	for (i = 0, my = ty = m->gap->ogap, c = nexttiled(m->clients); c; c = nexttiled(c->next), i++)
 		if (i < m->nmaster) {
-			h = ((m->wh - my) / (MIN(n, m->nmaster) - i)) - (m->gap->ogap / (MIN(n, m->nmaster)) - i) - m->gap->gappx - (m->gap->gappx / (MIN(n, m->nmaster) - i));
-			resize(c, m->wx + m->gap->ogap, m->wy + my, mw - (2*c->bw) - m->gap->gappx, h - (2*c->bw), 0);
+			xposition = m->wx + m->gap->ogap;
+			yposition = m->wy + my - (m->gap->ogap / 2);
+
+			if (m->nmaster == 1) {
+				yposition = m->wy + my - (m->gap->ogap / 2);
+			}
+
+			h = (m->wh - my) / (MIN(n, m->nmaster) - i) - m->gap->gappx ;
+
+			xlength = mw - (2*c->bw) - m->gap->gappx;
+			ylength = h - (2*c->bw);
+
+			resize(c, xposition, yposition, xlength, ylength, 0);
 			if (my + HEIGHT(c) + m->gap->gappx < m->wh)
 				my += HEIGHT(c) + m->gap->gappx;
 		} else {
-			h = ((m->wh - ty) / (n - i)) - (m->gap->ogap / (n - i)) - m->gap->gappx - (m->gap->gappx / (n - i));
-			resize(c, m->wx + mw + m->gap->gappx, m->wy + ty, m->ww - mw - (2*c->bw) - 2*m->gap->gappx, h - (2*c->bw), 0);
+			if (i == n - 1) {
+				h = (m->wh - ty) / (n - i) - m->gap->ogap; //+ m->gap->gappx / (n - m->nmaster);
+			} else {
+				h = (m->wh - ty) / (n - i) - m->gap->gappx;
+			}
+		
+			h = (m->wh - ty) / (n - i) - m->gap->gappx;
+			yposition = m->wy + ty - (m->gap->ogap / 2);
+			if (n - m->nmaster == 1) {
+				yposition = m->wy + ty - (m->gap->ogap / 2);
+			}
+
+			xposition = m->wx + mw + m->gap->ogap + m->gap->gappx;
+			xlength = m->ww - mw - (2*c->bw) - 2*m->gap->ogap - m->gap->gappx;
+			ylength = h - (2*c->bw);
+			resize(c, xposition, yposition, xlength, ylength, 0);
 			if (ty + HEIGHT(c) + m->gap->gappx < m->wh)
 				ty += HEIGHT(c) + m->gap->gappx;
 		}