moveresize.c - dwm - 🖥 my version of dwm (frankenstein's monster)
 (HTM) git clone https://git.drkhsh.at/dwm.git
 (DIR) Log
 (DIR) Files
 (DIR) Refs
 (DIR) README
 (DIR) LICENSE
       ---
       moveresize.c (3866B)
       ---
            1 static void moveresize(const Arg *arg);
            2 static void moveresizeedge(const Arg *arg);
            3 
            4 void
            5 moveresize(const Arg *arg) {
            6         /* only floating windows can be moved */
            7         Client *c;
            8         c = selmon->sel;
            9         int x, y, w, h, nx, ny, nw, nh, ox, oy, ow, oh;
           10         char xAbs, yAbs, wAbs, hAbs;
           11         int msx, msy, dx, dy, nmx, nmy;
           12         unsigned int dui;
           13         Window dummy;
           14 
           15         if (!c || !arg)
           16                 return;
           17         if (selmon->lt[selmon->sellt]->arrange && !c->isfloating)
           18                 return;
           19         if (sscanf((char *)arg->v, "%d%c %d%c %d%c %d%c", &x, &xAbs, &y, &yAbs, &w, &wAbs, &h, &hAbs) != 8)
           20                 return;
           21 
           22         /* compute new window position; prevent window from be positioned outside the current monitor */
           23         nw = c->w + w;
           24         if (wAbs == 'W')
           25                 nw = w < selmon->mw - 2 * c->bw ? w : selmon->mw - 2 * c->bw;
           26 
           27         nh = c->h + h;
           28         if (hAbs == 'H')
           29                 nh = h < selmon->mh - 2 * c->bw ? h : selmon->mh - 2 * c->bw;
           30 
           31         nx = c->x + x;
           32         if (xAbs == 'X') {
           33                 if (x < selmon->mx)
           34                         nx = selmon->mx;
           35                 else if (x > selmon->mx + selmon->mw)
           36                         nx = selmon->mx + selmon->mw - nw - 2 * c->bw;
           37                 else
           38                         nx = x;
           39         }
           40 
           41         ny = c->y + y;
           42         if (yAbs == 'Y') {
           43                 if (y < selmon->my)
           44                         ny = selmon->my;
           45                 else if (y > selmon->my + selmon->mh)
           46                         ny = selmon->my + selmon->mh - nh - 2 * c->bw;
           47                 else
           48                         ny = y;
           49         }
           50 
           51         ox = c->x;
           52         oy = c->y;
           53         ow = c->w;
           54         oh = c->h;
           55 
           56         XRaiseWindow(dpy, c->win);
           57         Bool xqp = XQueryPointer(dpy, root, &dummy, &dummy, &msx, &msy, &dx, &dy, &dui);
           58         resize(c, nx, ny, nw, nh, True);
           59 
           60         /* move cursor along with the window to avoid problems caused by the sloppy focus */
           61         if (xqp && ox <= msx && (ox + ow) >= msx && oy <= msy && (oy + oh) >= msy)
           62         {
           63                 nmx = c->x - ox + c->w - ow;
           64                 nmy = c->y - oy + c->h - oh;
           65                 /* make sure the cursor stays inside the window */
           66                 if ((msx + nmx) > c->x && (msy + nmy) > c->y)
           67                         XWarpPointer(dpy, None, None, 0, 0, 0, 0, nmx, nmy);
           68         }
           69 }
           70 
           71 void
           72 moveresizeedge(const Arg *arg) {
           73         /* move or resize floating window to edge of screen */
           74         Client *c;
           75         c = selmon->sel;
           76         char e;
           77         int nx, ny, nw, nh, ox, oy, ow, oh, bp;
           78         int msx, msy, dx, dy, nmx, nmy;
           79         int starty;
           80         unsigned int dui;
           81         Window dummy;
           82 
           83         nx = c->x;
           84         ny = c->y;
           85         nw = c->w;
           86         nh = c->h;
           87 
           88         starty = selmon->showbar && topbar ? bh : 0;
           89         bp = selmon->showbar && !topbar ? bh : 0;
           90 
           91         if (!c || !arg)
           92                 return;
           93         if (selmon->lt[selmon->sellt]->arrange && !c->isfloating)
           94                 return;
           95         if(sscanf((char *)arg->v, "%c", &e) != 1)
           96                 return;
           97 
           98         if(e == 't')
           99                 ny = starty;
          100 
          101         if(e == 'b')
          102                 ny = c->h > selmon->mh - 2 * c->bw ? c->h - bp : selmon->mh - c->h - 2 * c->bw - bp;
          103 
          104         if(e == 'l')
          105                 nx = selmon->mx;
          106 
          107         if(e == 'r')
          108                 nx = c->w > selmon->mw - 2 * c->bw ? selmon->mx + c->w : selmon->mx + selmon->mw - c->w - 2 * c->bw;
          109 
          110         if(e == 'T') {
          111                 /* if you click to resize again, it will return to old size/position */
          112                 if(c->h + starty == c->oldh + c->oldy) {
          113                         nh = c->oldh;
          114                         ny = c->oldy;
          115                 } else {
          116                         nh = c->h + c->y - starty;
          117                         ny = starty;
          118                 }
          119         }
          120 
          121         if(e == 'B')
          122                 nh = c->h + c->y + 2 * c->bw + bp == selmon->mh ? c->oldh : selmon->mh - c->y - 2 * c->bw - bp;
          123 
          124         if(e == 'L') {
          125                 if(selmon->mx + c->w == c->oldw + c->oldx) {
          126                         nw = c->oldw;
          127                         nx = c->oldx;
          128                 } else {
          129                         nw = c->w + c->x - selmon->mx;
          130                         nx = selmon->mx;
          131                 }
          132         }
          133 
          134         if(e == 'R')
          135                 nw = c->w + c->x + 2 * c->bw == selmon->mx + selmon->mw ? c->oldw : selmon->mx + selmon->mw - c->x - 2 * c->bw;
          136 
          137         ox = c->x;
          138         oy = c->y;
          139         ow = c->w;
          140         oh = c->h;
          141 
          142         XRaiseWindow(dpy, c->win);
          143         Bool xqp = XQueryPointer(dpy, root, &dummy, &dummy, &msx, &msy, &dx, &dy, &dui);
          144         resizeclient(c, nx, ny, nw, nh);
          145 
          146         /* move cursor along with the window to avoid problems caused by the sloppy focus */
          147         if (xqp && ox <= msx && (ox + ow) >= msx && oy <= msy && (oy + oh) >= msy) {
          148                         nmx = c->x - ox + c->w - ow;
          149                         nmy = c->y - oy + c->h - oh;
          150                         /* make sure the cursor stays inside the window */
          151                         if ((msx + nmx) > c->x && (msy + nmy) > c->y)
          152                                 XWarpPointer(dpy, None, None, 0, 0, 0, 0, nmx, nmy);
          153         }
          154 }
          155