summary refs log tree commit diff
diff options
context:
space:
mode:
authorNoah Gold <nkgold@google.com>2019-12-11 09:13:24 -0800
committerCommit Bot <commit-bot@chromium.org>2020-01-03 20:03:49 +0000
commit778953faa1c2f7b72e944273494c9c2515a84908 (patch)
tree8a485277d70ba1e2be3c3452df603e1b9d19a075
parent313f64127f812b5ffa97fcfeb74a1e98e64f6fbf (diff)
downloadcrosvm-778953faa1c2f7b72e944273494c9c2515a84908.tar
crosvm-778953faa1c2f7b72e944273494c9c2515a84908.tar.gz
crosvm-778953faa1c2f7b72e944273494c9c2515a84908.tar.bz2
crosvm-778953faa1c2f7b72e944273494c9c2515a84908.tar.lz
crosvm-778953faa1c2f7b72e944273494c9c2515a84908.tar.xz
crosvm-778953faa1c2f7b72e944273494c9c2515a84908.tar.zst
crosvm-778953faa1c2f7b72e944273494c9c2515a84908.zip
Add Linux input_event struct & generator funcs.
BUG=chromium:1023975
TEST=builds.

Change-Id: Ibc9e1f58b1188f197d57534f60e1cdc16091f116
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/crosvm/+/1962752
Reviewed-by: Zach Reizner <zachr@chromium.org>
Tested-by: kokoro <noreply+kokoro@google.com>
Commit-Queue: Noah Gold <nkgold@google.com>
-rw-r--r--linux_input_sys/Cargo.toml10
-rw-r--r--linux_input_sys/src/lib.rs83
2 files changed, 93 insertions, 0 deletions
diff --git a/linux_input_sys/Cargo.toml b/linux_input_sys/Cargo.toml
new file mode 100644
index 0000000..51fec70
--- /dev/null
+++ b/linux_input_sys/Cargo.toml
@@ -0,0 +1,10 @@
+[package]
+name = "linux_input_sys"
+version = "0.1.0"
+authors = ["The Chromium OS Authors"]
+edition = "2018"
+
+[dependencies]
+data_model = { path = "../data_model" }
+libc = "*"
+sys_util = { path = "../sys_util" }
diff --git a/linux_input_sys/src/lib.rs b/linux_input_sys/src/lib.rs
new file mode 100644
index 0000000..4824e43
--- /dev/null
+++ b/linux_input_sys/src/lib.rs
@@ -0,0 +1,83 @@
+// Copyright 2019 The Chromium OS Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+use data_model::DataInit;
+use std::mem::size_of;
+
+const EV_SYN: u16 = 0x00;
+const EV_KEY: u16 = 0x01;
+const EV_REL: u16 = 0x02;
+const EV_ABS: u16 = 0x03;
+const SYN_REPORT: u16 = 0;
+const REL_X: u16 = 0x00;
+const REL_Y: u16 = 0x01;
+const ABS_X: u16 = 0x00;
+const ABS_Y: u16 = 0x01;
+const BTN_TOUCH: u16 = 0x14a;
+const BTN_TOOL_FINGER: u16 = 0x145;
+
+#[derive(Copy, Clone, Debug, Default, Eq, PartialEq)]
+#[repr(C)]
+pub struct input_event {
+    pub timestamp_fields: [u64; 2],
+    pub type_: u16,
+    pub code: u16,
+    pub value: u32,
+}
+// Safe because it only has data and has no implicit padding.
+unsafe impl DataInit for input_event {}
+
+impl input_event {
+    pub const EVENT_SIZE: usize = size_of::<input_event>();
+
+    #[inline]
+    pub fn syn() -> input_event {
+        input_event {
+            timestamp_fields: [0, 0],
+            type_: EV_SYN,
+            code: SYN_REPORT,
+            value: 0,
+        }
+    }
+
+    #[inline]
+    pub fn absolute(code: u16, value: u32) -> input_event {
+        input_event {
+            timestamp_fields: [0, 0],
+            type_: EV_ABS,
+            code,
+            value,
+        }
+    }
+
+    #[inline]
+    pub fn absolute_x(x: u32) -> input_event {
+        Self::absolute(ABS_X, x)
+    }
+
+    #[inline]
+    pub fn absolute_y(y: u32) -> input_event {
+        Self::absolute(ABS_Y, y)
+    }
+
+    #[inline]
+    pub fn touch(has_contact: bool) -> input_event {
+        Self::key(BTN_TOUCH, has_contact)
+    }
+
+    #[inline]
+    pub fn finger_tool(active: bool) -> input_event {
+        Self::key(BTN_TOOL_FINGER, active)
+    }
+
+    #[inline]
+    pub fn key(code: u16, pressed: bool) -> input_event {
+        input_event {
+            timestamp_fields: [0, 0],
+            type_: EV_KEY,
+            code,
+            value: if pressed { 1 } else { 0 },
+        }
+    }
+}