summaryrefslogtreecommitdiff
path: root/services/inputflinger/reader/mapper/gestures/TimerProvider.h
blob: 7c870e0a0f84b5d2f31642c0cfec8d051e577719 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
/*
 * Copyright 2023 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#pragma once

#include <functional>
#include <list>
#include <map>
#include <memory>
#include <vector>

#include <utils/Timers.h>

#include "InputReaderContext.h"
#include "NotifyArgs.h"
#include "include/gestures.h"

namespace android {

extern const GesturesTimerProvider kGestureTimerProvider;

// Implementation of a gestures library timer provider, which allows the library to set and cancel
// callbacks.
class TimerProvider {
public:
    TimerProvider(InputReaderContext& context);
    virtual ~TimerProvider() = default;

    // Disable copy and move, since pointers to TimerProvider objects are used in callbacks.
    TimerProvider(const TimerProvider&) = delete;
    TimerProvider& operator=(const TimerProvider&) = delete;

    std::string dump();
    void triggerCallbacks(nsecs_t when);

    // Methods to be called by the gestures library:
    GesturesTimer* createTimer();
    void setDeadline(GesturesTimer* timer, nsecs_t delay, GesturesTimerCallback callback,
                     void* callbackData);
    void cancelTimer(GesturesTimer* timer);
    void freeTimer(GesturesTimer* timer);

protected:
    // A wrapper for the system clock, to allow tests to override it.
    virtual nsecs_t getCurrentTime();

private:
    void setDeadlineWithoutRequestingTimeout(GesturesTimer* timer, nsecs_t delay,
                                             GesturesTimerCallback callback, void* callbackData);
    // Requests a timeout from the InputReader for the nearest deadline in mDeadlines. Must be
    // called whenever mDeadlines is modified.
    void requestTimeout();

    InputReaderContext& mReaderContext;
    int mNextTimerId = 0;
    std::vector<std::unique_ptr<GesturesTimer>> mTimers;

    struct Deadline {
        Deadline(std::function<void(nsecs_t)> callback, int timerId)
              : callback(callback), timerId(timerId) {}
        const std::function<void(nsecs_t)> callback;
        const int timerId;
    };

    std::multimap<nsecs_t /*time*/, Deadline> mDeadlines;
};

} // namespace android

// Represents a "timer" registered by the gestures library. In practice, this just means a set of
// deadlines that can be cancelled as a group. The library's API requires this to be in the
// top-level namespace.
struct GesturesTimer {
    int id = -1;
};