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