Detect graphical session with GTK/GDK?

Is there a way to detect presence of a graphical session using GTK / GDK ? Ie, does the current session have an X / Wayland server? I’m guessing probably not, given that most GNOME apps seem to just hang when the current session doesn’t have a GUI (e.g., trying to run gnome-terminal over an SSH connection).

The reason I ask is that I’ve got some old code that simply calls XOpenDisplay() and checks the result. This obviously needs to be changed to support Wayland, but I was hoping there was some GTK way of doing this.

Well, there’s gtk_init_check() (as opposed to gtk_init()). It will return false if it can’t open a display.

Well, I’ll be darned. gtk_init_check() does seem to work.

I was confused initially because I was SSHing to another machine and running tests there. I just realized that the graphical windows I was creating there actually appeared on the separate Wayland session that I have running there, even though my SSH session doesn’t have WAYLAND_DISPLAY set. That is why I believed they were hanging.