Drop sudo requirement and stop forking swayipc

This commit is contained in:
Takashi Kokubun 2021-12-22 21:23:33 -08:00
parent bcd621762d
commit 13719b9f70
No known key found for this signature in database
GPG Key ID: 6FFC433B12EE23DD
5 changed files with 135 additions and 46 deletions

112
Cargo.lock generated
View File

@ -2,6 +2,21 @@
# It is not intended for manual editing. # It is not intended for manual editing.
version = 3 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]] [[package]]
name = "aho-corasick" name = "aho-corasick"
version = "0.7.18" version = "0.7.18"
@ -47,6 +62,21 @@ version = "1.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cdb031dd78e28731d87d56cc8ffef4a8f36ca26c38fe2de700543e627f8a464a" 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]] [[package]]
name = "bitflags" name = "bitflags"
version = "1.2.1" version = "1.2.1"
@ -154,6 +184,28 @@ dependencies = [
"nix 0.23.1", "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]] [[package]]
name = "fastrand" name = "fastrand"
version = "1.6.0" version = "1.6.0"
@ -282,6 +334,12 @@ dependencies = [
"unicode-width", "unicode-width",
] ]
[[package]]
name = "gimli"
version = "0.26.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "78cc372d058dcf6d5ecd98510e7fbc9e5aec4d21de70f65fea8fecebcd881bd4"
[[package]] [[package]]
name = "hashbrown" name = "hashbrown"
version = "0.11.2" version = "0.11.2"
@ -370,6 +428,16 @@ dependencies = [
"autocfg", "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]] [[package]]
name = "nb-connect" name = "nb-connect"
version = "1.2.0" version = "1.2.0"
@ -406,6 +474,15 @@ dependencies = [
"memoffset", "memoffset",
] ]
[[package]]
name = "object"
version = "0.27.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "67ac1d3f9a1d3616fd9a60c8d74296f22406a238b6a72f5cc1e6f314df4ffbf9"
dependencies = [
"memchr",
]
[[package]] [[package]]
name = "once_cell" name = "once_cell"
version = "1.9.0" version = "1.9.0"
@ -509,6 +586,12 @@ version = "0.6.25"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f497285884f3fcff424ffc933e56d7cbca511def0c9831a7f9b5f6153e3cc89b" checksum = "f497285884f3fcff424ffc933e56d7cbca511def0c9831a7f9b5f6153e3cc89b"
[[package]]
name = "rustc-demangle"
version = "0.1.21"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7ef03e0a2b150c7a90d01faf6254c9c48a41e95fb2a8c2ac1c6f0d2b9aefc342"
[[package]] [[package]]
name = "ryu" name = "ryu"
version = "1.0.9" version = "1.0.9"
@ -599,22 +682,13 @@ checksum = "a2eb9349b6444b326872e140eb1cf5e7c522154d69e7a0ffb0fb81c06b37543f"
[[package]] [[package]]
name = "swayipc" name = "swayipc"
version = "3.0.0-alpha.3" version = "2.7.2"
source = "git+https://github.com/k0kubun/swayipc-rs#580b5b6e02b911547c4741bfe240ea8d81d1147c" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "92c6565a48b46668f894564e855ea0adfef31f51f549af6fedd9cba8052b210f"
dependencies = [ dependencies = [
"failure",
"serde", "serde",
"serde_json", "serde_json",
"swayipc-types",
]
[[package]]
name = "swayipc-types"
version = "1.0.0-alpha.3"
source = "git+https://github.com/k0kubun/swayipc-rs#580b5b6e02b911547c4741bfe240ea8d81d1147c"
dependencies = [
"serde",
"serde_json",
"thiserror",
] ]
[[package]] [[package]]
@ -628,6 +702,18 @@ dependencies = [
"unicode-xid", "unicode-xid",
] ]
[[package]]
name = "synstructure"
version = "0.12.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f36bdaa60a83aca3921b5259d5400cbf5e90fc51931376a9bd4a0eb79aa7210f"
dependencies = [
"proc-macro2",
"quote",
"syn",
"unicode-xid",
]
[[package]] [[package]]
name = "tap" name = "tap"
version = "1.0.1" version = "1.0.1"

View File

@ -17,7 +17,7 @@ nix = "0.23.1"
serde = { version = "1.0", features = ["derive"] } serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0" serde_json = "1.0"
serde_yaml = "0.8" 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"] } x11_rs = { package = "x11", version = "2.19.1", features = ["xlib"] }
zbus = "1.9.2" zbus = "1.9.2"

View File

@ -11,9 +11,21 @@
* **Language-agnostic** - The config is JSON-compatible. Generate it from any language, * **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). 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 ## 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) 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`: 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 cargo build xremap --release
``` ```
You may also need to install `libx11-dev` to run the `xremap` binary for X11.
## Usage ## Usage
Write [a config file](#Configuration) directly, or generate it with 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: Then run:
``` ```
sudo xremap config.yml xremap config.yml
``` ```
### Dynamic binding ### 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, 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. 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, If you need this feature, make sure you specify the correct binary or `--features` option,
and follow the following instructions. and pay attention to the error messages from `xremap`.
#### 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
<policy context="default">
+ <allow user="root"/>
<!-- Allow everything to be sent -->
<allow send_destination="*" eavesdrop="true"/>
<!-- Allow everything to be received -->
```
## Configuration ## Configuration
Your `config.yml` should look like this: Your `config.yml` should look like this:

View File

@ -11,9 +11,7 @@ impl GnomeClient {
} }
fn connect(&mut self) { fn connect(&mut self) {
let uid = 1000; // Assume a first nornal Linux user. TODO: Make it configurable match Connection::new_session() {
let address = format!("unix:path=/run/user/{}/bus", uid);
match Connection::new_for_address(&address, true) {
Ok(connection) => self.connection = Some(connection), Ok(connection) => self.connection = Some(connection),
Err(e) => println!("GnomeClient#connect() failed: {}", e), Err(e) => println!("GnomeClient#connect() failed: {}", e),
} }

View File

@ -1,7 +1,7 @@
use crate::client::Client; use std::env;
use std::fs::read_dir; use std::fs::read_dir;
use std::os::unix::ffi::OsStrExt; use std::os::unix::ffi::OsStrExt;
use std::os::unix::net::UnixStream; use crate::client::Client;
use swayipc::Connection; use swayipc::Connection;
pub struct SwayClient { pub struct SwayClient {
@ -15,10 +15,21 @@ impl SwayClient {
fn connect(&mut self) { fn connect(&mut self) {
if let None = self.connection { if let None = self.connection {
if let Some(socket) = find_socket() { if let Err(env::VarError::NotPresent) = env::var("SWAYSOCK") {
if let Ok(unix_stream) = UnixStream::connect(socket) { let path = match find_socket() {
self.connection = Some(Connection(unix_stream)); 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),
} }
} }
} }