Skip to content

Commit

Permalink
feat(Lua): add event scripting via Lua
Browse files Browse the repository at this point in the history
  • Loading branch information
ShadowApex committed Nov 26, 2024
1 parent 3eff7f4 commit c7b394d
Show file tree
Hide file tree
Showing 6 changed files with 292 additions and 1 deletion.
93 changes: 92 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ log = { version = "0.4.22", features = [
"release_max_level_debug",
] }
mio = { version = "0.8.11", features = ["os-poll", "os-ext", "net"] }
mlua = { version = "0.10.1", features = ["lua54", "vendored", "async", "send"] }
nix = { version = "0.29.0", features = ["fs"] }
packed_struct = "0.10.1"
procfs = "0.16.0"
Expand Down
20 changes: 20 additions & 0 deletions rootfs/usr/share/inputplumber/scripts/test.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
--print("---")

-- process_event is called on every input event
local process_event = function(event)
--print("Called process event!")
--print(event.capability)
--print(event.value)

if event.capability == "Gamepad:Button:Guide" then
print("Got guide button: ", event.value)
end

--for _, path in ipairs(source_device_paths) do
-- print(path)
--end
end

return {
process_event = process_event,
}
20 changes: 20 additions & 0 deletions src/input/capability.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,26 @@ pub enum Capability {
Touchscreen(Touch),
}

impl Capability {
pub fn to_capability_string(&self) -> String {
match self {
Capability::Gamepad(gamepad) => match gamepad {
Gamepad::Button(button) => format!("Gamepad:Button:{}", button),
Gamepad::Axis(axis) => format!("Gamepad:Axis:{}", axis),
Gamepad::Trigger(trigger) => format!("Gamepad:Trigger:{}", trigger),
Gamepad::Accelerometer => "Gamepad:Accelerometer".to_string(),
Gamepad::Gyro => "Gamepad:Gyro".to_string(),
},
Capability::Mouse(mouse) => match mouse {
Mouse::Motion => "Mouse:Motion".to_string(),
Mouse::Button(button) => format!("Mouse:Button:{}", button),
},
Capability::Keyboard(key) => format!("Keyboard:{}", key),
_ => self.to_string(),
}
}
}

impl fmt::Display for Capability {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
Expand Down
12 changes: 12 additions & 0 deletions src/input/composite_device/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
pub mod client;
pub mod command;
pub mod script;

use script::CompositeDeviceLua;
use std::{
borrow::Borrow,
collections::{BTreeSet, HashMap, HashSet},
Expand Down Expand Up @@ -64,6 +66,8 @@ pub enum InterceptMode {
pub struct CompositeDevice {
/// Connection to DBus
conn: Connection,
/// Lua state instance
lua: CompositeDeviceLua,
/// Transmit channel to communicate with the input manager
manager: mpsc::Sender<ManagerCommand>,
/// Configuration for the CompositeDevice
Expand Down Expand Up @@ -164,6 +168,7 @@ impl CompositeDevice {
let name = config.name.clone();
let mut device = Self {
conn,
lua: CompositeDeviceLua::new(tx.clone().into()),
manager,
config,
name,
Expand Down Expand Up @@ -671,6 +676,9 @@ impl CompositeDevice {
return Ok(());
}

// Process the event with lua
self.lua.process_event(&event);

// Check if the event needs to be translated based on the
// capability map. Translated events will be re-enqueued, so this will
// return early.
Expand Down Expand Up @@ -1368,6 +1376,8 @@ impl CompositeDevice {

if let Some(idx) = self.source_device_paths.iter().position(|str| str == &path) {
self.source_device_paths.remove(idx);
self.lua
.set_source_device_paths(self.source_device_paths.clone());
};

if let Some(idx) = self.source_devices_used.iter().position(|str| str == &id) {
Expand Down Expand Up @@ -1475,6 +1485,8 @@ impl CompositeDevice {
let device_path = source_device.get_device_path();
self.source_devices_discovered.push(source_device);
self.source_device_paths.push(device_path);
self.lua
.set_source_device_paths(self.source_device_paths.clone());
self.source_devices_used.push(id);

Ok(())
Expand Down
Loading

0 comments on commit c7b394d

Please sign in to comment.