From 681d9c4f1da7f51be7ef21dfb943cf9677785c4b Mon Sep 17 00:00:00 2001 From: Unrud Date: Thu, 29 Dec 2022 22:19:38 +0100 Subject: [PATCH] Improve XWayland detection --- inputcontrol/controller_x11.go | 39 ++++++++++++++++++++++++++-------- snap/snapcraft.yaml | 2 ++ 2 files changed, 32 insertions(+), 9 deletions(-) diff --git a/inputcontrol/controller_x11.go b/inputcontrol/controller_x11.go index 3d7c9f7..5ccf236 100644 --- a/inputcontrol/controller_x11.go +++ b/inputcontrol/controller_x11.go @@ -21,16 +21,19 @@ package inputcontrol -// #cgo LDFLAGS: -lX11 -lXtst +// #cgo LDFLAGS: -lX11 -lXrandr -lXtst // #include // #include +// #include // #include // #include +// Window MacroDefaultRootWindow(Display *dpy) { +// return DefaultRootWindow(dpy); +// } import "C" import ( "errors" - "fmt" - "os" + "strings" "sync" "time" "unsafe" @@ -55,17 +58,35 @@ func init() { } func InitX11Controller() (Controller, error) { - sessionType := os.Getenv("XDG_SESSION_TYPE") - if sessionType != "" && sessionType != "x11" { - return nil, UnsupportedPlatformError{errors.New(fmt.Sprintf( - "unsupported session type '%v'", sessionType))} - } display := C.XOpenDisplay(nil) if display == nil { return nil, UnsupportedPlatformError{ errors.New("failed to connect to X server")} } - return &x11Controller{display: display}, nil + p := &x11Controller{display: display} + if p.xIsXwayland() { + p.Close() + return nil, UnsupportedPlatformError{ + errors.New("X server is Xwayland")} + } + return p, nil +} + +func (p *x11Controller) xIsXwayland() bool { + resources := C.XRRGetScreenResourcesCurrent(p.display, C.MacroDefaultRootWindow(p.display)) + if resources == nil { + return false + } + defer C.XRRFreeScreenResources(resources) + if resources.noutput < 1 { + return false + } + output := C.XRRGetOutputInfo(p.display, resources, *resources.outputs) + if output == nil { + return false + } + defer C.XRRFreeOutputInfo(output) + return strings.HasPrefix(C.GoString(output.name), "XWAYLAND") } func (p *x11Controller) Close() error { diff --git a/snap/snapcraft.yaml b/snap/snapcraft.yaml index c7f1d92..26fa53a 100644 --- a/snap/snapcraft.yaml +++ b/snap/snapcraft.yaml @@ -40,9 +40,11 @@ parts: - gcc - libc6-dev - libxt-dev + - libxrandr-dev - libxtst-dev stage-packages: - libxt6 + - libxrandr2 - libxtst6 override-pull: | snapcraftctl pull