use crate::dll::{NativeCall, NativeFailure, NativeResult};
use alloc::string::{String, ToString};
use alloc::vec::Vec;
#[derive(Debug, Clone, PartialEq)]
pub enum WindowAnswer {
Active,
NotActive,
Absent,
Unknown(String),
}
pub trait Keyboard {
fn type_text(&mut self, text: &str) -> Result<String, String>;
fn press_key(&mut self, name: &str, modifiers: &[String]) -> Result<String, String>;
fn key_down(&mut self, _name: &str) -> Result<String, String> {
Err("this desktop cannot hold a key down".to_string())
}
fn key_up(&mut self, _name: &str) -> Result<String, String> {
Err("this desktop cannot hold a key down".to_string())
}
fn key_is_down(&mut self, _name: &str) -> Result<bool, String> {
Err("this desktop cannot tell which keys are down".to_string())
}
fn keys_down(&mut self) -> Result<Vec<String>, String> {
Err("this desktop cannot tell which keys are down".to_string())
}
}
pub trait Pointer {
fn click(&mut self, x: i64, y: i64, button: &str, count: u32) -> Result<String, String>;
fn mouse_down(
&mut self,
_x: Option<i64>,
_y: Option<i64>,
_button: &str,
) -> Result<String, String> {
Err("this desktop cannot hold a mouse button down".to_string())
}
fn mouse_up(
&mut self,
_x: Option<i64>,
_y: Option<i64>,
_button: &str,
) -> Result<String, String> {
Err("this desktop cannot hold a mouse button down".to_string())
}
fn move_pointer(&mut self, _x: i64, _y: i64) -> Result<String, String> {
Err("this desktop cannot move the pointer".to_string())
}
fn mouse_position(&mut self) -> Result<(i64, i64), String> {
Err("this desktop cannot tell where the pointer is".to_string())
}
fn set_mouse_speed(&mut self, _speed: i64) -> Result<String, String> {
Err("this desktop cannot change the speed of the pointer".to_string())
}
fn set_mouse_delay(&mut self, _delay: i64) -> Result<String, String> {
Err("this desktop cannot change the delay of the pointer".to_string())
}
}
pub trait Screen {
fn capture(
&mut self,
x: i64,
y: i64,
width: i64,
height: i64,
filename: &str,
) -> Result<String, String>;
fn pixel(&mut self, x: i64, y: i64) -> Result<String, String>;
fn screen_size(&mut self) -> Result<(i64, i64), String> {
Err("this desktop cannot tell how big the screen is".to_string())
}
fn pixel_search(
&mut self,
_x1: i64,
_y1: i64,
_x2: i64,
_y2: i64,
_colour: &str,
) -> Result<Option<(i64, i64)>, String> {
Err("this desktop cannot search the screen for a colour".to_string())
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum TitleMatch {
Starts,
Contains,
Exact,
Pattern,
}
impl TitleMatch {
pub fn matches(self, title: &str, wanted: &str) -> Result<bool, String> {
match self {
TitleMatch::Starts => Ok(title.starts_with(wanted)),
TitleMatch::Contains => Ok(title.contains(wanted)),
TitleMatch::Exact => Ok(title == wanted),
TitleMatch::Pattern => {
let (pattern, ignore_case) = match wanted.strip_prefix("i)") {
Some(rest) => (rest, true),
None => (wanted, false),
};
let pattern = if ignore_case {
alloc::format!("(?i){pattern}")
} else {
pattern.to_string()
};
let regex = crate::regex::Regex::new(&pattern)?;
Ok(regex.find(title)?.is_some())
}
}
}
}
pub trait Windows {
fn window_active(&mut self, _title: &str, _mode: TitleMatch) -> Result<WindowAnswer, String> {
Err("this desktop cannot tell which window is active".to_string())
}
fn window_exists(&mut self, _title: &str, _mode: TitleMatch) -> Result<WindowAnswer, String> {
Err("this desktop cannot list the windows".to_string())
}
fn active_window_title(&mut self) -> Result<Option<String>, String> {
Err("this desktop cannot tell which window is active".to_string())
}
}
pub trait Processes {
fn run(&mut self, command: &str, window: &str) -> Result<(i32, String, String), String>;
fn launch(&mut self, _command: &str, _working_dir: &str) -> Result<u32, String> {
Err("this desktop cannot launch a program".to_string())
}
fn run_and_wait(
&mut self,
_command: &str,
_working_dir: &str,
) -> Result<(u32, i32, String, String), String> {
Err("this desktop cannot launch a program".to_string())
}
fn env_get(&mut self, _name: &str) -> Result<Option<String>, String> {
Err("this desktop cannot read the environment".to_string())
}
fn env_set(&mut self, _name: &str, _value: &str) -> Result<String, String> {
Err("this desktop cannot write the environment".to_string())
}
}
pub trait Files {
fn set_working_directory(&mut self, _directory: &str) -> Result<String, String> {
Err("this desktop cannot change the working directory".to_string())
}
fn list_files(
&mut self,
_folder: &str,
_pattern: &str,
_mode: &str,
) -> Result<Vec<String>, String> {
Err("this desktop cannot list the files of a directory".to_string())
}
fn initial_working_directory(&mut self) -> Result<String, String> {
Err("this desktop cannot say where the run started".to_string())
}
fn working_directory(&mut self) -> Result<String, String> {
Err("this desktop cannot tell where the script runs".to_string())
}
fn read_file(&mut self, _path: &str) -> Result<Vec<String>, String> {
Err("this desktop cannot read a file".to_string())
}
fn read_the_bytes_of_a_file(&mut self, _path: &str) -> Result<Vec<u8>, String> {
Err("this desktop cannot read a file".to_string())
}
fn write_file(&mut self, _path: &str, _text: &str) -> Result<String, String> {
Err("this desktop cannot write a file".to_string())
}
fn append_file(&mut self, _path: &str, _bytes: &[u8], _mark: &[u8]) -> Result<String, String> {
Err("this desktop cannot write a file".to_string())
}
fn attributes_of_the_pattern(
&mut self,
_pattern: &str,
_mode: &str,
) -> Result<Vec<String>, String> {
Err("this desktop cannot look for a file".to_string())
}
fn delete_file(&mut self, _path: &str) -> Result<String, String> {
Err("this desktop cannot delete a file".to_string())
}
fn create_dir(&mut self, _path: &str) -> Result<String, String> {
Err("this desktop cannot make a directory".to_string())
}
fn dir_delete(&mut self, _path: &str, _recurse: bool) -> Result<String, String> {
Err("this desktop cannot delete a directory".to_string())
}
fn open_file(&mut self, _path: &str, _mode: &str) -> Result<i64, String> {
Err("this desktop cannot open a file".to_string())
}
fn read_file_at(&mut self, _handle: i64, _pos: i64, _count: i64) -> Result<Vec<u8>, String> {
Err("this desktop cannot read a file".to_string())
}
fn write_file_at(&mut self, _handle: i64, _pos: i64, _bytes: &[u8]) -> Result<usize, String> {
Err("this desktop cannot write a file".to_string())
}
fn file_size(&mut self, _handle: i64) -> Result<i64, String> {
Err("this desktop cannot tell how big a file is".to_string())
}
fn the_size_of_a_file(&mut self, _path: &str) -> Result<i64, String> {
Err("this desktop cannot tell how big a file is".to_string())
}
fn close_file(&mut self, _handle: i64) -> Result<(), String> {
Err("this desktop cannot close a file".to_string())
}
fn copy_file(&mut self, _from: &str, _to: &str, _overwrite: bool) -> Result<String, String> {
Err("this desktop cannot copy a file".to_string())
}
fn move_file(&mut self, _from: &str, _to: &str, _overwrite: bool) -> Result<String, String> {
Err("this desktop cannot move a file".to_string())
}
fn copy_dir(&mut self, _from: &str, _to: &str, _overwrite: bool) -> Result<String, String> {
Err("this desktop cannot copy a folder".to_string())
}
fn move_dir(&mut self, _from: &str, _to: &str, _overwrite: bool) -> Result<String, String> {
Err("this desktop cannot move a folder".to_string())
}
fn file_attributes(&mut self, _path: &str) -> Result<Vec<String>, String> {
Err("this desktop cannot read the attributes of a file".to_string())
}
fn set_file_attributes(
&mut self,
_path: &str,
_attributes: &[String],
) -> Result<String, String> {
Err("this desktop cannot change the attributes of a file".to_string())
}
fn file_times(&mut self, _path: &str) -> Result<(i64, i64, i64), String> {
Err("this desktop cannot read the times of a file".to_string())
}
fn set_file_times(
&mut self,
_path: &str,
_created: Option<i64>,
_modified: Option<i64>,
_accessed: Option<i64>,
) -> Result<String, String> {
Err("this desktop cannot change the times of a file".to_string())
}
fn ini_read(
&mut self,
_path: &str,
_section: &str,
_key: &str,
_default: &str,
) -> Result<String, String> {
Err("this desktop cannot read an ini file".to_string())
}
fn ini_write(
&mut self,
_path: &str,
_section: &str,
_key: &str,
_value: &str,
) -> Result<String, String> {
Err("this desktop cannot write an ini file".to_string())
}
fn ini_delete(&mut self, _path: &str, _section: &str, _key: &str) -> Result<String, String> {
Err("this desktop cannot write an ini file".to_string())
}
}
pub trait Clipboard {
fn clipboard_read(&mut self) -> Result<String, String> {
Err("this desktop cannot read the clipboard".to_string())
}
fn clipboard_write(&mut self, _text: &str) -> Result<String, String> {
Err("this desktop cannot write the clipboard".to_string())
}
}
pub trait Timers {
fn takes_over_timers(&mut self) -> bool {
false
}
}
pub trait Hotkeys {
fn register_hotkey(&mut self, _name: &str) -> Result<String, String> {
Err("this desktop cannot register a hotkey".to_string())
}
fn unregister_hotkey(&mut self, _name: &str) -> Result<String, String> {
Err("this desktop cannot register a hotkey".to_string())
}
fn hotkeys(&mut self) -> Result<Vec<String>, String> {
Err("this desktop cannot list the hotkeys".to_string())
}
fn fired_hotkey(&mut self) -> Result<Option<String>, String> {
Err("this desktop cannot watch the keyboard".to_string())
}
}
pub trait Native {
fn call_native(&mut self, _call: &mut NativeCall) -> Result<NativeResult, NativeFailure> {
Err(NativeFailure::invalid(
"this desktop cannot make a native call.",
))
}
}
pub trait Pictures {
fn load_picture(
&mut self,
_filename: &str,
_options: &PictureOptions,
) -> Result<Option<u32>, String> {
Err("the picture is read by the desktop, and this desktop does not read the images of the machine.".to_string())
}
fn image_search(
&mut self,
_x1: i64,
_y1: i64,
_x2: i64,
_y2: i64,
_image: &Image,
_options: &ImageOptions,
) -> Result<ImageSearch, String> {
Err("the search of a picture is made by the desktop, and this desktop does not read the images of the machine.".to_string())
}
}
pub trait Network {
fn download(&mut self, _url: &str, _file: &str) -> Result<String, String> {
Err(
"the download is made by the desktop, and this desktop does not reach the network."
.to_string(),
)
}
}
#[derive(Clone, Debug, Default)]
pub struct PictureOptions {
pub width: Option<i64>,
pub height: Option<i64>,
pub icon: Option<i64>,
}
#[derive(Clone, Debug, Default)]
pub struct ImageOptions {
pub variation: i64,
pub width: Option<i64>,
pub height: Option<i64>,
pub transparent: Option<String>,
}
#[derive(Clone, Debug)]
pub enum Image {
File(String),
Loaded(u32),
}
#[derive(Clone, Debug)]
pub enum ImageSearch {
NotFound,
Found(i64, i64),
NotLoadable,
}
pub trait Sound {
fn sound_volume(&mut self, _device: &str) -> Result<f64, String> {
Err("this desktop cannot say the volume of a device".to_string())
}
fn sound_set_volume(&mut self, _device: &str, _volume: f64) -> Result<(), String> {
Err("this desktop cannot set the volume of a device".to_string())
}
fn sound_muted(&mut self, _device: &str) -> Result<bool, String> {
Err("this desktop cannot say whether a device is muted".to_string())
}
fn sound_set_muted(&mut self, _device: &str, _muted: bool) -> Result<(), String> {
Err("this desktop cannot mute a device".to_string())
}
fn sound_play(&mut self, _path: &str) -> Result<String, String> {
Err("this desktop cannot play a sound".to_string())
}
fn sound_beep(&mut self, _frequency: f64, _duration_ms: u64) -> Result<String, String> {
Err("this desktop cannot play a tone".to_string())
}
}
pub trait Machine {
fn user_name(&mut self) -> Result<String, String> {
Err("this desktop cannot say the name of the user".to_string())
}
fn computer_name(&mut self) -> Result<String, String> {
Err("this desktop cannot say the name of the computer".to_string())
}
fn shell(&mut self) -> Result<String, String> {
Err("this desktop cannot say the shell of the session".to_string())
}
fn folders(&mut self) -> Result<Folders, String> {
Err("this desktop cannot say the folders of the machine".to_string())
}
fn os_version(&mut self) -> Result<String, String> {
Err("this desktop cannot say the version of the operating system".to_string())
}
fn language(&mut self) -> Result<String, String> {
Err("this desktop cannot say the language of the session".to_string())
}
fn is_admin(&mut self) -> Result<bool, String> {
Err("this desktop cannot say whether the script is elevated".to_string())
}
fn idle_times(&mut self) -> Result<Idle, String> {
Err("this desktop cannot say how long the session has been idle".to_string())
}
}
pub trait Dialogs {
fn input_box(
&mut self,
_prompt: &str,
_title: &str,
_options: &InputOptions,
_default: &str,
) -> Result<InputAnswer, String> {
Err("this desktop cannot ask the user for a text".to_string())
}
fn file_select(
&mut self,
_options: &FileOptions,
_root: &str,
_title: &str,
_filter: &str,
) -> Result<FileAnswer, String> {
Err("this desktop cannot ask the user for a file".to_string())
}
fn dir_select(
&mut self,
_starting: &str,
_options: i64,
_prompt: &str,
) -> Result<String, String> {
Err("this desktop cannot ask the user for a folder".to_string())
}
}
#[derive(Clone, Debug, Default)]
pub struct InputAnswer {
pub value: String,
pub result: String,
}
#[derive(Clone, Debug)]
pub enum FileAnswer {
One(String),
Many(Vec<String>),
}
#[derive(Clone, Debug, Default)]
pub struct InputOptions {
pub timeout: Option<f64>,
pub password: bool,
pub width: Option<i64>,
pub height: Option<i64>,
pub x: Option<i64>,
pub y: Option<i64>,
}
#[derive(Clone, Debug, Default)]
pub struct FileOptions {
pub flags: i64,
pub directory: bool,
pub multiple: bool,
pub save: bool,
}
#[derive(Clone, Debug, Default)]
pub struct Folders {
pub app_data: Option<String>,
pub app_data_common: Option<String>,
pub desktop: Option<String>,
pub desktop_common: Option<String>,
pub start_menu: Option<String>,
pub start_menu_common: Option<String>,
pub programs: Option<String>,
pub programs_common: Option<String>,
pub startup: Option<String>,
pub startup_common: Option<String>,
pub my_documents: Option<String>,
pub temp: Option<String>,
}
#[derive(Clone, Copy, Debug, Default)]
pub struct Idle {
pub any: u64,
pub physical: u64,
pub keyboard: u64,
pub mouse: u64,
}
pub trait Desktop:
Keyboard
+ Pointer
+ Screen
+ Windows
+ Processes
+ Files
+ Clipboard
+ Timers
+ Hotkeys
+ Native
+ Sound
+ Machine
+ Dialogs
+ Network
+ Pictures
{
}