You are not logged in.

#1 2025-08-20 07:08:42

johnraff
nullglob
From: Nagoya, Japan
Registered: 2015-09-09
Posts: 12,553
Website

Detecting session type - X11 or Wayland

This topic keeps coming up. If we want to be able to use the same BL utilities in X11 or Wayland sessions, sometimes they have to adjust their behaviour according to the type of session they're in. It's come up already with eg xwwall and bl-exit and probably many more soon enough.

https://forums.bunsenlabs.org/viewtopic … 13#p145013
https://forums.bunsenlabs.org/viewtopic … 70#p145170
https://forums.bunsenlabs.org/viewtopic … 62#p144962

micko01 wrote:

It would be good to "standardise" a test to use anywhere, maybe even set something in our own environment?

The difficulty depends on whether you're making a test for BL only, or something portable enough to run on different systems (eg xwwall).

Possible tests that have come up:

1) Check the environment variables $DISPLAY and $WAYLAND_DISPLAY. Just be aware that if xwayland is running (it usually is) then both of those will be set in a Wayland session. A check like this should be OK:

    if [[ -n $WAYLAND_DISPLAY ]]
    then
        session_type='wayland'
    elif [[ -n $DISPLAY ]]
    then
        session_type='xorg'
    else
        : # check if on a terminal
    fi

Unfortunately, WAYLAND_SESSION is...

micko01 wrote:

not always reliably set in sway, and I assume in other compositors.

But as BL uses labwc, that might not be an issue for us.

2) Check the envvar $XDG_SESSION_TYPE, which usually returns "wayland"  "x11" or "tty". Unfortunately, it returns "tty" on an X11 session started from 'startx'. It's OK with 'labwc' from a terminal though.

3)

loginctl show-session $XDG_SESSION_ID -p Type

This is probably the same under the hood as 2) and also reports 'tty' for X sessions launched from a terminal with 'startx'.

4) If wlr-randr (small utility that ships with BL for Wayland) will run, it means it's found access to a Wayland server, so we can test:

if wlr-randr >/dev/null 2>&1
then
    session_type=wayland
fi

The snag is that even on a tty it will say yes as long as there's a Wayland compositor running somewhere.

5) If we're just dealing with a BL session then it should be very easy though! The BL session script of course knows if it's trying to start up a Wayland or X11 session and we can use Micko's idea of setting an environment variable BUNSEN_SESSION_TYPE or whatever, and all other utilities can refer to it.

I have to leave it here today, but any comments are welcome. smile


...elevator in the Brain Hotel, broken down but just as well...
( a boring Japan blog (currently paused), now on Bluesky, there's also some GitStuff )

Introduction to the Bunsenlabs Boron Desktop

Offline

#2 2025-08-20 13:34:53

Sector11
Mod Squid Tpyo Knig
From: Upstairs
Registered: 2015-08-20
Posts: 8,010

Re: Detecting session type - X11 or Wayland

{read mode off}

My first thought it to make it work in BL and state as much.
Interested parties can tweak it to their environment.

{read mod on}


Debian 12 Beardog, SoxDog and still a Conky 1.9er

Offline

#3 2025-08-21 05:12:51

johnraff
nullglob
From: Nagoya, Japan
Registered: 2015-09-09
Posts: 12,553
Website

Re: Detecting session type - X11 or Wayland

johnraff wrote:

5) The BL session script of course knows if it's trying to start up a Wayland or X11 session and we can use Micko's idea of setting an environment variable BUNSEN_SESSION_TYPE or whatever, and all other utilities can refer to it.

We're almost doing this already - in a BL Wayland session the envvar BL_SESSION is available and set to wayland.
At the moment it's only exported in the wayland case so in X11 'echo $BL_SESSION' returns nothing. All we'd need to do to implement this simple test is to export BL_SESSION in both Wayland and X11 sessions. ie edit this section of /usr/bin/bunsenlabs-session:

BL_SESSION=xorg

# check who calls here
case $0 in
    *bunsenlabs-session-wayland)
        BL_SESSION=wayland
        export BL_SESSION
    ;;
esac

to

BL_SESSION=xorg
export BL_SESSION

# check who calls here
case $0 in
    *bunsenlabs-session-wayland)
        BL_SESSION=wayland
    ;;
esac

Possibly rename BL_SESSION to the more obvious BUNSEN_SESSION_TYPE?


...elevator in the Brain Hotel, broken down but just as well...
( a boring Japan blog (currently paused), now on Bluesky, there's also some GitStuff )

Introduction to the Bunsenlabs Boron Desktop

Offline

#4 2025-08-21 10:18:49

micko01
void main()
From: Queensland, Australia
Registered: 2024-04-07
Posts: 492
Website

Re: Detecting session type - X11 or Wayland

I think the BUNSEN_SESSION_TYPE envar is a good idea (the name wink). I have always thought that the envar itself is a good idea.

This can easily be patched in to xwwall and still keep it distro agnostic (well, unless you dive into the code big_smile).

Many other apps can benefit from this approach too, including but certainly not limited to, bl-exit.


#!/bin/sh
echo '#include <stdio.h>\nvoid main() { printf("Hi, bunsenlabs\\n"); return; }' > bunsen.c
gcc bunsen.c -o bunsen
./bunsen

Offline

#5 2025-08-22 01:22:06

johnraff
nullglob
From: Nagoya, Japan
Registered: 2015-09-09
Posts: 12,553
Website

Re: Detecting session type - X11 or Wayland

^Looks like the way to go, and very easily applied - just a tiny edit to bunsenlabs-session.

I guess scripts that want to be portable to non-BL setups could check BUNSEN_SESSION_TYPE first (very fast) and move on to other tests if that envvar isn't set.

Also, trivial change but let's make the X11 value of BUNSEN_SESSION_TYPE "x11" to match $XDG_SESSION_TYPE and the loginctl test.


...elevator in the Brain Hotel, broken down but just as well...
( a boring Japan blog (currently paused), now on Bluesky, there's also some GitStuff )

Introduction to the Bunsenlabs Boron Desktop

Offline

#6 2025-08-30 06:29:03

johnraff
nullglob
From: Nagoya, Japan
Registered: 2015-09-09
Posts: 12,553
Website

Re: Detecting session type - X11 or Wayland

BL_SESSION renamed to BUNSEN_SESSION_TYPE and Xorg value set to 'x11' in bunsen-configs on github.
The envvar is now exported whether it's x11 or wayland.
These changes will go in the next upgrade of bunsen-configs.

Last edited by johnraff (2025-08-30 06:34:17)


...elevator in the Brain Hotel, broken down but just as well...
( a boring Japan blog (currently paused), now on Bluesky, there's also some GitStuff )

Introduction to the Bunsenlabs Boron Desktop

Offline

Board footer

Powered by FluxBB