Flutter Linux Embedder
fl_touch_manager.cc File Reference

Go to the source code of this file.

Classes

struct  FlTouchPoint
 
struct  _FlTouchManager
 

Functions

 G_DEFINE_TYPE (FlTouchManager, fl_touch_manager, G_TYPE_OBJECT)
 
static void fl_touch_manager_dispose (GObject *object)
 
static void fl_touch_manager_class_init (FlTouchManagerClass *klass)
 
static void fl_touch_manager_init (FlTouchManager *self)
 
FlTouchManager * fl_touch_manager_new (FlEngine *engine, FlutterViewId view_id)
 
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 uint32_t get_generated_id (_FlTouchManager *self, uint32_t number)
 
static void release_number (_FlTouchManager *self, uint32_t number)
 
static void remove_touch_point (_FlTouchManager *self, uint32_t number, uint32_t touch_id)
 
void fl_touch_manager_handle_touch_event (FlTouchManager *self, GdkEventTouch *touch_event, gint scale_factor)
 
void fl_touch_manager_cancel_input (FlTouchManager *self, guint event_time)
 

Variables

static constexpr int kMicrosecondsPerMillisecond = 1000
 
static const int kMinTouchDeviceId = 0
 
static const int kMaxTouchDeviceId = 128
 

Function Documentation

◆ ensure_touch_added()

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

Definition at line 81 of file fl_touch_manager.cc.

87  {
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 
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 }
g_autoptr(FlEngine) engine
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
static constexpr int kMicrosecondsPerMillisecond
GHashTable * added_touch_devices
FlutterViewId view_id

References fl_engine_send_touch_add_event(), g_autoptr(), kMicrosecondsPerMillisecond, FlTouchPoint::number, FlTouchPoint::x, and FlTouchPoint::y.

Referenced by fl_touch_manager_handle_touch_event().

◆ fl_touch_manager_cancel_input()

void fl_touch_manager_cancel_input ( FlTouchManager *  manager,
guint  event_time 
)

fl_touch_manager_cancel_input: @manager: an #FlTouchManager. @event_time: time event occurred in milliseconds.

Handle touch events no longer being delivered to this view, e.g. because the window system took the touch to move or resize the window, or the view was hidden. Any touch points in contact with the screen are cancelled, as their end events will never arrive.

Definition at line 233 of file fl_touch_manager.cc.

233  {
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 }
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
uint8_t value
static void release_number(_FlTouchManager *self, uint32_t number)

References fl_engine_send_touch_cancel_event(), fl_engine_send_touch_remove_event(), g_autoptr(), kMicrosecondsPerMillisecond, FlTouchPoint::number, release_number(), value, FlTouchPoint::x, and FlTouchPoint::y.

Referenced by cancel_input(), and TEST_F().

◆ fl_touch_manager_class_init()

static void fl_touch_manager_class_init ( FlTouchManagerClass *  klass)
static

Definition at line 53 of file fl_touch_manager.cc.

53  {
54  G_OBJECT_CLASS(klass)->dispose = fl_touch_manager_dispose;
55 }
static void fl_touch_manager_dispose(GObject *object)

References fl_touch_manager_dispose().

◆ fl_touch_manager_dispose()

static void fl_touch_manager_dispose ( GObject *  object)
static

Definition at line 41 of file fl_touch_manager.cc.

41  {
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 }

Referenced by fl_touch_manager_class_init().

◆ fl_touch_manager_handle_touch_event()

void fl_touch_manager_handle_touch_event ( FlTouchManager *  manager,
GdkEventTouch *  event,
gint  scale_factor 
)

fl_touch_manager_handle_touch_event: @manager: an #FlTouchManager. @event: the touch event. @scale_factor: the GTK scaling factor of the window.

Definition at line 161 of file fl_touch_manager.cc.

163  {
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 }
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_down_event(FlEngine *self, FlutterViewId view_id, size_t timestamp, double x, double y, int32_t device)
Definition: fl_engine.cc:1307
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 uint32_t get_generated_id(_FlTouchManager *self, uint32_t number)
static void remove_touch_point(_FlTouchManager *self, uint32_t number, uint32_t touch_id)

References ensure_touch_added(), fl_engine_send_touch_cancel_event(), fl_engine_send_touch_down_event(), fl_engine_send_touch_move_event(), fl_engine_send_touch_remove_event(), fl_engine_send_touch_up_event(), g_autoptr(), get_generated_id(), kMicrosecondsPerMillisecond, and remove_touch_point().

Referenced by TEST_F(), and touch_event_cb().

◆ fl_touch_manager_init()

static void fl_touch_manager_init ( FlTouchManager *  self)
static

Definition at line 57 of file fl_touch_manager.cc.

57 {}

◆ fl_touch_manager_new()

FlTouchManager* fl_touch_manager_new ( FlEngine *  engine,
FlutterViewId  view_id 
)

fl_touch_manager_new: @engine: an #FlEngine. @view_id: the view being managed.

Create a new #FlTouchManager.

Returns: a new #FlTouchManager.

Definition at line 59 of file fl_touch_manager.cc.

59  {
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 }
static const int kMinTouchDeviceId
G_BEGIN_DECLS FlutterViewId view_id

References kMinTouchDeviceId, and view_id.

Referenced by init_touch(), and TEST_F().

◆ G_DEFINE_TYPE()

G_DEFINE_TYPE ( FlTouchManager  ,
fl_touch_manager  ,
G_TYPE_OBJECT   
)

◆ get_generated_id()

static uint32_t get_generated_id ( _FlTouchManager *  self,
uint32_t  number 
)
static

Definition at line 115 of file fl_touch_manager.cc.

115  {
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 &&
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 }
int64_t id
static const int kMaxTouchDeviceId
GHashTable * number_to_id

References g_autoptr(), id, kMaxTouchDeviceId, kMinTouchDeviceId, and value.

Referenced by fl_touch_manager_handle_touch_event().

◆ release_number()

static void release_number ( _FlTouchManager *  self,
uint32_t  number 
)
static

Definition at line 143 of file fl_touch_manager.cc.

143  {
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 }

Referenced by fl_touch_manager_cancel_input(), and remove_touch_point().

◆ remove_touch_point()

static void remove_touch_point ( _FlTouchManager *  self,
uint32_t  number,
uint32_t  touch_id 
)
static

Definition at line 154 of file fl_touch_manager.cc.

156  {
157  release_number(self, number);
158  g_hash_table_remove(self->added_touch_devices, GINT_TO_POINTER(touch_id));
159 }

References release_number().

Referenced by fl_touch_manager_handle_touch_event().

Variable Documentation

◆ kMaxTouchDeviceId

const int kMaxTouchDeviceId = 128
static

Definition at line 10 of file fl_touch_manager.cc.

Referenced by get_generated_id().

◆ kMicrosecondsPerMillisecond

constexpr int kMicrosecondsPerMillisecond = 1000
staticconstexpr

◆ kMinTouchDeviceId

const int kMinTouchDeviceId = 0
static

Definition at line 9 of file fl_touch_manager.cc.

Referenced by fl_touch_manager_new(), and get_generated_id().