rename plugin to backend
This commit is contained in:
parent
7edb092f93
commit
8e7c945f0f
8 changed files with 68 additions and 68 deletions
|
|
@ -39,15 +39,15 @@ const (
|
|||
KeyLimit
|
||||
)
|
||||
|
||||
type PluginInfo struct {
|
||||
type BackendInfo struct {
|
||||
Name string
|
||||
Init func() (Plugin, error)
|
||||
Init func() (Backend, error)
|
||||
}
|
||||
|
||||
var Plugins []PluginInfo = []PluginInfo{
|
||||
{"X11", InitX11Plugin},
|
||||
{"RemoteDesktop portal", InitPortalPlugin},
|
||||
{"Windows", InitWindowsPlugin},
|
||||
var Backends []BackendInfo = []BackendInfo{
|
||||
{"X11", InitX11Backend},
|
||||
{"RemoteDesktop portal", InitPortalBackend},
|
||||
{"Windows", InitWindowsBackend},
|
||||
}
|
||||
|
||||
type UnsupportedPlatformError struct {
|
||||
|
|
@ -58,7 +58,7 @@ func (e UnsupportedPlatformError) Error() string {
|
|||
return e.err.Error()
|
||||
}
|
||||
|
||||
type Plugin interface {
|
||||
type Backend interface {
|
||||
Close() error
|
||||
KeyboardText(text string) error
|
||||
KeyboardKey(key Key) error
|
||||
|
|
@ -40,14 +40,14 @@ const (
|
|||
btnMiddle int32 = 0x112
|
||||
)
|
||||
|
||||
type portalPlugin struct {
|
||||
type portalBackend struct {
|
||||
bus *dbus.Conn
|
||||
remoteDesktop dbus.BusObject
|
||||
sessionHandle dbus.ObjectPath
|
||||
lock sync.RWMutex
|
||||
}
|
||||
|
||||
func InitPortalPlugin() (Plugin, error) {
|
||||
func InitPortalBackend() (Backend, error) {
|
||||
bus, err := dbus.SessionBusPrivate()
|
||||
if err != nil {
|
||||
return nil, UnsupportedPlatformError{err}
|
||||
|
|
@ -139,7 +139,7 @@ func InitPortalPlugin() (Plugin, error) {
|
|||
return nil, errors.New("keyboard or pointer access denied")
|
||||
}
|
||||
cleanupBus = false
|
||||
return &portalPlugin{bus: bus, remoteDesktop: remoteDesktop,
|
||||
return &portalBackend{bus: bus, remoteDesktop: remoteDesktop,
|
||||
sessionHandle: sessionHandle}, nil
|
||||
}
|
||||
|
||||
|
|
@ -171,7 +171,7 @@ func getResponse(bus *dbus.Conn, object dbus.BusObject, method string,
|
|||
}
|
||||
}
|
||||
|
||||
func (p *portalPlugin) Close() error {
|
||||
func (p *portalBackend) Close() error {
|
||||
p.lock.Lock()
|
||||
defer p.lock.Unlock()
|
||||
if p.bus == nil {
|
||||
|
|
@ -185,7 +185,7 @@ func (p *portalPlugin) Close() error {
|
|||
return nil
|
||||
}
|
||||
|
||||
func (p *portalPlugin) keyboardKeys(keys []Keysym) error {
|
||||
func (p *portalBackend) keyboardKeys(keys []Keysym) error {
|
||||
p.lock.RLock()
|
||||
defer p.lock.RUnlock()
|
||||
if p.bus == nil {
|
||||
|
|
@ -204,7 +204,7 @@ func (p *portalPlugin) keyboardKeys(keys []Keysym) error {
|
|||
return nil
|
||||
}
|
||||
|
||||
func (p *portalPlugin) KeyboardText(text string) error {
|
||||
func (p *portalBackend) KeyboardText(text string) error {
|
||||
keys := make([]Keysym, 0, len(text))
|
||||
for _, runeValue := range text {
|
||||
keysym, err := RuneToKeysym(runeValue)
|
||||
|
|
@ -216,7 +216,7 @@ func (p *portalPlugin) KeyboardText(text string) error {
|
|||
return p.keyboardKeys(keys)
|
||||
}
|
||||
|
||||
func (p *portalPlugin) KeyboardKey(key Key) error {
|
||||
func (p *portalBackend) KeyboardKey(key Key) error {
|
||||
keysym, err := KeyToKeysym(key)
|
||||
if err != nil {
|
||||
return err
|
||||
|
|
@ -225,7 +225,7 @@ func (p *portalPlugin) KeyboardKey(key Key) error {
|
|||
return p.keyboardKeys(keys[:])
|
||||
}
|
||||
|
||||
func (p *portalPlugin) PointerButton(button PointerButton, press bool) error {
|
||||
func (p *portalBackend) PointerButton(button PointerButton, press bool) error {
|
||||
p.lock.RLock()
|
||||
defer p.lock.RUnlock()
|
||||
if p.bus == nil {
|
||||
|
|
@ -253,7 +253,7 @@ func (p *portalPlugin) PointerButton(button PointerButton, press bool) error {
|
|||
return nil
|
||||
}
|
||||
|
||||
func (p *portalPlugin) PointerMove(deltaX, deltaY int) error {
|
||||
func (p *portalBackend) PointerMove(deltaX, deltaY int) error {
|
||||
p.lock.RLock()
|
||||
defer p.lock.RUnlock()
|
||||
if p.bus == nil {
|
||||
|
|
@ -267,7 +267,7 @@ func (p *portalPlugin) PointerMove(deltaX, deltaY int) error {
|
|||
return nil
|
||||
}
|
||||
|
||||
func (p *portalPlugin) pointerScrollFull(deltaHorizontal, deltaVertical int, finish bool) error {
|
||||
func (p *portalBackend) pointerScrollFull(deltaHorizontal, deltaVertical int, finish bool) error {
|
||||
p.lock.RLock()
|
||||
defer p.lock.RUnlock()
|
||||
if p.bus == nil {
|
||||
|
|
@ -282,10 +282,10 @@ func (p *portalPlugin) pointerScrollFull(deltaHorizontal, deltaVertical int, fin
|
|||
return nil
|
||||
}
|
||||
|
||||
func (p *portalPlugin) PointerScroll(deltaHorizontal, deltaVertical int) error {
|
||||
func (p *portalBackend) PointerScroll(deltaHorizontal, deltaVertical int) error {
|
||||
return p.pointerScrollFull(deltaHorizontal, deltaVertical, false)
|
||||
}
|
||||
|
||||
func (p *portalPlugin) PointerScrollFinish() error {
|
||||
func (p *portalBackend) PointerScrollFinish() error {
|
||||
return p.pointerScrollFull(0, 0, true)
|
||||
}
|
||||
|
|
@ -23,6 +23,6 @@ package main
|
|||
|
||||
import "errors"
|
||||
|
||||
func InitPortalPlugin() (Plugin, error) {
|
||||
func InitPortalBackend() (Backend, error) {
|
||||
return nil, UnsupportedPlatformError{errors.New("disabled")}
|
||||
}
|
||||
|
|
@ -77,21 +77,21 @@ type keybdInput struct {
|
|||
padding [8]byte
|
||||
}
|
||||
|
||||
type windowsPlugin struct{}
|
||||
type windowsBackend struct{}
|
||||
|
||||
func InitWindowsPlugin() (Plugin, error) {
|
||||
p := &windowsPlugin{}
|
||||
func InitWindowsBackend() (Backend, error) {
|
||||
p := &windowsBackend{}
|
||||
if err := sendInputProc.Find(); err != nil {
|
||||
return nil, UnsupportedPlatformError{err}
|
||||
}
|
||||
return p, nil
|
||||
}
|
||||
|
||||
func (p *windowsPlugin) Close() error {
|
||||
func (p *windowsBackend) Close() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (p *windowsPlugin) sendInput(inputs []keybdInput) error {
|
||||
func (p *windowsBackend) sendInput(inputs []keybdInput) error {
|
||||
if len(inputs) == 0 {
|
||||
return nil
|
||||
}
|
||||
|
|
@ -103,7 +103,7 @@ func (p *windowsPlugin) sendInput(inputs []keybdInput) error {
|
|||
return nil
|
||||
}
|
||||
|
||||
func (p *windowsPlugin) KeyboardText(text string) error {
|
||||
func (p *windowsBackend) KeyboardText(text string) error {
|
||||
inputs := make([]keybdInput, 0, len(text)*2)
|
||||
for _, runeValue := range text {
|
||||
in := keybdInput{typ: inputKeyboard, wScan: uint16(runeValue), dwFlags: keyeventfUnicode}
|
||||
|
|
@ -117,7 +117,7 @@ func (p *windowsPlugin) KeyboardText(text string) error {
|
|||
return p.sendInput(inputs)
|
||||
}
|
||||
|
||||
func (p *windowsPlugin) KeyboardKey(key Key) error {
|
||||
func (p *windowsBackend) KeyboardKey(key Key) error {
|
||||
input := keybdInput{typ: inputKeyboard}
|
||||
if key == KeyVolumeMute {
|
||||
input.wVk = vkVolumeMute
|
||||
|
|
@ -139,7 +139,7 @@ func (p *windowsPlugin) KeyboardKey(key Key) error {
|
|||
return p.sendInput(inputs[:])
|
||||
}
|
||||
|
||||
func (p *windowsPlugin) PointerButton(button PointerButton, press bool) error {
|
||||
func (p *windowsBackend) PointerButton(button PointerButton, press bool) error {
|
||||
input := mouseInput{typ: inputMouse}
|
||||
if button == PointerButtonLeft && press {
|
||||
input.dwFlags = mouseeventfLeftdown
|
||||
|
|
@ -163,7 +163,7 @@ func (p *windowsPlugin) PointerButton(button PointerButton, press bool) error {
|
|||
return nil
|
||||
}
|
||||
|
||||
func (p *windowsPlugin) PointerMove(deltaX, deltaY int) error {
|
||||
func (p *windowsBackend) PointerMove(deltaX, deltaY int) error {
|
||||
input := mouseInput{
|
||||
typ: inputMouse,
|
||||
dx: int32(deltaX),
|
||||
|
|
@ -177,7 +177,7 @@ func (p *windowsPlugin) PointerMove(deltaX, deltaY int) error {
|
|||
return nil
|
||||
}
|
||||
|
||||
func (p *windowsPlugin) PointerScroll(deltaHorizontal, deltaVertical int) error {
|
||||
func (p *windowsBackend) PointerScroll(deltaHorizontal, deltaVertical int) error {
|
||||
inputs := make([]mouseInput, 0, 2)
|
||||
if deltaHorizontal != 0 {
|
||||
inputs = append(inputs, mouseInput{
|
||||
|
|
@ -204,6 +204,6 @@ func (p *windowsPlugin) PointerScroll(deltaHorizontal, deltaVertical int) error
|
|||
return nil
|
||||
}
|
||||
|
||||
func (p *windowsPlugin) PointerScrollFinish() error {
|
||||
func (p *windowsBackend) PointerScrollFinish() error {
|
||||
return nil
|
||||
}
|
||||
|
|
@ -23,6 +23,6 @@ package main
|
|||
|
||||
import "errors"
|
||||
|
||||
func InitWindowsPlugin() (Plugin, error) {
|
||||
func InitWindowsBackend() (Backend, error) {
|
||||
return nil, UnsupportedPlatformError{errors.New("disabled")}
|
||||
}
|
||||
|
|
@ -44,13 +44,13 @@ const (
|
|||
var modifierIndices [6]uint = [...]uint{C.ShiftMapIndex, C.Mod1MapIndex,
|
||||
C.Mod2MapIndex, C.Mod3MapIndex, C.Mod4MapIndex, C.Mod5MapIndex}
|
||||
|
||||
type x11Plugin struct {
|
||||
type x11Backend struct {
|
||||
display *C.Display
|
||||
lock sync.Mutex
|
||||
scrollHorizontal, scrollVertical int
|
||||
}
|
||||
|
||||
func InitX11Plugin() (Plugin, error) {
|
||||
func InitX11Backend() (Backend, error) {
|
||||
sessionType := os.Getenv("XDG_SESSION_TYPE")
|
||||
if sessionType != "" && sessionType != "x11" {
|
||||
return nil, UnsupportedPlatformError{errors.New(fmt.Sprintf(
|
||||
|
|
@ -61,10 +61,10 @@ func InitX11Plugin() (Plugin, error) {
|
|||
return nil, UnsupportedPlatformError{
|
||||
errors.New("failed to connect to X server")}
|
||||
}
|
||||
return &x11Plugin{display: display}, nil
|
||||
return &x11Backend{display: display}, nil
|
||||
}
|
||||
|
||||
func (p *x11Plugin) Close() error {
|
||||
func (p *x11Backend) Close() error {
|
||||
p.lock.Lock()
|
||||
defer p.lock.Unlock()
|
||||
if p.display == nil {
|
||||
|
|
@ -75,7 +75,7 @@ func (p *x11Plugin) Close() error {
|
|||
return nil
|
||||
}
|
||||
|
||||
func (p *x11Plugin) findEmptyKeycodeLocked() (C.KeyCode, C.int, error) {
|
||||
func (p *x11Backend) findEmptyKeycodeLocked() (C.KeyCode, C.int, error) {
|
||||
var minKeycodes, maxKeycodes C.int
|
||||
C.XDisplayKeycodes(p.display, &minKeycodes, &maxKeycodes)
|
||||
var keysymsPerKeycode C.int
|
||||
|
|
@ -101,7 +101,7 @@ keycodes:
|
|||
return 0, 0, errors.New("no empty keycode found")
|
||||
}
|
||||
|
||||
func (p *x11Plugin) changeKeyMappingLocked(keysymsPerKeycode C.int,
|
||||
func (p *x11Backend) changeKeyMappingLocked(keysymsPerKeycode C.int,
|
||||
keycode C.KeyCode, keysym Keysym) {
|
||||
keycodeMapping := make([]C.KeySym, keysymsPerKeycode)
|
||||
for i := range keycodeMapping {
|
||||
|
|
@ -112,7 +112,7 @@ func (p *x11Plugin) changeKeyMappingLocked(keysymsPerKeycode C.int,
|
|||
C.XFlush(p.display)
|
||||
}
|
||||
|
||||
func (p *x11Plugin) getModKeycodesLocked() map[uint]C.KeyCode {
|
||||
func (p *x11Backend) getModKeycodesLocked() map[uint]C.KeyCode {
|
||||
modKeymap := C.XGetModifierMapping(p.display)
|
||||
defer C.XFreeModifiermap(modKeymap)
|
||||
modKeycodes := make(map[uint]C.KeyCode)
|
||||
|
|
@ -129,7 +129,7 @@ func (p *x11Plugin) getModKeycodesLocked() map[uint]C.KeyCode {
|
|||
return modKeycodes
|
||||
}
|
||||
|
||||
func (p *x11Plugin) findKeycodeLocked(keyboard C.XkbDescPtr,
|
||||
func (p *x11Backend) 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))
|
||||
|
|
@ -165,7 +165,7 @@ func (p *x11Plugin) findKeycodeLocked(keyboard C.XkbDescPtr,
|
|||
return 0, 0
|
||||
}
|
||||
|
||||
func (p *x11Plugin) sendModsLocked(modKeycodes map[uint]C.KeyCode, mods C.uint,
|
||||
func (p *x11Backend) sendModsLocked(modKeycodes map[uint]C.KeyCode, mods C.uint,
|
||||
press bool) {
|
||||
var pressC C.int = C.False
|
||||
if press {
|
||||
|
|
@ -178,7 +178,7 @@ func (p *x11Plugin) sendModsLocked(modKeycodes map[uint]C.KeyCode, mods C.uint,
|
|||
}
|
||||
}
|
||||
|
||||
func (p *x11Plugin) keyboardKeys(keys []Keysym) error {
|
||||
func (p *x11Backend) keyboardKeys(keys []Keysym) error {
|
||||
p.lock.Lock()
|
||||
defer p.lock.Unlock()
|
||||
if p.display == nil {
|
||||
|
|
@ -236,7 +236,7 @@ func (p *x11Plugin) keyboardKeys(keys []Keysym) error {
|
|||
return nil
|
||||
}
|
||||
|
||||
func (p *x11Plugin) KeyboardText(text string) error {
|
||||
func (p *x11Backend) KeyboardText(text string) error {
|
||||
keys := make([]Keysym, 0, len(text))
|
||||
for _, runeValue := range text {
|
||||
keysym, err := RuneToKeysym(runeValue)
|
||||
|
|
@ -248,7 +248,7 @@ func (p *x11Plugin) KeyboardText(text string) error {
|
|||
return p.keyboardKeys(keys)
|
||||
}
|
||||
|
||||
func (p *x11Plugin) KeyboardKey(key Key) error {
|
||||
func (p *x11Backend) KeyboardKey(key Key) error {
|
||||
keysym, err := KeyToKeysym(key)
|
||||
if err != nil {
|
||||
return err
|
||||
|
|
@ -257,7 +257,7 @@ func (p *x11Plugin) KeyboardKey(key Key) error {
|
|||
return p.keyboardKeys(keys[:])
|
||||
}
|
||||
|
||||
func (p *x11Plugin) sendButton(button uint, press bool) error {
|
||||
func (p *x11Backend) sendButton(button uint, press bool) error {
|
||||
p.lock.Lock()
|
||||
defer p.lock.Unlock()
|
||||
if p.display == nil {
|
||||
|
|
@ -275,7 +275,7 @@ func (p *x11Plugin) sendButton(button uint, press bool) error {
|
|||
return nil
|
||||
}
|
||||
|
||||
func (p *x11Plugin) PointerButton(button PointerButton, press bool) error {
|
||||
func (p *x11Backend) PointerButton(button PointerButton, press bool) error {
|
||||
if button == PointerButtonLeft {
|
||||
return p.sendButton(1, press)
|
||||
}
|
||||
|
|
@ -288,7 +288,7 @@ func (p *x11Plugin) PointerButton(button PointerButton, press bool) error {
|
|||
return errors.New("unsupported pointer button")
|
||||
}
|
||||
|
||||
func (p *x11Plugin) PointerMove(deltaX, deltaY int) error {
|
||||
func (p *x11Backend) PointerMove(deltaX, deltaY int) error {
|
||||
p.lock.Lock()
|
||||
defer p.lock.Unlock()
|
||||
if p.display == nil {
|
||||
|
|
@ -299,7 +299,7 @@ func (p *x11Plugin) PointerMove(deltaX, deltaY int) error {
|
|||
return nil
|
||||
}
|
||||
|
||||
func (p *x11Plugin) PointerScroll(deltaHorizontal, deltaVertical int) error {
|
||||
func (p *x11Backend) PointerScroll(deltaHorizontal, deltaVertical int) error {
|
||||
p.lock.Lock()
|
||||
stepsHorizontal := (p.scrollHorizontal + deltaHorizontal) / scrollDiv
|
||||
stepsVertical := (p.scrollVertical + deltaVertical) / scrollDiv
|
||||
|
|
@ -335,7 +335,7 @@ func (p *x11Plugin) PointerScroll(deltaHorizontal, deltaVertical int) error {
|
|||
return nil
|
||||
}
|
||||
|
||||
func (p *x11Plugin) PointerScrollFinish() error {
|
||||
func (p *x11Backend) PointerScrollFinish() error {
|
||||
p.lock.Lock()
|
||||
defer p.lock.Unlock()
|
||||
p.scrollHorizontal = 0
|
||||
|
|
@ -23,6 +23,6 @@ package main
|
|||
|
||||
import "errors"
|
||||
|
||||
func InitX11Plugin() (Plugin, error) {
|
||||
func InitX11Backend() (Backend, error) {
|
||||
return nil, UnsupportedPlatformError{errors.New("disabled")}
|
||||
}
|
||||
36
main.go
36
main.go
|
|
@ -48,12 +48,12 @@ const (
|
|||
version string = "0.0.10"
|
||||
)
|
||||
|
||||
func processCommand(plugin Plugin, command string) error {
|
||||
func processCommand(backend Backend, command string) error {
|
||||
if len(command) == 0 {
|
||||
return errors.New("empty command")
|
||||
}
|
||||
if command == "sf" {
|
||||
return plugin.PointerScrollFinish()
|
||||
return backend.PointerScrollFinish()
|
||||
}
|
||||
if command[0] == 't' {
|
||||
text := command[1:]
|
||||
|
|
@ -63,7 +63,7 @@ func processCommand(plugin Plugin, command string) error {
|
|||
if !utf8.ValidString(text) {
|
||||
return errors.New("invalid utf-8")
|
||||
}
|
||||
return plugin.KeyboardText(text)
|
||||
return backend.KeyboardText(text)
|
||||
}
|
||||
arguments := strings.Split(command[1:], ";")
|
||||
if command[0] == 'k' && len(arguments) != 1 ||
|
||||
|
|
@ -78,17 +78,17 @@ func processCommand(plugin Plugin, command string) error {
|
|||
if x < 0 || x >= int64(KeyLimit) {
|
||||
return errors.New("unsupported key")
|
||||
}
|
||||
return plugin.KeyboardKey(Key(x))
|
||||
return backend.KeyboardKey(Key(x))
|
||||
}
|
||||
y, err := strconv.ParseInt(arguments[1], 10, 32)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if command[0] == 'm' {
|
||||
return plugin.PointerMove(int(x), int(y))
|
||||
return backend.PointerMove(int(x), int(y))
|
||||
}
|
||||
if command[0] == 's' {
|
||||
return plugin.PointerScroll(int(x), int(y))
|
||||
return backend.PointerScroll(int(x), int(y))
|
||||
}
|
||||
if command[0] == 'b' {
|
||||
if x < 0 || x >= int64(PointerButtonLimit) {
|
||||
|
|
@ -98,7 +98,7 @@ func processCommand(plugin Plugin, command string) error {
|
|||
if y == 0 {
|
||||
b = false
|
||||
}
|
||||
return plugin.PointerButton(PointerButton(x), b)
|
||||
return backend.PointerButton(PointerButton(x), b)
|
||||
}
|
||||
return errors.New("unsupported command")
|
||||
}
|
||||
|
|
@ -171,25 +171,25 @@ func main() {
|
|||
if secret == "" {
|
||||
secret = secureRandBase64(defaultSecretLength)
|
||||
}
|
||||
var plugin Plugin
|
||||
var pluginName string
|
||||
var backend Backend
|
||||
var backendName string
|
||||
platformErrors := ""
|
||||
for _, pluginInfo := range Plugins {
|
||||
pluginName = pluginInfo.Name
|
||||
for _, backendInfo := range Backends {
|
||||
backendName = backendInfo.Name
|
||||
var err error
|
||||
plugin, err = pluginInfo.Init()
|
||||
backend, err = backendInfo.Init()
|
||||
if err == nil {
|
||||
break
|
||||
} else if _, ok := err.(UnsupportedPlatformError); ok {
|
||||
platformErrors += fmt.Sprintf("%s plugin: %v\n", pluginName, err)
|
||||
platformErrors += fmt.Sprintf("%s backend: %v\n", backendName, err)
|
||||
} else {
|
||||
log.Fatal(fmt.Sprintf("%s plugin: %v", pluginName, err))
|
||||
log.Fatal(fmt.Sprintf("%s backend: %v", backendName, err))
|
||||
}
|
||||
}
|
||||
if plugin == nil {
|
||||
if backend == nil {
|
||||
log.Fatal("unsupported platform:\n" + platformErrors)
|
||||
}
|
||||
defer plugin.Close()
|
||||
defer backend.Close()
|
||||
authenticationChallenges := make(chan challenge, authenticationRateBurst)
|
||||
go authenticationChallengeGenerator(secret, authenticationChallenges)
|
||||
listener, err := net.Listen("tcp", bind)
|
||||
|
|
@ -228,8 +228,8 @@ func main() {
|
|||
if err := websocket.Message.Receive(ws, &message); err != nil {
|
||||
return
|
||||
}
|
||||
if err := processCommand(plugin, message); err != nil {
|
||||
log.Print(fmt.Sprintf("%s plugin: %v", pluginName, err))
|
||||
if err := processCommand(backend, message); err != nil {
|
||||
log.Print(fmt.Sprintf("%s backend: %v", backendName, err))
|
||||
return
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue