Skip to content

Commit

Permalink
fix: retry get_kb_layouts_count 5 times with 1 sec delay
Browse files Browse the repository at this point in the history
- fix #20
  • Loading branch information
coffebar committed Jul 27, 2024
1 parent 9101266 commit 0a541bf
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 6 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
name = "hyprland-per-window-layout"
description = "Per window keyboard layout (language) for Hyprland wayland compositor"
version = "0.2.11"
version = "0.2.12"
edition = "2021"
exclude = ["target", "Cargo.lock"]
readme = "README.md"
Expand Down
28 changes: 24 additions & 4 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,18 +50,22 @@ fn listen(socket_addr: String) -> std::io::Result<()> {
}

// get keyboards count listed in hyprland conf file (input section)
fn get_kb_layouts_count() -> u16 {
// return -1 if failed
fn get_kb_layouts_count() -> i16 {
// get layouts list from hyprctl cli call
match hyprctl(["getoption", "input:kb_layout", "-j"].to_vec()) {
Ok(output) => {
log::debug!("input:kb_layout: {}", output);
// parse the string from stdin into serde_json::Value
let json: Value = serde_json::from_str(&output).unwrap();
let json: Value = serde_json::from_str(&output).unwrap_or(Value::Null);
if json.is_null() {
return -1;
}
let kb_layout = str::replace(&json["str"].to_string().trim(), "\"", "");

if kb_layout.len() > 0 {
let items: Vec<&str> = kb_layout.split(",").collect();
return items.len() as u16;
return items.len() as i16;
} else {
0
}
Expand All @@ -73,6 +77,22 @@ fn get_kb_layouts_count() -> u16 {
}
}

// try to get kb layouts count 5 times with 1 sec delay
fn get_kb_layouts_count_retry() -> i16 {
let mut count = 0;
loop {
let layouts_found = get_kb_layouts_count();
if layouts_found > -1 {
return layouts_found;
}
count += 1;
if count > 5 {
return -1;
}
std::thread::sleep(std::time::Duration::from_secs(1));
}
}

// check kb_file option is set in hyprland conf file
fn kb_file_isset() -> bool {
// get layouts list from hyprctl cli call
Expand Down Expand Up @@ -126,7 +146,7 @@ fn main() {
std::process::exit(1);
}
// this program make sense if you have 2+ layouts
let layouts_found = get_kb_layouts_count();
let layouts_found = get_kb_layouts_count_retry();

if layouts_found < 2 && !kb_file_isset() {
println!("Fatal error: You need to configure layouts on Hyprland");
Expand Down

0 comments on commit 0a541bf

Please sign in to comment.