From 13719b9f702af158f139285e790c10ee306caa0a Mon Sep 17 00:00:00 2001 From: Takashi Kokubun Date: Wed, 22 Dec 2021 21:23:33 -0800 Subject: [PATCH] Drop sudo requirement and stop forking swayipc --- Cargo.lock | 110 +++++++++++++++++++++++++++++++++---- Cargo.toml | 2 +- README.md | 40 ++++++-------- src/client/gnome_client.rs | 4 +- src/client/sway_client.rs | 23 ++++++-- 5 files changed, 134 insertions(+), 45 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 9e0c55c..d62b1ce 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2,6 +2,21 @@ # It is not intended for manual editing. version = 3 +[[package]] +name = "addr2line" +version = "0.17.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b9ecd88a8c8378ca913a680cd98f0f13ac67383d35993f86c90a70e3f137816b" +dependencies = [ + "gimli", +] + +[[package]] +name = "adler" +version = "1.0.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f26201604c87b1e01bd3d98f8d5d9a8fcbb815e8cedb41ffccbeb4bf593a35fe" + [[package]] name = "aho-corasick" version = "0.7.18" @@ -47,6 +62,21 @@ version = "1.0.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "cdb031dd78e28731d87d56cc8ffef4a8f36ca26c38fe2de700543e627f8a464a" +[[package]] +name = "backtrace" +version = "0.3.63" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "321629d8ba6513061f26707241fa9bc89524ff1cd7a915a97ef0c62c666ce1b6" +dependencies = [ + "addr2line", + "cc", + "cfg-if", + "libc", + "miniz_oxide", + "object", + "rustc-demangle", +] + [[package]] name = "bitflags" version = "1.2.1" @@ -154,6 +184,28 @@ dependencies = [ "nix 0.23.1", ] +[[package]] +name = "failure" +version = "0.1.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d32e9bd16cc02eae7db7ef620b392808b89f6a5e16bb3497d159c6b92a0f4f86" +dependencies = [ + "backtrace", + "failure_derive", +] + +[[package]] +name = "failure_derive" +version = "0.1.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "aa4da3c766cd7a0db8242e326e9e4e081edd567072893ed320008189715366a4" +dependencies = [ + "proc-macro2", + "quote", + "syn", + "synstructure", +] + [[package]] name = "fastrand" version = "1.6.0" @@ -282,6 +334,12 @@ dependencies = [ "unicode-width", ] +[[package]] +name = "gimli" +version = "0.26.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "78cc372d058dcf6d5ecd98510e7fbc9e5aec4d21de70f65fea8fecebcd881bd4" + [[package]] name = "hashbrown" version = "0.11.2" @@ -370,6 +428,16 @@ dependencies = [ "autocfg", ] +[[package]] +name = "miniz_oxide" +version = "0.4.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a92518e98c078586bc6c934028adcca4c92a53d6a958196de835170a01d84e4b" +dependencies = [ + "adler", + "autocfg", +] + [[package]] name = "nb-connect" version = "1.2.0" @@ -406,6 +474,15 @@ dependencies = [ "memoffset", ] +[[package]] +name = "object" +version = "0.27.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "67ac1d3f9a1d3616fd9a60c8d74296f22406a238b6a72f5cc1e6f314df4ffbf9" +dependencies = [ + "memchr", +] + [[package]] name = "once_cell" version = "1.9.0" @@ -509,6 +586,12 @@ version = "0.6.25" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f497285884f3fcff424ffc933e56d7cbca511def0c9831a7f9b5f6153e3cc89b" +[[package]] +name = "rustc-demangle" +version = "0.1.21" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7ef03e0a2b150c7a90d01faf6254c9c48a41e95fb2a8c2ac1c6f0d2b9aefc342" + [[package]] name = "ryu" version = "1.0.9" @@ -599,32 +682,35 @@ checksum = "a2eb9349b6444b326872e140eb1cf5e7c522154d69e7a0ffb0fb81c06b37543f" [[package]] name = "swayipc" -version = "3.0.0-alpha.3" -source = "git+https://github.com/k0kubun/swayipc-rs#580b5b6e02b911547c4741bfe240ea8d81d1147c" +version = "2.7.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "92c6565a48b46668f894564e855ea0adfef31f51f549af6fedd9cba8052b210f" dependencies = [ + "failure", "serde", "serde_json", - "swayipc-types", ] [[package]] -name = "swayipc-types" -version = "1.0.0-alpha.3" -source = "git+https://github.com/k0kubun/swayipc-rs#580b5b6e02b911547c4741bfe240ea8d81d1147c" +name = "syn" +version = "1.0.82" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8daf5dd0bb60cbd4137b1b587d2fc0ae729bc07cf01cd70b36a1ed5ade3b9d59" dependencies = [ - "serde", - "serde_json", - "thiserror", + "proc-macro2", + "quote", + "unicode-xid", ] [[package]] -name = "syn" -version = "1.0.82" +name = "synstructure" +version = "0.12.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8daf5dd0bb60cbd4137b1b587d2fc0ae729bc07cf01cd70b36a1ed5ade3b9d59" +checksum = "f36bdaa60a83aca3921b5259d5400cbf5e90fc51931376a9bd4a0eb79aa7210f" dependencies = [ "proc-macro2", "quote", + "syn", "unicode-xid", ] diff --git a/Cargo.toml b/Cargo.toml index 97b18df..560216c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -17,7 +17,7 @@ nix = "0.23.1" serde = { version = "1.0", features = ["derive"] } serde_json = "1.0" serde_yaml = "0.8" -swayipc = { git = "https://github.com/k0kubun/swayipc-rs" } +swayipc = "2.7.2" x11_rs = { package = "x11", version = "2.19.1", features = ["xlib"] } zbus = "1.9.2" diff --git a/README.md b/README.md index d4a2ecb..5dc59ad 100644 --- a/README.md +++ b/README.md @@ -11,9 +11,21 @@ * **Language-agnostic** - The config is JSON-compatible. Generate it from any language, e.g. [Ruby](https://github.com/xremap/xremap-ruby), [Python](https://github.com/xremap/xremap-python). +## Prerequisite + +`xremap` assumes that your user can use `evdev` and `uinput` without sudo. +This can be configured by the following commands: + +```bash +sudo gpasswd -a YOUR_USER input +echo 'KERNEL=="uinput", GROUP="input"' | sudo tee /etc/udev/rules.d/input.rules +``` + +Then reboot your machine to make sure udev rules are applied. + ## Installation -Download a binary from [Releases](https://github.com/k0kubun/xremap/releases). +After the reboot, download a binary from [Releases](https://github.com/k0kubun/xremap/releases). If it doesn't work, please [install Rust](https://doc.rust-lang.org/cargo/getting-started/installation.html) and run one of the following commands in this repository and use `target/release/xremap`: @@ -32,6 +44,8 @@ cargo build xremap --release --features sway cargo build xremap --release ``` +You may also need to install `libx11-dev` to run the `xremap` binary for X11. + ## Usage Write [a config file](#Configuration) directly, or generate it with @@ -39,7 +53,7 @@ Write [a config file](#Configuration) directly, or generate it with Then run: ``` -sudo xremap config.yml +xremap config.yml ``` ### Dynamic binding @@ -49,27 +63,7 @@ Xremap supports application-specific key remapping. While Xremap uses `evdev` and `uinput`, which is a lower layer than X11 and Wayland, Xremap also uses X11 or Wayland compositor-specific protocols to support `application` config. If you need this feature, make sure you specify the correct binary or `--features` option, -and follow the following instructions. - -#### X11 - -You may need to run `xhost +SI:localuser:root` before starting `xremap`. -It prints `No protocol specified` for such cases. - -You may also need to install `libx11-dev` to run the `xremap` binary for X11. - -#### GNOME Wayland - -Before using `xremap`, update `/usr/share/dbus-1/session.conf` as follows and reboot your machine -to allow `sudo xremap` to talk to your user's D-Bus. - -```diff - -+ - - - -``` +and pay attention to the error messages from `xremap`. ## Configuration Your `config.yml` should look like this: diff --git a/src/client/gnome_client.rs b/src/client/gnome_client.rs index 28e19d6..76f03ea 100644 --- a/src/client/gnome_client.rs +++ b/src/client/gnome_client.rs @@ -11,9 +11,7 @@ impl GnomeClient { } fn connect(&mut self) { - let uid = 1000; // Assume a first nornal Linux user. TODO: Make it configurable - let address = format!("unix:path=/run/user/{}/bus", uid); - match Connection::new_for_address(&address, true) { + match Connection::new_session() { Ok(connection) => self.connection = Some(connection), Err(e) => println!("GnomeClient#connect() failed: {}", e), } diff --git a/src/client/sway_client.rs b/src/client/sway_client.rs index 72a250c..e345d82 100644 --- a/src/client/sway_client.rs +++ b/src/client/sway_client.rs @@ -1,7 +1,7 @@ -use crate::client::Client; +use std::env; use std::fs::read_dir; use std::os::unix::ffi::OsStrExt; -use std::os::unix::net::UnixStream; +use crate::client::Client; use swayipc::Connection; pub struct SwayClient { @@ -15,10 +15,21 @@ impl SwayClient { fn connect(&mut self) { if let None = self.connection { - if let Some(socket) = find_socket() { - if let Ok(unix_stream) = UnixStream::connect(socket) { - self.connection = Some(Connection(unix_stream)); - } + if let Err(env::VarError::NotPresent) = env::var("SWAYSOCK") { + let path = match find_socket() { + Some(path) => path, + None => { + println!("Failed to locate a SWAYSOCK from /run/user/1000/sway-ipc.*"); + return; + }, + }; + println!("$SWAYSOCK is not set. Defaulting to \"{}\"", path); + env::set_var("SWAYSOCK", path); + } + + match Connection::new() { + Ok(connection) => self.connection = Some(connection), + Err(e) => println!("SwayClient#connect() failed: {}", e), } } }