1 /*
2  * Copyright (C) 2022 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 <vector>
20 
21 #include <input/Input.h>
22 #include <input/InputDevice.h>
23 #include <input/TouchVideoFrame.h>
24 
25 namespace android {
26 
27 /* Describes a change in any of the connected input devices. */
28 struct NotifyInputDevicesChangedArgs {
29     int32_t id;
30     std::vector<InputDeviceInfo> inputDeviceInfos;
31 
NotifyInputDevicesChangedArgsNotifyInputDevicesChangedArgs32     inline NotifyInputDevicesChangedArgs() {}
33 
34     NotifyInputDevicesChangedArgs(int32_t id, std::vector<InputDeviceInfo> infos);
35 
36     bool operator==(const NotifyInputDevicesChangedArgs& rhs) const = default;
37 
38     NotifyInputDevicesChangedArgs(const NotifyInputDevicesChangedArgs& other) = default;
39     NotifyInputDevicesChangedArgs& operator=(const NotifyInputDevicesChangedArgs&) = default;
40 };
41 
42 /* Describes a configuration change event. */
43 struct NotifyConfigurationChangedArgs {
44     int32_t id;
45     nsecs_t eventTime;
46 
NotifyConfigurationChangedArgsNotifyConfigurationChangedArgs47     inline NotifyConfigurationChangedArgs() {}
48 
49     NotifyConfigurationChangedArgs(int32_t id, nsecs_t eventTime);
50 
51     bool operator==(const NotifyConfigurationChangedArgs& rhs) const = default;
52 
53     NotifyConfigurationChangedArgs(const NotifyConfigurationChangedArgs& other) = default;
54     NotifyConfigurationChangedArgs& operator=(const NotifyConfigurationChangedArgs&) = default;
55 };
56 
57 /* Describes a key event. */
58 struct NotifyKeyArgs {
59     int32_t id;
60     nsecs_t eventTime;
61 
62     int32_t deviceId;
63     uint32_t source;
64     ui::LogicalDisplayId displayId{ui::LogicalDisplayId::INVALID};
65     uint32_t policyFlags;
66     int32_t action;
67     int32_t flags;
68     int32_t keyCode;
69     int32_t scanCode;
70     int32_t metaState;
71     nsecs_t downTime;
72     nsecs_t readTime;
73 
NotifyKeyArgsNotifyKeyArgs74     inline NotifyKeyArgs() {}
75 
76     NotifyKeyArgs(int32_t id, nsecs_t eventTime, nsecs_t readTime, int32_t deviceId,
77                   uint32_t source, ui::LogicalDisplayId displayId, uint32_t policyFlags,
78                   int32_t action, int32_t flags, int32_t keyCode, int32_t scanCode,
79                   int32_t metaState, nsecs_t downTime);
80 
81     bool operator==(const NotifyKeyArgs& rhs) const = default;
82 
83     NotifyKeyArgs(const NotifyKeyArgs& other) = default;
84     NotifyKeyArgs& operator=(const NotifyKeyArgs&) = default;
85 };
86 
87 /* Describes a motion event. */
88 struct NotifyMotionArgs {
89     int32_t id;
90     nsecs_t eventTime;
91 
92     int32_t deviceId;
93     uint32_t source;
94     ui::LogicalDisplayId displayId{ui::LogicalDisplayId::INVALID};
95     uint32_t policyFlags;
96     int32_t action;
97     int32_t actionButton;
98     int32_t flags;
99     int32_t metaState;
100     int32_t buttonState;
101     /**
102      * Classification of the current touch gesture
103      */
104     MotionClassification classification;
105     int32_t edgeFlags;
106 
107     // Vectors 'pointerProperties' and 'pointerCoords' must always have the same number of elements
108     std::vector<PointerProperties> pointerProperties;
109     std::vector<PointerCoords> pointerCoords;
110     float xPrecision;
111     float yPrecision;
112     /**
113      * Mouse cursor position when this event is reported relative to the origin of the specified
114      * display. Only valid if this is a mouse event (originates from a mouse or from a trackpad in
115      * gestures enabled mode.
116      */
117     float xCursorPosition;
118     float yCursorPosition;
119     nsecs_t downTime;
120     nsecs_t readTime;
121     std::vector<TouchVideoFrame> videoFrames;
122 
NotifyMotionArgsNotifyMotionArgs123     inline NotifyMotionArgs() {}
124 
125     NotifyMotionArgs(int32_t id, nsecs_t eventTime, nsecs_t readTime, int32_t deviceId,
126                      uint32_t source, ui::LogicalDisplayId displayId, uint32_t policyFlags,
127                      int32_t action, int32_t actionButton, int32_t flags, int32_t metaState,
128                      int32_t buttonState, MotionClassification classification, int32_t edgeFlags,
129                      uint32_t pointerCount, const PointerProperties* pointerProperties,
130                      const PointerCoords* pointerCoords, float xPrecision, float yPrecision,
131                      float xCursorPosition, float yCursorPosition, nsecs_t downTime,
132                      const std::vector<TouchVideoFrame>& videoFrames);
133 
134     NotifyMotionArgs(const NotifyMotionArgs& other) = default;
135     NotifyMotionArgs& operator=(const android::NotifyMotionArgs&) = default;
136 
137     bool operator==(const NotifyMotionArgs& rhs) const;
138 
getPointerCountNotifyMotionArgs139     inline size_t getPointerCount() const { return pointerProperties.size(); }
140 
141     std::string dump() const;
142 };
143 
144 /* Describes a sensor event. */
145 struct NotifySensorArgs {
146     int32_t id;
147     nsecs_t eventTime;
148 
149     int32_t deviceId;
150     uint32_t source;
151     InputDeviceSensorType sensorType;
152     InputDeviceSensorAccuracy accuracy;
153     bool accuracyChanged;
154     nsecs_t hwTimestamp;
155     std::vector<float> values;
156 
NotifySensorArgsNotifySensorArgs157     inline NotifySensorArgs() {}
158 
159     NotifySensorArgs(int32_t id, nsecs_t eventTime, int32_t deviceId, uint32_t source,
160                      InputDeviceSensorType sensorType, InputDeviceSensorAccuracy accuracy,
161                      bool accuracyChanged, nsecs_t hwTimestamp, std::vector<float> values);
162 
163     NotifySensorArgs(const NotifySensorArgs& other) = default;
164     NotifySensorArgs& operator=(const NotifySensorArgs&) = default;
165 };
166 
167 /* Describes a switch event. */
168 struct NotifySwitchArgs {
169     int32_t id;
170     nsecs_t eventTime;
171 
172     uint32_t policyFlags;
173     uint32_t switchValues;
174     uint32_t switchMask;
175 
NotifySwitchArgsNotifySwitchArgs176     inline NotifySwitchArgs() {}
177 
178     NotifySwitchArgs(int32_t id, nsecs_t eventTime, uint32_t policyFlags, uint32_t switchValues,
179                      uint32_t switchMask);
180 
181     NotifySwitchArgs(const NotifySwitchArgs& other) = default;
182     NotifySwitchArgs& operator=(const NotifySwitchArgs&) = default;
183 
184     bool operator==(const NotifySwitchArgs& rhs) const = default;
185 };
186 
187 /* Describes a device reset event, such as when a device is added,
188  * reconfigured, or removed. */
189 struct NotifyDeviceResetArgs {
190     int32_t id;
191     nsecs_t eventTime;
192 
193     int32_t deviceId;
194 
NotifyDeviceResetArgsNotifyDeviceResetArgs195     inline NotifyDeviceResetArgs() {}
196 
197     NotifyDeviceResetArgs(int32_t id, nsecs_t eventTime, int32_t deviceId);
198 
199     NotifyDeviceResetArgs(const NotifyDeviceResetArgs& other) = default;
200     NotifyDeviceResetArgs& operator=(const NotifyDeviceResetArgs&) = default;
201 
202     bool operator==(const NotifyDeviceResetArgs& rhs) const = default;
203 };
204 
205 /* Describes a change in the state of Pointer Capture. */
206 struct NotifyPointerCaptureChangedArgs {
207     int32_t id;
208     nsecs_t eventTime;
209 
210     PointerCaptureRequest request;
211 
NotifyPointerCaptureChangedArgsNotifyPointerCaptureChangedArgs212     inline NotifyPointerCaptureChangedArgs() {}
213 
214     NotifyPointerCaptureChangedArgs(int32_t id, nsecs_t eventTime, const PointerCaptureRequest&);
215 
216     NotifyPointerCaptureChangedArgs(const NotifyPointerCaptureChangedArgs& other) = default;
217     NotifyPointerCaptureChangedArgs& operator=(const NotifyPointerCaptureChangedArgs&) = default;
218 };
219 
220 /* Describes a vibrator state event. */
221 struct NotifyVibratorStateArgs {
222     int32_t id;
223     nsecs_t eventTime;
224 
225     int32_t deviceId;
226     bool isOn;
227 
NotifyVibratorStateArgsNotifyVibratorStateArgs228     inline NotifyVibratorStateArgs() {}
229 
230     NotifyVibratorStateArgs(int32_t id, nsecs_t eventTIme, int32_t deviceId, bool isOn);
231 
232     NotifyVibratorStateArgs(const NotifyVibratorStateArgs& other) = default;
233     NotifyVibratorStateArgs& operator=(const NotifyVibratorStateArgs&) = default;
234 };
235 
236 using NotifyArgs =
237         std::variant<NotifyInputDevicesChangedArgs, NotifyConfigurationChangedArgs, NotifyKeyArgs,
238                      NotifyMotionArgs, NotifySensorArgs, NotifySwitchArgs, NotifyDeviceResetArgs,
239                      NotifyPointerCaptureChangedArgs, NotifyVibratorStateArgs>;
240 
241 const char* toString(const NotifyArgs& args);
242 
243 } // namespace android
244