| 1 | use crate::keyboard::keycodes; |
| 2 | use anyhow::Result; |
| 3 | use x11rb::connection::Connection; |
| 4 | use x11rb::protocol::xproto::*; |
| 5 | |
| 6 | #[derive(Debug)] |
| 7 | pub enum KeyAction { |
| 8 | SpawnTerminal, |
| 9 | CloseWindow, |
| 10 | CycleWindow, |
| 11 | Quit, |
| 12 | None, |
| 13 | } |
| 14 | |
| 15 | pub fn setup_keybinds(connection: &impl Connection, root: Window) -> Result<()> { |
| 16 | connection.grab_key( |
| 17 | false, |
| 18 | root, |
| 19 | ModMask::M1.into(), |
| 20 | keycodes::RETURN, |
| 21 | GrabMode::ASYNC, |
| 22 | GrabMode::ASYNC, |
| 23 | )?; |
| 24 | |
| 25 | connection.grab_key( |
| 26 | false, |
| 27 | root, |
| 28 | (ModMask::M1 | ModMask::SHIFT).into(), |
| 29 | keycodes::Q, |
| 30 | GrabMode::ASYNC, |
| 31 | GrabMode::ASYNC, |
| 32 | )?; |
| 33 | |
| 34 | connection.grab_key( |
| 35 | false, |
| 36 | root, |
| 37 | ModMask::M1.into(), |
| 38 | keycodes::Q, |
| 39 | GrabMode::ASYNC, |
| 40 | GrabMode::ASYNC, |
| 41 | )?; |
| 42 | |
| 43 | connection.grab_key( |
| 44 | false, |
| 45 | root, |
| 46 | ModMask::M1.into(), |
| 47 | keycodes::J, |
| 48 | GrabMode::ASYNC, |
| 49 | GrabMode::ASYNC, |
| 50 | )?; |
| 51 | |
| 52 | Ok(()) |
| 53 | } |
| 54 | |
| 55 | pub fn handle_key_press(event: KeyPressEvent) -> Result<KeyAction> { |
| 56 | println!("KeyPress: detail={}, state={:?}", event.detail, event.state); |
| 57 | let action = match (event.detail, event.state) { |
| 58 | (keycodes::RETURN, state) if state.contains(KeyButMask::MOD1) => KeyAction::SpawnTerminal, |
| 59 | (keycodes::Q, state) if state.contains(KeyButMask::MOD1 | KeyButMask::SHIFT) => { |
| 60 | KeyAction::CloseWindow |
| 61 | } |
| 62 | (keycodes::Q, state) if state.contains(KeyButMask::MOD1) => KeyAction::Quit, |
| 63 | (keycodes::J, state) if state.contains(KeyButMask::MOD1) => KeyAction::CycleWindow, |
| 64 | _ => KeyAction::None, |
| 65 | }; |
| 66 | Ok(action) |
| 67 | } |
| 68 | |
| 69 | // pub struct Key { |
| 70 | // pub(crate) modifiers: &'static [KeyButMask], // List of modifiers |
| 71 | // pub(crate) key: Keycode, |
| 72 | // pub(crate) func: KeyAction, |
| 73 | // pub(crate) arg: Arg, |
| 74 | // } |
| 75 | // |
| 76 | // impl Key { |
| 77 | // pub const fn new( |
| 78 | // modifiers: &'static [KeyButMask], |
| 79 | // key: Keycode, |
| 80 | // func: KeyAction, |
| 81 | // arg: Arg, |
| 82 | // ) -> Self { |
| 83 | // Self { |
| 84 | // modifiers, |
| 85 | // key, |
| 86 | // func, |
| 87 | // arg, |
| 88 | // } |
| 89 | // } |
| 90 | // } |
| 91 | // |
| 92 | // const KEYBINDINGS: &[Key] = &[ |
| 93 | // Key::new( |
| 94 | // &[KeyButMask::MOD1], |
| 95 | // keycodes::RETURN, |
| 96 | // KeyAction::Spawn, |
| 97 | // Arg::Str("xclock"), |
| 98 | // ), |
| 99 | // Key::new( |
| 100 | // &[KeyButMask::MOD1, KeyButMask::SHIFT], |
| 101 | // keycodes::Q, |
| 102 | // KeyAction::KillClient, |
| 103 | // Arg::None, |
| 104 | // ), |
| 105 | // Key::new(&[KeyButMask::MOD1], keycodes::Q, KeyAction::Quit, Arg::None), |
| 106 | // Key::new( |
| 107 | // &[KeyButMask::MOD1], |
| 108 | // keycodes::J, |
| 109 | // KeyAction::FocusStack, |
| 110 | // Arg::Int(1), |
| 111 | // ), |
| 112 | // Key::new( |
| 113 | // &[KeyButMask::MOD1], |
| 114 | // keycodes::K, |
| 115 | // KeyAction::FocusStack, |
| 116 | // Arg::Int(-1), |
| 117 | // ), |
| 118 | // ]; |
| 119 | // |