From 2bf53f0397f16ee0367be99373b57dadf1742195 Mon Sep 17 00:00:00 2001 From: Mathias Fredriksson Date: Tue, 8 Feb 2022 23:50:49 +0200 Subject: Add support for mouse click and scroll wheel Requires the webos client to have support for these events, see: https://github.com/Wouterdek/magic4pc/pull/10 --- cmd/magic4linux/main.go | 23 ++++++++++++++++++++--- m4p/client.go | 9 +++++++-- m4p/message.go | 20 +++++++++++++++++++- 3 files changed, 46 insertions(+), 6 deletions(-) diff --git a/cmd/magic4linux/main.go b/cmd/magic4linux/main.go index 0275d64..a2775ac 100644 --- a/cmd/magic4linux/main.go +++ b/cmd/magic4linux/main.go @@ -36,6 +36,12 @@ func run(ctx context.Context) error { } defer kbd.Close() + mouse, err := uinput.CreateMouse("/dev/uinput", []byte("magic4linux-mouse")) + if err != nil { + return err + } + defer mouse.Close() + tp, err := uinput.CreateTouchPad("/dev/uinput", []byte("magic4linux-touchpad"), 0, 1920, 0, 1080) if err != nil { return err @@ -54,7 +60,7 @@ func run(ctx context.Context) error { return nil case dev := <-d.NextDevice(): - err = connect(ctx, dev, kbd, tp) + err = connect(ctx, dev, kbd, mouse, tp) if err != nil { log.Printf("connect: %v", err) } @@ -62,7 +68,7 @@ func run(ctx context.Context) error { } } -func connect(ctx context.Context, dev m4p.DeviceInfo, kbd uinput.Keyboard, tp uinput.TouchPad) error { +func connect(ctx context.Context, dev m4p.DeviceInfo, kbd uinput.Keyboard, mouse uinput.Mouse, tp uinput.TouchPad) error { addr := fmt.Sprintf("%s:%d", dev.IPAddr, dev.Port) log.Printf("connect: connecting to: %s", addr) @@ -160,11 +166,22 @@ func connect(ctx context.Context, dev m4p.DeviceInfo, kbd uinput.Keyboard, tp ui x := coordinate[0] y := coordinate[1] - fmt.Println("Move mouse", x, y) + // fmt.Println("Move mouse", x, y) tp.MoveTo(x, y) // log.Printf("connect: %d %d %#v %#v %#v %#v", returnValue, deviceID, coordinate, gyroscope, acceleration, quaternion) + case m4p.MouseMessage: + switch m.Mouse.Type { + case "mousedown": + tp.LeftPress() + case "mouseup": + tp.LeftRelease() + } + + case m4p.WheelMessage: + mouse.Wheel(false, m.Wheel.Delta) + default: } } diff --git a/m4p/client.go b/m4p/client.go index 45f8b77..3467b3f 100644 --- a/m4p/client.go +++ b/m4p/client.go @@ -2,7 +2,6 @@ package m4p import ( "context" - "encoding/hex" "encoding/json" "errors" "fmt" @@ -132,8 +131,14 @@ recvLoop: case InputMessage: log.Printf("m4p: Client: recv: got %s: %v", m.Type, m.Input) + case MouseMessage: + log.Printf("m4p: Client: recv: got %s: %v", m.Type, m.Mouse) + + case WheelMessage: + log.Printf("m4p: Client: recv: got %s: %v", m.Type, m.Wheel) + case RemoteUpdateMessage: - log.Printf("m4p: Client: recv: got %s: %s", m.Type, hex.EncodeToString(m.RemoteUpdate.Payload)) + // log.Printf("m4p: Client: recv: got %s: %s", m.Type, hex.EncodeToString(m.RemoteUpdate.Payload)) default: log.Printf("m4p: Client: recv: unknown message: %s", m.Type) diff --git a/m4p/message.go b/m4p/message.go index 34a9383..0a9ebeb 100644 --- a/m4p/message.go +++ b/m4p/message.go @@ -13,6 +13,8 @@ const ( SubSensorMessage MessageType = "sub_sensor" RemoteUpdateMessage MessageType = "remote_update" InputMessage MessageType = "input" + MouseMessage MessageType = "mouse" + WheelMessage MessageType = "wheel" KeepAliveMessage MessageType = "keepalive" ) @@ -24,6 +26,8 @@ type Message struct { *Register *RemoteUpdate *Input + Mouse Mouse `json:"mouse"` + Wheel Wheel `json:"wheel"` } // NewMessage initializes a message with the type and protocol version. @@ -62,10 +66,24 @@ type RemoteUpdate struct { Payload []byte `json:"payload"` } +type Coordinates struct { + X int32 `json:"x"` + Y int32 `json:"y"` +} + +type Mouse struct { + Type string `json:"type"` // mousedown, mouseup + Coordinates +} + +type Wheel struct { + Delta int32 `json:"delta"` + Coordinates +} + // type ( // ReturnValue uint8 // DeviceID uint8 -// Coordinates struct{ X, Y int32 } // Gyroscope struct{ X, Y, Z float32 } // Acceleration struct{ X, Y, Z float32 } // Quaternion struct{ Q0, Q1, Q2, Q3 float32 } -- cgit v1.2.3