1 /* 2 * Copyright (c) 2024 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 #ifndef INPUT_EVENT_SAMPLER_H 17 #define INPUT_EVENT_SAMPLER_H 18 19 #include <atomic> 20 #include <mutex> 21 #include <queue> 22 #include <unordered_set> 23 24 #include "pointer_event.h" 25 26 namespace OHOS { 27 namespace Msdp { 28 namespace DeviceStatus { 29 namespace Cooperate { 30 31 using PointerEventHandler = std::function<void(std::shared_ptr<MMI::PointerEvent>)>; 32 33 struct RawEvent { 34 std::shared_ptr<MMI::PointerEvent> pointerEvent; 35 std::chrono::steady_clock::time_point rcvdTimeStamp; 36 }; 37 38 class InputEventSampler final { 39 public: 40 InputEventSampler(); 41 void OnPointerEvent(std::shared_ptr<MMI::PointerEvent> pointerEvent); 42 void SetPointerEventHandler(PointerEventHandler pointerEventHandler); 43 private: 44 bool IsTouchPadEvent(std::shared_ptr<MMI::PointerEvent> pointerEvent); 45 bool IsSpecialEvent(std::shared_ptr<MMI::PointerEvent> pointerEvent); 46 bool IsDurationMatched(); 47 bool IsOffsetMatched(std::shared_ptr<MMI::PointerEvent> pointerEvent); 48 bool IsRawEventsExpired(); 49 bool IsSkipNeeded(std::shared_ptr<MMI::PointerEvent> pointerEvent); 50 void AggregateRawEvents(std::shared_ptr<MMI::PointerEvent> pointerEvent); 51 void HandleTouchPadEvent(std::shared_ptr<MMI::PointerEvent> pointerEvent); 52 void HandleMouseEvent(std::shared_ptr<MMI::PointerEvent> pointerEvent); 53 void OnDownSampledEvent(); 54 void ClearRawEvents(); 55 void UpdateAggregationTimeStamp(); 56 bool IsAggregationIntervalMatched(); 57 private: 58 PointerEventHandler pointerEventHandler_; 59 std::mutex rawEventMutex_; 60 std::queue<RawEvent> rawEvents_; 61 std::mutex sampledEventMutex_; 62 std::queue<std::shared_ptr<MMI::PointerEvent>> sampledEvents_; 63 std::mutex aggregationTimeStampMutex_; 64 std::chrono::steady_clock::time_point aggregationTimeStamp_; 65 66 std::atomic_int32_t prefixRawDxSum_ { 0 }; 67 std::atomic_int32_t prefixRawDySum_ { 0 }; 68 69 static int32_t idealEventIntervalMS_; 70 static int32_t expiredIntervalMS_; 71 static int32_t rawDxThreshold_; 72 static int32_t rawDyThreshold_; 73 static std::unordered_set<int32_t> filterPointerActions_; 74 }; 75 } // namespace Cooperate 76 } // namespace DeviceStatus 77 } // namespace Msdp 78 } // namespace OHOS 79 #endif // INPUT_EVENT_SAMPLER_H 80