Skip to content

Commit

Permalink
all: Increase Rust edition to 2024
Browse files Browse the repository at this point in the history
  • Loading branch information
pentamassiv committed Feb 26, 2025
1 parent f47bf6e commit 8a1fb6b
Show file tree
Hide file tree
Showing 15 changed files with 126 additions and 102 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ authors = [
"pentamassiv <[email protected]>",
"Dustin Bensing <[email protected]>",
]
edition = "2021"
edition = "2024"
rust-version = "1.85"
description = "Cross-platform (Linux, Windows, macOS & BSD) library to simulate keyboard and mouse events"
documentation = "https://docs.rs/enigo/"
Expand Down
2 changes: 1 addition & 1 deletion examples/serde.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use enigo::{
agent::{Agent, Token},
Button, Enigo, Key, Settings,
agent::{Agent, Token},
};
use std::{thread, time::Duration};

Expand Down
35 changes: 18 additions & 17 deletions src/keycodes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -739,7 +739,7 @@ impl TryFrom<Key> for windows::Win32::UI::Input::KeyboardAndMouse::VIRTUAL_KEY {
#[allow(clippy::too_many_lines)]
fn try_from(key: Key) -> Result<Self, Self::Error> {
use windows::Win32::UI::Input::KeyboardAndMouse::{
VK__none_, VIRTUAL_KEY, VK_0, VK_1, VK_2, VK_3, VK_4, VK_5, VK_6, VK_7, VK_8, VK_9,
VIRTUAL_KEY, VK__none_, VK_0, VK_1, VK_2, VK_3, VK_4, VK_5, VK_6, VK_7, VK_8, VK_9,
VK_A, VK_ABNT_C1, VK_ABNT_C2, VK_ACCEPT, VK_ADD, VK_APPS, VK_ATTN, VK_B, VK_BACK,
VK_BROWSER_BACK, VK_BROWSER_FAVORITES, VK_BROWSER_FORWARD, VK_BROWSER_HOME,
VK_BROWSER_REFRESH, VK_BROWSER_SEARCH, VK_BROWSER_STOP, VK_C, VK_CANCEL, VK_CAPITAL,
Expand All @@ -748,9 +748,9 @@ impl TryFrom<Key> for windows::Win32::UI::Input::KeyboardAndMouse::VIRTUAL_KEY {
VK_DBE_ENTERDLGCONVERSIONMODE, VK_DBE_ENTERIMECONFIGMODE, VK_DBE_ENTERWORDREGISTERMODE,
VK_DBE_FLUSHSTRING, VK_DBE_HIRAGANA, VK_DBE_KATAKANA, VK_DBE_NOCODEINPUT,
VK_DBE_NOROMAN, VK_DBE_ROMAN, VK_DBE_SBCSCHAR, VK_DECIMAL, VK_DELETE, VK_DIVIDE,
VK_DOWN, VK_E, VK_END, VK_EREOF, VK_ESCAPE, VK_EXECUTE, VK_EXSEL, VK_F, VK_F1, VK_F10,
VK_F11, VK_F12, VK_F13, VK_F14, VK_F15, VK_F16, VK_F17, VK_F18, VK_F19, VK_F2, VK_F20,
VK_F21, VK_F22, VK_F23, VK_F24, VK_F3, VK_F4, VK_F5, VK_F6, VK_F7, VK_F8, VK_F9,
VK_DOWN, VK_E, VK_END, VK_EREOF, VK_ESCAPE, VK_EXECUTE, VK_EXSEL, VK_F, VK_F1, VK_F2,
VK_F3, VK_F4, VK_F5, VK_F6, VK_F7, VK_F8, VK_F9, VK_F10, VK_F11, VK_F12, VK_F13,
VK_F14, VK_F15, VK_F16, VK_F17, VK_F18, VK_F19, VK_F20, VK_F21, VK_F22, VK_F23, VK_F24,
VK_FINAL, VK_G, VK_GAMEPAD_A, VK_GAMEPAD_B, VK_GAMEPAD_DPAD_DOWN, VK_GAMEPAD_DPAD_LEFT,
VK_GAMEPAD_DPAD_RIGHT, VK_GAMEPAD_DPAD_UP, VK_GAMEPAD_LEFT_SHOULDER,
VK_GAMEPAD_LEFT_THUMBSTICK_BUTTON, VK_GAMEPAD_LEFT_THUMBSTICK_DOWN,
Expand All @@ -769,17 +769,18 @@ impl TryFrom<Key> for windows::Win32::UI::Input::KeyboardAndMouse::VIRTUAL_KEY {
VK_NAVIGATION_DOWN, VK_NAVIGATION_LEFT, VK_NAVIGATION_MENU, VK_NAVIGATION_RIGHT,
VK_NAVIGATION_UP, VK_NAVIGATION_VIEW, VK_NEXT, VK_NONAME, VK_NONCONVERT, VK_NUMLOCK,
VK_NUMPAD0, VK_NUMPAD1, VK_NUMPAD2, VK_NUMPAD3, VK_NUMPAD4, VK_NUMPAD5, VK_NUMPAD6,
VK_NUMPAD7, VK_NUMPAD8, VK_NUMPAD9, VK_O, VK_OEM_1, VK_OEM_102, VK_OEM_2, VK_OEM_3,
VK_OEM_4, VK_OEM_5, VK_OEM_6, VK_OEM_7, VK_OEM_8, VK_OEM_ATTN, VK_OEM_AUTO, VK_OEM_AX,
VK_OEM_BACKTAB, VK_OEM_CLEAR, VK_OEM_COMMA, VK_OEM_COPY, VK_OEM_CUSEL, VK_OEM_ENLW,
VK_OEM_FINISH, VK_OEM_FJ_JISHO, VK_OEM_FJ_LOYA, VK_OEM_FJ_MASSHOU, VK_OEM_FJ_ROYA,
VK_OEM_FJ_TOUROKU, VK_OEM_JUMP, VK_OEM_MINUS, VK_OEM_NEC_EQUAL, VK_OEM_PA1, VK_OEM_PA2,
VK_OEM_PA3, VK_OEM_PERIOD, VK_OEM_PLUS, VK_OEM_RESET, VK_OEM_WSCTRL, VK_P, VK_PA1,
VK_PACKET, VK_PAUSE, VK_PLAY, VK_PRINT, VK_PRIOR, VK_PROCESSKEY, VK_Q, VK_R,
VK_RBUTTON, VK_RCONTROL, VK_RETURN, VK_RIGHT, VK_RMENU, VK_RSHIFT, VK_RWIN, VK_S,
VK_SCROLL, VK_SELECT, VK_SEPARATOR, VK_SHIFT, VK_SLEEP, VK_SNAPSHOT, VK_SPACE,
VK_SUBTRACT, VK_T, VK_TAB, VK_U, VK_UP, VK_V, VK_VOLUME_DOWN, VK_VOLUME_MUTE,
VK_VOLUME_UP, VK_W, VK_X, VK_XBUTTON1, VK_XBUTTON2, VK_Y, VK_Z, VK_ZOOM,
VK_NUMPAD7, VK_NUMPAD8, VK_NUMPAD9, VK_O, VK_OEM_1, VK_OEM_2, VK_OEM_3, VK_OEM_4,
VK_OEM_5, VK_OEM_6, VK_OEM_7, VK_OEM_8, VK_OEM_102, VK_OEM_ATTN, VK_OEM_AUTO,
VK_OEM_AX, VK_OEM_BACKTAB, VK_OEM_CLEAR, VK_OEM_COMMA, VK_OEM_COPY, VK_OEM_CUSEL,
VK_OEM_ENLW, VK_OEM_FINISH, VK_OEM_FJ_JISHO, VK_OEM_FJ_LOYA, VK_OEM_FJ_MASSHOU,
VK_OEM_FJ_ROYA, VK_OEM_FJ_TOUROKU, VK_OEM_JUMP, VK_OEM_MINUS, VK_OEM_NEC_EQUAL,
VK_OEM_PA1, VK_OEM_PA2, VK_OEM_PA3, VK_OEM_PERIOD, VK_OEM_PLUS, VK_OEM_RESET,
VK_OEM_WSCTRL, VK_P, VK_PA1, VK_PACKET, VK_PAUSE, VK_PLAY, VK_PRINT, VK_PRIOR,
VK_PROCESSKEY, VK_Q, VK_R, VK_RBUTTON, VK_RCONTROL, VK_RETURN, VK_RIGHT, VK_RMENU,
VK_RSHIFT, VK_RWIN, VK_S, VK_SCROLL, VK_SELECT, VK_SEPARATOR, VK_SHIFT, VK_SLEEP,
VK_SNAPSHOT, VK_SPACE, VK_SUBTRACT, VK_T, VK_TAB, VK_U, VK_UP, VK_V, VK_VOLUME_DOWN,
VK_VOLUME_MUTE, VK_VOLUME_UP, VK_W, VK_X, VK_XBUTTON1, VK_XBUTTON2, VK_Y, VK_Z,
VK_ZOOM,
};

trace!("Key::try_from(key: {key:?})");
Expand Down Expand Up @@ -1036,7 +1037,7 @@ impl TryFrom<Key> for windows::Win32::UI::Input::KeyboardAndMouse::VIRTUAL_KEY {
'\n' => break 'unicode_handling VK_RETURN,

'\r' => { // TODO: What is the correct key to type here?
// break 'unicode_handling VK_,
// break 'unicode_handling VK_,
}
'\t' => break 'unicode_handling VK_TAB,
'\0' => {
Expand All @@ -1057,7 +1058,7 @@ impl TryFrom<Key> for windows::Win32::UI::Input::KeyboardAndMouse::VIRTUAL_KEY {
// virtual-key code and the high-order byte contains the shift state, which can
// be a combination of the following flag bits. If the function finds no key
// that translates to the passed character code, both the low-order and
// high-order bytes contain 1
// high-order bytes contain -1
let vk = unsafe {
windows::Win32::UI::Input::KeyboardAndMouse::VkKeyScanExW(
utf16_surrogates[0],
Expand Down
4 changes: 2 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,10 +73,10 @@ pub mod agent;
mod platform;
pub use platform::Enigo;

#[cfg(target_os = "windows")]
pub use platform::set_dpi_awareness;
#[cfg(target_os = "windows")]
pub use platform::EXT;
#[cfg(target_os = "windows")]
pub use platform::set_dpi_awareness;

mod keycodes;
/// Contains the available keycodes
Expand Down
19 changes: 9 additions & 10 deletions src/linux/keymap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,14 @@ pub struct KeyMap<Keycode> {

// TODO: Check if the bounds can be simplified
impl<
Keycode: std::ops::Sub
+ PartialEq
+ Copy
+ Clone
+ Display
+ TryInto<usize>
+ std::convert::TryFrom<usize>,
> KeyMap<Keycode>
Keycode: std::ops::Sub
+ PartialEq
+ Copy
+ Clone
+ Display
+ TryInto<usize>
+ std::convert::TryFrom<usize>,
> KeyMap<Keycode>
where
<Keycode as TryInto<usize>>::Error: std::fmt::Debug,
<Keycode as TryFrom<usize>>::Error: std::fmt::Debug,
Expand Down Expand Up @@ -173,8 +173,7 @@ where
Some(unused_keycode) => {
trace!(
"trying to map keycode {} to keysym {:?}",
unused_keycode,
keysym
unused_keycode, keysym
);
if c.bind_key(unused_keycode, keysym).is_err() {
return Err(InputError::Mapping(format!("{keysym:?}")));
Expand Down
34 changes: 20 additions & 14 deletions src/linux/libei.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use ashpd::desktop::remote_desktop::RemoteDesktop;
use log::{debug, error, trace, warn};
use reis::{
PendingRequestResult,
ei::{self, Connection},
handshake::HandshakeResp,
PendingRequestResult,
};
use std::{collections::HashMap, os::unix::net::UnixStream, time::Instant};
use xkbcommon::xkb;
Expand Down Expand Up @@ -182,7 +182,7 @@ impl Con {
con.update(libei_name)
.map_err(|_| NewConError::EstablishCon("unable to update the libei connection"))?;

for (device, device_data) in con.devices.iter_mut().filter(|(_, ref device_data)| {
for (device, device_data) in con.devices.iter_mut().filter(|(_, device_data)| {
device_data.device_type == Some(reis::ei::device::DeviceType::Virtual)
&& device_data.state == DeviceState::Resumed
// TODO: Should all devices start emulating?
Expand Down Expand Up @@ -281,7 +281,9 @@ impl Con {
invalid_id,
} => {
// TODO: Try to recover?
error!("the serial {last_serial} contained an invalid object with the id {invalid_id}");
error!(
"the serial {last_serial} contained an invalid object with the id {invalid_id}"
);
}
ei::connection::Event::Ping { ping } => {
debug!("ping");
Expand Down Expand Up @@ -434,11 +436,11 @@ impl Con {
latched,
group,
} => { // TODO: Handle updated modifiers
// Notification that the EIS
// implementation has changed modifier states
// on this device. Future ei_keyboard.key
// requests must take the new modifier state
// into account.
// Notification that the EIS
// implementation has changed modifier states
// on this device. Future ei_keyboard.key
// requests must take the new modifier state
// into account.
}
_ => {}
}
Expand Down Expand Up @@ -484,7 +486,7 @@ impl Keyboard for Con {
if let Some((device, device_data)) = self
.devices
.iter_mut()
.find(|(_, ref device_data)| device_data.interface::<ei::Keyboard>().is_some())
.find(|(_, device_data)| device_data.interface::<ei::Keyboard>().is_some())
{
if let Some((keyboard, keymap)) = self.keyboards.iter().next() {
let keycode = key_to_keycode(keymap, key)?;
Expand Down Expand Up @@ -514,7 +516,7 @@ impl Keyboard for Con {
if let Some((device, device_data)) = self
.devices
.iter_mut()
.find(|(_, ref device_data)| device_data.interface::<ei::Keyboard>().is_some())
.find(|(_, device_data)| device_data.interface::<ei::Keyboard>().is_some())
{
let keyboard = device_data.interface::<ei::Keyboard>().unwrap();

Expand Down Expand Up @@ -542,7 +544,7 @@ impl Mouse for Con {
if let Some((device, device_data)) = self
.devices
.iter_mut()
.find(|(_, ref device_data)| device_data.interface::<ei::Button>().is_some())
.find(|(_, device_data)| device_data.interface::<ei::Button>().is_some())
{
// Do nothing if one of the mouse scroll buttons was released
// Releasing one of the scroll mouse buttons has no effect
Expand Down Expand Up @@ -691,13 +693,17 @@ impl Mouse for Con {

fn main_display(&self) -> InputResult<(i32, i32)> {
// TODO Implement this
error!("You tried to get the dimensions of the main display. I don't know how this is possible under Wayland. Let me know if there is a new protocol");
error!(
"You tried to get the dimensions of the main display. I don't know how this is possible under Wayland. Let me know if there is a new protocol"
);
Err(InputError::Simulate("Not implemented yet"))
}

fn location(&self) -> InputResult<(i32, i32)> {
// TODO Implement this
error!("You tried to get the mouse location. I don't know how this is possible under Wayland. Let me know if there is a new protocol");
error!(
"You tried to get the mouse location. I don't know how this is possible under Wayland. Let me know if there is a new protocol"
);
Err(InputError::Simulate("Not implemented yet"))
}
}
Expand All @@ -716,7 +722,7 @@ impl Drop for Con {
self.connection.disconnect(); // Let the server know we voluntarily disconnected

let _ = self.context.flush(); // Ignore the errors if the connection was
// dropped
// dropped
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/linux/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use crate::{
feature = "libei"
)))]
compile_error!(
"either feature `wayland`, `x11rb`, `xdo` or `libei` must be enabled for this crate when using linux"
"either feature `wayland`, `x11rb`, `xdo` or `libei` must be enabled for this crate when using linux"
);

#[cfg(feature = "libei")]
Expand Down
18 changes: 10 additions & 8 deletions src/linux/wayland.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@ use std::{

use log::{debug, error, trace, warn};
use wayland_client::{
Connection, Dispatch, EventQueue, QueueHandle,
protocol::{
wl_keyboard::{self, WlKeyboard},
wl_pointer::{self, WlPointer},
wl_registry,
wl_seat::{self, Capability},
},
Connection, Dispatch, EventQueue, QueueHandle,
};
use wayland_protocols_misc::{
zwp_input_method_v2::client::{zwp_input_method_manager_v2, zwp_input_method_v2},
Expand All @@ -28,8 +28,8 @@ use wayland_protocols_wlr::virtual_pointer::v1::client::{

use super::keymap::{Bind, KeyMap};
use crate::{
keycodes::Modifier, keycodes::ModifierBitflag, Axis, Button, Coordinate, Direction, InputError,
InputResult, Key, Keyboard, Mouse, NewConError,
Axis, Button, Coordinate, Direction, InputError, InputResult, Key, Keyboard, Mouse,
NewConError, keycodes::Modifier, keycodes::ModifierBitflag,
};

pub type Keycode = u32;
Expand Down Expand Up @@ -468,9 +468,7 @@ impl Dispatch<wl_registry::WlRegistry, ()> for WaylandState {
{
trace!(
"Global announced: {} (name: {}, version: {})",
interface,
name,
version
interface, name, version
);
state.globals.insert(interface, (name, version));
}
Expand Down Expand Up @@ -836,13 +834,17 @@ impl Mouse for Con {

fn main_display(&self) -> InputResult<(i32, i32)> {
// TODO Implement this
error!("You tried to get the dimensions of the main display. I don't know how this is possible under Wayland. Let me know if there is a new protocol");
error!(
"You tried to get the dimensions of the main display. I don't know how this is possible under Wayland. Let me know if there is a new protocol"
);
Err(InputError::Simulate("Not implemented yet"))
}

fn location(&self) -> InputResult<(i32, i32)> {
// TODO Implement this
error!("You tried to get the mouse location. I don't know how this is possible under Wayland. Let me know if there is a new protocol");
error!(
"You tried to get the mouse location. I don't know how this is possible under Wayland. Let me know if there is a new protocol"
);
Err(InputError::Simulate("Not implemented yet"))
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/linux/x11rb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ use x11rb::{

use super::keymap::{Bind, KeyMap, Keysym};
use crate::{
keycodes::Modifier, Axis, Button, Coordinate, Direction, InputError, InputResult, Key,
Keyboard, Mouse, NewConError,
Axis, Button, Coordinate, Direction, InputError, InputResult, Key, Keyboard, Mouse,
NewConError, keycodes::Modifier,
};

type CompositorConnection = RustConnection<DefaultStream>;
Expand Down
4 changes: 2 additions & 2 deletions src/linux/xdo.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use std::{
ffi::{c_char, c_int, c_ulong, c_void, CString},
ffi::{CString, c_char, c_int, c_ulong, c_void},
ptr,
};

Expand All @@ -19,7 +19,7 @@ type Window = c_ulong;
type Xdo = *const c_void;

#[link(name = "xdo")]
extern "C" {
unsafe extern "C" {
fn xdo_free(xdo: Xdo);
fn xdo_new(display: *const c_char) -> Xdo;

Expand Down
Loading

0 comments on commit 8a1fb6b

Please sign in to comment.