Use structure for screen and window area geometries

This commit is contained in:
Alex Kotov 2021-11-20 23:52:28 +05:00
parent b0b1393355
commit 3818554417
Signed by: kotovalexarian
GPG Key ID: 553C0EBBEB5D5F08
3 changed files with 106 additions and 88 deletions

106
src/dwm.c
View File

@ -47,9 +47,20 @@
Mod3Mask | Mod4Mask | Mod5Mask) \
)
#define INTERSECT(x,y,w,h,m) ( \
MAX(0, MIN((x) + (w), (m)->wx + (m)->ww) - MAX((x), (m)->wx)) * \
MAX(0, MIN((y) + (h), (m)->wy + (m)->wh) - MAX((y), (m)->wy)) \
#define INTERSECT(x,y,w,h,m) ( \
MAX( \
0, \
MIN((x) + (w), (m)->window_area_geometry.x + (m)->window_area_geometry.w) \
- \
MAX((x), (m)->window_area_geometry.x) \
) \
* \
MAX( \
0, \
MIN((y) + (h), (m)->window_area_geometry.y + (m)->window_area_geometry.h) \
- \
MAX((y), (m)->window_area_geometry.y) \
) \
)
/*********
@ -117,12 +128,12 @@ typedef struct {
} Layout;
struct Monitor {
struct BasicGeometry screen_geometry;
struct BasicGeometry window_area_geometry;
Unit unit;
int nmaster;
int num;
int mx, my, mw, mh; /* screen size */
int wx, wy, ww, wh; /* window area */
unsigned int sellt;
Client *clients;
Client *sel;
@ -321,17 +332,17 @@ int applysizehints(
*y = 0;
}
} else {
if (*x >= m->wx + m->ww) {
*x = m->wx + m->ww - WIDTH(c);
if (*x >= m->window_area_geometry.x + m->window_area_geometry.w) {
*x = m->window_area_geometry.x + m->window_area_geometry.w - WIDTH(c);
}
if (*y >= m->wy + m->wh) {
*y = m->wy + m->wh - HEIGHT(c);
if (*y >= m->window_area_geometry.y + m->window_area_geometry.h) {
*y = m->window_area_geometry.y + m->window_area_geometry.h - HEIGHT(c);
}
if (*x + *w + 2 * bw <= m->wx) {
*x = m->wx;
if (*x + *w + 2 * bw <= m->window_area_geometry.x) {
*x = m->window_area_geometry.x;
}
if (*y + *h + 2 * bw <= m->wy) {
*y = m->wy;
if (*y + *h + 2 * bw <= m->window_area_geometry.y) {
*y = m->window_area_geometry.y;
}
}
@ -875,16 +886,16 @@ void manage(Window w, XWindowAttributes *wa)
}
}
if (c->geometry.basic.x + WIDTH(c) > c->mon->mx + c->mon->mw) {
c->geometry.basic.x = c->mon->mx + c->mon->mw - WIDTH(c);
if (c->geometry.basic.x + WIDTH(c) > c->mon->screen_geometry.x + c->mon->screen_geometry.w) {
c->geometry.basic.x = c->mon->screen_geometry.x + c->mon->screen_geometry.w - WIDTH(c);
}
if (c->geometry.basic.y + HEIGHT(c) > c->mon->my + c->mon->mh) {
c->geometry.basic.y = c->mon->my + c->mon->mh - HEIGHT(c);
if (c->geometry.basic.y + HEIGHT(c) > c->mon->screen_geometry.y + c->mon->screen_geometry.h) {
c->geometry.basic.y = c->mon->screen_geometry.y + c->mon->screen_geometry.h - HEIGHT(c);
}
c->geometry.basic.x = MAX(c->geometry.basic.x, c->mon->mx);
c->geometry.basic.y = MAX(c->geometry.basic.y, c->mon->my);
c->geometry.basic.x = MAX(c->geometry.basic.x, c->mon->screen_geometry.x);
c->geometry.basic.y = MAX(c->geometry.basic.y, c->mon->screen_geometry.y);
c->geometry.bw = settings_get_border_width();
@ -901,8 +912,8 @@ void manage(Window w, XWindowAttributes *wa)
updatesizehints(c);
updatewmhints(c);
c->geometry.basic.x = c->mon->mx + (c->mon->mw - WIDTH(c)) / 2;
c->geometry.basic.y = c->mon->my + (c->mon->mh - HEIGHT(c)) / 2;
c->geometry.basic.x = c->mon->screen_geometry.x + (c->mon->screen_geometry.w - WIDTH(c)) / 2;
c->geometry.basic.y = c->mon->screen_geometry.y + (c->mon->screen_geometry.h - HEIGHT(c)) / 2;
XSelectInput(
dpy,
@ -1002,16 +1013,16 @@ void movemouse(__attribute__((unused)) const Arg *arg)
int nx = ocx + (ev.xmotion.x - x);
int ny = ocy + (ev.xmotion.y - y);
if (abs(selmon->wx - nx) < snap_distance) {
nx = selmon->wx;
} else if (abs((selmon->wx + selmon->ww) - (nx + WIDTH(c))) < snap_distance) {
nx = selmon->wx + selmon->ww - WIDTH(c);
if (abs(selmon->window_area_geometry.x - nx) < snap_distance) {
nx = selmon->window_area_geometry.x;
} else if (abs((selmon->window_area_geometry.x + selmon->window_area_geometry.w) - (nx + WIDTH(c))) < snap_distance) {
nx = selmon->window_area_geometry.x + selmon->window_area_geometry.w - WIDTH(c);
}
if (abs(selmon->wy - ny) < snap_distance) {
ny = selmon->wy;
} else if (abs((selmon->wy + selmon->wh) - (ny + HEIGHT(c))) < snap_distance) {
ny = selmon->wy + selmon->wh - HEIGHT(c);
if (abs(selmon->window_area_geometry.y - ny) < snap_distance) {
ny = selmon->window_area_geometry.y;
} else if (abs((selmon->window_area_geometry.y + selmon->window_area_geometry.h) - (ny + HEIGHT(c))) < snap_distance) {
ny = selmon->window_area_geometry.y + selmon->window_area_geometry.h - HEIGHT(c);
}
if (!c->isfloating &&
@ -1239,10 +1250,10 @@ void resizemouse(__attribute__((unused)) const Arg *arg)
const int nw = MAX(ev.xmotion.x - ocx - 2 * c->geometry.bw + 1, 1);
const int nh = MAX(ev.xmotion.y - ocy - 2 * c->geometry.bw + 1, 1);
if (c->mon->wx + nw >= selmon->wx &&
c->mon->wx + nw <= selmon->wx + selmon->ww &&
c->mon->wy + nh >= selmon->wy &&
c->mon->wy + nh <= selmon->wy + selmon->wh)
if (c->mon->window_area_geometry.x + nw >= selmon->window_area_geometry.x &&
c->mon->window_area_geometry.x + nw <= selmon->window_area_geometry.x + selmon->window_area_geometry.w &&
c->mon->window_area_geometry.y + nh >= selmon->window_area_geometry.y &&
c->mon->window_area_geometry.y + nh <= selmon->window_area_geometry.y + selmon->window_area_geometry.h)
{
if (!c->isfloating &&
(selmon->lt[selmon->sellt]->arrange == NULL ||
@ -1748,16 +1759,23 @@ int updategeom()
mons = createmon();
}
for (i = 0, m = mons; i < nn && m; m = m->next, i++)
if (i >= n
|| unique[i].x_org != m->mx || unique[i].y_org != m->my
|| unique[i].width != m->mw || unique[i].height != m->mh)
{
if (
i >= n
||
unique[i].x_org != m->screen_geometry.x
||
unique[i].y_org != m->screen_geometry.y
||
unique[i].width != m->screen_geometry.w
||
unique[i].height != m->screen_geometry.h
) {
dirty = 1;
m->num = i;
m->mx = m->wx = unique[i].x_org;
m->my = m->wy = unique[i].y_org;
m->mw = m->ww = unique[i].width;
m->mh = m->wh = unique[i].height;
m->screen_geometry.x = m->window_area_geometry.x = unique[i].x_org;
m->screen_geometry.y = m->window_area_geometry.y = unique[i].y_org;
m->screen_geometry.w = m->window_area_geometry.w = unique[i].width;
m->screen_geometry.h = m->window_area_geometry.h = unique[i].height;
}
} else { /* less monitors available nn < n */
for (i = nn; i < n; i++) {
@ -1781,10 +1799,10 @@ int updategeom()
{ /* default monitor setup */
if (!mons)
mons = createmon();
if (mons->mw != sw || mons->mh != sh) {
if (mons->screen_geometry.w != sw || mons->screen_geometry.h != sh) {
dirty = 1;
mons->mw = mons->ww = sw;
mons->mh = mons->wh = sh;
mons->screen_geometry.w = mons->window_area_geometry.w = sw;
mons->screen_geometry.h = mons->window_area_geometry.h = sh;
}
}
if (dirty) {

View File

@ -77,10 +77,10 @@ void on_configure_request(XEvent *e)
} else if (c->isfloating || !selmon->lt[selmon->sellt]->arrange) {
m = c->mon;
if (ev->value_mask & CWX) {
c->geometry.basic.x = m->mx + ev->x;
c->geometry.basic.x = m->screen_geometry.x + ev->x;
}
if (ev->value_mask & CWY) {
c->geometry.basic.y = m->my + ev->y;
c->geometry.basic.y = m->screen_geometry.y + ev->y;
}
if (ev->value_mask & CWWidth) {
c->geometry.basic.w = ev->width;
@ -88,11 +88,11 @@ void on_configure_request(XEvent *e)
if (ev->value_mask & CWHeight) {
c->geometry.basic.h = ev->height;
}
if ((c->geometry.basic.x + c->geometry.basic.w) > m->mx + m->mw && c->isfloating) {
c->geometry.basic.x = m->mx + (m->mw / 2 - WIDTH(c) / 2); /* center in x direction */
if ((c->geometry.basic.x + c->geometry.basic.w) > m->screen_geometry.x + m->screen_geometry.w && c->isfloating) {
c->geometry.basic.x = m->screen_geometry.x + (m->screen_geometry.w / 2 - WIDTH(c) / 2); /* center in x direction */
}
if ((c->geometry.basic.y + c->geometry.basic.h) > m->my + m->mh && c->isfloating) {
c->geometry.basic.y = m->my + (m->mh / 2 - HEIGHT(c) / 2); /* center in y direction */
if ((c->geometry.basic.y + c->geometry.basic.h) > m->screen_geometry.y + m->screen_geometry.h && c->isfloating) {
c->geometry.basic.y = m->screen_geometry.y + (m->screen_geometry.h / 2 - HEIGHT(c) / 2); /* center in y direction */
}
if ((ev->value_mask & (CWX|CWY)) && !(ev->value_mask & (CWWidth|CWHeight))) {
configure(c);

View File

@ -10,17 +10,17 @@ void centeredmaster(Monitor *m)
const float master_area_factor = unit_get_master_area_factor(m->unit);
unsigned int mx = 0;
unsigned int mw = m->ww;
unsigned int mw = m->window_area_geometry.w;
unsigned int tw = mw;
if (n > m->nmaster) {
/* go mfact box in the center if more than nmaster clients */
mw = m->nmaster ? m->ww * master_area_factor : 0;
tw = m->ww - mw;
mw = m->nmaster ? m->window_area_geometry.w * master_area_factor : 0;
tw = m->window_area_geometry.w - mw;
if (n - m->nmaster > 1) {
/* only one client */
mx = (m->ww - mw) / 2;
tw = (m->ww - mw) / 2;
mx = (m->window_area_geometry.w - mw) / 2;
tw = (m->window_area_geometry.w - mw) / 2;
}
}
@ -39,7 +39,7 @@ void centeredmaster(Monitor *m)
if (i < m->nmaster) {
// nmaster clients are stacked vertically,
// in the center of the screen
const unsigned int h = (m->wh - my) / (MIN(n, m->nmaster) - i);
const unsigned int h = (m->window_area_geometry.h - my) / (MIN(n, m->nmaster) - i);
const int left_gap = (n <= m->nmaster + 1) ? gap_size : top_left_half_gap;
const int top_gap = i == 0 ? gap_size : top_left_half_gap;
@ -48,8 +48,8 @@ void centeredmaster(Monitor *m)
resize(
c,
m->wx + mx + left_gap,
m->wy + my + top_gap,
m->window_area_geometry.x + mx + left_gap,
m->window_area_geometry.y + my + top_gap,
mw - 2 * border_width - left_gap - right_gap,
h - 2 * border_width - top_gap - bottom_gap,
border_width,
@ -60,7 +60,7 @@ void centeredmaster(Monitor *m)
} else {
// stack clients are stacked vertically
if ((i - m->nmaster) % 2) {
const unsigned int h = (m->wh - ety) / ((1 + n - i) / 2);
const unsigned int h = (m->window_area_geometry.h - ety) / ((1 + n - i) / 2);
const int left_gap = gap_size;
const int top_gap = (i == m->nmaster + 1) ? gap_size : top_left_half_gap;
@ -69,8 +69,8 @@ void centeredmaster(Monitor *m)
resize(
c,
m->wx + left_gap,
m->wy + ety + top_gap,
m->window_area_geometry.x + left_gap,
m->window_area_geometry.y + ety + top_gap,
tw - 2 * border_width - left_gap - right_gap,
h - 2 * border_width - top_gap - bottom_gap,
border_width,
@ -79,7 +79,7 @@ void centeredmaster(Monitor *m)
ety += HEIGHT(c) + top_gap + bottom_gap;
} else {
const unsigned int h = (m->wh - oty) / ((1 + n - i) / 2);
const unsigned int h = (m->window_area_geometry.h - oty) / ((1 + n - i) / 2);
const int left_gap = (m->nmaster == 0 && n == 1) ? gap_size : top_left_half_gap;
const int top_gap = i == m->nmaster ? gap_size : top_left_half_gap;
@ -88,8 +88,8 @@ void centeredmaster(Monitor *m)
resize(
c,
m->wx + mx + mw + left_gap,
m->wy + oty + top_gap,
m->window_area_geometry.x + mx + mw + left_gap,
m->window_area_geometry.y + oty + top_gap,
tw - 2 * border_width - left_gap - right_gap,
h - 2 * border_width - top_gap - bottom_gap,
border_width,
@ -137,13 +137,13 @@ void horizontile(Monitor *m)
const int top_left_half_gap = gap_size / 2;
const int bottom_right_half_gap = gap_size - top_left_half_gap;
const unsigned int mh = n > m->nmaster ? (m->nmaster ? m->wh * master_area_factor : 0) : m->wh;
const unsigned int mh = n > m->nmaster ? (m->nmaster ? m->window_area_geometry.h * master_area_factor : 0) : m->window_area_geometry.h;
Client *c = nexttiled(m->clients);
for (unsigned int i = 0, mx = 0, tx = 0; c; c = nexttiled(c->next), ++i) {
if (i < m->nmaster) {
const unsigned int w = (m->ww - mx) / (MIN(n, m->nmaster) - i);
const unsigned int w = (m->window_area_geometry.w - mx) / (MIN(n, m->nmaster) - i);
const unsigned int left_gap = i == 0 ? gap_size : top_left_half_gap;
const unsigned int top_gap = gap_size;
@ -152,8 +152,8 @@ void horizontile(Monitor *m)
resize(
c,
m->wx + mx + left_gap,
m->wy + top_gap,
m->window_area_geometry.x + mx + left_gap,
m->window_area_geometry.y + top_gap,
w - 2 * border_width - left_gap - right_gap,
mh - 2 * border_width - top_gap - bottom_gap,
border_width,
@ -161,11 +161,11 @@ void horizontile(Monitor *m)
);
// FIXME: maybe need + left_gap + right_gap
if (mx + WIDTH(c) < m->ww) {
if (mx + WIDTH(c) < m->window_area_geometry.w) {
mx += WIDTH(c) + left_gap + right_gap;
}
} else {
const unsigned int w = (m->ww - tx) / (n - i);
const unsigned int w = (m->window_area_geometry.w - tx) / (n - i);
const unsigned int left_gap = i == m->nmaster ? gap_size : top_left_half_gap;
const unsigned int top_gap = m->nmaster == 0 ? gap_size : top_left_half_gap;
@ -174,16 +174,16 @@ void horizontile(Monitor *m)
resize(
c,
m->wx + tx + left_gap,
m->wy + mh + top_gap,
m->window_area_geometry.x + tx + left_gap,
m->window_area_geometry.y + mh + top_gap,
w - 2 * border_width - left_gap - right_gap,
m->wh - mh - 2 * border_width - top_gap - bottom_gap,
m->window_area_geometry.h - mh - 2 * border_width - top_gap - bottom_gap,
border_width,
0
);
// FIXME: maybe need + left_gap + right_gap
if (tx + WIDTH(c) < m->ww) {
if (tx + WIDTH(c) < m->window_area_geometry.w) {
tx += WIDTH(c) + left_gap + right_gap;
}
}
@ -205,10 +205,10 @@ void monocle(Monitor *m)
for (Client *c = nexttiled(m->clients); c; c = nexttiled(c->next)) {
resize(
c,
m->wx + gap_size,
m->wy + gap_size,
m->ww - 2 * border_width - 2 * gap_size,
m->wh - 2 * border_width - 2 * gap_size,
m->window_area_geometry.x + gap_size,
m->window_area_geometry.y + gap_size,
m->window_area_geometry.w - 2 * border_width - 2 * gap_size,
m->window_area_geometry.h - 2 * border_width - 2 * gap_size,
border_width,
0
);
@ -231,13 +231,13 @@ void tile(Monitor *m)
const int top_left_half_gap = gap_size / 2;
const int bottom_right_half_gap = gap_size - top_left_half_gap;
const unsigned int mw = n > m->nmaster ? (m->nmaster ? m->ww * master_area_factor : 0) : m->ww;
const unsigned int mw = n > m->nmaster ? (m->nmaster ? m->window_area_geometry.w * master_area_factor : 0) : m->window_area_geometry.w;
Client *c = nexttiled(m->clients);
for (unsigned int i = 0, my = 0, ty = 0; c; c = nexttiled(c->next), ++i) {
if (i < m->nmaster) {
const unsigned int h = (m->wh - my) / (MIN(n, m->nmaster) - i);
const unsigned int h = (m->window_area_geometry.h - my) / (MIN(n, m->nmaster) - i);
const unsigned int left_gap = gap_size;
const unsigned int top_gap = i == 0 ? gap_size : top_left_half_gap;
@ -246,8 +246,8 @@ void tile(Monitor *m)
resize(
c,
m->wx + left_gap,
m->wy + my + top_gap,
m->window_area_geometry.x + left_gap,
m->window_area_geometry.y + my + top_gap,
mw - 2 * border_width - left_gap - right_gap,
h - 2 * border_width - top_gap - bottom_gap,
border_width,
@ -255,11 +255,11 @@ void tile(Monitor *m)
);
// FIXME: maybe need + top_gap + bottom_gap
if (my + HEIGHT(c) < m->wh) {
if (my + HEIGHT(c) < m->window_area_geometry.h) {
my += HEIGHT(c) + top_gap + bottom_gap;
}
} else {
const unsigned int h = (m->wh - ty) / (n - i);
const unsigned int h = (m->window_area_geometry.h - ty) / (n - i);
const unsigned int left_gap = m->nmaster == 0 ? gap_size : top_left_half_gap;
const unsigned int top_gap = i == m->nmaster ? gap_size : top_left_half_gap;
@ -268,16 +268,16 @@ void tile(Monitor *m)
resize(
c,
m->wx + mw + left_gap,
m->wy + ty + top_gap,
m->ww - mw - 2 * border_width - left_gap - right_gap,
m->window_area_geometry.x + mw + left_gap,
m->window_area_geometry.y + ty + top_gap,
m->window_area_geometry.w - mw - 2 * border_width - left_gap - right_gap,
h - 2 * border_width - top_gap - bottom_gap,
border_width,
0
);
// FIXME: maybe need + top_gap + bottom_gap
if (ty + HEIGHT(c) < m->wh) {
if (ty + HEIGHT(c) < m->window_area_geometry.h) {
ty += HEIGHT(c) + top_gap + bottom_gap;
}
}