• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2012 The ChromiumOS Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #include "include/integral_gesture_filter_interpreter.h"
6 
7 #include <math.h>
8 
9 #include "include/gestures.h"
10 #include "include/interpreter.h"
11 #include "include/logging.h"
12 #include "include/tracer.h"
13 
14 namespace gestures {
15 
16 // Takes ownership of |next|:
IntegralGestureFilterInterpreter(Interpreter * next,Tracer * tracer)17 IntegralGestureFilterInterpreter::IntegralGestureFilterInterpreter(
18     Interpreter* next, Tracer* tracer)
19     : FilterInterpreter(NULL, next, tracer, false),
20       hscroll_remainder_(0.0),
21       vscroll_remainder_(0.0),
22       hscroll_ordinal_remainder_(0.0),
23       vscroll_ordinal_remainder_(0.0),
24       remainder_reset_deadline_(NO_DEADLINE) {
25   InitName();
26 }
27 
SyncInterpretImpl(HardwareState * hwstate,stime_t * timeout)28 void IntegralGestureFilterInterpreter::SyncInterpretImpl(
29     HardwareState* hwstate, stime_t* timeout) {
30   can_clear_remainders_ = hwstate->finger_cnt == 0 && hwstate->touch_cnt == 0;
31   stime_t next_timeout = NO_DEADLINE;
32   next_->SyncInterpret(hwstate, &next_timeout);
33   *timeout = SetNextDeadlineAndReturnTimeoutVal(
34       hwstate->timestamp, remainder_reset_deadline_, next_timeout);
35 }
36 
HandleTimerImpl(stime_t now,stime_t * timeout)37 void IntegralGestureFilterInterpreter::HandleTimerImpl(
38     stime_t now, stime_t *timeout) {
39   if (ShouldCallNextTimer(remainder_reset_deadline_)) {
40     if (next_timer_deadline_ > now) {
41       Err("Spurious callback. now: %f, next deadline: %f",
42           now, next_timer_deadline_);
43       return;
44     }
45 
46     stime_t next_timeout = NO_DEADLINE;
47     next_->HandleTimer(now, &next_timeout);
48     *timeout = SetNextDeadlineAndReturnTimeoutVal(now,
49                                                   remainder_reset_deadline_,
50                                                   next_timeout);
51   } else {
52     if (remainder_reset_deadline_ > now) {
53       Err("Spurious callback. now: %f, remainder reset deadline: %f",
54           now, remainder_reset_deadline_);
55       return;
56     }
57     if (can_clear_remainders_)
58       hscroll_ordinal_remainder_ = vscroll_ordinal_remainder_ =
59           hscroll_remainder_ = vscroll_remainder_ = 0.0;
60 
61     remainder_reset_deadline_ = NO_DEADLINE;
62     stime_t next_timeout =
63       next_timer_deadline_ == NO_DEADLINE || next_timer_deadline_ <= now ?
64       NO_DEADLINE : next_timer_deadline_ - now;
65     *timeout = SetNextDeadlineAndReturnTimeoutVal(now,
66                                                   remainder_reset_deadline_,
67                                                   next_timeout);
68   }
69 }
70 
71 namespace {
Truncate(float input,float * overflow)72 float Truncate(float input, float* overflow) {
73   input += *overflow;
74   float input_ret = truncf(input);
75   *overflow = input - input_ret;
76   return input_ret;
77 }
78 }  // namespace {}
79 
80 // Truncate the fractional part off any input, but store it. If the
81 // absolute value of an input is < 1, we will change it to 0, unless
82 // there has been enough fractional accumulation to bring it above 1.
ConsumeGesture(const Gesture & gesture)83 void IntegralGestureFilterInterpreter::ConsumeGesture(const Gesture& gesture) {
84   Gesture copy = gesture;
85   switch (gesture.type) {
86     case kGestureTypeMove:
87       if (gesture.details.move.dx != 0.0 || gesture.details.move.dy != 0.0 ||
88           gesture.details.move.ordinal_dx != 0.0 ||
89           gesture.details.move.ordinal_dy != 0.0)
90         ProduceGesture(gesture);
91       break;
92     case kGestureTypeScroll:
93       copy.details.scroll.dx = Truncate(copy.details.scroll.dx,
94                                         &hscroll_remainder_);
95       copy.details.scroll.dy = Truncate(copy.details.scroll.dy,
96                                         &vscroll_remainder_);
97       copy.details.scroll.ordinal_dx = Truncate(copy.details.scroll.ordinal_dx,
98                                                 &hscroll_ordinal_remainder_);
99       copy.details.scroll.ordinal_dy = Truncate(copy.details.scroll.ordinal_dy,
100                                                 &vscroll_ordinal_remainder_);
101       if (copy.details.scroll.dx != 0.0 || copy.details.scroll.dy != 0.0 ||
102           copy.details.scroll.ordinal_dx != 0.0 ||
103           copy.details.scroll.ordinal_dy != 0.0) {
104         ProduceGesture(copy);
105       } else if (copy.details.scroll.stop_fling) {
106         ProduceGesture(Gesture(kGestureFling, copy.start_time, copy.end_time,
107                                0, 0, GESTURES_FLING_TAP_DOWN));
108       }
109       remainder_reset_deadline_ = copy.end_time + 1.0;
110       break;
111     case kGestureTypeMouseWheel:
112       copy.details.wheel.dx = Truncate(copy.details.wheel.dx,
113                                        &hscroll_remainder_);
114       copy.details.wheel.dy = Truncate(copy.details.wheel.dy,
115                                        &vscroll_remainder_);
116       if (copy.details.wheel.dx != 0.0 || copy.details.wheel.dy != 0.0 ||
117           copy.details.wheel.tick_120ths_dx != 0.0 ||
118           copy.details.wheel.tick_120ths_dy != 0.0) {
119         ProduceGesture(copy);
120       }
121       remainder_reset_deadline_ = copy.end_time + 1.0;
122       break;
123     default:
124       ProduceGesture(gesture);
125       break;
126   }
127 }
128 
129 }  // namespace gestures
130