1/*
2 * Copyright (c) 2020-2021 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#include "common/input_device_manager.h"
17#include "common/task_manager.h"
18#include "components/ui_tree_manager.h"
19#include "gfx_utils/graphic_log.h"
20
21namespace OHOS {
22InputDeviceManager* InputDeviceManager::GetInstance()
23{
24    static InputDeviceManager instance;
25    return &instance;
26}
27
28void InputDeviceManager::Init()
29{
30    UITreeManager::GetInstance().RegistViewLifeEvent(OnViewLifeEvent);
31    if (INDEV_READ_PERIOD > 0) {
32        SetPeriod(INDEV_READ_PERIOD);
33        TaskManager::GetInstance()->Add(this);
34    }
35}
36
37void InputDeviceManager::Add(InputDevice* device)
38{
39    if (device == nullptr) {
40        GRAPHIC_LOGE("InputDeviceManager::Add invalid param\n");
41        return;
42    }
43    deviceList_.PushBack(device);
44}
45
46void InputDeviceManager::Remove(InputDevice* device)
47{
48    if (device == nullptr) {
49        return;
50    }
51    ListNode<InputDevice*>* node = deviceList_.Begin();
52    while (node != deviceList_.End()) {
53        if (node->data_ == device) {
54            deviceList_.Remove(node);
55            return;
56        }
57        node = node->next_;
58    }
59}
60
61void InputDeviceManager::Callback()
62{
63    ListNode<InputDevice*>* node = deviceList_.Begin();
64    while (node != deviceList_.End()) {
65        node->data_->ProcessEvent();
66        node = node->next_;
67    }
68}
69
70void InputDeviceManager::Clear()
71{
72    deviceList_.Clear();
73}
74
75void InputDeviceManager::OnViewLifeEvent()
76{
77    auto& devices = GetInstance()->deviceList_;
78    auto node = devices.Begin();
79    while (node != devices.End()) {
80        node->data_->OnViewLifeEvent();
81        node = node->next_;
82    }
83}
84
85} // namespace OHOS
86