Browse Source

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)
Emmanuel Lepage Vallee 3 years ago
parent
commit
9991f9ccc8
7 changed files with 34 additions and 26 deletions
  1. 9
    7
      objects/client.c
  2. 10
    8
      objects/drawin.c
  3. 1
    1
      objects/window.c
  4. 1
    0
      objects/window.h
  5. 4
    2
      root.c
  6. 5
    4
      strut.c
  7. 4
    4
      systray.c

+ 9
- 7
objects/client.c View File

@@ -98,6 +98,8 @@
98 98
 #include "systray.h"
99 99
 #include "xwindow.h"
100 100
 
101
+#include "math.h"
102
+
101 103
 #include <xcb/xcb_atom.h>
102 104
 #include <xcb/shape.h>
103 105
 #include <cairo-xcb.h>
@@ -2538,7 +2540,7 @@ luaA_client_titlebar_ ## name(lua_State *L)                       \
2538 2540
         if (lua_isnil(L, 2))                                      \
2539 2541
             titlebar_resize(L, 1, c, index, 0);                   \
2540 2542
         else                                                      \
2541
-            titlebar_resize(L, 1, c, index, luaA_checkinteger_range(L, 2, 0, MAX_X11_SIZE)); \
2543
+            titlebar_resize(L, 1, c, index, ceil(luaA_checknumber_range(L, 2, 0, MAX_X11_SIZE))); \
2542 2544
     }                                                             \
2543 2545
                                                                   \
2544 2546
     luaA_object_push_item(L, 1, titlebar_get_drawable(L, c, 1, index)); \
@@ -2566,8 +2568,8 @@ luaA_client_geometry(lua_State *L)
2566 2568
         area_t geometry;
2567 2569
 
2568 2570
         luaA_checktable(L, 2);
2569
-        geometry.x = luaA_getopt_integer_range(L, 2, "x", c->geometry.x, MIN_X11_COORDINATE, MAX_X11_COORDINATE);
2570
-        geometry.y = luaA_getopt_integer_range(L, 2, "y", c->geometry.y, MIN_X11_COORDINATE, MAX_X11_COORDINATE);
2571
+        geometry.x = round(luaA_getopt_number_range(L, 2, "x", c->geometry.x, MIN_X11_COORDINATE, MAX_X11_COORDINATE));
2572
+        geometry.y = round(luaA_getopt_number_range(L, 2, "y", c->geometry.y, MIN_X11_COORDINATE, MAX_X11_COORDINATE));
2571 2573
         if(client_isfixed(c))
2572 2574
         {
2573 2575
             geometry.width = c->geometry.width;
@@ -2575,8 +2577,8 @@ luaA_client_geometry(lua_State *L)
2575 2577
         }
2576 2578
         else
2577 2579
         {
2578
-            geometry.width = luaA_getopt_integer_range(L, 2, "width", c->geometry.width, MIN_X11_SIZE, MAX_X11_SIZE);
2579
-            geometry.height = luaA_getopt_integer_range(L, 2, "height", c->geometry.height, MIN_X11_SIZE, MAX_X11_SIZE);
2580
+            geometry.width = ceil(luaA_getopt_number_range(L, 2, "width", c->geometry.width, MIN_X11_SIZE, MAX_X11_SIZE));
2581
+            geometry.height = ceil(luaA_getopt_number_range(L, 2, "height", c->geometry.height, MIN_X11_SIZE, MAX_X11_SIZE));
2580 2582
         }
2581 2583
 
2582 2584
         client_resize(c, geometry, c->size_hints_honor);
@@ -2600,8 +2602,8 @@ luaA_client_apply_size_hints(lua_State *L)
2600 2602
     area_t geometry = c->geometry;
2601 2603
     if(!client_isfixed(c))
2602 2604
     {
2603
-        geometry.width = luaA_checkinteger_range(L, 2, MIN_X11_SIZE, MAX_X11_SIZE);
2604
-        geometry.height = luaA_checkinteger_range(L, 3, MIN_X11_SIZE, MAX_X11_SIZE);
2605
+        geometry.width = ceil(luaA_checknumber_range(L, 2, MIN_X11_SIZE, MAX_X11_SIZE));
2606
+        geometry.height = ceil(luaA_checknumber_range(L, 3, MIN_X11_SIZE, MAX_X11_SIZE));
2605 2607
     }
2606 2608
 
2607 2609
     if (c->size_hints_honor)

+ 10
- 8
objects/drawin.c View File

@@ -42,6 +42,8 @@
42 42
 #include "systray.h"
43 43
 #include "xwindow.h"
44 44
 
45
+#include "math.h"
46
+
45 47
 #include <cairo-xcb.h>
46 48
 #include <xcb/shape.h>
47 49
 
@@ -407,10 +409,10 @@ luaA_drawin_geometry(lua_State *L)
407 409
         area_t wingeom;
