modularized keyboard
Browse files
+0 −28 src/keyboard.rs deleted file mode 100644
| @@ -1,28 +0,0 @@ | |||
| 1 | -// wip | ||
| 2 | -pub const RETURN: u8 = 36; | ||
| 3 | -pub const Q: u8 = 24; | ||
| 4 | -pub const ESCAPE: u8 = 9; | ||
| 5 | -pub const SPACE: u8 = 65; | ||
| 6 | -pub const TAB: u8 = 23; | ||
| 7 | -pub const BACKSPACE: u8 = 22; | ||
| 8 | -pub const DELETE: u8 = 119; | ||
| 9 | - | ||
| 10 | -// Function keys | ||
| 11 | -pub const F1: u8 = 67; | ||
| 12 | -pub const F2: u8 = 68; | ||
| 13 | -pub const F3: u8 = 69; | ||
| 14 | -pub const F4: u8 = 70; | ||
| 15 | - | ||
| 16 | -// Letters (assuming QWERTY) | ||
| 17 | -pub const A: u8 = 38; | ||
| 18 | -pub const S: u8 = 39; | ||
| 19 | -pub const D: u8 = 40; | ||
| 20 | -pub const F: u8 = 41; | ||
| 21 | -pub const J: u8 = 44; | ||
| 22 | -pub const K: u8 = 45; | ||
| 23 | -pub const L: u8 = 46; | ||
| 24 | - | ||
| 25 | -// Numbers | ||
| 26 | -pub const KEY_1: u8 = 10; | ||
| 27 | -pub const KEY_2: u8 = 11; | ||
| 28 | -pub const KEY_3: u8 = 12; | ||
+52 −0 src/keyboard/handlers.rs new file mode 100644
| @@ -0,0 +1,52 @@ | |||
| 1 | +use crate::keyboard::keycodes; | ||
| 2 | +use anyhow::Result; | ||
| 3 | +use x11rb::connection::Connection; | ||
| 4 | +use x11rb::protocol::xproto::*; | ||
| 5 | + | ||
| 6 | +pub fn setup_keybinds(connection: &impl Connection, root: Window) -> Result<()> { | ||
| 7 | + connection.grab_key( | ||
| 8 | + false, | ||
| 9 | + root, | ||
| 10 | + ModMask::M4.into(), | ||
| 11 | + keycodes::RETURN, | ||
| 12 | + GrabMode::ASYNC, | ||
| 13 | + GrabMode::ASYNC, | ||
| 14 | + )?; | ||
| 15 | + connection.grab_key( | ||
| 16 | + false, | ||
| 17 | + root, | ||
| 18 | + (ModMask::M1 | ModMask::SHIFT).into(), | ||
| 19 | + keycodes::Q, | ||
| 20 | + GrabMode::ASYNC, | ||
| 21 | + GrabMode::ASYNC, | ||
| 22 | + )?; | ||
| 23 | + connection.grab_key( | ||
| 24 | + false, | ||
| 25 | + root, | ||
| 26 | + ModMask::M4.into(), | ||
| 27 | + keycodes::Q, | ||
| 28 | + GrabMode::ASYNC, | ||
| 29 | + GrabMode::ASYNC, | ||
| 30 | + )?; | ||
| 31 | + Ok(()) | ||
| 32 | +} | ||
| 33 | + | ||
| 34 | +pub fn handle_key_press(connection: &impl Connection, event: KeyPressEvent) -> Result<()> { | ||
| 35 | + println!("KeyPress: detail={}, state={:?}", event.detail, event.state); | ||
| 36 | + match (event.detail, event.state) { | ||
| 37 | + (keycodes::RETURN, state) if state.contains(KeyButMask::MOD1) => { | ||
| 38 | + println!("Spawning terminal"); | ||
| 39 | + std::process::Command::new("xterm").spawn()?; | ||
| 40 | + } | ||
| 41 | + (keycodes::Q, state) if state.contains(KeyButMask::MOD1 | KeyButMask::SHIFT) => { | ||
| 42 | + println!("Closing focused window"); | ||
| 43 | + let focus_reply = connection.get_input_focus()?.reply()?; | ||
| 44 | + if focus_reply.focus != x11rb::NONE && focus_reply.focus != event.root { | ||
| 45 | + connection.kill_client(focus_reply.focus)?; | ||
| 46 | + connection.flush()?; | ||
| 47 | + } | ||
| 48 | + } | ||
| 49 | + _ => {} | ||
| 50 | + } | ||
| 51 | + Ok(()) | ||
| 52 | +} | ||
+28 −0 src/keyboard/keycodes.rs new file mode 100644
| @@ -0,0 +1,28 @@ | |||
| 1 | +// wip | ||
| 2 | +pub const RETURN: u8 = 36; | ||
| 3 | +pub const Q: u8 = 24; | ||
| 4 | +pub const ESCAPE: u8 = 9; | ||
| 5 | +pub const SPACE: u8 = 65; | ||
| 6 | +pub const TAB: u8 = 23; | ||
| 7 | +pub const BACKSPACE: u8 = 22; | ||
| 8 | +pub const DELETE: u8 = 119; | ||
| 9 | + | ||
| 10 | +// Function keys | ||
| 11 | +pub const F1: u8 = 67; | ||
| 12 | +pub const F2: u8 = 68; | ||
| 13 | +pub const F3: u8 = 69; | ||
| 14 | +pub const F4: u8 = 70; | ||
| 15 | + | ||
| 16 | +// Letters (assuming QWERTY) | ||
| 17 | +pub const A: u8 = 38; | ||
| 18 | +pub const S: u8 = 39; | ||
| 19 | +pub const D: u8 = 40; | ||
| 20 | +pub const F: u8 = 41; | ||
| 21 | +pub const J: u8 = 44; | ||
| 22 | +pub const K: u8 = 45; | ||
| 23 | +pub const L: u8 = 46; | ||
| 24 | + | ||
| 25 | +// Numbers | ||
| 26 | +pub const KEY_1: u8 = 10; | ||
| 27 | +pub const KEY_2: u8 = 11; | ||
| 28 | +pub const KEY_3: u8 = 12; | ||
+7 −0 src/keyboard/mod.rs new file mode 100644
| @@ -0,0 +1,7 @@ | |||
| 1 | +// Re-export the public interface | ||
| 2 | +pub mod handlers; | ||
| 3 | +pub mod keycodes; | ||
| 4 | + | ||
| 5 | +// Re-export commonly used items for convenience | ||
| 6 | +pub use handlers::{handle_key_press, setup_keybinds}; | ||
| 7 | +pub use keycodes::*; | ||
+0 −55 src/keys.rs deleted file mode 100644
| @@ -1,55 +0,0 @@ | |||
| 1 | -use crate::keyboard; | ||
| 2 | -use anyhow::Result; | ||
| 3 | -use x11rb::connection::Connection; | ||
| 4 | -use x11rb::protocol::xproto::*; | ||
| 5 | - | ||
| 6 | -pub fn setup_keybinds(connection: &impl Connection, root: Window) -> Result<()> { | ||
| 7 | - connection.grab_key( | ||
| 8 | - false, | ||
| 9 | - root, | ||
| 10 | - ModMask::M4.into(), | ||
| 11 | - keyboard::RETURN, | ||
| 12 | - GrabMode::ASYNC, | ||
| 13 | - GrabMode::ASYNC, | ||
| 14 | - )?; | ||
| 15 | - connection.grab_key( | ||
| 16 | - false, | ||
| 17 | - root, | ||
| 18 | - (ModMask::M1 | ModMask::SHIFT).into(), | ||
| 19 | - keyboard::Q, | ||
| 20 | - GrabMode::ASYNC, | ||
| 21 | - GrabMode::ASYNC, | ||
| 22 | - )?; | ||
| 23 | - | ||
| 24 | - connection.grab_key( | ||
| 25 | - false, | ||
| 26 | - root, | ||
| 27 | - ModMask::M4.into(), | ||
| 28 | - keyboard::Q, | ||
| 29 | - GrabMode::ASYNC, | ||
| 30 | - GrabMode::ASYNC, | ||
| 31 | - )?; | ||
| 32 | - | ||
| 33 | - Ok(()) | ||
| 34 | -} | ||
| 35 | - | ||
| 36 | -pub fn handle_key_press(connection: &impl Connection, event: KeyPressEvent) -> Result<()> { | ||
| 37 | - println!("KeyPress: detail={}, state={:?}", event.detail, event.state); | ||
| 38 | - | ||
| 39 | - match (event.detail, event.state) { | ||
| 40 | - (keyboard::RETURN, state) if state.contains(KeyButMask::MOD1) => { | ||
| 41 | - println!("Spawning terminal"); | ||
| 42 | - std::process::Command::new("xterm").spawn()?; | ||
| 43 | - } | ||
| 44 | - (keyboard::Q, state) if state.contains(KeyButMask::MOD1 | KeyButMask::SHIFT) => { | ||
| 45 | - println!("Closing focused window"); | ||
| 46 | - let focus_reply = connection.get_input_focus()?.reply()?; | ||
| 47 | - if focus_reply.focus != x11rb::NONE && focus_reply.focus != event.root { | ||
| 48 | - connection.kill_client(focus_reply.focus)?; | ||
| 49 | - connection.flush()?; | ||
| 50 | - } | ||
| 51 | - } | ||
| 52 | - _ => {} | ||
| 53 | - } | ||
| 54 | - Ok(()) | ||
| 55 | -} | ||
+0 −1 src/main.rs
| @@ -1,6 +1,5 @@ | |||
| 1 | use anyhow::Result; | 1 | use anyhow::Result; |
| 2 | mod keyboard; | 2 | mod keyboard; |
| 3 | -mod keys; | ||
| 4 | mod layout; | 3 | mod layout; |
| 5 | mod window_manager; | 4 | mod window_manager; |
| 6 | 5 | ||
+4 −3 src/window_manager.rs
| @@ -1,4 +1,5 @@ | |||
| 1 | -use crate::keys; | 1 | +use crate::keyboard; |
| 2 | +// use crate::keys; | ||
| 2 | use crate::layout::Layout; | 3 | use crate::layout::Layout; |
| 3 | use crate::layout::tiling::TilingLayout; | 4 | use crate::layout::tiling::TilingLayout; |
| 4 | 5 | ||
| @@ -49,7 +50,7 @@ impl WindowManager { | |||
| 49 | pub fn run(&mut self) -> Result<()> { | 50 | pub fn run(&mut self) -> Result<()> { |
| 50 | println!("oxwm started on display {}", self.screen_number); | 51 | println!("oxwm started on display {}", self.screen_number); |
| 51 | 52 | ||
| 52 | - keys::setup_keybinds(&self.connection, self.root)?; | 53 | + keyboard::setup_keybinds(&self.connection, self.root)?; |
| 53 | 54 | ||
| 54 | loop { | 55 | loop { |
| 55 | let event = self.connection.wait_for_event()?; | 56 | let event = self.connection.wait_for_event()?; |
| @@ -73,7 +74,7 @@ impl WindowManager { | |||
| 73 | } | 74 | } |
| 74 | Event::KeyPress(event) => { | 75 | Event::KeyPress(event) => { |
| 75 | println!("KeyPress event received!"); | 76 | println!("KeyPress event received!"); |
| 76 | - keys::handle_key_press(&self.connection, event)?; | 77 | + keyboard::handle_key_press(&self.connection, event)?; |
| 77 | } | 78 | } |
| 78 | _ => {} | 79 | _ => {} |
| 79 | } | 80 | } |