[PATCH libinput 2/3] Add a delta_coords type and use it were applicable

Hans de Goede hdegoede at redhat.com
Tue Mar 24 05:14:18 PDT 2015


tp_normalize_coords is one of the last functions taking separate x, y
values rather a coordinate pair, this commit cleans this up.

Signed-off-by: Hans de Goede <hdegoede at redhat.com>
---
 src/evdev-mt-touchpad-edge-scroll.c |  9 ++-------
 src/evdev-mt-touchpad-tap.c         |  6 ++----
 src/evdev-mt-touchpad.c             | 29 ++++++++++++++---------------
 src/evdev-mt-touchpad.h             | 14 ++++++++------
 src/libinput-private.h              | 20 ++++++++++++++++++++
 5 files changed, 46 insertions(+), 32 deletions(-)

diff --git a/src/evdev-mt-touchpad-edge-scroll.c b/src/evdev-mt-touchpad-edge-scroll.c
index d304316..b83f462 100644
--- a/src/evdev-mt-touchpad-edge-scroll.c
+++ b/src/evdev-mt-touchpad-edge-scroll.c
@@ -313,7 +313,6 @@ tp_edge_scroll_post_events(struct tp_dispatch *tp, uint64_t time)
 	struct tp_touch *t;
 	enum libinput_pointer_axis axis;
 	double *delta;
-	double initial_dx, initial_dy;
 	struct normalized_coords normalized;
 	const struct normalized_coords zero = { 0.0, 0.0 };
 	const struct discrete_coords zero_discrete = { 0.0, 0.0 };
@@ -361,12 +360,8 @@ tp_edge_scroll_post_events(struct tp_dispatch *tp, uint64_t time)
 					 t->scroll.edge_state);
 			break;
 		case EDGE_SCROLL_TOUCH_STATE_EDGE_NEW:
-			initial_dx = t->point.x - t->scroll.initial.x;
-			initial_dy = t->point.y - t->scroll.initial.y;
-			tp_normalize_delta(tp,
-					   initial_dx,
-					   initial_dy,
-					   &normalized);
+			normalized = tp_normalize_delta(tp,
+				device_delta(t->point, t->scroll.initial));
 			if (fabs(*delta) < DEFAULT_SCROLL_THRESHOLD)
 				normalized = zero;
 			break;
diff --git a/src/evdev-mt-touchpad-tap.c b/src/evdev-mt-touchpad-tap.c
index 6bd7c58..cb8fa2e 100644
--- a/src/evdev-mt-touchpad-tap.c
+++ b/src/evdev-mt-touchpad-tap.c
@@ -531,12 +531,10 @@ tp_tap_exceeds_motion_threshold(struct tp_dispatch *tp,
 				struct tp_touch *t)
 {
 	int threshold = DEFAULT_TAP_MOVE_THRESHOLD;
-	double dx, dy;
 	struct normalized_coords normalized;
 
-	dx = abs(t->tap.initial.x - t->point.x);
-	dy = abs(t->tap.initial.y - t->point.y);
-	tp_normalize_delta(tp, dx, dy, &normalized);
+	normalized = tp_normalize_delta(tp,
+				device_delta(t->point, t->tap.initial));
 
 	return normalized.x * normalized.x + normalized.y * normalized.y
 			> threshold * threshold;
diff --git a/src/evdev-mt-touchpad.c b/src/evdev-mt-touchpad.c
index 856af4a..37ad13a 100644
--- a/src/evdev-mt-touchpad.c
+++ b/src/evdev-mt-touchpad.c
@@ -261,23 +261,22 @@ tp_estimate_delta(int x0, int x1, int x2, int x3)
 struct normalized_coords
 tp_get_delta(struct tp_touch *t)
 {
-	double dx, dy; /* in device coords */
-	struct normalized_coords normalized = { 0.0, 0.0 };
+	struct delta_coords delta;
+	const struct normalized_coords zero = { 0.0, 0.0 };
 
 	if (t->history.count < TOUCHPAD_MIN_SAMPLES)
-		return normalized;
-
-	dx = tp_estimate_delta(tp_motion_history_offset(t, 0)->x,
-			       tp_motion_history_offset(t, 1)->x,
-			       tp_motion_history_offset(t, 2)->x,
-			       tp_motion_history_offset(t, 3)->x);
-	dy = tp_estimate_delta(tp_motion_history_offset(t, 0)->y,
-			       tp_motion_history_offset(t, 1)->y,
-			       tp_motion_history_offset(t, 2)->y,
-			       tp_motion_history_offset(t, 3)->y);
-	tp_normalize_delta(t->tp, dx, dy, &normalized);
-
-	return normalized;
+		return zero;
+
+	delta.dx = tp_estimate_delta(tp_motion_history_offset(t, 0)->x,
+				     tp_motion_history_offset(t, 1)->x,
+				     tp_motion_history_offset(t, 2)->x,
+				     tp_motion_history_offset(t, 3)->x);
+	delta.dy = tp_estimate_delta(tp_motion_history_offset(t, 0)->y,
+				     tp_motion_history_offset(t, 1)->y,
+				     tp_motion_history_offset(t, 2)->y,
+				     tp_motion_history_offset(t, 3)->y);
+
+	return tp_normalize_delta(t->tp, delta);
 }
 
 static void
diff --git a/src/evdev-mt-touchpad.h b/src/evdev-mt-touchpad.h
index 9980f90..0c6d87e 100644
--- a/src/evdev-mt-touchpad.h
+++ b/src/evdev-mt-touchpad.h
@@ -274,13 +274,15 @@ struct tp_dispatch {
 #define tp_for_each_touch(_tp, _t) \
 	for (unsigned int _i = 0; _i < (_tp)->ntouches && (_t = &(_tp)->touches[_i]); _i++)
 
-static inline void
-tp_normalize_delta(struct tp_dispatch *tp,
-		   double dx, double dy,
-		   struct normalized_coords *normalized)
+static inline struct normalized_coords
+tp_normalize_delta(struct tp_dispatch *tp, struct delta_coords delta)
 {
-	normalized->x = dx * tp->accel.x_scale_coeff;
-	normalized->y = dy * tp->accel.y_scale_coeff;
+	struct normalized_coords normalized;
+
+	normalized.x = delta.dx * tp->accel.x_scale_coeff;
+	normalized.y = delta.dy * tp->accel.y_scale_coeff;
+
+	return normalized;
 }
 
 struct normalized_coords
diff --git a/src/libinput-private.h b/src/libinput-private.h
index e39ac5e..ec5caf6 100644
--- a/src/libinput-private.h
+++ b/src/libinput-private.h
@@ -37,6 +37,14 @@ struct device_coords {
 	int x, y;
 };
 
+/*
+ * A delta between 2 device coordinates,
+ * may be non discrete because of averaging.
+ */
+struct delta_coords {
+	double dx, dy;
+};
+
 /* A dpi-normalized coordinate pair */
 struct normalized_coords {
 	double x, y;
@@ -353,4 +361,16 @@ libinput_now(struct libinput *libinput)
 
 	return ts.tv_sec * 1000ULL + ts.tv_nsec / 1000000;
 }
+
+static inline struct delta_coords
+device_delta(struct device_coords a, struct device_coords b)
+{
+	struct delta_coords delta;
+
+	delta.dx = a.x - b.x;
+	delta.dy = a.y - b.y;
+
+	return delta;
+}
+
 #endif /* LIBINPUT_PRIVATE_H */
-- 
2.3.3



More information about the wayland-devel mailing list