diff --git a/Cargo.lock b/Cargo.lock index 7489104..92e855b 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2,6 +2,16 @@ # It is not intended for manual editing. version = 3 +[[package]] +name = "Inflector" +version = "0.11.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fe438c63458706e03479442743baae6c88256498e6431708f6dfc520a26515d3" +dependencies = [ + "lazy_static", + "regex", +] + [[package]] name = "addr2line" version = "0.12.1" @@ -279,9 +289,9 @@ dependencies = [ [[package]] name = "deno_bindgen" version = "0.1.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0c0a79e27b0ad3f091a6a9d6ba22676f0f4f5c211933f6cb324fbc22b634cba9" +source = "git+https://github.com/littledivy/deno_bindgen?rev=3ca55e7#3ca55e748af0161e1c50519132b9ee30c0b3d645" dependencies = [ + "Inflector", "proc-macro2 1.0.27", "quote 1.0.6", "serde", diff --git a/Cargo.toml b/Cargo.toml index 5527dd9..5b238ed 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -10,7 +10,7 @@ path = "./src/lib.rs" crate-type = ["cdylib"] [dependencies] -deno_bindgen = "0.1.1" +deno_bindgen = { git = "https://github.com/littledivy/deno_bindgen", rev = "3ca55e7" } autopilot = { path = "./autopilot-rs/" } serde = { version = "1", features = ["derive"] } serde_json = "1" diff --git a/bindings/bindings.ts b/bindings/bindings.ts index 86046a4..a5963b7 100644 --- a/bindings/bindings.ts +++ b/bindings/bindings.ts @@ -1,40 +1,153 @@ // Auto-generated with deno_bindgen import { Plug } from "https://deno.land/x/plug@0.4.0/mod.ts"; -const encode = (s: string) => new TextEncoder().encode(s); +function encode(v: string | Uint8Array): Uint8Array { + if (typeof v !== "string") return v; + return new TextEncoder().encode(v); +} const opts = { name: "autopilot_deno", - url: "https://github.com/littledivy/autopilot-deno/releases/download/0.4.0", + url: "target/debug", }; const _lib = await Plug.prepare(opts, { - mouse_pixel_color_g: { parameters: [], result: "u8" }, - smooth_mouse_move: { parameters: ["buffer", "usize"], result: "void" }, - mouse_pixel_color_b: { parameters: [], result: "u8" }, - toggle_key: { parameters: ["buffer", "usize"], result: "void" }, - screenscale: { parameters: [], result: "f64" }, - mouse_pixel_color_r: { parameters: [], result: "u8" }, - mouse_move: { parameters: ["buffer", "usize"], result: "void" }, - mouse_scroll: { parameters: [], result: "void" }, - notify: { parameters: ["buffer", "usize"], result: "void" }, - type_: { parameters: ["buffer", "usize"], result: "void" }, - screensize_width: { parameters: [], result: "f64" }, - screenshot: { parameters: ["buffer", "usize"], result: "void" }, - alert: { parameters: ["buffer", "usize"], result: "void" }, - mouse_click: { parameters: ["buffer", "usize"], result: "void" }, - screensize_height: { parameters: [], result: "f64" }, - mouse_pos_x: { parameters: [], result: "f64" }, - tap: { parameters: ["buffer", "usize"], result: "void" }, - mouse_pixel_color_a: { parameters: [], result: "u8" }, - mouse_pos_y: { parameters: [], result: "f64" }, + mouse_pos_x: { parameters: [], result: "f64", nonblocking: false }, + notify: { + parameters: ["buffer", "usize"], + result: "void", + nonblocking: false, + }, + mouse_pixel_color_g: { parameters: [], result: "u8", nonblocking: false }, + smooth_mouse_move: { + parameters: ["buffer", "usize"], + result: "void", + nonblocking: false, + }, + mouse_move: { + parameters: ["buffer", "usize"], + result: "void", + nonblocking: false, + }, + screensize_height: { parameters: [], result: "f64", nonblocking: false }, + mouse_scroll: { parameters: [], result: "void", nonblocking: false }, + mouse_pixel_color_r: { parameters: [], result: "u8", nonblocking: false }, + mouse_pixel_color_a: { parameters: [], result: "u8", nonblocking: false }, + screensize_width: { parameters: [], result: "f64", nonblocking: false }, + mouse_pixel_color_b: { parameters: [], result: "u8", nonblocking: false }, + tap: { parameters: ["buffer", "usize"], result: "void", nonblocking: false }, + mouse_pos_y: { parameters: [], result: "f64", nonblocking: false }, + alert: { + parameters: ["buffer", "usize"], + result: "void", + nonblocking: false, + }, + type_: { + parameters: ["buffer", "usize"], + result: "void", + nonblocking: false, + }, + mouse_click: { + parameters: ["buffer", "usize"], + result: "void", + nonblocking: false, + }, + screenscale: { parameters: [], result: "f64", nonblocking: false }, + toggle_key: { + parameters: ["buffer", "usize"], + result: "void", + nonblocking: false, + }, + screenshot: { + parameters: ["buffer", "usize"], + result: "void", + nonblocking: false, + }, }); -type MouseMoveParams = { d: any; y: number; x: number }; -type KeyToggleParams = { key: any; down: any }; -type NotifyParams = { title: any; body: any }; -type Point = { x: number; y: number }; -type StrArg = { text: any }; -type AlertParams = { title: any; msg: any }; -type MouseClickParamsWrapper = { params: any }; -type ScreenSize = { width: number; height: number }; -type Pixel = { a: number; r: number; g: number; b: number }; +export type ScreenSize = { + height: number; + width: number; +}; +export type Point = { + x: number; + y: number; +}; +export type NotifyParams = { + title: string; + body: string; +}; +export type AlertParams = { + msg: string; + title: string; +}; +export type MouseMoveParams = { + x: number; + y: number; + d: number | undefined | null; +}; +export type MouseClickParams = + | "left" + | "middle" + | "right"; +export type KeyToggleParams = { + key: KeyCode; + down: boolean; +}; +export type Pixel = { + r: number; + g: number; + b: number; + a: number; +}; +export type KeyCode = + | "f1" + | "f2" + | "f3" + | "f4" + | "f5" + | "f6" + | "f7" + | "f8" + | "f9" + | "f10" + | "f11" + | "f12" + | "f13" + | "f14" + | "f15" + | "f16" + | "f17" + | "f18" + | "f19" + | "f20" + | "f21" + | "f22" + | "f23" + | "f24" + | "leftarrow" + | "uparrow" + | "rightarrow" + | "downarrow" + | "insert" + | "delete" + | "home" + | "end" + | "pageup" + | "pagedown" + | "tab" + | "backspace" + | "enter" + | "escape" + | "space" + | "meta" + | "alt" + | "control" + | "shift" + | "capslock"; +export function mouse_pos_x() { + return _lib.symbols.mouse_pos_x() as number; +} +export function notify(a0: NotifyParams) { + const a0_buf = encode(JSON.stringify(a0)); + return _lib.symbols.notify(a0_buf, a0_buf.byteLength) as null; +} export function mouse_pixel_color_g() { return _lib.symbols.mouse_pixel_color_g() as number; } @@ -42,62 +155,55 @@ export function smooth_mouse_move(a0: MouseMoveParams) { const a0_buf = encode(JSON.stringify(a0)); return _lib.symbols.smooth_mouse_move(a0_buf, a0_buf.byteLength) as null; } -export function mouse_pixel_color_b() { - return _lib.symbols.mouse_pixel_color_b() as number; -} -export function toggle_key(a0: KeyToggleParams) { - const a0_buf = encode(JSON.stringify(a0)); - return _lib.symbols.toggle_key(a0_buf, a0_buf.byteLength) as null; -} -export function screenscale() { - return _lib.symbols.screenscale() as number; -} -export function mouse_pixel_color_r() { - return _lib.symbols.mouse_pixel_color_r() as number; -} export function mouse_move(a0: MouseMoveParams) { const a0_buf = encode(JSON.stringify(a0)); return _lib.symbols.mouse_move(a0_buf, a0_buf.byteLength) as null; } +export function screensize_height() { + return _lib.symbols.screensize_height() as number; +} export function mouse_scroll() { return _lib.symbols.mouse_scroll() as null; } -export function notify(a0: NotifyParams) { - const a0_buf = encode(JSON.stringify(a0)); - return _lib.symbols.notify(a0_buf, a0_buf.byteLength) as null; +export function mouse_pixel_color_r() { + return _lib.symbols.mouse_pixel_color_r() as number; } -export function type_(a0: StrArg) { - const a0_buf = encode(JSON.stringify(a0)); - return _lib.symbols.type_(a0_buf, a0_buf.byteLength) as null; +export function mouse_pixel_color_a() { + return _lib.symbols.mouse_pixel_color_a() as number; } export function screensize_width() { return _lib.symbols.screensize_width() as number; } -export function screenshot(a0: StrArg) { +export function mouse_pixel_color_b() { + return _lib.symbols.mouse_pixel_color_b() as number; +} +export function tap(a0: KeyToggleParams) { const a0_buf = encode(JSON.stringify(a0)); - return _lib.symbols.screenshot(a0_buf, a0_buf.byteLength) as null; + return _lib.symbols.tap(a0_buf, a0_buf.byteLength) as null; +} +export function mouse_pos_y() { + return _lib.symbols.mouse_pos_y() as number; } export function alert(a0: AlertParams) { const a0_buf = encode(JSON.stringify(a0)); return _lib.symbols.alert(a0_buf, a0_buf.byteLength) as null; } -export function mouse_click(a0: MouseClickParamsWrapper) { +export function type_(a0: string) { + const a0_buf = encode(a0); + return _lib.symbols.type_(a0_buf, a0_buf.byteLength) as null; +} +export function mouse_click(a0: MouseClickParams) { const a0_buf = encode(JSON.stringify(a0)); return _lib.symbols.mouse_click(a0_buf, a0_buf.byteLength) as null; } -export function screensize_height() { - return _lib.symbols.screensize_height() as number; -} -export function mouse_pos_x() { - return _lib.symbols.mouse_pos_x() as number; +export function screenscale() { + return _lib.symbols.screenscale() as number; } -export function tap(a0: KeyToggleParams) { +export function toggle_key(a0: KeyToggleParams) { const a0_buf = encode(JSON.stringify(a0)); - return _lib.symbols.tap(a0_buf, a0_buf.byteLength) as null; -} -export function mouse_pixel_color_a() { - return _lib.symbols.mouse_pixel_color_a() as number; + return _lib.symbols.toggle_key(a0_buf, a0_buf.byteLength) as null; } -export function mouse_pos_y() { - return _lib.symbols.mouse_pos_y() as number; +export function screenshot(a0: string) { + const a0_buf = encode(a0); + return _lib.symbols.screenshot(a0_buf, a0_buf.byteLength) as null; } diff --git a/src/autopilot.ts b/src/autopilot.ts index 4e243b9..4a1f0ac 100644 --- a/src/autopilot.ts +++ b/src/autopilot.ts @@ -1,4 +1,26 @@ -import * as inner from "../bindings/bindings.ts"; +import { + alert, + AlertParams, + KeyCode, + mouse_click, + mouse_pixel_color_a, + mouse_pixel_color_b, + mouse_pixel_color_g, + mouse_pixel_color_r, + mouse_pos_x, + mouse_pos_y, + mouse_scroll, + MouseClickParams, + notify, + screenscale, + screenshot, + screensize_height, + screensize_width, + smooth_mouse_move, + tap, + toggle_key, + type_, +} from "../bindings/bindings.ts"; export function isAscii(str: string): boolean { return /^[\x00-\x7F]*$/.test(str); @@ -14,15 +36,15 @@ class AutoPilot { */ type(text: string) { !isAscii(text) && throwAsciiError(); - inner.type_({ text }); + type_(text); return this; } /** * @param opt alert options or msg to display alert. */ - alert(opt: AlertOptions) { - inner.alert(opt); + alert(opt: AlertParams) { + alert(opt); return this; } @@ -31,7 +53,7 @@ class AutoPilot { * @param body The body of the notification */ notify(title: string, body: string) { - inner.notify({ + notify({ title, body, }); @@ -43,8 +65,8 @@ class AutoPilot { */ screenSize(): Dimensions { return { - width: inner.screensize_width(), - height: inner.screensize_height(), + width: screensize_width(), + height: screensize_height(), }; } @@ -58,7 +80,7 @@ class AutoPilot { if (isNaN(x) || isNaN(y)) { throw new TypeError("height or width is NaN"); } - inner.smooth_mouse_move({ x, y, d }); + smooth_mouse_move({ x, y, d }); return this; } @@ -66,32 +88,32 @@ class AutoPilot { * @param {string} file The output file name */ screenshot(file: string) { - inner.screenshot({ text: file }); + screenshot(file); return this; } /** * @param {string} arg The key name */ - tap(arg: ToggleKeys) { + tap(arg: KeyCode) { (arg as string) = arg.trim().toLowerCase(); - inner.tap({ key: arg, down: false }); + tap({ key: arg, down: false }); return this; } /** * @param {ClickOptions} arg The mouse section to click */ - click(arg: string) { - inner.mouse_click({ params: arg }); + click(param: MouseClickParams) { + mouse_click(param); return this; } /** * @param {ScrollOptions} arg The direction of scroll */ - scroll(arg: ScrollOptions) { - inner.mouse_scroll(); + scroll(arg: string) { + mouse_scroll(); return this; } @@ -99,7 +121,7 @@ class AutoPilot { * @return {object} The coordinates of mouse on screen */ mousePosition(): Point { - return { x: inner.mouse_pos_x(), y: inner.mouse_pos_y() }; + return { x: mouse_pos_x(), y: mouse_pos_y() }; } /** @@ -107,10 +129,10 @@ class AutoPilot { */ pixelColor(): Pixel { return { - r: inner.mouse_pixel_color_r(), - g: inner.mouse_pixel_color_g(), - b: inner.mouse_pixel_color_b(), - a: inner.mouse_pixel_color_a(), + r: mouse_pixel_color_r(), + g: mouse_pixel_color_g(), + b: mouse_pixel_color_b(), + a: mouse_pixel_color_a(), }; } @@ -118,8 +140,8 @@ class AutoPilot { * @param {string} key The key to be toggled * @param {boolean} down Whether to press the key or unpress it */ - toggleKey(key: ToggleKeys, down: boolean) { - inner.toggle_key({ + toggleKey(key: KeyCode, down: boolean) { + toggle_key({ key, down, }); @@ -130,68 +152,10 @@ class AutoPilot { * @return {number} The number of pixels in a point */ screenScale(): number { - return inner.screenscale(); + return screenscale(); } } -export interface AlertOptions { - title: string; - msg: string; -} -export interface NotificationParams { - title: string; - body: string; -} - -export type ClickOptions = "left" | "right" | "middle"; - -export type ScrollOptions = "up" | "down"; - -export type ToggleKeys = - | "f1" - | "f2" - | "f3" - | "f4" - | "f5" - | "f6" - | "f7" - | "f8" - | "f9" - | "f10" - | "f11" - | "f12" - | "f13" - | "f14" - | "f15" - | "f16" - | "f17" - | "f18" - | "f19" - | "f20" - | "f21" - | "f22" - | "f23" - | "f24" - | "leftarrow" - | "control" - | "rightarrow" - | "downarrow" - | "end" - | "uparrow" - | "pageup" - | "alt" - | "return" - | "pagedown" - | "delete" - | "home" - | "escape" - | "backspace" - | "meta" - | "capslock" - | "shift" - | "tab" - | "space"; - interface Point { x: number; y: number; diff --git a/src/lib.rs b/src/lib.rs index 22787fb..d67e4cd 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -3,13 +3,8 @@ use serde::Deserialize; use std::path::Path; #[deno_bindgen] -struct StrArg { - text: String, -} - -#[deno_bindgen] -fn type_(arg: StrArg) { - autopilot::key::type_string(&arg.text, &[], 200., 0.); +fn type_(arg: &str) { + autopilot::key::type_string(&arg, &[], 200., 0.); } #[deno_bindgen] @@ -18,7 +13,7 @@ pub struct KeyToggleParams { pub down: bool, } -#[derive(Deserialize)] +#[deno_bindgen] #[serde(rename_all = "lowercase")] pub enum KeyCode { F1, @@ -201,8 +196,8 @@ fn mouse_move(arg: MouseMoveParams) { // Ok(()) // } -#[derive(Deserialize)] -#[serde(rename_all = "camelCase")] +#[deno_bindgen] +#[serde(rename_all = "lowercase")] pub enum MouseClickParams { Left, Middle, @@ -219,16 +214,9 @@ impl From for autopilot::mouse::Button { } } -// TODO: Wrapper for MouseClickParams enum -// remove when deno_bindgen supports enums. -#[deno_bindgen] -pub struct MouseClickParamsWrapper { - params: MouseClickParams, -} - #[deno_bindgen] -fn mouse_click(arg: MouseClickParamsWrapper) { - autopilot::mouse::click(autopilot::mouse::Button::from(arg.params), Some(10)); +fn mouse_click(arg: MouseClickParams) { + autopilot::mouse::click(autopilot::mouse::Button::from(arg), Some(10)); } // fn op_mouse_click( @@ -303,14 +291,14 @@ fn alert(arg: AlertParams) { // } #[deno_bindgen] -fn screenshot(arg: StrArg) { +fn screenshot(arg: &str) { let bitmap = autopilot::bitmap::capture_screen().unwrap(); let path = Path::new(file!()) .parent() .unwrap() .parent() .unwrap() - .join(&arg.text); + .join(&arg); bitmap.image.save(&path).unwrap(); }