Skip to content

Commit

Permalink
upgrade deps
Browse files Browse the repository at this point in the history
  • Loading branch information
Yamakaky committed May 8, 2024
1 parent 6adb0e8 commit 7126c31
Show file tree
Hide file tree
Showing 12 changed files with 796 additions and 480 deletions.
1,085 changes: 719 additions & 366 deletions Cargo.lock

Large diffs are not rendered by default.

18 changes: 6 additions & 12 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,23 +19,17 @@ lto = true
[dependencies]
anyhow = { version = "1.0", features = ["backtrace"] }
cgmath = "0.18"
clap = "3.0.0-beta.2"
enigo = { git = "https://github.com/Yamakaky/enigo", branch = "xdo-sys" }
# see https://gitlab.com/KonradBorowski/enum-map/-/issues/20
enum-map = "0.6"
clap = { version = "4.0", features = ["cargo", "derive"] }
enigo = { version = "0.2", features = ["wayland"] }
enum-map = "2.0"
hid-gamepad-types = { git = "https://github.com/Yamakaky/joy" }
hid-gamepad = { git = "https://github.com/Yamakaky/joy", optional = true }
joycon = { git = "https://github.com/Yamakaky/joy", optional = true }
nom = "7.0.0"
nom-supreme = "0.6.0"
human-panic = "1.0.3"
nom-supreme = "0.8"
human-panic = "2.0"
virtual-gamepad = { git = "https://github.com/Yamakaky/virtual-gamepad", optional = true }

[dependencies.sdl2]
git = "https://github.com/Yamakaky/rust-sdl2"
branch = "sensor-api"
optional = true
default-features = false
sdl2 = { version = "0.36", features = ["hidapi"], optional = true }

[package.metadata.vcpkg]
dependencies = ["sdl2"]
Expand Down
16 changes: 8 additions & 8 deletions mappings/overwatch
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@ ZR = e
ZL = SHIFT
R = LMOUSE
L = RMOUSE
#N = a
#W = r f
#E = none gyro_off
#S = SPACE
N = a
W = r f
E = none gyro_off
S = SPACE

N = X_Y
S = X_A
W = X_X
E = X_B
#N = X_Y
#S = X_A
#W = X_X
#E = X_B
17 changes: 8 additions & 9 deletions src/backend/sdl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,7 @@ use anyhow::{bail, Result};
use cgmath::{vec2, Vector3};
use hid_gamepad_types::{Acceleration, JoyKey, Motion, RotationSpeed};
use sdl2::{
self,
controller::{Axis, Button, GameController},
event::Event,
keyboard::Keycode,
sensor::SensorType,
GameControllerSubsystem, Sdl,
self, controller::{Axis, Button, GameController}, event::Event, keyboard::Keycode, sensor::SensorType, GameControllerSubsystem, Sdl
};

use crate::{
Expand All @@ -29,7 +24,6 @@ use super::Backend;
pub struct SDLBackend {
sdl: Sdl,
game_controller_system: GameControllerSubsystem,
mouse: Mouse,
}

impl SDLBackend {
Expand All @@ -51,7 +45,6 @@ impl SDLBackend {
Ok(Self {
sdl,
game_controller_system,
mouse: Mouse::new(),
})
}
}
Expand Down Expand Up @@ -144,7 +137,7 @@ impl Backend for SDLBackend {
settings.clone(),
bindings.clone(),
Calibration::empty(),
self.mouse.clone(),
Mouse::new()?,
)?;
controllers.insert(
controller.instance_id(),
Expand Down Expand Up @@ -278,5 +271,11 @@ fn sdl_to_sys(button: Button) -> JoyKey {
Button::DPadDown => JoyKey::Down,
Button::DPadLeft => JoyKey::Left,
Button::DPadRight => JoyKey::Right,
Button::Misc1 => todo!(),
Button::Paddle1 => todo!(),
Button::Paddle2 => todo!(),
Button::Paddle3 => todo!(),
Button::Paddle4 => todo!(),
Button::Touchpad => todo!(),
}
}
6 changes: 3 additions & 3 deletions src/config/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -464,7 +464,7 @@ fn keyboardkey(input: Input) -> IRes<'_, enigo::Key> {
use enigo::Key::*;
let char_parse = |input| {
satisfy(|c| is_alphanumeric(c as u8))(input)
.map(|(i, x)| (i, Layout(x)))
.map(|(i, x)| (i, Unicode(x)))
.map_err(|_: nom::Err<ErrorTree<Input<'_>>>| {
nom::Err::Error(ErrorTree::from_tag(input, "a keyboard letter"))
})
Expand Down Expand Up @@ -517,8 +517,8 @@ fn keyboardkey(input: Input) -> IRes<'_, enigo::Key> {
))(input)
}

