blob: 686c4323a4c57ab874a56a9c5d6b21ae65a7ed0a [file] [log] [blame]
Garfield Tane84e6f92019-08-29 17:28:41 -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
Garfield Tane84e6f92019-08-29 17:28:41 -070018
19#include "CancelationOptions.h"
20#include "Entry.h"
21
22#include <utils/Timers.h>
Siarhei Vishniakou8a878352023-01-30 14:05:01 -080023#include <bitset>
Garfield Tane84e6f92019-08-29 17:28:41 -070024
Vaibhav Devmurariff798f32022-05-09 23:45:04 +000025namespace android {
26namespace inputdispatcher {
Garfield Tane84e6f92019-08-29 17:28:41 -070027
Svet Ganov5d3bc372020-01-26 23:11:07 -080028static constexpr int32_t INVALID_POINTER_INDEX = -1;
29
Garfield Tane84e6f92019-08-29 17:28:41 -070030/* Tracks dispatched key and motion event state so that cancellation events can be
31 * synthesized when events are dropped. */
32class InputState {
33public:
Garfield Tanff1f1bb2020-01-28 13:24:04 -080034 explicit InputState(const IdGenerator& idGenerator);
Garfield Tane84e6f92019-08-29 17:28:41 -070035 ~InputState();
36
Garfield Tane84e6f92019-08-29 17:28:41 -070037 // Returns true if the specified source is known to have received a hover enter
38 // motion event.
Siarhei Vishniakou7e2f8f12023-07-11 10:51:20 -070039 bool isHovering(DeviceId deviceId, uint32_t source, int32_t displayId) const;
Garfield Tane84e6f92019-08-29 17:28:41 -070040
41 // Records tracking information for a key event that has just been published.
42 // Returns true if the event should be delivered, false if it is inconsistent
43 // and should be skipped.
Siarhei Vishniakoud2770042019-10-29 11:08:14 -070044 bool trackKey(const KeyEntry& entry, int32_t action, int32_t flags);
Garfield Tane84e6f92019-08-29 17:28:41 -070045
46 // Records tracking information for a motion event that has just been published.
47 // Returns true if the event should be delivered, false if it is inconsistent
48 // and should be skipped.
Siarhei Vishniakoud2770042019-10-29 11:08:14 -070049 bool trackMotion(const MotionEntry& entry, int32_t action, int32_t flags);
Garfield Tane84e6f92019-08-29 17:28:41 -070050
Siarhei Vishniakou2899c552023-07-10 18:20:46 -070051 // Create cancel events for the previous stream if the current motionEntry requires it.
52 std::unique_ptr<EventEntry> cancelConflictingInputStream(const MotionEntry& motionEntry,
53 int32_t resolvedAction);
54
Garfield Tane84e6f92019-08-29 17:28:41 -070055 // Synthesizes cancelation events for the current state and resets the tracked state.
Siarhei Vishniakoua9a7ee82019-10-14 16:28:19 -070056 std::vector<std::unique_ptr<EventEntry>> synthesizeCancelationEvents(
57 nsecs_t currentTime, const CancelationOptions& options);
Garfield Tane84e6f92019-08-29 17:28:41 -070058
Svet Ganov5d3bc372020-01-26 23:11:07 -080059 // Synthesizes down events for the current state.
Siarhei Vishniakoua9a7ee82019-10-14 16:28:19 -070060 std::vector<std::unique_ptr<EventEntry>> synthesizePointerDownEvents(nsecs_t currentTime);
Svet Ganov5d3bc372020-01-26 23:11:07 -080061
Garfield Tane84e6f92019-08-29 17:28:41 -070062 // Clears the current state.
63 void clear();
64
Svet Ganov5d3bc372020-01-26 23:11:07 -080065 // Merges pointer-related parts of the input state into another instance.
66 void mergePointerStateTo(InputState& other);
Garfield Tane84e6f92019-08-29 17:28:41 -070067
68 // Gets the fallback key associated with a keycode.
Siarhei Vishniakou0fe01262023-04-17 08:11:37 -070069 // Returns std::nullopt if none.
Garfield Tane84e6f92019-08-29 17:28:41 -070070 // Returns AKEYCODE_UNKNOWN if we are only dispatching the unhandled key to the policy.
Siarhei Vishniakou0fe01262023-04-17 08:11:37 -070071 std::optional<int32_t> getFallbackKey(int32_t originalKeyCode);
Garfield Tane84e6f92019-08-29 17:28:41 -070072
73 // Sets the fallback key for a particular keycode.
74 void setFallbackKey(int32_t originalKeyCode, int32_t fallbackKeyCode);
75
76 // Removes the fallback key for a particular keycode.
77 void removeFallbackKey(int32_t originalKeyCode);
78
Siarhei Vishniakou0fe01262023-04-17 08:11:37 -070079 inline const std::map<int32_t, int32_t>& getFallbackKeys() const { return mFallbackKeys; }
Garfield Tane84e6f92019-08-29 17:28:41 -070080
81private:
82 struct KeyMemento {
Siarhei Vishniakou7e2f8f12023-07-11 10:51:20 -070083 DeviceId deviceId;
Garfield Tane84e6f92019-08-29 17:28:41 -070084 uint32_t source;
85 int32_t displayId;
86 int32_t keyCode;
87 int32_t scanCode;
88 int32_t metaState;
89 int32_t flags;
90 nsecs_t downTime;
91 uint32_t policyFlags;
92 };
93
94 struct MotionMemento {
Siarhei Vishniakou7e2f8f12023-07-11 10:51:20 -070095 DeviceId deviceId;
Garfield Tane84e6f92019-08-29 17:28:41 -070096 uint32_t source;
97 int32_t displayId;
98 int32_t flags;
99 float xPrecision;
100 float yPrecision;
101 float xCursorPosition;
102 float yCursorPosition;
103 nsecs_t downTime;
Siarhei Vishniakou47a02a12023-10-18 09:56:00 -0700104 std::vector<PointerProperties> pointerProperties;
105 std::vector<PointerCoords> pointerCoords;
Svet Ganov5d3bc372020-01-26 23:11:07 -0800106 // Track for which pointers the target doesn't know about.
107 int32_t firstNewPointerIdx = INVALID_POINTER_INDEX;
Garfield Tane84e6f92019-08-29 17:28:41 -0700108 bool hovering;
109 uint32_t policyFlags;
110
Siarhei Vishniakoud2770042019-10-29 11:08:14 -0700111 void setPointers(const MotionEntry& entry);
Svet Ganov5d3bc372020-01-26 23:11:07 -0800112 void mergePointerStateTo(MotionMemento& other) const;
Siarhei Vishniakou47a02a12023-10-18 09:56:00 -0700113 size_t getPointerCount() const;
Garfield Tane84e6f92019-08-29 17:28:41 -0700114 };
115
Garfield Tanff1f1bb2020-01-28 13:24:04 -0800116 const IdGenerator& mIdGenerator; // InputDispatcher owns it so we won't have dangling reference.
117
Garfield Tane84e6f92019-08-29 17:28:41 -0700118 std::vector<KeyMemento> mKeyMementos;
119 std::vector<MotionMemento> mMotionMementos;
Siarhei Vishniakou0fe01262023-04-17 08:11:37 -0700120 std::map</*originalKeyCode*/int32_t, /*fallbackKeyCode*/int32_t> mFallbackKeys;
Garfield Tane84e6f92019-08-29 17:28:41 -0700121
Siarhei Vishniakoud2770042019-10-29 11:08:14 -0700122 ssize_t findKeyMemento(const KeyEntry& entry) const;
123 ssize_t findMotionMemento(const MotionEntry& entry, bool hovering) const;
Garfield Tane84e6f92019-08-29 17:28:41 -0700124
Siarhei Vishniakoud2770042019-10-29 11:08:14 -0700125 void addKeyMemento(const KeyEntry& entry, int32_t flags);
126 void addMotionMemento(const MotionEntry& entry, int32_t flags, bool hovering);
Garfield Tane84e6f92019-08-29 17:28:41 -0700127
128 static bool shouldCancelKey(const KeyMemento& memento, const CancelationOptions& options);
129 static bool shouldCancelMotion(const MotionMemento& memento, const CancelationOptions& options);
Siarhei Vishniakou2899c552023-07-10 18:20:46 -0700130 bool shouldCancelPreviousStream(const MotionEntry& motionEntry, int32_t resolvedAction) const;
131 std::unique_ptr<MotionEntry> createCancelEntryForMemento(const MotionMemento& memento,
132 nsecs_t eventTime) const;
Vaibhav Devmurariff798f32022-05-09 23:45:04 +0000133
134 // Synthesizes pointer cancel events for a particular set of pointers.
135 std::vector<std::unique_ptr<MotionEntry>> synthesizeCancelationEventsForPointers(
Siarhei Vishniakou8a878352023-01-30 14:05:01 -0800136 const MotionMemento& memento, std::bitset<MAX_POINTER_ID + 1> pointerIds,
137 nsecs_t currentTime);
Siarhei Vishniakoud38a1e02023-07-18 11:55:17 -0700138 friend std::ostream& operator<<(std::ostream& out, const InputState& state);
Garfield Tane84e6f92019-08-29 17:28:41 -0700139};
140
Siarhei Vishniakoud38a1e02023-07-18 11:55:17 -0700141std::ostream& operator<<(std::ostream& out, const InputState& state);
142
Vaibhav Devmurariff798f32022-05-09 23:45:04 +0000143} // namespace inputdispatcher
144} // namespace android