1c29fa5a6Sopenharmony_ci/*
2c29fa5a6Sopenharmony_ci * Copyright (c) 2023 Huawei Device Co., Ltd.
3c29fa5a6Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4c29fa5a6Sopenharmony_ci * you may not use this file except in compliance with the License.
5c29fa5a6Sopenharmony_ci * You may obtain a copy of the License at
6c29fa5a6Sopenharmony_ci *
7c29fa5a6Sopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0
8c29fa5a6Sopenharmony_ci *
9c29fa5a6Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10c29fa5a6Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11c29fa5a6Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12c29fa5a6Sopenharmony_ci * See the License for the specific language governing permissions and
13c29fa5a6Sopenharmony_ci * limitations under the License.
14c29fa5a6Sopenharmony_ci */
15c29fa5a6Sopenharmony_ci
16c29fa5a6Sopenharmony_ci#ifndef SWITCH_SUBSCRIBER_HANDLER_H
17c29fa5a6Sopenharmony_ci#define SWITCH_SUBSCRIBER_HANDLER_H
18c29fa5a6Sopenharmony_ci
19c29fa5a6Sopenharmony_ci#include <algorithm>
20c29fa5a6Sopenharmony_ci#include <atomic>
21c29fa5a6Sopenharmony_ci#include <list>
22c29fa5a6Sopenharmony_ci#include <map>
23c29fa5a6Sopenharmony_ci#include <memory>
24c29fa5a6Sopenharmony_ci#include <mutex>
25c29fa5a6Sopenharmony_ci#include <set>
26c29fa5a6Sopenharmony_ci#include <thread>
27c29fa5a6Sopenharmony_ci
28c29fa5a6Sopenharmony_ci#include "i_input_event_handler.h"
29c29fa5a6Sopenharmony_ci#include "key_event.h"
30c29fa5a6Sopenharmony_ci#include "switch_event.h"
31c29fa5a6Sopenharmony_ci#include "uds_server.h"
32c29fa5a6Sopenharmony_ci
33c29fa5a6Sopenharmony_cinamespace OHOS {
34c29fa5a6Sopenharmony_cinamespace MMI {
35c29fa5a6Sopenharmony_ciclass SwitchSubscriberHandler final : public IInputEventHandler {
36c29fa5a6Sopenharmony_cipublic:
37c29fa5a6Sopenharmony_ci    SwitchSubscriberHandler() = default;
38c29fa5a6Sopenharmony_ci    DISALLOW_COPY_AND_MOVE(SwitchSubscriberHandler);
39c29fa5a6Sopenharmony_ci    ~SwitchSubscriberHandler() = default;
40c29fa5a6Sopenharmony_ci#ifdef OHOS_BUILD_ENABLE_KEYBOARD
41c29fa5a6Sopenharmony_ci    void HandleKeyEvent(const std::shared_ptr<KeyEvent> keyEvent) override;
42c29fa5a6Sopenharmony_ci#endif // OHOS_BUILD_ENABLE_KEYBOARD
43c29fa5a6Sopenharmony_ci#ifdef OHOS_BUILD_ENABLE_POINTER
44c29fa5a6Sopenharmony_ci    void HandlePointerEvent(const std::shared_ptr<PointerEvent> pointerEvent) override;
45c29fa5a6Sopenharmony_ci#endif // OHOS_BUILD_ENABLE_POINTER
46c29fa5a6Sopenharmony_ci#ifdef OHOS_BUILD_ENABLE_TOUCH
47c29fa5a6Sopenharmony_ci    void HandleTouchEvent(const std::shared_ptr<PointerEvent> pointerEvent) override;
48c29fa5a6Sopenharmony_ci#endif // OHOS_BUILD_ENABLE_TOUCH
49c29fa5a6Sopenharmony_ci#ifdef OHOS_BUILD_ENABLE_SWITCH
50c29fa5a6Sopenharmony_ci    void HandleSwitchEvent(const std::shared_ptr<SwitchEvent> switchEvent) override;
51c29fa5a6Sopenharmony_ci#endif // OHOS_BUILD_ENABLE_SWITCH
52c29fa5a6Sopenharmony_ci    int32_t SubscribeSwitchEvent(SessionPtr sess, int32_t subscribeId, int32_t switchType);
53c29fa5a6Sopenharmony_ci    int32_t UnsubscribeSwitchEvent(SessionPtr sess, int32_t subscribeId);
54c29fa5a6Sopenharmony_ci    void Dump(int32_t fd, const std::vector<std::string> &args);
55c29fa5a6Sopenharmony_ciprivate:
56c29fa5a6Sopenharmony_ci    struct Subscriber {
57c29fa5a6Sopenharmony_ci        Subscriber(int32_t id, SessionPtr sess, int32_t switchType)
58c29fa5a6Sopenharmony_ci            : id_(id), sess_(sess), switchType_(switchType), timerId_(-1) {}
59c29fa5a6Sopenharmony_ci        int32_t id_ { -1 };
60c29fa5a6Sopenharmony_ci        SessionPtr sess_ { nullptr };
61c29fa5a6Sopenharmony_ci        int32_t switchType_ { -1 };
62c29fa5a6Sopenharmony_ci        int32_t timerId_ { -1 };
63c29fa5a6Sopenharmony_ci        std::shared_ptr<SwitchEvent> switchEvent_ { nullptr };
64c29fa5a6Sopenharmony_ci    };
65c29fa5a6Sopenharmony_ci    void InsertSubScriber(std::shared_ptr<Subscriber> subs);
66c29fa5a6Sopenharmony_ci
67c29fa5a6Sopenharmony_ciprivate:
68c29fa5a6Sopenharmony_ci    bool OnSubscribeSwitchEvent(std::shared_ptr<SwitchEvent> keyEvent);
69c29fa5a6Sopenharmony_ci    void NotifySubscriber(std::shared_ptr<SwitchEvent> keyEvent,
70c29fa5a6Sopenharmony_ci        const std::shared_ptr<Subscriber> &subscriber);
71c29fa5a6Sopenharmony_ci    void OnSessionDelete(SessionPtr sess);
72c29fa5a6Sopenharmony_ci    bool InitSessionDeleteCallback();
73c29fa5a6Sopenharmony_ci
74c29fa5a6Sopenharmony_ciprivate:
75c29fa5a6Sopenharmony_ci    std::list<std::shared_ptr<Subscriber>> subscribers_ {};
76c29fa5a6Sopenharmony_ci    std::atomic_bool callbackInitialized_ { false };
77c29fa5a6Sopenharmony_ci    std::shared_ptr<SwitchEvent> switchEvent_ { nullptr };
78c29fa5a6Sopenharmony_ci};
79c29fa5a6Sopenharmony_ci} // namespace MMI
80c29fa5a6Sopenharmony_ci} // namespace OHOS
81c29fa5a6Sopenharmony_ci#endif // SWITCH_SUBSCRIBER_HANDLER_H
82