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 SWITCH_SUBSCRIBER_HANDLER_H
17 #define SWITCH_SUBSCRIBER_HANDLER_H
18 
19 #include <algorithm>
20 #include <atomic>
21 #include <list>
22 #include <map>
23 #include <memory>
24 #include <mutex>
25 #include <set>
26 #include <thread>
27 
28 #include "i_input_event_handler.h"
29 #include "key_event.h"
30 #include "switch_event.h"
31 #include "uds_server.h"
32 
33 namespace OHOS {
34 namespace MMI {
35 class SwitchSubscriberHandler final : public IInputEventHandler {
36 public:
37     SwitchSubscriberHandler() = default;
38     DISALLOW_COPY_AND_MOVE(SwitchSubscriberHandler);
39     ~SwitchSubscriberHandler() = default;
40 #ifdef OHOS_BUILD_ENABLE_KEYBOARD
41     void HandleKeyEvent(const std::shared_ptr<KeyEvent> keyEvent) override;
42 #endif // OHOS_BUILD_ENABLE_KEYBOARD
43 #ifdef OHOS_BUILD_ENABLE_POINTER
44     void HandlePointerEvent(const std::shared_ptr<PointerEvent> pointerEvent) override;
45 #endif // OHOS_BUILD_ENABLE_POINTER
46 #ifdef OHOS_BUILD_ENABLE_TOUCH
47     void HandleTouchEvent(const std::shared_ptr<PointerEvent> pointerEvent) override;
48 #endif // OHOS_BUILD_ENABLE_TOUCH
49 #ifdef OHOS_BUILD_ENABLE_SWITCH
50     void HandleSwitchEvent(const std::shared_ptr<SwitchEvent> switchEvent) override;
51 #endif // OHOS_BUILD_ENABLE_SWITCH
52     int32_t SubscribeSwitchEvent(SessionPtr sess, int32_t subscribeId, int32_t switchType);
53     int32_t UnsubscribeSwitchEvent(SessionPtr sess, int32_t subscribeId);
54     void Dump(int32_t fd, const std::vector<std::string> &args);
55 private:
56     struct Subscriber {
SubscriberSubscriber57         Subscriber(int32_t id, SessionPtr sess, int32_t switchType)
58             : id_(id), sess_(sess), switchType_(switchType), timerId_(-1) {}
59         int32_t id_ { -1 };
60         SessionPtr sess_ { nullptr };
61         int32_t switchType_ { -1 };
62         int32_t timerId_ { -1 };
63         std::shared_ptr<SwitchEvent> switchEvent_ { nullptr };
64     };
65     void InsertSubScriber(std::shared_ptr<Subscriber> subs);
66 
67 private:
68     bool OnSubscribeSwitchEvent(std::shared_ptr<SwitchEvent> keyEvent);
69     void NotifySubscriber(std::shared_ptr<SwitchEvent> keyEvent,
70         const std::shared_ptr<Subscriber> &subscriber);
71     void OnSessionDelete(SessionPtr sess);
72     bool InitSessionDeleteCallback();
73 
74 private:
75     std::list<std::shared_ptr<Subscriber>> subscribers_ {};
76     std::atomic_bool callbackInitialized_ { false };
77     std::shared_ptr<SwitchEvent> switchEvent_ { nullptr };
78 };
79 } // namespace MMI
80 } // namespace OHOS
81 #endif // SWITCH_SUBSCRIBER_HANDLER_H
82