Skip to content

Commit

Permalink
0.4.3
Browse files Browse the repository at this point in the history
fix(linux): Replace ATOM_WM_NAME for _NET_WM_NAME to get window title
  • Loading branch information
miniben-90 committed Apr 1, 2023
1 parent cd61024 commit 69b735c
Show file tree
Hide file tree
Showing 9 changed files with 55 additions and 34 deletions.
2 changes: 1 addition & 1 deletion npm/darwin-arm64/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@miniben90/x-win-darwin-arm64",
"version": "0.4.2",
"version": "0.4.3",
"os": [
"darwin"
],
Expand Down
2 changes: 1 addition & 1 deletion npm/darwin-universal/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@miniben90/x-win-darwin-universal",
"version": "0.4.2",
"version": "0.4.3",
"os": [
"darwin"
],
Expand Down
2 changes: 1 addition & 1 deletion npm/darwin-x64/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@miniben90/x-win-darwin-x64",
"version": "0.4.2",
"version": "0.4.3",
"os": [
"darwin"
],
Expand Down
2 changes: 1 addition & 1 deletion npm/linux-x64-gnu/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@miniben90/x-win-linux-x64-gnu",
"version": "0.4.2",
"version": "0.4.3",
"os": [
"linux"
],
Expand Down
2 changes: 1 addition & 1 deletion npm/win32-arm64-msvc/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@miniben90/x-win-win32-arm64-msvc",
"version": "0.4.2",
"version": "0.4.3",
"os": [
"win32"
],
Expand Down
2 changes: 1 addition & 1 deletion npm/win32-ia32-msvc/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@miniben90/x-win-win32-ia32-msvc",
"version": "0.4.2",
"version": "0.4.3",
"os": [
"win32"
],
Expand Down
2 changes: 1 addition & 1 deletion npm/win32-x64-msvc/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@miniben90/x-win-win32-x64-msvc",
"version": "0.4.2",
"version": "0.4.3",
"os": [
"win32"
],
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@miniben90/x-win",
"description": "This package allows you to retrieve precise information about active and open windows on Windows, MacOS, and Linux. You can obtain the position, size, title, and other memory of windows.",
"version": "0.4.2",
"version": "0.4.3",
"main": "index.js",
"types": "index.d.ts",
"files": [
Expand Down
73 changes: 47 additions & 26 deletions src/linux/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -249,46 +249,67 @@ fn decode_text(bytes: &[u8]) -> String {
while index < bytes.len() {
let ch = bytes[index];
if ch < 128 {
result.push(ch as char);
index += 1;
result.push(ch as char);
index += 1;
} else {
let ch_utf8 = match std::str::from_utf8(&bytes[index..index + 3]) {
Ok(ch) => ch.to_string(),
Err(_) => xcb::Lat1String::from_bytes(&bytes[index..index + 3])
.to_utf8()
.to_string(),
let ch_utf8 = match std::str::from_utf8(&bytes[index..index+3]) {
Ok(ch) => ch.to_string(),
Err(_) => xcb::Lat1String::from_bytes(&bytes[index..index+3]).to_utf8().to_string(),
};
if ch == 194 || ch == 195 {
result.push_str(&ch_utf8);
index += 2;
} else {
result.push_str(&ch_utf8);
index += 3;
}

result.push_str(&ch_utf8);
index += 3;
}
}
}
if let Ok(result) = strip_ansi_escapes::strip(&result) {
return std::str::from_utf8(&result).unwrap().to_string();
} else {
return result;
}
}

/**
* Generate Atom of COMPOUND_TEXT value
*/
fn get_compound_text_atom(conn: &xcb::Connection) -> xcb::Result<x::Atom> {
let compound_text = conn.send_request(&x::InternAtom {
only_if_exists: true,
name: b"C_STRING",
});
Ok(conn.wait_for_reply(compound_text)?.atom())
}

/**
* Generate Atom of COMPOUND_TEXT value
*/
fn get_wm_name_atom(conn: &xcb::Connection) -> xcb::Result<x::Atom> {
let compound_text = conn.send_request(&x::InternAtom {
only_if_exists: true,
name: b"_NET_WM_NAME",
});
Ok(conn.wait_for_reply(compound_text)?.atom())
}

/**
* Get window title
*/
fn get_window_title(conn: &xcb::Connection, window: x::Window) -> String {
let window_title = conn.send_request(&x::GetProperty {
delete: false,
window,
property: x::ATOM_WM_NAME,
r#type: x::ATOM_ANY,
long_offset: 0,
long_length: std::u32::MAX,
});
if let Ok(window_title) = conn.wait_for_reply(window_title) {
let window_title: &[u8] = window_title.value();
return decode_text(window_title);
if let Ok(atom_wm_name) = get_wm_name_atom(&conn) {
if let Ok(atom_compound_text) = get_compound_text_atom(&conn) {
let window_title = conn.send_request(&x::GetProperty {
delete: false,
window,
property: atom_wm_name,
r#type: atom_compound_text,
long_offset: 0,
long_length: std::u32::MAX,
});
if let Ok(window_title) = conn.wait_for_reply(window_title) {
let window_title: &[u8] = window_title.value();
let window_title = decode_text(window_title);
return window_title;
}
}
}
return "".to_owned();
}
Expand Down

0 comments on commit 69b735c

Please sign in to comment.