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 #include "input_event.h"
16 #include <unistd.h>
17 #include "log/log.h"
18 #include "keys_input_device.h"
19
20 namespace Updater {
RegisterAddInputDeviceHelper(void)21 extern "C" __attribute__((constructor)) void RegisterAddInputDeviceHelper(void)
22 {
23 InputEvent::GetInstance().RegisterAddInputDeviceHelper(AddInputDevice);
24 }
25
RegisterAddInputDeviceHelper(AddInputDeviceFunc ptr)26 void InputEvent::RegisterAddInputDeviceHelper(AddInputDeviceFunc ptr)
27 {
28 addInputDeviceHelper_ = std::move(ptr);
29 }
30
RegisterHandleEventHelper(void)31 extern "C" __attribute__((constructor)) void RegisterHandleEventHelper(void)
32 {
33 InputEvent::GetInstance().RegisterHandleEventHelper(HandlePointersEvent);
34 }
35
RegisterHandleEventHelper(HandlePointersEventFunc ptr)36 void InputEvent::RegisterHandleEventHelper(HandlePointersEventFunc ptr)
37 {
38 handlePointersEventHelper_ = std::move(ptr);
39 }
40
GetInstance()41 InputEvent &InputEvent::GetInstance()
42 {
43 static InputEvent instance;
44 return instance;
45 }
46
HandleInputEvent(const struct input_event * iev,uint32_t type)47 int InputEvent::HandleInputEvent(const struct input_event *iev, uint32_t type)
48 {
49 struct input_event ev {};
50 ev.type = iev->type;
51 ev.code = iev->code;
52 ev.value = iev->value;
53
54 KeysInputDevice::GetInstance().HandleKeyEvent(ev, type);
55 handlePointersEventHelper_(ev, type);
56 return 0;
57 }
58
GetInputDeviceType(uint32_t devIndex,uint32_t & type)59 void InputEvent::GetInputDeviceType(uint32_t devIndex, uint32_t &type)
60 {
61 auto it = devTypeMap_.find(devIndex);
62 if (it == devTypeMap_.end()) {
63 LOG(ERROR) << "devTypeMap_ devIndex: " << devIndex << "not valid";
64 return;
65 }
66 type = it->second;
67 }
68
EventPkgCallback(const std::vector<OHOS::HDI::Input::V1_0::EventPackage> & pkgs,uint32_t devIndex)69 int32_t InputEvent::HdfInputEventCallback::EventPkgCallback(
70 const std::vector<OHOS::HDI::Input::V1_0::EventPackage>& pkgs, uint32_t devIndex)
71 {
72 if (pkgs.empty()) {
73 LOG(WARNING) << "pkgs is empty";
74 return HDF_FAILURE;
75 }
76 for (uint32_t i = 0; i < pkgs.size(); i++) {
77 struct input_event ev = {
78 .type = static_cast<__u16>(pkgs[i].type),
79 .code = static_cast<__u16>(pkgs[i].code),
80 .value = pkgs[i].value,
81 };
82 uint32_t type = 0;
83 InputEvent::GetInstance().GetInputDeviceType(devIndex, type);
84 InputEvent::GetInstance().HandleInputEvent(&ev, type);
85 }
86 return HDF_SUCCESS;
87 }
88
HotPlugCallback(const OHOS::HDI::Input::V1_0::HotPlugEvent & event)89 int32_t InputEvent::HdfInputEventCallback::HotPlugCallback(const OHOS::HDI::Input::V1_0::HotPlugEvent &event)
90 {
91 return HDF_SUCCESS;
92 }
93
HdfInit()94 int InputEvent::HdfInit()
95 {
96 inputInterface_ = OHOS::HDI::Input::V1_0::IInputInterfaces::Get(true);
97 if (inputInterface_ == nullptr) {
98 LOG(ERROR) << "get input driver interface failed";
99 return HDF_FAILURE;
100 }
101
102 sleep(1); // need wait thread running
103
104 std::vector<OHOS::HDI::Input::V1_0::DevDesc> sta = {};
105 int ret = inputInterface_->ScanInputDevice(sta);
106 if (ret != HDF_SUCCESS) {
107 LOG(ERROR) << "scan device failed";
108 return ret;
109 }
110
111 for (size_t i = 0; i < sta.size(); i++) {
112 uint32_t idx = sta[i].devIndex;
113 uint32_t dev = sta[i].devType;
114 if ((idx == 0) || (inputInterface_->OpenInputDevice(idx) == HDF_FAILURE)) {
115 continue;
116 }
117 devTypeMap_.insert(std::pair<uint32_t, uint32_t>(idx, dev));
118
119 LOG(INFO) << "hdf devType:" << dev << ", devIndex:" << idx;
120 }
121
122 /* first param not necessary, pass default 1 */
123 callback_ = new (std::nothrow) HdfInputEventCallback();
124 if (callback_ == nullptr) {
125 LOG(ERROR) << "callback is nullptr";
126 return ret;
127 }
128 ret = inputInterface_->RegisterReportCallback(1, callback_);
129 if (ret != HDF_SUCCESS) {
130 LOG(ERROR) << "register callback failed for device 1";
131 return ret;
132 }
133
134 OHOS::InputDeviceManager::GetInstance()->Add(&KeysInputDevice::GetInstance());
135 OHOS::InputDeviceManager::GetInstance()->SetPeriod(0);
136 addInputDeviceHelper_();
137 LOG(INFO) << "add InputDevice done";
138
139 return 0;
140 }
141 } // namespace Updater
142