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 #include "input_device_manager.h"
17
18 namespace OHOS {
19 namespace MMI {
20 namespace {
21 constexpr OHOS::HiviewDFX::HiLogLabel LABEL = {LOG_CORE, MMI_LOG_DOMAIN, "InputDeviceManager"};
22 constexpr int32_t INVALID_DEVICE_ID {-1};
23 }
24
GetInputDevice(int32_t id)25 std::shared_ptr<InputDevice> InputDeviceManager::GetInputDevice(int32_t id)
26 {
27 MMI_LOGD("begin");
28 auto item = inputDevice_.find(id);
29 if (item == inputDevice_.end()) {
30 MMI_LOGE("failed to search for the device");
31 return nullptr;
32 }
33
34 std::shared_ptr<InputDevice> inputDevice = std::make_shared<InputDevice>();
35 if (inputDevice == nullptr) {
36 MMI_LOGE("create InputDevice ptr failed");
37 return nullptr;
38 }
39 inputDevice->SetId(item->first);
40 int32_t deviceType = static_cast<int32_t>(libinput_device_get_tags(item->second));
41 inputDevice->SetType(deviceType);
42 std::string name = libinput_device_get_name(item->second);
43 inputDevice->SetName(name);
44 MMI_LOGD("end");
45 return inputDevice;
46 }
47
GetInputDeviceIds()48 std::vector<int32_t> InputDeviceManager::GetInputDeviceIds()
49 {
50 MMI_LOGD("begin");
51 std::vector<int32_t> ids;
52 for (const auto &item : inputDevice_) {
53 ids.push_back(item.first);
54 }
55 MMI_LOGD("end");
56 return ids;
57 }
58
OnInputDeviceAdded(struct libinput_device * inputDevice)59 void InputDeviceManager::OnInputDeviceAdded(struct libinput_device* inputDevice)
60 {
61 MMI_LOGD("begin");
62 CHKPV(inputDevice);
63 for (const auto& item : inputDevice_) {
64 if (item.second == inputDevice) {
65 MMI_LOGI("the device already exists");
66 return;
67 }
68 }
69 if (nextId_ == INT32_MAX) {
70 MMI_LOGE("the nextId_ exceeded the upper limit");
71 return;
72 }
73 inputDevice_[nextId_] = inputDevice;
74 ++nextId_;
75
76 if (IsPointerDevice(static_cast<struct libinput_device *>(inputDevice))) {
77 NotifyPointerDevice(true);
78 }
79 MMI_LOGD("end");
80 }
81
OnInputDeviceRemoved(struct libinput_device * inputDevice)82 void InputDeviceManager::OnInputDeviceRemoved(struct libinput_device* inputDevice)
83 {
84 MMI_LOGD("begin");
85 CHKPV(inputDevice);
86 for (auto it = inputDevice_.begin(); it != inputDevice_.end(); ++it) {
87 if (it->second == inputDevice) {
88 inputDevice_.erase(it);
89 break;
90 }
91 }
92 ScanPointerDevice();
93 MMI_LOGD("end");
94 }
95
ScanPointerDevice()96 void InputDeviceManager::ScanPointerDevice()
97 {
98 bool hasPointerDevice = false;
99 for (auto it = inputDevice_.begin(); it != inputDevice_.end(); ++it) {
100 if (IsPointerDevice(it->second)) {
101 hasPointerDevice = true;
102 break;
103 }
104 }
105 if (!hasPointerDevice) {
106 NotifyPointerDevice(false);
107 }
108 }
109
IsPointerDevice(struct libinput_device * device)110 bool InputDeviceManager::IsPointerDevice(struct libinput_device* device)
111 {
112 CHKPF(device);
113 enum evdev_device_udev_tags udevTags = libinput_device_get_tags(device);
114 MMI_LOGD("udev tag:%{public}d", static_cast<int32_t>(udevTags));
115 return udevTags & (EVDEV_UDEV_TAG_MOUSE | EVDEV_UDEV_TAG_TRACKBALL | EVDEV_UDEV_TAG_POINTINGSTICK |
116 EVDEV_UDEV_TAG_TOUCHPAD | EVDEV_UDEV_TAG_TABLET_PAD);
117 }
118
Attach(std::shared_ptr<DeviceObserver> observer)119 void InputDeviceManager::Attach(std::shared_ptr<DeviceObserver> observer)
120 {
121 MMI_LOGI("begin");
122 observers_.push_back(observer);
123 }
124
Detach(std::shared_ptr<DeviceObserver> observer)125 void InputDeviceManager::Detach(std::shared_ptr<DeviceObserver> observer)
126 {
127 MMI_LOGI("begin");
128 observers_.remove(observer);
129 }
130
NotifyPointerDevice(bool hasPointerDevice)131 void InputDeviceManager::NotifyPointerDevice(bool hasPointerDevice)
132 {
133 MMI_LOGI("observers_ size:%{public}zu", observers_.size());
134 for (auto observer = observers_.begin(); observer != observers_.end(); observer++) {
135 (*observer)->UpdatePointerDevice(hasPointerDevice);
136 }
137 }
138
FindInputDeviceId(struct libinput_device * inputDevice)139 int32_t InputDeviceManager::FindInputDeviceId(struct libinput_device* inputDevice)
140 {
141 MMI_LOGD("begin");
142 CHKPR(inputDevice, INVALID_DEVICE_ID);
143 for (const auto& item : inputDevice_) {
144 if (item.second == inputDevice) {
145 MMI_LOGI("find input device id success");
146 return item.first;
147 }
148 }
149 MMI_LOGE("find input device id failed");
150 return INVALID_DEVICE_ID;
151 }
152 } // namespace MMI
153 } // namespace OHOS
154