如何判断linux 显示服务器是xorg (x11) 还是wayland

背景

大家好,我是Tarzan。在使用Linux操作系统的时候我们需要注意自己的显示服务是xorg 还是wayland模式,因为有很多软件或者框架不支持或者兼容性不好,大部分都支持x11,但是wayland就不一定能用了。
那么我们怎么知道自己的display server呢?你在网上查的大多数是执行 echo $XDG_SESSION_TYPE,确实可以获取到,但是如果我们采用ssh连接的服务器,那么这个命令就没有参考价值了。

uos@uos-PC:~/Desktop$ echo $XDG_SESSION_TYPE
tty

解决办法

安装的系统不同,可能结果不同,仅当参考
其实很简单,我们知道显示服务它是个服务,那么肯定有进程啊!
如果远程的机器是x11那么

uos@uos-PC:~/Desktop/pyat_activator$ ps -ef |grep x11
uos       2451  2372  0 2月03 ?       01:14:29 kwin_x11 -platform dde-kwin-xcb:appFilePath=/usr/bin/kwin_no_scale

我们在x11机器上找wayland,很显然没有wayland任何信息

uos@uos-PC:~/Desktop/pyat_activator$ ps -ef |grep wayland
uos       4201  7379  0 15:12 pts/4    00:00:00 grep wayland

反过来:我们远程链接wayland机器上搜索x11,也是搜不到

uos@uosPC:~$ ps -ef|grep x11
uos  20433 20351  0 15:22 pts/1    00:00:00 grep x11

远程链接wayland机器上搜索wayland

uos-klu@uos-klu-PC:~$ ps -ef|grep wayland
uos-klu   3180  2677  0 2月20 ?       00:01:40 /usr/bin/kwin_wayland -platform dde-kwin-wayland --xwayland --drm --no-lockscreen startdde-wayland
uos-klu   3311  3180  0 2月20 ?       00:00:03 /usr/bin/Xwayland -displayfd 43 -rootless -wm 46
uos-klu  21603 20351  0 15:24 pts/1    00:00:00 grep wayland

总结:

def display_server():
    cmd = 'ps -ef |grep kwin_x11'
    res = subprocess.getoutput(cmd=cmd)
    if 'kwin_x11 -platform' in res:
        return 'x11'
    else:
        return 'wayland'

说明:目前此方法适用于UOS操作系统,查看了一下ubuntu和uos有点差异,但是可以大体看出ubuntu 22已经使用了wayland了
image

posted @ 2023-02-24 15:26  Tarzen  阅读(3276)  评论(4编辑  收藏  举报