xref: /aosp_15_r20/external/libchrome-gestures/src/box_filter_interpreter.cc (revision aed3e5085e770be5b69ce25295ecf6ddf906af95)
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/box_filter_interpreter.h"
6 
7 #include "include/macros.h"
8 #include "include/tracer.h"
9 #include "include/util.h"
10 
11 namespace gestures {
12 
BoxFilterInterpreter(PropRegistry * prop_reg,Interpreter * next,Tracer * tracer)13 BoxFilterInterpreter::BoxFilterInterpreter(PropRegistry* prop_reg,
14                                            Interpreter* next,
15                                            Tracer* tracer)
16     : FilterInterpreter(nullptr, next, tracer, false),
17       box_width_(prop_reg, "Box Width", 0.0),
18       box_height_(prop_reg, "Box Height", 0.0) {
19   InitName();
20 }
21 
SyncInterpretImpl(HardwareState & hwstate,stime_t * timeout)22 void BoxFilterInterpreter::SyncInterpretImpl(HardwareState& hwstate,
23                                              stime_t* timeout) {
24   const char name[] = "BoxFilterInterpreter::SyncInterpretImpl";
25   LogHardwareStatePre(name, hwstate);
26 
27   if (box_width_.val_ == 0.0 && box_height_.val_ == 0.0) {
28     LogHardwareStatePost(name, hwstate);
29     next_->SyncInterpret(hwstate, timeout);
30     return;
31   }
32   RemoveMissingIdsFromMap(&previous_output_, hwstate);
33 
34   const float kHalfWidth = box_width_.val_ * 0.5;
35   const float kHalfHeight = box_height_.val_ * 0.5;
36 
37   for (size_t i = 0; i < hwstate.finger_cnt; i++) {
38     FingerState& fs = hwstate.fingers[i];
39     // If it's new, pass it through
40     if (!MapContainsKey(previous_output_, fs.tracking_id))
41       continue;
42     const FingerState& prev_out = previous_output_[fs.tracking_id];
43     float FingerState::*fields[] = { &FingerState::position_x,
44                                      &FingerState::position_y };
45     const float kBounds[] = { kHalfWidth, kHalfHeight };
46     unsigned warp[] = { GESTURES_FINGER_WARP_X_MOVE,
47                         GESTURES_FINGER_WARP_Y_MOVE };
48     for (size_t f_idx = 0; f_idx < arraysize(fields); f_idx++) {
49       if (fs.flags & warp[f_idx])  // If warping, just move to the new point
50         continue;
51       float FingerState::*field = fields[f_idx];
52       float prev_out_val = prev_out.*field;
53       float val = fs.*field;
54       float bound = kBounds[f_idx];
55 
56       if (prev_out_val - bound < val && val < prev_out_val + bound) {
57         // keep box in place
58         fs.*field = prev_out_val;
59       } else if (val > prev_out_val) {
60         fs.*field -= bound;
61       } else {
62         fs.*field += bound;
63       }
64     }
65   }
66 
67   for (size_t i = 0; i < hwstate.finger_cnt; i++)
68     previous_output_[hwstate.fingers[i].tracking_id] = hwstate.fingers[i];
69 
70   LogHardwareStatePost(name, hwstate);
71   next_->SyncInterpret(hwstate, timeout);
72 }
73 
74 }  // namespace gestures
75