1 /*
2  * Copyright (c) 2021-2022 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 EVENT_FILTER_HANDLER_H
17 #define EVENT_FILTER_HANDLER_H
18 
19 #include <memory>
20 #include <mutex>
21 #include <queue>
22 
23 #include "nocopyable.h"
24 
25 #include "event_filter_death_recipient.h"
26 #include "i_event_filter.h"
27 #include "i_input_event_handler.h"
28 
29 namespace OHOS {
30 namespace MMI {
31 class EventFilterHandler final : public IInputEventHandler, public std::enable_shared_from_this<EventFilterHandler> {
32 public:
33     EventFilterHandler() = default;
34     DISALLOW_COPY_AND_MOVE(EventFilterHandler);
35     ~EventFilterHandler() override = default;
36 #ifdef OHOS_BUILD_ENABLE_KEYBOARD
37     void HandleKeyEvent(const std::shared_ptr<KeyEvent> keyEvent) override;
38 #endif // OHOS_BUILD_ENABLE_KEYBOARD
39 #ifdef OHOS_BUILD_ENABLE_POINTER
40     void HandlePointerEvent(const std::shared_ptr<PointerEvent> pointerEvent) override;
41 #endif // OHOS_BUILD_ENABLE_POINTER
42 #ifdef OHOS_BUILD_ENABLE_TOUCH
43     void HandleTouchEvent(const std::shared_ptr<PointerEvent> pointerEvent) override;
44 #endif // OHOS_BUILD_ENABLE_TOUCH
45     int32_t AddInputEventFilter(sptr<IEventFilter> filter, int32_t filterId, int32_t priority, uint32_t deviceTags,
46         int32_t clientPid);
47     int32_t RemoveInputEventFilter(int32_t filterId, int32_t clientPid);
48     void Dump(int32_t fd, const std::vector<std::string> &args);
49     bool HandleKeyEventFilter(std::shared_ptr<KeyEvent> event);
50     bool HandlePointerEventFilter(std::shared_ptr<PointerEvent> event);
51 private:
52     std::mutex lockFilter_;
53     struct FilterInfo {
54         const sptr<IEventFilter> filter;
55         sptr<IRemoteObject::DeathRecipient> deathRecipient { nullptr };
56         const int32_t filterId;
57         const int32_t priority;
58         const uint32_t deviceTags;
59         const int32_t clientPid;
IsSameClientFilterInfo60         bool IsSameClient(int32_t id, int32_t pid) const { return ((filterId == id) && (clientPid == pid)); }
61     };
62     std::list<FilterInfo> filters_;
63 };
64 } // namespace MMI
65 } // namespace OHOS
66 #endif // EVENT_FILTER_HANDLER_H