1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475
|
/*
* Copyright (C) 2019 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#pragma once
#include <ftl/flags.h>
#include <input/DisplayViewport.h>
#include <input/InputDevice.h>
#include <input/PropertyMap.h>
#include <cstdint>
#include <optional>
#include <unordered_map>
#include <vector>
#include "EventHub.h"
#include "InputReaderBase.h"
#include "InputReaderContext.h"
#include "NotifyArgs.h"
namespace android {
class PeripheralController;
class PeripheralControllerInterface;
class InputDeviceContext;
class InputMapper;
/* Represents the state of a single input device. */
class InputDevice {
public:
InputDevice(InputReaderContext* context, int32_t id, int32_t generation,
const InputDeviceIdentifier& identifier);
~InputDevice();
inline InputReaderContext* getContext() { return mContext; }
inline int32_t getId() const { return mId; }
inline int32_t getControllerNumber() const { return mControllerNumber; }
inline int32_t getGeneration() const { return mGeneration; }
inline const std::string getName() const { return mIdentifier.name; }
inline const std::string getDescriptor() { return mIdentifier.descriptor; }
inline std::optional<std::string> getBluetoothAddress() const {
return mIdentifier.bluetoothAddress;
}
inline const std::string getLocation() const { return mIdentifier.location; }
inline ftl::Flags<InputDeviceClass> getClasses() const { return mClasses; }
inline uint32_t getSources() const { return mSources; }
inline bool hasEventHubDevices() const { return !mDevices.empty(); }
inline bool isExternal() { return mIsExternal; }
inline std::optional<uint8_t> getAssociatedDisplayPort() const {
return mAssociatedDisplayPort;
}
inline std::optional<std::string> getAssociatedDisplayUniqueId() const {
return mAssociatedDisplayUniqueId;
}
inline std::optional<std::string> getDeviceTypeAssociation() const {
return mAssociatedDeviceType;
}
inline std::optional<DisplayViewport> getAssociatedViewport() const {
return mAssociatedViewport;
}
inline bool hasMic() const { return mHasMic; }
inline bool isIgnored() { return !getMapperCount() && !mController; }
bool isEnabled();
void dump(std::string& dump, const std::string& eventHubDevStr);
void addEmptyEventHubDevice(int32_t eventHubId);
[[nodiscard]] std::list<NotifyArgs> addEventHubDevice(
nsecs_t when, int32_t eventHubId, const InputReaderConfiguration& readerConfig);
void removeEventHubDevice(int32_t eventHubId);
[[nodiscard]] std::list<NotifyArgs> configure(nsecs_t when,
const InputReaderConfiguration& readerConfig,
ConfigurationChanges changes);
[[nodiscard]] std::list<NotifyArgs> reset(nsecs_t when);
[[nodiscard]] std::list<NotifyArgs> process(const RawEvent* rawEvents, size_t count);
[[nodiscard]] std::list<NotifyArgs> timeoutExpired(nsecs_t when);
[[nodiscard]] std::list<NotifyArgs> updateExternalStylusState(const StylusState& state);
InputDeviceInfo getDeviceInfo();
int32_t getKeyCodeState(uint32_t sourceMask, int32_t keyCode);
int32_t getScanCodeState(uint32_t sourceMask, int32_t scanCode);
int32_t getSwitchState(uint32_t sourceMask, int32_t switchCode);
int32_t getKeyCodeForKeyLocation(int32_t locationKeyCode) const;
bool markSupportedKeyCodes(uint32_t sourceMask, const std::vector<int32_t>& keyCodes,
uint8_t* outFlags);
[[nodiscard]] std::list<NotifyArgs> vibrate(const VibrationSequence& sequence, ssize_t repeat,
int32_t token);
[[nodiscard]] std::list<NotifyArgs> cancelVibrate(int32_t token);
bool isVibrating();
std::vector<int32_t> getVibratorIds();
[[nodiscard]] std::list<NotifyArgs> cancelTouch(nsecs_t when, nsecs_t readTime);
bool enableSensor(InputDeviceSensorType sensorType, std::chrono::microseconds samplingPeriod,
std::chrono::microseconds maxBatchReportLatency);
void disableSensor(InputDeviceSensorType sensorType);
void flushSensor(InputDeviceSensorType sensorType);
std::optional<int32_t> getBatteryEventHubId() const;
bool setLightColor(int32_t lightId, int32_t color);
bool setLightPlayerId(int32_t lightId, int32_t playerId);
std::optional<int32_t> getLightColor(int32_t lightId);
std::optional<int32_t> getLightPlayerId(int32_t lightId);
int32_t getMetaState();
void updateMetaState(int32_t keyCode);
void addKeyRemapping(int32_t fromKeyCode, int32_t toKeyCode);
void bumpGeneration();
[[nodiscard]] NotifyDeviceResetArgs notifyReset(nsecs_t when);
inline const PropertyMap& getConfiguration() { return mConfiguration; }
inline EventHubInterface* getEventHub() { return mContext->getEventHub(); }
std::optional<int32_t> getAssociatedDisplayId();
void updateLedState(bool reset);
size_t getMapperCount();
// construct and add a mapper to the input device
template <class T, typename... Args>
T& addMapper(int32_t eventHubId, Args... args) {
// ensure a device entry exists for this eventHubId
addEmptyEventHubDevice(eventHubId);
// create mapper
auto& devicePair = mDevices[eventHubId];
auto& deviceContext = devicePair.first;
auto& mappers = devicePair.second;
T* mapper = new T(*deviceContext, args...);
mappers.emplace_back(mapper);
return *mapper;
}
template <class T, typename... Args>
T& constructAndAddMapper(int32_t eventHubId, Args... args) {
// create mapper
auto& devicePair = mDevices[eventHubId];
auto& deviceContext = devicePair.first;
auto& mappers = devicePair.second;
mappers.push_back(createInputMapper<T>(*deviceContext, args...));
return static_cast<T&>(*mappers.back());
}
// construct and add a controller to the input device
template <class T>
T& addController(int32_t eventHubId) {
// ensure a device entry exists for this eventHubId
addEmptyEventHubDevice(eventHubId);
// create controller
auto& devicePair = mDevices[eventHubId];
auto& deviceContext = devicePair.first;
mController = std::make_unique<T>(*deviceContext);
return *(reinterpret_cast<T*>(mController.get()));
}
private:
InputReaderContext* mContext;
int32_t mId;
int32_t mGeneration;
int32_t mControllerNumber;
InputDeviceIdentifier mIdentifier;
std::string mAlias;
ftl::Flags<InputDeviceClass> mClasses;
// map from eventHubId to device context and mappers
using MapperVector = std::vector<std::unique_ptr<InputMapper>>;
using DevicePair = std::pair<std::unique_ptr<InputDeviceContext>, MapperVector>;
// Map from EventHub ID to pair of device context and vector of mapper.
std::unordered_map<int32_t, DevicePair> mDevices;
// Misc devices controller for lights, battery, etc.
std::unique_ptr<PeripheralControllerInterface> mController;
uint32_t mSources;
bool mIsWaking;
bool mIsExternal;
std::optional<uint8_t> mAssociatedDisplayPort;
std::optional<std::string> mAssociatedDisplayUniqueId;
std::optional<std::string> mAssociatedDeviceType;
std::optional<DisplayViewport> mAssociatedViewport;
bool mHasMic;
bool mDropUntilNextSync;
std::optional<bool> mShouldSmoothScroll;
typedef int32_t (InputMapper::*GetStateFunc)(uint32_t sourceMask, int32_t code);
int32_t getState(uint32_t sourceMask, int32_t code, GetStateFunc getStateFunc);
std::vector<std::unique_ptr<InputMapper>> createMappers(
InputDeviceContext& contextPtr, const InputReaderConfiguration& readerConfig);
[[nodiscard]] std::list<NotifyArgs> configureInternal(
nsecs_t when, const InputReaderConfiguration& readerConfig,
ConfigurationChanges changes, bool forceEnable = false);
[[nodiscard]] std::list<NotifyArgs> updateEnableState(
nsecs_t when, const InputReaderConfiguration& readerConfig, bool forceEnable = false);
PropertyMap mConfiguration;
// Runs logic post a `process` call. This can be used to update the generated `NotifyArgs` as
// per the properties of the InputDevice.
void postProcess(std::list<NotifyArgs>& args) const;
// helpers to interate over the devices collection
// run a function against every mapper on every subdevice
inline void for_each_mapper(std::function<void(InputMapper&)> f) {
for (auto& deviceEntry : mDevices) {
auto& devicePair = deviceEntry.second;
auto& mappers = devicePair.second;
for (auto& mapperPtr : mappers) {
f(*mapperPtr);
}
}
}
// run a function against every mapper on a specific subdevice
inline void for_each_mapper_in_subdevice(int32_t eventHubDevice,
std::function<void(InputMapper&)> f) {
auto deviceIt = mDevices.find(eventHubDevice);
if (deviceIt != mDevices.end()) {
auto& devicePair = deviceIt->second;
auto& mappers = devicePair.second;
for (auto& mapperPtr : mappers) {
f(*mapperPtr);
}
}
}
// run a function against every subdevice
inline void for_each_subdevice(std::function<void(InputDeviceContext&)> f) {
for (auto& deviceEntry : mDevices) {
auto& devicePair = deviceEntry.second;
auto& contextPtr = devicePair.first;
f(*contextPtr);
}
}
// return the first value returned by a function over every mapper.
// if all mappers return nullopt, return nullopt.
template <typename T>
inline std::optional<T> first_in_mappers(
std::function<std::optional<T>(InputMapper&)> f) const {
for (auto& deviceEntry : mDevices) {
auto& devicePair = deviceEntry.second;
auto& mappers = devicePair.second;
for (auto& mapperPtr : mappers) {
std::optional<T> ret = f(*mapperPtr);
if (ret) {
return ret;
}
}
}
return std::nullopt;
}
};
/* Provides access to EventHub methods, but limits access to the current InputDevice.
* Essentially an implementation of EventHubInterface, but for a specific device id.
* Helps hide implementation details of InputDevice and EventHub. Used by mappers to
* check the status of the associated hardware device
*/
class InputDeviceContext {
public:
InputDeviceContext(InputDevice& device, int32_t eventHubId);
virtual ~InputDeviceContext();
inline InputReaderContext* getContext() { return mContext; }
inline int32_t getId() { return mDeviceId; }
inline int32_t getEventHubId() { return mId; }
inline ftl::Flags<InputDeviceClass> getDeviceClasses() const {
return mEventHub->getDeviceClasses(mId);
}
inline InputDeviceIdentifier getDeviceIdentifier() const {
return mEventHub->getDeviceIdentifier(mId);
}
inline int32_t getDeviceControllerNumber() const {
return mEventHub->getDeviceControllerNumber(mId);
}
inline status_t getAbsoluteAxisInfo(int32_t code, RawAbsoluteAxisInfo* axisInfo) const {
if (const auto status = mEventHub->getAbsoluteAxisInfo(mId, code, axisInfo); status != OK) {
return status;
}
// Validate axis info for InputDevice.
if (axisInfo->valid && axisInfo->minValue == axisInfo->maxValue) {
// Historically, we deem axes with the same min and max values as invalid to avoid
// dividing by zero when scaling by max - min.
// TODO(b/291772515): Perform axis info validation on a per-axis basis when it is used.
axisInfo->valid = false;
}
return OK;
}
inline bool hasRelativeAxis(int32_t code) const {
return mEventHub->hasRelativeAxis(mId, code);
}
inline bool hasInputProperty(int32_t property) const {
return mEventHub->hasInputProperty(mId, property);
}
inline bool hasMscEvent(int mscEvent) const { return mEventHub->hasMscEvent(mId, mscEvent); }
inline void addKeyRemapping(int32_t fromKeyCode, int32_t toKeyCode) const {
mEventHub->addKeyRemapping(mId, fromKeyCode, toKeyCode);
}
inline status_t mapKey(int32_t scanCode, int32_t usageCode, int32_t metaState,
int32_t* outKeycode, int32_t* outMetaState, uint32_t* outFlags) const {
return mEventHub->mapKey(mId, scanCode, usageCode, metaState, outKeycode, outMetaState,
outFlags);
}
inline status_t mapAxis(int32_t scanCode, AxisInfo* outAxisInfo) const {
return mEventHub->mapAxis(mId, scanCode, outAxisInfo);
}
inline base::Result<std::pair<InputDeviceSensorType, int32_t>> mapSensor(int32_t absCode) {
return mEventHub->mapSensor(mId, absCode);
}
inline const std::vector<int32_t> getRawLightIds() { return mEventHub->getRawLightIds(mId); }
inline std::optional<RawLightInfo> getRawLightInfo(int32_t lightId) {
return mEventHub->getRawLightInfo(mId, lightId);
}
inline std::optional<int32_t> getLightBrightness(int32_t lightId) {
return mEventHub->getLightBrightness(mId, lightId);
}
inline void setLightBrightness(int32_t lightId, int32_t brightness) {
return mEventHub->setLightBrightness(mId, lightId, brightness);
}
inline std::optional<std::unordered_map<LightColor, int32_t>> getLightIntensities(
int32_t lightId) {
return mEventHub->getLightIntensities(mId, lightId);
}
inline void setLightIntensities(int32_t lightId,
std::unordered_map<LightColor, int32_t> intensities) {
return mEventHub->setLightIntensities(mId, lightId, intensities);
}
inline std::vector<TouchVideoFrame> getVideoFrames() { return mEventHub->getVideoFrames(mId); }
inline int32_t getScanCodeState(int32_t scanCode) const {
return mEventHub->getScanCodeState(mId, scanCode);
}
inline int32_t getKeyCodeState(int32_t keyCode) const {
return mEventHub->getKeyCodeState(mId, keyCode);
}
inline int32_t getKeyCodeForKeyLocation(int32_t locationKeyCode) const {
return mEventHub->getKeyCodeForKeyLocation(mId, locationKeyCode);
}
inline int32_t getSwitchState(int32_t sw) const { return mEventHub->getSwitchState(mId, sw); }
inline status_t getAbsoluteAxisValue(int32_t code, int32_t* outValue) const {
return mEventHub->getAbsoluteAxisValue(mId, code, outValue);
}
inline base::Result<std::vector<int32_t>> getMtSlotValues(int32_t axis,
size_t slotCount) const {
return mEventHub->getMtSlotValues(mId, axis, slotCount);
}
inline bool markSupportedKeyCodes(const std::vector<int32_t>& keyCodes,
uint8_t* outFlags) const {
return mEventHub->markSupportedKeyCodes(mId, keyCodes, outFlags);
}
inline bool hasScanCode(int32_t scanCode) const {
return mEventHub->hasScanCode(mId, scanCode);
}
inline bool hasKeyCode(int32_t keyCode) const { return mEventHub->hasKeyCode(mId, keyCode); }
inline bool hasLed(int32_t led) const { return mEventHub->hasLed(mId, led); }
inline void setLedState(int32_t led, bool on) { return mEventHub->setLedState(mId, led, on); }
inline void getVirtualKeyDefinitions(std::vector<VirtualKeyDefinition>& outVirtualKeys) const {
return mEventHub->getVirtualKeyDefinitions(mId, outVirtualKeys);
}
inline const std::shared_ptr<KeyCharacterMap> getKeyCharacterMap() const {
return mEventHub->getKeyCharacterMap(mId);
}
inline bool setKeyboardLayoutOverlay(std::shared_ptr<KeyCharacterMap> map) {
return mEventHub->setKeyboardLayoutOverlay(mId, map);
}
inline const std::optional<RawLayoutInfo> getRawLayoutInfo() {
return mEventHub->getRawLayoutInfo(mId);
}
inline void vibrate(const VibrationElement& element) {
return mEventHub->vibrate(mId, element);
}
inline void cancelVibrate() { return mEventHub->cancelVibrate(mId); }
inline std::vector<int32_t> getVibratorIds() { return mEventHub->getVibratorIds(mId); }
inline const std::vector<int32_t> getRawBatteryIds() {
return mEventHub->getRawBatteryIds(mId);
}
inline std::optional<RawBatteryInfo> getRawBatteryInfo(int32_t batteryId) {
return mEventHub->getRawBatteryInfo(mId, batteryId);
}
inline std::optional<int32_t> getBatteryCapacity(int32_t batteryId) {
return mEventHub->getBatteryCapacity(mId, batteryId);
}
inline std::optional<int32_t> getBatteryStatus(int32_t batteryId) {
return mEventHub->getBatteryStatus(mId, batteryId);
}
inline bool hasAbsoluteAxis(int32_t code) const {
RawAbsoluteAxisInfo info;
mEventHub->getAbsoluteAxisInfo(mId, code, &info);
return info.valid;
}
inline bool isKeyPressed(int32_t scanCode) const {
return mEventHub->getScanCodeState(mId, scanCode) == AKEY_STATE_DOWN;
}
inline bool isKeyCodePressed(int32_t keyCode) const {
return mEventHub->getKeyCodeState(mId, keyCode) == AKEY_STATE_DOWN;
}
inline int32_t getAbsoluteAxisValue(int32_t code) const {
int32_t value;
mEventHub->getAbsoluteAxisValue(mId, code, &value);
return value;
}
inline bool isDeviceEnabled() { return mEventHub->isDeviceEnabled(mId); }
inline status_t enableDevice() { return mEventHub->enableDevice(mId); }
inline status_t disableDevice() { return mEventHub->disableDevice(mId); }
inline const std::string getName() const { return mDevice.getName(); }
inline const std::string getDescriptor() { return mDevice.getDescriptor(); }
inline const std::string getLocation() { return mDevice.getLocation(); }
inline bool isExternal() const { return mDevice.isExternal(); }
inline std::optional<uint8_t> getAssociatedDisplayPort() const {
return mDevice.getAssociatedDisplayPort();
}
inline std::optional<std::string> getAssociatedDisplayUniqueId() const {
return mDevice.getAssociatedDisplayUniqueId();
}
inline std::optional<std::string> getDeviceTypeAssociation() const {
return mDevice.getDeviceTypeAssociation();
}
virtual std::optional<DisplayViewport> getAssociatedViewport() const {
return mDevice.getAssociatedViewport();
}
[[nodiscard]] inline std::list<NotifyArgs> cancelTouch(nsecs_t when, nsecs_t readTime) {
return mDevice.cancelTouch(when, readTime);
}
inline void bumpGeneration() { mDevice.bumpGeneration(); }
inline const PropertyMap& getConfiguration() const { return mDevice.getConfiguration(); }
private:
InputDevice& mDevice;
InputReaderContext* mContext;
EventHubInterface* mEventHub;
int32_t mId;
int32_t mDeviceId;
};
} // namespace android
|