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