1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
use std::time::SystemTime;

use mlua::Lua;
use rdev::{simulate, Button, Event, EventType};

use crate::create_body;

use super::event_handler::{parse_key, EventEvent};

#[doc(hidden)]
pub fn init(lua: &Lua) -> mlua::Result<mlua::Table> {
    create_body! (lua,
        "create_mouse_move" => lua.create_function(create_mouse_move)?,
        "create_wheel" => lua.create_function(create_wheel)?,
        "create_key_press" => lua.create_function(create_key_press)?,
        "create_key_release" => lua.create_function(create_key_release)?,
        "create_button_press" => lua.create_function(create_button_press)?,
        "create_button_release" => lua.create_function(create_button_release)?,
        "simulate" => lua.create_function(simulate_event)?
    )
}

fn ev(event_type: EventType) -> mlua::Result<EventEvent> {
    Ok(EventEvent(Event {
        time: SystemTime::now(),
        name: None,
        event_type,
    }))
}

pub fn create_mouse_move(_: &Lua, (x, y): (f64, f64)) -> mlua::Result<EventEvent> {
    ev(EventType::MouseMove { x, y })
}

pub fn create_wheel(_: &Lua, (x, y): (i64, i64)) -> mlua::Result<EventEvent> {
    ev(EventType::Wheel { delta_x: x, delta_y: y })
}

pub fn create_key_press(_: &Lua, key: String) -> mlua::Result<EventEvent> {
    ev(EventType::KeyPress(parse_key(key)?))
}

pub fn create_key_release(_: &Lua, key: String) -> mlua::Result<EventEvent> {
    ev(EventType::KeyRelease(parse_key(key)?))
}

pub fn create_button_press(_: &Lua, button: String) -> mlua::Result<EventEvent> {
    ev(EventType::ButtonPress(match button.as_str() {
        "left" => Button::Left,
        "right" => Button::Right,
        "middle" => Button::Middle,
        _ => Button::Unknown(button.parse().unwrap()),
    }))
}

pub fn create_button_release(_: &Lua, button: String) -> mlua::Result<EventEvent> {
    ev(EventType::ButtonRelease(match button.as_str() {
        "left" => Button::Left,
        "right" => Button::Right,
        "middle" => Button::Middle,
        _ => Button::Unknown(button.parse().unwrap()),
    }))
}

pub fn simulate_event(_: &Lua, event: EventEvent) -> mlua::Result<()> {
    simulate(&event.0.event_type).map_err(|e| mlua::Error::RuntimeError(e.to_string()))
}