[PATCH libinput 05/20] touchpad: move button-related code into a separate file

Hans de Goede hdegoede at redhat.com
Tue Apr 15 05:28:02 PDT 2014


From: Peter Hutterer <peter.hutterer at who-t.net>

This is about to become more complicated with the support for software button
areas. Move it to a separate file to have it logically grouped together.
No functional changes.

Signed-off-by: Peter Hutterer <peter.hutterer at who-t.net>
Signed-off-by: Hans de Goede <hdegoede at redhat.com>
---
 src/Makefile.am                 |   1 +
 src/evdev-mt-touchpad-buttons.c | 146 ++++++++++++++++++++++++++++++++++++++++
 src/evdev-mt-touchpad.c         | 102 ++--------------------------
 src/evdev-mt-touchpad.h         |  11 +++
 4 files changed, 162 insertions(+), 98 deletions(-)
 create mode 100644 src/evdev-mt-touchpad-buttons.c

diff --git a/src/Makefile.am b/src/Makefile.am
index 579ed25..ffa6a29 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -14,6 +14,7 @@ libinput_la_SOURCES =			\
 	evdev-mt-touchpad.c		\
 	evdev-mt-touchpad.h		\
 	evdev-mt-touchpad-tap.c		\
+	evdev-mt-touchpad-buttons.c	\
 	evdev-touchpad.c		\
 	filter.c			\
 	filter.h			\
diff --git a/src/evdev-mt-touchpad-buttons.c b/src/evdev-mt-touchpad-buttons.c
new file mode 100644
index 0000000..8946fc7
--- /dev/null
+++ b/src/evdev-mt-touchpad-buttons.c
@@ -0,0 +1,146 @@
+/*
+ * Copyright © 2014 Red Hat, Inc.
+ *
+ * Permission to use, copy, modify, distribute, and sell this software and
+ * its documentation for any purpose is hereby granted without fee, provided
+ * that the above copyright notice appear in all copies and that both that
+ * copyright notice and this permission notice appear in supporting
+ * documentation, and that the name of the copyright holders not be used in
+ * advertising or publicity pertaining to distribution of the software
+ * without specific, written prior permission.  The copyright holders make
+ * no representations about the suitability of this software for any
+ * purpose.  It is provided "as is" without express or implied warranty.
+ *
+ * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS
+ * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
+ * FITNESS, IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY
+ * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
+ * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF
+ * CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
+ * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+ */
+
+#include <math.h>
+
+#include "evdev-mt-touchpad.h"
+
+#define DEFAULT_BUTTON_MOTION_THRESHOLD 0.02 /* in percent of size */
+
+int
+tp_process_button(struct tp_dispatch *tp,
+		  const struct input_event *e,
+		  uint32_t time)
+{
+	uint32_t mask = 1 << (e->code - BTN_LEFT);
+	if (e->value) {
+		tp->buttons.state |= mask;
+		tp->queued |= TOUCHPAD_EVENT_BUTTON_PRESS;
+	} else {
+		tp->buttons.state &= ~mask;
+		tp->queued |= TOUCHPAD_EVENT_BUTTON_RELEASE;
+	}
+
+	return 0;
+}
+
+int
+tp_init_buttons(struct tp_dispatch *tp,
+		struct evdev_device *device)
+{
+	int width, height;
+	double diagonal;
+
+	if (libevdev_has_event_code(device->evdev, EV_KEY, BTN_MIDDLE) ||
+	    libevdev_has_event_code(device->evdev, EV_KEY, BTN_RIGHT))
+		tp->buttons.has_buttons = true;
+
+	width = abs(device->abs.max_x - device->abs.min_x);
+	height = abs(device->abs.max_y - device->abs.min_y);
+	diagonal = sqrt(width*width + height*height);
+
+	tp->buttons.motion_dist = diagonal * DEFAULT_BUTTON_MOTION_THRESHOLD;
+
+	return 0;
+}
+
+static int
+tp_post_clickfinger_buttons(struct tp_dispatch *tp, uint32_t time)
+{
+	uint32_t current, old, button;
+	enum libinput_pointer_button_state state;
+
+	current = tp->buttons.state;
+	old = tp->buttons.old_state;
+
+	if (current == old)
+		return 0;
+
+	switch (tp->nfingers_down) {
+		case 1: button = BTN_LEFT; break;
+		case 2: button = BTN_RIGHT; break;
+		case 3: button = BTN_MIDDLE; break;
+		default:
+			return 0;
+	}
+
+	if (current)
+		state = LIBINPUT_POINTER_BUTTON_STATE_PRESSED;
+	else
+		state = LIBINPUT_POINTER_BUTTON_STATE_RELEASED;
+
+	pointer_notify_button(&tp->device->base,
+			      time,
+			      button,
+			      state);
+	return 1;
+}
+
+static int
+tp_post_physical_buttons(struct tp_dispatch *tp, uint32_t time)
+{
+	uint32_t current, old, button;
+
+	current = tp->buttons.state;
+	old = tp->buttons.old_state;
+	button = BTN_LEFT;
+
+	while (current || old) {
+		enum libinput_pointer_button_state state;
+
+		if ((current & 0x1) ^ (old & 0x1)) {
+			if (!!(current & 0x1))
+				state = LIBINPUT_POINTER_BUTTON_STATE_PRESSED;
+			else
+				state = LIBINPUT_POINTER_BUTTON_STATE_RELEASED;
+
+			pointer_notify_button(&tp->device->base,
+					      time,
+					      button,
+					      state);
+		}
+
+		button++;
+		current >>= 1;
+		old >>= 1;
+	}
+
+	return 0;
+}
+
+int
+tp_post_button_events(struct tp_dispatch *tp, uint32_t time)
+{
+	int rc;
+
+	if ((tp->queued &
+		(TOUCHPAD_EVENT_BUTTON_PRESS|TOUCHPAD_EVENT_BUTTON_RELEASE)) == 0)
+				return 0;
+
+	if (tp->buttons.has_buttons)
+		rc = tp_post_physical_buttons(tp, time);
+	else
+		rc = tp_post_clickfinger_buttons(tp, time);
+
+	return rc;
+}
+
diff --git a/src/evdev-mt-touchpad.c b/src/evdev-mt-touchpad.c
index fbe0a7b..1873569 100644
--- a/src/evdev-mt-touchpad.c
+++ b/src/evdev-mt-touchpad.c
@@ -32,7 +32,6 @@
 #define DEFAULT_MIN_ACCEL_FACTOR 0.16
 #define DEFAULT_MAX_ACCEL_FACTOR 1.0
 #define DEFAULT_HYSTERESIS_MARGIN_DENOMINATOR 700.0
-#define DEFAULT_BUTTON_MOTION_THRESHOLD 0.02 /* in percent of size */
 
 static inline int
 tp_hysteresis(int in, int center, int margin)
@@ -322,20 +321,11 @@ tp_process_key(struct tp_dispatch *tp,
 	       const struct input_event *e,
 	       uint32_t time)
 {
-	uint32_t mask;
-
 	switch (e->code) {
 		case BTN_LEFT:
 		case BTN_MIDDLE:
 		case BTN_RIGHT:
-			mask = 1 << (e->code - BTN_LEFT);
-			if (e->value) {
-				tp->buttons.state |= mask;
-				tp->queued |= TOUCHPAD_EVENT_BUTTON_PRESS;
-			} else {
-				tp->buttons.state &= ~mask;
-				tp->queued |= TOUCHPAD_EVENT_BUTTON_RELEASE;
-			}
+			tp_process_button(tp, e, time);
 			break;
 		case BTN_TOUCH:
 		case BTN_TOOL_DOUBLETAP:
@@ -533,87 +523,6 @@ tp_post_scroll_events(struct tp_dispatch *tp, uint32_t time)
 	return 0;
 }
 
-static int
-tp_post_clickfinger_buttons(struct tp_dispatch *tp, uint32_t time)
-{
-	uint32_t current, old, button;
-	enum libinput_pointer_button_state state;
-
-	current = tp->buttons.state;
-	old = tp->buttons.old_state;
-
-	if (current == old)
-		return 0;
-
-	switch (tp->nfingers_down) {
-		case 1: button = BTN_LEFT; break;
-		case 2: button = BTN_RIGHT; break;
-		case 3: button = BTN_MIDDLE; break;
-		default:
-			return 0;
-	}
-
-	if (current)
-		state = LIBINPUT_POINTER_BUTTON_STATE_PRESSED;
-	else
-		state = LIBINPUT_POINTER_BUTTON_STATE_RELEASED;
-
-	pointer_notify_button(&tp->device->base,
-			      time,
-			      button,
-			      state);
-	return 1;
-}
-
-static int
-tp_post_physical_buttons(struct tp_dispatch *tp, uint32_t time)
-{
-	uint32_t current, old, button;
-
-	current = tp->buttons.state;
-	old = tp->buttons.old_state;
-	button = BTN_LEFT;
-
-	while (current || old) {
-		enum libinput_pointer_button_state state;
-
-		if ((current & 0x1) ^ (old & 0x1)) {
-			if (!!(current & 0x1))
-				state = LIBINPUT_POINTER_BUTTON_STATE_PRESSED;
-			else
-				state = LIBINPUT_POINTER_BUTTON_STATE_RELEASED;
-
-			pointer_notify_button(&tp->device->base,
-					      time,
-					      button,
-					      state);
-		}
-
-		button++;
-		current >>= 1;
-		old >>= 1;
-	}
-
-	return 0;
-}
-
-static int
-tp_post_button_events(struct tp_dispatch *tp, uint32_t time)
-{
-	int rc;
-
-	if ((tp->queued &
-		(TOUCHPAD_EVENT_BUTTON_PRESS|TOUCHPAD_EVENT_BUTTON_RELEASE)) == 0)
-				return 0;
-
-	if (tp->buttons.has_buttons)
-		rc = tp_post_physical_buttons(tp, time);
-	else
-		rc = tp_post_clickfinger_buttons(tp, time);
-
-	return rc;
-}
-
 static void
 tp_post_events(struct tp_dispatch *tp, uint32_t time)
 {
@@ -791,12 +700,6 @@ tp_init(struct tp_dispatch *tp,
 	tp->hysteresis.margin_y =
 		diagonal / DEFAULT_HYSTERESIS_MARGIN_DENOMINATOR;
 
-	tp->buttons.motion_dist = diagonal * DEFAULT_BUTTON_MOTION_THRESHOLD;
-
-	if (libevdev_has_event_code(device->evdev, EV_KEY, BTN_MIDDLE) ||
-	    libevdev_has_event_code(device->evdev, EV_KEY, BTN_RIGHT))
-		tp->buttons.has_buttons = true;
-
 	if (tp_init_scroll(tp) != 0)
 		return -1;
 
@@ -806,6 +709,9 @@ tp_init(struct tp_dispatch *tp,
 	if (tp_init_tap(tp) != 0)
 		return -1;
 
+	if (tp_init_buttons(tp, device) != 0)
+		return -1;
+
 	return 0;
 }
 
diff --git a/src/evdev-mt-touchpad.h b/src/evdev-mt-touchpad.h
index e5fbd7a..d84c9e8 100644
--- a/src/evdev-mt-touchpad.h
+++ b/src/evdev-mt-touchpad.h
@@ -168,4 +168,15 @@ tp_init_tap(struct tp_dispatch *tp);
 void
 tp_destroy_tap(struct tp_dispatch *tp);
 
+int
+tp_init_buttons(struct tp_dispatch *tp, struct evdev_device *device);
+
+int
+tp_process_button(struct tp_dispatch *tp,
+		  const struct input_event *e,
+		  uint32_t time);
+
+int
+tp_post_button_events(struct tp_dispatch *tp, uint32_t time);
+
 #endif
-- 
1.9.0



More information about the wayland-devel mailing list