Flutter Linux Embedder
fl_pointer_manager.cc File Reference

Go to the source code of this file.

Classes

struct  _FlPointerManager
 

Functions

 G_DEFINE_TYPE (FlPointerManager, fl_pointer_manager, G_TYPE_OBJECT)
 
static gboolean get_mouse_button (guint gdk_button, int64_t *button)
 
static gboolean get_button (FlutterPointerDeviceKind device_kind, guint gdk_button, int64_t *button)
 
static gboolean has_buttons_pressed (guint gdk_state)
 
static void record_pointer_state (FlPointerManager *self, FlutterPointerDeviceKind device_kind, gdouble x, gdouble y, gdouble rotation, gdouble pressure)
 
static void ensure_pointer_added (FlPointerManager *self, guint event_time, FlutterPointerDeviceKind device_kind, gdouble x, gdouble y, gdouble rotation, gdouble pressure)
 
static void fl_pointer_manager_dispose (GObject *object)
 
static void fl_pointer_manager_class_init (FlPointerManagerClass *klass)
 
static void fl_pointer_manager_init (FlPointerManager *self)
 
FlPointerManager * fl_pointer_manager_new (FlutterViewId view_id, FlEngine *engine)
 
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)
 
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_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_leave (FlPointerManager *self, guint event_time, FlutterPointerDeviceKind device_kind, gdouble x, gdouble y, gdouble rotation, gdouble pressure)
 
gboolean fl_pointer_manager_cancel_input (FlPointerManager *self, guint event_time)
 

Variables

static constexpr int kMicrosecondsPerMillisecond = 1000
 
static constexpr guint kMouseButtonBack = 8
 
static constexpr guint kMouseButtonForward = 9
 

Function Documentation

◆ ensure_pointer_added()

static void ensure_pointer_added ( FlPointerManager *  self,
guint  event_time,
FlutterPointerDeviceKind  device_kind,
gdouble  x,
gdouble  y,
gdouble  rotation,
gdouble  pressure 
)
static

Definition at line 118 of file fl_pointer_manager.cc.

124  {
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 }
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 constexpr int kMicrosecondsPerMillisecond

References fl_engine_send_mouse_pointer_event(), g_autoptr(), kMicrosecondsPerMillisecond, record_pointer_state(), and TRUE.

Referenced by fl_pointer_manager_handle_button_press(), fl_pointer_manager_handle_enter(), and fl_pointer_manager_handle_motion().

◆ fl_pointer_manager_cancel_input()

gboolean fl_pointer_manager_cancel_input ( FlPointerManager *  manager,
guint  event_time 
)

fl_pointer_manager_cancel_input: @manager: an #FlPointerManager. @event_time: time event occurred in milliseconds.

Handle the release of any buttons Flutter believes are pressed never being delivered, e.g. because the window system took the pointer to move or resize the window. Those buttons are cancelled.

Returns: TRUE if the event was handled.

Definition at line 345 of file fl_pointer_manager.cc.

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

References fl_engine_send_mouse_pointer_event(), g_autoptr(), kMicrosecondsPerMillisecond, and TRUE.

Referenced by cancel_input(), fl_pointer_manager_handle_button_press(), fl_pointer_manager_handle_enter(), and TEST_F().

◆ fl_pointer_manager_class_init()

static void fl_pointer_manager_class_init ( FlPointerManagerClass *  klass)
static

Definition at line 153 of file fl_pointer_manager.cc.

153  {
154  G_OBJECT_CLASS(klass)->dispose = fl_pointer_manager_dispose;
155 }
static void fl_pointer_manager_dispose(GObject *object)

References fl_pointer_manager_dispose().

◆ fl_pointer_manager_dispose()

static void fl_pointer_manager_dispose ( GObject *  object)
static

Definition at line 145 of file fl_pointer_manager.cc.

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

Referenced by fl_pointer_manager_class_init().

◆ fl_pointer_manager_handle_button_press()

gboolean fl_pointer_manager_handle_button_press ( FlPointerManager *  manager,
guint  event_time,
FlutterPointerDeviceKind  device_kind,
gdouble  x,
gdouble  y,
guint  gdk_button,
gdouble  rotation,
gdouble  pressure 
)

