How can I reliably detect a Wayland or X11 session in an autostart script?

I’m doing some testing with Wayland and at the moment I’m oscillating between it and X11.
As some of the options in my autostart scripts at present cause problems in Wayland, I want to bypass those if I’m not running an X11 session.

The ways I’ve found so far to detect which is running are:

echo $DESKTOP_SESSION
echo $XDG_SESSION_TYPE
loginctl show-session $(loginctl | grep "$USER" | awk '{print $1}') -p Type

Any one of these works once I’ve logged out then back in, but none of them works in the first session logged in after a reboot. Hence my point abour reliably detecting the session type.

Has anyone come across this and/or found a workaround (other than just to log out after a reboot)?
Thanks.

[Edit]: unfortunately, commenters seem to be missing the point. That such things as $XDG_SESSION_TYPE are, for some reason, not available during the first login after a boot. (My suspicion is that this is some sort of SDDM bug).
So what I need is something that will work in an autostart script on the first login after a boot.

You could use what I use in the freerdp template [root tip] [Utility Script] FreeRDP script template

The script checks the current session and choose the correct binary

basically

if [[ ${XDG_SESSION_TYPE} = "wayland" ]]; then

else

fi

If you are not logged in then the session does not exist and the type is irrelevant.

The pacman-mirrors application (python) also checks the environment when it is run.
pacman_mirrors/functions/cliFn.py · master · Applications / pacman-mirrors · GitLab

        # wayland - sway - console - gtk
        if (
            os.environ.get("XDG_SESSION_TYPE") == "wayland"
            or os.environ.get("XDG_SESSION_TYPE") == "tty"
            or not os.environ.get("DISPLAY")
            or not gtk_available
        ):

            self.no_display = True
1 Like

Easystroke - X11 mouse gestures - shouldn’t launch on Wayland.

So I do this:

#!/bin/bash

is_wayland() {
  if [ "$XDG_SESSION_TYPE" = "wayland" ]; then
    return 0
  else
    return 1
  fi
}

if is_wayland; then
  echo "The session is running on Wayland. Easystroke will not be started."
else
  echo "The session is not running on Wayland. Starting Easystroke..."
  easystroke &
fi
3 Likes

I simply use two different scripts the wayland for wayland, the x11 for x11. BUT, I don’t use any DEs that might run optionally x11 or wayland.

Hi there @manyroads - I think you missed the ‘KDE Plasma’ flag in the title - OP wants to launch Plasma desktop with X11 and Wayland available, so ONE script in Autostart has to detect the desktop before deciding what to do.

You might consider upgrading your autostart routine - then just one script can cope with multiple situations.

As the OP asked how to detect a Wayland or X11 session, I slightly modified @Ben’s script so that it runs only when an x11 session is detected (yes, I know I’m being pedantic as, if it isn’t Wayland, it must be x11, but…):

#!/usr/bin/env bash

is_x11() {
  if [ "$XDG_SESSION_TYPE" = "x11" ]; then
    return 0
  else
    return 1
  fi
}

if is_x11; then
echo "The session is running on X11. Starting the command..."
# (uncomment line below to run the command)
#  thecommand &
else
  echo "The session is not running on X11. The command will not be started."
fi

I’ll answer my own question (for the benefit of anyone who might come along later needing to do the same thing) as unfortunately everyone has missed the problem that the suggested environment variables aren’t getting set for the first login after a reboot.

The answer is to run:
pgrep -f /usr/bin/startplasma-wayland
Then either test for the return code or count the number of lines returned.

There is wayland-detector (python script) which, as the name suggests, detects the current Wayland session.

If Wayland isn’t detected, we can presume that X11 is being used; although it would also be nice to detect when using only a TTY, which this script seems to lack.

Perhaps you can find a use for it.


An interesting Gnome shell extension is:


Note that I add this purely for the benefit of passers-by that might have an interest.

Regards.

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.