From 25d5cfa5051cfb42b7d501045570b7dda2b60356 Mon Sep 17 00:00:00 2001 From: Unrud Date: Mon, 21 Feb 2022 22:30:28 +0100 Subject: [PATCH] Rename backend to controller --- backend.go => controller.go | 16 ++++----- backend_portal.go => controller_portal.go | 22 ++++++------ backend_windows.go => controller_windows.go | 22 ++++++------ backend_x11.go => controller_x11.go | 34 +++++++++--------- main.go | 38 ++++++++++----------- 5 files changed, 66 insertions(+), 66 deletions(-) rename backend.go => controller.go (80%) rename backend_portal.go => controller_portal.go (91%) rename backend_windows.go => controller_windows.go (90%) rename backend_x11.go => controller_x11.go (89%) diff --git a/backend.go b/controller.go similarity index 80% rename from backend.go rename to controller.go index f749138..28597f0 100644 --- a/backend.go +++ b/controller.go @@ -53,19 +53,19 @@ const ( KeyLimit ) -type BackendInfo struct { +type ControllerInfo struct { Name string - Init func() (Backend, error) + Init func() (Controller, error) priority int } -var Backends []BackendInfo +var Controllers []ControllerInfo -func RegisterBackend(name string, init func() (Backend, error), priority int) { - Backends = append(Backends, BackendInfo{name, init, priority}) - sort.SliceStable(Backends, func(i, j int) bool { - return Backends[i].priority < Backends[j].priority +func RegisterController(name string, init func() (Controller, error), priority int) { + Controllers = append(Controllers, ControllerInfo{name, init, priority}) + sort.SliceStable(Controllers, func(i, j int) bool { + return Controllers[i].priority < Controllers[j].priority }) } @@ -77,7 +77,7 @@ func (e UnsupportedPlatformError) Error() string { return e.err.Error() } -type Backend interface { +type Controller interface { Close() error KeyboardText(text string) error KeyboardKey(key Key) error diff --git a/backend_portal.go b/controller_portal.go similarity index 91% rename from backend_portal.go rename to controller_portal.go index d4662ea..ad902f4 100644 --- a/backend_portal.go +++ b/controller_portal.go @@ -41,7 +41,7 @@ const ( btnMiddle int32 = 0x112 ) -type portalBackend struct { +type portalController struct { bus *dbus.Conn remoteDesktop dbus.BusObject sessionHandle dbus.ObjectPath @@ -49,10 +49,10 @@ type portalBackend struct { } func init() { - RegisterBackend("RemoteDesktop portal", InitPortalBackend, 1) + RegisterController("RemoteDesktop portal", InitPortalController, 1) } -func InitPortalBackend() (Backend, error) { +func InitPortalController() (Controller, error) { bus, err := dbus.SessionBusPrivate() if err != nil { return nil, UnsupportedPlatformError{err} @@ -144,7 +144,7 @@ func InitPortalBackend() (Backend, error) { return nil, errors.New("keyboard or pointer access denied") } cleanupBus = false - return &portalBackend{bus: bus, remoteDesktop: remoteDesktop, + return &portalController{bus: bus, remoteDesktop: remoteDesktop, sessionHandle: sessionHandle}, nil } @@ -176,7 +176,7 @@ func getResponse(bus *dbus.Conn, object dbus.BusObject, method string, } } -func (p *portalBackend) Close() error { +func (p *portalController) Close() error { p.lock.Lock() defer p.lock.Unlock() if p.bus == nil { @@ -190,7 +190,7 @@ func (p *portalBackend) Close() error { return nil } -func (p *portalBackend) keyboardKeys(keys []Keysym) error { +func (p *portalController) keyboardKeys(keys []Keysym) error { p.lock.RLock() defer p.lock.RUnlock() if p.bus == nil { @@ -209,7 +209,7 @@ func (p *portalBackend) keyboardKeys(keys []Keysym) error { return nil } -func (p *portalBackend) KeyboardText(text string) error { +func (p *portalController) KeyboardText(text string) error { keys := make([]Keysym, 0, len(text)) for _, runeValue := range text { keysym, err := RuneToKeysym(runeValue) @@ -221,7 +221,7 @@ func (p *portalBackend) KeyboardText(text string) error { return p.keyboardKeys(keys) } -func (p *portalBackend) KeyboardKey(key Key) error { +func (p *portalController) KeyboardKey(key Key) error { keysym, err := KeyToKeysym(key) if err != nil { return err @@ -230,7 +230,7 @@ func (p *portalBackend) KeyboardKey(key Key) error { return p.keyboardKeys(keys[:]) } -func (p *portalBackend) PointerButton(button PointerButton, press bool) error { +func (p *portalController) PointerButton(button PointerButton, press bool) error { p.lock.RLock() defer p.lock.RUnlock() if p.bus == nil { @@ -258,7 +258,7 @@ func (p *portalBackend) PointerButton(button PointerButton, press bool) error { return nil } -func (p *portalBackend) PointerMove(deltaX, deltaY int) error { +func (p *portalController) PointerMove(deltaX, deltaY int) error { p.lock.RLock() defer p.lock.RUnlock() if p.bus == nil { @@ -272,7 +272,7 @@ func (p *portalBackend) PointerMove(deltaX, deltaY int) error { return nil } -func (p *portalBackend) PointerScroll(deltaHorizontal, deltaVertical int, finish bool) error { +func (p *portalController) PointerScroll(deltaHorizontal, deltaVertical int, finish bool) error { p.lock.RLock() defer p.lock.RUnlock() if p.bus == nil { diff --git a/backend_windows.go b/controller_windows.go similarity index 90% rename from backend_windows.go rename to controller_windows.go index 272f764..daae558 100644 --- a/backend_windows.go +++ b/controller_windows.go @@ -89,25 +89,25 @@ type keybdInput struct { padding [8]byte } -type windowsBackend struct{} +type windowsController struct{} func init() { - RegisterBackend("Windows", InitWindowsBackend, 0) + RegisterController("Windows", InitWindowsController, 0) } -func InitWindowsBackend() (Backend, error) { - p := &windowsBackend{} +func InitWindowsController() (Controller, error) { + p := &windowsController{} if err := sendInputProc.Find(); err != nil { return nil, UnsupportedPlatformError{err} } return p, nil } -func (p *windowsBackend) Close() error { +func (p *windowsController) Close() error { return nil } -func (p *windowsBackend) sendInput(inputs []keybdInput) error { +func (p *windowsController) sendInput(inputs []keybdInput) error { if len(inputs) == 0 { return nil } @@ -119,7 +119,7 @@ func (p *windowsBackend) sendInput(inputs []keybdInput) error { return nil } -func (p *windowsBackend) KeyboardText(text string) error { +func (p *windowsController) KeyboardText(text string) error { inputs := make([]keybdInput, 0, len(text)*2) for _, runeValue := range text { in := keybdInput{typ: inputKeyboard, wScan: uint16(runeValue), dwFlags: keyeventfUnicode} @@ -133,7 +133,7 @@ func (p *windowsBackend) KeyboardText(text string) error { return p.sendInput(inputs) } -func (p *windowsBackend) KeyboardKey(key Key) error { +func (p *windowsController) KeyboardKey(key Key) error { input := keybdInput{typ: inputKeyboard} if key == KeyBackSpace { input.wVk = vkBack @@ -179,7 +179,7 @@ func (p *windowsBackend) KeyboardKey(key Key) error { return p.sendInput(inputs[:]) } -func (p *windowsBackend) PointerButton(button PointerButton, press bool) error { +func (p *windowsController) PointerButton(button PointerButton, press bool) error { input := mouseInput{typ: inputMouse} if button == PointerButtonLeft && press { input.dwFlags = mouseeventfLeftdown @@ -203,7 +203,7 @@ func (p *windowsBackend) PointerButton(button PointerButton, press bool) error { return nil } -func (p *windowsBackend) PointerMove(deltaX, deltaY int) error { +func (p *windowsController) PointerMove(deltaX, deltaY int) error { input := mouseInput{ typ: inputMouse, dx: int32(deltaX), @@ -217,7 +217,7 @@ func (p *windowsBackend) PointerMove(deltaX, deltaY int) error { return nil } -func (p *windowsBackend) PointerScroll(deltaHorizontal, deltaVertical int, finish bool) error { +func (p *windowsController) PointerScroll(deltaHorizontal, deltaVertical int, finish bool) error { inputs := make([]mouseInput, 0, 2) if deltaHorizontal != 0 { inputs = append(inputs, mouseInput{ diff --git a/backend_x11.go b/controller_x11.go similarity index 89% rename from backend_x11.go rename to controller_x11.go index 499f316..26b081f 100644 --- a/backend_x11.go +++ b/controller_x11.go @@ -44,17 +44,17 @@ const ( var modifierIndices [6]uint = [...]uint{C.ShiftMapIndex, C.Mod1MapIndex, C.Mod2MapIndex, C.Mod3MapIndex, C.Mod4MapIndex, C.Mod5MapIndex} -type x11Backend struct { +type x11Controller struct { display *C.Display lock sync.Mutex scrollHorizontal, scrollVertical int } func init() { - RegisterBackend("X11", InitX11Backend, 0) + RegisterController("X11", InitX11Controller, 0) } -func InitX11Backend() (Backend, error) { +func InitX11Controller() (Controller, error) { sessionType := os.Getenv("XDG_SESSION_TYPE") if sessionType != "" && sessionType != "x11" { return nil, UnsupportedPlatformError{errors.New(fmt.Sprintf( @@ -65,10 +65,10 @@ func InitX11Backend() (Backend, error) { return nil, UnsupportedPlatformError{ errors.New("failed to connect to X server")} } - return &x11Backend{display: display}, nil + return &x11Controller{display: display}, nil } -func (p *x11Backend) Close() error { +func (p *x11Controller) Close() error { p.lock.Lock() defer p.lock.Unlock() if p.display == nil { @@ -79,7 +79,7 @@ func (p *x11Backend) Close() error { return nil } -func (p *x11Backend) findEmptyKeycodeLocked() (C.KeyCode, C.int, error) { +func (p *x11Controller) findEmptyKeycodeLocked() (C.KeyCode, C.int, error) { var minKeycodes, maxKeycodes C.int C.XDisplayKeycodes(p.display, &minKeycodes, &maxKeycodes) var keysymsPerKeycode C.int @@ -105,7 +105,7 @@ keycodes: return 0, 0, errors.New("no empty keycode found") } -func (p *x11Backend) changeKeyMappingLocked(keysymsPerKeycode C.int, +func (p *x11Controller) changeKeyMappingLocked(keysymsPerKeycode C.int, keycode C.KeyCode, keysym Keysym) { keycodeMapping := make([]C.KeySym, keysymsPerKeycode) for i := range keycodeMapping { @@ -116,7 +116,7 @@ func (p *x11Backend) changeKeyMappingLocked(keysymsPerKeycode C.int, C.XFlush(p.display) } -func (p *x11Backend) getModKeycodesLocked() map[uint]C.KeyCode { +func (p *x11Controller) getModKeycodesLocked() map[uint]C.KeyCode { modKeymap := C.XGetModifierMapping(p.display) defer C.XFreeModifiermap(modKeymap) modKeycodes := make(map[uint]C.KeyCode) @@ -133,7 +133,7 @@ func (p *x11Backend) getModKeycodesLocked() map[uint]C.KeyCode { return modKeycodes } -func (p *x11Backend) findKeycodeLocked(keyboard C.XkbDescPtr, +func (p *x11Controller) findKeycodeLocked(keyboard C.XkbDescPtr, modKeycodes map[uint]C.KeyCode, activeMods C.uint, keysym Keysym) (C.KeyCode, C.uint) { keycode := C.XKeysymToKeycode(p.display, C.KeySym(keysym)) @@ -169,7 +169,7 @@ func (p *x11Backend) findKeycodeLocked(keyboard C.XkbDescPtr, return 0, 0 } -func (p *x11Backend) sendModsLocked(modKeycodes map[uint]C.KeyCode, mods C.uint, +func (p *x11Controller) sendModsLocked(modKeycodes map[uint]C.KeyCode, mods C.uint, press bool) { var pressC C.int = C.False if press { @@ -182,7 +182,7 @@ func (p *x11Backend) sendModsLocked(modKeycodes map[uint]C.KeyCode, mods C.uint, } } -func (p *x11Backend) keyboardKeys(keys []Keysym) error { +func (p *x11Controller) keyboardKeys(keys []Keysym) error { p.lock.Lock() defer p.lock.Unlock() if p.display == nil { @@ -240,7 +240,7 @@ func (p *x11Backend) keyboardKeys(keys []Keysym) error { return nil } -func (p *x11Backend) KeyboardText(text string) error { +func (p *x11Controller) KeyboardText(text string) error { keys := make([]Keysym, 0, len(text)) for _, runeValue := range text { keysym, err := RuneToKeysym(runeValue) @@ -252,7 +252,7 @@ func (p *x11Backend) KeyboardText(text string) error { return p.keyboardKeys(keys) } -func (p *x11Backend) KeyboardKey(key Key) error { +func (p *x11Controller) KeyboardKey(key Key) error { keysym, err := KeyToKeysym(key) if err != nil { return err @@ -261,7 +261,7 @@ func (p *x11Backend) KeyboardKey(key Key) error { return p.keyboardKeys(keys[:]) } -func (p *x11Backend) sendButton(button uint, press bool) error { +func (p *x11Controller) sendButton(button uint, press bool) error { p.lock.Lock() defer p.lock.Unlock() if p.display == nil { @@ -279,7 +279,7 @@ func (p *x11Backend) sendButton(button uint, press bool) error { return nil } -func (p *x11Backend) PointerButton(button PointerButton, press bool) error { +func (p *x11Controller) PointerButton(button PointerButton, press bool) error { if button == PointerButtonLeft { return p.sendButton(1, press) } @@ -292,7 +292,7 @@ func (p *x11Backend) PointerButton(button PointerButton, press bool) error { return errors.New("unsupported pointer button") } -func (p *x11Backend) PointerMove(deltaX, deltaY int) error { +func (p *x11Controller) PointerMove(deltaX, deltaY int) error { p.lock.Lock() defer p.lock.Unlock() if p.display == nil { @@ -303,7 +303,7 @@ func (p *x11Backend) PointerMove(deltaX, deltaY int) error { return nil } -func (p *x11Backend) PointerScroll(deltaHorizontal, deltaVertical int, finish bool) error { +func (p *x11Controller) PointerScroll(deltaHorizontal, deltaVertical int, finish bool) error { p.lock.Lock() stepsHorizontal := (p.scrollHorizontal + deltaHorizontal) / scrollDiv stepsVertical := (p.scrollVertical + deltaVertical) / scrollDiv diff --git a/main.go b/main.go index ceee109..3b0199e 100644 --- a/main.go +++ b/main.go @@ -56,12 +56,12 @@ type config struct { MouseMoveSpeed float64 `json:"mouseMoveSpeed"` } -func processCommand(backend Backend, command string) error { +func processCommand(controller Controller, command string) error { if len(command) == 0 { return errors.New("empty command") } if command == "S" { - return backend.PointerScroll(0, 0, true) + return controller.PointerScroll(0, 0, true) } if command[0] == 't' { text := command[1:] @@ -71,7 +71,7 @@ func processCommand(backend Backend, command string) error { if !utf8.ValidString(text) { return errors.New("invalid utf-8") } - return backend.KeyboardText(text) + return controller.KeyboardText(text) } arguments := strings.Split(command[1:], ";") if command[0] == 'k' && len(arguments) != 1 || @@ -86,20 +86,20 @@ func processCommand(backend Backend, command string) error { if x < 0 || x >= int64(KeyLimit) { return errors.New("unsupported key") } - return backend.KeyboardKey(Key(x)) + return controller.KeyboardKey(Key(x)) } y, err := strconv.ParseInt(arguments[1], 10, 32) if err != nil { return err } if command[0] == 'm' { - return backend.PointerMove(int(x), int(y)) + return controller.PointerMove(int(x), int(y)) } if command[0] == 's' { - return backend.PointerScroll(int(x), int(y), false) + return controller.PointerScroll(int(x), int(y), false) } if command[0] == 'S' { - return backend.PointerScroll(int(x), int(y), true) + return controller.PointerScroll(int(x), int(y), true) } if command[0] == 'b' { if x < 0 || x >= int64(PointerButtonLimit) { @@ -109,7 +109,7 @@ func processCommand(backend Backend, command string) error { if y == 0 { b = false } - return backend.PointerButton(PointerButton(x), b) + return controller.PointerButton(PointerButton(x), b) } return errors.New("unsupported command") } @@ -178,25 +178,25 @@ func main() { if secret == "" { secret = secureRandBase64(defaultSecretLength) } - var backend Backend - var backendName string + var controller Controller + var controllerName string platformErrors := "" - for _, backendInfo := range Backends { - backendName = backendInfo.Name + for _, controllerInfo := range Controllers { + controllerName = controllerInfo.Name var err error - backend, err = backendInfo.Init() + controller, err = controllerInfo.Init() if err == nil { break } else if _, ok := err.(UnsupportedPlatformError); ok { - platformErrors += fmt.Sprintf("%s backend: %v\n", backendName, err) + platformErrors += fmt.Sprintf("%s controller: %v\n", controllerName, err) } else { - log.Fatalf("%s backend: %v", backendName, err) + log.Fatalf("%s controller: %v", controllerName, err) } } - if backend == nil { + if controller == nil { log.Fatal("unsupported platform:\n" + platformErrors) } - defer backend.Close() + defer controller.Close() authenticationChallenges := make(chan challenge, authenticationRateBurst) go authenticationChallengeGenerator(secret, authenticationChallenges) listener, err := net.Listen("tcp", bind) @@ -236,8 +236,8 @@ func main() { if err := websocket.Message.Receive(ws, &message); err != nil { return } - if err := processCommand(backend, message); err != nil { - log.Printf("%s backend: %v", backendName, err) + if err := processCommand(controller, message); err != nil { + log.Printf("%s controller: %v", controllerName, err) return } }