askrobots / network-computer
Public
Your phone is the terminal. Stream a Linux/Mac desktop to an iPhone over WebRTC through NAT, with audio and input. Open source, single binaries.
Dan Quellhorst
Firefox policy: no WebRTC mDNS (a page using WebRTC made Firefox listen on UDP 5353)
a1d92f3
1 month, 3 weeks ago
History
input_windows.go · 5.6 KB · 249 lines
| 1 | //go:build windows |
| 2 | |
| 3 | package input |
| 4 | |
| 5 | import ( |
| 6 | "encoding/binary" |
| 7 | "log" |
| 8 | "sync" |
| 9 | "syscall" |
| 10 | "unsafe" |
| 11 | |
| 12 | "github.com/askrobots/network-computer/internal/proto" |
| 13 | ) |
| 14 | |
| 15 | // Windows injector via SendInput (user32.dll). Pure Go, no cgo. One INPUT is 40 |
| 16 | // bytes on amd64: DWORD type + 4 pad + a 32-byte union. We build the bytes by |
| 17 | // hand to avoid union-layout guesswork. |
| 18 | |
| 19 | var ( |
| 20 | user32 = syscall.NewLazyDLL("user32.dll") |
| 21 | procSend = user32.NewProc("SendInput") |
| 22 | procMetrics = user32.NewProc("GetSystemMetrics") |
| 23 | ) |
| 24 | |
| 25 | const ( |
| 26 | inputMouse = 0 |
| 27 | inputKeyboard = 1 |
| 28 | inputSize = 40 |
| 29 | |
| 30 | mouseMove = 0x0001 |
| 31 | mouseLeftDown = 0x0002 |
| 32 | mouseLeftUp = 0x0004 |
| 33 | mouseRightDown = 0x0008 |
| 34 | mouseRightUp = 0x0010 |
| 35 | mouseMiddleDown = 0x0020 |
| 36 | mouseMiddleUp = 0x0040 |
| 37 | mouseWheel = 0x0800 |
| 38 | mouseHWheel = 0x1000 |
| 39 | mouseAbsolute = 0x8000 |
| 40 | mouseVirtualDsk = 0x4000 |
| 41 | |
| 42 | keyKeyUp = 0x0002 |
| 43 | keyExtended = 0x0001 |
| 44 | wheelDelta = 120 |
| 45 | smXVirtual = 76 |
| 46 | smYVirtual = 77 |
| 47 | smCXVirtual = 78 |
| 48 | smCYVirtual = 79 |
| 49 | ) |
| 50 | |
| 51 | type win struct { |
| 52 | mu sync.Mutex |
| 53 | heldKeys map[uint16]bool |
| 54 | buttons [3]bool |
| 55 | } |
| 56 | |
| 57 | func newPlatform(display int) (Injector, error) { |
| 58 | log.Printf("input: using SendInput (windows)") |
| 59 | return &win{heldKeys: map[uint16]bool{}}, nil |
| 60 | } |
| 61 | |
| 62 | func metric(i uintptr) int32 { |
| 63 | r, _, _ := procMetrics.Call(i) |
| 64 | return int32(r) |
| 65 | } |
| 66 | |
| 67 | func send(buf []byte) { |
| 68 | procSend.Call(1, uintptr(unsafe.Pointer(&buf[0])), uintptr(inputSize)) |
| 69 | } |
| 70 | |
| 71 | func mouseEvent(flags uint32, dx, dy int32, data uint32) { |
| 72 | b := make([]byte, inputSize) |
| 73 | binary.LittleEndian.PutUint32(b[0:], inputMouse) |
| 74 | // union at offset 8: dx, dy, mouseData, dwFlags, time, dwExtraInfo |
| 75 | binary.LittleEndian.PutUint32(b[8:], uint32(dx)) |
| 76 | binary.LittleEndian.PutUint32(b[12:], uint32(dy)) |
| 77 | binary.LittleEndian.PutUint32(b[16:], data) |
| 78 | binary.LittleEndian.PutUint32(b[20:], flags) |
| 79 | send(b) |
| 80 | } |
| 81 | |
| 82 | func keyEvent(vk uint16, flags uint32) { |
| 83 | b := make([]byte, inputSize) |
| 84 | binary.LittleEndian.PutUint32(b[0:], inputKeyboard) |
| 85 | // union at offset 8: wVk, wScan, dwFlags, time, dwExtraInfo |
| 86 | binary.LittleEndian.PutUint16(b[8:], vk) |
| 87 | binary.LittleEndian.PutUint32(b[12:], flags) |
| 88 | send(b) |
| 89 | } |
| 90 | |
| 91 | func (w *win) Handle(e proto.InputEvent) { |
| 92 | w.mu.Lock() |
| 93 | defer w.mu.Unlock() |
| 94 | switch e.T { |
| 95 | case "mm": |
| 96 | // absolute over the virtual desktop, normalised 0..65535 |
| 97 | vx, vy := metric(smXVirtual), metric(smYVirtual) |
| 98 | vw, vh := metric(smCXVirtual), metric(smCYVirtual) |
| 99 | if vw <= 0 || vh <= 0 { |
| 100 | return |
| 101 | } |
| 102 | nx := int32((e.X*float64(vw) + float64(vx)) / float64(vw) * 65535) |
| 103 | ny := int32((e.Y*float64(vh) + float64(vy)) / float64(vh) * 65535) |
| 104 | mouseEvent(mouseMove|mouseAbsolute|mouseVirtualDsk, nx, ny, 0) |
| 105 | case "mr": |
| 106 | mouseEvent(mouseMove, int32(e.DX), int32(e.DY), 0) |
| 107 | case "md", "mu": |
| 108 | down := e.T == "md" |
| 109 | var flag uint32 |
| 110 | switch e.B { |
| 111 | case 0: |
| 112 | flag = pick(down, mouseLeftDown, mouseLeftUp) |
| 113 | case 1: |
| 114 | flag = pick(down, mouseMiddleDown, mouseMiddleUp) |
| 115 | case 2: |
| 116 | flag = pick(down, mouseRightDown, mouseRightUp) |
| 117 | default: |
| 118 | return |
| 119 | } |
| 120 | if e.B >= 0 && e.B < 3 { |
| 121 | w.buttons[e.B] = down |
| 122 | } |
| 123 | mouseEvent(flag, 0, 0, 0) |
| 124 | case "wh": |
| 125 | if e.DY != 0 { |
| 126 | mouseEvent(mouseWheel, 0, 0, uint32(int32(-e.DY/100*wheelDelta))) |
| 127 | } |
| 128 | if e.DX != 0 { |
| 129 | mouseEvent(mouseHWheel, 0, 0, uint32(int32(e.DX/100*wheelDelta))) |
| 130 | } |
| 131 | case "kd", "ku": |
| 132 | k, ok := winVK[e.Code] |
| 133 | if !ok { |
| 134 | log.Printf("input: unmapped key %q", e.Code) |
| 135 | return |
| 136 | } |
| 137 | vk := k.vk |
| 138 | flags := uint32(0) |
| 139 | if k.ext { |
| 140 | flags |= keyExtended |
| 141 | } |
| 142 | down := e.T == "kd" |
| 143 | if !down { |
| 144 | flags |= keyKeyUp |
| 145 | delete(w.heldKeys, vk) |
| 146 | } else { |
| 147 | w.heldKeys[vk] = true |
| 148 | } |
| 149 | keyEvent(vk, flags) |
| 150 | } |
| 151 | } |
| 152 | |
| 153 | func (w *win) ReleaseAll() { |
| 154 | w.mu.Lock() |
| 155 | defer w.mu.Unlock() |
| 156 | for vk := range w.heldKeys { |
| 157 | keyEvent(vk, keyKeyUp) |
| 158 | } |
| 159 | w.heldKeys = map[uint16]bool{} |
| 160 | for i, held := range w.buttons { |
| 161 | if held { |
| 162 | w.buttons[i] = false |
| 163 | switch i { |
| 164 | case 0: |
| 165 | mouseEvent(mouseLeftUp, 0, 0, 0) |
| 166 | case 1: |
| 167 | mouseEvent(mouseMiddleUp, 0, 0, 0) |
| 168 | case 2: |
| 169 | mouseEvent(mouseRightUp, 0, 0, 0) |
| 170 | } |
| 171 | } |
| 172 | } |
| 173 | } |
| 174 | |
| 175 | func pick(cond bool, a, b uint32) uint32 { |
| 176 | if cond { |
| 177 | return a |
| 178 | } |
| 179 | return b |
| 180 | } |
| 181 | |
| 182 | // winVK maps KeyboardEvent.code -> (virtual-key, isExtended). |
| 183 | var winVK = func() map[string]struct { |
| 184 | vk uint16 |
| 185 | ext bool |
| 186 | } { |
| 187 | m := map[string]struct { |
| 188 | vk uint16 |
| 189 | ext bool |
| 190 | }{} |
| 191 | set := func(code string, vk uint16, ext bool) { |
| 192 | m[code] = struct { |
| 193 | vk uint16 |
| 194 | ext bool |
| 195 | }{vk, ext} |
| 196 | } |
| 197 | for c := byte('A'); c <= 'Z'; c++ { |
| 198 | set("Key"+string(c), uint16(c), false) |
| 199 | } |
| 200 | for d := 0; d <= 9; d++ { |
| 201 | set("Digit"+string(rune('0'+d)), uint16('0'+d), false) |
| 202 | } |
| 203 | for f := 1; f <= 12; f++ { |
| 204 | set("F"+itoa(f), uint16(0x70+f-1), false) |
| 205 | } |
| 206 | set("Enter", 0x0D, false) |
| 207 | set("Escape", 0x1B, false) |
| 208 | set("Backspace", 0x08, false) |
| 209 | set("Tab", 0x09, false) |
| 210 | set("Space", 0x20, false) |
| 211 | set("CapsLock", 0x14, false) |
| 212 | set("ShiftLeft", 0xA0, false) |
| 213 | set("ShiftRight", 0xA1, true) |
| 214 | set("ControlLeft", 0xA2, false) |
| 215 | set("ControlRight", 0xA3, true) |
| 216 | set("AltLeft", 0xA4, false) |
| 217 | set("AltRight", 0xA5, true) |
| 218 | set("MetaLeft", 0x5B, true) |
| 219 | set("MetaRight", 0x5C, true) |
| 220 | set("ArrowLeft", 0x25, true) |
| 221 | set("ArrowUp", 0x26, true) |
| 222 | set("ArrowRight", 0x27, true) |
| 223 | set("ArrowDown", 0x28, true) |
| 224 | set("Home", 0x24, true) |
| 225 | set("End", 0x23, true) |
| 226 | set("PageUp", 0x21, true) |
| 227 | set("PageDown", 0x22, true) |
| 228 | set("Insert", 0x2D, true) |
| 229 | set("Delete", 0x2E, true) |
| 230 | set("Semicolon", 0xBA, false) |
| 231 | set("Equal", 0xBB, false) |
| 232 | set("Comma", 0xBC, false) |
| 233 | set("Minus", 0xBD, false) |
| 234 | set("Period", 0xBE, false) |
| 235 | set("Slash", 0xBF, false) |
| 236 | set("Backquote", 0xC0, false) |
| 237 | set("BracketLeft", 0xDB, false) |
| 238 | set("Backslash", 0xDC, false) |
| 239 | set("BracketRight", 0xDD, false) |
| 240 | set("Quote", 0xDE, false) |
| 241 | return m |
| 242 | }() |
| 243 | |
| 244 | func itoa(n int) string { |
| 245 | if n < 10 { |
| 246 | return string(rune('0' + n)) |
| 247 | } |
| 248 | return string(rune('0'+n/10)) + string(rune('0'+n%10)) |
| 249 | } |