Improve XWayland detection

This commit is contained in:
Unrud 2022-12-29 22:19:38 +01:00
parent 2fc5d0825b
commit 681d9c4f1d
2 changed files with 32 additions and 9 deletions

View file

@ -21,16 +21,19 @@
package inputcontrol
// #cgo LDFLAGS: -lX11 -lXtst
// #cgo LDFLAGS: -lX11 -lXrandr -lXtst
// #include <X11/Xlib.h>
// #include <X11/Intrinsic.h>
// #include <X11/extensions/Xrandr.h>
// #include <X11/extensions/XTest.h>
// #include <X11/XKBlib.h>
// 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 {

View file

@ -40,9 +40,11 @@ parts:
- gcc
- libc6-dev
- libxt-dev
- libxrandr-dev
- libxtst-dev
stage-packages:
- libxt6
- libxrandr2
- libxtst6
override-pull: |
snapcraftctl pull