1 /*
2  * Copyright (c) 2023 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #ifndef COOPERATE_EVENTS_H
17 #define COOPERATE_EVENTS_H
18 
19 #include <future>
20 #include <string>
21 #include <variant>
22 
23 #include "coordination_message.h"
24 #include "i_cooperate.h"
25 #include "i_device.h"
26 
27 namespace OHOS {
28 namespace Msdp {
29 namespace DeviceStatus {
30 namespace Cooperate {
31 enum CooperateState : size_t {
32     COOPERATE_STATE_FREE = 0,
33     COOPERATE_STATE_OUT,
34     COOPERATE_STATE_IN,
35     N_COOPERATE_STATES,
36 };
37 
38 enum class CooperateEventType {
39     NOOP,
40     QUIT,
41     ADD_OBSERVER,
42     REMOVE_OBSERVER,
43     REGISTER_LISTENER,
44     UNREGISTER_LISTENER,
45     REGISTER_HOTAREA_LISTENER,
46     UNREGISTER_HOTAREA_LISTENER,
47     ENABLE,
48     DISABLE,
49     START,
50     STOP,
51     GET_COOPERATE_STATE,
52     REGISTER_EVENT_LISTENER,
53     UNREGISTER_EVENT_LISTENER,
54     DUMP,
55     APP_CLOSED,
56     DDM_BOARD_ONLINE,
57     DDM_BOARD_OFFLINE,
58     DDP_COOPERATE_SWITCH_CHANGED,
59     INPUT_HOTPLUG_EVENT,
60     INPUT_POINTER_EVENT,
61     DSOFTBUS_SESSION_OPENED,
62     DSOFTBUS_SESSION_CLOSED,
63     DSOFTBUS_START_COOPERATE,
64     DSOFTBUS_COME_BACK,
65     DSOFTBUS_STOP_COOPERATE,
66     DSOFTBUS_RELAY_COOPERATE,
67     DSOFTBUS_RELAY_COOPERATE_FINISHED,
68     DSOFTBUS_SUBSCRIBE_MOUSE_LOCATION,
69     DSOFTBUS_UNSUBSCRIBE_MOUSE_LOCATION,
70     DSOFTBUS_REPLY_SUBSCRIBE_MOUSE_LOCATION,
71     DSOFTBUS_REPLY_UNSUBSCRIBE_MOUSE_LOCATION,
72     DSOFTBUS_MOUSE_LOCATION,
73     UPDATE_COOPERATE_FLAG,
74     DSOFTBUS_INPUT_DEV_SYNC,
75     DSOFTBUS_INPUT_DEV_HOT_PLUG,
76 };
77 
78 struct Rectangle {
79     int32_t width;
80     int32_t height;
81     int32_t x;
82     int32_t y;
83 };
84 
85 struct AddObserverEvent {
86     std::shared_ptr<ICooperateObserver> observer;
87 };
88 using RemoveObserverEvent = AddObserverEvent;
89 
90 struct RegisterListenerEvent {
91     int32_t pid;
92     int32_t userData;
93 };
94 
95 using UnregisterListenerEvent = RegisterListenerEvent;
96 using RegisterHotareaListenerEvent = RegisterListenerEvent;
97 using UnregisterHotareaListenerEvent = RegisterListenerEvent;
98 using DisableCooperateEvent = RegisterListenerEvent;
99 
100 struct StartCooperateEvent {
101     int32_t pid;
102     int32_t userData;
103     std::string remoteNetworkId;
104     int32_t startDeviceId;
105     std::shared_ptr<std::promise<int32_t>> errCode;
106 };
107 
108 struct EnableCooperateEvent {
109     int32_t tokenId;
110     int32_t pid;
111     int32_t userData;
112 };
113 
114 struct ClientDiedEvent {
115     int32_t pid;
116 };
117 
118 struct StopCooperateEvent {
119     int32_t pid;
120     int32_t userData;
121     bool isUnchained;
122     std::string networkId;
123 };
124 
125 struct GetCooperateStateEvent {
126     int32_t pid;
127     int32_t userData;
128     std::string networkId;
129 };
130 
131 struct RegisterEventListenerEvent {
132     int32_t pid;
133     std::string networkId;
134 };
135 using UnregisterEventListenerEvent = RegisterEventListenerEvent;
136 
137 struct DumpEvent {
138     int32_t fd;
139 };
140 
141 struct DDMBoardOnlineEvent {
142     std::string networkId;
143     bool normal;
144     int32_t errCode { static_cast<int32_t>(CoordinationErrCode::COORDINATION_OK) };
145 };
146 
147 using DDMBoardOfflineEvent = DDMBoardOnlineEvent;
148 using DDPCooperateSwitchChanged = DDMBoardOnlineEvent;
149 
150 enum class InputHotplugType {
151     PLUG,
152     UNPLUG,
153 };
154 
155 struct InputHotplugEvent {
156     int32_t deviceId;
157     InputHotplugType type;
158     bool isKeyboard { false };
159 };
160 
161 struct InputPointerEvent {
162     int32_t deviceId;
163     int32_t pointerAction;
164     int32_t sourceType;
165     Coordinate position;
166 };
167 
168 using DSoftbusSessionOpened = DDMBoardOnlineEvent;
169 using DSoftbusSessionClosed = DDMBoardOnlineEvent;
170 
171 struct DSoftbusStartCooperate {
172     std::string networkId;
173     std::string originNetworkId;
174     bool success;
175     NormalizedCoordinate cursorPos;
176     StartCooperateData extra;
177     int32_t errCode { static_cast<int32_t>(CoordinationErrCode::COORDINATION_OK) };
178 };
179 
180 using DSoftbusStartCooperateFinished = DSoftbusStartCooperate;
181 using DSoftbusComeBack = DSoftbusStartCooperate;
182 using DSoftbusStopCooperate = DDMBoardOnlineEvent;
183 using DSoftbusStopCooperateFinished = DDMBoardOnlineEvent;
184 
185 struct DSoftbusRelayCooperate {
186     std::string networkId;
187     std::string targetNetworkId;
188     bool normal;
189 };
190 
191 struct DSoftbusSubscribeMouseLocation {
192     std::string networkId;
193     std::string remoteNetworkId;
194 };
195 
196 struct DSoftbusReplySubscribeMouseLocation {
197     std::string networkId;
198     std::string remoteNetworkId;
199     bool result { false };
200 };
201 
202 struct LocationInfo {
203     int32_t displayX;
204     int32_t displayY;
205     int32_t displayWidth;
206     int32_t displayHeight;
207 };
208 struct DSoftbusSyncMouseLocation {
209     std::string networkId;
210     std::string remoteNetworkId;
211     LocationInfo mouseLocation;
212 };
213 
214 struct DSoftbusSyncInputDevice {
215     std::string networkId;
216     std::vector<std::shared_ptr<IDevice>> devices;
217 };
218 
219 struct DSoftbusHotPlugEvent {
220     std::string networkId;
221     InputHotplugType type;
222     std::shared_ptr<IDevice> device;
223 };
224 
225 using DSoftbusReplyUnSubscribeMouseLocation = DSoftbusReplySubscribeMouseLocation;
226 using DSoftbusUnSubscribeMouseLocation = DSoftbusSubscribeMouseLocation;
227 
228 using DSoftbusRelayCooperateFinished = DSoftbusRelayCooperate;
229 
230 struct UpdateCooperateFlagEvent {
231     uint32_t mask;
232     uint32_t flag;
233 };
234 
235 struct CooperateEvent {
CooperateEventCooperateEvent236     CooperateEvent() : type(CooperateEventType::QUIT) {}
237 
CooperateEventCooperateEvent238     explicit CooperateEvent(CooperateEventType ty) : type(ty) {}
239 
240     template<typename Event>
CooperateEventCooperateEvent241     CooperateEvent(CooperateEventType ty, Event ev) : type(ty), event(ev) {}
242 
243     CooperateEventType type;
244     std::variant<
245         AddObserverEvent,
246         RegisterListenerEvent,
247         StartCooperateEvent,
248         StopCooperateEvent,
249         EnableCooperateEvent,
250         GetCooperateStateEvent,
251         RegisterEventListenerEvent,
252         DSoftbusSubscribeMouseLocation,
253         DSoftbusReplySubscribeMouseLocation,
254         DSoftbusSyncMouseLocation,
255         DumpEvent,
256         DDMBoardOnlineEvent,
257         InputHotplugEvent,
258         InputPointerEvent,
259         DSoftbusStartCooperate,
260         DSoftbusRelayCooperate,
261         ClientDiedEvent,
262         UpdateCooperateFlagEvent,
263         DSoftbusSyncInputDevice,
264         DSoftbusHotPlugEvent
265     > event;
266 };
267 
268 inline constexpr int32_t DEFAULT_TIMEOUT { 3000 };
269 inline constexpr int32_t REPEAT_ONCE { 1 };
270 inline constexpr int32_t DEFAULT_COOLING_TIME { 10 };
271 inline constexpr int32_t UP_WAIT_TIMEOUT { 100 };
272 inline constexpr uint32_t MOTION_DRAG_PRIV { 0x1 };
273 } // namespace Cooperate
274 } // namespace DeviceStatus
275 } // namespace Msdp
276 } // namespace OHOS
277 #endif // COOPERATE_EVENTS_H
278