From 21b7b29630d19f946ea497defab3f4424d5f20a2 Mon Sep 17 00:00:00 2001 From: Uli Schlachter Date: Fri, 17 Aug 2018 09:58:30 +0200 Subject: [PATCH 1/2] xwindow_translate_for_gravity: Change instead of set argument Previously, this function overwrote the value of its argument with the result. After this change, the function merely changes the given variable by the calculated argument. Thus, the old behaviour is achieved by setting the variable to zero before the call, which all callers already did. However, for most callers this change means that a temporary variable can be removed and instead xwindow_translate_for_gravity() will directly change the target variable. No change in behaviour intended. Signed-off-by: Uli Schlachter --- event.c | 8 ++------ objects/client.c | 10 ++-------- xwindow.c | 8 ++++---- 3 files changed, 8 insertions(+), 18 deletions(-) diff --git a/event.c b/event.c index 935f8bc4b..9ab676538 100644 --- a/event.c +++ b/event.c @@ -345,17 +345,13 @@ event_handle_configurerequest(xcb_configure_request_event_t *ev) if(ev->value_mask & XCB_CONFIG_WINDOW_X) { - int16_t diff = 0; geometry.x = ev->x; - xwindow_translate_for_gravity(c->size_hints.win_gravity, deco_left, 0, deco_right, 0, &diff, NULL); - geometry.x += diff; + xwindow_translate_for_gravity(c->size_hints.win_gravity, deco_left, 0, deco_right, 0, &geometry.x, NULL); } if(ev->value_mask & XCB_CONFIG_WINDOW_Y) { - int16_t diff = 0; geometry.y = ev->y; - xwindow_translate_for_gravity(c->size_hints.win_gravity, 0, deco_top, 0, deco_bottom, NULL, &diff); - geometry.y += diff; + xwindow_translate_for_gravity(c->size_hints.win_gravity, 0, deco_top, 0, deco_bottom, NULL, &geometry.y); } if(ev->value_mask & XCB_CONFIG_WINDOW_WIDTH) { diff --git a/objects/client.c b/objects/client.c index 817205638..5e51ba718 100644 --- a/objects/client.c +++ b/objects/client.c @@ -1387,12 +1387,9 @@ border_width_callback(client_t *c, uint16_t old_width, uint16_t new_width) { area_t geometry = c->geometry; int16_t diff = new_width - old_width; - int16_t diff_x = 0, diff_y = 0; xwindow_translate_for_gravity(c->size_hints.win_gravity, diff, diff, diff, diff, - &diff_x, &diff_y); - geometry.x += diff_x; - geometry.y += diff_y; + &geometry.x, &geometry.y); /* inform client about changes */ client_resize_do(c, geometry); } @@ -2870,13 +2867,10 @@ titlebar_resize(lua_State *L, int cidx, client_t *c, client_titlebar_t bar, int if(c->size_hints.flags & XCB_ICCCM_SIZE_HINT_P_WIN_GRAVITY) { - int16_t diff_x = 0, diff_y = 0; xwindow_translate_for_gravity(c->size_hints.win_gravity, diff_left, diff_top, diff_right, diff_bottom, - &diff_x, &diff_y); - geometry.x += diff_x; - geometry.y += diff_y; + &geometry.x, &geometry.y); } c->titlebar[bar].size = size; diff --git a/xwindow.c b/xwindow.c index c5ff9e722..fdcd50566 100644 --- a/xwindow.c +++ b/xwindow.c @@ -400,8 +400,8 @@ xwindow_set_shape(xcb_window_t win, int width, int height, enum xcb_shape_sk_t k * \param change_height_before The window height difference that will be applied. * \param change_width_after The window width difference that will be applied. * \param change_height_after The window height difference that will be applied. - * \param dx On return, this will be set to the amount the pixel has to be moved. - * \param dy On return, this will be set to the amount the pixel has to be moved. + * \param dx On return, this will be changed by the amount the pixel has to be moved. + * \param dy On return, this will be changed by the amount the pixel has to be moved. */ void xwindow_translate_for_gravity(xcb_gravity_t gravity, int16_t change_width_before, int16_t change_height_before, int16_t change_width_after, int16_t change_height_after, int16_t *dx, int16_t *dy) @@ -449,9 +449,9 @@ void xwindow_translate_for_gravity(xcb_gravity_t gravity, int16_t change_width_b } if (dx) - *dx = x; + *dx += x; if (dy) - *dy = y; + *dy += y; } // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:textwidth=80 From a996b21ee2a63d16b136374d9f5709652b4b8781 Mon Sep 17 00:00:00 2001 From: Uli Schlachter Date: Fri, 17 Aug 2018 11:48:49 +0200 Subject: [PATCH 2/2] test-gravity.c: Accept different roundings Some window gravities require a division by two. Up to now, test-gravity.c expected this division to always be rounded up. This commit changes the code to also allow rounding down. Signed-off-by: Uli Schlachter --- tests/test-gravity.c | 33 +++++++++++++++++++++------------ 1 file changed, 21 insertions(+), 12 deletions(-) diff --git a/tests/test-gravity.c b/tests/test-gravity.c index 047b2794e..281bcb484 100644 --- a/tests/test-gravity.c +++ b/tests/test-gravity.c @@ -244,6 +244,14 @@ static const char *state_to_string(enum test_state state) } } +static int32_t div2(int32_t value, int32_t *rounding) +{ + /* If value is odd, we could round up or down */ + if (value & 1) + *rounding = 1; + return value / 2; +} + static void check_geometry(int32_t expected_x, int32_t expected_y, uint32_t expected_width, uint32_t expected_height) { int32_t actual_x, actual_y; @@ -252,6 +260,7 @@ static void check_geometry(int32_t expected_x, int32_t expected_y, uint32_t expe &left, &right, &top, &bottom); int32_t offset_x, offset_y; + int32_t extra_x = 0, extra_y = 0; int32_t diff_x = state_difference[window_state.state][0], diff_y = state_difference[window_state.state][1]; switch (window_state.gravity) { @@ -260,7 +269,7 @@ static void check_geometry(int32_t expected_x, int32_t expected_y, uint32_t expe offset_y = top; break; case XCB_GRAVITY_NORTH: - offset_x = (left - right + diff_x + 1) / 2; + offset_x = div2(left - right + diff_x, &extra_x);; offset_y = top; break; case XCB_GRAVITY_NORTH_EAST: @@ -269,22 +278,22 @@ static void check_geometry(int32_t expected_x, int32_t expected_y, uint32_t expe break; case XCB_GRAVITY_WEST: offset_x = left; - offset_y = (top - bottom + diff_y + 1) / 2; + offset_y = div2(top - bottom + diff_y, &extra_y); break; case XCB_GRAVITY_CENTER: - offset_x = (left - right + diff_x + 1) / 2; - offset_y = (top - bottom + diff_y + 1) / 2; + offset_x = div2(left - right + diff_x, &extra_x); + offset_y = div2(top - bottom + diff_y, &extra_y); break; case XCB_GRAVITY_EAST: offset_x = -right + diff_x; - offset_y = (top - bottom + diff_y + 1) / 2; + offset_y = div2(top - bottom + diff_y, &extra_y); break; case XCB_GRAVITY_SOUTH_WEST: offset_x = left; offset_y = -bottom + diff_y; break; case XCB_GRAVITY_SOUTH: - offset_x = (left - right + diff_x + 1) / 2; + offset_x = div2(left - right + diff_x, &extra_x); offset_y = -bottom + diff_y; break; case XCB_GRAVITY_SOUTH_EAST: @@ -312,12 +321,12 @@ static void check_geometry(int32_t expected_x, int32_t expected_y, uint32_t expe check(expected_height == actual_height, "For window with gravity %s in state %s, expected height = %d, but got %d", gravity_to_string(window_state.gravity), state_to_string(window_state.state), (int) expected_height, (int) actual_height); - check(expected_x + offset_x == actual_x, - "For window with gravity %s in state %s, expected x = %d+%d, but got %d", - gravity_to_string(window_state.gravity), state_to_string(window_state.state), (int) expected_x, (int) offset_x, (int) actual_x); - check(expected_y + offset_y == actual_y, - "For window with gravity %s in state %s, expected y = %d+%d, but got %d", - gravity_to_string(window_state.gravity), state_to_string(window_state.state), (int) expected_y, (int) offset_y, (int) actual_y); + check(expected_x + offset_x == actual_x || expected_x + offset_x + extra_x == actual_x, + "For window with gravity %s in state %s, expected x = %d+%d (+%d), but got %d", + gravity_to_string(window_state.gravity), state_to_string(window_state.state), (int) expected_x, (int) offset_x, (int) extra_x, (int) actual_x); + check(expected_y + offset_y == actual_y || expected_y + offset_y + extra_y == actual_y, + "For window with gravity %s in state %s, expected y = %d+%d (+%d), but got %d", + gravity_to_string(window_state.gravity), state_to_string(window_state.state), (int) expected_y, (int) offset_y, (int) extra_y, (int) actual_y); } static void init(xcb_gravity_t gravity)