408 410
 
409 411
         luaA_checktable(L, 2);
410
-        wingeom.x = luaA_getopt_integer_range(L, 2, "x", drawin->geometry.x, MIN_X11_COORDINATE, MAX_X11_COORDINATE);
411
-        wingeom.y = luaA_getopt_integer_range(L, 2, "y", drawin->geometry.y, MIN_X11_COORDINATE, MAX_X11_COORDINATE);
412
-        wingeom.width = luaA_getopt_integer_range(L, 2, "width", drawin->geometry.width, MIN_X11_SIZE, MAX_X11_SIZE);
413
-        wingeom.height = luaA_getopt_integer_range(L, 2, "height", drawin->geometry.height, MIN_X11_SIZE, MAX_X11_SIZE);
412
+        wingeom.x = round(luaA_getopt_number_range(L, 2, "x", drawin->geometry.x, MIN_X11_COORDINATE, MAX_X11_COORDINATE));
413
+        wingeom.y = round(luaA_getopt_number_range(L, 2, "y", drawin->geometry.y, MIN_X11_COORDINATE, MAX_X11_COORDINATE));
414
+        wingeom.width = ceil(luaA_getopt_number_range(L, 2, "width", drawin->geometry.width, MIN_X11_SIZE, MAX_X11_SIZE));
415
+        wingeom.height = ceil(luaA_getopt_number_range(L, 2, "height", drawin->geometry.height, MIN_X11_SIZE, MAX_X11_SIZE));
414 416
 
415 417
         if(wingeom.width > 0 && wingeom.height > 0)
416 418
             drawin_moveresize(L, 1, wingeom);
@@ -427,7 +429,7 @@ LUA_OBJECT_EXPORT_PROPERTY(drawin, drawin_t, visible, lua_pushboolean)
427 429
 static int
428 430
 luaA_drawin_set_x(lua_State *L, drawin_t *drawin)
