162b8cbc9Sopenharmony_ci/* 262b8cbc9Sopenharmony_ci * Copyright (c) 2020-2021 Huawei Device Co., Ltd. 362b8cbc9Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 462b8cbc9Sopenharmony_ci * you may not use this file except in compliance with the License. 562b8cbc9Sopenharmony_ci * You may obtain a copy of the License at 662b8cbc9Sopenharmony_ci * 762b8cbc9Sopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 862b8cbc9Sopenharmony_ci * 962b8cbc9Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software 1062b8cbc9Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 1162b8cbc9Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 1262b8cbc9Sopenharmony_ci * See the License for the specific language governing permissions and 1362b8cbc9Sopenharmony_ci * limitations under the License. 1462b8cbc9Sopenharmony_ci */ 1562b8cbc9Sopenharmony_ci 1662b8cbc9Sopenharmony_ci#include "input_event_hub.h" 1762b8cbc9Sopenharmony_ci#include "gfx_utils/graphic_log.h" 1862b8cbc9Sopenharmony_ci 1962b8cbc9Sopenharmony_cinamespace OHOS { 2062b8cbc9Sopenharmony_cinamespace { 2162b8cbc9Sopenharmony_ciconst uint32_t TOUCH_DEV_ID = 1; 2262b8cbc9Sopenharmony_ciconst uint32_t MOUSE_DEV_ID = 2; 2362b8cbc9Sopenharmony_ciconst uint32_t UNKNOW_DEV_ID = 32; 2462b8cbc9Sopenharmony_ci} 2562b8cbc9Sopenharmony_ciIInputInterface* InputEventHub::inputInterface_ = nullptr; 2662b8cbc9Sopenharmony_ciInputEventCb InputEventHub::callback_ = { 0 }; 2762b8cbc9Sopenharmony_ciInputEventHub::ReadCallback InputEventHub::readCallback_ = nullptr; 2862b8cbc9Sopenharmony_ci 2962b8cbc9Sopenharmony_ciInputEventHub* InputEventHub::GetInstance() 3062b8cbc9Sopenharmony_ci{ 3162b8cbc9Sopenharmony_ci static InputEventHub instance; 3262b8cbc9Sopenharmony_ci return &instance; 3362b8cbc9Sopenharmony_ci} 3462b8cbc9Sopenharmony_ci 3562b8cbc9Sopenharmony_ciInputEventHub::InputEventHub() 3662b8cbc9Sopenharmony_ci{ 3762b8cbc9Sopenharmony_ci for (uint8_t i = 0; i < MAX_INPUT_DEVICE_NUM; i++) { 3862b8cbc9Sopenharmony_ci mountDevIndex_[i] = UNKNOW_DEV_ID; 3962b8cbc9Sopenharmony_ci } 4062b8cbc9Sopenharmony_ci openDev_ = 0; 4162b8cbc9Sopenharmony_ci 4262b8cbc9Sopenharmony_ci data_.deviceId = UNKNOW_DEV_ID; 4362b8cbc9Sopenharmony_ci data_.state = 0; 4462b8cbc9Sopenharmony_ci data_.timestamp = 0; 4562b8cbc9Sopenharmony_ci data_.type = InputDevType::INDEV_TYPE_UNKNOWN; 4662b8cbc9Sopenharmony_ci data_.x = 0; 4762b8cbc9Sopenharmony_ci data_.y = 0; 4862b8cbc9Sopenharmony_ci} 4962b8cbc9Sopenharmony_ci 5062b8cbc9Sopenharmony_civoid InputEventHub::SetUp() 5162b8cbc9Sopenharmony_ci{ 5262b8cbc9Sopenharmony_ci int32_t ret = GetInputInterface(&inputInterface_); 5362b8cbc9Sopenharmony_ci if (ret != INPUT_SUCCESS) { 5462b8cbc9Sopenharmony_ci GRAPHIC_LOGE("get input driver interface failed!"); 5562b8cbc9Sopenharmony_ci return; 5662b8cbc9Sopenharmony_ci } 5762b8cbc9Sopenharmony_ci uint8_t num = ScanInputDevice(); 5862b8cbc9Sopenharmony_ci if (num == 0) { 5962b8cbc9Sopenharmony_ci GRAPHIC_LOGE("There is no device!"); 6062b8cbc9Sopenharmony_ci return; 6162b8cbc9Sopenharmony_ci } 6262b8cbc9Sopenharmony_ci for (uint8_t i = 0; i < num; i++) { 6362b8cbc9Sopenharmony_ci if (inputInterface_ == nullptr || inputInterface_->iInputManager == nullptr) { 6462b8cbc9Sopenharmony_ci GRAPHIC_LOGE("input interface or input manager is nullptr, open device failed!"); 6562b8cbc9Sopenharmony_ci return; 6662b8cbc9Sopenharmony_ci } 6762b8cbc9Sopenharmony_ci ret = inputInterface_->iInputManager->OpenInputDevice(mountDevIndex_[i]); 6862b8cbc9Sopenharmony_ci if (ret == INPUT_SUCCESS && inputInterface_->iInputReporter != nullptr) { 6962b8cbc9Sopenharmony_ci callback_.EventPkgCallback = EventCallback; 7062b8cbc9Sopenharmony_ci ret = inputInterface_->iInputReporter->RegisterReportCallback(mountDevIndex_[i], &callback_); 7162b8cbc9Sopenharmony_ci if (ret != INPUT_SUCCESS) { 7262b8cbc9Sopenharmony_ci GRAPHIC_LOGE("device dose not exist, can't register callback to it!"); 7362b8cbc9Sopenharmony_ci return; 7462b8cbc9Sopenharmony_ci } 7562b8cbc9Sopenharmony_ci openDev_ = openDev_ | (1 << i); 7662b8cbc9Sopenharmony_ci } 7762b8cbc9Sopenharmony_ci } 7862b8cbc9Sopenharmony_ci} 7962b8cbc9Sopenharmony_ci 8062b8cbc9Sopenharmony_civoid InputEventHub::TearDown() 8162b8cbc9Sopenharmony_ci{ 8262b8cbc9Sopenharmony_ci int32_t ret = 0; 8362b8cbc9Sopenharmony_ci for (uint8_t i = 0; i < MAX_INPUT_DEVICE_NUM; i++) { 8462b8cbc9Sopenharmony_ci if ((openDev_ & (1 << i)) == 0) { 8562b8cbc9Sopenharmony_ci continue; 8662b8cbc9Sopenharmony_ci } 8762b8cbc9Sopenharmony_ci if (inputInterface_ == nullptr) { 8862b8cbc9Sopenharmony_ci GRAPHIC_LOGE("input interface point is nullptr!"); 8962b8cbc9Sopenharmony_ci return; 9062b8cbc9Sopenharmony_ci } 9162b8cbc9Sopenharmony_ci if (inputInterface_->iInputReporter == nullptr || inputInterface_->iInputManager == nullptr) { 9262b8cbc9Sopenharmony_ci GRAPHIC_LOGE("input interface or input manager is nullptr, open device failed!"); 9362b8cbc9Sopenharmony_ci return; 9462b8cbc9Sopenharmony_ci } 9562b8cbc9Sopenharmony_ci ret = inputInterface_->iInputReporter->UnregisterReportCallback(mountDevIndex_[i]); 9662b8cbc9Sopenharmony_ci if (ret != INPUT_SUCCESS) { 9762b8cbc9Sopenharmony_ci GRAPHIC_LOGE("Unregister callback failed!"); 9862b8cbc9Sopenharmony_ci } 9962b8cbc9Sopenharmony_ci ret = inputInterface_->iInputManager->CloseInputDevice(mountDevIndex_[i]); 10062b8cbc9Sopenharmony_ci if (ret != INPUT_SUCCESS) { 10162b8cbc9Sopenharmony_ci GRAPHIC_LOGE("Unmount device failed!"); 10262b8cbc9Sopenharmony_ci } 10362b8cbc9Sopenharmony_ci openDev_ = openDev_ & ~(1 << i); 10462b8cbc9Sopenharmony_ci } 10562b8cbc9Sopenharmony_ci 10662b8cbc9Sopenharmony_ci if (inputInterface_ != nullptr) { 10762b8cbc9Sopenharmony_ci if (inputInterface_->iInputManager != nullptr) { 10862b8cbc9Sopenharmony_ci free(inputInterface_->iInputManager); 10962b8cbc9Sopenharmony_ci } 11062b8cbc9Sopenharmony_ci if (inputInterface_->iInputReporter != nullptr) { 11162b8cbc9Sopenharmony_ci free(inputInterface_->iInputReporter); 11262b8cbc9Sopenharmony_ci } 11362b8cbc9Sopenharmony_ci if (inputInterface_->iInputController != nullptr) { 11462b8cbc9Sopenharmony_ci free(inputInterface_->iInputController); 11562b8cbc9Sopenharmony_ci } 11662b8cbc9Sopenharmony_ci free(inputInterface_); 11762b8cbc9Sopenharmony_ci inputInterface_ = nullptr; 11862b8cbc9Sopenharmony_ci } 11962b8cbc9Sopenharmony_ci} 12062b8cbc9Sopenharmony_ci 12162b8cbc9Sopenharmony_civoid InputEventHub::EventCallback(const InputEventPackage **pkgs, uint32_t count, uint32_t devIndex) 12262b8cbc9Sopenharmony_ci{ 12362b8cbc9Sopenharmony_ci if (pkgs == nullptr || readCallback_ == nullptr || count == 0) { 12462b8cbc9Sopenharmony_ci return; 12562b8cbc9Sopenharmony_ci } 12662b8cbc9Sopenharmony_ci 12762b8cbc9Sopenharmony_ci RawEvent& data = InputEventHub::GetInstance()->data_; 12862b8cbc9Sopenharmony_ci for (uint32_t i = 0; i < count; i++) { 12962b8cbc9Sopenharmony_ci if (pkgs[i]->type == EV_REL) { 13062b8cbc9Sopenharmony_ci data.type = InputDevType::INDEV_TYPE_MOUSE; 13162b8cbc9Sopenharmony_ci if (pkgs[i]->code == REL_X) 13262b8cbc9Sopenharmony_ci data.x += pkgs[i]->value; 13362b8cbc9Sopenharmony_ci else if (pkgs[i]->code == REL_Y) 13462b8cbc9Sopenharmony_ci data.y += pkgs[i]->value; 13562b8cbc9Sopenharmony_ci } else if (pkgs[i]->type == EV_ABS) { 13662b8cbc9Sopenharmony_ci data.type = InputDevType::INDEV_TYPE_TOUCH; 13762b8cbc9Sopenharmony_ci if (pkgs[i]->code == ABS_MT_POSITION_X) 13862b8cbc9Sopenharmony_ci data.x = pkgs[i]->value; 13962b8cbc9Sopenharmony_ci else if (pkgs[i]->code == ABS_MT_POSITION_Y) 14062b8cbc9Sopenharmony_ci data.y = pkgs[i]->value; 14162b8cbc9Sopenharmony_ci } else if (pkgs[i]->type == EV_KEY) { 14262b8cbc9Sopenharmony_ci if (pkgs[i]->code == BTN_MOUSE || pkgs[i]->code == BTN_TOUCH) { 14362b8cbc9Sopenharmony_ci if (pkgs[i]->value == 0) 14462b8cbc9Sopenharmony_ci data.state = 0; 14562b8cbc9Sopenharmony_ci else if (pkgs[i]->value == 1) 14662b8cbc9Sopenharmony_ci data.state = 1; 14762b8cbc9Sopenharmony_ci } 14862b8cbc9Sopenharmony_ci } else if (pkgs[i]->type == EV_SYN) { 14962b8cbc9Sopenharmony_ci if (pkgs[i]->code == SYN_REPORT) { 15062b8cbc9Sopenharmony_ci break; 15162b8cbc9Sopenharmony_ci } 15262b8cbc9Sopenharmony_ci } 15362b8cbc9Sopenharmony_ci } 15462b8cbc9Sopenharmony_ci 15562b8cbc9Sopenharmony_ci readCallback_(&data); 15662b8cbc9Sopenharmony_ci} 15762b8cbc9Sopenharmony_ci 15862b8cbc9Sopenharmony_ciInputDevType InputEventHub::GetDeviceType(uint32_t devIndex) 15962b8cbc9Sopenharmony_ci{ 16062b8cbc9Sopenharmony_ci switch (devIndex) { 16162b8cbc9Sopenharmony_ci case TOUCH_DEV_ID: { 16262b8cbc9Sopenharmony_ci return InputDevType::INDEV_TYPE_TOUCH; 16362b8cbc9Sopenharmony_ci } 16462b8cbc9Sopenharmony_ci case MOUSE_DEV_ID: { 16562b8cbc9Sopenharmony_ci return InputDevType::INDEV_TYPE_MOUSE; 16662b8cbc9Sopenharmony_ci } 16762b8cbc9Sopenharmony_ci default: { 16862b8cbc9Sopenharmony_ci return InputDevType::INDEV_TYPE_UNKNOWN; 16962b8cbc9Sopenharmony_ci } 17062b8cbc9Sopenharmony_ci } 17162b8cbc9Sopenharmony_ci} 17262b8cbc9Sopenharmony_ci 17362b8cbc9Sopenharmony_ciuint8_t InputEventHub::ScanInputDevice() 17462b8cbc9Sopenharmony_ci{ 17562b8cbc9Sopenharmony_ci for (uint8_t i = 0; i < MAX_INPUT_DEVICE_NUM; i++) { 17662b8cbc9Sopenharmony_ci mountDevIndex_[i] = UNKNOW_DEV_ID; 17762b8cbc9Sopenharmony_ci } 17862b8cbc9Sopenharmony_ci /* later will be change get device mode */ 17962b8cbc9Sopenharmony_ci uint32_t deviceNum = 0; 18062b8cbc9Sopenharmony_ci mountDevIndex_[0] = TOUCH_DEV_ID; 18162b8cbc9Sopenharmony_ci mountDevIndex_[1] = MOUSE_DEV_ID; 18262b8cbc9Sopenharmony_ci deviceNum = 2; // 2:Number of current devices 18362b8cbc9Sopenharmony_ci return deviceNum; 18462b8cbc9Sopenharmony_ci} 18562b8cbc9Sopenharmony_ci} // namespace OHOS 186