77 * mouse, keyboard, touch, clipboard, and gesture interactions.
88 */
99import os from "node:os"
10- import path from "node:path"
1110import { applyMotion } from "./drivers/utils"
12- import { WindowsInputInjector } from "./drivers/windows/index"
13- export interface InputMessage {
14- type :
15- | "move"
16- | "paste"
17- | "copy"
18- | "click"
19- | "scroll"
20- | "key"
21- | "text"
22- | "zoom"
23- | "combo"
24- | "touch"
25- | "update-settings"
26- dx ?: number
27- dy ?: number
28- config ?: Partial < InputConfig >
29- button ?: "left" | "right" | "middle"
30- press ?: boolean
31- key ?: string
32- keys ?: string [ ]
33- text ?: string
34- delta ?: number
35- contacts ?: Array < {
36- id : number
37- x : number
38- y : number
39- state : "down" | "move" | "up"
40- } >
41- }
42-
43- export interface InputConfig {
44- sensitivity : number
45- invertScroll : boolean
46- acceleration : boolean
47- }
48-
49- type PlatformInjector = {
50- updateConfig ( config : Partial < InputConfig > ) : void
51- injectMouseMove ( dx : number , dy : number ) : void
52- injectMouseButton ( button : "left" | "right" | "middle" , isDown : boolean ) : void
53- injectMouseWheel ( dx : number , dy : number ) : void
54- injectKey ( key : string ) : void
55- injectCombo ( keys : string [ ] ) : void
56- injectText ( text : string ) : void
57- injectTouch ( contacts : NonNullable < InputMessage [ "contacts" ] > ) : void
58- destroy ( ) : void
59- }
11+ import {
12+ DEFAULT_SCREEN_HEIGHT ,
13+ DEFAULT_SCREEN_WIDTH ,
14+ MAX_TEXT_LENGTH ,
15+ MAX_COMBO_KEYS ,
16+ MAX_COORD ,
17+ MAX_KEY_LENGTH ,
18+ } from "./constants.ts"
19+ import type { InputConfig , InputMessage , PlatformInjector } from "./types.ts"
6020
6121const VALID_BUTTONS = [ "left" , "right" , "middle" ] as const
6222type MouseButton = ( typeof VALID_BUTTONS ) [ number ]
6323
64- const MAX_COORD = 2000
65- const MAX_TEXT_LENGTH = 500
66- const MAX_COMBO_KEYS = 10
67- const MAX_KEY_LENGTH = 50
68-
6924export class InputHandler {
7025 private injector : PlatformInjector
7126 private platform : "win32" | "linux" | "darwin" | "other"
72-
7327 private lastMoveTime = 0
7428 private lastScrollTime = 0
7529 private pendingMove : InputMessage | null = null
@@ -82,6 +36,8 @@ export class InputHandler {
8236 sensitivity : 1.0 ,
8337 invertScroll : false ,
8438 acceleration : true ,
39+ screenWidth : DEFAULT_SCREEN_WIDTH ,
40+ screenHeight : DEFAULT_SCREEN_HEIGHT ,
8541 }
8642
8743 constructor ( config : Partial < InputConfig > = { } , throttleMs = 8 ) {
@@ -92,22 +48,15 @@ export class InputHandler {
9248
9349 if ( plat === "win32" ) {
9450 this . platform = "win32"
51+ const { WindowsInputInjector } = require ( "./drivers/windows" )
9552 this . injector = new WindowsInputInjector ( this . config ) as PlatformInjector
9653 } else if ( plat === "linux" ) {
9754 this . platform = "linux"
98- const linuxPath = path . join (
99- process . cwd ( ) ,
100- "src/server/drivers/linux/index.ts" ,
101- )
102- const { LinuxInputInjector } = require ( linuxPath )
55+ const { LinuxInputInjector } = require ( "./drivers/linux" )
10356 this . injector = new LinuxInputInjector ( this . config ) as PlatformInjector
10457 } else if ( plat === "darwin" ) {
10558 this . platform = "darwin"
106- const macPath = path . join (
107- process . cwd ( ) ,
108- "src/server/drivers/mac/index.ts" ,
109- )
110- const { MacInputInjector } = require ( macPath )
59+ const { MacInputInjector } = require ( "./drivers/mac" )
11160 this . injector = new MacInputInjector ( this . config ) as PlatformInjector
11261 } else {
11362 this . platform = "other"
@@ -293,7 +242,12 @@ export class InputHandler {
293242 }
294243
295244 case "text" : {
296- if ( ! msg . text || typeof msg . text !== "string" ) break
245+ if (
246+ ! msg . text ||
247+ typeof msg . text !== "string" ||
248+ msg . text . length > MAX_TEXT_LENGTH
249+ )
250+ break
297251 this . injector . injectText ( msg . text )
298252 break
299253 }
0 commit comments