Skip to content

Commit

Permalink
Merge pull request #236 from pentamassiv/x11rb_fixes
Browse files Browse the repository at this point in the history
X11rb fixes
  • Loading branch information
pentamassiv authored Oct 23, 2023
2 parents 605ddf0 + 0a4c51e commit 96d5fd2
Show file tree
Hide file tree
Showing 10 changed files with 498 additions and 219 deletions.
1 change: 1 addition & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
- Linux: Partial support for Wayland was added. Use the experimental feature `wayland` to test it. Only the virtual_keyboard and input_method protocol can be used. This is not going to work on GNOME, but should work for example with phosh
- win: Use DirectInput in addition to the SetCursorPos function in order to support DirectX
- All: You can now chose how long the delay between keypresses should be on each platform and change it during the runtime
- All: You can now use a logger to investigate errors

## Fixed
- macOS: Add info how much a mouse was moved relative to the last position
Expand Down
80 changes: 78 additions & 2 deletions src/keycodes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -572,12 +572,12 @@ pub enum Key {

#[cfg(target_os = "linux")]
/// Converts a Key to a Keysym
impl TryFrom<Key> for xkbcommon::xkb::Keysym {
impl TryFrom<Key> for xkeysym::Keysym {
type Error = &'static str;

#[allow(clippy::too_many_lines)]
fn try_from(key: Key) -> Result<Self, &'static str> {
use xkbcommon::xkb::Keysym;
use xkeysym::Keysym;

#[allow(clippy::match_same_arms)]
Ok(match key {
Expand Down Expand Up @@ -676,3 +676,79 @@ impl TryFrom<Key> for xkbcommon::xkb::Keysym {
})
}
}

#[cfg(target_os = "linux")]
#[cfg(any(feature = "wayland", feature = "x11rb"))]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum Modifier {
Shift,
Lock,
Control,
Mod1,
Mod2,
Mod3,
Mod4,
Mod5,
}

#[cfg(target_os = "linux")]
#[cfg(any(feature = "wayland", feature = "x11rb"))]
impl Modifier {
/// Returns the bitflag of the modifier that is usually associated with it
/// on Linux
#[must_use]
pub fn bitflag(&self) -> ModifierBitflag {
match self {
Self::Shift => 0x1,
Self::Lock => 0x2,
Self::Control => 0x4,
Self::Mod1 => 0x8,
Self::Mod2 => 0x10,
Self::Mod3 => 0x20,
Self::Mod4 => 0x40,
Self::Mod5 => 0x80,
}
}

/// Returns the number of the modifier that is usually associated with it
/// on Linux
#[must_use]
pub fn no(&self) -> usize {
match self {
Self::Shift => 0,
Self::Lock => 1,
Self::Control => 2,
Self::Mod1 => 3,
Self::Mod2 => 4,
Self::Mod3 => 5,
Self::Mod4 => 6,
Self::Mod5 => 7,
}
}
}

#[cfg(target_os = "linux")]
#[cfg(any(feature = "wayland", feature = "x11rb"))]
/// Converts a Key to a modifier
impl TryFrom<Key> for Modifier {
type Error = &'static str;

fn try_from(key: Key) -> Result<Self, &'static str> {
match key {
Key::Shift | Key::LShift | Key::RShift => Ok(Self::Shift),
Key::CapsLock => Ok(Self::Lock),
Key::Control | Key::LControl | Key::RControl => Ok(Self::Control),
Key::Alt | Key::Option => Ok(Self::Mod1),
Key::Numlock => Ok(Self::Mod2),
// The Mod3 modifier is usually unmapped
// Key::Mod3 => Ok(Self::Mod3),
Key::Command | Key::Super | Key::Windows | Key::Meta => Ok(Self::Mod4),
Key::ModeChange => Ok(Self::Mod5),
_ => Err("not a modifier key"),
}
}
}

#[cfg(target_os = "linux")]
#[cfg(any(feature = "wayland", feature = "x11rb"))]
pub(crate) type ModifierBitflag = u32; // TODO: Maybe create a proper type for this
23 changes: 21 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -615,7 +615,16 @@ pub enum InputError {

impl Display for InputError {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
write!(f, "error establishing X11 connection with x11rb")
let string = match self {
InputError::Mapping(e) => format!("error when mapping keycode to keysym: ({e})"),
InputError::Unmapping(e) => format!("error when unmapping keysym: ({e})"),
InputError::NoEmptyKeycodes => {
"there were no empty keycodes that could be used".to_string()
}
InputError::Simulate(e) => format!("simulating input failed: ({e})"),
InputError::InvalidInput(e) => format!("you tried to simulate invalid input: ({e})"),
};
write!(f, "{string}")
}
}

Expand All @@ -634,7 +643,17 @@ pub enum NewConError {

impl Display for NewConError {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
write!(f, "error establishing X11 connection with x11rb")
let string = match self {
NewConError::EstablishCon(e) => format!("no connection could be established: ({e})"),
NewConError::Reply => {
"there was an error with the reply from the display server. this should not happen"
.to_string()
}
NewConError::NoEmptyKeycodes => {
"there were no empty keycodes that could be used".to_string()
}
};
write!(f, "{string}")
}
}

Expand Down
Loading

0 comments on commit 96d5fd2

Please sign in to comment.