FreeRDP
wlf_input.c
1 
21 #include <stdlib.h>
22 #include <float.h>
23 
24 #include <linux/input.h>
25 
26 #include <winpr/assert.h>
27 
28 #include <freerdp/config.h>
29 #include <freerdp/locale/keyboard.h>
30 #if defined(CHANNEL_RDPEI_CLIENT)
31 #include <freerdp/client/rdpei.h>
32 #endif
33 #include <uwac/uwac.h>
34 
35 #include "wlfreerdp.h"
36 #include "wlf_input.h"
37 
38 static BOOL scale_signed_coordinates(rdpContext* context, int32_t* x, int32_t* y,
39  BOOL fromLocalToRDP)
40 {
41  BOOL rc = 0;
42  UINT32 ux = 0;
43  UINT32 uy = 0;
44  WINPR_ASSERT(context);
45  WINPR_ASSERT(x);
46  WINPR_ASSERT(y);
47  WINPR_ASSERT(*x >= 0);
48  WINPR_ASSERT(*y >= 0);
49 
50  ux = (UINT32)*x;
51  uy = (UINT32)*y;
52  rc = wlf_scale_coordinates(context, &ux, &uy, fromLocalToRDP);
53  WINPR_ASSERT(ux < INT32_MAX);
54  WINPR_ASSERT(uy < INT32_MAX);
55  *x = (int32_t)ux;
56  *y = (int32_t)uy;
57  return rc;
58 }
59 
60 BOOL wlf_handle_pointer_enter(freerdp* instance, const UwacPointerEnterLeaveEvent* ev)
61 {
62  uint32_t x = 0;
63  uint32_t y = 0;
64  rdpClientContext* cctx = NULL;
65 
66  if (!instance || !ev)
67  return FALSE;
68 
69  x = ev->x;
70  y = ev->y;
71 
72  if (!wlf_scale_coordinates(instance->context, &x, &y, TRUE))
73  return FALSE;
74 
75  WINPR_ASSERT(x <= UINT16_MAX);
76  WINPR_ASSERT(y <= UINT16_MAX);
77  cctx = (rdpClientContext*)instance->context;
78  return freerdp_client_send_button_event(cctx, FALSE, PTR_FLAGS_MOVE, x, y);
79 }
80 
81 BOOL wlf_handle_pointer_motion(freerdp* instance, const UwacPointerMotionEvent* ev)
82 {
83  uint32_t x = 0;
84  uint32_t y = 0;
85  rdpClientContext* cctx = NULL;
86 
87  if (!instance || !ev)
88  return FALSE;
89 
90  cctx = (rdpClientContext*)instance->context;
91  WINPR_ASSERT(cctx);
92 
93  x = ev->x;
94  y = ev->y;
95 
96  if (!wlf_scale_coordinates(instance->context, &x, &y, TRUE))
97  return FALSE;
98 
99  WINPR_ASSERT(x <= UINT16_MAX);
100  WINPR_ASSERT(y <= UINT16_MAX);
101  return freerdp_client_send_button_event(cctx, FALSE, PTR_FLAGS_MOVE, x, y);
102 }
103 
104 BOOL wlf_handle_pointer_buttons(freerdp* instance, const UwacPointerButtonEvent* ev)
105 {
106  rdpClientContext* cctx = NULL;
107  UINT16 flags = 0;
108  UINT16 xflags = 0;
109  uint32_t x = 0;
110  uint32_t y = 0;
111 
112  if (!instance || !ev)
113  return FALSE;
114 
115  cctx = (rdpClientContext*)instance->context;
116  WINPR_ASSERT(cctx);
117 
118  x = ev->x;
119  y = ev->y;
120 
121  if (!wlf_scale_coordinates(instance->context, &x, &y, TRUE))
122  return FALSE;
123 
124  if (ev->state == WL_POINTER_BUTTON_STATE_PRESSED)
125  {
126  flags |= PTR_FLAGS_DOWN;
127  xflags |= PTR_XFLAGS_DOWN;
128  }
129 
130  switch (ev->button)
131  {
132  case BTN_LEFT:
133  flags |= PTR_FLAGS_BUTTON1;
134  break;
135 
136  case BTN_RIGHT:
137  flags |= PTR_FLAGS_BUTTON2;
138  break;
139 
140  case BTN_MIDDLE:
141  flags |= PTR_FLAGS_BUTTON3;
142  break;
143 
144  case BTN_SIDE:
145  xflags |= PTR_XFLAGS_BUTTON1;
146  break;
147 
148  case BTN_EXTRA:
149  xflags |= PTR_XFLAGS_BUTTON2;
150  break;
151 
152  default:
153  return TRUE;
154  }
155 
156  WINPR_ASSERT(x <= UINT16_MAX);
157  WINPR_ASSERT(y <= UINT16_MAX);
158 
159  if ((flags & ~PTR_FLAGS_DOWN) != 0)
160  return freerdp_client_send_button_event(cctx, FALSE, flags, x, y);
161 
162  if ((xflags & ~PTR_XFLAGS_DOWN) != 0)
163  return freerdp_client_send_extended_button_event(cctx, FALSE, xflags, x, y);
164 
165  return FALSE;
166 }
167 
168 BOOL wlf_handle_pointer_axis(freerdp* instance, const UwacPointerAxisEvent* ev)
169 {
170  wlfContext* context = NULL;
171  if (!instance || !instance->context || !ev)
172  return FALSE;
173 
174  context = (wlfContext*)instance->context;
175  return ArrayList_Append(context->events, ev);
176 }
177 
178 BOOL wlf_handle_pointer_axis_discrete(freerdp* instance, const UwacPointerAxisEvent* ev)
179 {
180  wlfContext* context = NULL;
181  if (!instance || !instance->context || !ev)
182  return FALSE;
183 
184  context = (wlfContext*)instance->context;
185  return ArrayList_Append(context->events, ev);
186 }
187 
188 static BOOL wlf_handle_wheel(freerdp* instance, uint32_t x, uint32_t y, uint32_t axis,
189  int32_t value)
190 {
191  rdpClientContext* cctx = NULL;
192  UINT16 flags = 0;
193  int32_t direction = 0;
194  uint32_t avalue = (uint32_t)abs(value);
195 
196  WINPR_ASSERT(instance);
197 
198  cctx = (rdpClientContext*)instance->context;
199  WINPR_ASSERT(cctx);
200 
201  if (!wlf_scale_coordinates(instance->context, &x, &y, TRUE))
202  return FALSE;
203 
204  direction = value;
205  switch (axis)
206  {
207  case WL_POINTER_AXIS_VERTICAL_SCROLL:
208  flags |= PTR_FLAGS_WHEEL;
209  if (direction > 0)
210  flags |= PTR_FLAGS_WHEEL_NEGATIVE;
211  break;
212 
213  case WL_POINTER_AXIS_HORIZONTAL_SCROLL:
214  flags |= PTR_FLAGS_HWHEEL;
215  if (direction < 0)
216  flags |= PTR_FLAGS_WHEEL_NEGATIVE;
217  break;
218 
219  default:
220  return FALSE;
221  }
222 
223  /* Wheel rotation steps:
224  *
225  * positive: 0 ... 0xFF -> slow ... fast
226  * negative: 0 ... 0xFF -> fast ... slow
227  */
228 
229  while (avalue > 0)
230  {
231  const UINT16 cval = (avalue > 0xFF) ? 0xFF : (UINT16)avalue;
232  UINT16 cflags = flags | cval;
233  /* Convert negative values to 9bit twos complement */
234  if (flags & PTR_FLAGS_WHEEL_NEGATIVE)
235  cflags = (flags & 0xFF00) | (0x100 - cval);
236  if (!freerdp_client_send_wheel_event(cctx, cflags))
237  return FALSE;
238 
239  avalue -= cval;
240  }
241  return TRUE;
242 }
243 
244 BOOL wlf_handle_pointer_frame(freerdp* instance, const UwacPointerFrameEvent* ev)
245 {
246  BOOL success = TRUE;
247  BOOL handle = FALSE;
248  wlfContext* context = NULL;
249  enum wl_pointer_axis_source source = WL_POINTER_AXIS_SOURCE_CONTINUOUS;
250 
251  if (!instance || !ev || !instance->context)
252  return FALSE;
253 
254  context = (wlfContext*)instance->context;
255 
256  for (size_t x = 0; x < ArrayList_Count(context->events); x++)
257  {
258  UwacEvent* cev = ArrayList_GetItem(context->events, x);
259  if (!cev)
260  continue;
261  if (cev->type == UWAC_EVENT_POINTER_SOURCE)
262  {
263  handle = TRUE;
264  source = cev->mouse_source.axis_source;
265  }
266  }
267 
268  /* We need source events to determine how to interpret the data */
269  if (handle)
270  {
271  for (size_t x = 0; x < ArrayList_Count(context->events); x++)
272  {
273  UwacEvent* cev = ArrayList_GetItem(context->events, x);
274  if (!cev)
275  continue;
276 
277  switch (source)
278  {
279  /* If we have a mouse wheel, just use discrete data */
280  case WL_POINTER_AXIS_SOURCE_WHEEL:
281 #if defined(WL_POINTER_AXIS_SOURCE_WHEEL_TILT_SINCE_VERSION)
282  case WL_POINTER_AXIS_SOURCE_WHEEL_TILT:
283 #endif
284  if (cev->type == UWAC_EVENT_POINTER_AXIS_DISCRETE)
285  {
286  /* Get the number of steps, multiply by default step width of 120 */
287  int32_t val = cev->mouse_axis.value * 0x78;
288  /* No wheel event received, success! */
289  if (!wlf_handle_wheel(instance, cev->mouse_axis.x, cev->mouse_axis.y,
290  cev->mouse_axis.axis, val))
291  success = FALSE;
292  }
293  break;
294  /* If we have a touch pad we get actual data, scale */
295  case WL_POINTER_AXIS_SOURCE_FINGER:
296  case WL_POINTER_AXIS_SOURCE_CONTINUOUS:
297  if (cev->type == UWAC_EVENT_POINTER_AXIS)
298  {
299  double dval = wl_fixed_to_double(cev->mouse_axis.value);
300  int32_t val = (int32_t)(dval * 0x78 / 10.0);
301  if (!wlf_handle_wheel(instance, cev->mouse_axis.x, cev->mouse_axis.y,
302  cev->mouse_axis.axis, val))
303  success = FALSE;
304  }
305  break;
306  default:
307  break;
308  }
309  }
310  }
311  ArrayList_Clear(context->events);
312  return success;
313 }
314 
315 BOOL wlf_handle_pointer_source(freerdp* instance, const UwacPointerSourceEvent* ev)
316 {
317  wlfContext* context = NULL;
318  if (!instance || !instance->context || !ev)
319  return FALSE;
320 
321  context = (wlfContext*)instance->context;
322  return ArrayList_Append(context->events, ev);
323 }
324 
325 BOOL wlf_handle_key(freerdp* instance, const UwacKeyEvent* ev)
326 {
327  rdpInput* input = NULL;
328  DWORD rdp_scancode = 0;
329 
330  if (!instance || !ev)
331  return FALSE;
332 
333  WINPR_ASSERT(instance->context);
334  if (freerdp_settings_get_bool(instance->context->settings, FreeRDP_GrabKeyboard) &&
335  ev->raw_key == KEY_RIGHTCTRL)
336  wlf_handle_ungrab_key(instance, ev);
337 
338  input = instance->context->input;
339  rdp_scancode = freerdp_keyboard_get_rdp_scancode_from_x11_keycode(ev->raw_key + 8);
340 
341  if (rdp_scancode == RDP_SCANCODE_UNKNOWN)
342  return TRUE;
343 
344  return freerdp_input_send_keyboard_event_ex(input, ev->pressed, ev->repeated, rdp_scancode);
345 }
346 
347 BOOL wlf_handle_ungrab_key(freerdp* instance, const UwacKeyEvent* ev)
348 {
349  wlfContext* context = NULL;
350  if (!instance || !instance->context || !ev)
351  return FALSE;
352 
353  context = (wlfContext*)instance->context;
354 
355  return UwacSeatInhibitShortcuts(context->seat, false) == UWAC_SUCCESS;
356 }
357 
358 BOOL wlf_keyboard_enter(freerdp* instance, const UwacKeyboardEnterLeaveEvent* ev)
359 {
360  if (!instance || !ev)
361  return FALSE;
362 
363  ((wlfContext*)instance->context)->focusing = TRUE;
364  return TRUE;
365 }
366 
367 BOOL wlf_keyboard_modifiers(freerdp* instance, const UwacKeyboardModifiersEvent* ev)
368 {
369  rdpInput* input = NULL;
370  UINT16 syncFlags = 0;
371  wlfContext* wlf = NULL;
372 
373  if (!instance || !ev)
374  return FALSE;
375 
376  wlf = (wlfContext*)instance->context;
377  WINPR_ASSERT(wlf);
378 
379  input = instance->context->input;
380  WINPR_ASSERT(input);
381 
382  syncFlags = 0;
383 
384  if (ev->modifiers & UWAC_MOD_CAPS_MASK)
385  syncFlags |= KBD_SYNC_CAPS_LOCK;
386  if (ev->modifiers & UWAC_MOD_NUM_MASK)
387  syncFlags |= KBD_SYNC_NUM_LOCK;
388 
389  if (!wlf->focusing)
390  return TRUE;
391 
392  ((wlfContext*)instance->context)->focusing = FALSE;
393 
394  return freerdp_input_send_focus_in_event(input, syncFlags) &&
395  freerdp_client_send_button_event(&wlf->common, FALSE, PTR_FLAGS_MOVE, 0, 0);
396 }
397 
398 BOOL wlf_handle_touch_up(freerdp* instance, const UwacTouchUp* ev)
399 {
400  int32_t x = 0;
401  int32_t y = 0;
402 
403  WINPR_ASSERT(instance);
404  WINPR_ASSERT(ev);
405 
406  wlfContext* wlf = (wlfContext*)instance->context;
407  WINPR_ASSERT(wlf);
408 
409  x = ev->x;
410  y = ev->y;
411 
412  if (!scale_signed_coordinates(instance->context, &x, &y, TRUE))
413  return FALSE;
414 
415  return freerdp_client_handle_touch(&wlf->common, FREERDP_TOUCH_UP, ev->id, 0, x, y);
416 }
417 
418 BOOL wlf_handle_touch_down(freerdp* instance, const UwacTouchDown* ev)
419 {
420  int32_t x = 0;
421  int32_t y = 0;
422 
423  WINPR_ASSERT(instance);
424  WINPR_ASSERT(ev);
425 
426  wlfContext* wlf = (wlfContext*)instance->context;
427  WINPR_ASSERT(wlf);
428 
429  x = ev->x;
430  y = ev->y;
431 
432  if (!scale_signed_coordinates(instance->context, &x, &y, TRUE))
433  return FALSE;
434 
435  return freerdp_client_handle_touch(&wlf->common, FREERDP_TOUCH_DOWN, ev->id, 0, x, y);
436 }
437 
438 BOOL wlf_handle_touch_motion(freerdp* instance, const UwacTouchMotion* ev)
439 {
440  int32_t x = 0;
441  int32_t y = 0;
442 
443  WINPR_ASSERT(instance);
444  WINPR_ASSERT(ev);
445 
446  wlfContext* wlf = (wlfContext*)instance->context;
447  WINPR_ASSERT(wlf);
448 
449  x = ev->x;
450  y = ev->y;
451 
452  if (!scale_signed_coordinates(instance->context, &x, &y, TRUE))
453  return FALSE;
454 
455  return freerdp_client_handle_touch(&wlf->common, FREERDP_TOUCH_MOTION, 0, ev->id, x, y);
456 }
FREERDP_API BOOL freerdp_settings_get_bool(const rdpSettings *settings, FreeRDP_Settings_Keys_Bool id)
Returns a boolean settings value.