summaryrefslogtreecommitdiff
path: root/cmd/magic4linux/main.go
blob: fa98371c50eda5258e413c62b0097390874d5e58 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
package main

import (
	"bytes"
	"context"
	"encoding/binary"
	"fmt"
	"log"
	"os"
	"os/signal"
	"syscall"

	"github.com/bendahl/uinput"

	"github.com/mafredri/magic4linux/m4p"
)

const (
	broadcastPort    = 42830
	subscriptionPort = 42831
)

func main() {
	ctx, cancel := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM)
	defer cancel()

	if err := run(ctx); err != nil {
		panic(err)
	}
}

func run(ctx context.Context) error {
	kbd, err := uinput.CreateKeyboard("/dev/uinput", []byte("magic4linux-keyboard"))
	if err != nil {
		return err
	}
	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
	}
	defer tp.Close()

	d, err := m4p.NewDiscoverer(broadcastPort)
	if err != nil {
		return err
	}
	defer d.Close()

	for {
		select {
		case <-ctx.Done():
			return nil

		case dev := <-d.NextDevice():
			err = connect(ctx, dev, kbd, mouse, tp)
			if err != nil {
				log.Printf("connect: %v", err)
			}
		}
	}
}

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)

	client, err := m4p.Dial(ctx, addr)
	if err != nil {
		return err
	}
	defer client.Close()

	for {
		m, err := client.Recv(ctx)
		if err != nil {
			return err
		}

		switch m.Type {
		case m4p.InputMessage:
			log.Printf("connect: got %s: %v", m.Type, m.Input)

			// PoC Kodi keyboard mapping.
			key := m.Input.Parameters.KeyCode
			switch key {
			case m4p.KeyWheelPressed:
				key = uinput.KeyEnter
			case m4p.KeyChannelUp:
				key = uinput.KeyPageup
			case m4p.KeyChannelDown:
				key = uinput.KeyPagedown
			case m4p.KeyLeft:
				key = uinput.KeyLeft
			case m4p.KeyUp:
				key = uinput.KeyUp
			case m4p.KeyRight:
				key = uinput.KeyRight
			case m4p.KeyDown:
				key = uinput.KeyDown
			case m4p.Key0:
				key = uinput.Key0
			case m4p.Key1:
				key = uinput.Key1
			case m4p.Key2:
				key = uinput.Key2
			case m4p.Key3:
				key = uinput.Key3
			case m4p.Key4:
				key = uinput.Key4
			case m4p.Key5:
				key = uinput.Key5
			case m4p.Key6:
				key = uinput.Key6
			case m4p.Key7:
				key = uinput.Key7
			case m4p.Key8:
				key = uinput.Key8
			case m4p.Key9:
				key = uinput.Key9
			case m4p.KeyRed:
				key = uinput.KeyStop
			case m4p.KeyGreen:
				key = uinput.KeyPlaypause
			case m4p.KeyYellow:
				key = uinput.KeyZ
			case m4p.KeyBlue:
				key = uinput.KeyC
			case m4p.KeyBack:
				key = uinput.KeyBackspace
			}

			if m.Input.Parameters.IsDown {
				kbd.KeyDown(key)
			} else {
				kbd.KeyUp(key)
			}
		case m4p.RemoteUpdateMessage:
			// log.Printf("connect: got %s: %s", m.Type, hex.EncodeToString(m.RemoteUpdate.Payload))

			r := bytes.NewReader(m.RemoteUpdate.Payload)
			var returnValue, deviceID uint8
			var coordinate [2]int32
			var gyroscope, acceleration [3]float32
			var quaternion [4]float32
			for _, fn := range []func() error{
				func() error { return binary.Read(r, binary.LittleEndian, &returnValue) },
				func() error { return binary.Read(r, binary.LittleEndian, &deviceID) },
				func() error { return binary.Read(r, binary.LittleEndian, coordinate[:]) },
				func() error { return binary.Read(r, binary.LittleEndian, gyroscope[:]) },
				func() error { return binary.Read(r, binary.LittleEndian, acceleration[:]) },
				func() error { return binary.Read(r, binary.LittleEndian, quaternion[:]) },
			} {
				if err := fn(); err != nil {
					log.Printf("connect: %s decode failed: %v", m.Type, err)
					break
				}
			}

			x := coordinate[0]
			y := coordinate[1]
			// 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:
		}
	}
}