1fb299fa2Sopenharmony_ci/* 2fb299fa2Sopenharmony_ci * Copyright (c) 2021-2023 Huawei Device Co., Ltd. 3fb299fa2Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 4fb299fa2Sopenharmony_ci * you may not use this file except in compliance with the License. 5fb299fa2Sopenharmony_ci * You may obtain a copy of the License at 6fb299fa2Sopenharmony_ci * 7fb299fa2Sopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 8fb299fa2Sopenharmony_ci * 9fb299fa2Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software 10fb299fa2Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 11fb299fa2Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12fb299fa2Sopenharmony_ci * See the License for the specific language governing permissions and 13fb299fa2Sopenharmony_ci * limitations under the License. 14fb299fa2Sopenharmony_ci */ 15fb299fa2Sopenharmony_ci#include "input_event.h" 16fb299fa2Sopenharmony_ci#include <unistd.h> 17fb299fa2Sopenharmony_ci#include "log/log.h" 18fb299fa2Sopenharmony_ci#include "keys_input_device.h" 19fb299fa2Sopenharmony_ci 20fb299fa2Sopenharmony_cinamespace Updater { 21fb299fa2Sopenharmony_ciconstexpr const int MAX_INPUT_DEVICES = 32; 22fb299fa2Sopenharmony_ciextern "C" __attribute__((constructor)) void RegisterAddInputDeviceHelper(void) 23fb299fa2Sopenharmony_ci{ 24fb299fa2Sopenharmony_ci InputEvent::GetInstance().RegisterAddInputDeviceHelper(AddInputDevice); 25fb299fa2Sopenharmony_ci} 26fb299fa2Sopenharmony_ci 27fb299fa2Sopenharmony_civoid InputEvent::RegisterAddInputDeviceHelper(AddInputDeviceFunc ptr) 28fb299fa2Sopenharmony_ci{ 29fb299fa2Sopenharmony_ci addInputDeviceHelper_ = std::move(ptr); 30fb299fa2Sopenharmony_ci} 31fb299fa2Sopenharmony_ci 32fb299fa2Sopenharmony_ciextern "C" __attribute__((constructor)) void RegisterHandleEventHelper(void) 33fb299fa2Sopenharmony_ci{ 34fb299fa2Sopenharmony_ci InputEvent::GetInstance().RegisterHandleEventHelper(HandlePointersEvent); 35fb299fa2Sopenharmony_ci} 36fb299fa2Sopenharmony_ci 37fb299fa2Sopenharmony_civoid InputEvent::RegisterHandleEventHelper(HandlePointersEventFunc ptr) 38fb299fa2Sopenharmony_ci{ 39fb299fa2Sopenharmony_ci handlePointersEventHelper_ = std::move(ptr); 40fb299fa2Sopenharmony_ci} 41fb299fa2Sopenharmony_ci 42fb299fa2Sopenharmony_ciInputEvent &InputEvent::GetInstance() 43fb299fa2Sopenharmony_ci{ 44fb299fa2Sopenharmony_ci static InputEvent instance; 45fb299fa2Sopenharmony_ci return instance; 46fb299fa2Sopenharmony_ci} 47fb299fa2Sopenharmony_ci 48fb299fa2Sopenharmony_ciint InputEvent::HandleInputEvent(const struct input_event *iev, uint32_t type) 49fb299fa2Sopenharmony_ci{ 50fb299fa2Sopenharmony_ci struct input_event ev {}; 51fb299fa2Sopenharmony_ci ev.type = iev->type; 52fb299fa2Sopenharmony_ci ev.code = iev->code; 53fb299fa2Sopenharmony_ci ev.value = iev->value; 54fb299fa2Sopenharmony_ci 55fb299fa2Sopenharmony_ci KeysInputDevice::GetInstance().HandleKeyEvent(ev, type); 56fb299fa2Sopenharmony_ci handlePointersEventHelper_(ev, type); 57fb299fa2Sopenharmony_ci return 0; 58fb299fa2Sopenharmony_ci} 59fb299fa2Sopenharmony_ci 60fb299fa2Sopenharmony_civoid InputEvent::GetInputDeviceType(uint32_t devIndex, uint32_t &type) 61fb299fa2Sopenharmony_ci{ 62fb299fa2Sopenharmony_ci auto it = devTypeMap_.find(devIndex); 63fb299fa2Sopenharmony_ci if (it == devTypeMap_.end()) { 64fb299fa2Sopenharmony_ci LOG(ERROR) << "devTypeMap_ devIndex: " << devIndex << "not valid"; 65fb299fa2Sopenharmony_ci return; 66fb299fa2Sopenharmony_ci } 67fb299fa2Sopenharmony_ci type = it->second; 68fb299fa2Sopenharmony_ci} 69fb299fa2Sopenharmony_ci 70fb299fa2Sopenharmony_civoid InputEvent::ReportEventPkgCallback(const InputEventPackage **pkgs, const uint32_t count, uint32_t devIndex) 71fb299fa2Sopenharmony_ci{ 72fb299fa2Sopenharmony_ci if (pkgs == nullptr || *pkgs == nullptr) { 73fb299fa2Sopenharmony_ci return; 74fb299fa2Sopenharmony_ci } 75fb299fa2Sopenharmony_ci for (uint32_t i = 0; i < count; i++) { 76fb299fa2Sopenharmony_ci struct input_event ev = { 77fb299fa2Sopenharmony_ci .type = static_cast<__u16>(pkgs[i]->type), 78fb299fa2Sopenharmony_ci .code = static_cast<__u16>(pkgs[i]->code), 79fb299fa2Sopenharmony_ci .value = pkgs[i]->value, 80fb299fa2Sopenharmony_ci }; 81fb299fa2Sopenharmony_ci uint32_t type = 0; 82fb299fa2Sopenharmony_ci InputEvent::GetInstance().GetInputDeviceType(devIndex, type); 83fb299fa2Sopenharmony_ci InputEvent::GetInstance().HandleInputEvent(&ev, type); 84fb299fa2Sopenharmony_ci } 85fb299fa2Sopenharmony_ci return; 86fb299fa2Sopenharmony_ci} 87fb299fa2Sopenharmony_ci 88fb299fa2Sopenharmony_ciint InputEvent::HdfInit() 89fb299fa2Sopenharmony_ci{ 90fb299fa2Sopenharmony_ci int ret = GetInputInterface(&inputInterface_); 91fb299fa2Sopenharmony_ci if (ret != INPUT_SUCCESS) { 92fb299fa2Sopenharmony_ci LOG(ERROR) << "get input driver interface failed"; 93fb299fa2Sopenharmony_ci return ret; 94fb299fa2Sopenharmony_ci } 95fb299fa2Sopenharmony_ci 96fb299fa2Sopenharmony_ci sleep(1); // need wait thread running 97fb299fa2Sopenharmony_ci 98fb299fa2Sopenharmony_ci InputDevDesc sta[MAX_INPUT_DEVICES] = {{0}}; 99fb299fa2Sopenharmony_ci ret = inputInterface_->iInputManager->ScanInputDevice(sta, MAX_INPUT_DEVICES); 100fb299fa2Sopenharmony_ci if (ret != INPUT_SUCCESS) { 101fb299fa2Sopenharmony_ci LOG(ERROR) << "scan device failed"; 102fb299fa2Sopenharmony_ci return ret; 103fb299fa2Sopenharmony_ci } 104fb299fa2Sopenharmony_ci 105fb299fa2Sopenharmony_ci for (int i = 0; i < MAX_INPUT_DEVICES; i++) { 106fb299fa2Sopenharmony_ci uint32_t idx = sta[i].devIndex; 107fb299fa2Sopenharmony_ci uint32_t dev = sta[i].devType; 108fb299fa2Sopenharmony_ci if ((idx == 0) || (inputInterface_->iInputManager->OpenInputDevice(idx) == INPUT_FAILURE)) { 109fb299fa2Sopenharmony_ci continue; 110fb299fa2Sopenharmony_ci } 111fb299fa2Sopenharmony_ci devTypeMap_.insert(std::pair<uint32_t, uint32_t>(idx, dev)); 112fb299fa2Sopenharmony_ci 113fb299fa2Sopenharmony_ci LOG(INFO) << "hdf devType:" << dev << ", devIndex:" << idx; 114fb299fa2Sopenharmony_ci } 115fb299fa2Sopenharmony_ci 116fb299fa2Sopenharmony_ci /* first param not necessary, pass default 1 */ 117fb299fa2Sopenharmony_ci callback_.EventPkgCallback = ReportEventPkgCallback; 118fb299fa2Sopenharmony_ci ret = inputInterface_->iInputReporter->RegisterReportCallback(1, &callback_); 119fb299fa2Sopenharmony_ci if (ret != INPUT_SUCCESS) { 120fb299fa2Sopenharmony_ci LOG(ERROR) << "register callback failed for device 1"; 121fb299fa2Sopenharmony_ci return ret; 122fb299fa2Sopenharmony_ci } 123fb299fa2Sopenharmony_ci 124fb299fa2Sopenharmony_ci OHOS::InputDeviceManager::GetInstance()->Add(&KeysInputDevice::GetInstance()); 125fb299fa2Sopenharmony_ci OHOS::InputDeviceManager::GetInstance()->SetPeriod(0); 126fb299fa2Sopenharmony_ci addInputDeviceHelper_(); 127fb299fa2Sopenharmony_ci LOG(INFO) << "add InputDevice done"; 128fb299fa2Sopenharmony_ci 129fb299fa2Sopenharmony_ci return 0; 130fb299fa2Sopenharmony_ci} 131fb299fa2Sopenharmony_ci} // namespace Updater 132