fn mousekey(input: Input) -> IRes<'_, enigo::MouseButton> {
use enigo::MouseButton::*;
fn mousekey(input: Input) -> IRes<'_, enigo::Button> {
use enigo::Button::*;
let key_parse = |key, tag| value(key, tag_no_case(tag));
alt((
key_parse(Left, "LMouse"),
Expand Down
2 changes: 1 addition & 1 deletion src/config/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ pub struct JSMAction {
#[derive(Debug, Copy, Clone)]
pub enum ActionType {
Key(enigo::Key),
Mouse(enigo::MouseButton),
Mouse(enigo::Button),
Special(SpecialKey),
#[cfg(feature = "vgamepad")]
Gamepad(virtual_gamepad::Key),
Expand Down
14 changes: 7 additions & 7 deletions src/engine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use std::{
};

use cgmath::Vector2;
use enigo::{KeyboardControllable, MouseControllable};
use enigo::{Direction, Keyboard as _, Mouse as _};
use hid_gamepad_types::{Acceleration, Motion, RotationSpeed};

use crate::{
Expand Down Expand Up @@ -111,16 +111,16 @@ impl Engine {
ExtAction::GyroOn(ClickType::Click) | ExtAction::GyroOff(ClickType::Click) => {
eprintln!("Warning: event type Click has no effect on gyro on/off");
}
ExtAction::KeyPress(c, ClickType::Click) => self.mouse.enigo().key_click(c),
ExtAction::KeyPress(c, ClickType::Press) => self.mouse.enigo().key_down(c),
ExtAction::KeyPress(c, ClickType::Release) => self.mouse.enigo().key_up(c),
ExtAction::KeyPress(c, ClickType::Click) => self.mouse.enigo().key(c, Direction::Click)?,
ExtAction::KeyPress(c, ClickType::Press) => self.mouse.enigo().key(c, Direction::Press)?,
ExtAction::KeyPress(c, ClickType::Release) => self.mouse.enigo().key(c, Direction::Release)?,
ExtAction::KeyPress(_, ClickType::Toggle) => {
// TODO: Implement key press toggle
eprintln!("Warning: key press toggle is not implemented");
}
ExtAction::MousePress(c, ClickType::Click) => self.mouse.enigo().mouse_click(c),
ExtAction::MousePress(c, ClickType::Press) => self.mouse.enigo().mouse_down(c),
ExtAction::MousePress(c, ClickType::Release) => self.mouse.enigo().mouse_up(c),
ExtAction::MousePress(c, ClickType::Click) => self.mouse.enigo().button(c, Direction::Click)?,
ExtAction::MousePress(c, ClickType::Press) => self.mouse.enigo().button(c, Direction::Press)?,
ExtAction::MousePress(c, ClickType::Release) => self.mouse.enigo().button(c, Direction::Release)?,
ExtAction::MousePress(_, ClickType::Toggle) => {
// TODO: Implement mouse click toggle
eprintln!("Warning: mouse click toggle is not implemented");
Expand Down
4 changes: 2 additions & 2 deletions src/joystick.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
use std::time::{Duration, Instant};

use cgmath::{vec2, AbsDiffEq, Angle, Deg, ElementWise, InnerSpace, Rad, Vector2, Zero};
use enigo::MouseControllable;
use enigo::{Axis, Mouse as _};

use crate::{
config::{settings::Settings, types::RingMode},
Expand Down Expand Up @@ -392,7 +392,7 @@ impl Stick for ScrollStick {
let delta = (angle - *last).normalize_signed() / settings.stick.scroll.sens + *acc;
let delta_rounded = delta.round();
*acc = delta - delta_rounded;
mouse.enigo().mouse_scroll_y(delta_rounded as i32);
mouse.enigo().scroll(delta_rounded as i32, Axis::Vertical).unwrap();
*last = angle;
}
}
Expand Down
12 changes: 5 additions & 7 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use std::{fs::File, io::Read};

use anyhow::{bail, Context};
use backend::Backend;
use clap::Clap;
use clap::Parser as _;
use nom_supreme::{
error::{BaseErrorKind, ErrorTree},
final_parser::{ExtractContext, Location},
Expand All @@ -35,12 +35,10 @@ pub enum ClickType {

fn main() {
// https://github.com/rust-cli/human-panic/issues/77
human_panic::setup_panic!(human_panic::Metadata {
version: env!("CARGO_PKG_VERSION").into(),
name: env!("CARGO_PKG_NAME").into(),
authors: env!("CARGO_PKG_AUTHORS").replace(":", ", ").into(),
homepage: env!("CARGO_PKG_REPOSITORY").into(),
});
human_panic::setup_panic!(human_panic::Metadata::new(env!("CARGO_PKG_NAME"), env!("CARGO_PKG_VERSION"))
.authors(env!("CARGO_PKG_AUTHORS").replace(":", ", "))
.homepage(env!("CARGO_PKG_REPOSITORY"))
);

if let Err(e) = do_main() {
eprintln!("Error: {:?}", e);
Expand Down
59 changes: 21 additions & 38 deletions src/mapping.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
use enigo::{Key, MouseButton};
use enum_map::{Enum, EnumMap};
use enigo::{Key, Button};
use enum_map::{Enum, EnumArray, EnumMap};
use hid_gamepad_types::JoyKey;
use std::{
collections::HashMap,
fmt::{Debug, Display},
mem::MaybeUninit,
fmt::Display,
time::Duration,
};
use std::{convert::TryInto, time::Instant};
Expand All @@ -21,7 +20,7 @@ pub enum Action {
pub enum ExtAction {
None,
KeyPress(Key, ClickType),
MousePress(MouseButton, ClickType),
MousePress(Button, ClickType),
#[cfg(feature = "vgamepad")]
GamepadKeyPress(virtual_gamepad::Key, ClickType),
GyroOn(ClickType),
Expand Down Expand Up @@ -140,55 +139,39 @@ pub enum MapKey {

impl MapKey {
pub fn to_layer(self) -> u8 {
<Self as Enum<()>>::to_usize(self)
<Self as Enum>::into_usize(self)
.try_into()
.expect("error converting MapKey to u8")
}
}

const JOYKEY_SIZE: usize = <JoyKey as Enum<()>>::POSSIBLE_VALUES;
const VIRTKEY_SIZE: usize = <VirtualKey as Enum<()>>::POSSIBLE_VALUES;
const JOYKEY_SIZE: usize = <JoyKey as Enum>::LENGTH;
const VIRTKEY_SIZE: usize = <VirtualKey as Enum>::LENGTH;
const MAP_KEY_SIZE: usize = JOYKEY_SIZE + VIRTKEY_SIZE;

impl<V: Default + Sized> Enum<V> for MapKey {
type Array = [V; MAP_KEY_SIZE];

const POSSIBLE_VALUES: usize = MAP_KEY_SIZE;

fn slice(array: &Self::Array) -> &[V] {
array
}

fn slice_mut(array: &mut Self::Array) -> &mut [V] {
array
}
impl Enum for MapKey {
const LENGTH: usize = MAP_KEY_SIZE;

fn from_usize(value: usize) -> Self {
if value < JOYKEY_SIZE {
<JoyKey as Enum<()>>::from_usize(value).into()
<JoyKey as Enum>::from_usize(value).into()
} else if value < MAP_KEY_SIZE {
<VirtualKey as Enum<()>>::from_usize(value - JOYKEY_SIZE).into()
<VirtualKey as Enum>::from_usize(value - JOYKEY_SIZE).into()
} else {
unreachable!("MapKey value cannot be > MAP_KEY_SIZE");
}
}

fn to_usize(self) -> usize {
fn into_usize(self) -> usize {
match self {
MapKey::Physical(p) => <JoyKey as Enum<()>>::to_usize(p),
MapKey::Virtual(v) => <VirtualKey as Enum<()>>::to_usize(v) + JOYKEY_SIZE,
MapKey::Physical(p) => <JoyKey as Enum>::into_usize(p),
MapKey::Virtual(v) => <VirtualKey as Enum>::into_usize(v) + JOYKEY_SIZE,
}
}
}

fn from_function<F: FnMut(Self) -> V>(mut f: F) -> Self::Array {
unsafe {
let mut out = MaybeUninit::<[MaybeUninit<V>; MAP_KEY_SIZE]>::uninit().assume_init();
for (i, out) in out.iter_mut().enumerate() {
*out = MaybeUninit::new(f(<Self as Enum<V>>::from_usize(i)));
}
out.as_ptr().cast::<Self::Array>().read()
}
}
impl<T> EnumArray<T> for MapKey{
type Array = [T; MAP_KEY_SIZE];
}

impl From<JoyKey> for MapKey {
Expand Down Expand Up @@ -218,8 +201,8 @@ pub struct Buttons {
impl Buttons {
pub fn new() -> Self {
Buttons {
bindings: EnumMap::new(),
state: EnumMap::new(),
bindings: EnumMap::default(),
state: EnumMap::default(),
current_layers: vec![0],
ext_actions: Vec::new(),
hold_delay: Duration::from_millis(100),
Expand All @@ -236,8 +219,8 @@ impl Buttons {
}

pub fn tick(&mut self, now: Instant) -> impl Iterator<Item = ExtAction> + '_ {
for key in (0..<MapKey as Enum<KeyStatus>>::POSSIBLE_VALUES)
.map(<MapKey as Enum<KeyStatus>>::from_usize)
for key in (0..<MapKey as Enum>::LENGTH)
.map(<MapKey as Enum>::from_usize)
{
let binding = self.find_binding(key);
match self.state[key].status {
Expand Down
19 changes: 7 additions & 12 deletions src/mouse.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::ops::AddAssign;

use cgmath::{vec2, Deg, Vector2, Zero};
use enigo::{Enigo, MouseControllable};
use enigo::{Coordinate, Enigo, Mouse as _};

use crate::config::settings::MouseSettings;

Expand Down Expand Up @@ -42,23 +42,18 @@ pub struct Mouse {
error_accumulator: Vector2<f64>,
}

impl Clone for Mouse {
fn clone(&self) -> Self {
Self { ..Self::new() }
}
}

impl Mouse {
pub fn new() -> Self {
pub fn new() -> anyhow::Result<Self> {
#[allow(unused_mut)]
let mut enigo = Enigo::new();
let mut enigo = Enigo::new(&enigo::Settings::default())?;
// Lower delay for xdo, see #1
#[cfg(target_os = "linux")]
enigo.set_delay(100);
Mouse {
Ok(Mouse {
enigo,
error_accumulator: Vector2::zero(),
}
})
}

// mouse movement is pixel perfect, so we keep track of the error.
Expand All @@ -75,13 +70,13 @@ impl Mouse {
if let Some(rounded) = rounded.cast::<i32>() {
if rounded != Vector2::zero() {
// In enigo, +y is toward the bottom
self.enigo.mouse_move_relative(rounded.x, rounded.y);
self.enigo.move_mouse(rounded.x, rounded.y, Coordinate::Rel).unwrap();
}
}
}

pub fn mouse_move_absolute_pixel(&mut self, offset: Vector2<i32>) {
self.enigo.mouse_move_to(offset.x, offset.y);
self.enigo.move_mouse(offset.x, offset.y, Coordinate::Abs).unwrap();
}

pub fn enigo(&mut self) -> &mut Enigo {
Expand Down
Loading

0 comments on commit 7126c31

Please sign in to comment.