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 "get_device_node.h"
17
18 using namespace OHOS::MMI;
19
20 namespace {
21 constexpr OHOS::HiviewDFX::HiLogLabel LABEL = { LOG_CORE, MMI_LOG_DOMAIN, "GetDeviceNode" };
22 } // namespace
23
GetDeviceNode()24 GetDeviceNode::GetDeviceNode()
25 {
26 InitDeviceInfo();
27 }
28
GetDeviceNodeName(const std::string & targetName,std::string & deviceNode,uint16_t devIndex)29 int32_t GetDeviceNode::GetDeviceNodeName(const std::string &targetName, std::string &deviceNode, uint16_t devIndex)
30 {
31 std::string cmd = "cat /proc/bus/input/devices";
32 std::vector<std::string> cmdResult;
33 ExecuteCmd(cmd, cmdResult);
34 DeviceMapData deviceMapData;
35 GetDeviceInfoCmdResult(cmdResult, deviceMapData);
36 std::string deviceName = deviceMap_[targetName];
37 auto iter = deviceMapData.find(deviceName);
38 if (iter == deviceMapData.end()) {
39 MMI_LOGE("faild for find deviceName:%{public}s", deviceName.c_str());
40 return RET_ERR;
41 }
42 size_t targetSize = iter->second.size();
43 if (devIndex > targetSize) {
44 MMI_LOGE("faild for devIndex:%{public}d > targetSize:%{public}zu", devIndex, targetSize);
45 return RET_ERR;
46 }
47 std::string nodeRootPath = "/dev/input/";
48 deviceNode = nodeRootPath + iter->second[devIndex];
49 MMI_LOGI("%{public}s[%{public}d] --> %{public}s", targetName.c_str(), devIndex,
50 deviceNode.c_str());
51
52 return RET_OK;
53 }
54
InitDeviceInfo()55 void GetDeviceNode::InitDeviceInfo()
56 {
57 deviceMap_["mouse"] = "Virtual Mouse";
58 deviceMap_["touch"] = "Virtual TouchScreen";
59 deviceMap_["finger"] = "Virtual Finger";
60 deviceMap_["pad"] = "Virtual Touchpad";
61 deviceMap_["pen"] = "Virtual Stylus";
62 deviceMap_["gamePad"] = "Virtual GamePad";
63 deviceMap_["joystick"] = "Virtual Joystick";
64 deviceMap_["remoteControl"] = "Virtual RemoteControl";
65 deviceMap_["knob model1"] = "Virtual KnobConsumerCtrl";
66 deviceMap_["knob model2"] = "Virtual Knob";
67 deviceMap_["knob model3"] = "Virtual KnobMouse";
68 deviceMap_["keyboard model1"] = "Virtual keyboard";
69 deviceMap_["keyboard model2"] = "Virtual KeyboardConsumerCtrl";
70 deviceMap_["keyboard model3"] = "Virtual KeyboardSysCtrl";
71 deviceMap_["trackball"] = "Virtual Trackball";
72 deviceMap_["trackpad model1"] = "Virtual TrackPadMouse";
73 deviceMap_["trackpad model2"] = "Virtual Trackpad";
74 }
75
ExecuteCmd(const std::string cmd,std::vector<std::string> & cmdResult)76 int32_t GetDeviceNode::ExecuteCmd(const std::string cmd, std::vector<std::string> &cmdResult)
77 {
78 if (cmd.empty()) {
79 return RET_ERR;
80 }
81 char buffer[READ_CMD_BUFF_SIZE] = {};
82 FILE* pin = popen(cmd.c_str(), "r");
83 if (pin == nullptr) {
84 return RET_ERR;
85 }
86 cmdResult.clear();
87 while (!feof(pin)) {
88 if (fgets(buffer, sizeof(buffer), pin) != nullptr) {
89 cmdResult.push_back(buffer);
90 }
91 }
92 return pclose(pin);
93 }
94
GetDeviceInfoCmdResult(const std::vector<std::string> & cmdResult,DeviceMapData & deviceMapData)95 void GetDeviceNode::GetDeviceInfoCmdResult(const std::vector<std::string>& cmdResult, DeviceMapData& deviceMapData)
96 {
97 std::string name;
98 std::string target;
99 std::string temp;
100 uint64_t endPos = 0;
101 uint64_t startPos = 0;
102 uint64_t eventLength = CMD_EVENT_LENGTH;
103 for (const auto &item : cmdResult) {
104 temp = item.substr(0, 1);
105 if (temp == "N") {
106 startPos = item.find("=") + strlen("N:");
107 endPos = item.size() - 1;
108 name = item.substr(startPos, endPos - startPos - 1);
109 } else if (temp == "H") {
110 startPos = item.find("event");
111 std::string endString = item.substr(startPos + strlen("event") + 1, 1);
112 if (endString != " ") {
113 eventLength = CMD_EVENT_LENGTH + 1;
114 }
115 target = item.substr(startPos, eventLength);
116 if (!(name.empty())) {
117 deviceMapData[name].push_back(target);
118 name.clear();
119 target.clear();
120 }
121 } else {
122 // nothing to do.
123 }
124 }
125 }