17c804472Sopenharmony_ci/* 27c804472Sopenharmony_ci * Copyright (c) 2023 Huawei Device Co., Ltd. 37c804472Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 47c804472Sopenharmony_ci * you may not use this file except in compliance with the License. 57c804472Sopenharmony_ci * You may obtain a copy of the License at 67c804472Sopenharmony_ci * 77c804472Sopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 87c804472Sopenharmony_ci * 97c804472Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software 107c804472Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 117c804472Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 127c804472Sopenharmony_ci * See the License for the specific language governing permissions and 137c804472Sopenharmony_ci * limitations under the License. 147c804472Sopenharmony_ci */ 157c804472Sopenharmony_ci 167c804472Sopenharmony_ci#include "MouseInputImpl.h" 177c804472Sopenharmony_ci 187c804472Sopenharmony_ci#include <thread> 197c804472Sopenharmony_ci#include <vector> 207c804472Sopenharmony_ci#include <chrono> 217c804472Sopenharmony_ci#include <sstream> 227c804472Sopenharmony_ci 237c804472Sopenharmony_ci#include "PreviewerEngineLog.h" 247c804472Sopenharmony_ci 257c804472Sopenharmony_ciusing namespace OHOS::MMI; 267c804472Sopenharmony_ci 277c804472Sopenharmony_ciMouseInputImpl::MouseInputImpl() noexcept 287c804472Sopenharmony_ci{ 297c804472Sopenharmony_ci} 307c804472Sopenharmony_ci 317c804472Sopenharmony_ciTouchType MouseInputImpl::ConvertToOsType(int status) const 327c804472Sopenharmony_ci{ 337c804472Sopenharmony_ci if (status < static_cast<int>(TouchType::DOWN) || status > static_cast<int>(TouchType::UNKNOWN)) { 347c804472Sopenharmony_ci return TouchType::UNKNOWN; 357c804472Sopenharmony_ci } 367c804472Sopenharmony_ci return static_cast<TouchType>(status); 377c804472Sopenharmony_ci} 387c804472Sopenharmony_ci 397c804472Sopenharmony_ciSourceTool MouseInputImpl::ConvertToOsTool(int tools) const 407c804472Sopenharmony_ci{ 417c804472Sopenharmony_ci if (tools < static_cast<int>(SourceTool::UNKNOWN) || tools > static_cast<int>(SourceTool::LENS)) { 427c804472Sopenharmony_ci return SourceTool::UNKNOWN; 437c804472Sopenharmony_ci } 447c804472Sopenharmony_ci return static_cast<SourceTool>(tools); 457c804472Sopenharmony_ci} 467c804472Sopenharmony_ci 477c804472Sopenharmony_civoid MouseInputImpl::DispatchOsTouchEvent() const 487c804472Sopenharmony_ci{ 497c804472Sopenharmony_ci auto pointerEvent = std::make_shared<PointerEvent>(); 507c804472Sopenharmony_ci struct timespec ts; 517c804472Sopenharmony_ci clock_gettime(CLOCK_MONOTONIC, &ts); 527c804472Sopenharmony_ci std::chrono::duration<int64_t, std::nano> duration(ts.tv_sec * SEC_TO_NANOSEC + ts.tv_nsec); 537c804472Sopenharmony_ci pointerEvent->time = std::chrono::high_resolution_clock::time_point(duration); 547c804472Sopenharmony_ci pointerEvent->id = 1; 557c804472Sopenharmony_ci pointerEvent->x = mouseXPosition; 567c804472Sopenharmony_ci pointerEvent->y = mouseYPosition; 577c804472Sopenharmony_ci pointerEvent->type = ConvertToOsType(touchAction); 587c804472Sopenharmony_ci pointerEvent->buttonId_ = pointButton; 597c804472Sopenharmony_ci pointerEvent->pointerAction_ = pointAction; 607c804472Sopenharmony_ci pointerEvent->sourceType = sourceType; 617c804472Sopenharmony_ci pointerEvent->sourceTool = ConvertToOsTool(sourceTool); 627c804472Sopenharmony_ci pointerEvent->pressedButtons_ = pressedBtnsVec; 637c804472Sopenharmony_ci std::copy(axisValuesArr.begin(), axisValuesArr.end(), pointerEvent->axisValues_.begin()); 647c804472Sopenharmony_ci pointerEvent->size = sizeof (PointerEvent); 657c804472Sopenharmony_ci std::stringstream ss; 667c804472Sopenharmony_ci ss << "["; 677c804472Sopenharmony_ci for (double val : axisValuesArr) { 687c804472Sopenharmony_ci ss << " " << val << " "; 697c804472Sopenharmony_ci } 707c804472Sopenharmony_ci ss << "]" << std::endl; 717c804472Sopenharmony_ci ILOG("MouseInputImpl::DispatchEvent x: %f y:%f type:%d buttonId_:%d pointerAction_:%d sourceType:%d \ 727c804472Sopenharmony_ci sourceTool:%d pressedButtonsSize:%d axisValuesArr:%s", pointerEvent->x, pointerEvent->y, 737c804472Sopenharmony_ci pointerEvent->type, pointerEvent->buttonId_, pointerEvent->pointerAction_, pointerEvent->sourceType, 747c804472Sopenharmony_ci pointerEvent->sourceTool, pointerEvent->pressedButtons_.size(), ss.str().c_str()); 757c804472Sopenharmony_ci ILOG("current thread: %d", std::this_thread::get_id()); 767c804472Sopenharmony_ci JsAppImpl::GetInstance().DispatchPointerEvent(pointerEvent); 777c804472Sopenharmony_ci} 787c804472Sopenharmony_ci 797c804472Sopenharmony_civoid MouseInputImpl::DispatchOsBackEvent() const 807c804472Sopenharmony_ci{ 817c804472Sopenharmony_ci ILOG("DispatchBackPressedEvent run."); 827c804472Sopenharmony_ci ILOG("current thread: %d", std::this_thread::get_id()); 837c804472Sopenharmony_ci JsAppImpl::GetInstance().DispatchBackPressedEvent(); 847c804472Sopenharmony_ci} 857c804472Sopenharmony_ci 867c804472Sopenharmony_ciMouseInputImpl& MouseInputImpl::GetInstance() 877c804472Sopenharmony_ci{ 887c804472Sopenharmony_ci static MouseInputImpl instance; 897c804472Sopenharmony_ci return instance; 907c804472Sopenharmony_ci} 917c804472Sopenharmony_ci 927c804472Sopenharmony_civoid MouseInputImpl::SetMouseStatus(int status) 937c804472Sopenharmony_ci{ 947c804472Sopenharmony_ci touchAction = status; 957c804472Sopenharmony_ci} 967c804472Sopenharmony_ci 977c804472Sopenharmony_civoid MouseInputImpl::SetMousePosition(double xPosition, double yPosition) 987c804472Sopenharmony_ci{ 997c804472Sopenharmony_ci mouseXPosition = xPosition; 1007c804472Sopenharmony_ci mouseYPosition = yPosition; 1017c804472Sopenharmony_ci} 102