blob: 7d9b3e44fadf47a1fe2ac4806d522e30faf77f72 [file] [log] [blame]
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001/*
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
Prabir Pradhan48108662022-09-09 21:22:04 +000017#pragma once
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070018
Henry Barnora7ed6372024-10-21 14:33:43 -070019#include <com_android_input_flags.h>
20
Prabir Pradhan84395e62022-10-26 19:52:00 +000021#include "HidUsageAccumulator.h"
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070022#include "InputMapper.h"
23
24namespace android {
25
26class KeyboardInputMapper : public InputMapper {
27public:
Arpit Singh67ca6842023-04-26 14:43:16 +000028 template <class T, class... Args>
29 friend std::unique_ptr<T> createInputMapper(InputDeviceContext& deviceContext,
30 const InputReaderConfiguration& readerConfig,
31 Args... args);
Prabir Pradhan2197cb42022-10-11 02:56:14 +000032 ~KeyboardInputMapper() override = default;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070033
Siarhei Vishniakou2935db72022-09-22 13:35:22 -070034 uint32_t getSources() const override;
Harry Cuttsd02ea102023-03-17 18:21:30 +000035 void populateDeviceInfo(InputDeviceInfo& deviceInfo) override;
Siarhei Vishniakou2935db72022-09-22 13:35:22 -070036 void dump(std::string& dump) override;
Arpit Singh4be4eef2023-03-28 14:26:01 +000037 [[nodiscard]] std::list<NotifyArgs> reconfigure(nsecs_t when,
Arpit Singhed6c3de2023-04-05 19:24:37 +000038 const InputReaderConfiguration& config,
Prabir Pradhan4bf6d452023-04-18 21:26:56 +000039 ConfigurationChanges changes) override;
Siarhei Vishniakou2935db72022-09-22 13:35:22 -070040 [[nodiscard]] std::list<NotifyArgs> reset(nsecs_t when) override;
Harry Cuttsa32a1192024-06-04 15:10:31 +000041 [[nodiscard]] std::list<NotifyArgs> process(const RawEvent& rawEvent) override;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070042
Siarhei Vishniakou2935db72022-09-22 13:35:22 -070043 int32_t getKeyCodeState(uint32_t sourceMask, int32_t keyCode) override;
44 int32_t getScanCodeState(uint32_t sourceMask, int32_t scanCode) override;
45 bool markSupportedKeyCodes(uint32_t sourceMask, const std::vector<int32_t>& keyCodes,
46 uint8_t* outFlags) override;
47 int32_t getKeyCodeForKeyLocation(int32_t locationKeyCode) const override;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070048
Siarhei Vishniakou2935db72022-09-22 13:35:22 -070049 int32_t getMetaState() override;
Linnan Li13bf76a2024-05-05 19:18:02 +080050 std::optional<ui::LogicalDisplayId> getAssociatedDisplayId() override;
Siarhei Vishniakou2935db72022-09-22 13:35:22 -070051 void updateLedState(bool reset) override;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070052
53private:
54 // The current viewport.
Prabir Pradhan2197cb42022-10-11 02:56:14 +000055 std::optional<DisplayViewport> mViewport{};
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070056
57 struct KeyDown {
Prabir Pradhan2197cb42022-10-11 02:56:14 +000058 nsecs_t downTime{};
59 int32_t keyCode{};
60 int32_t scanCode{};
Justin Chung71ddb432023-03-27 04:29:07 +000061 int32_t flags{};
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070062 };
63
Prabir Pradhan38636652024-07-23 21:59:36 +000064 // The keyboard source for this mapper. Events generated should use the source shared
65 // by all KeyboardInputMappers for this input device.
66 uint32_t mMapperSource{};
67
Zixuan Qufecb6062022-11-12 04:44:31 +000068 std::optional<KeyboardLayoutInfo> mKeyboardLayoutInfo;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070069
Prabir Pradhan2197cb42022-10-11 02:56:14 +000070 std::vector<KeyDown> mKeyDowns{}; // keys that are down
71 int32_t mMetaState{};
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070072
Prabir Pradhan84395e62022-10-26 19:52:00 +000073 HidUsageAccumulator mHidUsageAccumulator;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070074
75 struct LedState {
Prabir Pradhan2197cb42022-10-11 02:56:14 +000076 bool avail{}; // led is available
77 bool on{}; // we think the led is currently on
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070078 };
Prabir Pradhan2197cb42022-10-11 02:56:14 +000079 LedState mCapsLockLedState{};
80 LedState mNumLockLedState{};
81 LedState mScrollLockLedState{};
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070082
83 // Immutable configuration parameters.
84 struct Parameters {
Prabir Pradhan2197cb42022-10-11 02:56:14 +000085 bool orientationAware{};
86 bool handlesKeyRepeat{};
87 bool doNotWakeByDefault{};
88 } mParameters{};
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070089
Henry Barnora7ed6372024-10-21 14:33:43 -070090 // Store the value of enable wake for alphanumeric keyboard flag.
91 const bool mEnableAlphabeticKeyboardWakeFlag =
92 com::android::input::flags::enable_alphabetic_keyboard_wake();
93
Arpit Singh67ca6842023-04-26 14:43:16 +000094 KeyboardInputMapper(InputDeviceContext& deviceContext,
Vaibhav Devmurarie58ffb92024-05-22 17:38:25 +000095 const InputReaderConfiguration& readerConfig, uint32_t source);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070096 void configureParameters();
Prabir Pradhan2197cb42022-10-11 02:56:14 +000097 void dumpParameters(std::string& dump) const;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070098
Michael Wrighta9cf4192022-12-01 23:46:39 +000099 ui::Rotation getOrientation();
Linnan Li13bf76a2024-05-05 19:18:02 +0800100 ui::LogicalDisplayId getDisplayId();
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700101
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700102 [[nodiscard]] std::list<NotifyArgs> processKey(nsecs_t when, nsecs_t readTime, bool down,
103 int32_t scanCode, int32_t usageCode);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700104
105 bool updateMetaStateIfNeeded(int32_t keyCode, bool down);
106
Prabir Pradhan2197cb42022-10-11 02:56:14 +0000107 std::optional<size_t> findKeyDownIndex(int32_t scanCode);
Vaibhav Devmuraridec30802023-07-11 15:02:03 +0000108 std::optional<KeyboardLayoutInfo> getKeyboardLayoutInfo() const;
109 bool updateKeyboardLayoutOverlay();
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700110
111 void resetLedState();
112 void initializeLedState(LedState& ledState, int32_t led);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700113 void updateLedStateForModifier(LedState& ledState, int32_t led, int32_t modifier, bool reset);
Arpit Singhed6c3de2023-04-05 19:24:37 +0000114 std::optional<DisplayViewport> findViewport(const InputReaderConfiguration& readerConfig);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700115 [[nodiscard]] std::list<NotifyArgs> cancelAllDownKeys(nsecs_t when);
Arpit Singh82e413e2023-10-10 19:30:58 +0000116 void onKeyDownProcessed(nsecs_t downTime);
Prabir Pradhan38636652024-07-23 21:59:36 +0000117 uint32_t getEventSource() const;
Henry Barnora7ed6372024-10-21 14:33:43 -0700118
119 bool wakeOnAlphabeticKeyboard(const KeyboardType keyboardType) const;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700120};
121
122} // namespace android