History

input: add multitouch support functions

The goal of multitouch support is to interpret as little as possible: mpv just stores the touch point information and makes the position available for query. Does not participate in the deprecated input section system. The API is modeled after the common part of the touch input APIs of Win32, X11, and Wayland, to make sure the platform-specific implementations are as simple as possible.

nanahi committed 2 years ago

Commit 28a4fb81 parent 91bb2f22 files · +102 −0Browse files

Unified · Split

2 files
  1. input/input.c +92 −0
  2. input/input.h +10 −0
input/input.c+92−0
@@ -93,6 +93,11 @@ struct wheel_state { · 92 unmodified lines
9393 double unit_accum;
9494 };
9595
96+struct touch_point {
97+ int id;
98+ int x, y;
99+};
100+
96101 struct input_ctx {
97102 mp_mutex mutex;
98103 struct mp_log *log;
@@ -143,6 +148,10 @@ struct input_ctx { · 44 unmodified lines
143148 struct active_section *active_sections;
144149 int num_active_sections;
145150
151+ // List currently active touch points
152+ struct touch_point *touch_points;
153+ int num_touch_points;
154+
146155 unsigned int mouse_event_counter;
147156
148157 struct mp_input_src *sources[MP_MAX_SOURCES];
@@ -894,6 +903,88 @@ void mp_input_set_mouse_pos(struct input_ctx *ictx, int x, int y) · 745 unmodified lines
894903 input_unlock(ictx);
895904 }
896905
906+static int find_touch_point_index(struct input_ctx *ictx, int id)
907+{
908+ for (int i = 0; i < ictx->num_touch_points; i++) {
909+ if (ictx->touch_points[i].id == id)
910+ return i;
911+ }
912+ return -1;
913+}
914+
915+static void notify_touch_update(struct input_ctx *ictx)
916+{
917+ // queue dummy cmd so that touch-pos can notify observers
918+ mp_cmd_t *cmd = mp_input_parse_cmd(ictx, bstr0("ignore"), "<internal>");
919+ queue_cmd(ictx, cmd);
920+}
921+
922+static void update_touch_point(struct input_ctx *ictx, int idx, int id, int x, int y)
923+{
924+ MP_TRACE(ictx, "Touch point %d update (id %d) %d/%d\n",
925+ idx, id, x, y);
926+ if (ictx->touch_points[idx].x == x && ictx->touch_points[idx].y == y)
927+ return;
928+ ictx->touch_points[idx].x = x;
929+ ictx->touch_points[idx].y = y;
930+ notify_touch_update(ictx);
931+}
932+
933+void mp_input_add_touch_point(struct input_ctx *ictx, int id, int x, int y)
934+{
935+ input_lock(ictx);
936+ int idx = find_touch_point_index(ictx, id);
937+ if (idx != -1) {
938+ MP_WARN(ictx, "Touch point %d (id %d) already exists! Treat as update.\n",
939+ idx, id);
940+ update_touch_point(ictx, idx, id, x, y);
941+ } else {
942+ MP_TRACE(ictx, "Touch point %d add (id %d) %d/%d\n",
943+ ictx->num_touch_points, id, x, y);
944+ MP_TARRAY_APPEND(ictx, ictx->touch_points, ictx->num_touch_points,
945+ (struct touch_point){id, x, y});
946+ notify_touch_update(ictx);
947+ }
948+ input_unlock(ictx);
949+}
950+
951+void mp_input_update_touch_point(struct input_ctx *ictx, int id, int x, int y)
952+{
953+ input_lock(ictx);
954+ int idx = find_touch_point_index(ictx, id);
955+ if (idx != -1) {
956+ update_touch_point(ictx, idx, id, x, y);
957+ } else {
958+ MP_WARN(ictx, "Touch point id %d does not exist!\n", id);
959+ }
960+ input_unlock(ictx);
961+}
962+
963+void mp_input_remove_touch_point(struct input_ctx *ictx, int id)
964+{
965+ input_lock(ictx);
966+ int idx = find_touch_point_index(ictx, id);
967+ if (idx != -1) {
968+ MP_TRACE(ictx, "Touch point %d remove (id %d)\n", idx, id);
969+ MP_TARRAY_REMOVE_AT(ictx->touch_points, ictx->num_touch_points, idx);
970+ notify_touch_update(ictx);
971+ }
972+ input_unlock(ictx);
973+}
974+
975+int mp_input_get_touch_pos(struct input_ctx *ictx, int count, int *x, int *y, int *id)
976+{
977+ input_lock(ictx);
978+ int num_touch_points = ictx->num_touch_points;
979+ for (int i = 0; i < MPMIN(num_touch_points, count); i++) {
980+ x[i] = ictx->touch_points[i].x;
981+ y[i] = ictx->touch_points[i].y;
982+ id[i] = ictx->touch_points[i].id;
983+ }
984+ input_unlock(ictx);
985+ return num_touch_points;
986+}
987+
897988 static bool test_mouse(struct input_ctx *ictx, int x, int y, int rej_flags)
898989 {
899990 bool res = false;
@@ -1348,6 +1439,7 @@ struct input_ctx *mp_input_init(struct mpv_global *global, · 448 unmodified lines
13481439 .wakeup_cb = wakeup_cb,
13491440 .wakeup_ctx = wakeup_ctx,
13501441 .active_sections = talloc_array(ictx, struct active_section, 0),
1442+ .touch_points = talloc_array(ictx, struct touch_point, 0),
13511443 };
13521444
13531445 ictx->opts = ictx->opts_cache->opts;
input/input.h+10−0
@@ -102,6 +102,16 @@ void mp_input_set_mouse_pos_artificial(struct input_ctx *ictx, int x, int y); · 101 unmodified lines
102102
103103 void mp_input_get_mouse_pos(struct input_ctx *ictx, int *x, int *y, int *hover);
104104
105+// Add/Update/Remove a touch point (in window coordinates).
106+void mp_input_add_touch_point(struct input_ctx *ictx, int id, int x, int y);
107+void mp_input_update_touch_point(struct input_ctx *ictx, int id, int x, int y);
108+void mp_input_remove_touch_point(struct input_ctx *ictx, int id);
109+
110+// Get the positions of the touch points. xs and ys are arrays of at least
111+// count elements. ids is an array of at least count elements to uniquely
112+// identify touch points. Return the current number of touch points.
113+int mp_input_get_touch_pos(struct input_ctx *ictx, int count, int *xs, int *ys, int *ids);
114+
105115 // Return whether we want/accept mouse input.
106116 bool mp_input_mouse_enabled(struct input_ctx *ictx);
107117

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t