-
Notifications
You must be signed in to change notification settings - Fork 110
Use the user's keyboard layout from the x11 system #210
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
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
ec80f1d
Use the user's keyboard layout from the x11 system
TristanCacqueray e1d1b99
Add support for missing xkbcommon library
TristanCacqueray 76c4c8e
Add XkbcommonState Drop impl
TristanCacqueray d62b3dd
Run cargo fmt
TristanCacqueray b50bbfc
Add missing xkb context unref
TristanCacqueray File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,86 @@ | ||
| use xkbcommon_dl as xkbc; | ||
|
|
||
| pub(crate) type Keycode = xkbcommon_dl::xkb_keycode_t; | ||
| /// A xkbcommon state object | ||
| pub struct XkbcommonState { | ||
| state: *mut xkbc::xkb_state, | ||
| xkb_common: &'static xkbc::XkbCommon, | ||
| } | ||
|
|
||
| impl XkbcommonState { | ||
| pub fn new(xcb_connection: &crate::x11::XcbConnection) -> Option<Self> { | ||
| let xkb_common = xkbc::xkbcommon_option()?; | ||
| let xkb_x11 = xkbc::x11::xkbcommon_x11_option()?; | ||
|
|
||
| // libxkbcommon is avail, let's allocate a new context | ||
| let context = | ||
| unsafe { (xkb_common.xkb_context_new)(xkbc::xkb_context_flags::XKB_CONTEXT_NO_FLAGS) }; | ||
|
|
||
| let conn: *mut xkbc::x11::xcb_connection_t = | ||
| xcb_connection.conn.xcb_connection().get_raw_xcb_connection(); | ||
|
|
||
| let state = unsafe { | ||
| let device_id = (xkb_x11.xkb_x11_get_core_keyboard_device_id)(conn); | ||
| assert!(device_id >= 0); | ||
| // keymap should be unref when XkbcommonState drop | ||
| let keymap = (xkb_x11.xkb_x11_keymap_new_from_device)( | ||
| context, | ||
| conn, | ||
| device_id, | ||
| xkbc::xkb_keymap_compile_flags::XKB_KEYMAP_COMPILE_NO_FLAGS, | ||
| ); | ||
| // state will be unref on drop | ||
| (xkb_x11.xkb_x11_state_new_from_device)(keymap, conn, device_id) | ||
| }; | ||
|
|
||
| // free the context, it's no longer needed | ||
| unsafe { (xkb_common.xkb_context_unref)(context) }; | ||
|
|
||
| Some(XkbcommonState { state, xkb_common }) | ||
| } | ||
|
|
||
| pub fn key_get_utf8(&self, code: Keycode) -> String { | ||
| // A buffer to store the cstr | ||
| let buffer_size = 32; | ||
| let mut buffer = vec![0; buffer_size]; | ||
| let result = unsafe { | ||
| (self.xkb_common.xkb_state_key_get_utf8)( | ||
| self.state, | ||
| code, | ||
| buffer.as_mut_ptr(), | ||
| buffer_size, | ||
| ) | ||
| }; | ||
|
|
||
| // Convert back to String | ||
| if result < 0 { | ||
| "".to_string() | ||
| } else { | ||
| let c_str = unsafe { std::ffi::CStr::from_ptr(buffer.as_ptr()) }; | ||
| match c_str.to_str() { | ||
| Ok(s) => s.to_string(), | ||
| Err(_) => "".to_string(), | ||
| } | ||
| } | ||
| } | ||
|
|
||
| pub fn update_key(&mut self, code: Keycode, dir: xkbc::xkb_key_direction) { | ||
| unsafe { | ||
| (self.xkb_common.xkb_state_update_key)(self.state, code, dir); | ||
| } | ||
| } | ||
|
|
||
| pub fn update_key_down(&mut self, code: Keycode) { | ||
| self.update_key(code, xkbc::xkb_key_direction::XKB_KEY_DOWN) | ||
| } | ||
|
|
||
| pub fn update_key_up(&mut self, code: Keycode) { | ||
| self.update_key(code, xkbc::xkb_key_direction::XKB_KEY_UP) | ||
| } | ||
| } | ||
|
|
||
| impl Drop for XkbcommonState { | ||
| fn drop(&mut self) { | ||
| unsafe { (self.xkb_common.xkb_state_unref)(self.state) }; | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.