sites

public wiki contents of suckless.org
git clone git://git.suckless.org/sites
Log | Files | Refs

dwm-moveresize-20221210-7ac106c.diff (6719B)


      1 From 7ac106cad72c909fbb05d302f84191cdec8f2ac0 Mon Sep 17 00:00:00 2001
      2 From: oxinosg <oxinosg@gmail.com>
      3 Date: Sat, 10 Dec 2022 15:24:14 +0100
      4 Subject: [PATCH] [dwm][moveresize]: fix for resize to edge
      5 
      6 ---
      7  config.def.h |  16 ++++++
      8  dwm.c        | 154 +++++++++++++++++++++++++++++++++++++++++++++++++++
      9  2 files changed, 170 insertions(+)
     10 
     11 diff --git a/config.def.h b/config.def.h
     12 index 9efa774..99049e7 100644
     13 --- a/config.def.h
     14 +++ b/config.def.h
     15 @@ -79,6 +79,22 @@ static const Key keys[] = {
     16  	{ MODKEY,                       XK_m,      setlayout,      {.v = &layouts[2]} },
     17  	{ MODKEY,                       XK_space,  setlayout,      {0} },
     18  	{ MODKEY|ShiftMask,             XK_space,  togglefloating, {0} },
     19 +	{ MODKEY,                       XK_Down,   moveresize,     {.v = "0x 25y 0w 0h" } },
     20 +	{ MODKEY,                       XK_Up,     moveresize,     {.v = "0x -25y 0w 0h" } },
     21 +	{ MODKEY,                       XK_Right,  moveresize,     {.v = "25x 0y 0w 0h" } },
     22 +	{ MODKEY,                       XK_Left,   moveresize,     {.v = "-25x 0y 0w 0h" } },
     23 +	{ MODKEY|ShiftMask,             XK_Down,   moveresize,     {.v = "0x 0y 0w 25h" } },
     24 +	{ MODKEY|ShiftMask,             XK_Up,     moveresize,     {.v = "0x 0y 0w -25h" } },
     25 +	{ MODKEY|ShiftMask,             XK_Right,  moveresize,     {.v = "0x 0y 25w 0h" } },
     26 +	{ MODKEY|ShiftMask,             XK_Left,   moveresize,     {.v = "0x 0y -25w 0h" } },
     27 +	{ MODKEY|ControlMask,           XK_Up,     moveresizeedge, {.v = "t"} },
     28 +	{ MODKEY|ControlMask,           XK_Down,   moveresizeedge, {.v = "b"} },
     29 +	{ MODKEY|ControlMask,           XK_Left,   moveresizeedge, {.v = "l"} },
     30 +	{ MODKEY|ControlMask,           XK_Right,  moveresizeedge, {.v = "r"} },
     31 +	{ MODKEY|ControlMask|ShiftMask, XK_Up,     moveresizeedge, {.v = "T"} },
     32 +	{ MODKEY|ControlMask|ShiftMask, XK_Down,   moveresizeedge, {.v = "B"} },
     33 +	{ MODKEY|ControlMask|ShiftMask, XK_Left,   moveresizeedge, {.v = "L"} },
     34 +	{ MODKEY|ControlMask|ShiftMask, XK_Right,  moveresizeedge, {.v = "R"} },
     35  	{ MODKEY,                       XK_0,      view,           {.ui = ~0 } },
     36  	{ MODKEY|ShiftMask,             XK_0,      tag,            {.ui = ~0 } },
     37  	{ MODKEY,                       XK_comma,  focusmon,       {.i = -1 } },
     38 diff --git a/dwm.c b/dwm.c
     39 index 03baf42..89ec70d 100644
     40 --- a/dwm.c
     41 +++ b/dwm.c
     42 @@ -183,6 +183,8 @@ static void mappingnotify(XEvent *e);
     43  static void maprequest(XEvent *e);
     44  static void monocle(Monitor *m);
     45  static void motionnotify(XEvent *e);
     46 +static void moveresize(const Arg *arg);
     47 +static void moveresizeedge(const Arg *arg);
     48  static void movemouse(const Arg *arg);
     49  static Client *nexttiled(Client *c);
     50  static void pop(Client *c);
     51 @@ -1203,6 +1205,158 @@ movemouse(const Arg *arg)
     52  	}
     53  }
     54  
     55 +void
     56 +moveresize(const Arg *arg) {
     57 +	/* only floating windows can be moved */
     58 +	Client *c;
     59 +	c = selmon->sel;
     60 +	int x, y, w, h, nx, ny, nw, nh, ox, oy, ow, oh;
     61 +	char xAbs, yAbs, wAbs, hAbs;
     62 +	int msx, msy, dx, dy, nmx, nmy;
     63 +	unsigned int dui;
     64 +	Window dummy;
     65 +
     66 +	if (!c || !arg)
     67 +		return;
     68 +	if (selmon->lt[selmon->sellt]->arrange && !c->isfloating)
     69 +		return;
     70 +	if (sscanf((char *)arg->v, "%d%c %d%c %d%c %d%c", &x, &xAbs, &y, &yAbs, &w, &wAbs, &h, &hAbs) != 8)
     71 +		return;
     72 +
     73 +	/* compute new window position; prevent window from be positioned outside the current monitor */
     74 +	nw = c->w + w;
     75 +	if (wAbs == 'W')
     76 +		nw = w < selmon->mw - 2 * c->bw ? w : selmon->mw - 2 * c->bw;
     77 +
     78 +	nh = c->h + h;
     79 +	if (hAbs == 'H')
     80 +		nh = h < selmon->mh - 2 * c->bw ? h : selmon->mh - 2 * c->bw;
     81 +
     82 +	nx = c->x + x;
     83 +	if (xAbs == 'X') {
     84 +		if (x < selmon->mx)
     85 +			nx = selmon->mx;
     86 +		else if (x > selmon->mx + selmon->mw)
     87 +			nx = selmon->mx + selmon->mw - nw - 2 * c->bw;
     88 +		else
     89 +			nx = x;
     90 +	}
     91 +
     92 +	ny = c->y + y;
     93 +	if (yAbs == 'Y') {
     94 +		if (y < selmon->my)
     95 +			ny = selmon->my;
     96 +		else if (y > selmon->my + selmon->mh)
     97 +			ny = selmon->my + selmon->mh - nh - 2 * c->bw;
     98 +		else
     99 +			ny = y;
    100 +	}
    101 +
    102 +	ox = c->x;
    103 +	oy = c->y;
    104 +	ow = c->w;
    105 +	oh = c->h;
    106 +
    107 +	XRaiseWindow(dpy, c->win);
    108 +	Bool xqp = XQueryPointer(dpy, root, &dummy, &dummy, &msx, &msy, &dx, &dy, &dui);
    109 +	resize(c, nx, ny, nw, nh, True);
    110 +
    111 +	/* move cursor along with the window to avoid problems caused by the sloppy focus */
    112 +	if (xqp && ox <= msx && (ox + ow) >= msx && oy <= msy && (oy + oh) >= msy)
    113 +	{
    114 +		nmx = c->x - ox + c->w - ow;
    115 +		nmy = c->y - oy + c->h - oh;
    116 +		/* make sure the cursor stays inside the window */
    117 +		if ((msx + nmx) > c->x && (msy + nmy) > c->y)
    118 +			XWarpPointer(dpy, None, None, 0, 0, 0, 0, nmx, nmy);
    119 +	}
    120 +}
    121 +
    122 +void
    123 +moveresizeedge(const Arg *arg) {
    124 +	/* move or resize floating window to edge of screen */
    125 +	Client *c;
    126 +	c = selmon->sel;
    127 +	char e;
    128 +	int nx, ny, nw, nh, ox, oy, ow, oh, bp;
    129 +	int msx, msy, dx, dy, nmx, nmy;
    130 +	int starty;
    131 +	unsigned int dui;
    132 +	Window dummy;
    133 +
    134 +	nx = c->x;
    135 +	ny = c->y;
    136 +	nw = c->w;
    137 +	nh = c->h;
    138 +
    139 +	starty = selmon->showbar && topbar ? bh : 0;
    140 +	bp = selmon->showbar && !topbar ? bh : 0;
    141 +
    142 +	if (!c || !arg)
    143 +		return;
    144 +	if (selmon->lt[selmon->sellt]->arrange && !c->isfloating)
    145 +		return;
    146 +	if(sscanf((char *)arg->v, "%c", &e) != 1)
    147 +		return;
    148 +
    149 +	if(e == 't')
    150 +		ny = starty;
    151 +
    152 +	if(e == 'b')
    153 +		ny = c->h > selmon->mh - 2 * c->bw ? c->h - bp : selmon->mh - c->h - 2 * c->bw - bp;
    154 +
    155 +	if(e == 'l')
    156 +		nx = selmon->mx;
    157 +
    158 +	if(e == 'r')
    159 +		nx = c->w > selmon->mw - 2 * c->bw ? selmon->mx + c->w : selmon->mx + selmon->mw - c->w - 2 * c->bw;
    160 +
    161 +	if(e == 'T') {
    162 +		/* if you click to resize again, it will return to old size/position */
    163 +		if(c->h + starty == c->oldh + c->oldy) {
    164 +			nh = c->oldh;
    165 +			ny = c->oldy;
    166 +		} else {
    167 +			nh = c->h + c->y - starty;
    168 +			ny = starty;
    169 +		}
    170 +	}
    171 +
    172 +	if(e == 'B')
    173 +		nh = c->h + c->y + 2 * c->bw + bp == selmon->mh ? c->oldh : selmon->mh - c->y - 2 * c->bw - bp;
    174 +
    175 +	if(e == 'L') {
    176 +		if(selmon->mx + c->w == c->oldw + c->oldx) {
    177 +			nw = c->oldw;
    178 +			nx = c->oldx;
    179 +		} else {
    180 +			nw = c->w + c->x - selmon->mx;
    181 +			nx = selmon->mx;
    182 +		}
    183 +	}
    184 +
    185 +	if(e == 'R')
    186 +		nw = c->w + c->x + 2 * c->bw == selmon->mx + selmon->mw ? c->oldw : selmon->mx + selmon->mw - c->x - 2 * c->bw;
    187 +
    188 +	ox = c->x;
    189 +	oy = c->y;
    190 +	ow = c->w;
    191 +	oh = c->h;
    192 +
    193 +	XRaiseWindow(dpy, c->win);
    194 +	Bool xqp = XQueryPointer(dpy, root, &dummy, &dummy, &msx, &msy, &dx, &dy, &dui);
    195 +	resizeclient(c, nx, ny, nw, nh);
    196 +
    197 +	/* move cursor along with the window to avoid problems caused by the sloppy focus */
    198 +	if (xqp && ox <= msx && (ox + ow) >= msx && oy <= msy && (oy + oh) >= msy) {
    199 +		nmx = c->x - ox + c->w - ow;
    200 +		nmy = c->y - oy + c->h - oh;
    201 +		/* make sure the cursor stays inside the window */
    202 +		if ((msx + nmx) > c->x && (msy + nmy) > c->y)
    203 +			XWarpPointer(dpy, None, None, 0, 0, 0, 0, nmx, nmy);
    204 +	}
    205 +}
    206 +
    207  Client *
    208  nexttiled(Client *c)
    209  {
    210 -- 
    211 2.34.1
    212