| 1 | package workbench |
| 2 | |
| 3 | import ( |
| 4 | "bytes" |
| 5 | "context" |
| 6 | "encoding/base64" |
| 7 | "encoding/json" |
| 8 | "errors" |
| 9 | "image" |
| 10 | "image/jpeg" |
| 11 | "math" |
| 12 | "net/http" |
| 13 | "time" |
| 14 | |
| 15 | "golang.org/x/image/draw" |
| 16 | ) |
| 17 | |
| 18 | |
| 19 | |
| 20 | type windowsPhoneDesktopSession struct { |
| 21 | connection *nativeWindowConnection |
| 22 | id, token string |
| 23 | width, height int |
| 24 | ratio, density float64 |
| 25 | frame windowsWindowFrame |
| 26 | jpeg string |
| 27 | focus string |
| 28 | idle *time.Timer |
| 29 | } |
| 30 | |
| 31 | func finite(value float64) bool { return !math.IsNaN(value) && !math.IsInf(value, 0) } |
| 32 | |
| 33 | func (p *windowsPhoneDesktopSession) close() { |
| 34 | if p.idle != nil { |
| 35 | p.idle.Stop() |
| 36 | } |
| 37 | p.connection.Close() |
| 38 | } |
| 39 | |
| 40 | func (s *Server) windowsPhoneDesktop(ctx context.Context, token, op string, input phoneSurfaceInput) ([]byte, int, error) { |
| 41 | state := &s.phones.desktop |
| 42 | p := state.nativeDesktop |
| 43 | if op == "close" { |
| 44 | if p != nil && p.id == input.ID && equalSecret(p.token, token) { |
| 45 | state.nativeDesktop = nil |
| 46 | p.close() |
| 47 | } |
| 48 | out, err := json.Marshal(map[string]string{"id": input.ID}) |
| 49 | return out, http.StatusOK, err |
| 50 | } |
| 51 | lease := state.session |
| 52 | if token == "" || lease == nil || lease.id != input.ID || !equalSecret(lease.token, token) || lease.ctx.Err() != nil { |
| 53 | return nil, http.StatusGone, nil |
| 54 | } |
| 55 | if p == nil { |
| 56 | if op != "open" { |
| 57 | return nil, http.StatusGone, nil |
| 58 | } |
| 59 | connection, err := s.windowsWindowConnection(lease.ctx) |
| 60 | if err != nil { |
| 61 | return nil, http.StatusBadGateway, err |
| 62 | } |
| 63 | p = &windowsPhoneDesktopSession{connection: connection, id: input.ID, token: token} |
| 64 | p.idle = time.AfterFunc(2*time.Minute, lease.cancel) |
| 65 | state.nativeDesktop = p |
| 66 | } |
| 67 | if p.id != input.ID || !equalSecret(p.token, token) { |
| 68 | return nil, http.StatusGone, nil |
| 69 | } |
| 70 | p.idle.Reset(2 * time.Minute) |
| 71 | operation, cancel := context.WithTimeout(ctx, 12*time.Second) |
| 72 | defer cancel() |
| 73 | stop := context.AfterFunc(operation, p.connection.Close) |
| 74 | defer stop() |
| 75 | var out []byte |
| 76 | var err error |
| 77 | switch op { |
| 78 | case "open": |
| 79 | var status struct { |
| 80 | Installed, Running bool |
| 81 | PhoneSupported bool `json:"phone_supported"` |
| 82 | Density float64 `json:"display_scale"` |
| 83 | } |
| 84 | if err = p.connection.request(map[string]any{"action": "status"}, &status); err != nil { |
| 85 | break |
| 86 | } |
| 87 | if !status.PhoneSupported { |
| 88 | return nil, http.StatusUpgradeRequired, nil |
| 89 | } |
| 90 | if !status.Installed { |
| 91 | return nil, http.StatusNotImplemented, nil |
| 92 | } |
| 93 | if !status.Running { |
| 94 | return nil, http.StatusPreconditionFailed, nil |
| 95 | } |
| 96 | p.density = status.Density |
| 97 | if !finite(p.density) || p.density < 1 || p.density > 8 { |
| 98 | return nil, http.StatusBadGateway, nil |
| 99 | } |
| 100 | err = p.resize(input) |
| 101 | case "frame": |
| 102 | out, err = p.capture() |
| 103 | case "action": |
| 104 | if input.Type == "resize" { |
| 105 | err = p.resize(input) |
| 106 | } else { |
| 107 | err = p.action(input) |
| 108 | } |
| 109 | default: |
| 110 | return nil, http.StatusBadRequest, nil |
| 111 | } |
| 112 | if err != nil { |
| 113 | status := windowsPhoneDesktopStatus(err) |
| 114 | var failure windowsWindowFailure |
| 115 | if errors.As(err, &failure) && failure.code == "requires_gui_login" { |
| 116 | return nil, status, err |
| 117 | } |
| 118 | return nil, status, nil |
| 119 | } |
| 120 | if operation.Err() != nil { |
| 121 | return nil, http.StatusGone, nil |
| 122 | } |
| 123 | if out == nil { |
| 124 | out, err = json.Marshal(map[string]string{"id": input.ID}) |
| 125 | } |
| 126 | return out, http.StatusOK, err |
| 127 | } |
| 128 | |
| 129 | func windowsPhoneDesktopStatus(err error) int { |
| 130 | var failure windowsWindowFailure |
| 131 | if !errors.As(err, &failure) { |
| 132 | return http.StatusGone |
| 133 | } |
| 134 | switch failure.code { |
| 135 | case "desktop_not_installed": |
| 136 | return http.StatusNotImplemented |
| 137 | case "desktop_not_running", "desktop_capture_busy": |
| 138 | return http.StatusServiceUnavailable |
| 139 | case "desktop_target_changed", "desktop_phone_focus_changed": |
| 140 | return http.StatusConflict |
| 141 | case "requires_gui_login": |
| 142 | return http.StatusGone |
| 143 | case "desktop_size_invalid", "desktop_pointer_invalid", "desktop_key_invalid", "desktop_text_limit", "desktop_request_invalid": |
| 144 | return http.StatusBadRequest |
| 145 | default: |
| 146 | return http.StatusBadGateway |
| 147 | } |
| 148 | } |
| 149 | |
| 150 | func (p *windowsPhoneDesktopSession) resize(input phoneSurfaceInput) error { |
| 151 | if !input.valid(phoneDesktop, "open") { |
| 152 | return windowsWindowFailure{code: "desktop_size_invalid"} |
| 153 | } |
| 154 | ratio := input.PixelRatio |
| 155 | if ratio == 0 { |
| 156 | ratio = 1 |
| 157 | } |
| 158 | width := int(math.Round(float64(max(320, input.Width)) * p.density)) |
| 159 | height := int(math.Round(float64(max(240, input.Height)) * p.density)) |
| 160 | if err := p.connection.request(map[string]any{"action": "resize", "width": width, "height": height}, nil); err != nil { |
| 161 | return err |
| 162 | } |
| 163 | p.width, p.height, p.ratio = input.Width, input.Height, ratio |
| 164 | p.frame, p.jpeg, p.focus = windowsWindowFrame{}, "", "" |
| 165 | return nil |
| 166 | } |
| 167 | |
| 168 | func (p *windowsPhoneDesktopSession) capture() ([]byte, error) { |
| 169 | var frame windowsWindowFrame |
| 170 | if err := p.connection.request(map[string]any{"action": "frame", "after": p.frame.Sequence}, &frame); err != nil { |
| 171 | return nil, err |
| 172 | } |
| 173 | if !frame.Unchanged { |
| 174 | if !finite(frame.Density) || frame.Density < 1 || frame.Density > 8 { |
| 175 | return nil, windowsWindowFailure{code: "desktop_frame_invalid"} |
| 176 | } |
| 177 | encoded, err := windowsPhoneJPEG(frame, p.width, p.height, p.ratio) |
| 178 | if err != nil { |
| 179 | return nil, err |
| 180 | } |
| 181 | p.frame, p.jpeg, p.density = frame, encoded, frame.Density |
| 182 | } else if frame.Sequence == "" || frame.Sequence != p.frame.Sequence { |
| 183 | return nil, windowsWindowFailure{code: "desktop_frame_invalid"} |
| 184 | } |
| 185 | if p.jpeg == "" || p.frame.Target == "" || len(p.frame.Target) > 128 { |
| 186 | return nil, windowsWindowFailure{code: "desktop_frame_invalid"} |
| 187 | } |
| 188 | var focus struct { |
| 189 | Available bool `json:"available"` |
| 190 | Context string `json:"context"` |
| 191 | Password bool `json:"password"` |
| 192 | } |
| 193 | err := p.connection.request(map[string]any{"action": "ime", "mode": "status"}, &focus) |
| 194 | if err != nil && windowsPhoneDesktopStatus(err) != http.StatusConflict { |
| 195 | return nil, err |
| 196 | } |
| 197 | p.focus = "" |
| 198 | if err == nil && focus.Available && !focus.Password && len(focus.Context) <= 128 { |
| 199 | p.focus = focus.Context |
| 200 | } |
| 201 | return json.Marshal(map[string]any{ |
| 202 | "id": p.id, "document": p.frame.Target, "image": p.jpeg, |
| 203 | "width": p.width, "height": p.height, "content_width": float64(p.frame.Width) / p.density, "content_height": float64(p.frame.Height) / p.density, |
| 204 | "focus": p.focus, "password": focus.Password, "window": "desktop", "shared": true, |
| 205 | "login_supported": false, "pixel_ratio_control": true, "requested_pixel_ratio": p.ratio, |
| 206 | }) |
| 207 | } |
| 208 | |
| 209 | func (p *windowsPhoneDesktopSession) action(input phoneSurfaceInput) error { |
| 210 | if p.frame.Target == "" || input.Document != p.frame.Target { |
| 211 | return windowsWindowFailure{code: "desktop_target_changed"} |
| 212 | } |
| 213 | request := map[string]any{"target": p.frame.Target} |
| 214 | send := func() error { |
| 215 | err := p.connection.request(request, nil) |
| 216 | if err != nil { |
| 217 | |
| 218 | |
| 219 | p.connection.Close() |
| 220 | } |
| 221 | return err |
| 222 | } |
| 223 | typing := input.Type == "text" || input.Type == "key" && input.Key != "Tab" && input.Key != "Escape" && input.Key != "ArrowUp" && input.Key != "ArrowDown" && input.Key != "ArrowLeft" && input.Key != "ArrowRight" |
| 224 | if typing { |
| 225 | if p.focus == "" || input.Focus != p.focus { |
| 226 | return windowsWindowFailure{code: "desktop_phone_focus_changed"} |
| 227 | } |
| 228 | request["phone_focus"] = p.focus |
| 229 | } |
| 230 | switch input.Type { |
| 231 | case "tap", "scroll": |
| 232 | if input.X == nil || input.Y == nil || !finite(*input.X) || !finite(*input.Y) || *input.X < 0 || *input.Y < 0 || *input.X >= float64(p.frame.Width)/p.density || *input.Y >= float64(p.frame.Height)/p.density { |
| 233 | return windowsWindowFailure{code: "desktop_pointer_invalid"} |
| 234 | } |
| 235 | request["action"], request["x"], request["y"] = "pointer", int(*input.X*p.density), int(*input.Y*p.density) |
| 236 | masks := []int{1} |
| 237 | if input.Type == "scroll" { |
| 238 | if input.DX == nil || input.DY == nil || !finite(*input.DX) || !finite(*input.DY) || math.Abs(*input.DX) > 3200 || math.Abs(*input.DY) > 3200 { |
| 239 | return windowsWindowFailure{code: "desktop_pointer_invalid"} |
| 240 | } |
| 241 | masks = nil |
| 242 | for _, axis := range []struct { |
| 243 | delta float64 |
| 244 | negative, positive int |
| 245 | }{{*input.DY, 8, 16}, {*input.DX, 32, 64}} { |
| 246 | mask := axis.positive |
| 247 | if axis.delta < 0 { |
| 248 | mask = axis.negative |
| 249 | } |
| 250 | for steps := int(math.Ceil(math.Abs(axis.delta) / 120)); steps > 0; steps-- { |
| 251 | masks = append(masks, mask) |
| 252 | } |
| 253 | } |
| 254 | } |
| 255 | for _, mask := range masks { |
| 256 | request["mask"] = mask |
| 257 | if err := send(); err != nil { |
| 258 | return err |
| 259 | } |
| 260 | request["mask"] = 0 |
| 261 | if err := send(); err != nil { |
| 262 | return err |
| 263 | } |
| 264 | } |
| 265 | return nil |
| 266 | case "text": |
| 267 | request["action"], request["text"] = "text", input.Text |
| 268 | case "key": |
| 269 | if len(input.Key) == 1 && input.Key[0] >= 0x20 && input.Key[0] <= 0x7e { |
| 270 | request["action"], request["text"] = "text", input.Key |
| 271 | break |
| 272 | } |
| 273 | keys := map[string]uint32{"Backspace": 0xff08, "Tab": 0xff09, "Enter": 0xff0d, "Escape": 0xff1b, "Delete": 0xffff, "ArrowLeft": 0xff51, "ArrowUp": 0xff52, "ArrowRight": 0xff53, "ArrowDown": 0xff54} |
| 274 | key := keys[input.Key] |
| 275 | if key == 0 { |
| 276 | return windowsWindowFailure{code: "desktop_key_invalid"} |
| 277 | } |
| 278 | request["action"], request["keysym"], request["down"] = "key", key, true |
| 279 | if err := send(); err != nil { |
| 280 | return err |
| 281 | } |
| 282 | request["down"] = false |
| 283 | delete(request, "phone_focus") |
| 284 | default: |
| 285 | return windowsWindowFailure{code: "desktop_request_invalid"} |
| 286 | } |
| 287 | return send() |
| 288 | } |
| 289 | |
| 290 | func windowsPhoneJPEG(frame windowsWindowFrame, width, height int, ratio float64) (string, error) { |
| 291 | img, err := nativeFrameImage(frame) |
| 292 | if err != nil { |
| 293 | return "", windowsWindowFailure{code: "desktop_frame_invalid"} |
| 294 | } |
| 295 | scale := min(1, float64(width)*min(2, ratio)/float64(frame.Width), float64(height)*min(2, ratio)/float64(frame.Height)) |
| 296 | if scale < 1 { |
| 297 | resized := image.NewRGBA(image.Rect(0, 0, max(1, int(float64(frame.Width)*scale)), max(1, int(float64(frame.Height)*scale)))) |
| 298 | draw.CatmullRom.Scale(resized, resized.Bounds(), img, img.Bounds(), draw.Src, nil) |
| 299 | img = resized |
| 300 | } |
| 301 | for _, quality := range []int{90, 78, 62, 45} { |
| 302 | var output bytes.Buffer |
| 303 | if err := jpeg.Encode(&output, img, &jpeg.Options{Quality: quality}); err != nil { |
| 304 | return "", windowsWindowFailure{code: "desktop_frame_invalid"} |
| 305 | } |
| 306 | if output.Len() <= 2<<20 { |
| 307 | return base64.StdEncoding.EncodeToString(output.Bytes()), nil |
| 308 | } |
| 309 | } |
| 310 | return "", windowsWindowFailure{code: "desktop_frame_limit"} |
| 311 | } |