fl_pointer_manager_handle_button_press: @manager: an #FlPointerManager. @event_time: event time in milliseconds. @device_kind: kind of device generating the event. @x: x co-ordinate of event. @y: y co-ordinate of event. @gdk_button: button being pressed. @rotation: rotation of the pointer device in degrees. @pressure: pressure of the pointer device.

Returns TRUE if this event was handled.

Definition at line 172 of file fl_pointer_manager.cc.

180  {
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 }
static gboolean get_button(FlutterPointerDeviceKind device_kind, guint gdk_button, int64_t *button)
static void ensure_pointer_added(FlPointerManager *self, guint event_time, FlutterPointerDeviceKind device_kind, gdouble x, gdouble y, gdouble rotation, gdouble pressure)
gboolean fl_pointer_manager_cancel_input(FlPointerManager *self, guint event_time)

References ensure_pointer_added(), fl_engine_send_mouse_pointer_event(), fl_pointer_manager_cancel_input(), g_autoptr(), get_button(), kMicrosecondsPerMillisecond, and TRUE.

Referenced by button_press_event_cb(), and TEST_F().

◆ fl_pointer_manager_handle_button_release()

gboolean fl_pointer_manager_handle_button_release ( FlPointerManager *  manager,
guint  event_time,
FlutterPointerDeviceKind  device_kind,
gdouble  x,
gdouble  y,
guint  gdk_button,
gdouble  rotation,
gdouble  pressure 
)

fl_pointer_manager_handle_button_release: @manager: an #FlPointerManager. @event_time: event time in milliseconds. @device_kind: kind of device generating the event. @x: x co-ordinate of event. @y: y co-ordinate of event. @gdk_button: button being released. @rotation: rotation of the pointer device in degrees. @pressure: pressure of the pointer device.

Returns TRUE if this event was handled.

Definition at line 216 of file fl_pointer_manager.cc.

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

References fl_engine_send_mouse_pointer_event(), g_autoptr(), get_button(), kMicrosecondsPerMillisecond, record_pointer_state(), and TRUE.

Referenced by button_release_event_cb(), and TEST_F().

◆ fl_pointer_manager_handle_enter()

gboolean fl_pointer_manager_handle_enter ( FlPointerManager *  manager,
guint  event_time,
FlutterPointerDeviceKind  device_kind,
gdouble  x,
gdouble  y,
guint  gdk_state,
gdouble  rotation,
gdouble  pressure 
)

fl_pointer_manager_handle_enter: @manager: an #FlPointerManager. @event_time: event time in milliseconds. @device_kind: kind of device generating the event. @x: x co-ordinate of event. @y: y co-ordinate of event. @gdk_state: GDK state of buttons and modifiers when the event occurred. @rotation: rotation of the pointer device in degrees. @pressure: pressure of the pointer device.

Returns TRUE if this event was handled.

Definition at line 280 of file fl_pointer_manager.cc.

287  {
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 }
static gboolean has_buttons_pressed(guint gdk_state)

References ensure_pointer_added(), fl_pointer_manager_cancel_input(), g_autoptr(), has_buttons_pressed(), and TRUE.

Referenced by enter_notify_event_cb(), and TEST_F().

◆ fl_pointer_manager_handle_leave()

gboolean fl_pointer_manager_handle_leave ( FlPointerManager *  manager,
guint  event_time,
FlutterPointerDeviceKind  device_kind,
gdouble  x,
gdouble  y,
gdouble  rotation,
gdouble  pressure 
)

fl_pointer_manager_handle_leave: @manager: an #FlPointerManager. @event_time: event time in milliseconds. @device_kind: kind of device generating the event. @x: x co-ordinate of event. @y: y co-ordinate of event. @rotation: rotation of the pointer device in degrees. @pressure: pressure of the pointer device.

Returns TRUE if this event was handled.

Definition at line 307 of file fl_pointer_manager.cc.

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

References fl_engine_send_mouse_pointer_event(), g_autoptr(), kMicrosecondsPerMillisecond, record_pointer_state(), and TRUE.

Referenced by leave_notify_event_cb(), and TEST_F().

