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