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(nullptr, 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 const char name[] = "IntegralGestureFilterInterpreter::SyncInterpretImpl";
31 LogHardwareStatePre(name, hwstate);
32
33 can_clear_remainders_ = hwstate.finger_cnt == 0 && hwstate.touch_cnt == 0;
34 stime_t next_timeout = NO_DEADLINE;
35
36 LogHardwareStatePost(name, hwstate);
37 next_->SyncInterpret(hwstate, &next_timeout);
38 *timeout = SetNextDeadlineAndReturnTimeoutVal(
39 hwstate.timestamp, remainder_reset_deadline_, next_timeout);
40 }
41
HandleTimerImpl(stime_t now,stime_t * timeout)42 void IntegralGestureFilterInterpreter::HandleTimerImpl(
43 stime_t now, stime_t *timeout) {
44 const char name[] = "IntegralGestureFilterInterpreter::HandleTimerImpl";
45 LogHandleTimerPre(name, now, timeout);
46
47 stime_t next_timeout;
48 if (ShouldCallNextTimer(remainder_reset_deadline_)) {
49 if (next_timer_deadline_ > now) {
50 Err("Spurious callback. now: %f, next deadline: %f",
51 now, next_timer_deadline_);
52 return;
53 }
54 next_timeout = NO_DEADLINE;
55 next_->HandleTimer(now, &next_timeout);
56 } else {
57 if (remainder_reset_deadline_ > now) {
58 Err("Spurious callback. now: %f, remainder reset deadline: %f",
59 now, remainder_reset_deadline_);
60 return;
61 }
62 if (can_clear_remainders_)
63 hscroll_ordinal_remainder_ = vscroll_ordinal_remainder_ =
64 hscroll_remainder_ = vscroll_remainder_ = 0.0;
65
66 remainder_reset_deadline_ = NO_DEADLINE;
67 next_timeout = next_timer_deadline_ == NO_DEADLINE ||
68 next_timer_deadline_ <= now
69 ? NO_DEADLINE
70 : next_timer_deadline_ - now;
71 }
72 *timeout = SetNextDeadlineAndReturnTimeoutVal(now,
73 remainder_reset_deadline_,
74 next_timeout);
75 LogHandleTimerPost(name, now, timeout);
76 }
77
78 namespace {
Truncate(float input,float * overflow)79 float Truncate(float input, float* overflow) {
80 input += *overflow;
81 float input_ret = truncf(input);
82 *overflow = input - input_ret;
83 return input_ret;
84 }
85 } // namespace {}
86
87 // Truncate the fractional part off any input, but store it. If the
88 // absolute value of an input is < 1, we will change it to 0, unless
89 // there has been enough fractional accumulation to bring it above 1.
ConsumeGesture(const Gesture & gesture)90 void IntegralGestureFilterInterpreter::ConsumeGesture(const Gesture& gesture) {
91 const char name[] = "IntegralGestureFilterInterpreter::ConsumeGesture";
92 LogGestureConsume(name, gesture);
93
94 Gesture copy = gesture;
95 switch (gesture.type) {
96 case kGestureTypeMove:
97 if (gesture.details.move.dx != 0.0 || gesture.details.move.dy != 0.0 ||
98 gesture.details.move.ordinal_dx != 0.0 ||
99 gesture.details.move.ordinal_dy != 0.0) {
100 LogGestureProduce(name, gesture);
101 ProduceGesture(gesture);
102 }
103 break;
104 case kGestureTypeScroll:
105 copy.details.scroll.dx = Truncate(copy.details.scroll.dx,
106 &hscroll_remainder_);
107 copy.details.scroll.dy = Truncate(copy.details.scroll.dy,
108 &vscroll_remainder_);
109 copy.details.scroll.ordinal_dx = Truncate(copy.details.scroll.ordinal_dx,
110 &hscroll_ordinal_remainder_);
111 copy.details.scroll.ordinal_dy = Truncate(copy.details.scroll.ordinal_dy,
112 &vscroll_ordinal_remainder_);
113 if (copy.details.scroll.dx != 0.0 || copy.details.scroll.dy != 0.0 ||
114 copy.details.scroll.ordinal_dx != 0.0 ||
115 copy.details.scroll.ordinal_dy != 0.0) {
116 LogGestureProduce(name, copy);
117 ProduceGesture(copy);
118 } else if (copy.details.scroll.stop_fling) {
119 auto fling_tap_down = Gesture(kGestureFling,
120 copy.start_time, copy.end_time,
121 0, 0, GESTURES_FLING_TAP_DOWN);
122 LogGestureProduce(name, fling_tap_down);
123 ProduceGesture(fling_tap_down);
124 }
125 remainder_reset_deadline_ = copy.end_time + 1.0;
126 break;
127 case kGestureTypeMouseWheel:
128 copy.details.wheel.dx = Truncate(copy.details.wheel.dx,
129 &hscroll_remainder_);
130 copy.details.wheel.dy = Truncate(copy.details.wheel.dy,
131 &vscroll_remainder_);
132 if (copy.details.wheel.dx != 0.0 || copy.details.wheel.dy != 0.0 ||
133 copy.details.wheel.tick_120ths_dx != 0.0 ||
134 copy.details.wheel.tick_120ths_dy != 0.0) {
135 LogGestureProduce(name, copy);
136 ProduceGesture(copy);
137 }
138 remainder_reset_deadline_ = copy.end_time + 1.0;
139 break;
140 default:
141 LogGestureProduce(name, gesture);
142 ProduceGesture(gesture);
143 break;
144 }
145 }
146
147 } // namespace gestures
148