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;
324static void xf_input_detect_pan(xfContext* xfc)
327 rdpContext* ctx = &xfc->common.context;
330 if (xfc->active_contacts != 2)
335 const double dx[] = { xfc->contacts[0].pos_x - xfc->contacts[0].last_x,
336 xfc->contacts[1].pos_x - xfc->contacts[1].last_x };
337 const double dy[] = { xfc->contacts[0].pos_y - xfc->contacts[0].last_y,
338 xfc->contacts[1].pos_y - xfc->contacts[1].last_y };
339 const double px = fabs(dx[0]) < fabs(dx[1]) ? dx[0] : dx[1];
340 const double py = fabs(dy[0]) < fabs(dy[1]) ? dy[0] : dy[1];
341 xfc->px_vector += px;
342 xfc->py_vector += py;
343 const double dist_x = fabs(xfc->contacts[0].pos_x - xfc->contacts[1].pos_x);
344 const double dist_y = fabs(xfc->contacts[0].pos_y - xfc->contacts[1].pos_y);
346 if (dist_y > MIN_FINGER_DIST)
348 if (xfc->px_vector > PAN_THRESHOLD)
351 PanningChangeEventArgs e;
352 EventArgsInit(&e,
"xfreerdp");
355 PubSub_OnPanningChange(ctx->pubSub, xfc, &e);
361 else if (xfc->px_vector < -PAN_THRESHOLD)
364 PanningChangeEventArgs e;
365 EventArgsInit(&e,
"xfreerdp");
368 PubSub_OnPanningChange(ctx->pubSub, xfc, &e);
376 if (dist_x > MIN_FINGER_DIST)
378 if (xfc->py_vector > PAN_THRESHOLD)
381 PanningChangeEventArgs e;
382 EventArgsInit(&e,
"xfreerdp");
385 PubSub_OnPanningChange(ctx->pubSub, xfc, &e);
391 else if (xfc->py_vector < -PAN_THRESHOLD)
394 PanningChangeEventArgs e;
395 EventArgsInit(&e,
"xfreerdp");
398 PubSub_OnPanningChange(ctx->pubSub, xfc, &e);
407static void xf_input_detect_pinch(xfContext* xfc)
409 ZoomingChangeEventArgs e = WINPR_C_ARRAY_INIT;
412 rdpContext* ctx = &xfc->common.context;
415 if (xfc->active_contacts != 2)
417 xfc->firstDist = -1.0;
422 const double dist = sqrt(pow(xfc->contacts[1].pos_x - xfc->contacts[0].last_x, 2.0) +
423 pow(xfc->contacts[1].pos_y - xfc->contacts[0].last_y, 2.0));
426 if (xfc->firstDist <= 0)
428 xfc->firstDist = dist;
429 xfc->lastDist = xfc->firstDist;
436 double delta = xfc->lastDist - dist;
445 xfc->z_vector += delta;
446 xfc->lastDist = dist;
448 if (xfc->z_vector > ZOOM_THRESHOLD)
450 EventArgsInit(&e,
"xfreerdp");
452 PubSub_OnZoomingChange(ctx->pubSub, xfc, &e);
458 if (xfc->z_vector < -ZOOM_THRESHOLD)
460 EventArgsInit(&e,
"xfreerdp");
462 PubSub_OnZoomingChange(ctx->pubSub, xfc, &e);
470static void xf_input_touch_begin(xfContext* xfc,
const XIDeviceEvent* event)
473 for (
int i = 0; i < MAX_CONTACTS; i++)
475 if (xfc->contacts[i].id == 0)
477 xfc->contacts[i].id =
event->detail;
478 xfc->contacts[i].count = 1;
479 xfc->contacts[i].pos_x =
event->event_x;
480 xfc->contacts[i].pos_y =
event->event_y;
481 xfc->active_contacts++;
487static void xf_input_touch_update(xfContext* xfc,
const XIDeviceEvent* event)
492 for (
int i = 0; i < MAX_CONTACTS; i++)
494 if (xfc->contacts[i].id == event->detail)
496 xfc->contacts[i].count++;
497 xfc->contacts[i].last_x = xfc->contacts[i].pos_x;
498 xfc->contacts[i].last_y = xfc->contacts[i].pos_y;
499 xfc->contacts[i].pos_x =
event->event_x;
500 xfc->contacts[i].pos_y =
event->event_y;
501 xf_input_detect_pinch(xfc);
502 xf_input_detect_pan(xfc);
508static void xf_input_touch_end(xfContext* xfc,
const XIDeviceEvent* event)
511 for (
int i = 0; i < MAX_CONTACTS; i++)
513 if (xfc->contacts[i].id == event->detail)
515 xfc->contacts[i].id = 0;
516 xfc->contacts[i].count = 0;
517 xfc->active_contacts--;
523static int xf_input_handle_event_local(xfContext* xfc,
const XEvent* event)
527 const XGenericEventCookie* cc;
528 XGenericEventCookie* vc;
530 cookie.cc = &
event->xcookie;
531 XGetEventData(xfc->display, cookie.vc);
533 if ((cookie.cc->type == GenericEvent) && (cookie.cc->extension == xfc->XInputOpcode))
535 switch (cookie.cc->evtype)
538 if (xf_input_is_duplicate(xfc, cookie.cc) == FALSE)
539 xf_input_touch_begin(xfc, cookie.cc->data);
541 xf_input_save_last_event(xfc, cookie.cc);
545 if (xf_input_is_duplicate(xfc, cookie.cc) == FALSE)
546 xf_input_touch_update(xfc, cookie.cc->data);
548 xf_input_save_last_event(xfc, cookie.cc);
552 if (xf_input_is_duplicate(xfc, cookie.cc) == FALSE)
553 xf_input_touch_end(xfc, cookie.cc->data);
555 xf_input_save_last_event(xfc, cookie.cc);
559 xf_input_event(xfc, event, cookie.cc->data, cookie.cc->evtype);
564 XFreeEventData(xfc->display, cookie.vc);
568static void xf_input_hide_cursor(xfContext* xfc)
572 if (!xfc->cursorHidden)
574 XcursorImage ci = WINPR_C_ARRAY_INIT;
576 static Cursor nullcursor = None;
578 ci.version = XCURSOR_IMAGE_VERSION;
579 ci.size =
sizeof(ci);
580 ci.width = ci.height = 1;
581 ci.xhot = ci.yhot = 0;
583 nullcursor = XcursorImageLoadCursor(xfc->display, &ci);
585 if ((xfc->window) && (nullcursor != None))
586 XDefineCursor(xfc->display, xfc->window->handle, nullcursor);
588 xfc->cursorHidden = TRUE;
595static void xf_input_show_cursor(xfContext* xfc)
600 if (xfc->cursorHidden)
605 XUndefineCursor(xfc->display, xfc->window->handle);
607 XDefineCursor(xfc->display, xfc->window->handle, xfc->pointer->cursor);
610 xfc->cursorHidden = FALSE;
617static int xf_input_touch_remote(xfContext* xfc, XIDeviceEvent* event,
int evtype)
622 RdpeiClientContext* rdpei = xfc->common.rdpei;
627 xf_input_hide_cursor(xfc);
628 touchId =
event->detail;
629 x = (int)event->event_x;
630 y = (int)event->event_y;
631 xf_event_adjust_coordinates(xfc, &x, &y);
636 freerdp_client_handle_touch(&xfc->common, FREERDP_TOUCH_DOWN, touchId, 0, x, y);
639 freerdp_client_handle_touch(&xfc->common, FREERDP_TOUCH_MOTION, touchId, 0, x, y);
642 freerdp_client_handle_touch(&xfc->common, FREERDP_TOUCH_UP, touchId, 0, x, y);
651static BOOL xf_input_pen_remote(xfContext* xfc, XIDeviceEvent* event,
int evtype,
int deviceid)
655 RdpeiClientContext* rdpei = xfc->common.rdpei;
660 xf_input_hide_cursor(xfc);
661 x = (int)event->event_x;
662 y = (int)event->event_y;
663 xf_event_adjust_coordinates(xfc, &x, &y);
665 double pressure = 0.0;
666 double* val =
event->valuators.values;
667 for (
int i = 0; i < MIN(event->valuators.mask_len * 8, 3); i++)
669 if (XIMaskIsSet(event->valuators.mask, i))
671 double value = *val++;
677 UINT32 flags = FREERDP_PEN_HAS_PRESSURE;
678 if ((evtype == XI_ButtonPress) || (evtype == XI_ButtonRelease))
680 WLog_DBG(TAG,
"pen button %d", event->detail);
681 switch (event->detail)
686 flags |= FREERDP_PEN_BARREL_PRESSED;
696 flags |= FREERDP_PEN_PRESS;
697 if (!freerdp_client_handle_pen(&xfc->common, flags, deviceid, x, y, pressure))
701 flags |= FREERDP_PEN_MOTION;
702 if (!freerdp_client_handle_pen(&xfc->common, flags, deviceid, x, y, pressure))
705 case XI_ButtonRelease:
706 flags |= FREERDP_PEN_RELEASE;
707 if (!freerdp_client_handle_pen(&xfc->common, flags, deviceid, x, y, pressure))
716static int xf_input_pens_unhover(xfContext* xfc)
720 freerdp_client_pen_cancel_all(&xfc->common);
724bool xf_use_rel_mouse(xfContext* xfc)
726 if (!freerdp_client_use_relative_mouse_events(&xfc->common))
728 if (!xfc->isCursorHidden)
733int xf_input_event(xfContext* xfc, WINPR_ATTR_UNUSED
const XEvent* xevent, XIDeviceEvent* event,
737 WINPR_ASSERT(xevent);
740 xfWindow* window = xfc->window;
743 if (xf_floatbar_is_locked(window->floatbar))
747 xf_input_show_cursor(xfc);
752 case XI_ButtonRelease:
753 xfc->xi_event = !xfc->common.mouse_grabbed || !xf_use_rel_mouse(xfc);
757 if (!xfc_is_floatbar_window(xfc, event->event) || (evtype != XI_ButtonPress))
759 xf_generic_ButtonEvent(xfc, (
int)event->event_x, (
int)event->event_y,
760 event->detail, event->event, xfc->remote_app,
761 evtype == XI_ButtonPress);
767 xfc->xi_event = !xfc->common.mouse_grabbed || !xf_use_rel_mouse(xfc);
771 xf_generic_MotionNotify(xfc, (
int)event->event_x, (
int)event->event_y, event->event,
775 case XI_RawButtonPress:
776 case XI_RawButtonRelease:
777 xfc->xi_rawevent = xfc->common.mouse_grabbed && xf_use_rel_mouse(xfc);
779 if (xfc->xi_rawevent)
781 const XIRawEvent* ev = (
const XIRawEvent*)event;
782 xf_generic_RawButtonEvent(xfc, ev->detail, xfc->remote_app,
783 evtype == XI_RawButtonPress);
787 xfc->xi_rawevent = xfc->common.mouse_grabbed && xf_use_rel_mouse(xfc);
789 if (xfc->xi_rawevent)
791 const XIRawEvent* ev = (
const XIRawEvent*)event;
794 if (XIMaskIsSet(ev->valuators.mask, 0))
795 x = ev->raw_values[0];
796 if (XIMaskIsSet(ev->valuators.mask, 1))
797 y = ev->raw_values[1];
799 xf_generic_RawMotionNotify(xfc, (
int)x, (
int)y, event->event, xfc->remote_app);
802 case XI_DeviceChanged:
804 const XIDeviceChangedEvent* ev = (
const XIDeviceChangedEvent*)event;
805 if (ev->reason != XIDeviceChange)
817 register_input_events(xfc, xfc->window->handle);
820 case XI_HierarchyChanged:
822 register_input_events(xfc, xfc->window->handle);
826 WLog_WARN(TAG,
"Unhandled event %d: Event was registered but is not handled!", evtype);
833static int xf_input_handle_event_remote(xfContext* xfc,
const XEvent* event)
837 const XGenericEventCookie* cc;
838 XGenericEventCookie* vc;
840 cookie.cc = &
event->xcookie;
841 XGetEventData(xfc->display, cookie.vc);
843 if ((cookie.cc->type == GenericEvent) && (cookie.cc->extension == xfc->XInputOpcode))
845 switch (cookie.cc->evtype)
848 xf_input_pens_unhover(xfc);
853 xf_input_touch_remote(xfc, cookie.cc->data, cookie.cc->evtype);
857 case XI_ButtonRelease:
859 WLog_DBG(TAG,
"checking for pen");
860 XIDeviceEvent* deviceEvent = (XIDeviceEvent*)cookie.cc->data;
861 int deviceid = deviceEvent->deviceid;
863 if (freerdp_client_is_pen(&xfc->common, deviceid))
865 if (!xf_input_pen_remote(xfc, cookie.cc->data, cookie.cc->evtype, deviceid))
868 xf_input_event(xfc, event, cookie.cc->data, cookie.cc->evtype);
876 xf_input_pens_unhover(xfc);
877 xf_input_event(xfc, event, cookie.cc->data, cookie.cc->evtype);
882 XFreeEventData(xfc->display, cookie.vc);
888int xf_input_init(xfContext* xfc, Window window)
895int xf_input_handle_event(xfContext* xfc,
const XEvent* event)
898 const rdpSettings* settings =
nullptr;
901 settings = xfc->common.context.settings;
902 WINPR_ASSERT(settings);
906 return xf_input_handle_event_remote(xfc, event);
910 return xf_input_handle_event_local(xfc, event);
914 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.