Skip to content
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
22 changes: 22 additions & 0 deletions Cargo.lock

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

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ ahash = { version = "0.8.12", features = [
], optional = true, default-features = false }
either = "1.15.0"
async-stream = "0.3.6"
chardetng = "0.1"
encoding_rs = "0.8"

[features]
default = [
Expand Down
42 changes: 42 additions & 0 deletions src/encoding.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
//! Encoding detection for byte streams that come back from the Hyprland IPC.
//!
//! Hyprland stores window titles verbatim — whatever bytes the X11/Wayland
//! client set via `_NET_WM_NAME` or `xdg_toplevel::set_title`. Some clients
//! (notably OnlyOffice and Java/Swing apps like Jubler) use a non-UTF-8
//! encoding (typically the JVM's default charset or Latin-1) and so the
//! title contains bytes that are not valid UTF-8.
//!
//! Naively calling `String::from_utf8_lossy` would replace every invalid byte
//! with U+FFFD and destroy the actual content (e.g. `tradu��ocoletiva.docx`
//! instead of `traduçãocoletiva.docx`).
//!
//! This module detects the most likely encoding of the byte stream using
//! `chardetng` (the same library Firefox uses) and decodes with
//! `encoding_rs`. If detection fails or the stream is ambiguous, we fall
//! back to UTF-8 lossy — same behavior as before, no regression.

use chardetng::EncodingDetector;

/// Decode arbitrary bytes as a string, trying to detect the encoding.
///
/// Behavior:
/// 1. If the bytes are valid UTF-8, return them as-is (no detection overhead).
/// 2. Otherwise, run `chardetng` to guess the encoding, then decode with
/// `encoding_rs`. Any remaining invalid bytes are replaced with U+FFFD.
/// 3. As a last resort, return `String::from_utf8_lossy(bytes)` (same as the
/// pre-fix behavior — better than crashing or panicking).
pub fn decode_ipc_response(bytes: &[u8]) -> String {
if let Ok(s) = std::str::from_utf8(bytes) {
return s.to_owned();
}
let mut det = EncodingDetector::new();
det.feed(bytes, true);
let enc = det.guess(None, true);
let (decoded, _, _) = enc.decode(bytes);
if decoded.contains('\u{FFFD}') {
// Detection was wrong / ambiguous — fall back to lossy UTF-8 so the
// caller at least gets the same behavior as before this fix.
return String::from_utf8_lossy(bytes).into_owned();
}
decoded.into_owned()
}
8 changes: 4 additions & 4 deletions src/instance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ impl Instance {
stream.write_all(&content.as_bytes())?;
let mut response = Vec::new();
stream.read_to_end(&mut response)?;
Ok(String::from_utf8_lossy(&response).to_string())
Ok(crate::encoding::decode_ipc_response(&response))
}

#[cfg(any(feature = "async-lite", feature = "tokio"))]
Expand All @@ -82,7 +82,7 @@ impl Instance {
stream.write_all(&content.as_bytes()).await?;
let mut response = Vec::new();
stream.read_to_end(&mut response).await?;
Ok(String::from_utf8_lossy(&response).to_string())
Ok(crate::encoding::decode_ipc_response(&response))
}

#[cfg(feature = "hyprpaper")]
Expand All @@ -104,7 +104,7 @@ impl Instance {
break;
}
}
Ok(String::from_utf8_lossy(&response).to_string())
Ok(crate::encoding::decode_ipc_response(&response))
}

#[cfg(all(feature = "hyprpaper", any(feature = "async-lite", feature = "tokio")))]
Expand All @@ -126,7 +126,7 @@ impl Instance {
break;
}
}
Ok(String::from_utf8_lossy(&response).to_string())
Ok(crate::encoding::decode_ipc_response(&response))
}

#[cfg(feature = "listener")]
Expand Down
4 changes: 4 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@ pub mod config;

/// Holds the error type used throughout the crate
pub mod error;

/// Encoding helpers used internally by the crate
mod encoding;

/// Used to generate the Instances to interface with Hyprland
pub mod instance;

Expand Down
Loading