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

Add API to manipulate XKB layouts #1188

Merged
merged 2 commits into from
Nov 8, 2023

Conversation

kchibisov
Copy link
Member

@kchibisov kchibisov commented Oct 30, 2023

While this is not ideal it does the job to implement basic layouts
functionality like switching layout on compositor and propagating the
change to client, resolving latin character for Keysym handles and
set/restore layouts.

--

Showcase could be found here YaLTeR/niri#65

@kchibisov kchibisov force-pushed the keyboard-layouts-are-fun branch from 462006c to 02005f8 Compare November 1, 2023 20:05
While this is not ideal it does the job to implement basic layouts
functionality like switching layout on compositor and propagating the
change to client, resolving latin character for Keysym handles and
set/restore layouts.
@kchibisov kchibisov force-pushed the keyboard-layouts-are-fun branch from 02005f8 to 97ff566 Compare November 1, 2023 20:22
@kchibisov kchibisov changed the title Add logic to manipulate keyboard layouts Add API to manipulate XKB layouts Nov 1, 2023
@kchibisov kchibisov marked this pull request as ready for review November 1, 2023 20:24
@codecov-commenter
Copy link

codecov-commenter commented Nov 1, 2023

Codecov Report

Attention: 103 lines in your changes are missing coverage. Please review.

Comparison is base (60e41f8) 22.00% compared to head (73bd4ff) 21.89%.
Report is 2 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #1188      +/-   ##
==========================================
- Coverage   22.00%   21.89%   -0.11%     
==========================================
  Files         154      154              
  Lines       24094    24194     +100     
==========================================
- Hits         5301     5298       -3     
- Misses      18793    18896     +103     
Flag Coverage Δ
wlcs-buffer 18.90% <0.00%> (-0.08%) ⬇️
wlcs-core 18.59% <0.00%> (-0.08%) ⬇️
wlcs-output 7.71% <0.00%> (-0.04%) ⬇️
wlcs-pointer-input 20.69% <0.00%> (-0.16%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

Files Coverage Δ
src/input/keyboard/mod.rs 10.36% <0.00%> (-2.65%) ⬇️

... and 1 file with indirect coverage changes

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

src/input/keyboard/mod.rs Outdated Show resolved Hide resolved
src/input/keyboard/mod.rs Outdated Show resolved Hide resolved
Comment on lines 261 to 290
pub fn raw_latin_sym_or_raw_current_sym(&self) -> Option<Keysym> {
let effective_layout = Layout(self.state.key_get_layout(self.keycode));
// NOTE: There's always a keysym in the current layout given that we have modified_sym.
let base_sym = *self.raw_syms().first()?;

// If the character is ascii or non-printable, return it.
if base_sym.key_char().map(|ch| ch.is_ascii()).unwrap_or(true) {
return Some(base_sym);
};

// Try to look other layouts and find the one with ascii character.
for layout in self.layouts() {
if layout == effective_layout {
continue;
}

for keysym in self.raw_syms_for_key_in_layout(self.keycode, layout) {
// NOTE: Only check for ascii alphabetic since the rest of the punctuation should
// be uniform across layouts.
if keysym
.key_char()
.map(|key| key.is_ascii_alphabetic())
.unwrap_or(false)
{
return Some(*keysym);
}
}
}

Some(base_sym)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I decided to go with something like that for now, it sort of works, even though not ideal, but it should be enough to not trigger bindings you don't want to trigger, like due to different Enter locations. Not sure how it's relevant though.

There's no helper to check for latin, but key_char logic sort of does that already.

src/input/keyboard/mod.rs Outdated Show resolved Hide resolved
src/input/keyboard/mod.rs Outdated Show resolved Hide resolved
src/input/keyboard/mod.rs Outdated Show resolved Hide resolved
src/input/keyboard/mod.rs Outdated Show resolved Hide resolved
src/input/keyboard/mod.rs Show resolved Hide resolved
src/input/keyboard/mod.rs Show resolved Hide resolved
src/input/keyboard/mod.rs Outdated Show resolved Hide resolved
src/input/keyboard/mod.rs Outdated Show resolved Hide resolved
src/input/keyboard/mod.rs Outdated Show resolved Hide resolved
pub fn raw_latin_sym_or_raw_current_sym(&self) -> Option<Keysym> {
let effective_layout = Layout(self.state.key_get_layout(self.keycode));
// NOTE: There's always a keysym in the current layout given that we have modified_sym.
let base_sym = *self.raw_syms().first()?;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see xkb has key_get_one_sym for when you only want a single keysym rather than multiple (it returns NoSymbol in case there would be multiple), but I don't see a corresponding by_level function. Maybe it would make sense to replicate this here by only proceeding when raw_syms() has exactly one element. But also it's probably fine as is?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it's fine as is, you need api for layout + level, the thing I use is basically the only one available.

@kchibisov kchibisov requested a review from YaLTeR November 2, 2023 10:10
@Drakulix Drakulix merged commit 79d4ec1 into Smithay:master Nov 8, 2023
13 checks passed
@kchibisov kchibisov deleted the keyboard-layouts-are-fun branch November 8, 2023 11:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants