From f68a4710b417f5501a0e825116663585fa0bd60d Mon Sep 17 00:00:00 2001 From: Bernd Schoolmann Date: Wed, 6 Dec 2023 02:09:43 +0100 Subject: [PATCH] Implement libportal autotype --- autofill/autotype/libportalautotype.go | 77 ++++++++++++++++++++++++++ autofill/autotype/uinputautotype.go | 2 +- 2 files changed, 78 insertions(+), 1 deletion(-) create mode 100644 autofill/autotype/libportalautotype.go diff --git a/autofill/autotype/libportalautotype.go b/autofill/autotype/libportalautotype.go new file mode 100644 index 0000000..23e85f8 --- /dev/null +++ b/autofill/autotype/libportalautotype.go @@ -0,0 +1,77 @@ +//go:build linux && !uinput + +package autotype + +import ( + "fmt" + "time" + + "github.com/godbus/dbus/v5" +) + +var globalID = 0 + +const autoTypeDelay = 1 * time.Millisecond + +func TypeString(textToType string, layout string) { + bus, err := dbus.SessionBus() + if err != nil { + panic(err) + } + + obj := bus.Object("org.freedesktop.portal.Desktop", "/org/freedesktop/portal/desktop") + obj.AddMatchSignal("org.freedesktop.portal.Request", "Response") + + globalID++ + obj.Call("org.freedesktop.portal.RemoteDesktop.CreateSession", 0, map[string]dbus.Variant{ + "session_handle_token": dbus.MakeVariant("u" + fmt.Sprint(globalID)), + }) + + signals := make(chan *dbus.Signal, 10) + bus.Signal(signals) + + var state = 0 + var sessionHandle dbus.ObjectPath + + for { + select { + case message := <-signals: + fmt.Println("Message:", message) + if state == 0 { + result := message.Body[1].(map[string]dbus.Variant) + resultSessionHandle := result["session_handle"] + sessionHandle = dbus.ObjectPath(resultSessionHandle.String()[1 : len(resultSessionHandle.String())-1]) + obj.Call("org.freedesktop.portal.RemoteDesktop.SelectDevices", 0, sessionHandle, map[string]dbus.Variant{}) + state = 1 + } else if state == 1 { + obj.Call("org.freedesktop.portal.RemoteDesktop.Start", 0, sessionHandle, "", map[string]dbus.Variant{}) + state = 2 + } else if state == 2 { + state = 3 + for _, char := range textToType { + if char == '\t' { + obj.Call("org.freedesktop.portal.RemoteDesktop.NotifyKeyboardKeycode", 0, sessionHandle, map[string]dbus.Variant{}, 15, uint32(1)) + time.Sleep(autoTypeDelay) + obj.Call("org.freedesktop.portal.RemoteDesktop.NotifyKeyboardKeycode", 0, sessionHandle, map[string]dbus.Variant{}, 15, uint32(0)) + time.Sleep(autoTypeDelay) + } else { + obj.Call("org.freedesktop.portal.RemoteDesktop.NotifyKeyboardKeysym", 0, sessionHandle, map[string]dbus.Variant{}, int32(char), uint32(1)) + time.Sleep(autoTypeDelay) + obj.Call("org.freedesktop.portal.RemoteDesktop.NotifyKeyboardKeysym", 0, sessionHandle, map[string]dbus.Variant{}, int32(char), uint32(0)) + time.Sleep(autoTypeDelay) + } + } + bus.Close() + return + } else { + fmt.Println("state", state) + fmt.Println("Message:", message) + } + } + } +} + +func Paste(layout string) error { + fmt.Println("Not implemented") + return nil +} diff --git a/autofill/autotype/uinputautotype.go b/autofill/autotype/uinputautotype.go index 5a32c46..fe125c9 100644 --- a/autofill/autotype/uinputautotype.go +++ b/autofill/autotype/uinputautotype.go @@ -1,4 +1,4 @@ -//go:build linux +//go:build linux && uinput package autotype