• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 namespace OHOS {
19 namespace MMI {
20 namespace {
21 constexpr OHOS::HiviewDFX::HiLogLabel LABEL = { LOG_CORE, MMI_LOG_DOMAIN, "GetDeviceNode" };
22 const std::string DEVICES_INFO_PATH = "/proc/bus/input/devices";
23 constexpr int32_t READ_CMD_BUFF_SIZE = 1024;
24 } // namespace
25 
GetDeviceNode()26 GetDeviceNode::GetDeviceNode()
27 {
28     InitDeviceInfo();
29 }
30 
GetDeviceNodeName(const std::string & targetName,uint16_t devIndex,std::string & deviceNode)31 int32_t GetDeviceNode::GetDeviceNodeName(const std::string &targetName, uint16_t devIndex, std::string &deviceNode)
32 {
33     std::vector<std::string> devices = ReadDeviceFile();
34     if (devices.empty()) {
35         MMI_HILOGE("Devices is empty");
36         return RET_ERR;
37     }
38     DeviceList deviceList;
39     AnalyseDevices(devices, deviceList);
40     if (deviceList.empty()) {
41         MMI_HILOGE("Device list is empty");
42         return RET_ERR;
43     }
44     std::string deviceName = deviceList_[targetName];
45     auto iter = deviceList.find(deviceName);
46     if (iter == deviceList.end()) {
47         MMI_HILOGE("Failed to find deviceName:%{public}s", deviceName.c_str());
48         return RET_ERR;
49     }
50     size_t targetSize = iter->second.size();
51     if (devIndex > targetSize) {
52         MMI_HILOGE("Failed to devIndex:%{public}d > targetSize:%{public}zu", devIndex, targetSize);
53         return RET_ERR;
54     }
55     std::string nodeRootPath = "/dev/input/";
56     deviceNode = nodeRootPath + iter->second[devIndex];
57     MMI_HILOGI("%{public}s[%{public}d] --> %{public}s", targetName.c_str(), devIndex,
58                deviceNode.c_str());
59 
60     return RET_OK;
61 }
62 
InitDeviceInfo()63 void GetDeviceNode::InitDeviceInfo()
64 {
65     deviceList_["mouse"] = "Virtual Mouse";
66     deviceList_["touch"] = "Virtual TouchScreen";
67     deviceList_["finger"] = "Virtual Finger";
68     deviceList_["pad"] = "Virtual Touchpad";
69     deviceList_["pen"] = "Virtual Stylus";
70     deviceList_["gamePad"] = "Virtual GamePad";
71     deviceList_["joystick"] = "Virtual Joystick";
72     deviceList_["remoteControl"] = "Virtual RemoteControl";
73     deviceList_["knob model1"] = "Virtual KnobConsumerCtrl";
74     deviceList_["knob model2"] = "Virtual Knob";
75     deviceList_["knob model3"] = "Virtual KnobMouse";
76     deviceList_["keyboard model1"] = "Virtual keyboard";
77     deviceList_["keyboard model2"] = "Virtual KeyboardConsumerCtrl";
78     deviceList_["keyboard model3"] = "Virtual KeyboardSysCtrl";
79     deviceList_["trackball"] = "Virtual Trackball";
80     deviceList_["trackpad model1"] = "Virtual TrackPadMouse";
81     deviceList_["trackpad model2"] = "Virtual Trackpad";
82 }
83 
ReadDeviceFile()84 std::vector<std::string> GetDeviceNode::ReadDeviceFile()
85 {
86     char realPath[PATH_MAX] = {};
87     if (realpath(DEVICES_INFO_PATH.c_str(), realPath) == nullptr) {
88         MMI_HILOGE("The path is error, path:%{public}s", DEVICES_INFO_PATH.c_str());
89         return {};
90     }
91     FILE* fp = fopen(DEVICES_INFO_PATH.c_str(), "r");
92     if (fp == nullptr) {
93         MMI_HILOGW("Open file:%{public}s failed", DEVICES_INFO_PATH.c_str());
94         return {};
95     }
96     char buf[READ_CMD_BUFF_SIZE] = {};
97     std::vector<std::string> deviceStrs;
98     while (fgets(buf, sizeof(buf), fp) != nullptr) {
99         deviceStrs.push_back(buf);
100     }
101     if (fclose(fp) != 0) {
102         MMI_HILOGW("Close file:%{public}s failed", DEVICES_INFO_PATH.c_str());
103     }
104     return deviceStrs;
105 }
106 
AnalyseDevices(const std::vector<std::string> & deviceStrs,DeviceList & deviceList) const107 void GetDeviceNode::AnalyseDevices(const std::vector<std::string> &deviceStrs, DeviceList &deviceList) const
108 {
109     std::string name;
110     for (const auto &item : deviceStrs) {
111         if (item.empty()) {
112             MMI_HILOGE("Device info is empty");
113             return;
114         }
115         std::string temp = item.substr(0, 1);
116         uint64_t endPos = 0;
117         uint64_t startPos = 0;
118         if (temp == "N") {
119             startPos = item.find("=") + strlen("N:");
120             endPos = item.size() - 1;
121             name = item.substr(startPos, endPos - startPos - 1);
122         }
123         if (temp == "H") {
124             startPos = item.rfind("event");
125             uint64_t eventLength = item.substr(startPos).find_first_of(" ");
126             std::string target = item.substr(startPos, eventLength);
127             if (!(name.empty())) {
128                 deviceList[name].push_back(target);
129                 name.clear();
130                 target.clear();
131             }
132         }
133     }
134 }
135 } // namespace MMI
136 } // namespace OHOS