Flutter Linux Embedder
fl_touch_manager.cc
Go to the documentation of this file.
1 // Copyright 2013 The Flutter Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
7 
8 static constexpr int kMicrosecondsPerMillisecond = 1000;
9 static const int kMinTouchDeviceId = 0;
10 static const int kMaxTouchDeviceId = 128;
11 
12 // State of a touch point that has been added to the engine.
13 typedef struct {
14  // Sequence number this touch point was generated from.
15  uint32_t number;
16 
17  // Last known location of this touch point.
18  gdouble x;
19  gdouble y;
20 } FlTouchPoint;
21 
23  GObject parent_instance;
24 
25  GWeakRef engine;
26 
27  FlutterViewId view_id;
28 
29  // Table of touch ID to #FlTouchPoint for the touch devices that have been
30  // added to the engine.
31  GHashTable* added_touch_devices;
32 
33  GHashTable* number_to_id;
34 
35  // Minimum touch device ID that can be used.
37 };
38 
39 G_DEFINE_TYPE(FlTouchManager, fl_touch_manager, G_TYPE_OBJECT);
40 
41 static void fl_touch_manager_dispose(GObject* object) {
42  FlTouchManager* self = FL_TOUCH_MANAGER(object);
43 
44  g_weak_ref_clear(&self->engine);
45 
46  g_clear_pointer(&self->added_touch_devices, g_hash_table_unref);
47 
48  g_clear_pointer(&self->number_to_id, g_hash_table_unref);
49 
50  G_OBJECT_CLASS(fl_touch_manager_parent_class)->dispose(object);
51 }
52 
53 static void fl_touch_manager_class_init(FlTouchManagerClass* klass) {
54  G_OBJECT_CLASS(klass)->dispose = fl_touch_manager_dispose;
55 }
56 
57 static void fl_touch_manager_init(FlTouchManager* self) {}
58 
59 FlTouchManager* fl_touch_manager_new(FlEngine* engine, FlutterViewId view_id) {
60  g_return_val_if_fail(FL_IS_ENGINE(engine), nullptr);
61 
62  FlTouchManager* self =
63  FL_TOUCH_MANAGER(g_object_new(fl_touch_manager_get_type(), nullptr));
64 
65  g_weak_ref_init(&self->engine, engine);
66  self->view_id = view_id;
67 
68  self->number_to_id =
69  g_hash_table_new_full(g_direct_hash, g_direct_equal, nullptr, nullptr);
70 
71  self->added_touch_devices =
72  g_hash_table_new_full(g_direct_hash, g_direct_equal, nullptr, g_free);
73 
74  self->min_touch_device_id = kMinTouchDeviceId;
75 
76  return self;
77 }
78 
79 // Ensures that a touch add event is sent for the given device and records the
80 // location of this touch point.
82  guint event_time,
83  gdouble x,
84  gdouble y,
85  uint32_t number,
86  int32_t touch_id,
87  int32_t device_id) {
88  FlTouchPoint* point = static_cast<FlTouchPoint*>(g_hash_table_lookup(
89  self->added_touch_devices, GINT_TO_POINTER(touch_id)));
90  if (point != nullptr) {
91  point->x = x;
92  point->y = y;
93  return;
94  }
95 
96  g_autoptr(FlEngine) engine = FL_ENGINE(g_weak_ref_get(&self->engine));
97  if (engine == nullptr) {
98  return;
99  }
100 
101  fl_engine_send_touch_add_event(engine, self->view_id,
102  event_time * kMicrosecondsPerMillisecond, x, y,
103  device_id);
104 
105  point = g_new0(FlTouchPoint, 1);
106  point->number = number;
107  point->x = x;
108  point->y = y;
109  g_hash_table_insert(self->added_touch_devices, GINT_TO_POINTER(touch_id),
110  point);
111 }
112 
113 // Generates a unique ID to represent |number|. The generated ID is the
114 // smallest available ID greater than or equal to the minimum touch device ID.
115 static uint32_t get_generated_id(_FlTouchManager* self, uint32_t number) {
116  gpointer value;
117  if (g_hash_table_lookup_extended(self->number_to_id, GUINT_TO_POINTER(number),
118  nullptr, &value)) {
119  uint32_t id;
120  if (value == nullptr) {
121  id = 0;
122  } else {
123  id = GPOINTER_TO_UINT(value);
124  }
125  return id;
126  }
127  g_autoptr(GList) values = g_hash_table_get_values(self->number_to_id);
128  while (values != nullptr &&
129  g_list_find(values, GUINT_TO_POINTER(self->min_touch_device_id)) !=
130  nullptr &&
131  self->min_touch_device_id < kMaxTouchDeviceId) {
132  ++self->min_touch_device_id;
133  }
134  if (self->min_touch_device_id >= kMaxTouchDeviceId) {
135  self->min_touch_device_id = kMinTouchDeviceId;
136  }
137 
138  g_hash_table_insert(self->number_to_id, GUINT_TO_POINTER(number),
139  GUINT_TO_POINTER(self->min_touch_device_id));
140  return self->min_touch_device_id;
141 }
142 
143 static void release_number(_FlTouchManager* self, uint32_t number) {
144  if (g_hash_table_contains(self->number_to_id, GINT_TO_POINTER(number))) {
145  auto id = g_hash_table_lookup(self->number_to_id, GINT_TO_POINTER(number));
146  if (GPOINTER_TO_UINT(id) < self->min_touch_device_id) {
147  self->min_touch_device_id = GPOINTER_TO_UINT(id);
148  }
149  g_hash_table_remove(self->number_to_id, GINT_TO_POINTER(number));
150  }
151 }
152 
153 // Forgets a touch point that is no longer in contact with the screen.
155  uint32_t number,
156  uint32_t touch_id) {
157  release_number(self, number);
158  g_hash_table_remove(self->added_touch_devices, GINT_TO_POINTER(touch_id));
159 }
160 
161 void fl_touch_manager_handle_touch_event(FlTouchManager* self,
162  GdkEventTouch* touch_event,
163  gint scale_factor) {
164  g_return_if_fail(FL_IS_TOUCH_MANAGER(self));
165 
166  g_autoptr(FlEngine) engine = FL_ENGINE(g_weak_ref_get(&self->engine));
167  if (engine == nullptr) {
168  return;
169  }
170 
171  GdkEvent* event = reinterpret_cast<GdkEvent*>(touch_event);
172  // get sequence id from GdkEvent
173  GdkEventSequence* seq = gdk_event_get_event_sequence(event);
174  // cast pointer to int to get unique id
175  uint32_t id = reinterpret_cast<uint64_t>(seq);
176  // generate touch id from unique id
177  auto touch_id = get_generated_id(self, id);
178  // get device id
179  auto device_id =
180  static_cast<int32_t>(kFlutterPointerDeviceKindTouch) << 28 | touch_id;
181 
182  gdouble event_x = 0.0, event_y = 0.0;
183  gdk_event_get_coords(event, &event_x, &event_y);
184 
185  double x = event_x * scale_factor;
186  double y = event_y * scale_factor;
187 
188  guint event_time = gdk_event_get_time(event);
189 
190  ensure_touch_added(self, event_time, x, y, id, touch_id, device_id);
191 
192  GdkEventType touch_event_type = gdk_event_get_event_type(event);
193 
194  switch (touch_event_type) {
195  case GDK_TOUCH_BEGIN:
196  fl_engine_send_touch_down_event(engine, self->view_id,
197  event_time * kMicrosecondsPerMillisecond,
198  x, y, device_id);
199  break;
200  case GDK_TOUCH_UPDATE:
201  fl_engine_send_touch_move_event(engine, self->view_id,
202  event_time * kMicrosecondsPerMillisecond,
203  x, y, device_id);
204  break;
205  case GDK_TOUCH_END:
206  fl_engine_send_touch_up_event(engine, self->view_id,
207  event_time * kMicrosecondsPerMillisecond, x,
208  y, device_id);
209 
211  engine, self->view_id, event_time * kMicrosecondsPerMillisecond, x, y,
212  device_id);
213  remove_touch_point(self, id, touch_id);
214  break;
215  case GDK_TOUCH_CANCEL:
216  // The touch sequence was aborted, e.g. the compositor took over the
217  // touch to move or resize the window. No touch end event will be
218  // received, so cancel the touch point here.
220  engine, self->view_id, event_time * kMicrosecondsPerMillisecond, x, y,
221  device_id);
222 
224  engine, self->view_id, event_time * kMicrosecondsPerMillisecond, x, y,
225  device_id);
226  remove_touch_point(self, id, touch_id);
227  break;
228  default:
229  break;
230  }
231 }
232 
233 void fl_touch_manager_cancel_input(FlTouchManager* self, guint event_time) {
234  g_return_if_fail(FL_IS_TOUCH_MANAGER(self));
235 
236  if (g_hash_table_size(self->added_touch_devices) == 0) {
237  return;
238  }
239 
240  g_autoptr(FlEngine) engine = FL_ENGINE(g_weak_ref_get(&self->engine));
241  if (engine == nullptr) {
242  return;
243  }
244 
245  GHashTableIter iter;
246  g_hash_table_iter_init(&iter, self->added_touch_devices);
247  gpointer key, value;
248  while (g_hash_table_iter_next(&iter, &key, &value)) {
249  uint32_t touch_id = GPOINTER_TO_UINT(key);
250  FlTouchPoint* point = static_cast<FlTouchPoint*>(value);
251  int32_t device_id =
252  static_cast<int32_t>(kFlutterPointerDeviceKindTouch) << 28 | touch_id;
253 
254  fl_engine_send_touch_cancel_event(engine, self->view_id,
255  event_time * kMicrosecondsPerMillisecond,
256  point->x, point->y, device_id);
257 
258  fl_engine_send_touch_remove_event(engine, self->view_id,
259  event_time * kMicrosecondsPerMillisecond,
260  point->x, point->y, device_id);
261 
262  release_number(self, point->number);
263  }
264 
265  // Every touch point has been removed.
266  g_hash_table_remove_all(self->added_touch_devices);
267 }
g_autoptr(FlEngine) engine
void fl_engine_send_touch_up_event(FlEngine *self, FlutterViewId view_id, size_t timestamp, double x, double y, int32_t device)
Definition: fl_engine.cc:1278
void fl_engine_send_touch_move_event(FlEngine *self, FlutterViewId view_id, size_t timestamp, double x, double y, int32_t device)
Definition: fl_engine.cc:1336
void fl_engine_send_touch_add_event(FlEngine *self, FlutterViewId view_id, size_t timestamp, double x, double y, int32_t device)
Definition: fl_engine.cc:1394
void fl_engine_send_touch_remove_event(FlEngine *self, FlutterViewId view_id, size_t timestamp, double x, double y, int32_t device)
Definition: fl_engine.cc:1423
void fl_engine_send_touch_cancel_event(FlEngine *self, FlutterViewId view_id, size_t timestamp, double x, double y, int32_t device)
Definition: fl_engine.cc:1365
void fl_engine_send_touch_down_event(FlEngine *self, FlutterViewId view_id, size_t timestamp, double x, double y, int32_t device)
Definition: fl_engine.cc:1307
int64_t id
uint8_t value
static void fl_touch_manager_class_init(FlTouchManagerClass *klass)
static void release_number(_FlTouchManager *self, uint32_t number)
static void fl_touch_manager_init(FlTouchManager *self)
static const int kMaxTouchDeviceId
G_DEFINE_TYPE(FlTouchManager, fl_touch_manager, G_TYPE_OBJECT)
void fl_touch_manager_cancel_input(FlTouchManager *self, guint event_time)
void fl_touch_manager_handle_touch_event(FlTouchManager *self, GdkEventTouch *touch_event, gint scale_factor)
static void ensure_touch_added(_FlTouchManager *self, guint event_time, gdouble x, gdouble y, uint32_t number, int32_t touch_id, int32_t device_id)
static const int kMinTouchDeviceId
static constexpr int kMicrosecondsPerMillisecond
static uint32_t get_generated_id(_FlTouchManager *self, uint32_t number)
static void fl_touch_manager_dispose(GObject *object)
static void remove_touch_point(_FlTouchManager *self, uint32_t number, uint32_t touch_id)
FlTouchManager * fl_touch_manager_new(FlEngine *engine, FlutterViewId view_id)
G_BEGIN_DECLS FlutterViewId view_id
GHashTable * added_touch_devices
GHashTable * number_to_id
FlutterViewId view_id