Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Find escape by keysym not keycode #17

Merged
merged 2 commits into from
Mar 25, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ cargo install --git https://github.com/neXromancers/hacksaw
### Stability
- Main functionality is all there and pretty solid
- You may experience bugs when invoking hacksaw while a popup is open
- Pressing escape to exit selection is not yet implemented

### Usage

Expand Down
28 changes: 23 additions & 5 deletions src/lib/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use self::parse_format::FormatToken;
use xcb::shape;

pub const CURSOR_GRAB_TRIES: i32 = 5;
const ESC_KEYCODE: u8 = 9;
const ESC_KEYSYM: u32 = 0xff1b;

/// Since MOD_MASK_ANY is apparently bug-ridden, we instead exploit the fact
/// that the modifier masks NONE to MOD_MASK_5 are 0, 1, 2, 4, 8, ... 128.
Expand Down Expand Up @@ -139,23 +139,41 @@ pub fn grab_pointer_set_cursor(conn: &xcb::Connection, root: u32) -> bool {
false
}

pub fn grab_escape_key(conn: &xcb::Connection, root: u32) {
pub fn find_escape_keycode(conn: &xcb::Connection) -> xcb::Keycode {
// https://stackoverflow.com/questions/18689863/obtain-keyboard-layout-and-keysyms-with-xcb
let setup = conn.get_setup();
let cookie = xcb::get_keyboard_mapping(
&conn,
setup.min_keycode(),
setup.max_keycode() - setup.min_keycode() + 1,
);
let reply = cookie.get_reply().expect("failed to get keyboard mapping");

let escape_index = reply
.keysyms()
.iter()
.position(|&keysym| keysym == ESC_KEYSYM)
.expect("failed to find escape keysym");
(escape_index / reply.keysyms_per_keycode() as usize) as u8 + setup.min_keycode()
}

pub fn grab_key(conn: &xcb::Connection, root: u32, keycode: u8) {
for mask in 0..=KEY_GRAB_MASK_MAX {
xcb::grab_key(
&conn,
true,
root,
mask as u16,
ESC_KEYCODE,
keycode,
xcb::GRAB_MODE_ASYNC as u8,
xcb::GRAB_MODE_ASYNC as u8,
);
}
}

pub fn ungrab_escape_key(conn: &xcb::Connection, root: u32) {
pub fn ungrab_key(conn: &xcb::Connection, root: u32, keycode: u8) {
for mask in 0..=KEY_GRAB_MASK_MAX {
xcb::ungrab_key(&conn, ESC_KEYCODE, root, mask as u16);
xcb::ungrab_key(&conn, keycode, root, mask as u16);
}
}

Expand Down
9 changes: 5 additions & 4 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ mod lib;

use lib::parse_args::Opt;
use lib::{
get_window_at_point, get_window_geom, grab_escape_key, grab_pointer_set_cursor, set_shape,
set_title, ungrab_escape_key, HacksawResult, CURSOR_GRAB_TRIES,
find_escape_keycode, get_window_at_point, get_window_geom, grab_key, grab_pointer_set_cursor,
set_shape, set_title, ungrab_key, HacksawResult, CURSOR_GRAB_TRIES,
};
use structopt::StructOpt;

Expand Down Expand Up @@ -52,7 +52,8 @@ fn main() -> Result<(), String> {
));
}

grab_escape_key(&conn, root);
let escape_keycode = find_escape_keycode(&conn);
grab_key(&conn, root, escape_keycode);

let screen_rect =
xcb::Rectangle::new(0, 0, screen.width_in_pixels(), screen.height_in_pixels());
Expand Down Expand Up @@ -207,7 +208,7 @@ fn main() -> Result<(), String> {
}

xcb::ungrab_pointer(&conn, xcb::CURRENT_TIME);
ungrab_escape_key(&conn, root);
ungrab_key(&conn, root, escape_keycode);
xcb::unmap_window(&conn, window);
xcb::destroy_window(&conn, window);
conn.flush();
Expand Down