changeset 1:6dbe2bbeef70

Improve input event types with helper functions.
author Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
date Fri, 09 Oct 2020 00:18:05 +0200
parents 816237b684ea
children 39f87ef69b2a
files src/main.rs
diffstat 1 files changed, 45 insertions(+), 68 deletions(-) [+]
line wrap: on
line diff
--- 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: &gtk::Application) {
     let dev = match create_uinput_device() {
         Ok(dev) => Arc::new(dev),
@@ -274,25 +286,16 @@ fn build_ui(application: &gtk::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: &gtk::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: &gtk::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)