From 18515319a8359d0fba282f85fa259c816ac5c9f4 Mon Sep 17 00:00:00 2001 From: Federico Terzi Date: Mon, 8 Feb 2021 21:23:28 +0100 Subject: [PATCH] Format code --- espanso-detect/src/evdev/context.rs | 12 +++++----- espanso-detect/src/evdev/device.rs | 17 ++++++-------- espanso-detect/src/evdev/keymap.rs | 23 ++++++++++++------- espanso-detect/src/evdev/mod.rs | 35 +++++++++++++++++++---------- espanso-detect/src/lib.rs | 2 +- espanso-detect/src/win32/mod.rs | 2 +- espanso-detect/src/x11/mod.rs | 10 ++++----- espanso-ui/src/lib.rs | 2 +- espanso-ui/src/mac/mod.rs | 35 +++++++++-------------------- espanso-ui/src/mac/native.mm | 8 +++---- espanso/src/main.rs | 6 ++--- 11 files changed, 73 insertions(+), 79 deletions(-) diff --git a/espanso-detect/src/evdev/context.rs b/espanso-detect/src/evdev/context.rs index 2fa57c0..bcda581 100644 --- a/espanso-detect/src/evdev/context.rs +++ b/espanso-detect/src/evdev/context.rs @@ -3,9 +3,9 @@ use scopeguard::ScopeGuard; -use super::ffi::{XKB_CONTEXT_NO_FLAGS, xkb_context, xkb_context_new, xkb_context_unref}; -use thiserror::Error; +use super::ffi::{xkb_context, xkb_context_new, xkb_context_unref, XKB_CONTEXT_NO_FLAGS}; use anyhow::Result; +use thiserror::Error; pub struct Context { context: *mut xkb_context, @@ -14,10 +14,8 @@ pub struct Context { impl Context { pub fn new() -> Result { let raw_context = unsafe { xkb_context_new(XKB_CONTEXT_NO_FLAGS) }; - let context = scopeguard::guard(raw_context, |raw_context| { - unsafe { - xkb_context_unref(raw_context); - } + let context = scopeguard::guard(raw_context, |raw_context| unsafe { + xkb_context_unref(raw_context); }); if raw_context.is_null() { @@ -46,4 +44,4 @@ impl Drop for Context { pub enum ContextError { #[error("could not create xkb context")] FailedCreation(), -} \ No newline at end of file +} diff --git a/espanso-detect/src/evdev/device.rs b/espanso-detect/src/evdev/device.rs index 4f3ba6b..ec87dfa 100644 --- a/espanso-detect/src/evdev/device.rs +++ b/espanso-detect/src/evdev/device.rs @@ -3,7 +3,7 @@ use anyhow::Result; use libc::{input_event, size_t, ssize_t, EWOULDBLOCK, O_CLOEXEC, O_NONBLOCK, O_RDONLY}; -use log::{trace}; +use log::trace; use scopeguard::ScopeGuard; use std::os::unix::io::AsRawFd; use std::{ @@ -15,10 +15,9 @@ use thiserror::Error; use super::{ ffi::{ - is_keyboard, xkb_key_direction, xkb_keycode_t, xkb_keymap_key_repeats, - xkb_state, xkb_state_get_keymap, xkb_state_key_get_one_sym, - xkb_state_key_get_utf8, xkb_state_new, xkb_state_unref, - xkb_state_update_key, EV_KEY, + is_keyboard, xkb_key_direction, xkb_keycode_t, xkb_keymap_key_repeats, xkb_state, + xkb_state_get_keymap, xkb_state_key_get_one_sym, xkb_state_key_get_utf8, xkb_state_new, + xkb_state_unref, xkb_state_update_key, EV_KEY, }, keymap::Keymap, }; @@ -66,10 +65,8 @@ impl Device { let raw_state = unsafe { xkb_state_new(keymap.get_handle()) }; // Automatically close the state if the function does not return correctly - let state = scopeguard::guard(raw_state, |raw_state| { - unsafe { - xkb_state_unref(raw_state); - } + let state = scopeguard::guard(raw_state, |raw_state| unsafe { + xkb_state_unref(raw_state); }); if raw_state.is_null() { @@ -115,7 +112,7 @@ impl Device { } let nevs: size_t = len as usize / std::mem::size_of::(); - + #[allow(clippy::needless_range_loop)] for i in 0..nevs { let event = self.process_event(evs[i].type_, evs[i].code, evs[i].value); diff --git a/espanso-detect/src/evdev/keymap.rs b/espanso-detect/src/evdev/keymap.rs index d751e3f..ac68fcf 100644 --- a/espanso-detect/src/evdev/keymap.rs +++ b/espanso-detect/src/evdev/keymap.rs @@ -3,10 +3,13 @@ use scopeguard::ScopeGuard; -use thiserror::Error; use anyhow::Result; +use thiserror::Error; -use super::{context::Context, ffi::{XKB_KEYMAP_COMPILE_NO_FLAGS, xkb_keymap, xkb_keymap_new_from_names, xkb_keymap_unref}}; +use super::{ + context::Context, + ffi::{xkb_keymap, xkb_keymap_new_from_names, xkb_keymap_unref, XKB_KEYMAP_COMPILE_NO_FLAGS}, +}; pub struct Keymap { keymap: *mut xkb_keymap, @@ -14,11 +17,15 @@ pub struct Keymap { impl Keymap { pub fn new(context: &Context) -> Result { - let raw_keymap = unsafe { xkb_keymap_new_from_names(context.get_handle(), std::ptr::null(), XKB_KEYMAP_COMPILE_NO_FLAGS) }; - let keymap = scopeguard::guard(raw_keymap, |raw_keymap| { - unsafe { - xkb_keymap_unref(raw_keymap); - } + let raw_keymap = unsafe { + xkb_keymap_new_from_names( + context.get_handle(), + std::ptr::null(), + XKB_KEYMAP_COMPILE_NO_FLAGS, + ) + }; + let keymap = scopeguard::guard(raw_keymap, |raw_keymap| unsafe { + xkb_keymap_unref(raw_keymap); }); if raw_keymap.is_null() { @@ -47,4 +54,4 @@ impl Drop for Keymap { pub enum KeymapError { #[error("could not create xkb keymap")] FailedCreation(), -} \ No newline at end of file +} diff --git a/espanso-detect/src/evdev/mod.rs b/espanso-detect/src/evdev/mod.rs index 37f3cde..7b2dc49 100644 --- a/espanso-detect/src/evdev/mod.rs +++ b/espanso-detect/src/evdev/mod.rs @@ -25,6 +25,7 @@ mod device; mod ffi; mod keymap; +use anyhow::Result; use context::Context; use device::{get_devices, Device}; use keymap::Keymap; @@ -32,7 +33,6 @@ use libc::{ __errno_location, close, epoll_ctl, epoll_event, epoll_wait, EINTR, EPOLLIN, EPOLL_CTL_ADD, }; use log::{error, trace}; -use anyhow::Result; use thiserror::Error; use crate::event::Status::*; @@ -73,10 +73,10 @@ impl EVDEVSource { error!( "You can either add the current user to the 'input' group or run espanso as root" ); - return Err(EVDEVSourceError::PermissionDenied().into()) + return Err(EVDEVSourceError::PermissionDenied().into()); } } - return Err(error) + return Err(error); } } @@ -165,7 +165,11 @@ impl From for Option { Some(keyboard_event.value) }; - let status = if keyboard_event.is_down { Pressed } else { Released }; + let status = if keyboard_event.is_down { + Pressed + } else { + Released + }; return Some(InputEvent::Keyboard(KeyboardEvent { key, @@ -177,7 +181,11 @@ impl From for Option { RawInputEvent::Mouse(mouse_event) => { let button = raw_to_mouse_button(mouse_event.code); - let status = if mouse_event.is_down { Pressed } else { Released }; + let status = if mouse_event.is_down { + Pressed + } else { + Released + }; if let Some(button) = button { return Some(InputEvent::Mouse(MouseEvent { button, status })); @@ -253,10 +261,10 @@ fn key_sym_to_key(key_sym: i32) -> (Key, Option) { fn raw_to_mouse_button(raw: u16) -> Option { match raw { BTN_LEFT => Some(MouseButton::Left), - BTN_RIGHT=> Some(MouseButton::Right), - BTN_MIDDLE=> Some(MouseButton::Middle), - BTN_SIDE=> Some(MouseButton::Button1), - BTN_EXTRA=> Some(MouseButton::Button2), + BTN_RIGHT => Some(MouseButton::Right), + BTN_MIDDLE => Some(MouseButton::Middle), + BTN_SIDE => Some(MouseButton::Button1), + BTN_EXTRA => Some(MouseButton::Button2), _ => None, } } @@ -265,9 +273,12 @@ fn raw_to_mouse_button(raw: u16) -> Option { mod tests { use device::RawMouseEvent; - use crate::event::{InputEvent, KeyboardEvent, Key::Other}; + use crate::event::{InputEvent, Key::Other, KeyboardEvent}; - use super::{*, device::{RawInputEvent, RawKeyboardEvent}}; + use super::{ + device::{RawInputEvent, RawKeyboardEvent}, + *, + }; #[test] fn raw_to_input_event_keyboard_works_correctly() { @@ -305,4 +316,4 @@ mod tests { }) ); } -} \ No newline at end of file +} diff --git a/espanso-detect/src/lib.rs b/espanso-detect/src/lib.rs index 384a43b..6c94591 100644 --- a/espanso-detect/src/lib.rs +++ b/espanso-detect/src/lib.rs @@ -26,4 +26,4 @@ pub mod win32; pub mod x11; #[cfg(target_os = "linux")] -pub mod evdev; \ No newline at end of file +pub mod evdev; diff --git a/espanso-detect/src/win32/mod.rs b/espanso-detect/src/win32/mod.rs index 87475a7..878e9a9 100644 --- a/espanso-detect/src/win32/mod.rs +++ b/espanso-detect/src/win32/mod.rs @@ -100,7 +100,7 @@ impl Win32Source { -2 => Win32SourceError::RawInputFailed(), _ => Win32SourceError::Unknown(), }; - return Err(error.into()) + return Err(error.into()); } self.handle = handle; diff --git a/espanso-detect/src/x11/mod.rs b/espanso-detect/src/x11/mod.rs index a3d15be..01c9704 100644 --- a/espanso-detect/src/x11/mod.rs +++ b/espanso-detect/src/x11/mod.rs @@ -17,9 +17,7 @@ * along with espanso. If not, see . */ -use std::{ - ffi::{c_void, CStr}, -}; +use std::ffi::{c_void, CStr}; use lazycell::LazyCell; use log::{error, trace, warn}; @@ -105,7 +103,7 @@ impl X11Source { -6 => X11SourceError::FailedRegistration("cannot enable XRecord context".to_owned()), _ => X11SourceError::Unknown(), }; - return Err(error.into()) + return Err(error.into()); } self.handle = handle; @@ -307,7 +305,7 @@ fn raw_to_mouse_button(raw: i32) -> Option { mod tests { use std::ffi::CString; - use super::*; + use super::*; fn default_raw_input_event() -> RawInputEvent { RawInputEvent { @@ -380,4 +378,4 @@ mod tests { let result: Option = raw.into(); assert!(result.is_none()); } -} \ No newline at end of file +} diff --git a/espanso-ui/src/lib.rs b/espanso-ui/src/lib.rs index 914c536..a960074 100644 --- a/espanso-ui/src/lib.rs +++ b/espanso-ui/src/lib.rs @@ -9,4 +9,4 @@ pub mod win32; pub mod linux; #[cfg(target_os = "macos")] -pub mod mac; \ No newline at end of file +pub mod mac; diff --git a/espanso-ui/src/mac/mod.rs b/espanso-ui/src/mac/mod.rs index 5142921..53d0254 100644 --- a/espanso-ui/src/mac/mod.rs +++ b/espanso-ui/src/mac/mod.rs @@ -17,7 +17,7 @@ * along with espanso. If not, see . */ -use std::{cmp::min, collections::HashMap, ffi::{CString}, os::raw::c_char, thread::ThreadId}; +use std::{cmp::min, collections::HashMap, ffi::CString, os::raw::c_char, thread::ThreadId}; use lazycell::LazyCell; use log::{error, trace}; @@ -49,10 +49,7 @@ pub struct RawUIEvent { #[allow(improper_ctypes)] #[link(name = "espansoui", kind = "static")] extern "C" { - pub fn ui_initialize( - _self: *const MacEventLoop, - options: RawUIOptions, - ); + pub fn ui_initialize(_self: *const MacEventLoop, options: RawUIOptions); pub fn ui_eventloop( event_callback: extern "C" fn(_self: *mut MacEventLoop, event: RawUIEvent), ) -> i32; @@ -80,10 +77,7 @@ pub fn create(options: MacUIOptions) -> (MacRemote, MacEventLoop) { icons.push(path.clone()); } - let eventloop = MacEventLoop::new( - icons, - options.show_icon, - ); + let eventloop = MacEventLoop::new(icons, options.show_icon); let remote = MacRemote::new(icon_indexes); (remote, eventloop) @@ -101,10 +95,7 @@ pub struct MacEventLoop { } impl MacEventLoop { - pub(crate) fn new( - icons: Vec, - show_icon: bool, - ) -> Self { + pub(crate) fn new(icons: Vec, show_icon: bool) -> Self { Self { icons, show_icon, @@ -118,7 +109,8 @@ impl MacEventLoop { let mut icon_paths: [[u8; MAX_FILE_PATH]; MAX_ICON_COUNT] = [[0; MAX_FILE_PATH]; MAX_ICON_COUNT]; for (i, icon_path) in icon_paths.iter_mut().enumerate().take(self.icons.len()) { - let c_path = CString::new(self.icons[i].clone()).expect("unable to create CString for UI tray icon path"); + let c_path = CString::new(self.icons[i].clone()) + .expect("unable to create CString for UI tray icon path"); let len = min(c_path.as_bytes().len(), MAX_FILE_PATH - 1); icon_path[0..len].clone_from_slice(&c_path.as_bytes()[..len]); } @@ -175,12 +167,8 @@ pub struct MacRemote { } impl MacRemote { - pub(crate) fn new( - icon_indexes: HashMap, - ) -> Self { - Self { - icon_indexes, - } + pub(crate) fn new(icon_indexes: HashMap) -> Self { + Self { icon_indexes } } pub fn update_tray_icon(&self, icon: TrayIcon) { @@ -196,10 +184,7 @@ impl MacRemote { match c_string { Ok(message) => unsafe { ui_show_notification(message.as_ptr(), 3.0) }, Err(error) => { - error!( - "Unable to show notification {}", - error - ); + error!("Unable to show notification {}", error); } } } @@ -238,4 +223,4 @@ impl From for Option { None } -} \ No newline at end of file +} diff --git a/espanso-ui/src/mac/native.mm b/espanso-ui/src/mac/native.mm index 9ed2692..31ea82d 100644 --- a/espanso-ui/src/mac/native.mm +++ b/espanso-ui/src/mac/native.mm @@ -37,7 +37,7 @@ void ui_initialize(void *_self, UIOptions _options) int32_t ui_eventloop(EventCallback _callback) { - AppDelegate *delegate = [[NSApplication sharedApplication] delegate]; + AppDelegate *delegate = (AppDelegate*)[[NSApplication sharedApplication] delegate]; delegate->event_callback = _callback; [NSApp run]; @@ -48,7 +48,7 @@ int32_t ui_eventloop(EventCallback _callback) void ui_update_tray_icon(int32_t index) { dispatch_async(dispatch_get_main_queue(), ^(void) { - AppDelegate *delegate = [[NSApplication sharedApplication] delegate]; + AppDelegate *delegate = (AppDelegate*)[[NSApplication sharedApplication] delegate]; [delegate setIcon: index]; }); } @@ -57,7 +57,7 @@ void ui_show_notification(char *message, double delay) { NSString *nsMessage = [NSString stringWithUTF8String:message]; dispatch_async(dispatch_get_main_queue(), ^(void) { - AppDelegate *delegate = [[NSApplication sharedApplication] delegate]; + AppDelegate *delegate = (AppDelegate*)[[NSApplication sharedApplication] delegate]; [delegate showNotification: nsMessage withDelay: delay]; }); } @@ -66,7 +66,7 @@ void ui_show_context_menu(char *payload) { NSString *nsPayload = [NSString stringWithUTF8String:payload]; dispatch_async(dispatch_get_main_queue(), ^(void) { - AppDelegate *delegate = [[NSApplication sharedApplication] delegate]; + AppDelegate *delegate = (AppDelegate*)[[NSApplication sharedApplication] delegate]; [delegate popupMenu: nsPayload]; }); } \ No newline at end of file diff --git a/espanso/src/main.rs b/espanso/src/main.rs index 5cc88bc..9ad23df 100644 --- a/espanso/src/main.rs +++ b/espanso/src/main.rs @@ -1,5 +1,5 @@ use espanso_detect::event::{InputEvent, Status}; -use espanso_ui::{icons::TrayIcon, mac::*, menu::*, event::UIEvent::*}; +use espanso_ui::{event::UIEvent::*, icons::TrayIcon, mac::*, menu::*}; use simplelog::{CombinedLogger, Config, LevelFilter, TermLogger, TerminalMode}; fn main() { @@ -35,7 +35,6 @@ fn main() { ), ]; - // let (remote, mut eventloop) = espanso_ui::win32::create(espanso_ui::win32::Win32UIOptions { // show_icon: true, // icon_paths: &icon_paths, @@ -48,7 +47,7 @@ fn main() { }); let handle = std::thread::spawn(move || { - + //let mut source = espanso_detect::win32::Win32Source::new(); //let mut source = espanso_detect::x11::X11Source::new(); // source.initialize(); @@ -91,6 +90,5 @@ fn main() { println!("item {:?}", menu.get_item_id(raw_id)); } } - })); }