From a6be4f00bd6f5c4883daca1b4d592f69551fff3f Mon Sep 17 00:00:00 2001 From: Takashi Kokubun Date: Wed, 22 Dec 2021 00:11:21 -0800 Subject: [PATCH] Add troubleshooting messages for X11 --- rustfmt.toml | 1 + src/client/gnome_client.rs | 8 +++----- src/client/mod.rs | 8 ++------ src/client/sway_client.rs | 4 +--- src/client/x11_client.rs | 25 ++++++++++++++----------- src/input.rs | 3 ++- 6 files changed, 23 insertions(+), 26 deletions(-) diff --git a/rustfmt.toml b/rustfmt.toml index 7530651..883c211 100644 --- a/rustfmt.toml +++ b/rustfmt.toml @@ -1 +1,2 @@ max_width = 120 +fn_call_width = 100 diff --git a/src/client/gnome_client.rs b/src/client/gnome_client.rs index 40e492e..28e19d6 100644 --- a/src/client/gnome_client.rs +++ b/src/client/gnome_client.rs @@ -1,5 +1,5 @@ -use zbus::Connection; use crate::client::Client; +use zbus::Connection; pub struct GnomeClient { connection: Option, @@ -7,9 +7,7 @@ pub struct GnomeClient { impl GnomeClient { pub fn new() -> GnomeClient { - GnomeClient { - connection: None, - } + GnomeClient { connection: None } } fn connect(&mut self) { @@ -48,7 +46,7 @@ impl Client for GnomeClient { ) { if let Ok((_actor, json)) = message.body::<(bool, String)>() { if let Ok(wm_class) = serde_json::from_str::(&json) { - return Some(wm_class) + return Some(wm_class); } } } diff --git a/src/client/mod.rs b/src/client/mod.rs index 89d5b2b..053c41b 100644 --- a/src/client/mod.rs +++ b/src/client/mod.rs @@ -24,14 +24,10 @@ impl WMClient { if let None = self.supported { let supported = self.client.supported(); self.supported = Some(supported); - println!( - "application-client: {} (supported: {})", - self.name, - supported - ); + println!("application-client: {} (supported: {})", self.name, supported); } if !self.supported.unwrap() { - return None + return None; } let result = self.client.current_application(); diff --git a/src/client/sway_client.rs b/src/client/sway_client.rs index 37bf6ee..72a250c 100644 --- a/src/client/sway_client.rs +++ b/src/client/sway_client.rs @@ -10,9 +10,7 @@ pub struct SwayClient { impl SwayClient { pub fn new() -> SwayClient { - SwayClient { - connection: None, - } + SwayClient { connection: None } } fn connect(&mut self) { diff --git a/src/client/x11_client.rs b/src/client/x11_client.rs index 4a18963..8a3c205 100644 --- a/src/client/x11_client.rs +++ b/src/client/x11_client.rs @@ -1,4 +1,5 @@ use crate::client::Client; +use std::env; use x11_rs::xlib; pub struct X11Client { @@ -7,16 +8,25 @@ pub struct X11Client { impl X11Client { pub fn new() -> X11Client { - X11Client { - display: None, - } + X11Client { display: None } } fn connect(&mut self) -> *mut xlib::Display { match self.display { Some(display) => display, None => { + if let Err(env::VarError::NotPresent) = env::var("DISPLAY") { + println!("$DISPLAY is not set. Defaulting to DISPLAY=:0"); + env::set_var("DISPLAY", ":0"); + } + let display = unsafe { xlib::XOpenDisplay(std::ptr::null()) }; + if display.is_null() { + let var = env::var("DISPLAY").unwrap(); + println!("warning: Failed to connect to X11."); + println!("If you saw \"No protocol specified\", try running `xhost +SI:localuser:root`."); + println!("If not, make sure `echo $DISPLAY` outputs xremap's $DISPLAY ({}).", var); + } self.display = Some(display); display } @@ -77,14 +87,7 @@ impl Client for X11Client { let mut parent: xlib::Window = 0; let mut children: *mut xlib::Window = &mut 0; unsafe { - if xlib::XQueryTree( - display, - focused_window, - &mut root, - &mut parent, - &mut children, - &mut nchildren, - ) == 0 + if xlib::XQueryTree(display, focused_window, &mut root, &mut parent, &mut children, &mut nchildren) == 0 { break; } diff --git a/src/input.rs b/src/input.rs index 453b98b..efc2c5d 100644 --- a/src/input.rs +++ b/src/input.rs @@ -140,7 +140,8 @@ fn is_keyboard(device: &Device) -> bool { keys.contains(Key::KEY_SPACE) && keys.contains(Key::KEY_A) && keys.contains(Key::KEY_Z) - && !keys.contains(Key::BTN_LEFT) // BTN_MOUSE + // BTN_MOUSE + && !keys.contains(Key::BTN_LEFT) } None => false, }