1 /*
2 * Copyright (c) 2021-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 #include "distributed_input_node_manager.h"
17
18 #include <cinttypes>
19 #include <cstring>
20
21 #include <pthread.h>
22
23 #include "softbus_bus_center.h"
24
25 #include "dinput_context.h"
26 #include "dinput_errcode.h"
27 #include "dinput_log.h"
28 #include "dinput_softbus_define.h"
29 #include "dinput_utils_tool.h"
30
31 namespace OHOS {
32 namespace DistributedHardware {
33 namespace DistributedInput {
DistributedInputNodeManager()34 DistributedInputNodeManager::DistributedInputNodeManager() : isInjectThreadCreated_(false),
35 isInjectThreadRunning_(false), inputHub_(std::make_unique<InputHub>()), virtualTouchScreenFd_(UN_INIT_FD_VALUE)
36 {
37 DHLOGI("DistributedInputNodeManager ctor");
38 std::shared_ptr<AppExecFwk::EventRunner> runner = AppExecFwk::EventRunner::Create(true);
39 callBackHandler_ = std::make_shared<DistributedInputNodeManager::DInputNodeManagerEventHandler>(runner, this);
40 }
41
~DistributedInputNodeManager()42 DistributedInputNodeManager::~DistributedInputNodeManager()
43 {
44 DHLOGI("DistributedInputNodeManager dtor");
45 isInjectThreadCreated_.store(false);
46 isInjectThreadRunning_.store(false);
47 if (eventInjectThread_.joinable()) {
48 eventInjectThread_.join();
49 }
50 {
51 std::lock_guard<std::mutex> lock(virtualDeviceMapMutex_);
52 virtualDeviceMap_.clear();
53 }
54 DHLOGI("destructor end");
55 }
56
OpenDevicesNode(const std::string & devId,const std::string & dhId,const std::string & parameters)57 int32_t DistributedInputNodeManager::OpenDevicesNode(const std::string &devId, const std::string &dhId,
58 const std::string ¶meters)
59 {
60 if (devId.size() > DEV_ID_LENGTH_MAX || devId.empty() || dhId.size() > DH_ID_LENGTH_MAX || dhId.empty() ||
61 parameters.size() > STRING_MAX_SIZE || parameters.empty()) {
62 DHLOGE("Params is invalid!");
63 return ERR_DH_INPUT_SERVER_SOURCE_OPEN_DEVICE_NODE_FAIL;
64 }
65 InputDevice event;
66 ParseInputDeviceJson(parameters, event);
67 if (CreateHandle(event, devId, dhId) < 0) {
68 DHLOGE("Can not create virtual node!");
69 return ERR_DH_INPUT_SERVER_SOURCE_OPEN_DEVICE_NODE_FAIL;
70 }
71 return DH_SUCCESS;
72 }
73
ParseInputDeviceJson(const std::string & str,InputDevice & pBuf)74 void DistributedInputNodeManager::ParseInputDeviceJson(const std::string &str, InputDevice &pBuf)
75 {
76 nlohmann::json inputDeviceJson = nlohmann::json::parse(str, nullptr, false);
77 if (inputDeviceJson.is_discarded()) {
78 DHLOGE("recMsg parse failed!");
79 return;
80 }
81 VerifyInputDevice(inputDeviceJson, pBuf);
82 }
83
VerifyInputDevice(const nlohmann::json & inputDeviceJson,InputDevice & pBuf)84 void DistributedInputNodeManager::VerifyInputDevice(const nlohmann::json &inputDeviceJson, InputDevice &pBuf)
85 {
86 if (IsString(inputDeviceJson, DEVICE_NAME)) {
87 pBuf.name = inputDeviceJson[DEVICE_NAME].get<std::string>();
88 }
89 if (IsString(inputDeviceJson, PHYSICAL_PATH)) {
90 pBuf.physicalPath = inputDeviceJson[PHYSICAL_PATH].get<std::string>();
91 }
92 if (IsString(inputDeviceJson, UNIQUE_ID)) {
93 pBuf.uniqueId = inputDeviceJson[UNIQUE_ID].get<std::string>();
94 }
95 if (IsUInt16(inputDeviceJson, BUS)) {
96 pBuf.bus = inputDeviceJson[BUS].get<uint16_t>();
97 }
98 if (IsUInt16(inputDeviceJson, VENDOR)) {
99 pBuf.vendor = inputDeviceJson[VENDOR].get<uint16_t>();
100 }
101 if (IsUInt16(inputDeviceJson, PRODUCT)) {
102 pBuf.product = inputDeviceJson[PRODUCT].get<uint16_t>();
103 }
104 if (IsUInt16(inputDeviceJson, VERSION)) {
105 pBuf.version = inputDeviceJson[VERSION].get<uint16_t>();
106 }
107 if (IsString(inputDeviceJson, DESCRIPTOR)) {
108 pBuf.descriptor = inputDeviceJson[DESCRIPTOR].get<std::string>();
109 }
110 if (IsUInt32(inputDeviceJson, CLASSES)) {
111 pBuf.classes = inputDeviceJson[CLASSES].get<uint32_t>();
112 }
113 if (IsArray(inputDeviceJson, EVENT_TYPES)) {
114 pBuf.eventTypes = inputDeviceJson[EVENT_TYPES].get<std::vector<uint32_t>>();
115 }
116 if (IsArray(inputDeviceJson, EVENT_KEYS)) {
117 pBuf.eventKeys = inputDeviceJson[EVENT_KEYS].get<std::vector<uint32_t>>();
118 }
119 if (IsArray(inputDeviceJson, ABS_TYPES)) {
120 pBuf.absTypes = inputDeviceJson[ABS_TYPES].get<std::vector<uint32_t>>();
121 }
122 if (IsArray(inputDeviceJson, ABS_INFOS)) {
123 pBuf.absInfos = inputDeviceJson[ABS_INFOS].get<std::map<uint32_t, std::vector<int32_t>>>();
124 }
125 if (IsArray(inputDeviceJson, REL_TYPES)) {
126 pBuf.relTypes = inputDeviceJson[REL_TYPES].get<std::vector<uint32_t>>();
127 }
128 if (IsArray(inputDeviceJson, PROPERTIES)) {
129 pBuf.properties = inputDeviceJson[PROPERTIES].get<std::vector<uint32_t>>();
130 }
131 }
132
ScanSinkInputDevices(const std::string & dhId)133 void DistributedInputNodeManager::ScanSinkInputDevices(const std::string &dhId)
134 {
135 DHLOGI("ScanSinkInputDevices enter, dhId %s.", dhId.c_str());
136 std::vector<std::string> vecInputDevPath;
137 ScanInputDevicesPath(DEVICE_PATH, vecInputDevPath);
138 for (auto &tempPath: vecInputDevPath) {
139 OpenInputDevice(tempPath, dhId);
140 }
141 }
142
ProcessEvent(const AppExecFwk::InnerEvent::Pointer & event)143 void DistributedInputNodeManager::DInputNodeManagerEventHandler::ProcessEvent(
144 const AppExecFwk::InnerEvent::Pointer &event)
145 {
146 DHLOGI("ProcessEvent enter.");
147 auto iter = eventFuncMap_.find(event->GetInnerEventId());
148 if (iter == eventFuncMap_.end()) {
149 DHLOGE("Event Id %d is undefined.", event->GetInnerEventId());
150 return;
151 }
152 nodeMgrFunc &func = iter->second;
153 (this->*func)(event);
154 }
155
DInputNodeManagerEventHandler(const std::shared_ptr<AppExecFwk::EventRunner> & runner,DistributedInputNodeManager * manager)156 DistributedInputNodeManager::DInputNodeManagerEventHandler::DInputNodeManagerEventHandler(
157 const std::shared_ptr<AppExecFwk::EventRunner> &runner, DistributedInputNodeManager *manager)
158 : AppExecFwk::EventHandler(runner)
159 {
160 eventFuncMap_[DINPUT_NODE_MANAGER_SCAN_ALL_NODE] = &DInputNodeManagerEventHandler::ScanAllNode;
161
162 nodeManagerObj_ = manager;
163 }
164
~DInputNodeManagerEventHandler()165 DistributedInputNodeManager::DInputNodeManagerEventHandler::~DInputNodeManagerEventHandler()
166 {
167 eventFuncMap_.clear();
168 nodeManagerObj_ = nullptr;
169 }
170
ScanAllNode(const AppExecFwk::InnerEvent::Pointer & event)171 void DistributedInputNodeManager::DInputNodeManagerEventHandler::ScanAllNode(
172 const AppExecFwk::InnerEvent::Pointer &event)
173 {
174 DHLOGI("ScanAllNode enter.");
175 std::shared_ptr<nlohmann::json> dataMsg = event->GetSharedObject<nlohmann::json>();
176 auto it = dataMsg->begin();
177 nlohmann::json innerMsg = *(it);
178 std::string devicedhId = innerMsg[INPUT_NODE_DHID];
179 nodeManagerObj_->ScanSinkInputDevices(devicedhId);
180 }
181
NotifyNodeMgrScanVirNode(const std::string & dhId)182 void DistributedInputNodeManager::NotifyNodeMgrScanVirNode(const std::string &dhId)
183 {
184 DHLOGI("NotifyNodeMgrScanVirNode enter.");
185 std::shared_ptr<nlohmann::json> jsonArrayMsg = std::make_shared<nlohmann::json>();
186 nlohmann::json tmpJson;
187 tmpJson[INPUT_NODE_DHID] = dhId;
188 jsonArrayMsg->push_back(tmpJson);
189 AppExecFwk::InnerEvent::Pointer msgEvent = AppExecFwk::InnerEvent::Get(
190 DINPUT_NODE_MANAGER_SCAN_ALL_NODE, jsonArrayMsg, 0);
191 callBackHandler_->SendEvent(msgEvent, 0, AppExecFwk::EventQueue::Priority::IMMEDIATE);
192 }
193
IsVirtualDev(int fd)194 bool DistributedInputNodeManager::IsVirtualDev(int fd)
195 {
196 char buffer[INPUT_EVENT_BUFFER_SIZE] = {0};
197 std::string deviceName;
198 if (ioctl(fd, EVIOCGNAME(sizeof(buffer) - 1), &buffer) < 1) {
199 DHLOGE("Could not get device name for %s.", ConvertErrNo().c_str());
200 return false;
201 }
202 buffer[sizeof(buffer) - 1] = '\0';
203 deviceName = buffer;
204
205 DHLOGD("IsVirtualDev deviceName: %s", buffer);
206 if (deviceName.find(VIRTUAL_DEVICE_NAME) == std::string::npos) {
207 DHLOGD("This is not a virtual device, fd %d, deviceName: %s.", fd, deviceName.c_str());
208 return false;
209 }
210 return true;
211 }
212
GetDevDhIdByFd(int fd,std::string & dhId,std::string & physicalPath)213 bool DistributedInputNodeManager::GetDevDhIdByFd(int fd, std::string &dhId, std::string &physicalPath)
214 {
215 char buffer[INPUT_EVENT_BUFFER_SIZE] = {0};
216 if (ioctl(fd, EVIOCGPHYS(sizeof(buffer) - 1), &buffer) < 1) {
217 DHLOGE("Could not get device physicalPath for %s.", ConvertErrNo().c_str());
218 return false;
219 }
220 buffer[sizeof(buffer) - 1] = '\0';
221 physicalPath = buffer;
222
223 DHLOGD("GetDevDhIdByFd physicalPath %s.", physicalPath.c_str());
224 dhId = physicalPath.substr(physicalPath.find(DH_ID_PREFIX));
225 if (dhId.size() == 0) {
226 DHLOGE("Get dev dhid failed.");
227 return false;
228 }
229 DHLOGD("Device dhId %s.", GetAnonyString(dhId).c_str());
230 return true;
231 }
232
SetPathForDevMap(std::string & dhId,const std::string & devicePath)233 void DistributedInputNodeManager::SetPathForDevMap(std::string &dhId, const std::string &devicePath)
234 {
235 std::lock_guard<std::mutex> lock(virtualDeviceMapMutex_);
236 auto iter = virtualDeviceMap_.begin();
237 while (iter != virtualDeviceMap_.end()) {
238 DHLOGD("Virtual device map dhid %s.", iter->first.c_str());
239 if (dhId.compare(iter->first) == 0) {
240 DHLOGD("Found the virtual device, set path :%s", devicePath.c_str());
241 iter->second->SetPath(devicePath);
242 break;
243 }
244 iter++;
245 }
246 }
247
OpenInputDevice(const std::string & devicePath,const std::string & dhId)248 void DistributedInputNodeManager::OpenInputDevice(const std::string &devicePath, const std::string &dhId)
249 {
250 DHLOGI("Opening input device path: %s", devicePath.c_str());
251 std::string curDhId;
252 std::string physicalPath;
253 int fd = OpenInputDeviceFdByPath(devicePath);
254 if (fd == UN_INIT_FD_VALUE) {
255 DHLOGE("The fd open failed, devicePath %s.", devicePath.c_str());
256 return;
257 }
258 if (!IsVirtualDev(fd)) {
259 DHLOGE("The dev not virtual, devicePath %s.", devicePath.c_str());
260 return;
261 }
262 if (!GetDevDhIdByFd(fd, curDhId, physicalPath) || dhId != curDhId) {
263 DHLOGE("This is not same dev, curDhId %s.", devicePath.c_str());
264 return;
265 }
266 DHLOGD("curDhId %s.", GetAnonyString(curDhId).c_str());
267 SetPathForDevMap(curDhId, devicePath);
268 }
269
GetVirtualKeyboardPathsByDhIds(const std::vector<std::string> & dhIds,std::vector<std::string> & shareDhidsPaths,std::vector<std::string> & shareDhIds)270 void DistributedInputNodeManager::GetVirtualKeyboardPathsByDhIds(const std::vector<std::string> &dhIds,
271 std::vector<std::string> &shareDhidsPaths, std::vector<std::string> &shareDhIds)
272 {
273 std::lock_guard<std::mutex> lock(virtualDeviceMapMutex_);
274 for (auto dhId_ : dhIds) {
275 auto iter = virtualDeviceMap_.begin();
276 while (iter != virtualDeviceMap_.end()) {
277 if (iter->second == nullptr) {
278 DHLOGE("device is nullptr");
279 continue;
280 }
281 if ((iter->first.compare(dhId_) == 0) &&
282 ((iter->second->GetClasses() & INPUT_DEVICE_CLASS_KEYBOARD) != 0)) {
283 DHLOGI("Found vir keyboard path %s, dhid %s", iter->second->GetPath().c_str(),
284 GetAnonyString(dhId_).c_str());
285 shareDhidsPaths.push_back(iter->second->GetPath());
286 shareDhIds.push_back(dhId_);
287 }
288 iter++;
289 }
290 }
291 }
292
CreateHandle(const InputDevice & inputDevice,const std::string & devId,const std::string & dhId)293 int32_t DistributedInputNodeManager::CreateHandle(const InputDevice &inputDevice, const std::string &devId,
294 const std::string &dhId)
295 {
296 std::unique_lock<std::mutex> my_lock(operationMutex_);
297 std::call_once(callOnceFlag_, [this]() { inputHub_->ScanInputDevices(DEVICE_PATH); });
298 std::unique_ptr<VirtualDevice> virtualDevice = std::make_unique<VirtualDevice>(inputDevice);
299
300 virtualDevice->SetNetWorkId(devId);
301
302 if (!virtualDevice->SetUp(inputDevice, devId, dhId)) {
303 DHLOGE("could not create new virtual device\n");
304 return ERR_DH_INPUT_SERVER_SOURCE_CREATE_HANDLE_FAIL;
305 }
306 AddDeviceLocked(inputDevice.descriptor, std::move(virtualDevice));
307 return DH_SUCCESS;
308 }
309
CreateVirtualTouchScreenNode(const std::string & devId,const std::string & dhId,const uint64_t srcWinId,const uint32_t sourcePhyWidth,const uint32_t sourcePhyHeight)310 int32_t DistributedInputNodeManager::CreateVirtualTouchScreenNode(const std::string &devId, const std::string &dhId,
311 const uint64_t srcWinId, const uint32_t sourcePhyWidth, const uint32_t sourcePhyHeight)
312 {
313 std::unique_lock<std::mutex> my_lock(operationMutex_);
314 std::unique_ptr<VirtualDevice> device;
315 LocalAbsInfo info = DInputContext::GetInstance().GetLocalTouchScreenInfo().localAbsInfo;
316 DHLOGI("CreateVirtualTouchScreenNode start, dhId: %s, sourcePhyWidth: %d, sourcePhyHeight: %d",
317 GetAnonyString(dhId).c_str(), sourcePhyWidth, sourcePhyHeight);
318 device = std::make_unique<VirtualDevice>(info.deviceInfo);
319 if (!device->SetUp(info.deviceInfo, devId, dhId)) {
320 DHLOGE("Virtual touch Screen setUp fail, devId: %s, dhId: %s", GetAnonyString(devId).c_str(),
321 GetAnonyString(dhId).c_str());
322 return ERR_DH_INPUT_SERVER_SOURCE_CREATE_HANDLE_FAIL;
323 }
324 virtualTouchScreenFd_ = device->GetDeviceFd();
325 AddDeviceLocked(dhId, std::move(device));
326 DHLOGI("CreateVirtualTouchScreenNode end, dhId: %s", GetAnonyString(dhId).c_str());
327 return DH_SUCCESS;
328 }
329
RemoveVirtualTouchScreenNode(const std::string & dhId)330 int32_t DistributedInputNodeManager::RemoveVirtualTouchScreenNode(const std::string &dhId)
331 {
332 return CloseDeviceLocked(dhId);
333 }
334
GetVirtualTouchScreenFd()335 int32_t DistributedInputNodeManager::GetVirtualTouchScreenFd()
336 {
337 return virtualTouchScreenFd_;
338 }
339
AddDeviceLocked(const std::string & dhId,std::unique_ptr<VirtualDevice> device)340 void DistributedInputNodeManager::AddDeviceLocked(const std::string &dhId, std::unique_ptr<VirtualDevice> device)
341 {
342 DHLOGI("dhId=%s", GetAnonyString(dhId).c_str());
343 std::lock_guard<std::mutex> lock(virtualDeviceMapMutex_);
344 auto [dev_it, inserted] = virtualDeviceMap_.insert_or_assign(dhId, std::move(device));
345 if (!inserted) {
346 DHLOGI("Device id %s exists, replaced. \n", GetAnonyString(dhId).c_str());
347 }
348 }
349
CloseDeviceLocked(const std::string & dhId)350 int32_t DistributedInputNodeManager::CloseDeviceLocked(const std::string &dhId)
351 {
352 DHLOGI("CloseDeviceLocked called, dhId=%s", GetAnonyString(dhId).c_str());
353 std::lock_guard<std::mutex> lock(virtualDeviceMapMutex_);
354 std::map<std::string, std::unique_ptr<VirtualDevice>>::iterator iter = virtualDeviceMap_.find(dhId);
355 if (iter != virtualDeviceMap_.end()) {
356 DHLOGI("CloseDeviceLocked called success, dhId=%s", GetAnonyString(dhId).c_str());
357 virtualDeviceMap_.erase(iter);
358 return DH_SUCCESS;
359 }
360 DHLOGE("CloseDeviceLocked called failure, dhId=%s", GetAnonyString(dhId).c_str());
361 return ERR_DH_INPUT_SERVER_SOURCE_CLOSE_DEVICE_FAIL;
362 }
363
GetDevice(const std::string & dhId,VirtualDevice * & device)364 int32_t DistributedInputNodeManager::GetDevice(const std::string &dhId, VirtualDevice *&device)
365 {
366 std::lock_guard<std::mutex> lock(virtualDeviceMapMutex_);
367 auto iter = virtualDeviceMap_.find(dhId);
368 if (iter != virtualDeviceMap_.end()) {
369 device = iter->second.get();
370 return DH_SUCCESS;
371 }
372 return ERR_DH_INPUT_SERVER_SOURCE_GET_DEVICE_FAIL;
373 }
374
StartInjectThread()375 void DistributedInputNodeManager::StartInjectThread()
376 {
377 if (isInjectThreadCreated_.load()) {
378 DHLOGI("InjectThread has been created.");
379 return;
380 }
381 DHLOGI("InjectThread does not created");
382 isInjectThreadCreated_.store(true);
383 isInjectThreadRunning_.store(true);
384 eventInjectThread_ = std::thread(&DistributedInputNodeManager::InjectEvent, this);
385 }
386
StopInjectThread()387 void DistributedInputNodeManager::StopInjectThread()
388 {
389 if (!isInjectThreadCreated_.load()) {
390 DHLOGI("InjectThread does not created, and not need to stop.");
391 }
392 DHLOGI("InjectThread has been created, and soon will be stopped.");
393 isInjectThreadRunning_.store(false);
394 isInjectThreadCreated_.store(false);
395 conditionVariable_.notify_all();
396 if (eventInjectThread_.joinable()) {
397 eventInjectThread_.join();
398 }
399 }
400
ReportEvent(const RawEvent rawEvent)401 void DistributedInputNodeManager::ReportEvent(const RawEvent rawEvent)
402 {
403 std::lock_guard<std::mutex> lockGuard(injectThreadMutex_);
404 injectQueue_.push(std::make_shared<RawEvent>(rawEvent));
405 conditionVariable_.notify_all();
406 }
407
InjectEvent()408 void DistributedInputNodeManager::InjectEvent()
409 {
410 int32_t ret = pthread_setname_np(pthread_self(), EVENT_INJECT_THREAD_NAME);
411 if (ret != 0) {
412 DHLOGE("InjectEvent setname failed.");
413 }
414 DHLOGI("start");
415 while (isInjectThreadRunning_.load()) {
416 std::shared_ptr<RawEvent> event = nullptr;
417 {
418 std::unique_lock<std::mutex> waitEventLock(injectThreadMutex_);
419 conditionVariable_.wait(waitEventLock,
420 [this]() { return !isInjectThreadRunning_.load() || !injectQueue_.empty(); });
421 if (injectQueue_.empty()) {
422 continue;
423 }
424 event = injectQueue_.front();
425 injectQueue_.pop();
426 }
427 if (event == nullptr) {
428 DHLOGD("event is null!");
429 continue;
430 }
431
432 DHLOGD("process event, inject queue size: %zu", injectQueue_.size());
433 ProcessInjectEvent(event);
434 }
435 DHLOGI("end");
436 }
437
ProcessInjectEvent(const std::shared_ptr<RawEvent> & rawEvent)438 void DistributedInputNodeManager::ProcessInjectEvent(const std::shared_ptr<RawEvent> &rawEvent)
439 {
440 std::string dhId = rawEvent->descriptor;
441 struct input_event event = {
442 .type = rawEvent->type,
443 .code = rawEvent->code,
444 .value = rawEvent->value
445 };
446 DHLOGI("InjectEvent dhId: %s, eventType: %d, eventCode: %d, eventValue: %d, when: " PRId64"",
447 GetAnonyString(dhId).c_str(), event.type, event.code, event.value, rawEvent->when);
448 VirtualDevice* device = nullptr;
449 if (GetDevice(dhId, device) < 0) {
450 DHLOGE("could not find the device");
451 return;
452 }
453 if (device != nullptr) {
454 device->InjectInputEvent(event);
455 }
456 }
457
GetDeviceInfo(std::string & deviceId)458 int32_t DistributedInputNodeManager::GetDeviceInfo(std::string &deviceId)
459 {
460 std::unique_lock<std::mutex> my_lock(operationMutex_);
461 auto localNode = std::make_unique<NodeBasicInfo>();
462 int32_t retCode = GetLocalNodeDeviceInfo(DINPUT_PKG_NAME.c_str(), localNode.get());
463 if (retCode != 0) {
464 DHLOGE("Could not get device id.");
465 return ERR_DH_INPUT_HANDLER_GET_DEVICE_ID_FAIL;
466 }
467
468 deviceId = localNode->networkId;
469 DHLOGI("device id is %s", GetAnonyString(deviceId).c_str());
470 return DH_SUCCESS;
471 }
472
GetDevicesInfoByType(const std::string & networkId,uint32_t inputTypes,std::map<int32_t,std::string> & datas)473 void DistributedInputNodeManager::GetDevicesInfoByType(const std::string &networkId, uint32_t inputTypes,
474 std::map<int32_t, std::string> &datas)
475 {
476 uint32_t inputType = 0;
477
478 if ((inputTypes & static_cast<uint32_t>(DInputDeviceType::MOUSE)) != 0) {
479 inputType |= INPUT_DEVICE_CLASS_CURSOR;
480 }
481
482 if ((inputTypes & static_cast<uint32_t>(DInputDeviceType::KEYBOARD)) != 0) {
483 inputType |= INPUT_DEVICE_CLASS_KEYBOARD;
484 }
485
486 if ((inputTypes & static_cast<uint32_t>(DInputDeviceType::MOUSE)) != 0) {
487 inputType |= INPUT_DEVICE_CLASS_TOUCH;
488 }
489
490 std::lock_guard<std::mutex> lock(virtualDeviceMapMutex_);
491 for (const auto &[id, virdevice] : virtualDeviceMap_) {
492 if ((virdevice->GetDeviceType() & inputType) && (virdevice->GetNetWorkId() == networkId)) {
493 datas.insert(std::pair<int32_t, std::string>(virdevice->GetDeviceFd(), id));
494 }
495 }
496 }
497
GetDevicesInfoByDhId(std::vector<std::string> dhidsVec,std::map<int32_t,std::string> & datas)498 void DistributedInputNodeManager::GetDevicesInfoByDhId(
499 std::vector<std::string> dhidsVec, std::map<int32_t, std::string> &datas)
500 {
501 for (auto dhId : dhidsVec) {
502 std::lock_guard<std::mutex> lock(virtualDeviceMapMutex_);
503 for (const auto &[id, virdevice] : virtualDeviceMap_) {
504 if (id == dhId) {
505 datas.insert(std::pair<int32_t, std::string>(virdevice->GetDeviceFd(), id));
506 break;
507 }
508 }
509 }
510 }
511 } // namespace DistributedInput
512 } // namespace DistributedHardware
513 } // namespace OHOS
514