From 9991f9ccc8786744430c2c0c7bb913f4f4699215 Mon Sep 17 00:00:00 2001 From: Emmanuel Lepage Vallee Date: Mon, 18 Apr 2016 01:15:15 -0400 Subject: [PATCH] geometry: Use the relevant rounding functions instead of integers In the case where one want to put the cursor at the middle of the workarea, it is logic to do: x=screen.workarea.x+screen.workasrea.width/2 However, this can cause floating points. This commit move the burden back to the C-API so the Lua placement code doesn't have to add a large number of rounding methods. Given 1 type of rounding cover a vast majority of use cases for each types of coordinates, the C-API can take care of it in peace. For the other corner cases, it is still possible for the Lua code to do the rounding there, but no longer necessary. The convenstions are: 'x' and 'y': use round (move to the closest point) 'width' and 'height': use ceil (to avoid involontary truncating) --- objects/client.c | 16 +++++++++------- objects/drawin.c | 18 ++++++++++-------- objects/window.c | 2 +- objects/window.h | 1 + root.c | 6 ++++-- strut.c | 9 +++++---- systray.c | 8 ++++---- 7 files changed, 34 insertions(+), 26 deletions(-) diff --git a/objects/client.c b/objects/client.c index 225b4048..8ec2118d 100644 --- a/objects/client.c +++ b/objects/client.c @@ -98,6 +98,8 @@ #include "systray.h" #include "xwindow.h" +#include "math.h" + #include #include #include @@ -2538,7 +2540,7 @@ luaA_client_titlebar_ ## name(lua_State *L) \ if (lua_isnil(L, 2)) \ titlebar_resize(L, 1, c, index, 0); \ else \ - titlebar_resize(L, 1, c, index, luaA_checkinteger_range(L, 2, 0, MAX_X11_SIZE)); \ + titlebar_resize(L, 1, c, index, ceil(luaA_checknumber_range(L, 2, 0, MAX_X11_SIZE))); \ } \ \ luaA_object_push_item(L, 1, titlebar_get_drawable(L, c, 1, index)); \ @@ -2566,8 +2568,8 @@ luaA_client_geometry(lua_State *L) area_t geometry; luaA_checktable(L, 2); - geometry.x = luaA_getopt_integer_range(L, 2, "x", c->geometry.x, MIN_X11_COORDINATE, MAX_X11_COORDINATE); - geometry.y = luaA_getopt_integer_range(L, 2, "y", c->geometry.y, MIN_X11_COORDINATE, MAX_X11_COORDINATE); + geometry.x = round(luaA_getopt_number_range(L, 2, "x", c->geometry.x, MIN_X11_COORDINATE, MAX_X11_COORDINATE)); + geometry.y = round(luaA_getopt_number_range(L, 2, "y", c->geometry.y, MIN_X11_COORDINATE, MAX_X11_COORDINATE)); if(client_isfixed(c)) { geometry.width = c->geometry.width; @@ -2575,8 +2577,8 @@ luaA_client_geometry(lua_State *L) } else { - geometry.width = luaA_getopt_integer_range(L, 2, "width", c->geometry.width, MIN_X11_SIZE, MAX_X11_SIZE); - geometry.height = luaA_getopt_integer_range(L, 2, "height", c->geometry.height, MIN_X11_SIZE, MAX_X11_SIZE); + geometry.width = ceil(luaA_getopt_number_range(L, 2, "width", c->geometry.width, MIN_X11_SIZE, MAX_X11_SIZE)); + geometry.height = ceil(luaA_getopt_number_range(L, 2, "height", c->geometry.height, MIN_X11_SIZE, MAX_X11_SIZE)); } client_resize(c, geometry, c->size_hints_honor); @@ -2600,8 +2602,8 @@ luaA_client_apply_size_hints(lua_State *L) area_t geometry = c->geometry; if(!client_isfixed(c)) { - geometry.width = luaA_checkinteger_range(L, 2, MIN_X11_SIZE, MAX_X11_SIZE); - geometry.height = luaA_checkinteger_range(L, 3, MIN_X11_SIZE, MAX_X11_SIZE); + geometry.width = ceil(luaA_checknumber_range(L, 2, MIN_X11_SIZE, MAX_X11_SIZE)); + geometry.height = ceil(luaA_checknumber_range(L, 3, MIN_X11_SIZE, MAX_X11_SIZE)); } if (c->size_hints_honor) diff --git a/objects/drawin.c b/objects/drawin.c index 8656e9b2..7e4bd415 100644 --- a/objects/drawin.c +++ b/objects/drawin.c @@ -42,6 +42,8 @@ #include "systray.h" #include "xwindow.h" +#include "math.h" + #include #include @@ -407,10 +409,10 @@ luaA_drawin_geometry(lua_State *L) area_t wingeom; luaA_checktable(L, 2); - wingeom.x = luaA_getopt_integer_range(L, 2, "x", drawin->geometry.x, MIN_X11_COORDINATE, MAX_X11_COORDINATE); - wingeom.y = luaA_getopt_integer_range(L, 2, "y", drawin->geometry.y, MIN_X11_COORDINATE, MAX_X11_COORDINATE); - wingeom.width = luaA_getopt_integer_range(L, 2, "width", drawin->geometry.width, MIN_X11_SIZE, MAX_X11_SIZE); - wingeom.height = luaA_getopt_integer_range(L, 2, "height", drawin->geometry.height, MIN_X11_SIZE, MAX_X11_SIZE); + wingeom.x = round(luaA_getopt_number_range(L, 2, "x", drawin->geometry.x, MIN_X11_COORDINATE, MAX_X11_COORDINATE)); + wingeom.y = round(luaA_getopt_number_range(L, 2, "y", drawin->geometry.y, MIN_X11_COORDINATE, MAX_X11_COORDINATE)); + wingeom.width = ceil(luaA_getopt_number_range(L, 2, "width", drawin->geometry.width, MIN_X11_SIZE, MAX_X11_SIZE)); + wingeom.height = ceil(luaA_getopt_number_range(L, 2, "height", drawin->geometry.height, MIN_X11_SIZE, MAX_X11_SIZE)); if(wingeom.width > 0 && wingeom.height > 0) drawin_moveresize(L, 1, wingeom); @@ -427,7 +429,7 @@ LUA_OBJECT_EXPORT_PROPERTY(drawin, drawin_t, visible, lua_pushboolean) static int luaA_drawin_set_x(lua_State *L, drawin_t *drawin) { - int x = luaA_checkinteger_range(L, -1, MIN_X11_COORDINATE, MAX_X11_COORDINATE); + int x = round(luaA_checknumber_range(L, -1, MIN_X11_COORDINATE, MAX_X11_COORDINATE)); drawin_moveresize(L, -3, (area_t) { .x = x, .y = drawin->geometry.y, .width = drawin->geometry.width, @@ -445,7 +447,7 @@ luaA_drawin_get_x(lua_State *L, drawin_t *drawin) static int luaA_drawin_set_y(lua_State *L, drawin_t *drawin) { - int y = luaA_checkinteger_range(L, -1, MIN_X11_COORDINATE, MAX_X11_COORDINATE); + int y = round(luaA_checknumber_range(L, -1, MIN_X11_COORDINATE, MAX_X11_COORDINATE)); drawin_moveresize(L, -3, (area_t) { .x = drawin->geometry.x, .y = y, .width = drawin->geometry.width, @@ -463,7 +465,7 @@ luaA_drawin_get_y(lua_State *L, drawin_t *drawin) static int luaA_drawin_set_width(lua_State *L, drawin_t *drawin) { - int width = luaA_checkinteger_range(L, -1, MIN_X11_SIZE, MAX_X11_SIZE); + int width = ceil(luaA_checknumber_range(L, -1, MIN_X11_SIZE, MAX_X11_SIZE)); drawin_moveresize(L, -3, (area_t) { .x = drawin->geometry.x, .y = drawin->geometry.y, .width = width, @@ -481,7 +483,7 @@ luaA_drawin_get_width(lua_State *L, drawin_t *drawin) static int luaA_drawin_set_height(lua_State *L, drawin_t *drawin) { - int height = luaA_checkinteger_range(L, -1, MIN_X11_SIZE, MAX_X11_SIZE); + int height = ceil(luaA_checknumber_range(L, -1, MIN_X11_SIZE, MAX_X11_SIZE)); drawin_moveresize(L, -3, (area_t) { .x = drawin->geometry.x, .y = drawin->geometry.y, .width = drawin->geometry.width, diff --git a/objects/window.c b/objects/window.c index 9ec1595b..15b582c8 100644 --- a/objects/window.c +++ b/objects/window.c @@ -476,7 +476,7 @@ window_translate_type(window_type_t type) static int luaA_window_set_border_width(lua_State *L, window_t *c) { - window_set_border_width(L, -3, luaA_checkinteger_range(L, -1, 0, MAX_X11_SIZE)); + window_set_border_width(L, -3, round(luaA_checknumber_range(L, -1, 0, MAX_X11_SIZE))); return 0; } diff --git a/objects/window.h b/objects/window.h index 3afcbb35..5386fafe 100644 --- a/objects/window.h +++ b/objects/window.h @@ -26,6 +26,7 @@ #include "common/luaclass.h" #include "objects/button.h" #include "strut.h" +#include "math.h" /** Windows type */ typedef enum diff --git a/root.c b/root.c index aa31cf1e..99ebdb1c 100644 --- a/root.c +++ b/root.c @@ -34,6 +34,8 @@ #include "objects/button.h" #include "xwindow.h" +#include "math.h" + #include #include #include @@ -266,8 +268,8 @@ luaA_root_fake_input(lua_State *L) { type = XCB_MOTION_NOTIFY; detail = luaA_checkboolean(L, 2); /* relative to the current position or not */ - x = luaA_checkinteger_range(L, 3, MIN_X11_COORDINATE, MAX_X11_COORDINATE); - y = luaA_checkinteger_range(L, 4, MIN_X11_COORDINATE, MAX_X11_COORDINATE); + x = round(luaA_checknumber_range(L, 3, MIN_X11_COORDINATE, MAX_X11_COORDINATE)); + y = round(luaA_checknumber_range(L, 4, MIN_X11_COORDINATE, MAX_X11_COORDINATE)); } else return 0; diff --git a/strut.c b/strut.c index b90c7518..8d46de34 100644 --- a/strut.c +++ b/strut.c @@ -21,6 +21,7 @@ #include "strut.h" #include "luaa.h" +#include "math.h" /** Push a strut type to a table on stack. * \param L The Lua VM state. @@ -51,10 +52,10 @@ void luaA_tostrut(lua_State *L, int idx, strut_t *strut) { luaA_checktable(L, idx); - strut->left = luaA_getopt_integer_range(L, idx, "left", strut->left, 0, UINT16_MAX); - strut->right = luaA_getopt_integer_range(L, idx, "right", strut->right, 0, UINT16_MAX); - strut->top = luaA_getopt_integer_range(L, idx, "top", strut->top, 0, UINT16_MAX); - strut->bottom = luaA_getopt_integer_range(L, idx, "bottom", strut->bottom, 0, UINT16_MAX); + strut->left = ceil(luaA_getopt_number_range(L, idx, "left", strut->left, 0, UINT16_MAX)); + strut->right = ceil(luaA_getopt_number_range(L, idx, "right", strut->right, 0, UINT16_MAX)); + strut->top = ceil(luaA_getopt_number_range(L, idx, "top", strut->top, 0, UINT16_MAX)); + strut->bottom = ceil(luaA_getopt_number_range(L, idx, "bottom", strut->bottom, 0, UINT16_MAX)); } // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:textwidth=80 diff --git a/systray.c b/systray.c index 105a5a04..cad77b5a 100644 --- a/systray.c +++ b/systray.c @@ -325,13 +325,13 @@ luaA_systray(lua_State *L) { size_t bg_len; drawin_t *w = luaA_checkudata(L, 1, &drawin_class); - int x = luaA_checkinteger_range(L, 2, MIN_X11_COORDINATE, MAX_X11_COORDINATE); - int y = luaA_checkinteger_range(L, 3, MIN_X11_COORDINATE, MAX_X11_COORDINATE); - int base_size = luaA_checkinteger_range(L, 4, MIN_X11_SIZE, MAX_X11_SIZE); + int x = round(luaA_checknumber_range(L, 2, MIN_X11_COORDINATE, MAX_X11_COORDINATE)); + int y = round(luaA_checknumber_range(L, 3, MIN_X11_COORDINATE, MAX_X11_COORDINATE)); + int base_size = ceil(luaA_checknumber_range(L, 4, MIN_X11_SIZE, MAX_X11_SIZE)); bool horiz = lua_toboolean(L, 5); const char *bg = luaL_checklstring(L, 6, &bg_len); bool revers = lua_toboolean(L, 7); - int spacing = luaA_checkinteger_range(L, 8, 0, MAX_X11_COORDINATE); + int spacing = ceil(luaA_checknumber_range(L, 8, 0, MAX_X11_COORDINATE)); color_t bg_color; bool force_redraw = false;