Pin Chrome's shortcut to the Win10 Start menu on install and OS upgrade.
[chromium-blink-merge.git] / ui / events / ozone / evdev / libgestures_glue / gesture_timer_provider.cc
blob3452d6927d651f543b37a6b9d96276dcb1ce51c4
1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "ui/events/ozone/evdev/libgestures_glue/gesture_timer_provider.h"
7 #include <gestures/gestures.h>
9 #include "base/timer/timer.h"
11 // libgestures requires that this be in the top level namespace.
12 struct GesturesTimer {
13 public:
14 GesturesTimer() {}
15 ~GesturesTimer() {}
17 void Set(stime_t delay, GesturesTimerCallback callback, void* callback_data) {
18 callback_ = callback;
19 callback_data_ = callback_data;
20 timer_.Start(FROM_HERE,
21 base::TimeDelta::FromMicroseconds(
22 delay * base::Time::kMicrosecondsPerSecond),
23 this,
24 &GesturesTimer::OnTimerExpired);
27 void Cancel() { timer_.Stop(); }
29 private:
30 void OnTimerExpired() {
31 // Run the callback and reschedule the next run if requested.
32 stime_t next_delay = callback_(ui::StimeNow(), callback_data_);
33 if (next_delay >= 0) {
34 timer_.Start(FROM_HERE,
35 base::TimeDelta::FromMicroseconds(
36 next_delay * base::Time::kMicrosecondsPerSecond),
37 this,
38 &GesturesTimer::OnTimerExpired);
42 GesturesTimerCallback callback_ = nullptr;
43 void* callback_data_ = nullptr;
44 base::OneShotTimer<GesturesTimer> timer_;
47 namespace ui {
49 namespace {
51 GesturesTimer* GesturesTimerCreate(void* data) { return new GesturesTimer; }
53 void GesturesTimerSet(void* data,
54 GesturesTimer* timer,
55 stime_t delay,
56 GesturesTimerCallback callback,
57 void* callback_data) {
58 timer->Set(delay, callback, callback_data);
61 void GesturesTimerCancel(void* data, GesturesTimer* timer) { timer->Cancel(); }
63 void GesturesTimerFree(void* data, GesturesTimer* timer) { delete timer; }
65 } // namespace
67 stime_t StimeNow() {
68 struct timespec ts;
70 if (clock_gettime(CLOCK_MONOTONIC, &ts))
71 PLOG(FATAL) << "clock_gettime";
73 return StimeFromTimespec(&ts);
76 const GesturesTimerProvider kGestureTimerProvider = {
77 GesturesTimerCreate, GesturesTimerSet, GesturesTimerCancel,
78 GesturesTimerFree};
80 } // namespace ui