20#include <freerdp/config.h>
26#include <X11/Xcursor/Xcursor.h>
30#include <X11/extensions/XInput2.h>
41#include <winpr/assert.h>
42#include <winpr/wtypes.h>
43#include <freerdp/log.h>
44#define TAG CLIENT_TAG("x11")
48#define PAN_THRESHOLD 50
49#define ZOOM_THRESHOLD 10
51#define MIN_FINGER_DIST 5
53static int xf_input_event(xfContext* xfc, WINPR_ATTR_UNUSED
const XEvent* xevent,
54 XIDeviceEvent* event,
int evtype);
57static const char* xf_input_get_class_string(
int class)
59 if (
class == XIKeyClass)
61 else if (
class == XIButtonClass)
62 return "XIButtonClass";
63 else if (
class == XIValuatorClass)
64 return "XIValuatorClass";
65 else if (
class == XIScrollClass)
66 return "XIScrollClass";
67 else if (
class == XITouchClass)
68 return "XITouchClass";
70 return "XIUnknownClass";
74static BOOL register_input_events(xfContext* xfc, Window window)
76#define MAX_NR_MASKS 64
79 XIEventMask evmasks[MAX_NR_MASKS] = WINPR_C_ARRAY_INIT;
80 BYTE masks[MAX_NR_MASKS][XIMaskLen(XI_LASTEVENT)] = WINPR_C_ARRAY_INIT;
84 rdpSettings* settings = xfc->common.context.settings;
85 WINPR_ASSERT(settings);
87 XIDeviceInfo* info = XIQueryDevice(xfc->display, XIAllDevices, &ndevices);
89 for (
int i = 0; i < MIN(ndevices, MAX_NR_MASKS); i++)
92 XIDeviceInfo* dev = &info[i];
94 evmasks[nmasks].mask = masks[nmasks];
95 evmasks[nmasks].mask_len =
sizeof(masks[0]);
96 evmasks[nmasks].deviceid = dev->deviceid;
99 if (strcmp(dev->name,
"Virtual core pointer") == 0)
102 for (
int j = 0; j < dev->num_classes; j++)
104 const XIAnyClassInfo* c_class = dev->classes[j];
106 switch (c_class->type)
111 const XITouchClassInfo* t = (
const XITouchClassInfo*)c_class;
112 if (t->mode == XIDirectTouch)
116 "%s %s touch device (id: %d, mode: %d), supporting %d touches.",
117 dev->name, (t->mode == XIDirectTouch) ?
"direct" :
"dependent",
118 dev->deviceid, t->mode, t->num_touches);
119 XISetMask(masks[nmasks], XI_TouchBegin);
120 XISetMask(masks[nmasks], XI_TouchUpdate);
121 XISetMask(masks[nmasks], XI_TouchEnd);
127 const XIButtonClassInfo* t = (
const XIButtonClassInfo*)c_class;
128 WLog_DBG(TAG,
"%s button device (id: %d, mode: %d)", dev->name, dev->deviceid,
130 XISetMask(masks[nmasks], XI_ButtonPress);
131 XISetMask(masks[nmasks], XI_ButtonRelease);
132 XISetMask(masks[nmasks], XI_Motion);
136 case XIValuatorClass:
138 static wLog* log =
nullptr;
142 const XIValuatorClassInfo* t = (
const XIValuatorClassInfo*)c_class;
144 t->label ? Safe_XGetAtomName(log, xfc->display, t->label) : nullptr;
146 WLog_Print(log, WLOG_DEBUG,
147 "%s device (id: %d) valuator %d label %s range %f - %f", dev->name,
148 dev->deviceid, t->number, name ? name :
"None", t->min, t->max);
153 double max_pressure = t->max;
155 char devName[200] = WINPR_C_ARRAY_INIT;
156 strncpy(devName, dev->name, ARRAYSIZE(devName) - 1);
157 CharLowerBuffA(devName, ARRAYSIZE(devName));
159 if (strstr(devName,
"eraser") !=
nullptr)
161 if (freerdp_client_handle_pen(&xfc->common,
162 FREERDP_PEN_REGISTER |
163 FREERDP_PEN_IS_INVERTED |
164 FREERDP_PEN_HAS_PRESSURE,
165 dev->deviceid, max_pressure))
166 WLog_DBG(TAG,
"registered eraser");
168 else if (strstr(devName,
"stylus") !=
nullptr ||
169 strstr(devName,
"pen") !=
nullptr)
171 if (freerdp_client_handle_pen(
172 &xfc->common, FREERDP_PEN_REGISTER | FREERDP_PEN_HAS_PRESSURE,
173 dev->deviceid, max_pressure))
174 WLog_DBG(TAG,
"registered pen");
187 XIFreeDeviceInfo(info);
191 Status xstatus = XISelectEvents(xfc->display, window, evmasks, nmasks);
193 WLog_WARN(TAG,
"XISelectEvents returned %d", xstatus);
199static BOOL register_raw_events(xfContext* xfc, Window window)
202 unsigned char mask_bytes[XIMaskLen(XI_LASTEVENT)] = WINPR_C_ARRAY_INIT;
203 rdpSettings* settings =
nullptr;
207 settings = xfc->common.context.settings;
208 WINPR_ASSERT(settings);
212 XISetMask(mask_bytes, XI_RawMotion);
213 XISetMask(mask_bytes, XI_RawButtonPress);
214 XISetMask(mask_bytes, XI_RawButtonRelease);
216 mask.deviceid = XIAllMasterDevices;
217 mask.mask_len =
sizeof(mask_bytes);
218 mask.mask = mask_bytes;
220 XISelectEvents(xfc->display, window, &mask, 1);
226static BOOL register_device_events(xfContext* xfc, Window window)
228 XIEventMask mask = WINPR_C_ARRAY_INIT;
229 unsigned char mask_bytes[XIMaskLen(XI_LASTEVENT)] = WINPR_C_ARRAY_INIT;
233 XISetMask(mask_bytes, XI_DeviceChanged);
234 XISetMask(mask_bytes, XI_HierarchyChanged);
236 mask.deviceid = XIAllDevices;
237 mask.mask_len =
sizeof(mask_bytes);
238 mask.mask = mask_bytes;
240 XISelectEvents(xfc->display, window, &mask, 1);
245int xf_input_init(xfContext* xfc, Window window)
247 int major = XI_2_Major;
248 int minor = XI_2_Minor;
255 xfc->firstDist = -1.0;
259 xfc->active_contacts = 0;
261 if (!XQueryExtension(xfc->display,
"XInputExtension", &opcode, &event, &error))
263 WLog_WARN(TAG,
"XInput extension not available.");
267 xfc->XInputOpcode = opcode;
268 XIQueryVersion(xfc->display, &major, &minor);
270 if ((major < XI_2_Major) || ((major == XI_2_Major) && (minor < 2)))
272 WLog_WARN(TAG,
"Server does not support XI 2.2");
277 int scr = DefaultScreen(xfc->display);
278 Window root = RootWindow(xfc->display, scr);
280 if (!register_raw_events(xfc, root))
282 if (!register_input_events(xfc, window))
284 if (!register_device_events(xfc, window))
291static BOOL xf_input_is_duplicate(xfContext* xfc,
const XGenericEventCookie* cookie)
293 const XIDeviceEvent*
event =
nullptr;
296 WINPR_ASSERT(cookie);
298 event = cookie->data;
301 return ((xfc->lastEvent.time == event->time) && (xfc->lastEvType == cookie->evtype) &&
302 (xfc->lastEvent.detail == event->detail) &&
303 (fabs(xfc->lastEvent.event_x - event->event_x) < DBL_EPSILON) &&
304 (fabs(xfc->lastEvent.event_y - event->event_y) < DBL_EPSILON));
307static void xf_input_save_last_event(xfContext* xfc,
const XGenericEventCookie* cookie)
309 const XIDeviceEvent*
event =
nullptr;
312 WINPR_ASSERT(cookie);
314 event = cookie->data;
317 xfc->lastEvType = cookie->evtype;
318 xfc->lastEvent.time =
event->time;
319 xfc->lastEvent.detail =
event->detail;
320 xfc->lastEvent.event_x =
event->event_x;
321 xfc->lastEvent.event_y =
event->event_y;
325static BOOL xf_input_detect_pan(xfContext* xfc)
328 rdpContext* ctx = &xfc->common.context;
331 if (xfc->active_contacts != 2)
336 const double dx[] = { xfc->contacts[0].pos_x - xfc->contacts[0].last_x,
337 xfc->contacts[1].pos_x - xfc->contacts[1].last_x };
338 const double dy[] = { xfc->contacts[0].pos_y - xfc->contacts[0].last_y,
339 xfc->contacts[1].pos_y - xfc->contacts[1].last_y };
340 const double px = fabs(dx[0]) < fabs(dx[1]) ? dx[0] : dx[1];
341 const double py = fabs(dy[0]) < fabs(dy[1]) ? dy[0] : dy[1];
342 xfc->px_vector += px;
343 xfc->py_vector += py;
344 const double dist_x = fabs(xfc->contacts[0].pos_x - xfc->contacts[1].pos_x);
345 const double dist_y = fabs(xfc->contacts[0].pos_y - xfc->contacts[1].pos_y);
347 if (dist_y > MIN_FINGER_DIST)
349 if (xfc->px_vector > PAN_THRESHOLD)
352 PanningChangeEventArgs e;
353 EventArgsInit(&e,
"xfreerdp");
356 if (PubSub_OnPanningChange(ctx->pubSub, xfc, &e) < 0)
363 else if (xfc->px_vector < -PAN_THRESHOLD)
366 PanningChangeEventArgs e;
367 EventArgsInit(&e,
"xfreerdp");
370 if (PubSub_OnPanningChange(ctx->pubSub, xfc, &e) < 0)
379 if (dist_x > MIN_FINGER_DIST)
381 if (xfc->py_vector > PAN_THRESHOLD)
384 PanningChangeEventArgs e;
385 EventArgsInit(&e,
"xfreerdp");
388 if (PubSub_OnPanningChange(ctx->pubSub, xfc, &e) < 0)
395 else if (xfc->py_vector < -PAN_THRESHOLD)
398 PanningChangeEventArgs e;
399 EventArgsInit(&e,
"xfreerdp");
402 if (PubSub_OnPanningChange(ctx->pubSub, xfc, &e) < 0)
414static BOOL xf_input_detect_pinch(xfContext* xfc)
416 ZoomingChangeEventArgs e = WINPR_C_ARRAY_INIT;
419 rdpContext* ctx = &xfc->common.context;
422 if (xfc->active_contacts != 2)
424 xfc->firstDist = -1.0;
429 const double dist = sqrt(pow(xfc->contacts[1].pos_x - xfc->contacts[0].last_x, 2.0) +
430 pow(xfc->contacts[1].pos_y - xfc->contacts[0].last_y, 2.0));
433 if (xfc->firstDist <= 0)
435 xfc->firstDist = dist;
436 xfc->lastDist = xfc->firstDist;
443 double delta = xfc->lastDist - dist;
452 xfc->z_vector += delta;
453 xfc->lastDist = dist;
455 if (xfc->z_vector > ZOOM_THRESHOLD)
457 EventArgsInit(&e,
"xfreerdp");
459 if (PubSub_OnZoomingChange(ctx->pubSub, xfc, &e) < 0)
466 if (xfc->z_vector < -ZOOM_THRESHOLD)
468 EventArgsInit(&e,
"xfreerdp");
470 if (PubSub_OnZoomingChange(ctx->pubSub, xfc, &e) < 0)
480static void xf_input_touch_begin(xfContext* xfc,
const XIDeviceEvent* event)
483 for (
int i = 0; i < MAX_CONTACTS; i++)
485 if (xfc->contacts[i].id == 0)
487 xfc->contacts[i].id =
event->detail;
488 xfc->contacts[i].count = 1;
489 xfc->contacts[i].pos_x =
event->event_x;
490 xfc->contacts[i].pos_y =
event->event_y;
491 xfc->active_contacts++;
498static BOOL xf_input_touch_update(xfContext* xfc,
const XIDeviceEvent* event)
503 for (
int i = 0; i < MAX_CONTACTS; i++)
505 if (xfc->contacts[i].id == event->detail)
507 xfc->contacts[i].count++;
508 xfc->contacts[i].last_x = xfc->contacts[i].pos_x;
509 xfc->contacts[i].last_y = xfc->contacts[i].pos_y;
510 xfc->contacts[i].pos_x =
event->event_x;
511 xfc->contacts[i].pos_y =
event->event_y;
512 if (!xf_input_detect_pinch(xfc))
514 if (!xf_input_detect_pan(xfc))
523static void xf_input_touch_end(xfContext* xfc,
const XIDeviceEvent* event)
526 for (
int i = 0; i < MAX_CONTACTS; i++)
528 if (xfc->contacts[i].id == event->detail)
530 xfc->contacts[i].id = 0;
531 xfc->contacts[i].count = 0;
532 xfc->active_contacts--;
538static int xf_input_handle_event_local(xfContext* xfc,
const XEvent* event)
542 const XGenericEventCookie* cc;
543 XGenericEventCookie* vc;
545 cookie.cc = &
event->xcookie;
546 XGetEventData(xfc->display, cookie.vc);
548 if ((cookie.cc->type == GenericEvent) && (cookie.cc->extension == xfc->XInputOpcode))
550 switch (cookie.cc->evtype)
553 if (xf_input_is_duplicate(xfc, cookie.cc) == FALSE)
554 xf_input_touch_begin(xfc, cookie.cc->data);
556 xf_input_save_last_event(xfc, cookie.cc);
560 if (xf_input_is_duplicate(xfc, cookie.cc) == FALSE)
562 if (!xf_input_touch_update(xfc, cookie.cc->data))
566 xf_input_save_last_event(xfc, cookie.cc);
570 if (xf_input_is_duplicate(xfc, cookie.cc) == FALSE)
571 xf_input_touch_end(xfc, cookie.cc->data);
573 xf_input_save_last_event(xfc, cookie.cc);
577 xf_input_event(xfc, event, cookie.cc->data, cookie.cc->evtype);
582 XFreeEventData(xfc->display, cookie.vc);
586static void xf_input_hide_cursor(xfContext* xfc)
590 if (!xfc->cursorHidden)
592 XcursorImage ci = WINPR_C_ARRAY_INIT;
594 static Cursor nullcursor = None;
596 ci.version = XCURSOR_IMAGE_VERSION;
597 ci.size =
sizeof(ci);
598 ci.width = ci.height = 1;
599 ci.xhot = ci.yhot = 0;
601 nullcursor = XcursorImageLoadCursor(xfc->display, &ci);
603 if ((xfc->window) && (nullcursor != None))
604 XDefineCursor(xfc->display, xfc->window->handle, nullcursor);
606 xfc->cursorHidden = TRUE;
613static void xf_input_show_cursor(xfContext* xfc)
618 if (xfc->cursorHidden)
623 XUndefineCursor(xfc->display, xfc->window->handle);
625 XDefineCursor(xfc->display, xfc->window->handle, xfc->pointer->cursor);
628 xfc->cursorHidden = FALSE;
635static int xf_input_touch_remote(xfContext* xfc, XIDeviceEvent* event,
int evtype)
640 RdpeiClientContext* rdpei = xfc->common.rdpei;
645 xf_input_hide_cursor(xfc);
646 touchId =
event->detail;
647 x = (int)event->event_x;
648 y = (int)event->event_y;
649 xf_event_adjust_coordinates(xfc, &x, &y);
654 freerdp_client_handle_touch(&xfc->common, FREERDP_TOUCH_DOWN, touchId, 0, x, y);
657 freerdp_client_handle_touch(&xfc->common, FREERDP_TOUCH_MOTION, touchId, 0, x, y);
660 freerdp_client_handle_touch(&xfc->common, FREERDP_TOUCH_UP, touchId, 0, x, y);
669static BOOL xf_input_pen_remote(xfContext* xfc, XIDeviceEvent* event,
int evtype,
int deviceid)
673 RdpeiClientContext* rdpei = xfc->common.rdpei;
678 xf_input_hide_cursor(xfc);
679 x = (int)event->event_x;
680 y = (int)event->event_y;
681 xf_event_adjust_coordinates(xfc, &x, &y);
683 double pressure = 0.0;
684 double* val =
event->valuators.values;
685 for (
int i = 0; i < MIN(event->valuators.mask_len * 8, 3); i++)
687 if (XIMaskIsSet(event->valuators.mask, i))
689 double value = *val++;
695 UINT32 flags = FREERDP_PEN_HAS_PRESSURE;
696 if ((evtype == XI_ButtonPress) || (evtype == XI_ButtonRelease))
698 WLog_DBG(TAG,
"pen button %d", event->detail);
699 switch (event->detail)
704 flags |= FREERDP_PEN_BARREL_PRESSED;
714 flags |= FREERDP_PEN_PRESS;
715 if (!freerdp_client_handle_pen(&xfc->common, flags, deviceid, x, y, pressure))
719 flags |= FREERDP_PEN_MOTION;
720 if (!freerdp_client_handle_pen(&xfc->common, flags, deviceid, x, y, pressure))
723 case XI_ButtonRelease:
724 flags |= FREERDP_PEN_RELEASE;
725 if (!freerdp_client_handle_pen(&xfc->common, flags, deviceid, x, y, pressure))
734static int xf_input_pens_unhover(xfContext* xfc)
738 freerdp_client_pen_cancel_all(&xfc->common);
742bool xf_use_rel_mouse(xfContext* xfc)
744 if (!freerdp_client_use_relative_mouse_events(&xfc->common))
746 if (!xfc->isCursorHidden)
751int xf_input_event(xfContext* xfc, WINPR_ATTR_UNUSED
const XEvent* xevent, XIDeviceEvent* event,
755 WINPR_ASSERT(xevent);
758 xfWindow* window = xfc->window;
761 if (xf_floatbar_is_locked(window->floatbar))
765 xf_input_show_cursor(xfc);
770 case XI_ButtonRelease:
771 xfc->xi_event = !xfc->common.mouse_grabbed || !xf_use_rel_mouse(xfc);
775 if (!xfc_is_floatbar_window(xfc, event->event) || (evtype != XI_ButtonPress))
777 xf_generic_ButtonEvent(xfc, (
int)event->event_x, (
int)event->event_y,
778 event->detail, event->event, xfc->remote_app,
779 evtype == XI_ButtonPress);
785 xfc->xi_event = !xfc->common.mouse_grabbed || !xf_use_rel_mouse(xfc);
789 xf_generic_MotionNotify(xfc, (
int)event->event_x, (
int)event->event_y, event->event,
793 case XI_RawButtonPress:
794 case XI_RawButtonRelease:
795 xfc->xi_rawevent = xfc->common.mouse_grabbed && xf_use_rel_mouse(xfc);
797 if (xfc->xi_rawevent)
799 const XIRawEvent* ev = (
const XIRawEvent*)event;
800 xf_generic_RawButtonEvent(xfc, ev->detail, xfc->remote_app,
801 evtype == XI_RawButtonPress);
805 xfc->xi_rawevent = xfc->common.mouse_grabbed && xf_use_rel_mouse(xfc);
807 if (xfc->xi_rawevent)
809 const XIRawEvent* ev = (
const XIRawEvent*)event;
812 if (XIMaskIsSet(ev->valuators.mask, 0))
813 x = ev->raw_values[0];
814 if (XIMaskIsSet(ev->valuators.mask, 1))
815 y = ev->raw_values[1];
817 xf_generic_RawMotionNotify(xfc, (
int)x, (
int)y, event->event, xfc->remote_app);
820 case XI_DeviceChanged:
822 const XIDeviceChangedEvent* ev = (
const XIDeviceChangedEvent*)event;
823 if (ev->reason != XIDeviceChange)
835 register_input_events(xfc, xfc->window->handle);
838 case XI_HierarchyChanged:
840 register_input_events(xfc, xfc->window->handle);
844 WLog_WARN(TAG,
"Unhandled event %d: Event was registered but is not handled!", evtype);
851static int xf_input_handle_event_remote(xfContext* xfc,
const XEvent* event)
855 const XGenericEventCookie* cc;
856 XGenericEventCookie* vc;
858 cookie.cc = &
event->xcookie;
859 XGetEventData(xfc->display, cookie.vc);
861 if ((cookie.cc->type == GenericEvent) && (cookie.cc->extension == xfc->XInputOpcode))
863 switch (cookie.cc->evtype)
866 xf_input_pens_unhover(xfc);
871 xf_input_touch_remote(xfc, cookie.cc->data, cookie.cc->evtype);
875 case XI_ButtonRelease:
877 WLog_DBG(TAG,
"checking for pen");
878 XIDeviceEvent* deviceEvent = (XIDeviceEvent*)cookie.cc->data;
879 int deviceid = deviceEvent->deviceid;
881 if (freerdp_client_is_pen(&xfc->common, deviceid))
883 if (!xf_input_pen_remote(xfc, cookie.cc->data, cookie.cc->evtype, deviceid))
886 xf_input_event(xfc, event, cookie.cc->data, cookie.cc->evtype);
894 xf_input_pens_unhover(xfc);
895 xf_input_event(xfc, event, cookie.cc->data, cookie.cc->evtype);
900 XFreeEventData(xfc->display, cookie.vc);
906int xf_input_init(xfContext* xfc, Window window)
913int xf_input_handle_event(xfContext* xfc,
const XEvent* event)
916 const rdpSettings* settings =
nullptr;
919 settings = xfc->common.context.settings;
920 WINPR_ASSERT(settings);
924 return xf_input_handle_event_remote(xfc, event);
928 return xf_input_handle_event_local(xfc, event);
932 return xf_input_handle_event_local(xfc, event);
WINPR_ATTR_NODISCARD FREERDP_API BOOL freerdp_settings_get_bool(const rdpSettings *settings, FreeRDP_Settings_Keys_Bool id)
Returns a boolean settings value.