I’m calling this piece of code from the “realize” signal in a GTK3 program to get the resolution of the monitor where a window resides:
void on_window_realize(GtkWidget* widget, gpointer user_data) {
GdkWindow* gdk_window = gtk_widget_get_window(widget);
if (gdk_window == NULL) {
g_print("No Gdk window\n");
return;
}
GdkDisplay* display = gdk_window_get_display(gdk_window);
GdkMonitor* monitor = gdk_display_get_monitor_at_window(display, gdk_window);
GdkRectangle geometry;
gdk_monitor_get_geometry(monitor, &geometry);
g_print("Geometry: %d, %d, %d, %d\n", geometry.x, geometry.y, geometry.width, geometry.height);
}
Unfortunately, it always returns the geometry of the main monitor, not the one in which the window is. But if I call it “manually” using a button, it returns the right value. Also, if I use the “map” signal, I can see that the value changes in the second “map” event… This is the output of a test program that I did, when I launch it at the second monitor:
./test_window
Realize event
Geometry: 0, 0, 1920, 1080
Map event
Geometry: 0, 0, 1920, 1080
Map event
Geometry: 1920, 0, 1920, 1080
Map event
Geometry: 1920, 0, 1920, 1080
Map event
Geometry: 1920, 0, 1920, 1080
I tried using ‘connect_after’ for “realize”, I tried with “map” and “map-event” instead, and even I connected to “screen-change”, but no luck.
Is this a bug in Gnome Shell/mutter? A bug in Gtk? A bug specific to Gtk3? Or is just that I’m doing it wrong?
EDIT: I tested this only on Wayland.