Implement KeyboardClassifier interface in rust
DD: go/project-imposter-android
This CL includes:
- Rust interface setup
Next CL to include:
- Basic categorization into alphabetic and non-alphabetic
- Updating categorization based on key presses
Test: atest --host libinput_rust_test
Test: atest inputflinger_tests
Bug: 263559234
Flag: com.android.input.flags.enable_keyboard_classifier_rust_impl
Change-Id: I52773be992ddd8efaa9546e0af8b0a78515d931c
diff --git a/services/inputflinger/reader/include/EventHub.h b/services/inputflinger/reader/include/EventHub.h
index 39d2f5b..7cf584d 100644
--- a/services/inputflinger/reader/include/EventHub.h
+++ b/services/inputflinger/reader/include/EventHub.h
@@ -85,64 +85,67 @@
/*
* Input device classes.
+ *
+ * These classes are duplicated in rust side here: /frameworks/native/libs/input/rust/input.rs.
+ * If any new classes are added, we need to add them in rust input side too.
*/
enum class InputDeviceClass : uint32_t {
/* The input device is a keyboard or has buttons. */
- KEYBOARD = 0x00000001,
+ KEYBOARD = android::os::IInputConstants::DEVICE_CLASS_KEYBOARD,
/* The input device is an alpha-numeric keyboard (not just a dial pad). */
- ALPHAKEY = 0x00000002,
+ ALPHAKEY = android::os::IInputConstants::DEVICE_CLASS_ALPHAKEY,
/* The input device is a touchscreen or a touchpad (either single-touch or multi-touch). */
- TOUCH = 0x00000004,
+ TOUCH = android::os::IInputConstants::DEVICE_CLASS_TOUCH,
/* The input device is a cursor device such as a trackball or mouse. */
- CURSOR = 0x00000008,
+ CURSOR = android::os::IInputConstants::DEVICE_CLASS_CURSOR,
/* The input device is a multi-touch touchscreen or touchpad. */
- TOUCH_MT = 0x00000010,
+ TOUCH_MT = android::os::IInputConstants::DEVICE_CLASS_TOUCH_MT,
/* The input device is a directional pad (implies keyboard, has DPAD keys). */
- DPAD = 0x00000020,
+ DPAD = android::os::IInputConstants::DEVICE_CLASS_DPAD,
/* The input device is a gamepad (implies keyboard, has BUTTON keys). */
- GAMEPAD = 0x00000040,
+ GAMEPAD = android::os::IInputConstants::DEVICE_CLASS_GAMEPAD,
/* The input device has switches. */
- SWITCH = 0x00000080,
+ SWITCH = android::os::IInputConstants::DEVICE_CLASS_SWITCH,
/* The input device is a joystick (implies gamepad, has joystick absolute axes). */
- JOYSTICK = 0x00000100,
+ JOYSTICK = android::os::IInputConstants::DEVICE_CLASS_JOYSTICK,
/* The input device has a vibrator (supports FF_RUMBLE). */
- VIBRATOR = 0x00000200,
+ VIBRATOR = android::os::IInputConstants::DEVICE_CLASS_VIBRATOR,
/* The input device has a microphone. */
- MIC = 0x00000400,
+ MIC = android::os::IInputConstants::DEVICE_CLASS_MIC,
/* The input device is an external stylus (has data we want to fuse with touch data). */
- EXTERNAL_STYLUS = 0x00000800,
+ EXTERNAL_STYLUS = android::os::IInputConstants::DEVICE_CLASS_EXTERNAL_STYLUS,
/* The input device has a rotary encoder */
- ROTARY_ENCODER = 0x00001000,
+ ROTARY_ENCODER = android::os::IInputConstants::DEVICE_CLASS_ROTARY_ENCODER,
/* The input device has a sensor like accelerometer, gyro, etc */
- SENSOR = 0x00002000,
+ SENSOR = android::os::IInputConstants::DEVICE_CLASS_SENSOR,
/* The input device has a battery */
- BATTERY = 0x00004000,
+ BATTERY = android::os::IInputConstants::DEVICE_CLASS_BATTERY,
/* The input device has sysfs controllable lights */
- LIGHT = 0x00008000,
+ LIGHT = android::os::IInputConstants::DEVICE_CLASS_LIGHT,
/* The input device is a touchpad, requiring an on-screen cursor. */
- TOUCHPAD = 0x00010000,
+ TOUCHPAD = android::os::IInputConstants::DEVICE_CLASS_TOUCHPAD,
/* The input device is virtual (not a real device, not part of UI configuration). */
- VIRTUAL = 0x40000000,
+ VIRTUAL = android::os::IInputConstants::DEVICE_CLASS_VIRTUAL,
/* The input device is external (not built-in). */
- EXTERNAL = 0x80000000,
+ EXTERNAL = android::os::IInputConstants::DEVICE_CLASS_EXTERNAL,
};
enum class SysfsClass : uint32_t {
diff --git a/services/inputflinger/reader/include/InputDevice.h b/services/inputflinger/reader/include/InputDevice.h
index 4c9af2e..2a7e262 100644
--- a/services/inputflinger/reader/include/InputDevice.h
+++ b/services/inputflinger/reader/include/InputDevice.h
@@ -79,6 +79,8 @@
inline bool isIgnored() { return !getMapperCount() && !mController; }
+ inline KeyboardType getKeyboardType() const { return mKeyboardType; }
+
bool isEnabled();
void dump(std::string& dump, const std::string& eventHubDevStr);
@@ -124,6 +126,8 @@
void addKeyRemapping(int32_t fromKeyCode, int32_t toKeyCode);
+ void setKeyboardType(KeyboardType keyboardType);
+
void bumpGeneration();
[[nodiscard]] NotifyDeviceResetArgs notifyReset(nsecs_t when);
@@ -196,6 +200,7 @@
uint32_t mSources;
bool mIsWaking;
bool mIsExternal;
+ KeyboardType mKeyboardType = KeyboardType::NONE;
std::optional<uint8_t> mAssociatedDisplayPort;
std::optional<std::string> mAssociatedDisplayUniqueIdByPort;
std::optional<std::string> mAssociatedDisplayUniqueIdByDescriptor;
@@ -470,6 +475,10 @@
}
inline void bumpGeneration() { mDevice.bumpGeneration(); }
inline const PropertyMap& getConfiguration() const { return mDevice.getConfiguration(); }
+ inline KeyboardType getKeyboardType() const { return mDevice.getKeyboardType(); }
+ inline void setKeyboardType(KeyboardType keyboardType) {
+ return mDevice.setKeyboardType(keyboardType);
+ }
private:
InputDevice& mDevice;
diff --git a/services/inputflinger/reader/include/InputReader.h b/services/inputflinger/reader/include/InputReader.h
index 7e701c5..6f8c289 100644
--- a/services/inputflinger/reader/include/InputReader.h
+++ b/services/inputflinger/reader/include/InputReader.h
@@ -157,6 +157,7 @@
void setLastKeyDownTimestamp(nsecs_t when) REQUIRES(mReader->mLock)
REQUIRES(mLock) override;
nsecs_t getLastKeyDownTimestamp() REQUIRES(mReader->mLock) REQUIRES(mLock) override;
+ KeyboardClassifier& getKeyboardClassifier() override;
} mContext;
friend class ContextImpl;
@@ -176,6 +177,10 @@
// The next stage that should receive the events generated inside InputReader.
InputListenerInterface& mNextListener;
+
+ // Classifier for keyboard/keyboard-like devices
+ std::unique_ptr<KeyboardClassifier> mKeyboardClassifier;
+
// As various events are generated inside InputReader, they are stored inside this list. The
// list can only be accessed with the lock, so the events inside it are well-ordered.
// Once the reader is done working, these events will be swapped into a temporary storage and
diff --git a/services/inputflinger/reader/include/InputReaderContext.h b/services/inputflinger/reader/include/InputReaderContext.h
index 907a49f..e0e0ac2 100644
--- a/services/inputflinger/reader/include/InputReaderContext.h
+++ b/services/inputflinger/reader/include/InputReaderContext.h
@@ -17,6 +17,7 @@
#pragma once
#include <input/InputDevice.h>
+#include <input/KeyboardClassifier.h>
#include "NotifyArgs.h"
#include <vector>
@@ -64,6 +65,8 @@
virtual void setLastKeyDownTimestamp(nsecs_t when) = 0;
virtual nsecs_t getLastKeyDownTimestamp() = 0;
+
+ virtual KeyboardClassifier& getKeyboardClassifier() = 0;
};
} // namespace android