Avoid using dimensions struct for unrelated uses

The `dimension_t` struct was used in many different places and even held
on the geometry info at some point which is counter-intuitive as it's
best to have each struct serve a single purpose.
This commit is contained in:
Nikos Tsipinakis 2018-03-08 18:25:56 +02:00
parent a7fd3cb0ec
commit 651be8eee9
5 changed files with 53 additions and 56 deletions

View File

@ -152,13 +152,9 @@ static bool have_dynamic_width(void)
return (settings.geometry.width_set && settings.geometry.w == 0);
}
static dimension_t calculate_dimensions(GSList *layouts)
static struct dimensions calculate_dimensions(GSList *layouts)
{
dimension_t dim;
dim.w = 0;
dim.h = 0;
dim.x = 0;
dim.y = 0;
struct dimensions dim = { 0 };
screen_info *scr = get_active_screen();
if (have_dynamic_width()) {
@ -167,13 +163,13 @@ static dimension_t calculate_dimensions(GSList *layouts)
} else if (settings.geometry.width_set) {
/* fixed width */
if (settings.geometry.negative_width) {
dim.w = scr->dim.w - settings.geometry.w;
dim.w = scr->w - settings.geometry.w;
} else {
dim.w = settings.geometry.w;
}
} else {
/* across the screen */
dim.w = scr->dim.w;
dim.w = scr->w;
}
dim.h += 2 * settings.frame_width;
@ -199,9 +195,9 @@ static dimension_t calculate_dimensions(GSList *layouts)
/* subtract height from the unwrapped text */
dim.h -= h;
if (total_width > scr->dim.w) {
if (total_width > scr->w) {
/* set width to screen width */
dim.w = scr->dim.w - settings.geometry.x * 2;
dim.w = scr->w - settings.geometry.x * 2;
} else if (have_dynamic_width() || (total_width < settings.geometry.w && settings.shrink)) {
/* set width to text width */
dim.w = total_width + 2 * settings.frame_width;
@ -320,7 +316,7 @@ static colored_layout *r_init_shared(cairo_t *c, notification *n)
cl->n = n;
dimension_t dim = calculate_dimensions(NULL);
struct dimensions dim = calculate_dimensions(NULL);
int width = dim.w;
if (have_dynamic_width()) {
@ -416,7 +412,7 @@ static void r_free_layouts(GSList *layouts)
g_slist_free_full(layouts, free_colored_layout);
}
static dimension_t x_render_layout(cairo_t *c, colored_layout *cl, colored_layout *cl_next, dimension_t dim, bool first, bool last)
static struct dimensions x_render_layout(cairo_t *c, colored_layout *cl, colored_layout *cl_next, struct dimensions dim, bool first, bool last)
{
int h;
int h_text = 0;
@ -520,7 +516,7 @@ void draw(void)
GSList *layouts = r_create_layouts(c_context);
dimension_t dim = calculate_dimensions(layouts);
struct dimensions dim = calculate_dimensions(layouts);
int width = dim.w;
int height = dim.h;

View File

@ -126,12 +126,12 @@ void randr_update(void)
alloc_screen_ar(n);
for (int i = 0; i < n; i++) {
screens[i].scr = i;
screens[i].dim.x = m[i].x;
screens[i].dim.y = m[i].y;
screens[i].dim.w = m[i].width;
screens[i].dim.h = m[i].height;
screens[i].dim.mmh = m[i].mheight;
screens[i].id = i;
screens[i].x = m[i].x;
screens[i].y = m[i].y;
screens[i].w = m[i].width;
screens[i].h = m[i].height;
screens[i].mmh = m[i].mheight;
}
XRRFreeMonitors(m);
@ -139,7 +139,7 @@ void randr_update(void)
static int autodetect_dpi(screen_info *scr)
{
return (double)scr->dim.h * 25.4 / (double)scr->dim.mmh;
return (double)scr->h * 25.4 / (double)scr->mmh;
}
void screen_check_event(XEvent event)
@ -165,11 +165,11 @@ void xinerama_update(void)
alloc_screen_ar(n);
for (int i = 0; i < n; i++) {
screens[i].scr = i;
screens[i].dim.x = info[i].x_org;
screens[i].dim.y = info[i].y_org;
screens[i].dim.h = info[i].height;
screens[i].dim.w = info[i].width;
screens[i].id = i;
screens[i].x = info[i].x_org;
screens[i].y = info[i].y_org;
screens[i].h = info[i].height;
screens[i].w = info[i].width;
}
XFree(info);
}
@ -184,8 +184,8 @@ void screen_update_fallback(void)
else
screen = DefaultScreen(xctx.dpy);
screens[0].dim.w = DisplayWidth(xctx.dpy, screen);
screens[0].dim.h = DisplayHeight(xctx.dpy, screen);
screens[0].w = DisplayWidth(xctx.dpy, screen);
screens[0].h = DisplayHeight(xctx.dpy, screen);
}
/* see screen.h */
@ -328,8 +328,8 @@ screen_info *get_active_screen(void)
}
for (int i = 0; i < screens_len; i++) {
if (INRECT(x, y, screens[i].dim.x, screens[i].dim.y,
screens[i].dim.w, screens[i].dim.h)) {
if (INRECT(x, y, screens[i].x, screens[i].y,
screens[i].w, screens[i].h)) {
ret = i;
}
}

View File

@ -7,17 +7,13 @@
#define INRECT(x,y,rx,ry,rw,rh) ((x) >= (rx) && (x) < (rx)+(rw) && (y) >= (ry) && (y) < (ry)+(rh))
typedef struct _dimension_t {
typedef struct {
int id;
int x;
int y;
unsigned int h;
unsigned int mmh;
unsigned int w;
} dimension_t;
typedef struct _screen_info {
int scr;
dimension_t dim;
} screen_info;
void init_screens(void);

View File

@ -53,35 +53,39 @@ cairo_surface_t *x_create_cairo_surface(void)
void x_win_move(int width, int height)
{
// Previous dimensions of the window to avoid calling X11 if no change
// is needed
static struct dimensions window_dim = { 0 };
int x, y;
screen_info *scr = get_active_screen();
xctx.cur_screen = scr->scr;
xctx.cur_screen = scr->id;
/* calculate window position */
if (settings.geometry.negative_x) {
x = (scr->dim.x + (scr->dim.w - width)) + settings.geometry.x;
x = (scr->x + (scr->w - width)) + settings.geometry.x;
} else {
x = scr->dim.x + settings.geometry.x;
x = scr->x + settings.geometry.x;
}
if (settings.geometry.negative_y) {
y = scr->dim.y + (scr->dim.h + settings.geometry.y) - height;
y = scr->y + (scr->h + settings.geometry.y) - height;
} else {
y = scr->dim.y + settings.geometry.y;
y = scr->y + settings.geometry.y;
}
/* move and resize */
if (x != xctx.window_dim.x || y != xctx.window_dim.y) {
if (x != window_dim.x || y != window_dim.y) {
XMoveWindow(xctx.dpy, xctx.win, x, y);
}
if (width != xctx.window_dim.w || height != xctx.window_dim.h) {
if (width != window_dim.w || height != window_dim.h) {
XResizeWindow(xctx.dpy, xctx.win, width, height);
}
xctx.window_dim.x = x;
xctx.window_dim.y = y;
xctx.window_dim.h = height;
xctx.window_dim.w = width;
window_dim.x = x;
window_dim.y = y;
window_dim.h = height;
window_dim.w = width;
}
static void setopacity(Window win, unsigned long opacity)
@ -250,7 +254,7 @@ gboolean x_mainloop_fd_dispatch(GSource *source, GSourceFunc callback, gpointer
* to detect a focus change to another screen
*/
&& xctx.visible
&& get_active_screen()->scr != xctx.cur_screen) {
&& get_active_screen()->id != xctx.cur_screen) {
draw();
}
break;
@ -467,11 +471,6 @@ static void x_win_setup(void)
Window root;
XSetWindowAttributes wa;
xctx.window_dim.x = 0;
xctx.window_dim.y = 0;
xctx.window_dim.w = 0;
xctx.window_dim.h = 0;
root = RootWindow(xctx.dpy, DefaultScreen(xctx.dpy));
xctx.utf8 = XInternAtom(xctx.dpy, "UTF8_STRING", false);
@ -484,9 +483,9 @@ static void x_win_setup(void)
screen_info *scr = get_active_screen();
xctx.win = XCreateWindow(xctx.dpy,
root,
scr->dim.x,
scr->dim.y,
scr->dim.w,
scr->x,
scr->y,
scr->w,
1,
0,
DefaultDepth(xctx.dpy, DefaultScreen(xctx.dpy)),

View File

@ -26,6 +26,13 @@ typedef struct _keyboard_shortcut {
// Cyclical dependency
#include "src/settings.h"
struct dimensions {
int x;
int y;
int w;
int h;
};
typedef struct _xctx {
Atom utf8;
Display *dpy;
@ -34,7 +41,6 @@ typedef struct _xctx {
bool visible;
const char *colors[3][3];
XScreenSaverInfo *screensaver_info;
dimension_t window_dim;
unsigned long sep_custom_col;
} xctx_t;