# HG changeset patch # User Emmanuel Gil Peyrot # Date 1602195485 -7200 # Node ID 6dbe2bbeef707f8da1be5effc8bfd4286463c099 # Parent 816237b684eaadfa79361d5f2780c0f80e3b3cf9 Improve input event types with helper functions. diff --git a/src/main.rs b/src/main.rs --- a/src/main.rs +++ b/src/main.rs @@ -8,7 +8,7 @@ use std::sync::{Arc, Mutex}; use input_linux::{ sys::input_event, sys::timeval, AbsoluteAxis, AbsoluteInfo, AbsoluteInfoSetup, EventKind, - InputId, InputProperty, Key, MiscKind, UInputHandle, + InputId, InputProperty, Key, MiscKind, SynchronizeKind, UInputHandle, }; const WIDTH: i32 = 320; @@ -163,18 +163,30 @@ fn create_uinput_device() -> std::io::Re Ok(dev) } -fn input_event_new(type_: EventKind, code: AbsoluteAxis, value: i32) -> input_event { +fn input_event_new(type_: EventKind, code: u16, value: i32) -> input_event { input_event { time: timeval { tv_sec: 0, tv_usec: 0, }, type_: type_ as u16, - code: code as u16, + code, value, } } +fn input_axis_new(code: AbsoluteAxis, value: i32) -> input_event { + input_event_new(EventKind::Absolute, code as u16, value) +} + +fn input_misc_new(code: MiscKind, value: i32) -> input_event { + input_event_new(EventKind::Absolute, code as u16, value) +} + +fn input_synchronize_new(code: SynchronizeKind, value: i32) -> input_event { + input_event_new(EventKind::Absolute, code as u16, value) +} + fn build_ui(application: >k::Application) { let dev = match create_uinput_device() { Ok(dev) => Arc::new(dev), @@ -274,25 +286,16 @@ fn build_ui(application: >k::Applicati let current_size = current_size.lock().unwrap(); let (width, height) = *current_size; dev.write(&[ - input_event_new( - EventKind::Absolute, - AbsoluteAxis::X, - (x * MAX_X as f64 / width) as i32, - ), - input_event_new( - EventKind::Absolute, - AbsoluteAxis::Y, - (y * MAX_Y as f64 / height) as i32, - ), - input_event_new(EventKind::Absolute, AbsoluteAxis::Z, 0), - input_event_new(EventKind::Absolute, AbsoluteAxis::Wheel, 0), - input_event_new(EventKind::Absolute, AbsoluteAxis::Pressure, 1024), - input_event_new(EventKind::Absolute, AbsoluteAxis::Distance, 0), - input_event_new(EventKind::Absolute, AbsoluteAxis::TiltX, 16), - input_event_new(EventKind::Absolute, AbsoluteAxis::TiltY, 0), - // TODO: Change the type of the second parameter here. - input_event_new(EventKind::Misc, AbsoluteAxis::X, 0), - input_event_new(EventKind::Synchronize, AbsoluteAxis::X, 0), + input_axis_new(AbsoluteAxis::X, (x * MAX_X as f64 / width) as i32), + input_axis_new(AbsoluteAxis::Y, (y * MAX_Y as f64 / height) as i32), + input_axis_new(AbsoluteAxis::Z, 0), + input_axis_new(AbsoluteAxis::Wheel, 0), + input_axis_new(AbsoluteAxis::Pressure, 1024), + input_axis_new(AbsoluteAxis::Distance, 0), + input_axis_new(AbsoluteAxis::TiltX, 16), + input_axis_new(AbsoluteAxis::TiltY, 0), + input_misc_new(MiscKind::Serial, 0), + input_synchronize_new(SynchronizeKind::Report, 0), ]) .unwrap(); Inhibit(false) @@ -315,25 +318,16 @@ fn build_ui(application: >k::Applicati let current_size = current_size.lock().unwrap(); let (width, height) = *current_size; dev.write(&[ - input_event_new( - EventKind::Absolute, - AbsoluteAxis::X, - (x * MAX_X as f64 / width) as i32, - ), - input_event_new( - EventKind::Absolute, - AbsoluteAxis::Y, - (y * MAX_Y as f64 / height) as i32, - ), - input_event_new(EventKind::Absolute, AbsoluteAxis::Z, 0), - input_event_new(EventKind::Absolute, AbsoluteAxis::Wheel, 0), - input_event_new(EventKind::Absolute, AbsoluteAxis::Pressure, 0), - input_event_new(EventKind::Absolute, AbsoluteAxis::Distance, 16), - input_event_new(EventKind::Absolute, AbsoluteAxis::TiltX, 16), - input_event_new(EventKind::Absolute, AbsoluteAxis::TiltY, 0), - // TODO: Change the type of the second parameter here. - input_event_new(EventKind::Misc, AbsoluteAxis::X, 0), - input_event_new(EventKind::Synchronize, AbsoluteAxis::X, 0), + input_axis_new(AbsoluteAxis::X, (x * MAX_X as f64 / width) as i32), + input_axis_new(AbsoluteAxis::Y, (y * MAX_Y as f64 / height) as i32), + input_axis_new(AbsoluteAxis::Z, 0), + input_axis_new(AbsoluteAxis::Wheel, 0), + input_axis_new(AbsoluteAxis::Pressure, 0), + input_axis_new(AbsoluteAxis::Distance, 16), + input_axis_new(AbsoluteAxis::TiltX, 16), + input_axis_new(AbsoluteAxis::TiltY, 0), + input_misc_new(MiscKind::Serial, 0), + input_synchronize_new(SynchronizeKind::Report, 0), ]) .unwrap(); Inhibit(false) @@ -345,33 +339,16 @@ fn build_ui(application: >k::Applicati let current_size = current_size.lock().unwrap(); let (width, height) = *current_size; dev.write(&[ - input_event_new( - EventKind::Absolute, - AbsoluteAxis::X, - (x * MAX_X as f64 / width) as i32, - ), - input_event_new( - EventKind::Absolute, - AbsoluteAxis::Y, - (y * MAX_Y as f64 / height) as i32, - ), - input_event_new(EventKind::Absolute, AbsoluteAxis::Z, 0), - input_event_new(EventKind::Absolute, AbsoluteAxis::Wheel, 0), - input_event_new( - EventKind::Absolute, - AbsoluteAxis::Pressure, - if *pressed { 2048 } else { 0 }, - ), - input_event_new( - EventKind::Absolute, - AbsoluteAxis::Distance, - if *pressed { 0 } else { 32 }, - ), - input_event_new(EventKind::Absolute, AbsoluteAxis::TiltX, 16), - input_event_new(EventKind::Absolute, AbsoluteAxis::TiltY, 0), - // TODO: Change the type of the second parameter here. - input_event_new(EventKind::Misc, AbsoluteAxis::X, 0), - input_event_new(EventKind::Synchronize, AbsoluteAxis::X, 0), + input_axis_new(AbsoluteAxis::X, (x * MAX_X as f64 / width) as i32), + input_axis_new(AbsoluteAxis::Y, (y * MAX_Y as f64 / height) as i32), + input_axis_new(AbsoluteAxis::Z, 0), + input_axis_new(AbsoluteAxis::Wheel, 0), + input_axis_new(AbsoluteAxis::Pressure, if *pressed { 2048 } else { 0 }), + input_axis_new(AbsoluteAxis::Distance, if *pressed { 0 } else { 32 }), + input_axis_new(AbsoluteAxis::TiltX, 16), + input_axis_new(AbsoluteAxis::TiltY, 0), + input_misc_new(MiscKind::Serial, 0), + input_synchronize_new(SynchronizeKind::Report, 0), ]) .unwrap(); Inhibit(false)