MacOS monitor-added signal trigered too early
I've noticed that the monitor-added
signal is triggered too early, before the monitor geometry is established, resized or moved. While there is the monitors-changed
signal, this is triggered too often to be a reliable PnP signal and on both plug and unplug.
MWE.c: (adapted from #2004 (closed))
#include <glib.h>
#include <gtk/gtk.h>
void on_monitor_added (GdkDisplay *display,GdkMonitor *monitor,gpointer userdata)
{
GdkRectangle geo;
gdk_monitor_get_geometry ((GdkMonitor*) monitor,&geo);
gchar* Mes=g_strdup_printf ("Monitor added:\nx=%d y=%d\n(w=%d h=%d)",geo.x,geo.y,geo.width,geo.height);
g_message ("Monitor added: x=%d y=%d (w=%d h=%d)",geo.x,geo.y,geo.width,geo.height);
g_object_set ((GtkLabel*) userdata, "label", Mes, NULL);
}
int main(int argc, char **argv) {
gtk_init (&argc, &argv);
GtkWidget *win = gtk_window_new (GTK_WINDOW_TOPLEVEL);
GtkWidget *label = gtk_label_new ("Change the monitor configuration");
gtk_container_add (GTK_CONTAINER (win), label);
gtk_widget_show_all (win);
g_signal_connect_after (gdk_display_get_default(),
"monitor-added",
G_CALLBACK (on_monitor_added),
(gpointer) label);
gtk_main ();
return 0;
}
As you can see if you run this example, the geometry is (0,0,0,0)
after monitor-added
but if we use any other signal this gives the proper values.