Updating trunk VERSION from 2139.0 to 2140.0
[chromium-blink-merge.git] / ui / events / ozone / evdev / libgestures_glue / gesture_timer_provider.cc
blob92f15b77825ee1e0c073d1753b8763c8aca5e567
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 class GesturesTimer {
13 public:
14 GesturesTimer() : callback_(NULL), callback_data_(NULL) {}
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 struct timespec ts;
32 CHECK(!clock_gettime(CLOCK_MONOTONIC, &ts));
33 stime_t next_delay = callback_(StimeFromTimespec(&ts), callback_data_);
34 if (next_delay >= 0) {
35 timer_.Start(FROM_HERE,
36 base::TimeDelta::FromMicroseconds(
37 next_delay * base::Time::kMicrosecondsPerSecond),
38 this,
39 &GesturesTimer::OnTimerExpired);
43 GesturesTimerCallback callback_;
44 void* callback_data_;
45 base::OneShotTimer<GesturesTimer> timer_;
48 namespace ui {
50 namespace {
52 GesturesTimer* GesturesTimerCreate(void* data) { return new GesturesTimer; }
54 void GesturesTimerSet(void* data,
55 GesturesTimer* timer,
56 stime_t delay,
57 GesturesTimerCallback callback,
58 void* callback_data) {
59 timer->Set(delay, callback, callback_data);
62 void GesturesTimerCancel(void* data, GesturesTimer* timer) { timer->Cancel(); }
64 void GesturesTimerFree(void* data, GesturesTimer* timer) { delete timer; }
66 } // namespace
68 const GesturesTimerProvider kGestureTimerProvider = {
69 GesturesTimerCreate, GesturesTimerSet, GesturesTimerCancel,
70 GesturesTimerFree};
72 } // namespace ui