Shyam
(Shyam Butani)
February 11, 2025, 6:09am
1
I want to query how many physical monitors are present in my Gtk3 Linux application.
I wrote the code to print dimensions of GtkScreen and list of GtkMonitors that screen consist of.
Then I disconnected the only physical monitor from my system, and tried to run the program.
Surprisingly it gave dimensions of the GtkScreen as 1920x1080, and number of monitor as 1 and it’s dimensions as 1920x1080.
I don’t understand why gdk_screen_get_n_monitors returns number of monitors as 1 when there is no physical monitors connected to the system.
Is there any another way to get the number of monitors, which gives 0 when no monitors are present?
ebassi
(Emmanuele Bassi)
February 11, 2025, 8:04am
2
You did not specify if you’re using a long running process, and monitoring for changes; or you run a script, disconnect the display, and then run the script again.
GTK initialises the list of monitors with what information the windowing systems provides. If the underlying windowing system API (XRandR under X11) still lists the physical display as attached, then there’s nothing GTK can do about it.
Sid
(Sid)
February 11, 2025, 8:17am
3
If it helps, the following works fine for me.
#include <gtk/gtk.h>
int main()
{
GdkScreen *s;
GdkDisplay *d;
gtk_init (0, 0);
s = gdk_screen_get_default ();
d = gdk_display_get_default ();
g_print ("screen = %p, n_monitors = %d\n", s, gdk_screen_get_n_monitors (s));
g_print ("display = %p, n_monitors = %d\n\n", d, gdk_display_get_n_monitors (d));
return 0;
}
0
is displayed below as expected, when monitor is disconnected for a second and reconnected.
$ while [ 1 ]; do ./a.out; sleep 1; done;
screen = 0x55b01c788eb0, n_monitors = 1
display = 0x55b01c786bf0, n_monitors = 1
screen = 0x55954b341eb0, n_monitors = 1
display = 0x55954b33fbf0, n_monitors = 1
screen = 0x556a6feaceb0, n_monitors = 0
display = 0x556a6feaabf0, n_monitors = 0
screen = 0x5652513c9eb0, n_monitors = 0
display = 0x5652513c7bf0, n_monitors = 0
screen = 0x558fa9095eb0, n_monitors = 1
display = 0x558fa9093bf0, n_monitors = 1
screen = 0x55754482deb0, n_monitors = 1
display = 0x55754482bbf0, n_monitors = 1
Shyam
(Shyam Butani)
February 11, 2025, 11:05am
4
@ebassi We can use keyboard to run the application even if there is no physical monitor connected.
So, what I’m doing is:
Compile the code and get executable
Write command in Terminal to run that executable but don’t press ‘Enter’ key
Remove the physical monitor
Press ‘Enter’ key, which run the executable and prints the information of screen/monitor.
Attach monitor back to see what our executable has written as output.
Sid
(Sid)
February 12, 2025, 2:06pm
5
Above steps work fine here. So, the issue is lower in the stack as pointed out by Emmanuele.
One thing you can try is to run in a loop as shown above, to see if n_monitors
returns zero in a span of few seconds (due to some delay / race) when the monitor is disconnected. Less likely, but worth a shot.
system
(system)
Closed
March 14, 2025, 2:07pm
6
This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.