[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: shell script: test for graphical session



On Tue, Oct 29, 2019 at 02:32:39PM +0900, John Crawley wrote:
> A (bash) script wants to put up a GUI (yad) if it can, or otherwise
> try to interact with the user on the terminal.
> 
> Previously I was using loginctl:
> loginctl show-session -p Type $XDG_SESSION_ID
> and looking for 'Type=x11' or 'Type=wayland'
> 
> However, if a user logs in on a tty and then runs 'startx' (I don't
> know if Wayland has an equivalent) then the above command returns
> 'Type=tty' even when X is running.
> ($XDG_SESSION_TYPE gives the same results.)
> 
> I've replaced the test with
> [[ -n $DISPLAY || -n $WAYLAND_DISPLAY ]] && <use GUI>
> but environment variables are a bit fragile and I was wondering if
> there might be a better way.

What is fragile about the environment?

First you'll have to find out what you are after. Note that there
may be several X sessions around in your box, that the X server
may be a remote one, that your application may be displaying things
on several X displays at once (I know, Gtk is, or used to be,
particularly buggy about that -- but we ain't going to go Microsoft's
way [1] about this, are we?

So yes, in the general case, there may be (way) more than one
graphical session to choose from: if you're looking for a criterion
to pick "the obvious one", then you'll have to specify your
criterion yourself.

Thus, environment is just about the right tool: a process inherits
its ancestor's environment -- and somewhere up the chain there is
the process "representing" the session (embodied in X by all that
Xsession stuff, where the variables are set).

The "startx" thing you mention is particularly misleading: the
shell where you'd issue "startx" from is *not" "in" the session!

Cheers

[1] declaring darkness as industry standard
    https://www.barrypopik.com/index.php/new_york_city/entry/how_many_microsoft_engineers_does_it_take

-- tomás

Attachment: signature.asc
Description: Digital signature


Reply to: