Format code

This commit is contained in:
Federico Terzi 2021-02-08 21:23:28 +01:00
parent 7d357149ff
commit 18515319a8
11 changed files with 73 additions and 79 deletions

View File

@ -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<Context> {
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(),
}
}

View File

@ -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::<input_event>();
#[allow(clippy::needless_range_loop)]
for i in 0..nevs {
let event = self.process_event(evs[i].type_, evs[i].code, evs[i].value);

View File

@ -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<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);
}
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(),
}
}

View File

@ -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<RawInputEvent> for Option<InputEvent> {
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<RawInputEvent> for Option<InputEvent> {
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<Variant>) {
fn raw_to_mouse_button(raw: u16) -> Option<MouseButton> {
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<MouseButton> {
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 {
})
);
}
}
}

View File

@ -26,4 +26,4 @@ pub mod win32;
pub mod x11;
#[cfg(target_os = "linux")]
pub mod evdev;
pub mod evdev;

View File

@ -100,7 +100,7 @@ impl Win32Source {
-2 => Win32SourceError::RawInputFailed(),
_ => Win32SourceError::Unknown(),
};
return Err(error.into())
return Err(error.into());
}
self.handle = handle;

View File

@ -17,9 +17,7 @@
* along with espanso. If not, see <https://www.gnu.org/licenses/>.
*/
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<MouseButton> {
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<InputEvent> = raw.into();
assert!(result.is_none());
}
}
}

View File

@ -9,4 +9,4 @@ pub mod win32;
pub mod linux;
#[cfg(target_os = "macos")]
pub mod mac;
pub mod mac;

View File

@ -17,7 +17,7 @@
* along with espanso. If not, see <https://www.gnu.org/licenses/>.
*/
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<String>,
show_icon: bool,
) -> Self {
pub(crate) fn new(icons: Vec<String>, 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<TrayIcon, usize>,
) -> Self {
Self {
icon_indexes,
}
pub(crate) fn new(icon_indexes: HashMap<TrayIcon, usize>) -> 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<RawUIEvent> for Option<UIEvent> {
None
}
}
}

View File

@ -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];
});
}

View File

@ -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));
}
}
}));
}