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 {
let context = scopeguard::guard(raw_context, |raw_context| unsafe {
xkb_context_unref(raw_context);
}
});
if raw_context.is_null() {

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 {
let state = scopeguard::guard(raw_state, |raw_state| unsafe {
xkb_state_unref(raw_state);
}
});
if raw_state.is_null() {

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 {
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() {

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

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;

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

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,
@ -91,6 +90,5 @@ fn main() {
println!("item {:?}", menu.get_item_id(raw_id));
}
}
}));
}