1 /* 2 * Copyright (C) 2019 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 #pragma once 18 19 #include "HidUsageAccumulator.h" 20 #include "InputMapper.h" 21 22 namespace android { 23 24 class KeyboardInputMapper : public InputMapper { 25 public: 26 template <class T, class... Args> 27 friend std::unique_ptr<T> createInputMapper(InputDeviceContext& deviceContext, 28 const InputReaderConfiguration& readerConfig, 29 Args... args); 30 ~KeyboardInputMapper() override = default; 31 32 uint32_t getSources() const override; 33 void populateDeviceInfo(InputDeviceInfo& deviceInfo) override; 34 void dump(std::string& dump) override; 35 [[nodiscard]] std::list<NotifyArgs> reconfigure(nsecs_t when, 36 const InputReaderConfiguration& config, 37 ConfigurationChanges changes) override; 38 [[nodiscard]] std::list<NotifyArgs> reset(nsecs_t when) override; 39 [[nodiscard]] std::list<NotifyArgs> process(const RawEvent& rawEvent) override; 40 41 int32_t getKeyCodeState(uint32_t sourceMask, int32_t keyCode) override; 42 int32_t getScanCodeState(uint32_t sourceMask, int32_t scanCode) override; 43 bool markSupportedKeyCodes(uint32_t sourceMask, const std::vector<int32_t>& keyCodes, 44 uint8_t* outFlags) override; 45 int32_t getKeyCodeForKeyLocation(int32_t locationKeyCode) const override; 46 47 int32_t getMetaState() override; 48 bool updateMetaState(int32_t keyCode) override; 49 std::optional<ui::LogicalDisplayId> getAssociatedDisplayId() override; 50 void updateLedState(bool reset) override; 51 52 private: 53 // The current viewport. 54 std::optional<DisplayViewport> mViewport{}; 55 56 struct KeyDown { 57 nsecs_t downTime{}; 58 int32_t keyCode{}; 59 int32_t scanCode{}; 60 int32_t flags{}; 61 }; 62 63 uint32_t mSource{}; 64 std::optional<KeyboardLayoutInfo> mKeyboardLayoutInfo; 65 66 std::vector<KeyDown> mKeyDowns{}; // keys that are down 67 int32_t mMetaState{}; 68 69 HidUsageAccumulator mHidUsageAccumulator; 70 71 struct LedState { 72 bool avail{}; // led is available 73 bool on{}; // we think the led is currently on 74 }; 75 LedState mCapsLockLedState{}; 76 LedState mNumLockLedState{}; 77 LedState mScrollLockLedState{}; 78 79 // Immutable configuration parameters. 80 struct Parameters { 81 bool orientationAware{}; 82 bool handlesKeyRepeat{}; 83 bool doNotWakeByDefault{}; 84 } mParameters{}; 85 86 KeyboardInputMapper(InputDeviceContext& deviceContext, 87 const InputReaderConfiguration& readerConfig, uint32_t source); 88 void configureParameters(); 89 void dumpParameters(std::string& dump) const; 90 91 ui::Rotation getOrientation(); 92 ui::LogicalDisplayId getDisplayId(); 93 94 [[nodiscard]] std::list<NotifyArgs> processKey(nsecs_t when, nsecs_t readTime, bool down, 95 int32_t scanCode, int32_t usageCode); 96 97 bool updateMetaStateIfNeeded(int32_t keyCode, bool down); 98 99 std::optional<size_t> findKeyDownIndex(int32_t scanCode); 100 std::optional<KeyboardLayoutInfo> getKeyboardLayoutInfo() const; 101 bool updateKeyboardLayoutOverlay(); 102 103 void resetLedState(); 104 void initializeLedState(LedState& ledState, int32_t led); 105 void updateLedStateForModifier(LedState& ledState, int32_t led, int32_t modifier, bool reset); 106 std::optional<DisplayViewport> findViewport(const InputReaderConfiguration& readerConfig); 107 [[nodiscard]] std::list<NotifyArgs> cancelAllDownKeys(nsecs_t when); 108 void onKeyDownProcessed(nsecs_t downTime); 109 }; 110 111 } // namespace android 112