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_darwin.go · 6.4 KB · 213 lines
| 1 | //go:build darwin |
| 2 | |
| 3 | package input |
| 4 | |
| 5 | /* |
| 6 | #cgo LDFLAGS: -framework ApplicationServices -framework CoreGraphics |
| 7 | #include <ApplicationServices/ApplicationServices.h> |
| 8 | |
| 9 | static int displayBounds(int idx, double *x, double *y, double *w, double *h) { |
| 10 | CGDirectDisplayID ids[16]; |
| 11 | uint32_t n = 0; |
| 12 | if (CGGetActiveDisplayList(16, ids, &n) != kCGErrorSuccess || idx < 0 || (uint32_t)idx >= n) return -1; |
| 13 | CGRect r = CGDisplayBounds(ids[idx]); |
| 14 | *x = r.origin.x; *y = r.origin.y; *w = r.size.width; *h = r.size.height; |
| 15 | return 0; |
| 16 | } |
| 17 | |
| 18 | static void postMouse(int type, double x, double y, int button, uint64_t flags) { |
| 19 | CGEventRef e = CGEventCreateMouseEvent(NULL, (CGEventType)type, CGPointMake(x, y), (CGMouseButton)button); |
| 20 | CGEventSetFlags(e, (CGEventFlags)flags); |
| 21 | CGEventPost(kCGHIDEventTap, e); |
| 22 | CFRelease(e); |
| 23 | } |
| 24 | |
| 25 | static void postScroll(double dx, double dy, uint64_t flags) { |
| 26 | CGEventRef e = CGEventCreateScrollWheelEvent(NULL, kCGScrollEventUnitPixel, 2, (int32_t)dy, (int32_t)dx); |
| 27 | CGEventSetFlags(e, (CGEventFlags)flags); |
| 28 | CGEventPost(kCGHIDEventTap, e); |
| 29 | CFRelease(e); |
| 30 | } |
| 31 | |
| 32 | static void postKey(int keycode, int down, uint64_t flags) { |
| 33 | CGEventRef e = CGEventCreateKeyboardEvent(NULL, (CGKeyCode)keycode, down ? true : false); |
| 34 | CGEventSetFlags(e, (CGEventFlags)flags); |
| 35 | CGEventPost(kCGHIDEventTap, e); |
| 36 | CFRelease(e); |
| 37 | } |
| 38 | */ |
| 39 | import "C" |
| 40 | |
| 41 | import ( |
| 42 | "fmt" |
| 43 | "log" |
| 44 | "sync" |
| 45 | |
| 46 | "github.com/askrobots/network-computer/internal/proto" |
| 47 | ) |
| 48 | |
| 49 | const ( |
| 50 | evMouseMove = 5 |
| 51 | evLeftDown = 1 |
| 52 | evLeftUp = 2 |
| 53 | evRightDown = 3 |
| 54 | evRightUp = 4 |
| 55 | evLeftDragged = 6 |
| 56 | evRightDragged = 7 |
| 57 | evOtherDown = 25 |
| 58 | evOtherUp = 26 |
| 59 | evOtherDragged = 27 |
| 60 | flagShift uint64 = 1 << 17 |
| 61 | flagCtrl uint64 = 1 << 18 |
| 62 | flagAlt uint64 = 1 << 19 |
| 63 | flagCmd uint64 = 1 << 20 |
| 64 | ) |
| 65 | |
| 66 | type mac struct { |
| 67 | mu sync.Mutex |
| 68 | ox, oy float64 // display origin in global points |
| 69 | w, h float64 |
| 70 | x, y float64 |
| 71 | buttons [3]bool |
| 72 | mods uint64 |
| 73 | heldKeys map[int]bool |
| 74 | display int |
| 75 | } |
| 76 | |
| 77 | func newPlatform(display int) (Injector, error) { |
| 78 | // avfoundation screen index == CG active display index in practice |
| 79 | // (both enumerate in the same order); revisit if it is not. |
| 80 | var x, y, w, h C.double |
| 81 | if C.displayBounds(C.int(display), &x, &y, &w, &h) != 0 { |
| 82 | return nil, fmt.Errorf("display %d not found", display) |
| 83 | } |
| 84 | m := &mac{ox: float64(x), oy: float64(y), w: float64(w), h: float64(h), heldKeys: map[int]bool{}, display: display} |
| 85 | log.Printf("input: display %d bounds %vx%v at (%v,%v). Needs Accessibility permission for this process.", display, w, h, x, y) |
| 86 | return m, nil |
| 87 | } |
| 88 | |
| 89 | func (m *mac) Handle(e proto.InputEvent) { |
| 90 | m.mu.Lock() |
| 91 | defer m.mu.Unlock() |
| 92 | switch e.T { |
| 93 | case "mm", "mr": |
| 94 | if e.T == "mm" { |
| 95 | m.x = m.ox + e.X*m.w |
| 96 | m.y = m.oy + e.Y*m.h |
| 97 | } else { |
| 98 | m.x = min(max(m.x+e.DX, m.ox), m.ox+m.w-1) |
| 99 | m.y = min(max(m.y+e.DY, m.oy), m.oy+m.h-1) |
| 100 | } |
| 101 | typ := evMouseMove |
| 102 | switch { |
| 103 | case m.buttons[0]: |
| 104 | typ = evLeftDragged |
| 105 | case m.buttons[2]: |
| 106 | typ = evRightDragged |
| 107 | case m.buttons[1]: |
| 108 | typ = evOtherDragged |
| 109 | } |
| 110 | C.postMouse(C.int(typ), C.double(m.x), C.double(m.y), C.int(m.cgButton()), C.uint64_t(m.mods)) |
| 111 | case "md", "mu": |
| 112 | down := e.T == "md" |
| 113 | b := e.B |
| 114 | if b < 0 || b > 2 { |
| 115 | return |
| 116 | } |
| 117 | m.buttons[b] = down |
| 118 | var typ, btn int |
| 119 | switch b { |
| 120 | case 0: |
| 121 | typ, btn = evLeftDown, 0 |
| 122 | case 2: |
| 123 | typ, btn = evRightDown, 1 |
| 124 | case 1: |
| 125 | typ, btn = evOtherDown, 2 |
| 126 | } |
| 127 | if !down { |
| 128 | typ++ // Up is Down+1 for all three |
| 129 | } |
| 130 | C.postMouse(C.int(typ), C.double(m.x), C.double(m.y), C.int(btn), C.uint64_t(m.mods)) |
| 131 | case "wh": |
| 132 | C.postScroll(C.double(-e.DX), C.double(-e.DY), C.uint64_t(m.mods)) |
| 133 | case "kd", "ku": |
| 134 | kc, ok := keyCodes[e.Code] |
| 135 | if !ok { |
| 136 | log.Printf("input: unmapped key %q", e.Code) |
| 137 | return |
| 138 | } |
| 139 | down := e.T == "kd" |
| 140 | if f, isMod := modFlags[e.Code]; isMod { |
| 141 | if down { |
| 142 | m.mods |= f |
| 143 | } else { |
| 144 | m.mods &^= f |
| 145 | } |
| 146 | } |
| 147 | if down { |
| 148 | m.heldKeys[kc] = true |
| 149 | } else { |
| 150 | delete(m.heldKeys, kc) |
| 151 | } |
| 152 | C.postKey(C.int(kc), boolInt(down), C.uint64_t(m.mods)) |
| 153 | } |
| 154 | } |
| 155 | |
| 156 | func (m *mac) cgButton() int { |
| 157 | switch { |
| 158 | case m.buttons[2]: |
| 159 | return 1 |
| 160 | case m.buttons[1]: |
| 161 | return 2 |
| 162 | } |
| 163 | return 0 |
| 164 | } |
| 165 | |
| 166 | func (m *mac) ReleaseAll() { |
| 167 | m.mu.Lock() |
| 168 | defer m.mu.Unlock() |
| 169 | for kc := range m.heldKeys { |
| 170 | C.postKey(C.int(kc), 0, 0) |
| 171 | } |
| 172 | m.heldKeys = map[int]bool{} |
| 173 | m.mods = 0 |
| 174 | for b, held := range m.buttons { |
| 175 | if held { |
| 176 | m.Handle(proto.InputEvent{T: "mu", B: b}) |
| 177 | } |
| 178 | } |
| 179 | } |
| 180 | |
| 181 | func boolInt(b bool) C.int { |
| 182 | if b { |
| 183 | return 1 |
| 184 | } |
| 185 | return 0 |
| 186 | } |
| 187 | |
| 188 | var modFlags = map[string]uint64{ |
| 189 | "ShiftLeft": flagShift, "ShiftRight": flagShift, |
| 190 | "ControlLeft": flagCtrl, "ControlRight": flagCtrl, |
| 191 | "AltLeft": flagAlt, "AltRight": flagAlt, |
| 192 | "MetaLeft": flagCmd, "MetaRight": flagCmd, |
| 193 | } |
| 194 | |
| 195 | // keyCodes maps KeyboardEvent.code to macOS virtual key codes (Carbon kVK_*). |
| 196 | var keyCodes = map[string]int{ |
| 197 | "KeyA": 0x00, "KeyS": 0x01, "KeyD": 0x02, "KeyF": 0x03, "KeyH": 0x04, "KeyG": 0x05, "KeyZ": 0x06, "KeyX": 0x07, |
| 198 | "KeyC": 0x08, "KeyV": 0x09, "KeyB": 0x0B, "KeyQ": 0x0C, "KeyW": 0x0D, "KeyE": 0x0E, "KeyR": 0x0F, "KeyY": 0x10, |
| 199 | "KeyT": 0x11, "Digit1": 0x12, "Digit2": 0x13, "Digit3": 0x14, "Digit4": 0x15, "Digit6": 0x16, "Digit5": 0x17, |
| 200 | "Equal": 0x18, "Digit9": 0x19, "Digit7": 0x1A, "Minus": 0x1B, "Digit8": 0x1C, "Digit0": 0x1D, "BracketRight": 0x1E, |
| 201 | "KeyO": 0x1F, "KeyU": 0x20, "BracketLeft": 0x21, "KeyI": 0x22, "KeyP": 0x23, "Enter": 0x24, "KeyL": 0x25, "KeyJ": 0x26, |
| 202 | "Quote": 0x27, "KeyK": 0x28, "Semicolon": 0x29, "Backslash": 0x2A, "Comma": 0x2B, "Slash": 0x2C, "KeyN": 0x2D, |
| 203 | "KeyM": 0x2E, "Period": 0x2F, "Tab": 0x30, "Space": 0x31, "Backquote": 0x32, "Backspace": 0x33, "Escape": 0x35, |
| 204 | "MetaRight": 0x36, "MetaLeft": 0x37, "ShiftLeft": 0x38, "CapsLock": 0x39, "AltLeft": 0x3A, "ControlLeft": 0x3B, |
| 205 | "ShiftRight": 0x3C, "AltRight": 0x3D, "ControlRight": 0x3E, "Fn": 0x3F, |
| 206 | "NumpadDecimal": 0x41, "NumpadMultiply": 0x43, "NumpadAdd": 0x45, "NumLock": 0x47, "NumpadDivide": 0x4B, |
| 207 | "NumpadEnter": 0x4C, "NumpadSubtract": 0x4E, "NumpadEqual": 0x51, "Numpad0": 0x52, "Numpad1": 0x53, "Numpad2": 0x54, |
| 208 | "Numpad3": 0x55, "Numpad4": 0x56, "Numpad5": 0x57, "Numpad6": 0x58, "Numpad7": 0x59, "Numpad8": 0x5B, "Numpad9": 0x5C, |
| 209 | "F5": 0x60, "F6": 0x61, "F7": 0x62, "F3": 0x63, "F8": 0x64, "F9": 0x65, "F11": 0x67, "F13": 0x69, "F16": 0x6A, |
| 210 | "F14": 0x6B, "F10": 0x6D, "F12": 0x6F, "F15": 0x71, "Help": 0x72, "Insert": 0x72, "Home": 0x73, "PageUp": 0x74, |
| 211 | "Delete": 0x75, "F4": 0x76, "End": 0x77, "F2": 0x78, "PageDown": 0x79, "F1": 0x7A, "ArrowLeft": 0x7B, |
| 212 | "ArrowRight": 0x7C, "ArrowDown": 0x7D, "ArrowUp": 0x7E, |
| 213 | } |