429 431
 {
430
-    int x = luaA_checkinteger_range(L, -1, MIN_X11_COORDINATE, MAX_X11_COORDINATE);
432
+    int x = round(luaA_checknumber_range(L, -1, MIN_X11_COORDINATE, MAX_X11_COORDINATE));
431 433
     drawin_moveresize(L, -3, (area_t) { .x = x,
432 434
                                         .y = drawin->geometry.y,
433 435
                                         .width = drawin->geometry.width,
@@ -445,7 +447,7 @@ luaA_drawin_get_x(lua_State *L, drawin_t *drawin)
445 447
 static int
446 448
 luaA_drawin_set_y(lua_State *L, drawin_t *drawin)
447 449
 {
448
-    int y = luaA_checkinteger_range(L, -1, MIN_X11_COORDINATE, MAX_X11_COORDINATE);
450
+    int y = round(luaA_checknumber_range(L, -1, MIN_X11_COORDINATE, MAX_X11_COORDINATE));
449 451
     drawin_moveresize(L, -3, (area_t) { .x = drawin->geometry.x,
450 452
                                         .y = y,
451 453
                                         .width = drawin->geometry.width,
@@ -463,7 +465,7 @@ luaA_drawin_get_y(lua_State *L, drawin_t *drawin)
463 465
 static int
464 466
 luaA_drawin_set_width(lua_State *L, drawin_t *drawin)
465 467
 {
466
-    int width = luaA_checkinteger_range(L, -1, MIN_X11_SIZE, MAX_X11_SIZE);
468
+    int width = ceil(luaA_checknumber_range(L, -1, MIN_X11_SIZE, MAX_X11_SIZE));
467 469
     drawin_moveresize(L, -3, (area_t) { .x = drawin->geometry.x,
468 470
                                         .y = drawin->geometry.y,
469 471
                                         .width = width,
@@ -481,7 +483,7 @@ luaA_drawin_get_width(lua_State *L, drawin_t *drawin)
481 483
 static int
482 484
 luaA_drawin_set_height(lua_State *L, drawin_t *drawin)
483 485
 {
484
-    int height = luaA_checkinteger_range(L, -1, MIN_X11_SIZE, MAX_X11_SIZE);
486
+    int height = ceil(luaA_checknumber_range(L, -1, MIN_X11_SIZE, MAX_X11_SIZE));
485 487
     drawin_moveresize(L, -3, (area_t) { .x = drawin->geometry.x,
486 488
                                        .y = drawin->geometry.y,
487 489
                                        .width = drawin->geometry.width,

+ 1
- 1
objects/window.c View File

@@ -476,7 +476,7 @@ window_translate_type(window_type_t type)
476 476
 static int
477 477
 luaA_window_set_border_width(lua_State *L, window_t *c)
478 478
 {
479
-    window_set_border_width(L, -3, luaA_checkinteger_range(L, -1, 0, MAX_X11_SIZE));
479
+    window_set_border_width(L, -3, round(luaA_checknumber_range(L, -1, 0, MAX_X11_SIZE)));
480 480
     return 0;
481 481
 }
482 482
 

+ 1
- 0
objects/window.h View File

@@ -26,6 +26,7 @@
26 26
 #include "common/luaclass.h"
27 27
 #include "objects/button.h"
28 28
 #include "strut.h"
29
+#include "math.h"
29 30
 
30 31
 /** Windows type */
31 32
 typedef enum

+ 4
- 2
root.c View File

@@ -34,6 +34,8 @@
34 34
 #include "objects/button.h"
35 35
 #include "xwindow.h"
36 36
 
37
+#include "math.h"
38
+
37 39
 #include <xcb/xtest.h>
38 40
 #include <xcb/xcb_aux.h>
39 41
 #include <cairo-xcb.h>
@@ -266,8 +268,8 @@ luaA_root_fake_input(lua_State *L)
266 268
     {
267 269
         type = XCB_MOTION_NOTIFY;
268 270
         detail = luaA_checkboolean(L, 2); /* relative to the current position or not */
269
-        x = luaA_checkinteger_range(L, 3, MIN_X11_COORDINATE, MAX_X11_COORDINATE);
270
-        y = luaA_checkinteger_range(L, 4, MIN_X11_COORDINATE, MAX_X11_COORDINATE);
271
+        x = round(luaA_checknumber_range(L, 3, MIN_X11_COORDINATE, MAX_X11_COORDINATE));
272
+        y = round(luaA_checknumber_range(L, 4, MIN_X11_COORDINATE, MAX_X11_COORDINATE));
271 273
     }
272 274
     else
273 275
         return 0;

+ 5
- 4
strut.c View File

@@ -21,6 +21,7 @@
21 21
 
22 22
 #include "strut.h"
23 23
 #include "luaa.h"
24
+#include "math.h"
24 25
 
25 26
 /** Push a strut type to a table on stack.
26 27
  * \param L The Lua VM state.
@@ -51,10 +52,10 @@ void
51 52
 luaA_tostrut(lua_State *L, int idx, strut_t *strut)
52 53
 {
53 54
     luaA_checktable(L, idx);
54
-    strut->left = luaA_getopt_integer_range(L, idx, "left", strut->left, 0, UINT16_MAX);
55
-    strut->right = luaA_getopt_integer_range(L, idx, "right", strut->right, 0, UINT16_MAX);
56
-    strut->top = luaA_getopt_integer_range(L, idx, "top", strut->top, 0, UINT16_MAX);
57
-    strut->bottom = luaA_getopt_integer_range(L, idx, "bottom", strut->bottom, 0, UINT16_MAX);
55
+    strut->left = ceil(luaA_getopt_number_range(L, idx, "left", strut->left, 0, UINT16_MAX));
56
+    strut->right = ceil(luaA_getopt_number_range(L, idx, "right", strut->right, 0, UINT16_MAX));
57
+    strut->top = ceil(luaA_getopt_number_range(L, idx, "top", strut->top, 0, UINT16_MAX));
58
+    strut->bottom = ceil(luaA_getopt_number_range(L, idx, "bottom", strut->bottom, 0, UINT16_MAX));
58 59
 }
59 60
 
60 61
 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:textwidth=80

+ 4
- 4
systray.c View File

@@ -325,13 +325,13 @@ luaA_systray(lua_State *L)
325 325
     {
326 326
         size_t bg_len;
327 327
         drawin_t *w = luaA_checkudata(L, 1, &drawin_class);
328
-        int x = luaA_checkinteger_range(L, 2, MIN_X11_COORDINATE, MAX_X11_COORDINATE);
329
-        int y = luaA_checkinteger_range(L, 3, MIN_X11_COORDINATE, MAX_X11_COORDINATE);
330
-        int base_size = luaA_checkinteger_range(L, 4, MIN_X11_SIZE, MAX_X11_SIZE);
328
+        int x = round(luaA_checknumber_range(L, 2, MIN_X11_COORDINATE, MAX_X11_COORDINATE));
329
+        int y = round(luaA_checknumber_range(L, 3, MIN_X11_COORDINATE, MAX_X11_COORDINATE));
330
+        int base_size = ceil(luaA_checknumber_range(L, 4, MIN_X11_SIZE, MAX_X11_SIZE));
331 331
         bool horiz = lua_toboolean(L, 5);
332 332
         const char *bg = luaL_checklstring(L, 6, &bg_len);
333 333
         bool revers = lua_toboolean(L, 7);
334
-        int spacing = luaA_checkinteger_range(L, 8, 0, MAX_X11_COORDINATE);
334
+        int spacing = ceil(luaA_checknumber_range(L, 8, 0, MAX_X11_COORDINATE));
335 335
         color_t bg_color;
336 336
         bool force_redraw = false;
337 337
 

Loading…
Cancel
Save