Detection of connection/disconnection of display

Is there a way using GTK to detect (get an event) when a monitor is connected/disconnected to the PC?
I tried connecting to seat_added but i get no event when a monitor is connected
I can poll all the displays and their attached monitors and detect a change myself but it is very inefficient.

You can think of a seat as a literal physical seat: It’s what a user has access to in a specific instance of using the computer — Note however multiple seats can exist (think remote desktop etc)

Display is a confusingly named concept - history happened - but does know about Monitors, which sounds like it’s what your want.

Does this mean that the only way to detect a change is by polling on the monitors attached to each display? Is there no event of changes in the number of monitors?

Just like the documentation for gdk_display_get_monitors() says: you connect to the items-changed signal on the GListModel returned by the function, and you’ll know if a monitor was added, removed, or changed.

1 Like

Thank you. I missed that bit.

I connected to the items-changed signal, and I get an event when there is a change but the data itself is not very helpful.

When a monitor is removed I get the index of the removed monitor but the list no longer contains that monitor. As I store the monitors in another list I can overcome this by checking which monitor is not in the updated list.

When a monitor is added it becomes an issue. The list (of monitors) appear to contain a new entry and i get its index but the monitor information is empty.
For example - if I call get_geometry on the newly connected monitor, it will return all 0.

Any idea how to get this list to refresh and include the newly added monitor information?

If your second list is stored in the same order as the monitor list, you can just remove the same index. To get the geometry, you will want to connect to the notify signals on each monitor, because the geometry could change.

The main issue is not with disconnect but rather with the connect.
The list appears to include a new entry for the connected monitor but it is all empty.

How the compositor decides to fill out the data is up to the compositor; GTK doesn’t do anything special about, and just relays data from the windowing system itself.

If that data gets updated asynchronously then you will need to connect to the notify signal for all the object properties you care about, like notify::geometry.

You can check if a monitor object is valid by using the GdkMonitor:valid read-only property.