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] = { 0 };
80 BYTE masks[MAX_NR_MASKS][XIMaskLen(XI_LASTEVENT)] = { 0 };
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*
class = dev->classes[j];
111 const XITouchClassInfo* t = (
const XITouchClassInfo*)
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*)
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 = NULL;
142 const XIValuatorClassInfo* t = (
const XIValuatorClassInfo*)
class;
143 char* name = t->label ? Safe_XGetAtomName(log, xfc->display, t->label) : NULL;
145 WLog_Print(log, WLOG_DEBUG,
146 "%s device (id: %d) valuator %d label %s range %f - %f", dev->name,
147 dev->deviceid, t->number, name ? name :
"None", t->min, t->max);
152 double max_pressure = t->max;
154 char devName[200] = { 0 };
155 strncpy(devName, dev->name, ARRAYSIZE(devName) - 1);
156 CharLowerBuffA(devName, ARRAYSIZE(devName));
158 if (strstr(devName,
"eraser") != NULL)
160 if (freerdp_client_handle_pen(&xfc->common,
161 FREERDP_PEN_REGISTER |
162 FREERDP_PEN_IS_INVERTED |
163 FREERDP_PEN_HAS_PRESSURE,
164 dev->deviceid, max_pressure))
165 WLog_DBG(TAG,
"registered eraser");
167 else if (strstr(devName,
"stylus") != NULL ||
168 strstr(devName,
"pen") != NULL)
170 if (freerdp_client_handle_pen(
171 &xfc->common, FREERDP_PEN_REGISTER | FREERDP_PEN_HAS_PRESSURE,
172 dev->deviceid, max_pressure))
173 WLog_DBG(TAG,
"registered pen");
186 XIFreeDeviceInfo(info);
190 Status xstatus = XISelectEvents(xfc->display, window, evmasks, nmasks);
192 WLog_WARN(TAG,
"XISelectEvents returned %d", xstatus);
198static BOOL register_raw_events(xfContext* xfc, Window window)
201 unsigned char mask_bytes[XIMaskLen(XI_LASTEVENT)] = { 0 };
202 rdpSettings* settings = NULL;
206 settings = xfc->common.context.settings;
207 WINPR_ASSERT(settings);
211 XISetMask(mask_bytes, XI_RawMotion);
212 XISetMask(mask_bytes, XI_RawButtonPress);
213 XISetMask(mask_bytes, XI_RawButtonRelease);
215 mask.deviceid = XIAllMasterDevices;
216 mask.mask_len =
sizeof(mask_bytes);
217 mask.mask = mask_bytes;
219 XISelectEvents(xfc->display, window, &mask, 1);
225static BOOL register_device_events(xfContext* xfc, Window window)
227 XIEventMask mask = { 0 };
228 unsigned char mask_bytes[XIMaskLen(XI_LASTEVENT)] = { 0 };
232 XISetMask(mask_bytes, XI_DeviceChanged);
233 XISetMask(mask_bytes, XI_HierarchyChanged);
235 mask.deviceid = XIAllDevices;
236 mask.mask_len =
sizeof(mask_bytes);
237 mask.mask = mask_bytes;
239 XISelectEvents(xfc->display, window, &mask, 1);
244int xf_input_init(xfContext* xfc, Window window)
246 int major = XI_2_Major;
247 int minor = XI_2_Minor;
254 xfc->firstDist = -1.0;
258 xfc->active_contacts = 0;
260 if (!XQueryExtension(xfc->display,
"XInputExtension", &opcode, &event, &error))
262 WLog_WARN(TAG,
"XInput extension not available.");
266 xfc->XInputOpcode = opcode;
267 XIQueryVersion(xfc->display, &major, &minor);
269 if ((major < XI_2_Major) || ((major == XI_2_Major) && (minor < 2)))
271 WLog_WARN(TAG,
"Server does not support XI 2.2");
276 int scr = DefaultScreen(xfc->display);
277 Window root = RootWindow(xfc->display, scr);
279 if (!register_raw_events(xfc, root))
281 if (!register_input_events(xfc, window))
283 if (!register_device_events(xfc, window))
290static BOOL xf_input_is_duplicate(xfContext* xfc,
const XGenericEventCookie* cookie)
292 const XIDeviceEvent*
event = NULL;
295 WINPR_ASSERT(cookie);
297 event = cookie->data;
300 if ((xfc->lastEvent.time == event->time) && (xfc->lastEvType == cookie->evtype) &&
301 (xfc->lastEvent.detail == event->detail) &&
302 (fabs(xfc->lastEvent.event_x - event->event_x) < DBL_EPSILON) &&
303 (fabs(xfc->lastEvent.event_y - event->event_y) < DBL_EPSILON))
311static void xf_input_save_last_event(xfContext* xfc,
const XGenericEventCookie* cookie)
313 const XIDeviceEvent*
event = NULL;
316 WINPR_ASSERT(cookie);
318 event = cookie->data;
321 xfc->lastEvType = cookie->evtype;
322 xfc->lastEvent.time =
event->time;
323 xfc->lastEvent.detail =
event->detail;
324 xfc->lastEvent.event_x =
event->event_x;
325 xfc->lastEvent.event_y =
event->event_y;
328static void xf_input_detect_pan(xfContext* xfc)
331 rdpContext* ctx = &xfc->common.context;
334 if (xfc->active_contacts != 2)
339 const double dx[] = { xfc->contacts[0].pos_x - xfc->contacts[0].last_x,
340 xfc->contacts[1].pos_x - xfc->contacts[1].last_x };
341 const double dy[] = { xfc->contacts[0].pos_y - xfc->contacts[0].last_y,
342 xfc->contacts[1].pos_y - xfc->contacts[1].last_y };
343 const double px = fabs(dx[0]) < fabs(dx[1]) ? dx[0] : dx[1];
344 const double py = fabs(dy[0]) < fabs(dy[1]) ? dy[0] : dy[1];
345 xfc->px_vector += px;
346 xfc->py_vector += py;
347 const double dist_x = fabs(xfc->contacts[0].pos_x - xfc->contacts[1].pos_x);
348 const double dist_y = fabs(xfc->contacts[0].pos_y - xfc->contacts[1].pos_y);
350 if (dist_y > MIN_FINGER_DIST)
352 if (xfc->px_vector > PAN_THRESHOLD)
355 PanningChangeEventArgs e;
356 EventArgsInit(&e,
"xfreerdp");
359 PubSub_OnPanningChange(ctx->pubSub, xfc, &e);
365 else if (xfc->px_vector < -PAN_THRESHOLD)
368 PanningChangeEventArgs e;
369 EventArgsInit(&e,
"xfreerdp");
372 PubSub_OnPanningChange(ctx->pubSub, xfc, &e);
380 if (dist_x > MIN_FINGER_DIST)
382 if (xfc->py_vector > PAN_THRESHOLD)
385 PanningChangeEventArgs e;
386 EventArgsInit(&e,
"xfreerdp");
389 PubSub_OnPanningChange(ctx->pubSub, xfc, &e);
395 else if (xfc->py_vector < -PAN_THRESHOLD)
398 PanningChangeEventArgs e;
399 EventArgsInit(&e,
"xfreerdp");
402 PubSub_OnPanningChange(ctx->pubSub, xfc, &e);
411static void xf_input_detect_pinch(xfContext* xfc)
413 ZoomingChangeEventArgs e = { 0 };
416 rdpContext* ctx = &xfc->common.context;
419 if (xfc->active_contacts != 2)
421 xfc->firstDist = -1.0;
426 const double dist = sqrt(pow(xfc->contacts[1].pos_x - xfc->contacts[0].last_x, 2.0) +
427 pow(xfc->contacts[1].pos_y - xfc->contacts[0].last_y, 2.0));
430 if (xfc->firstDist <= 0)
432 xfc->firstDist = dist;
433 xfc->lastDist = xfc->firstDist;
440 double delta = xfc->lastDist - dist;
449 xfc->z_vector += delta;
450 xfc->lastDist = dist;
452 if (xfc->z_vector > ZOOM_THRESHOLD)
454 EventArgsInit(&e,
"xfreerdp");
456 PubSub_OnZoomingChange(ctx->pubSub, xfc, &e);
462 if (xfc->z_vector < -ZOOM_THRESHOLD)
464 EventArgsInit(&e,
"xfreerdp");
466 PubSub_OnZoomingChange(ctx->pubSub, xfc, &e);
474static void xf_input_touch_begin(xfContext* xfc,
const XIDeviceEvent* event)
477 for (
int i = 0; i < MAX_CONTACTS; i++)
479 if (xfc->contacts[i].id == 0)
481 xfc->contacts[i].id =
event->detail;
482 xfc->contacts[i].count = 1;
483 xfc->contacts[i].pos_x =
event->event_x;
484 xfc->contacts[i].pos_y =
event->event_y;
485 xfc->active_contacts++;
491static void xf_input_touch_update(xfContext* xfc,
const XIDeviceEvent* event)
496 for (
int i = 0; i < MAX_CONTACTS; i++)
498 if (xfc->contacts[i].id == event->detail)
500 xfc->contacts[i].count++;
501 xfc->contacts[i].last_x = xfc->contacts[i].pos_x;
502 xfc->contacts[i].last_y = xfc->contacts[i].pos_y;
503 xfc->contacts[i].pos_x =
event->event_x;
504 xfc->contacts[i].pos_y =
event->event_y;
505 xf_input_detect_pinch(xfc);
506 xf_input_detect_pan(xfc);
512static void xf_input_touch_end(xfContext* xfc,
const XIDeviceEvent* event)
515 for (
int i = 0; i < MAX_CONTACTS; i++)
517 if (xfc->contacts[i].id == event->detail)
519 xfc->contacts[i].id = 0;
520 xfc->contacts[i].count = 0;
521 xfc->active_contacts--;
527static int xf_input_handle_event_local(xfContext* xfc,
const XEvent* event)
531 const XGenericEventCookie* cc;
532 XGenericEventCookie* vc;
534 cookie.cc = &
event->xcookie;
535 XGetEventData(xfc->display, cookie.vc);
537 if ((cookie.cc->type == GenericEvent) && (cookie.cc->extension == xfc->XInputOpcode))
539 switch (cookie.cc->evtype)
542 if (xf_input_is_duplicate(xfc, cookie.cc) == FALSE)
543 xf_input_touch_begin(xfc, cookie.cc->data);
545 xf_input_save_last_event(xfc, cookie.cc);
549 if (xf_input_is_duplicate(xfc, cookie.cc) == FALSE)
550 xf_input_touch_update(xfc, cookie.cc->data);
552 xf_input_save_last_event(xfc, cookie.cc);
556 if (xf_input_is_duplicate(xfc, cookie.cc) == FALSE)
557 xf_input_touch_end(xfc, cookie.cc->data);
559 xf_input_save_last_event(xfc, cookie.cc);
563 xf_input_event(xfc, event, cookie.cc->data, cookie.cc->evtype);
568 XFreeEventData(xfc->display, cookie.vc);
573static char* xf_input_touch_state_string(DWORD flags)
575 if (flags & RDPINPUT_CONTACT_FLAG_DOWN)
576 return "RDPINPUT_CONTACT_FLAG_DOWN";
577 else if (flags & RDPINPUT_CONTACT_FLAG_UPDATE)
578 return "RDPINPUT_CONTACT_FLAG_UPDATE";
579 else if (flags & RDPINPUT_CONTACT_FLAG_UP)
580 return "RDPINPUT_CONTACT_FLAG_UP";
581 else if (flags & RDPINPUT_CONTACT_FLAG_INRANGE)
582 return "RDPINPUT_CONTACT_FLAG_INRANGE";
583 else if (flags & RDPINPUT_CONTACT_FLAG_INCONTACT)
584 return "RDPINPUT_CONTACT_FLAG_INCONTACT";
585 else if (flags & RDPINPUT_CONTACT_FLAG_CANCELED)
586 return "RDPINPUT_CONTACT_FLAG_CANCELED";
588 return "RDPINPUT_CONTACT_FLAG_UNKNOWN";
592static void xf_input_hide_cursor(xfContext* xfc)
596 if (!xfc->cursorHidden)
598 XcursorImage ci = { 0 };
600 static Cursor nullcursor = None;
602 ci.version = XCURSOR_IMAGE_VERSION;
603 ci.size =
sizeof(ci);
604 ci.width = ci.height = 1;
605 ci.xhot = ci.yhot = 0;
607 nullcursor = XcursorImageLoadCursor(xfc->display, &ci);
609 if ((xfc->window) && (nullcursor != None))
610 XDefineCursor(xfc->display, xfc->window->handle, nullcursor);
612 xfc->cursorHidden = TRUE;
619static void xf_input_show_cursor(xfContext* xfc)
624 if (xfc->cursorHidden)
629 XUndefineCursor(xfc->display, xfc->window->handle);
631 XDefineCursor(xfc->display, xfc->window->handle, xfc->pointer->cursor);
634 xfc->cursorHidden = FALSE;
641static int xf_input_touch_remote(xfContext* xfc, XIDeviceEvent* event,
int evtype)
646 RdpeiClientContext* rdpei = xfc->common.rdpei;
651 xf_input_hide_cursor(xfc);
652 touchId =
event->detail;
653 x = (int)event->event_x;
654 y = (int)event->event_y;
655 xf_event_adjust_coordinates(xfc, &x, &y);
660 freerdp_client_handle_touch(&xfc->common, FREERDP_TOUCH_DOWN, touchId, 0, x, y);
663 freerdp_client_handle_touch(&xfc->common, FREERDP_TOUCH_MOTION, touchId, 0, x, y);
666 freerdp_client_handle_touch(&xfc->common, FREERDP_TOUCH_UP, touchId, 0, x, y);
675static BOOL xf_input_pen_remote(xfContext* xfc, XIDeviceEvent* event,
int evtype,
int deviceid)
679 RdpeiClientContext* rdpei = xfc->common.rdpei;
684 xf_input_hide_cursor(xfc);
685 x = (int)event->event_x;
686 y = (int)event->event_y;
687 xf_event_adjust_coordinates(xfc, &x, &y);
689 double pressure = 0.0;
690 double* val =
event->valuators.values;
691 for (
int i = 0; i < MIN(event->valuators.mask_len * 8, 3); i++)
693 if (XIMaskIsSet(event->valuators.mask, i))
695 double value = *val++;
701 UINT32 flags = FREERDP_PEN_HAS_PRESSURE;
702 if ((evtype == XI_ButtonPress) || (evtype == XI_ButtonRelease))
704 WLog_DBG(TAG,
"pen button %d", event->detail);
705 switch (event->detail)
710 flags |= FREERDP_PEN_BARREL_PRESSED;
720 flags |= FREERDP_PEN_PRESS;
721 if (!freerdp_client_handle_pen(&xfc->common, flags, deviceid, x, y, pressure))
725 flags |= FREERDP_PEN_MOTION;
726 if (!freerdp_client_handle_pen(&xfc->common, flags, deviceid, x, y, pressure))
729 case XI_ButtonRelease:
730 flags |= FREERDP_PEN_RELEASE;
731 if (!freerdp_client_handle_pen(&xfc->common, flags, deviceid, x, y, pressure))
740static int xf_input_pens_unhover(xfContext* xfc)
744 freerdp_client_pen_cancel_all(&xfc->common);
748static bool xf_use_rel_mouse(xfContext* xfc)
750 if (!freerdp_client_use_relative_mouse_events(&xfc->common))
752 if (!xfc->isCursorHidden)
757int xf_input_event(xfContext* xfc, WINPR_ATTR_UNUSED
const XEvent* xevent, XIDeviceEvent* event,
761 WINPR_ASSERT(xevent);
764 xfWindow* window = xfc->window;
767 if (xf_floatbar_is_locked(window->floatbar))
771 xf_input_show_cursor(xfc);
776 case XI_ButtonRelease:
777 xfc->xi_event = !xfc->common.mouse_grabbed || !xf_use_rel_mouse(xfc);
781 xf_generic_ButtonEvent(xfc, (
int)event->event_x, (
int)event->event_y, event->detail,
782 event->event, xfc->remote_app, evtype == XI_ButtonPress);
787 xfc->xi_event = !xfc->common.mouse_grabbed || !xf_use_rel_mouse(xfc);
791 xf_generic_MotionNotify(xfc, (
int)event->event_x, (
int)event->event_y,
792 event->detail, event->event, xfc->remote_app);
795 case XI_RawButtonPress:
796 case XI_RawButtonRelease:
797 xfc->xi_rawevent = xfc->common.mouse_grabbed && xf_use_rel_mouse(xfc);
799 if (xfc->xi_rawevent)
801 const XIRawEvent* ev = (
const XIRawEvent*)event;
802 xf_generic_RawButtonEvent(xfc, ev->detail, xfc->remote_app,
803 evtype == XI_RawButtonPress);
807 xfc->xi_rawevent = xfc->common.mouse_grabbed && xf_use_rel_mouse(xfc);
809 if (xfc->xi_rawevent)
811 const XIRawEvent* ev = (
const XIRawEvent*)event;
814 if (XIMaskIsSet(ev->valuators.mask, 0))
815 x = ev->raw_values[0];
816 if (XIMaskIsSet(ev->valuators.mask, 1))
817 y = ev->raw_values[1];
819 xf_generic_RawMotionNotify(xfc, (
int)x, (
int)y, event->event, xfc->remote_app);
822 case XI_DeviceChanged:
824 const XIDeviceChangedEvent* ev = (
const XIDeviceChangedEvent*)event;
825 if (ev->reason != XIDeviceChange)
837 register_input_events(xfc, xfc->window->handle);
840 case XI_HierarchyChanged:
842 register_input_events(xfc, xfc->window->handle);
846 WLog_WARN(TAG,
"Unhandled event %d: Event was registered but is not handled!", evtype);
853static int xf_input_handle_event_remote(xfContext* xfc,
const XEvent* event)
857 const XGenericEventCookie* cc;
858 XGenericEventCookie* vc;
860 cookie.cc = &
event->xcookie;
861 XGetEventData(xfc->display, cookie.vc);
863 if ((cookie.cc->type == GenericEvent) && (cookie.cc->extension == xfc->XInputOpcode))
865 switch (cookie.cc->evtype)
868 xf_input_pens_unhover(xfc);
873 xf_input_touch_remote(xfc, cookie.cc->data, cookie.cc->evtype);
877 case XI_ButtonRelease:
879 WLog_DBG(TAG,
"checking for pen");
880 XIDeviceEvent* deviceEvent = (XIDeviceEvent*)cookie.cc->data;
881 int deviceid = deviceEvent->deviceid;
883 if (freerdp_client_is_pen(&xfc->common, deviceid))
885 if (!xf_input_pen_remote(xfc, cookie.cc->data, cookie.cc->evtype, deviceid))
888 xf_input_event(xfc, event, cookie.cc->data, cookie.cc->evtype);
896 xf_input_pens_unhover(xfc);
897 xf_input_event(xfc, event, cookie.cc->data, cookie.cc->evtype);
902 XFreeEventData(xfc->display, cookie.vc);
908int xf_input_init(xfContext* xfc, Window window)
915int xf_input_handle_event(xfContext* xfc,
const XEvent* event)
918 const rdpSettings* settings = NULL;
921 settings = xfc->common.context.settings;
922 WINPR_ASSERT(settings);
926 return xf_input_handle_event_remote(xfc, event);
930 return xf_input_handle_event_local(xfc, event);
934 return xf_input_handle_event_local(xfc, event);
FREERDP_API BOOL freerdp_settings_get_bool(const rdpSettings *settings, FreeRDP_Settings_Keys_Bool id)
Returns a boolean settings value.