[hackers] [dwm][PATCH] Added moveresizeedge function which allows moving/resizing floating window to edge of screen

From: oxinosg <oxinosg_AT_gmail.com>
Date: Tue, 21 Apr 2020 19:03:16 +0200

When resizing to edge: in case you repeat the same command, the floating
window will return to old size/position

Example of key bindings:

{ MODKEY, XK_Up, moveresize, {.v = "0x -25y 0w 0h"} },
{ MODKEY, XK_Down, moveresize, {.v = "0x 25y 0w 0h"} },
{ MODKEY, XK_Left, moveresize, {.v = "-25x 0y 0w 0h"} },
{ MODKEY, XK_Right, moveresize, {.v = "25x 0y 0w 0h"} },
{ MODKEY|ShiftMask, XK_Up, moveresize, {.v = "0x 0y 0w -25h"} },
{ MODKEY|ShiftMask, XK_Down, moveresize, {.v = "0x 0y 0w 25h"} },
{ MODKEY|ShiftMask, XK_Left, moveresize, {.v = "0x 0y -25w 0h"} },
{ MODKEY|ShiftMask, XK_Right, moveresize, {.v = "0x 0y 25w 0h"} },
{ MODKEY|ControlMask, XK_Up, moveresizeedge, {.v = "t"} },
{ MODKEY|ControlMask, XK_Down, moveresizeedge, {.v = "b"} },
{ MODKEY|ControlMask, XK_Left, moveresizeedge, {.v = "l"} },
{ MODKEY|ControlMask, XK_Right, moveresizeedge, {.v = "r"} },
{ MODKEY|ControlMask|ShiftMask, XK_Up, moveresizeedge, {.v = "T"} },
{ MODKEY|ControlMask|ShiftMask, XK_Down, moveresizeedge, {.v = "B"} },
{ MODKEY|ControlMask|ShiftMask, XK_Left, moveresizeedge, {.v = "L"} },
{ MODKEY|ControlMask|ShiftMask, XK_Right, moveresizeedge, {.v = "R"} },

based on https://dwm.suckless.org/patches/moveresize/ patch
---
 config.def.h |  18 ++++++-
 moveresize.c | 146 +++++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 163 insertions(+), 1 deletion(-)
 create mode 100644 moveresize.c
