-
Notifications
You must be signed in to change notification settings - Fork 476
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
keyboard_layout: Add xkbswitch support (#1985)
- Loading branch information
1 parent
fb65e8d
commit 5500fed
Showing
2 changed files
with
60 additions
and
1 deletion.
There are no files selected for viewing
This file contains 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 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,44 @@ | ||
use super::*; | ||
use tokio::process::Command; | ||
|
||
pub(super) struct XkbSwitch(Seconds); | ||
|
||
impl XkbSwitch { | ||
pub(super) fn new(update_interval: Seconds) -> Self { | ||
Self(update_interval) | ||
} | ||
} | ||
|
||
#[async_trait] | ||
impl Backend for XkbSwitch { | ||
async fn get_info(&mut self) -> Result<Info> { | ||
// This command output is in the format of "layout(variant)" or "layout" | ||
let output = Command::new("xkb-switch") | ||
.arg("-p") | ||
.output() | ||
.await | ||
.error("Failed to execute 'xkb-switch -p'")?; | ||
|
||
let output = | ||
String::from_utf8(output.stdout).error("xkb-switch produces a non-UTF8 output")?; | ||
|
||
let mut components = output.trim_end().split('('); | ||
|
||
let layout = components | ||
.next() | ||
.error("Could not find layout entry in xkb-switch")? | ||
.to_string(); | ||
|
||
let variant = components | ||
.last() | ||
// Remove the trailing parenthesis ")" | ||
.map(|variant_str| variant_str.split_at(variant_str.len() - 1).0.to_string()); | ||
|
||
Ok(Info { layout, variant }) | ||
} | ||
|
||
async fn wait_for_change(&mut self) -> Result<()> { | ||
sleep(self.0 .0).await; | ||
Ok(()) | ||
} | ||
} |