Format code

This commit is contained in:
Unrud 2026-08-02 22:12:27 +02:00
parent 85b6d6ea9d
commit 6ef2be8776
6 changed files with 55 additions and 29 deletions

View file

@ -74,7 +74,8 @@ func findDefaultHost() string {
continue continue
} }
for _, linkLocalPrefix := range []string{ for _, linkLocalPrefix := range []string{
"169.254.", "fe8", "fe9", "fea", "feb"} { "169.254.", "fe8", "fe9", "fea", "feb",
} {
if strings.HasPrefix(ip.String(), linkLocalPrefix) { if strings.HasPrefix(ip.String(), linkLocalPrefix) {
addIP(20, ip) addIP(20, ip)
continue addrs continue addrs

View file

@ -21,8 +21,10 @@ package inputcontrol
import "sort" import "sort"
type PointerButton int type (
type Key int PointerButton int
Key int
)
const ( const (
PointerButtonLeft PointerButton = iota PointerButtonLeft PointerButton = iota

View file

@ -88,12 +88,14 @@ func InitPortalController() (Controller, error) {
"org.freedesktop.portal.RemoteDesktop.version") "org.freedesktop.portal.RemoteDesktop.version")
if err != nil { if err != nil {
return nil, &UnsupportedPlatformError{ return nil, &UnsupportedPlatformError{
fmt.Errorf("getting 'version' failed: %w", err)} fmt.Errorf("getting 'version' failed: %w", err),
}
} }
remoteDesktopVersion, ok := remoteDesktopVersionV.Value().(uint32) remoteDesktopVersion, ok := remoteDesktopVersionV.Value().(uint32)
if !ok { if !ok {
return nil, &UnsupportedPlatformError{ return nil, &UnsupportedPlatformError{
errors.New("unexpected 'version' type")} errors.New("unexpected 'version' type"),
}
} }
restoreTokenStore, err := func() (*secretStore, error) { restoreTokenStore, err := func() (*secretStore, error) {
if remoteDesktopVersion < 2 { if remoteDesktopVersion < 2 {
@ -103,7 +105,7 @@ func InitPortalController() (Controller, error) {
if err != nil { if err != nil {
return nil, err return nil, err
} }
if err := os.MkdirAll(cacheDirectory, 0700); err != nil { if err := os.MkdirAll(cacheDirectory, 0o700); err != nil {
return nil, err return nil, err
} }
secret, err := retrieveSecret(bus) secret, err := retrieveSecret(bus)
@ -120,17 +122,20 @@ func InitPortalController() (Controller, error) {
"org.freedesktop.portal.RemoteDesktop.AvailableDeviceTypes") "org.freedesktop.portal.RemoteDesktop.AvailableDeviceTypes")
if err != nil { if err != nil {
return nil, &UnsupportedPlatformError{ return nil, &UnsupportedPlatformError{
fmt.Errorf("getting 'AvailableDeviceTypes' failed: %w", err)} fmt.Errorf("getting 'AvailableDeviceTypes' failed: %w", err),
}
} }
availableDeviceTypes, ok := availableDeviceTypesV.Value().(uint32) availableDeviceTypes, ok := availableDeviceTypesV.Value().(uint32)
if !ok { if !ok {
return nil, &UnsupportedPlatformError{ return nil, &UnsupportedPlatformError{
errors.New("unexpected 'AvailableDeviceTypes' return type")} errors.New("unexpected 'AvailableDeviceTypes' return type"),
}
} }
if availableDeviceTypes&deviceKeyboard == 0 || if availableDeviceTypes&deviceKeyboard == 0 ||
availableDeviceTypes&devicePointer == 0 { availableDeviceTypes&devicePointer == 0 {
return nil, &UnsupportedPlatformError{ return nil, &UnsupportedPlatformError{
errors.New("keyboard or pointer source type not supported")} errors.New("keyboard or pointer source type not supported"),
}
} }
createSessionResults, err := checkResponse(getResponse(bus, portalDesktop, createSessionResults, err := checkResponse(getResponse(bus, portalDesktop,
"org.freedesktop.portal.RemoteDesktop.CreateSession", 0, "org.freedesktop.portal.RemoteDesktop.CreateSession", 0,
@ -138,12 +143,14 @@ func InitPortalController() (Controller, error) {
)) ))
if err != nil { if err != nil {
return nil, &UnsupportedPlatformError{ return nil, &UnsupportedPlatformError{
fmt.Errorf("calling 'CreateSession' failed: %w", err)} fmt.Errorf("calling 'CreateSession' failed: %w", err),
}
} }
sessionHandleString, ok := createSessionResults["session_handle"].Value().(string) sessionHandleString, ok := createSessionResults["session_handle"].Value().(string)
if !ok { if !ok {
return nil, &UnsupportedPlatformError{ return nil, &UnsupportedPlatformError{
errors.New("unexpected 'session_handle' type in 'CreateSession' return value")} errors.New("unexpected 'session_handle' type in 'CreateSession' return value"),
}
} }
sessionHandle := dbus.ObjectPath(sessionHandleString) sessionHandle := dbus.ObjectPath(sessionHandleString)
selectDevicesOptions := map[string]dbus.Variant{ selectDevicesOptions := map[string]dbus.Variant{
@ -165,7 +172,8 @@ func InitPortalController() (Controller, error) {
)) ))
if err != nil { if err != nil {
return nil, &UnsupportedPlatformError{ return nil, &UnsupportedPlatformError{
fmt.Errorf("calling 'SelectDevices' failed: %w", err)} fmt.Errorf("calling 'SelectDevices' failed: %w", err),
}
} }
startResponseStatus, startResults, err := getResponse(bus, portalDesktop, startResponseStatus, startResults, err := getResponse(bus, portalDesktop,
"org.freedesktop.portal.RemoteDesktop.Start", 0, "org.freedesktop.portal.RemoteDesktop.Start", 0,
@ -173,7 +181,8 @@ func InitPortalController() (Controller, error) {
) )
if err != nil { if err != nil {
return nil, &UnsupportedPlatformError{ return nil, &UnsupportedPlatformError{
fmt.Errorf("calling 'Start' failed: %w", err)} fmt.Errorf("calling 'Start' failed: %w", err),
}
} }
if startResponseStatus != 0 { if startResponseStatus != 0 {
return nil, errors.New("keyboard or pointer access denied") return nil, errors.New("keyboard or pointer access denied")
@ -186,14 +195,17 @@ func InitPortalController() (Controller, error) {
devices, ok := startResults["devices"].Value().(uint32) devices, ok := startResults["devices"].Value().(uint32)
if !ok { if !ok {
return nil, &UnsupportedPlatformError{ return nil, &UnsupportedPlatformError{
errors.New("unexpected 'devices' type in 'Start' return value")} errors.New("unexpected 'devices' type in 'Start' return value"),
}
} }
if devices&deviceKeyboard == 0 || devices&devicePointer == 0 { if devices&deviceKeyboard == 0 || devices&devicePointer == 0 {
return nil, errors.New("keyboard or pointer access denied") return nil, errors.New("keyboard or pointer access denied")
} }
cleanupBus = false cleanupBus = false
return &portalController{bus: bus, portalDesktop: portalDesktop, return &portalController{
sessionHandle: sessionHandle}, nil bus: bus, portalDesktop: portalDesktop,
sessionHandle: sessionHandle,
}, nil
} }
func retrieveSecret(bus *dbus.Conn) ([]byte, error) { func retrieveSecret(bus *dbus.Conn) ([]byte, error) {
@ -267,11 +279,12 @@ func (s *secretStore) Store(data []byte) error {
return err return err
} }
ciphertext := s.aesgcm.Seal(nil, nonce, data, nil) ciphertext := s.aesgcm.Seal(nil, nonce, data, nil)
return os.WriteFile(s.filename, slices.Concat(nonce, ciphertext), 0600) return os.WriteFile(s.filename, slices.Concat(nonce, ciphertext), 0o600)
} }
func getResponse(bus *dbus.Conn, object dbus.BusObject, method string, func getResponse(bus *dbus.Conn, object dbus.BusObject, method string,
flags dbus.Flags, args ...interface{}) (uint32, map[string]dbus.Variant, error) { flags dbus.Flags, args ...interface{},
) (uint32, map[string]dbus.Variant, error) {
ch := make(chan *dbus.Signal, 512) ch := make(chan *dbus.Signal, 512)
bus.Signal(ch) bus.Signal(ch)
defer bus.RemoveSignal(ch) defer bus.RemoveSignal(ch)

View file

@ -25,10 +25,11 @@ package inputcontrol
import ( import (
"errors" "errors"
"fmt" "fmt"
"github.com/bendahl/uinput"
"log" "log"
"os" "os"
"time" "time"
"github.com/bendahl/uinput"
) )
const shiftKeysDelay time.Duration = 50 * time.Millisecond const shiftKeysDelay time.Duration = 50 * time.Millisecond

View file

@ -32,6 +32,7 @@ package inputcontrol
// return DefaultRootWindow(dpy); // return DefaultRootWindow(dpy);
// } // }
import "C" import "C"
import ( import (
"errors" "errors"
"fmt" "fmt"
@ -46,8 +47,10 @@ const (
scrollDiv int = 20 scrollDiv int = 20
) )
var modifierIndices [6]uint = [...]uint{C.ShiftMapIndex, C.Mod1MapIndex, var modifierIndices [6]uint = [...]uint{
C.Mod2MapIndex, C.Mod3MapIndex, C.Mod4MapIndex, C.Mod5MapIndex} C.ShiftMapIndex, C.Mod1MapIndex,
C.Mod2MapIndex, C.Mod3MapIndex, C.Mod4MapIndex, C.Mod5MapIndex,
}
type x11Controller struct { type x11Controller struct {
display *C.Display display *C.Display
@ -63,13 +66,15 @@ func InitX11Controller() (Controller, error) {
display := C.XOpenDisplay(nil) display := C.XOpenDisplay(nil)
if display == nil { if display == nil {
return nil, &UnsupportedPlatformError{ return nil, &UnsupportedPlatformError{
errors.New("failed to connect to X server")} errors.New("failed to connect to X server"),
}
} }
p := &x11Controller{display: display} p := &x11Controller{display: display}
if p.xIsXwayland() { if p.xIsXwayland() {
p.Close() p.Close()
return nil, &UnsupportedPlatformError{ return nil, &UnsupportedPlatformError{
errors.New("X server is Xwayland")} errors.New("X server is Xwayland"),
}
} }
return p, nil return p, nil
} }
@ -140,7 +145,8 @@ keycodes:
} }
func (p *x11Controller) changeKeyMappingLocked(keysymsPerKeycode C.int, func (p *x11Controller) changeKeyMappingLocked(keysymsPerKeycode C.int,
keycode C.KeyCode, keysym Keysym) { keycode C.KeyCode, keysym Keysym,
) {
keycodeMapping := make([]C.KeySym, keysymsPerKeycode) keycodeMapping := make([]C.KeySym, keysymsPerKeycode)
for i := range keycodeMapping { for i := range keycodeMapping {
keycodeMapping[i] = C.KeySym(keysym) keycodeMapping[i] = C.KeySym(keysym)
@ -169,7 +175,8 @@ func (p *x11Controller) getModKeycodesLocked() map[uint]C.KeyCode {
func (p *x11Controller) findKeycodeLocked(keyboard C.XkbDescPtr, func (p *x11Controller) findKeycodeLocked(keyboard C.XkbDescPtr,
modKeycodes map[uint]C.KeyCode, activeMods C.uint, modKeycodes map[uint]C.KeyCode, activeMods C.uint,
keysym Keysym) (C.KeyCode, C.uint) { keysym Keysym,
) (C.KeyCode, C.uint) {
keycode := C.XKeysymToKeycode(p.display, C.KeySym(keysym)) keycode := C.XKeysymToKeycode(p.display, C.KeySym(keysym))
if keycode == 0 { if keycode == 0 {
return 0, 0 return 0, 0
@ -204,7 +211,8 @@ func (p *x11Controller) findKeycodeLocked(keyboard C.XkbDescPtr,
} }
func (p *x11Controller) sendModsLocked(modKeycodes map[uint]C.KeyCode, mods C.uint, func (p *x11Controller) sendModsLocked(modKeycodes map[uint]C.KeyCode, mods C.uint,
press bool) { press bool,
) {
var pressC C.int = C.False var pressC C.int = C.False
if press { if press {
pressC = C.True pressC = C.True

View file

@ -27,9 +27,6 @@ import (
"errors" "errors"
"flag" "flag"
"fmt" "fmt"
"github.com/unrud/remote-touchpad/inputcontrol"
"github.com/unrud/remote-touchpad/terminal"
"golang.org/x/net/websocket"
"log" "log"
mathrand "math/rand" mathrand "math/rand"
"net" "net"
@ -39,6 +36,10 @@ import (
"strings" "strings"
"time" "time"
"unicode/utf8" "unicode/utf8"
"github.com/unrud/remote-touchpad/inputcontrol"
"github.com/unrud/remote-touchpad/terminal"
"golang.org/x/net/websocket"
) )
const ( const (