diff --git a/config.def.h b/config.def.h
index 1c0b587..4190c06 100644
--- a/config.def.h
+++ b/config.def.h
_AT_@ -84,6 +84,23 @@ static Key keys[] = {
 	{ MODKEY,                       XK_period, focusmon,       {.i = +1 } },
 	{ MODKEY|ShiftMask,             XK_comma,  tagmon,         {.i = -1 } },
 	{ MODKEY|ShiftMask,             XK_period, tagmon,         {.i = +1 } },
+	{ MODKEY,			XK_Page_Down,  shiftview,      { .i = 1 } },
+	{ MODKEY,                       XK_Up,         moveresize,     {.v = "0x -25y 0w 0h"} },
+	{ MODKEY,                       XK_Down,       moveresize,     {.v = "0x 25y 0w 0h"} },
+	{ MODKEY,                       XK_Left,       moveresize,     {.v = "-25x 0y 0w 0h"} },
+	{ MODKEY,                       XK_Right,      moveresize,     {.v = "25x 0y 0w 0h"} },
+	{ MODKEY|ShiftMask,             XK_Up,         moveresize,     {.v = "0x 0y 0w -25h"} },
+	{ MODKEY|ShiftMask,             XK_Down,       moveresize,     {.v = "0x 0y 0w 25h"} },
+	{ MODKEY|ShiftMask,             XK_Left,       moveresize,     {.v = "0x 0y -25w 0h"} },
+	{ MODKEY|ShiftMask,             XK_Right,      moveresize,     {.v = "0x 0y 25w 0h"} },
+	{ MODKEY|ControlMask,           XK_Up,         moveresizeedge, {.v = "t"} },
+	{ MODKEY|ControlMask,           XK_Down,       moveresizeedge, {.v = "b"} },
+	{ MODKEY|ControlMask,           XK_Left,       moveresizeedge, {.v = "l"} },
+	{ MODKEY|ControlMask,           XK_Right,      moveresizeedge, {.v = "r"} },
+	{ MODKEY|ControlMask|ShiftMask, XK_Up,         moveresizeedge, {.v = "T"} },
+	{ MODKEY|ControlMask|ShiftMask, XK_Down,       moveresizeedge, {.v = "B"} },
+	{ MODKEY|ControlMask|ShiftMask, XK_Left,       moveresizeedge, {.v = "L"} },
+	{ MODKEY|ControlMask|ShiftMask, XK_Right,      moveresizeedge, {.v = "R"} },
 	TAGKEYS(                        XK_1,                      0)
 	TAGKEYS(                        XK_2,                      1)
 	TAGKEYS(                        XK_3,                      2)
_AT_@ -112,4 +129,3 @@ static Button buttons[] = {
 	{ ClkTagBar,            MODKEY,         Button1,        tag,            {0} },
 	{ ClkTagBar,            MODKEY,         Button3,        toggletag,      {0} },
 };
-
diff --git a/moveresize.c b/moveresize.c
new file mode 100644
index 0000000..cd8c3e6
--- /dev/null
+++ b/moveresize.c
_AT_@ -0,0 +1,146 @@
+// move or resize floating window to edge of screen
+void
+moveresizeedge(const Arg *arg) {
+	Client *c;
+	c = selmon->sel;
+	char e;
+	int nx, ny, nw, nh, ox, oy, ow, oh;
+	int msx, msy, dx, dy, nmx, nmy;
+	int starty;
+	unsigned int dui;
+	Window dummy;
+
+	nx = c->x;
+	ny = c->y;
+	nw = c->w;
+	nh = c->h;
+
+	starty = selmon->showbar ? bh : 0;
+
+	if (!c || !arg)
+		return;
+	if (selmon->lt[selmon->sellt]->arrange && !c->isfloating)
+		return;
+	if(sscanf((char *)arg->v, "%c", &e) != 1)
+		return;
+
+	if(e == 't')
+		ny = starty;
+
+	if(e == 'b')
+		ny = c->h > selmon->mh - 2 * c->bw ? c->h : selmon->mh - c->h - 2 * c->bw;
+
+	if(e == 'l')
+		nx = 0;
+
+	if(e == 'r')
+		nx = c->w > selmon->mw - 2 * c->bw ? c->w : selmon->mw - c->w - 2 * c->bw;
+
+	if(e == 'T') {
+		// if you click to resize again, it will return to old size/position
+		if(c->h + starty == c->oldh + c->oldy) {
+			nh = c->oldh;
+			ny = c->oldy;
+		} else {
+			nh = c->h + c->y - starty;
+			ny = starty;
+		}
+	}
+
+	if(e == 'B')
+		nh = c->h + c->y + 2 * c->bw == selmon->mh ? c->oldh : selmon->mh - c->y - 2 * c->bw;
+
+	if(e == 'L') {
+		if(c->w == c->oldw + c->oldx) {
+			nw = c->oldw;
+			nx = c->oldx;
+		} else {
+			nw = c->w + c->x;
+			nx = 0;
+		}
+	}
+
+	if(e == 'R')
+		nw = c->w + c->x + 2 * c->bw == selmon->mw ? c->oldw : selmon->mw - c->x - 2 * c->bw;
+
+	ox = c->x;
+	oy = c->y;
+	ow = c->w;
+	oh = c->h;
+
+	XRaiseWindow(dpy, c->win);
+	Bool xqp = XQueryPointer(dpy, root, &dummy, &dummy, &msx, &msy, &dx, &dy, &dui);
+	resize(c, nx, ny, nw, nh, True);
+
+	/* move cursor along with the window to avoid problems caused by the sloppy focus */
+	if (xqp && ox <= msx && (ox + ow) >= msx && oy <= msy && (oy + oh) >= msy) {
+		nmx = c->x - ox + c->w - ow;
+		nmy = c->y - oy + c->h - oh;
+		XWarpPointer(dpy, None, None, 0, 0, 0, 0, nmx, nmy);
+	}
+}
+
+void
+moveresize(const Arg *arg) {
+	/* only floating windows can be moved */
+	Client *c;
+	c = selmon->sel;
+	int x, y, w, h, nx, ny, nw, nh, ox, oy, ow, oh;
+	char xAbs, yAbs, wAbs, hAbs;
+	int msx, msy, dx, dy, nmx, nmy;
+	unsigned int dui;
+	Window dummy;
+
+	if (!c || !arg)
+		return;
+	if (selmon->lt[selmon->sellt]->arrange && !c->isfloating)
+		return;
+	if(sscanf((char *)arg->v, "%d%c %d%c %d%c %d%c", &x, &xAbs, &y, &yAbs, &w, &wAbs, &h, &hAbs) != 8)
+		return;
+	/* compute new window position; prevent window from be positioned outside the current monitor */
+	nw = c->w + w;
+	if(wAbs == 'W')
+		nw = w < selmon->mw - 2 * c->bw ? w : selmon->mw - 2 * c->bw;
+
+	nh = c->h + h;
+	if(hAbs == 'H')
+		nh = h < selmon->mh - 2 * c->bw ? h : selmon->mh - 2 * c->bw;
+
+	nx = c->x + x;
+	if(xAbs == 'X') {
+		if(x < selmon->mx)
+			nx = selmon->mx;
+		else if(x > selmon->mx + selmon->mw)
+			nx = selmon->mx + selmon->mw - nw - 2 * c->bw;
+		else
+			nx = x;
+	}
+
+	ny = c->y + y;
+	if(yAbs == 'Y') {
+		if(y < selmon->my)
+			ny = selmon->my;
+		else if(y > selmon->my + selmon->mh)
+			ny = selmon->my + selmon->mh - nh - 2 * c->bw;
+		else
+			ny = y;
+	}
+
+	ox = c->x;
+	oy = c->y;
+	ow = c->w;
+	oh = c->h;
+
+	XRaiseWindow(dpy, c->win);
+	Bool xqp = XQueryPointer(dpy, root, &dummy, &dummy, &msx, &msy, &dx, &dy, &dui);
+	resize(c, nx, ny, nw, nh, True);
+
+	/* move cursor along with the window to avoid problems caused by the sloppy focus */
+	if (xqp && ox <= msx && (ox + ow) >= msx && oy <= msy && (oy + oh) >= msy)
+	{
+		nmx = c->x - ox + c->w - ow;
+		nmy = c->y - oy + c->h - oh;
+		XWarpPointer(dpy, None, None, 0, 0, 0, 0, nmx, nmy);
+	}
+}
+
-- 
2.20.1
Received on Tue Apr 21 2020 - 19:03:16 CEST

This archive was generated by hypermail 2.3.0 : Tue Apr 21 2020 - 20:00:37 CEST