| 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 | } |