1 /* 2 * Copyright 2023 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 #pragma once 18 19 #include <functional> 20 #include <list> 21 #include <map> 22 #include <memory> 23 #include <vector> 24 25 #include <utils/Timers.h> 26 27 #include "InputReaderContext.h" 28 #include "NotifyArgs.h" 29 #include "include/gestures.h" 30 31 namespace android { 32 33 extern const GesturesTimerProvider kGestureTimerProvider; 34 35 // Implementation of a gestures library timer provider, which allows the library to set and cancel 36 // callbacks. 37 class TimerProvider { 38 public: 39 TimerProvider(InputReaderContext& context); 40 virtual ~TimerProvider() = default; 41 42 // Disable copy and move, since pointers to TimerProvider objects are used in callbacks. 43 TimerProvider(const TimerProvider&) = delete; 44 TimerProvider& operator=(const TimerProvider&) = delete; 45 46 std::string dump(); 47 void triggerCallbacks(nsecs_t when); 48 49 // Methods to be called by the gestures library: 50 GesturesTimer* createTimer(); 51 void setDeadline(GesturesTimer* timer, nsecs_t delay, GesturesTimerCallback callback, 52 void* callbackData); 53 void cancelTimer(GesturesTimer* timer); 54 void freeTimer(GesturesTimer* timer); 55 56 protected: 57 // A wrapper for the system clock, to allow tests to override it. 58 virtual nsecs_t getCurrentTime(); 59 60 private: 61 void setDeadlineWithoutRequestingTimeout(GesturesTimer* timer, nsecs_t delay, 62 GesturesTimerCallback callback, void* callbackData); 63 // Requests a timeout from the InputReader for the nearest deadline in mDeadlines. Must be 64 // called whenever mDeadlines is modified. 65 void requestTimeout(); 66 67 InputReaderContext& mReaderContext; 68 int mNextTimerId = 0; 69 std::vector<std::unique_ptr<GesturesTimer>> mTimers; 70 71 struct Deadline { DeadlineDeadline72 Deadline(std::function<void(nsecs_t)> callback, int timerId) 73 : callback(callback), timerId(timerId) {} 74 const std::function<void(nsecs_t)> callback; 75 const int timerId; 76 }; 77 78 std::multimap<nsecs_t /*time*/, Deadline> mDeadlines; 79 }; 80 81 } // namespace android 82 83 // Represents a "timer" registered by the gestures library. In practice, this just means a set of 84 // deadlines that can be cancelled as a group. The library's API requires this to be in the 85 // top-level namespace. 86 struct GesturesTimer { 87 int id = -1; 88 };