Center window in Weston

Published:
Published:

I found so many questions on StackOverflow without answer regarding this problem:

I even has started thinking about switching over to another compositor https://stackoverflow.com/questions/37730940/qt5-on-wayland-without-weston

But I successfully found a way to control the x,y position through weston/desktop-shell/shell.c in weston_view_set_initial_position(...). Now all windows appear centered.

diff --git a/desktop-shell/shell.c b/desktop-shell/shell.c
index f126393a..b494708b 100644
--- a/desktop-shell/shell.c
+++ b/desktop-shell/shell.c
@@ -4331,16 +4331,8 @@ weston_view_set_initial_position(struct weston_view *view,
         */
        get_output_work_area(shell, target_output, &area);

-       x = area.x;
-       y = area.y;
-       range_x = area.width - view->surface->width;
-       range_y = area.height - view->surface->height;
-
-       if (range_x > 0)
-               x += random() % range_x;
-
-       if (range_y > 0)
-               y += random() % range_y;
+       x = area.x + floor((view->surface->width - area.width) / 2.);
+       y = area.y + floor((view->surface->height - area.height) / 2.);

        weston_view_set_position(view, x, y);
 }

Rate this page