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* 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 = NULL;
142 const XIValuatorClassInfo* t = (
const XIValuatorClassInfo*)c_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);
572static void xf_input_hide_cursor(xfContext* xfc)
576 if (!xfc->cursorHidden)
578 XcursorImage ci = { 0 };
580 static Cursor nullcursor = None;
582 ci.version = XCURSOR_IMAGE_VERSION;
583 ci.size =
sizeof(ci);
584 ci.width = ci.height = 1;
585 ci.xhot = ci.yhot = 0;
587 nullcursor = XcursorImageLoadCursor(xfc->display, &ci);
589 if ((xfc->window) && (nullcursor != None))
590 XDefineCursor(xfc->display, xfc->window->handle, nullcursor);
592 xfc->cursorHidden = TRUE;
599static void xf_input_show_cursor(xfContext* xfc)
604 if (xfc->cursorHidden)
609 XUndefineCursor(xfc->display, xfc->window->handle);
611 XDefineCursor(xfc->display, xfc->window->handle, xfc->pointer->cursor);
614 xfc->cursorHidden = FALSE;
621static int xf_input_touch_remote(xfContext* xfc, XIDeviceEvent* event,
int evtype)
626 RdpeiClientContext* rdpei = xfc->common.rdpei;
631 xf_input_hide_cursor(xfc);
632 touchId =
event->detail;
633 x = (int)event->event_x;
634 y = (int)event->event_y;
635 xf_event_adjust_coordinates(xfc, &x, &y);
640 freerdp_client_handle_touch(&xfc->common, FREERDP_TOUCH_DOWN, touchId, 0, x, y);
643 freerdp_client_handle_touch(&xfc->common, FREERDP_TOUCH_MOTION, touchId, 0, x, y);
646 freerdp_client_handle_touch(&xfc->common, FREERDP_TOUCH_UP, touchId, 0, x, y);
655static BOOL xf_input_pen_remote(xfContext* xfc, XIDeviceEvent* event,
int evtype,
int deviceid)
659 RdpeiClientContext* rdpei = xfc->common.rdpei;
664 xf_input_hide_cursor(xfc);
665 x = (int)event->event_x;
666 y = (int)event->event_y;
667 xf_event_adjust_coordinates(xfc, &x, &y);
669 double pressure = 0.0;
670 double* val =
event->valuators.values;
671 for (
int i = 0; i < MIN(event->valuators.mask_len * 8, 3); i++)
673 if (XIMaskIsSet(event->valuators.mask, i))
675 double value = *val++;
681 UINT32 flags = FREERDP_PEN_HAS_PRESSURE;
682 if ((evtype == XI_ButtonPress) || (evtype == XI_ButtonRelease))
684 WLog_DBG(TAG,
"pen button %d", event->detail);
685 switch (event->detail)
690 flags |= FREERDP_PEN_BARREL_PRESSED;
700 flags |= FREERDP_PEN_PRESS;
701 if (!freerdp_client_handle_pen(&xfc->common, flags, deviceid, x, y, pressure))
705 flags |= FREERDP_PEN_MOTION;
706 if (!freerdp_client_handle_pen(&xfc->common, flags, deviceid, x, y, pressure))
709 case XI_ButtonRelease:
710 flags |= FREERDP_PEN_RELEASE;
711 if (!freerdp_client_handle_pen(&xfc->common, flags, deviceid, x, y, pressure))
720static int xf_input_pens_unhover(xfContext* xfc)
724 freerdp_client_pen_cancel_all(&xfc->common);
728bool xf_use_rel_mouse(xfContext* xfc)
730 if (!freerdp_client_use_relative_mouse_events(&xfc->common))
732 if (!xfc->isCursorHidden)
737int xf_input_event(xfContext* xfc, WINPR_ATTR_UNUSED
const XEvent* xevent, XIDeviceEvent* event,
741 WINPR_ASSERT(xevent);
744 xfWindow* window = xfc->window;
747 if (xf_floatbar_is_locked(window->floatbar))
751 xf_input_show_cursor(xfc);
756 case XI_ButtonRelease:
757 xfc->xi_event = !xfc->common.mouse_grabbed || !xf_use_rel_mouse(xfc);
761 if (!xfc_is_floatbar_window(xfc, event->event) || (evtype != XI_ButtonPress))
763 xf_generic_ButtonEvent(xfc, (
int)event->event_x, (
int)event->event_y,
764 event->detail, event->event, xfc->remote_app,
765 evtype == XI_ButtonPress);
771 xfc->xi_event = !xfc->common.mouse_grabbed || !xf_use_rel_mouse(xfc);
775 xf_generic_MotionNotify(xfc, (
int)event->event_x, (
int)event->event_y, event->event,
779 case XI_RawButtonPress:
780 case XI_RawButtonRelease:
781 xfc->xi_rawevent = xfc->common.mouse_grabbed && xf_use_rel_mouse(xfc);
783 if (xfc->xi_rawevent)
785 const XIRawEvent* ev = (
const XIRawEvent*)event;
786 xf_generic_RawButtonEvent(xfc, ev->detail, xfc->remote_app,
787 evtype == XI_RawButtonPress);
791 xfc->xi_rawevent = xfc->common.mouse_grabbed && xf_use_rel_mouse(xfc);
793 if (xfc->xi_rawevent)
795 const XIRawEvent* ev = (
const XIRawEvent*)event;
798 if (XIMaskIsSet(ev->valuators.mask, 0))
799 x = ev->raw_values[0];
800 if (XIMaskIsSet(ev->valuators.mask, 1))
801 y = ev->raw_values[1];
803 xf_generic_RawMotionNotify(xfc, (
int)x, (
int)y, event->event, xfc->remote_app);
806 case XI_DeviceChanged:
808 const XIDeviceChangedEvent* ev = (
const XIDeviceChangedEvent*)event;
809 if (ev->reason != XIDeviceChange)
821 register_input_events(xfc, xfc->window->handle);
824 case XI_HierarchyChanged:
826 register_input_events(xfc, xfc->window->handle);
830 WLog_WARN(TAG,
"Unhandled event %d: Event was registered but is not handled!", evtype);
837static int xf_input_handle_event_remote(xfContext* xfc,
const XEvent* event)
841 const XGenericEventCookie* cc;
842 XGenericEventCookie* vc;
844 cookie.cc = &
event->xcookie;
845 XGetEventData(xfc->display, cookie.vc);
847 if ((cookie.cc->type == GenericEvent) && (cookie.cc->extension == xfc->XInputOpcode))
849 switch (cookie.cc->evtype)
852 xf_input_pens_unhover(xfc);
857 xf_input_touch_remote(xfc, cookie.cc->data, cookie.cc->evtype);
861 case XI_ButtonRelease:
863 WLog_DBG(TAG,
"checking for pen");
864 XIDeviceEvent* deviceEvent = (XIDeviceEvent*)cookie.cc->data;
865 int deviceid = deviceEvent->deviceid;
867 if (freerdp_client_is_pen(&xfc->common, deviceid))
869 if (!xf_input_pen_remote(xfc, cookie.cc->data, cookie.cc->evtype, deviceid))
872 xf_input_event(xfc, event, cookie.cc->data, cookie.cc->evtype);
880 xf_input_pens_unhover(xfc);
881 xf_input_event(xfc, event, cookie.cc->data, cookie.cc->evtype);
886 XFreeEventData(xfc->display, cookie.vc);
892int xf_input_init(xfContext* xfc, Window window)
899int xf_input_handle_event(xfContext* xfc,
const XEvent* event)
902 const rdpSettings* settings = NULL;
905 settings = xfc->common.context.settings;
906 WINPR_ASSERT(settings);
910 return xf_input_handle_event_remote(xfc, event);
914 return xf_input_handle_event_local(xfc, event);
918 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.