From 2cb4bc8a39d27c82f7db4d7d270f2101b18b5e23 Mon Sep 17 00:00:00 2001 From: tcmal Date: Wed, 5 Jun 2024 21:19:28 +0100 Subject: actually run keybinds --- src/keys.rs | 27 ++++++++++++++++++++++----- 1 file changed, 22 insertions(+), 5 deletions(-) (limited to 'src') diff --git a/src/keys.rs b/src/keys.rs index 7e94944..79f1984 100644 --- a/src/keys.rs +++ b/src/keys.rs @@ -1,3 +1,5 @@ +use std::process::Command; + use crate::{error::*, WM}; use xcb::{ x::{ @@ -16,6 +18,7 @@ impl WM<'_> { }; KEYBINDS.dispatch( + self, sym, ModMask::from_bits_truncate(e.state().bits()) .difference(self.keyboard_state.numlock_mask() | ModMask::LOCK), @@ -167,11 +170,19 @@ impl KeyboardInfo { } /// A bound key -#[derive(Debug)] pub struct Keybind { modifiers: ModMask, key: Keysym, - // TODO: add action + action: &'static dyn Fn(&mut WM<'_>) -> (), +} + +impl std::fmt::Debug for Keybind { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + f.debug_struct("Keybind") + .field("modifiers", &self.modifiers) + .field("key", &self.key) + .finish() + } } /// A set of keybinds. Currently, there is only one instance of this defined statically: [`self::KEYBINDS`]. @@ -180,7 +191,13 @@ pub struct Keybinds(&'static [Keybind]); /// The keybinds to use. const KEYBINDS: Keybinds = Keybinds(&[Keybind { modifiers: ModMask::CONTROL, - key: Keysym::a, + key: Keysym::t, + action: &|_| { + // TODO: disown this process, probably using another way to spawn commands + if let Err(e) = Command::new("xterm").spawn() { + dbg!(e); + } + }, }]); impl Keybinds { @@ -190,12 +207,12 @@ impl Keybinds { } /// Attempt to run the action for the matching keybind, if it exists. - pub fn dispatch(&self, key: Keysym, modifiers: ModMask) { + pub fn dispatch(&self, wm: &mut WM<'_>, key: Keysym, modifiers: ModMask) { if let Some(bind) = self .binds() .find(|b| b.key == key && modifiers.contains(b.modifiers)) { - dbg!(bind); // TODO: run the bind + (bind.action)(wm); } } } -- cgit v1.2.3