◆ fl_pointer_manager_handle_motion()

gboolean fl_pointer_manager_handle_motion ( FlPointerManager *  manager,
guint  event_time,
FlutterPointerDeviceKind  device_kind,
gdouble  x,
gdouble  y,
gdouble  rotation,
gdouble  pressure 
)

fl_pointer_manager_handle_motion: @manager: an #FlPointerManager. @event_time: event time in milliseconds. @device_kind: kind of device generating the event. @x: x co-ordinate of event. @y: y co-ordinate of event. @rotation: rotation of the pointer device in degrees. @pressure: pressure of the pointer device.

Returns TRUE if this event was handled.

Definition at line 256 of file fl_pointer_manager.cc.

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

References ensure_pointer_added(), fl_engine_send_mouse_pointer_event(), g_autoptr(), kMicrosecondsPerMillisecond, and TRUE.

Referenced by motion_notify_event_cb(), and TEST_F().

◆ fl_pointer_manager_init()

static void fl_pointer_manager_init ( FlPointerManager *  self)
static

Definition at line 157 of file fl_pointer_manager.cc.

157  {
158  self->last_device_kind = kFlutterPointerDeviceKindMouse;
159 }

◆ fl_pointer_manager_new()

FlPointerManager* fl_pointer_manager_new ( FlutterViewId  view_id,
FlEngine *  engine 
)

fl_pointer_manager_new: @view_id: view ID to report events for. @engine: an #FlEngine.

Create a new #FlPointerManager.

Returns: a new #FlPointerManager.

Definition at line 161 of file fl_pointer_manager.cc.

162  {
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 }
G_BEGIN_DECLS FlutterViewId view_id

References view_id.

Referenced by setup_engine(), and TEST_F().

◆ G_DEFINE_TYPE()

G_DEFINE_TYPE ( FlPointerManager  ,
fl_pointer_manager  ,
G_TYPE_OBJECT   
)

◆ get_button()

static gboolean get_button ( FlutterPointerDeviceKind  device_kind,
guint  gdk_button,
int64_t *  button 
)
static

Definition at line 70 of file fl_pointer_manager.cc.

72  {
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 }
static gboolean get_mouse_button(guint gdk_button, int64_t *button)

References get_mouse_button(), and TRUE.

Referenced by fl_pointer_manager_handle_button_press(), and fl_pointer_manager_handle_button_release().

◆ get_mouse_button()

static gboolean get_mouse_button ( guint  gdk_button,
int64_t *  button 
)
static

Definition at line 48 of file fl_pointer_manager.cc.

48  {
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 }
static constexpr guint kMouseButtonForward
static constexpr guint kMouseButtonBack

References kMouseButtonBack, kMouseButtonForward, and TRUE.

Referenced by get_button().

◆ has_buttons_pressed()

static gboolean has_buttons_pressed ( guint  gdk_state)
static

Definition at line 97 of file fl_pointer_manager.cc.

97  {
98  return (gdk_state & (GDK_BUTTON1_MASK | GDK_BUTTON2_MASK | GDK_BUTTON3_MASK |
99  GDK_BUTTON4_MASK | GDK_BUTTON5_MASK)) != 0;
100 }

Referenced by fl_pointer_manager_handle_enter().

◆ record_pointer_state()

static void record_pointer_state ( FlPointerManager *  self,
FlutterPointerDeviceKind  device_kind,
gdouble  x,
gdouble  y,
gdouble  rotation,
gdouble  pressure 
)
static

Definition at line 104 of file fl_pointer_manager.cc.

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

Referenced by ensure_pointer_added(), fl_pointer_manager_handle_button_release(), and fl_pointer_manager_handle_leave().

Variable Documentation

◆ kMicrosecondsPerMillisecond

◆ kMouseButtonBack

constexpr guint kMouseButtonBack = 8
staticconstexpr

Definition at line 42 of file fl_pointer_manager.cc.

Referenced by get_mouse_button().

◆ kMouseButtonForward

constexpr guint kMouseButtonForward = 9
staticconstexpr

Definition at line 45 of file fl_pointer_manager.cc.

Referenced by get_mouse_button().