Flutter Linux Embedder
fl_pointer_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 
6 
8 
9 static constexpr int kMicrosecondsPerMillisecond = 1000;
10 
12  GObject parent_instance;
13 
14  // Engine to send pointer events to.
15  GWeakRef engine;
16 
17  // ID to mark events with.
18  FlutterViewId view_id;
19 
20  // TRUE if the mouse pointer is inside the view, used for generating missing
21  // add events.
22  gboolean pointer_inside;
23 
24  // Pointer button state recorded for sending status updates.
25  int64_t button_state;
26 
27  // TRUE if a leave event was received while a button was pressed, and the
28  // matching remove event has not been sent yet.
29  gboolean leave_pending;
30 
31  // Last known pointer position and device, used when synthesizing events.
32  FlutterPointerDeviceKind last_device_kind;
33  gdouble last_x;
34  gdouble last_y;
35  gdouble last_rotation;
36  gdouble last_pressure;
37 };
38 
39 G_DEFINE_TYPE(FlPointerManager, fl_pointer_manager, G_TYPE_OBJECT);
40 
41 // 8 corresponds to mouse back button on both x11 and wayland
42 static constexpr guint kMouseButtonBack = 8;
43 
44 // 9 corresponds to mouse forward button on both x11 and wayland
45 static constexpr guint kMouseButtonForward = 9;
46 
47 // Convert a GDK button ID into a Flutter button ID
48 static gboolean get_mouse_button(guint gdk_button, int64_t* button) {
49  switch (gdk_button) {
50  case GDK_BUTTON_PRIMARY:
51  *button = kFlutterPointerButtonMousePrimary;
52  return TRUE;
53  case GDK_BUTTON_MIDDLE:
54  *button = kFlutterPointerButtonMouseMiddle;
55  return TRUE;
56  case GDK_BUTTON_SECONDARY:
57  *button = kFlutterPointerButtonMouseSecondary;
58  return TRUE;
59  case kMouseButtonBack:
60  *button = kFlutterPointerButtonMouseBack;
61  return TRUE;
63  *button = kFlutterPointerButtonMouseForward;
64  return TRUE;
65  default:
66  return FALSE;
67  }
68 }
69 
70 static gboolean get_button(FlutterPointerDeviceKind device_kind,
71  guint gdk_button,
72  int64_t* button) {
73  if (device_kind == kFlutterPointerDeviceKindStylus ||
74  device_kind == kFlutterPointerDeviceKindInvertedStylus) {
75  // GDK button names describe the physical button action, where "primary"
76  // is the stylus tip contact. Flutter stylus button names reserve "primary"
77  // for the first barrel button, so the GDK secondary button maps there.
78  switch (gdk_button) {
79  case GDK_BUTTON_PRIMARY:
80  *button = kFlutterPointerButtonStylusContact;
81  return TRUE;
82  case GDK_BUTTON_SECONDARY:
83  *button = kFlutterPointerButtonStylusPrimary;
84  return TRUE;
85  case GDK_BUTTON_MIDDLE:
86  *button = kFlutterPointerButtonStylusSecondary;
87  return TRUE;
88  default:
89  return FALSE;
90  }
91  }
92 
93  return get_mouse_button(gdk_button, button);
94 }
95 
96 // Returns TRUE if any pointer button is pressed in a GDK modifier state.
97 static gboolean has_buttons_pressed(guint gdk_state) {
98  return (gdk_state & (GDK_BUTTON1_MASK | GDK_BUTTON2_MASK | GDK_BUTTON3_MASK |
99  GDK_BUTTON4_MASK | GDK_BUTTON5_MASK)) != 0;
100 }
101 
102 // Records the most recent pointer state so that events can be synthesized
103 // from it later.
104 static void record_pointer_state(FlPointerManager* self,
105  FlutterPointerDeviceKind device_kind,
106  gdouble x,
107  gdouble y,
108  gdouble rotation,
109  gdouble pressure) {
110  self->last_device_kind = device_kind;
111  self->last_x = x;
112  self->last_y = y;
113  self->last_rotation = rotation;
114  self->last_pressure = pressure;
115 }
116 
117 // Generates a mouse pointer event if the pointer appears inside the window.
118 static void ensure_pointer_added(FlPointerManager* self,
119  guint event_time,
120  FlutterPointerDeviceKind device_kind,
121  gdouble x,
122  gdouble y,
123  gdouble rotation,
124  gdouble pressure) {
125  record_pointer_state(self, device_kind, x, y, rotation, pressure);
126 
127  // The pointer is generating events again, so it is inside the view.
128  self->leave_pending = FALSE;
129 
130  if (self->pointer_inside) {
131  return;
132  }
133  self->pointer_inside = TRUE;
134 
135  g_autoptr(FlEngine) engine = FL_ENGINE(g_weak_ref_get(&self->engine));
136  if (engine == nullptr) {
137  return;
138  }
139 
141  engine, self->view_id, kAdd, event_time * kMicrosecondsPerMillisecond, x,
142  y, device_kind, 0, 0, self->button_state, rotation, pressure);
143 }
144 
145 static void fl_pointer_manager_dispose(GObject* object) {
146  FlPointerManager* self = FL_POINTER_MANAGER(object);
147 
148  g_weak_ref_clear(&self->engine);
149 
150  G_OBJECT_CLASS(fl_pointer_manager_parent_class)->dispose(object);
151 }
152 
153 static void fl_pointer_manager_class_init(FlPointerManagerClass* klass) {
154  G_OBJECT_CLASS(klass)->dispose = fl_pointer_manager_dispose;
155 }
156 
157 static void fl_pointer_manager_init(FlPointerManager* self) {
158  self->last_device_kind = kFlutterPointerDeviceKindMouse;
159 }
160 
161 FlPointerManager* fl_pointer_manager_new(FlutterViewId view_id,
162  FlEngine* engine) {
163  FlPointerManager* self =
164  FL_POINTER_MANAGER(g_object_new(fl_pointer_manager_get_type(), nullptr));
165 
166  self->view_id = view_id;
167  g_weak_ref_init(&self->engine, engine);
168 
169  return self;
170 }
171 
173  FlPointerManager* self,
174  guint event_time,
175  FlutterPointerDeviceKind device_kind,
176  gdouble x,
177  gdouble y,
178  guint gdk_button,
179  gdouble rotation,
180  gdouble pressure) {
181  g_return_val_if_fail(FL_IS_POINTER_MANAGER(self), FALSE);
182 
183  int64_t button;
184  if (!get_button(device_kind, gdk_button, &button)) {
185  return FALSE;
186  }
187 
188  ensure_pointer_added(self, event_time, device_kind, x, y, rotation, pressure);
189 
190  // GDK never sends two presses of the same button without a release in
191  // between, so if Flutter thinks this button is already down then the release
192  // was lost, e.g. it was delivered to the window manager because it ended an
193  // interactive move or resize. Cancel the stale press so this one is not
194  // dropped.
195  if ((self->button_state & button) != 0) {
196  fl_pointer_manager_cancel_input(self, event_time);
197  }
198 
199  int old_button_state = self->button_state;
200  FlutterPointerPhase phase = kMove;
201  self->button_state ^= button;
202  phase = old_button_state == 0 ? kDown : kMove;
203 
204  g_autoptr(FlEngine) engine = FL_ENGINE(g_weak_ref_get(&self->engine));
205  if (engine == nullptr) {
206  return FALSE;
207  }
208 
210  engine, self->view_id, phase, event_time * kMicrosecondsPerMillisecond, x,
211  y, device_kind, 0, 0, self->button_state, rotation, pressure);
212 
213  return TRUE;
214 }
215 
217  FlPointerManager* self,
218  guint event_time,
219  FlutterPointerDeviceKind device_kind,
220  gdouble x,
221  gdouble y,
222  guint gdk_button,
223  gdouble rotation,
224  gdouble pressure) {
225  g_return_val_if_fail(FL_IS_POINTER_MANAGER(self), FALSE);
226 
227  int64_t button;
228  if (!get_button(device_kind, gdk_button, &button)) {
229  return FALSE;
230  }
231 
232  record_pointer_state(self, device_kind, x, y, rotation, pressure);
233 
234  // Drop the event if Flutter already thinks the button is up.
235  if ((self->button_state & button) == 0) {
236  return FALSE;
237  }
238 
239  FlutterPointerPhase phase = kMove;
240  self->button_state ^= button;
241 
242  phase = self->button_state == 0 ? kUp : kMove;
243 
244  g_autoptr(FlEngine) engine = FL_ENGINE(g_weak_ref_get(&self->engine));
245  if (engine == nullptr) {
246  return FALSE;
247  }
248 
250  engine, self->view_id, phase, event_time * kMicrosecondsPerMillisecond, x,
251  y, device_kind, 0, 0, self->button_state, rotation, pressure);
252 
253  return TRUE;
254 }
255 
256 gboolean fl_pointer_manager_handle_motion(FlPointerManager* self,
257  guint event_time,
258  FlutterPointerDeviceKind device_kind,
259  gdouble x,
260  gdouble y,
261  gdouble rotation,
262  gdouble pressure) {
263  g_return_val_if_fail(FL_IS_POINTER_MANAGER(self), FALSE);
264 
265  g_autoptr(FlEngine) engine = FL_ENGINE(g_weak_ref_get(&self->engine));
266  if (engine == nullptr) {
267  return FALSE;
268  }
269 
270  ensure_pointer_added(self, event_time, device_kind, x, y, rotation, pressure);
271 
273  engine, self->view_id, self->button_state != 0 ? kMove : kHover,
274  event_time * kMicrosecondsPerMillisecond, x, y, device_kind, 0, 0,
275  self->button_state, rotation, pressure);
276 
277  return TRUE;
278 }
279 
280 gboolean fl_pointer_manager_handle_enter(FlPointerManager* self,
281  guint event_time,
282  FlutterPointerDeviceKind device_kind,
283  gdouble x,
284  gdouble y,
285  guint gdk_state,
286  gdouble rotation,
287  gdouble pressure) {
288  g_return_val_if_fail(FL_IS_POINTER_MANAGER(self), FALSE);
289 
290  g_autoptr(FlEngine) engine = FL_ENGINE(g_weak_ref_get(&self->engine));
291  if (engine == nullptr) {
292  return FALSE;
293  }
294 
295  ensure_pointer_added(self, event_time, device_kind, x, y, rotation, pressure);
296 
297  // The window system takes the pointer for interactive moves and resizes, and
298  // gives it back without saying the button was released. Cancel the press if
299  // the pointer has come back with nothing pressed.
300  if (self->button_state != 0 && !has_buttons_pressed(gdk_state)) {
301  fl_pointer_manager_cancel_input(self, event_time);
302  }
303 
304  return TRUE;
305 }
306 
307 gboolean fl_pointer_manager_handle_leave(FlPointerManager* self,
308  guint event_time,
309  FlutterPointerDeviceKind device_kind,
310  gdouble x,
311  gdouble y,
312  gdouble rotation,
313  gdouble pressure) {
314  g_return_val_if_fail(FL_IS_POINTER_MANAGER(self), FALSE);
315 
316  g_autoptr(FlEngine) engine = FL_ENGINE(g_weak_ref_get(&self->engine));
317  if (engine == nullptr) {
318  return FALSE;
319  }
320 
321  if (!self->pointer_inside) {
322  return TRUE;
323  }
324 
325  // Don't remove pointer while button is down; In case of dragging outside of
326  // window with mouse grab active Gtk will send another leave notify on
327  // release. Remember the leave so the pointer can still be removed if that
328  // release is never delivered, e.g. because the window system took the
329  // pointer.
330  if (self->button_state != 0) {
331  record_pointer_state(self, device_kind, x, y, rotation, pressure);
332  self->leave_pending = TRUE;
333  return TRUE;
334  }
335 
337  engine, self->view_id, kRemove, event_time * kMicrosecondsPerMillisecond,
338  x, y, device_kind, 0, 0, self->button_state, rotation, pressure);
339  self->pointer_inside = FALSE;
340  self->leave_pending = FALSE;
341 
342  return TRUE;
343 }
344 
345 gboolean fl_pointer_manager_cancel_input(FlPointerManager* self,
346  guint event_time) {
347  g_return_val_if_fail(FL_IS_POINTER_MANAGER(self), FALSE);
348 
349  // Nothing to do if no buttons are pressed.
350  if (self->button_state == 0) {
351  return FALSE;
352  }
353 
354  self->button_state = 0;
355 
356  g_autoptr(FlEngine) engine = FL_ENGINE(g_weak_ref_get(&self->engine));
357  if (engine == nullptr) {
358  return FALSE;
359  }
360 
362  engine, self->view_id, kCancel, event_time * kMicrosecondsPerMillisecond,
363  self->last_x, self->last_y, self->last_device_kind, 0, 0,
364  self->button_state, self->last_rotation, self->last_pressure);
365 
366  // The pointer left the view while the button was down, so the remove event
367  // was delayed until the button was released. That release will never
368  // arrive, so remove the pointer now.
369  if (self->leave_pending) {
371  engine, self->view_id, kRemove,
372  event_time * kMicrosecondsPerMillisecond, self->last_x, self->last_y,
373  self->last_device_kind, 0, 0, self->button_state, self->last_rotation,
374  self->last_pressure);
375  self->pointer_inside = FALSE;
376  self->leave_pending = FALSE;
377  }
378 
379  return TRUE;
380 }
g_autoptr(FlEngine) engine
void fl_engine_send_mouse_pointer_event(FlEngine *self, FlutterViewId view_id, FlutterPointerPhase phase, size_t timestamp, double x, double y, FlutterPointerDeviceKind device_kind, double scroll_delta_x, double scroll_delta_y, int64_t buttons, double rotation, double pressure)
Definition: fl_engine.cc:1229
static void record_pointer_state(FlPointerManager *self, FlutterPointerDeviceKind device_kind, gdouble x, gdouble y, gdouble rotation, gdouble pressure)
static gboolean get_button(FlutterPointerDeviceKind device_kind, guint gdk_button, int64_t *button)
FlPointerManager * fl_pointer_manager_new(FlutterViewId view_id, FlEngine *engine)
G_DEFINE_TYPE(FlPointerManager, fl_pointer_manager, G_TYPE_OBJECT)
static void ensure_pointer_added(FlPointerManager *self, guint event_time, FlutterPointerDeviceKind device_kind, gdouble x, gdouble y, gdouble rotation, gdouble pressure)
static gboolean get_mouse_button(guint gdk_button, int64_t *button)
static void fl_pointer_manager_dispose(GObject *object)
gboolean fl_pointer_manager_cancel_input(FlPointerManager *self, guint event_time)
static constexpr int kMicrosecondsPerMillisecond
static gboolean has_buttons_pressed(guint gdk_state)
gboolean fl_pointer_manager_handle_button_press(FlPointerManager *self, guint event_time, FlutterPointerDeviceKind device_kind, gdouble x, gdouble y, guint gdk_button, gdouble rotation, gdouble pressure)
static constexpr guint kMouseButtonForward
static constexpr guint kMouseButtonBack
static void fl_pointer_manager_class_init(FlPointerManagerClass *klass)
static void fl_pointer_manager_init(FlPointerManager *self)
gboolean fl_pointer_manager_handle_motion(FlPointerManager *self, guint event_time, FlutterPointerDeviceKind device_kind, gdouble x, gdouble y, gdouble rotation, gdouble pressure)
gboolean fl_pointer_manager_handle_enter(FlPointerManager *self, guint event_time, FlutterPointerDeviceKind device_kind, gdouble x, gdouble y, guint gdk_state, gdouble rotation, gdouble pressure)
gboolean fl_pointer_manager_handle_button_release(FlPointerManager *self, guint event_time, FlutterPointerDeviceKind device_kind, gdouble x, gdouble y, guint gdk_button, gdouble rotation, gdouble pressure)
gboolean fl_pointer_manager_handle_leave(FlPointerManager *self, guint event_time, FlutterPointerDeviceKind device_kind, gdouble x, gdouble y, gdouble rotation, gdouble pressure)
G_BEGIN_DECLS FlutterViewId view_id
FlutterPointerDeviceKind last_device_kind