1 /*
2  * Copyright © 2012 Jonas Ådahl
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a
5  * copy of this software and associated documentation files (the "Software"),
6  * to deal in the Software without restriction, including without limitation
7  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8  * and/or sell copies of the Software, and to permit persons to whom the
9  * Software is furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice (including the next
12  * paragraph) shall be included in all copies or substantial portions of the
13  * Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21  * DEALINGS IN THE SOFTWARE.
22  */
23 
24 #ifndef FILTER_PRIVATE_H
25 #define FILTER_PRIVATE_H
26 
27 #include "config.h"
28 
29 #include "filter.h"
30 
31 struct motion_filter_interface {
32 	enum libinput_config_accel_profile type;
33 	struct normalized_coords (*filter)(
34 			   struct motion_filter *filter,
35 			   const struct device_float_coords *unaccelerated,
36 			   void *data, uint64_t time);
37 	struct normalized_coords (*filter_constant)(
38 			   struct motion_filter *filter,
39 			   const struct device_float_coords *unaccelerated,
40 			   void *data, uint64_t time);
41 	void (*restart)(struct motion_filter *filter,
42 			void *data,
43 			uint64_t time);
44 	void (*destroy)(struct motion_filter *filter);
45 	bool (*set_speed)(struct motion_filter *filter,
46 			  double speed_adjustment);
47 };
48 
49 struct motion_filter {
50 	double speed_adjustment; /* normalized [-1, 1] */
51 	struct motion_filter_interface *interface;
52 };
53 
54 struct pointer_tracker {
55 	struct device_float_coords delta; /* delta to most recent event */
56 	uint64_t time;  /* us */
57 	uint32_t dir;
58 };
59 
60 /* For smoothing timestamps from devices with unreliable timing */
61 struct pointer_delta_smoothener {
62 	uint64_t threshold;
63 	uint64_t value;
64 };
65 
66 struct pointer_trackers {
67 	struct pointer_tracker *trackers;
68 	size_t ntrackers;
69 	unsigned int cur_tracker;
70 
71 	struct pointer_delta_smoothener *smoothener;
72 };
73 
74 void trackers_init(struct pointer_trackers *trackers, int ntrackers);
75 void trackers_free(struct pointer_trackers *trackers);
76 
77 void
78 trackers_reset(struct pointer_trackers *trackers,
79 	       uint64_t time);
80 void
81 trackers_feed(struct pointer_trackers *trackers,
82 	      const struct device_float_coords *delta,
83 	      uint64_t time);
84 
85 struct pointer_tracker *
86 trackers_by_offset(struct pointer_trackers *trackers, unsigned int offset);
87 
88 double
89 trackers_velocity(struct pointer_trackers *trackers, uint64_t time);
90 
91 double
92 calculate_acceleration_simpsons(struct motion_filter *filter,
93 				accel_profile_func_t profile,
94 				void *data,
95 				double velocity,
96 				double last_velocity,
97 				uint64_t time);
98 
99 /* Convert speed/velocity from units/us to units/ms */
100 static inline double
v_us2ms(double units_per_us)101 v_us2ms(double units_per_us)
102 {
103 	return units_per_us * 1000.0;
104 }
105 
106 static inline double
v_us2s(double units_per_us)107 v_us2s(double units_per_us)
108 {
109 	return units_per_us * 1000000.0;
110 }
111 
112 /* Convert speed/velocity from units/ms to units/us */
113 static inline double
v_ms2us(double units_per_ms)114 v_ms2us(double units_per_ms)
115 {
116 	return units_per_ms/1000.0;
117 }
118 
119 static inline struct normalized_coords
normalize_for_dpi(const struct device_float_coords * coords,int dpi)120 normalize_for_dpi(const struct device_float_coords *coords, int dpi)
121 {
122 	struct normalized_coords norm;
123 
124 	norm.x = coords->x * DEFAULT_MOUSE_DPI/dpi;
125 	norm.y = coords->y * DEFAULT_MOUSE_DPI/dpi;
126 
127 	return norm;
128 }
129 
130 #endif
131