demo / network-computer
Public
Copy of github.com/askrobots/network-computer (MIT): a desk you reach from anything. The original lives on GitHub.
GitStable demo
README: say this is a copy and where the original is
cde82c2
13 hours, 41 minutes ago
History
input_linux.go · 8.1 KB · 278 lines
| 1 | //go:build linux |
| 2 | |
| 3 | package input |
| 4 | |
| 5 | import ( |
| 6 | "encoding/binary" |
| 7 | "fmt" |
| 8 | "log" |
| 9 | "os" |
| 10 | "sync" |
| 11 | "syscall" |
| 12 | "time" |
| 13 | "unsafe" |
| 14 | |
| 15 | "github.com/askrobots/network-computer/internal/proto" |
| 16 | ) |
| 17 | |
| 18 | // Linux injector on /dev/uinput: one virtual device that is a keyboard, a |
| 19 | // relative mouse and an absolute tablet at once. Works under X11 and Wayland |
| 20 | // with no display-server-specific code. Needs write access to /dev/uinput |
| 21 | // (root, or a udev rule giving the "input" group access). |
| 22 | |
| 23 | const ( |
| 24 | evSyn = 0x00 |
| 25 | evKey = 0x01 |
| 26 | evRel = 0x02 |
| 27 | evAbs = 0x03 |
| 28 | |
| 29 | relX = 0x00 |
| 30 | relY = 0x01 |
| 31 | relHWheel = 0x06 |
| 32 | relWheel = 0x08 |
| 33 | absX = 0x00 |
| 34 | absY = 0x01 |
| 35 | |
| 36 | btnLeft = 0x110 |
| 37 | btnRight = 0x111 |
| 38 | btnMiddle = 0x112 |
| 39 | |
| 40 | uiSetEvBit = 0x40045564 |
| 41 | uiSetKeyBit = 0x40045565 |
| 42 | uiSetRelBit = 0x40045566 |
| 43 | uiSetAbsBit = 0x40045567 |
| 44 | uiDevCreate = 0x5501 |
| 45 | uiDevDestroy = 0x5502 |
| 46 | uiDevSetup = 0x405c5503 // _IOW(UINPUT_IOCTL_BASE, 3, struct uinput_setup) |
| 47 | uiAbsSetup = 0x401c5504 // _IOW(UINPUT_IOCTL_BASE, 4, struct uinput_abs_setup) |
| 48 | |
| 49 | absRange = 32767 |
| 50 | ) |
| 51 | |
| 52 | type inputEvent struct { |
| 53 | Sec, Usec int64 |
| 54 | Type uint16 |
| 55 | Code uint16 |
| 56 | Value int32 |
| 57 | } |
| 58 | |
| 59 | type uinputSetup struct { |
| 60 | Bustype uint16 |
| 61 | Vendor uint16 |
| 62 | Product uint16 |
| 63 | Version uint16 |
| 64 | Name [80]byte |
| 65 | FFMax uint32 |
| 66 | } |
| 67 | |
| 68 | type uinputAbsSetup struct { |
| 69 | Code uint16 |
| 70 | _ uint16 |
| 71 | Value, Minimum, Maximum, Fuzz, Flat, Resolution int32 |
| 72 | } |
| 73 | |
| 74 | // Two devices, because libinput treats anything with relative axes as a mouse |
| 75 | // and ignores its absolute events. "kbd" is a keyboard plus relative mouse and |
| 76 | // wheel; "tab" is an absolute pointer shaped like the QEMU USB tablet. |
| 77 | // Three devices, because libinput classifies a device by its axes: a device |
| 78 | // with relative axes is a mouse and its key events are ignored, and a device |
| 79 | // with both relative and absolute axes ignores the absolute ones. So the |
| 80 | // keyboard, the relative mouse and the absolute tablet must be separate. |
| 81 | type linux struct { |
| 82 | mu sync.Mutex |
| 83 | kbd, mouse, tab *os.File |
| 84 | heldKeys map[uint16]bool |
| 85 | buttons [3]bool |
| 86 | } |
| 87 | |
| 88 | const ( |
| 89 | kindKeyboard = iota |
| 90 | kindMouse |
| 91 | kindTablet |
| 92 | ) |
| 93 | |
| 94 | func newPlatform(display int) (Injector, error) { |
| 95 | kbd, err := createDevice("network-computer keyboard", kindKeyboard) |
| 96 | if err != nil { |
| 97 | return nil, err |
| 98 | } |
| 99 | mouse, err := createDevice("network-computer mouse", kindMouse) |
| 100 | if err != nil { |
| 101 | kbd.Close() |
| 102 | return nil, err |
| 103 | } |
| 104 | tab, err := createDevice("network-computer tablet", kindTablet) |
| 105 | if err != nil { |
| 106 | kbd.Close() |
| 107 | mouse.Close() |
| 108 | return nil, err |
| 109 | } |
| 110 | time.Sleep(300 * time.Millisecond) // let the display server pick the devices up |
| 111 | log.Printf("input: uinput devices created (keyboard, mouse, tablet)") |
| 112 | return &linux{kbd: kbd, mouse: mouse, tab: tab, heldKeys: map[uint16]bool{}}, nil |
| 113 | } |
| 114 | |
| 115 | func createDevice(name string, kind int) (*os.File, error) { |
| 116 | f, err := os.OpenFile("/dev/uinput", os.O_WRONLY|syscall.O_NONBLOCK, 0) |
| 117 | if err != nil { |
| 118 | return nil, fmt.Errorf("open /dev/uinput: %w (run as root or add a udev rule)", err) |
| 119 | } |
| 120 | ioctl := func(req, val uintptr) error { |
| 121 | if _, _, e := syscall.Syscall(syscall.SYS_IOCTL, f.Fd(), req, val); e != 0 { |
| 122 | return e |
| 123 | } |
| 124 | return nil |
| 125 | } |
| 126 | must := func(err error) { |
| 127 | if err != nil { |
| 128 | log.Printf("uinput ioctl: %v", err) |
| 129 | } |
| 130 | } |
| 131 | must(ioctl(uiSetEvBit, evKey)) |
| 132 | must(ioctl(uiSetEvBit, evSyn)) |
| 133 | switch kind { |
| 134 | case kindKeyboard: |
| 135 | // keyboard keys only, no buttons and no axes, so libinput makes a keyboard |
| 136 | for k := uintptr(1); k < 256; k++ { |
| 137 | must(ioctl(uiSetKeyBit, k)) |
| 138 | } |
| 139 | case kindMouse: |
| 140 | must(ioctl(uiSetEvBit, evRel)) |
| 141 | for _, b := range []uintptr{btnLeft, btnRight, btnMiddle} { |
| 142 | must(ioctl(uiSetKeyBit, b)) |
| 143 | } |
| 144 | for _, r := range []uintptr{relX, relY, relWheel, relHWheel} { |
| 145 | must(ioctl(uiSetRelBit, r)) |
| 146 | } |
| 147 | case kindTablet: |
| 148 | must(ioctl(uiSetEvBit, evAbs)) |
| 149 | for _, b := range []uintptr{btnLeft, btnRight, btnMiddle} { |
| 150 | must(ioctl(uiSetKeyBit, b)) |
| 151 | } |
| 152 | for _, a := range []uint16{absX, absY} { |
| 153 | must(ioctl(uiSetAbsBit, uintptr(a))) |
| 154 | as := uinputAbsSetup{Code: a, Minimum: 0, Maximum: absRange} |
| 155 | must(ioctl(uiAbsSetup, uintptr(unsafe.Pointer(&as)))) |
| 156 | } |
| 157 | } |
| 158 | us := uinputSetup{Bustype: 0x03, Vendor: 0x1d6b, Product: 0x0104, Version: 1} |
| 159 | copy(us.Name[:], name) |
| 160 | if err := ioctl(uiDevSetup, uintptr(unsafe.Pointer(&us))); err != nil { |
| 161 | f.Close() |
| 162 | return nil, fmt.Errorf("UI_DEV_SETUP: %w", err) |
| 163 | } |
| 164 | if err := ioctl(uiDevCreate, 0); err != nil { |
| 165 | f.Close() |
| 166 | return nil, fmt.Errorf("UI_DEV_CREATE: %w", err) |
| 167 | } |
| 168 | return f, nil |
| 169 | } |
| 170 | |
| 171 | func (l *linux) emit(f *os.File, typ, code uint16, val int32) { |
| 172 | ev := inputEvent{Type: typ, Code: code, Value: val} |
| 173 | buf := make([]byte, unsafe.Sizeof(ev)) |
| 174 | binary.LittleEndian.PutUint64(buf[0:], uint64(ev.Sec)) |
| 175 | binary.LittleEndian.PutUint64(buf[8:], uint64(ev.Usec)) |
| 176 | binary.LittleEndian.PutUint16(buf[16:], ev.Type) |
| 177 | binary.LittleEndian.PutUint16(buf[18:], ev.Code) |
| 178 | binary.LittleEndian.PutUint32(buf[20:], uint32(ev.Value)) |
| 179 | f.Write(buf) |
| 180 | } |
| 181 | |
| 182 | func (l *linux) syn(f *os.File) { l.emit(f, evSyn, 0, 0) } |
| 183 | |
| 184 | func (l *linux) Handle(e proto.InputEvent) { |
| 185 | l.mu.Lock() |
| 186 | defer l.mu.Unlock() |
| 187 | switch e.T { |
| 188 | case "mm": |
| 189 | l.emit(l.tab, evAbs, absX, int32(e.X*absRange)) |
| 190 | l.emit(l.tab, evAbs, absY, int32(e.Y*absRange)) |
| 191 | l.syn(l.tab) |
| 192 | case "mr": |
| 193 | l.emit(l.mouse, evRel, relX, int32(e.DX)) |
| 194 | l.emit(l.mouse, evRel, relY, int32(e.DY)) |
| 195 | l.syn(l.mouse) |
| 196 | case "md", "mu": |
| 197 | var code uint16 |
| 198 | switch e.B { |
| 199 | case 0: |
| 200 | code = btnLeft |
| 201 | case 1: |
| 202 | code = btnMiddle |
| 203 | case 2: |
| 204 | code = btnRight |
| 205 | default: |
| 206 | return |
| 207 | } |
| 208 | down := e.T == "md" |
| 209 | l.buttons[e.B] = down |
| 210 | l.emit(l.tab, evKey, code, boolInt32(down)) |
| 211 | l.syn(l.tab) |
| 212 | case "wh": |
| 213 | // browsers send pixels; one wheel notch is about 100px |
| 214 | if n := int32(-e.DY / 50); n != 0 { |
| 215 | l.emit(l.mouse, evRel, relWheel, n) |
| 216 | } |
| 217 | if n := int32(e.DX / 50); n != 0 { |
| 218 | l.emit(l.mouse, evRel, relHWheel, n) |
| 219 | } |
| 220 | l.syn(l.mouse) |
| 221 | case "kd", "ku": |
| 222 | kc, ok := linuxKeyCodes[e.Code] |
| 223 | if !ok { |
| 224 | log.Printf("input: unmapped key %q", e.Code) |
| 225 | return |
| 226 | } |
| 227 | down := e.T == "kd" |
| 228 | if down { |
| 229 | l.heldKeys[kc] = true |
| 230 | } else { |
| 231 | delete(l.heldKeys, kc) |
| 232 | } |
| 233 | l.emit(l.kbd, evKey, kc, boolInt32(down)) |
| 234 | l.syn(l.kbd) |
| 235 | } |
| 236 | } |
| 237 | |
| 238 | func (l *linux) ReleaseAll() { |
| 239 | l.mu.Lock() |
| 240 | defer l.mu.Unlock() |
| 241 | for kc := range l.heldKeys { |
| 242 | l.emit(l.kbd, evKey, kc, 0) |
| 243 | } |
| 244 | l.heldKeys = map[uint16]bool{} |
| 245 | l.syn(l.kbd) |
| 246 | for i, code := range []uint16{btnLeft, btnMiddle, btnRight} { |
| 247 | if l.buttons[i] { |
| 248 | l.emit(l.tab, evKey, code, 0) |
| 249 | l.buttons[i] = false |
| 250 | } |
| 251 | } |
| 252 | l.syn(l.tab) |
| 253 | } |
| 254 | |
| 255 | func boolInt32(b bool) int32 { |
| 256 | if b { |
| 257 | return 1 |
| 258 | } |
| 259 | return 0 |
| 260 | } |
| 261 | |
| 262 | // linuxKeyCodes maps KeyboardEvent.code to Linux KEY_* codes (input-event-codes.h). |
| 263 | var linuxKeyCodes = map[string]uint16{ |
| 264 | "Escape": 1, "Digit1": 2, "Digit2": 3, "Digit3": 4, "Digit4": 5, "Digit5": 6, "Digit6": 7, "Digit7": 8, "Digit8": 9, |
| 265 | "Digit9": 10, "Digit0": 11, "Minus": 12, "Equal": 13, "Backspace": 14, "Tab": 15, "KeyQ": 16, "KeyW": 17, "KeyE": 18, |
| 266 | "KeyR": 19, "KeyT": 20, "KeyY": 21, "KeyU": 22, "KeyI": 23, "KeyO": 24, "KeyP": 25, "BracketLeft": 26, |
| 267 | "BracketRight": 27, "Enter": 28, "ControlLeft": 29, "KeyA": 30, "KeyS": 31, "KeyD": 32, "KeyF": 33, "KeyG": 34, |
| 268 | "KeyH": 35, "KeyJ": 36, "KeyK": 37, "KeyL": 38, "Semicolon": 39, "Quote": 40, "Backquote": 41, "ShiftLeft": 42, |
| 269 | "Backslash": 43, "KeyZ": 44, "KeyX": 45, "KeyC": 46, "KeyV": 47, "KeyB": 48, "KeyN": 49, "KeyM": 50, "Comma": 51, |
| 270 | "Period": 52, "Slash": 53, "ShiftRight": 54, "NumpadMultiply": 55, "AltLeft": 56, "Space": 57, "CapsLock": 58, |
| 271 | "F1": 59, "F2": 60, "F3": 61, "F4": 62, "F5": 63, "F6": 64, "F7": 65, "F8": 66, "F9": 67, "F10": 68, "NumLock": 69, |
| 272 | "ScrollLock": 70, "Numpad7": 71, "Numpad8": 72, "Numpad9": 73, "NumpadSubtract": 74, "Numpad4": 75, "Numpad5": 76, |
| 273 | "Numpad6": 77, "NumpadAdd": 78, "Numpad1": 79, "Numpad2": 80, "Numpad3": 81, "Numpad0": 82, "NumpadDecimal": 83, |
| 274 | "F11": 87, "F12": 88, "NumpadEnter": 96, "ControlRight": 97, "NumpadDivide": 98, "PrintScreen": 99, "AltRight": 100, |
| 275 | "Home": 102, "ArrowUp": 103, "PageUp": 104, "ArrowLeft": 105, "ArrowRight": 106, "End": 107, "ArrowDown": 108, |
| 276 | "PageDown": 109, "Insert": 110, "Delete": 111, "Pause": 119, "MetaLeft": 125, "MetaRight": 126, "ContextMenu": 127, |
| 277 | "F13": 183, "F14": 184, "F15": 185, "F16": 186, "F17": 187, "F18": 188, "F19": 189, |
| 278 | } |