| 1 | use crate::keyboard::keycodes; |
| 2 | use anyhow::Result; |
| 3 | use x11rb::connection::Connection; |
| 4 | use x11rb::protocol::xproto::*; |
| 5 | |
| 6 | #[derive(Debug, Copy, Clone)] |
| 7 | pub enum KeyAction { |
| 8 | Spawn, |
| 9 | KillClient, |
| 10 | FocusStack, |
| 11 | Quit, |
| 12 | ViewTag, |
| 13 | MoveToTag, |
| 14 | None, |
| 15 | } |
| 16 | |
| 17 | #[derive(Debug)] |
| 18 | pub enum Arg { |
| 19 | Str(&'static str), |
| 20 | Int(i32), |
| 21 | None, |
| 22 | } |
| 23 | |
| 24 | pub struct Key { |
| 25 | pub(crate) modifiers: &'static [KeyButMask], |
| 26 | pub(crate) key: Keycode, |
| 27 | pub(crate) func: KeyAction, |
| 28 | pub(crate) arg: Arg, |
| 29 | } |
| 30 | |
| 31 | impl Key { |
| 32 | pub const fn new( |
| 33 | modifiers: &'static [KeyButMask], |
| 34 | key: Keycode, |
| 35 | func: KeyAction, |
| 36 | arg: Arg, |
| 37 | ) -> Self { |
| 38 | Self { |
| 39 | modifiers, |
| 40 | key, |
| 41 | func, |
| 42 | arg, |
| 43 | } |
| 44 | } |
| 45 | } |
| 46 | |
| 47 | const KEYBINDINGS: &[Key] = &[ |
| 48 | Key::new( |
| 49 | &[KeyButMask::MOD1], |
| 50 | keycodes::RETURN, |
| 51 | KeyAction::Spawn, |
| 52 | Arg::Str("alacritty"), |
| 53 | ), |
| 54 | Key::new( |
| 55 | &[KeyButMask::MOD1], |
| 56 | keycodes::Q, |
| 57 | KeyAction::KillClient, |
| 58 | Arg::None, |
| 59 | ), |
| 60 | Key::new( |
| 61 | &[KeyButMask::MOD1, KeyButMask::SHIFT], |
| 62 | keycodes::Q, |
| 63 | KeyAction::Quit, |
| 64 | Arg::None, |
| 65 | ), |
| 66 | Key::new( |
| 67 | &[KeyButMask::MOD1], |
| 68 | keycodes::K, |
| 69 | KeyAction::FocusStack, |
| 70 | Arg::Int(1), |
| 71 | ), |
| 72 | Key::new( |
| 73 | &[KeyButMask::MOD1], |
| 74 | keycodes::J, |
| 75 | KeyAction::FocusStack, |
| 76 | Arg::Int(-1), |
| 77 | ), |
| 78 | // NEW: Tag bindings (Mod+1 through Mod+9) |
| 79 | Key::new( |
| 80 | &[KeyButMask::MOD1], |
| 81 | keycodes::KEY_1, |
| 82 | KeyAction::ViewTag, |
| 83 | Arg::Int(0), |
| 84 | ), |
| 85 | Key::new( |
| 86 | &[KeyButMask::MOD1], |
| 87 | keycodes::KEY_2, |
| 88 | KeyAction::ViewTag, |
| 89 | Arg::Int(1), |
| 90 | ), |
| 91 | Key::new( |
| 92 | &[KeyButMask::MOD1], |
| 93 | keycodes::KEY_3, |
| 94 | KeyAction::ViewTag, |
| 95 | Arg::Int(2), |
| 96 | ), |
| 97 | // ... add KEY_4 through KEY_9 |
| 98 | |
| 99 | // NEW: Move window to tag (Mod+Shift+1 through Mod+Shift+9) |
| 100 | Key::new( |
| 101 | &[KeyButMask::MOD1, KeyButMask::SHIFT], |
| 102 | keycodes::KEY_1, |
| 103 | KeyAction::MoveToTag, |
| 104 | Arg::Int(0), |
| 105 | ), |
| 106 | Key::new( |
| 107 | &[KeyButMask::MOD1, KeyButMask::SHIFT], |
| 108 | keycodes::KEY_2, |
| 109 | KeyAction::MoveToTag, |
| 110 | Arg::Int(1), |
| 111 | ), |
| 112 | Key::new( |
| 113 | &[KeyButMask::MOD1, KeyButMask::SHIFT], |
| 114 | keycodes::KEY_3, |
| 115 | KeyAction::MoveToTag, |
| 116 | Arg::Int(2), |
| 117 | ), |
| 118 | // ... etc |
| 119 | ]; |
| 120 | |
| 121 | fn modifiers_to_mask(modifiers: &[KeyButMask]) -> u16 { |
| 122 | modifiers |
| 123 | .iter() |
| 124 | .fold(0u16, |acc, &modifier| acc | u16::from(modifier)) |
| 125 | } |
| 126 | |
| 127 | pub fn setup_keybinds(connection: &impl Connection, root: Window) -> Result<()> { |
| 128 | for keybinding in KEYBINDINGS { |
| 129 | let modifier_mask = modifiers_to_mask(keybinding.modifiers); |
| 130 | |
| 131 | connection.grab_key( |
| 132 | false, |
| 133 | root, |
| 134 | modifier_mask.into(), |
| 135 | keybinding.key, |
| 136 | GrabMode::ASYNC, |
| 137 | GrabMode::ASYNC, |
| 138 | )?; |
| 139 | } |
| 140 | Ok(()) |
| 141 | } |
| 142 | |
| 143 | pub fn handle_key_press(event: KeyPressEvent) -> Result<(KeyAction, &'static Arg)> { |
| 144 | println!("KeyPress: detail={}, state={:?}", event.detail, event.state); |
| 145 | |
| 146 | for keybinding in KEYBINDINGS { |
| 147 | let modifier_mask = modifiers_to_mask(keybinding.modifiers); |
| 148 | |
| 149 | if event.detail == keybinding.key && event.state == modifier_mask.into() { |
| 150 | return Ok((keybinding.func, &keybinding.arg)); |
| 151 | } |
| 152 | } |
| 153 | |
| 154 | Ok((KeyAction::None, &Arg::None)) |
| 155 | } |