Get Screen Width and Height

I have gtk+3.0, does anyone know how to get the screen width and height? this seems like it should be simple, on x11 it works fine, but Wayland it doesn’t work. It’s such a basic requirement I’m sure it must work and I’m just doing something wrong.

This works on x11…

      GdkRectangle workarea = {0};


I also tried gdk_monitor_get_geometry, the main problem it seams is that gdk_display_get_primary_monitor(display) returns null on Wayland. I read somewhere that it’s not implemented in Wayland. Ok, so how can I get width and height of the screen?

First of all, there’s no work area on Wayland. Second, as you noticed, there’s no primary monitor on Wayland either.

If you want to know the size and geometry of each monitor, you will need to iterate over them.

In practice, though, most of the cases in which you want to get the monitor geometry are generally suspicions, since you cannot do much with that knowledge; what are you trying to achieve?

im trying to provide my users an api to 1. get the width and height of the screen and then, if so desired 2. move the window within that space. Do you know of a simple example for iterating over them?

You can’t do that in Wayland. Each top level surface exist in an isolated coordinate space, with (0, 0) as the top-left corner of the surface itself. The only surfaces that can be moved around are popups, and they can only be moved within the coordinate space of their parent top level surface.

sorry, maybe i misunderstand, you cant move a window in wayland? :face_with_monocle:

You cannot programmatically move a top level window in Wayland, because you don’t have access to the global coordinate space.

The idea is that malicious applications cannot create an override redirect window, place it upon the password entry of your web browser or password manager, grab the keyboard and pointer devices, and swipe your passwords.

Only privileged components can have access to the whole of the display, meaning: only the compositor.

ah! ok thank you! that’s a very clear explanation and well reasoned decision.

what should i do to determine the initial size of my window? like, let’s say i want it to be 80% of the width and height of the screen. in that case should i iterate over the screens? if so, do you have a simple example or can you point me to one?

You should get the GdkSurface of your top level GtkWindow using the gtk_native_get_surface() function, and then call gdk_display_get_monitor_at_surface() to get the GdkMonitor. Once you have the GdkMonitor instance, you can get its size, and then call gtk_window_set_default_size().

1 Like

I think gtk_native_get_surface is only available in GTK4, but im stuck on GTK+3.0

For GTK3, you can get the GdkWindow out of the GtkWindow widget with gtk_widget_get_window(), and then use gdk_display_get_monitor_at_window().

1 Like

amazing, it works perfectly! thank you so much for all your help!! :beers:

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.