From 0d31bb0cc613eb2c5f97084ab9d0f2554999d07a Mon Sep 17 00:00:00 2001 From: Cacodemon345 Date: Tue, 15 Feb 2022 02:34:13 +0600 Subject: [PATCH] qt: X11 Xinput2 mouse motion backend --- src/qt/CMakeLists.txt | 3 +- src/qt/qt_main.cpp | 1 + src/qt/qt_mainwindow.cpp | 2 - src/qt/qt_mainwindow.hpp | 1 + src/qt/qt_rendererstack.cpp | 30 +++++-- src/qt/xinput2_mouse.cpp | 174 ++++++++++++++++++++++++++++++++++++ 6 files changed, 201 insertions(+), 10 deletions(-) create mode 100644 src/qt/xinput2_mouse.cpp diff --git a/src/qt/CMakeLists.txt b/src/qt/CMakeLists.txt index e246ce920..428a60dbe 100644 --- a/src/qt/CMakeLists.txt +++ b/src/qt/CMakeLists.txt @@ -260,7 +260,8 @@ endif() if (UNIX AND NOT APPLE) find_package(X11 REQUIRED) - target_link_libraries(ui PRIVATE X11::X11) + target_link_libraries(ui PRIVATE X11::X11 X11::Xi) + target_sources(ui PRIVATE xinput2_mouse.cpp) find_package(PkgConfig REQUIRED) pkg_check_modules(LIBEVDEV IMPORTED_TARGET libevdev) if (LIBEVDEV_FOUND) diff --git a/src/qt/qt_main.cpp b/src/qt/qt_main.cpp index d9d667c9e..cae6c6e5c 100644 --- a/src/qt/qt_main.cpp +++ b/src/qt/qt_main.cpp @@ -66,6 +66,7 @@ extern "C" #include "cocoa_mouse.hpp" #include "qt_styleoverride.hpp" + // Void Cast #define VC(x) const_cast(x) diff --git a/src/qt/qt_mainwindow.cpp b/src/qt/qt_mainwindow.cpp index 80a3792c3..05caf67e1 100644 --- a/src/qt/qt_mainwindow.cpp +++ b/src/qt/qt_mainwindow.cpp @@ -168,7 +168,6 @@ MainWindow::MainWindow(QWidget *parent) : mouse_capture = state ? 1 : 0; qt_mouse_capture(mouse_capture); if (mouse_capture) { - ui->stackedWidget->grabMouse(); this->grabKeyboard(); #ifdef WAYLAND if (QGuiApplication::platformName().contains("wayland")) { @@ -176,7 +175,6 @@ MainWindow::MainWindow(QWidget *parent) : } #endif } else { - ui->stackedWidget->releaseMouse(); this->releaseKeyboard(); #ifdef WAYLAND if (QGuiApplication::platformName().contains("wayland")) { diff --git a/src/qt/qt_mainwindow.hpp b/src/qt/qt_mainwindow.hpp index 2d74af987..3d5a6a02a 100644 --- a/src/qt/qt_mainwindow.hpp +++ b/src/qt/qt_mainwindow.hpp @@ -131,6 +131,7 @@ private: friend class SpecifyDimensions; friend class ProgSettings; + friend class RendererCommon; }; #endif // QT_MAINWINDOW_HPP diff --git a/src/qt/qt_rendererstack.cpp b/src/qt/qt_rendererstack.cpp index 16987099f..541bb4cf6 100644 --- a/src/qt/qt_rendererstack.cpp +++ b/src/qt/qt_rendererstack.cpp @@ -49,15 +49,21 @@ RendererStack::RendererStack(QWidget *parent) : { ui->setupUi(this); +#ifdef __unix__ #ifdef WAYLAND if (QApplication::platformName().contains("wayland")) { wl_init(); } #endif #ifdef EVDEV_INPUT - if (QApplication::platformName() == "xcb" || QApplication::platformName() == "eglfs") { + if (QApplication::platformName() == "eglfs") { evdev_init(); } +#endif + if (QApplication::platformName() == "xcb") { + extern void xinput2_init(); + xinput2_init(); + } #endif } @@ -91,20 +97,31 @@ void RendererStack::mousePoll() { #ifdef __APPLE__ return macos_poll_mouse(); -#else +#else /* !defined __APPLE__ */ mouse_x = mousedata.deltax; mouse_y = mousedata.deltay; mouse_z = mousedata.deltaz; mousedata.deltax = mousedata.deltay = mousedata.deltaz = 0; mouse_buttons = mousedata.mousebuttons; + +#ifdef __unix__ #ifdef WAYLAND if (QApplication::platformName().contains("wayland")) wl_mouse_poll(); #endif + #ifdef EVDEV_INPUT - evdev_mouse_poll(); -#endif + if (QApplication::platformName() == "eglfs") evdev_mouse_poll(); + else #endif + if (QApplication::platformName() == "xcb") + { + extern void xinput2_poll(); + xinput2_poll(); + } +#endif /* defined __unix__ */ +#endif /* !defined __APPLE__ */ + } int ignoreNextMouseEvent = 1; @@ -169,8 +186,8 @@ void RendererStack::mouseMoveEvent(QMouseEvent *event) leaveEvent((QEvent*)event); ignoreNextMouseEvent--; } - else if (event->globalPos().x() == 0 || event->globalPos().y() == 0) leaveEvent((QEvent*)event); - else if (event->globalPos().x() == (util::screenOfWidget(this)->geometry().width() - 1) || event->globalPos().y() == (util::screenOfWidget(this)->geometry().height() - 1)) leaveEvent((QEvent*)event); + QCursor::setPos(mapToGlobal(QPoint(width() / 2, height() / 2))); + ignoreNextMouseEvent = 2; oldPos = event->pos(); #endif } @@ -183,7 +200,6 @@ void RendererStack::leaveEvent(QEvent* event) return; } if (!mouse_capture) return; - QCursor::setPos(mapToGlobal(QPoint(width() / 2, height() / 2))); ignoreNextMouseEvent = 2; event->accept(); } diff --git a/src/qt/xinput2_mouse.cpp b/src/qt/xinput2_mouse.cpp new file mode 100644 index 000000000..361917bfc --- /dev/null +++ b/src/qt/xinput2_mouse.cpp @@ -0,0 +1,174 @@ +/* + * 86Box A hypervisor and IBM PC system emulator that specializes in + * running old operating systems and software designed for IBM + * PC systems and compatibles from 1981 through fairly recent + * system designs based on the PCI bus. + * + * This file is part of the 86Box distribution. + * + * X11 Xinput2 mouse input module. + * + * + * + * Authors: Cacodemon345 + * + * Copyright 2022 Cacodemon345 + */ + +/* Valuator parsing and duplicate event checking code from SDL2. */ +#include +#include +#include +#include +#include + +#include "qt_mainwindow.hpp" +extern MainWindow* main_window; + +#include +#include +#include + +#include + +extern "C" +{ +#include +#include +#include +#include + +#include <86box/86box.h> +#include <86box/mouse.h> +#include <86box/plat.h> +} + +int xi2flides[2] = { 0, 0 }; + +static Display* disp = nullptr; +static QThread* procThread = nullptr; +static bool xi2childinit = false; +static XIEventMask ximask; +static std::atomic exitfromthread = false; +static std::atomic xi2_mouse_x = 0, xi2_mouse_y = 0; +static int xi2opcode = 0; +static double prev_rel_coords[2] = { 0., 0. }; +static Time prev_time = 0; + +// From SDL2. +static void parse_valuators(const double *input_values, const unsigned char *mask,int mask_len, + double *output_values,int output_values_len) { + int i = 0,z = 0; + int top = mask_len * 8; + if (top > 16) + top = 16; + + memset(output_values,0,output_values_len * sizeof(double)); + for (; i < top && z < output_values_len; i++) { + if (XIMaskIsSet(mask, i)) { + const int value = (int) *input_values; + output_values[z] = value; + input_values++; + } + z++; + } +} + +static bool exitthread = false; + +void xinput2_proc() +{ + Window win; + win = DefaultRootWindow(disp); + + // XIAllMasterDevices doesn't work for click-and-drag operations. + ximask.deviceid = XIAllDevices; + ximask.mask_len = XIMaskLen(XI_LASTEVENT); + ximask.mask = (unsigned char*)calloc(ximask.mask_len, sizeof(unsigned char)); + + XISetMask(ximask.mask, XI_RawKeyPress); + XISetMask(ximask.mask, XI_RawKeyRelease); + XISetMask(ximask.mask, XI_RawButtonPress); + XISetMask(ximask.mask, XI_RawButtonRelease); + XISetMask(ximask.mask, XI_RawMotion); + + XISelectEvents(disp, win, &ximask, 1); + + XSync(disp, False); + while(true) + { + XEvent ev; + XGenericEventCookie *cookie = (XGenericEventCookie*)&ev.xcookie; + XNextEvent(disp, (XEvent*)&ev); + + if (XGetEventData(disp, cookie) && cookie->type == GenericEvent && cookie->extension == xi2opcode) { + switch (cookie->evtype) { + case XI_RawMotion: { + static int ss = 0; + const XIRawEvent *rawev = (const XIRawEvent*)cookie->data; + double relative_coords[2] = { 0., 0. }; + parse_valuators(rawev->raw_values,rawev->valuators.mask, + rawev->valuators.mask_len,relative_coords,2); + + if ((rawev->time == prev_time) && (relative_coords[0] == prev_rel_coords[0]) && (relative_coords[1] == prev_rel_coords[1])) { + break; // Ignore duplicated events. + } + xi2_mouse_x = xi2_mouse_x + relative_coords[0]; + xi2_mouse_y = xi2_mouse_y + relative_coords[1]; + prev_rel_coords[0] = relative_coords[0]; + prev_rel_coords[1] = relative_coords[1]; + prev_time = rawev->time; + } + } + } + + XFreeEventData(disp, cookie); + if (exitthread) break; + } + XCloseDisplay(disp); +} + +void xinput2_exit() +{ + exitthread = true; + procThread->wait(5000); + procThread->terminate(); +} + +void xinput2_init() +{ + disp = XOpenDisplay(nullptr); + if (!disp) + { + qWarning() << "Cannot open current X11 display"; + return; + } + auto event = 0, err = 0, minor = 0, major = 2; + if (XQueryExtension(disp, "XInputExtension", &xi2opcode, &event, &err)) + { + if (XIQueryVersion(disp, &major, &minor) == Success) + { + procThread = QThread::create(xinput2_proc); + procThread->start(); + atexit(xinput2_exit); + } + } +} + +void xinput2_poll() +{ + if (procThread && mouse_capture) + { + mouse_x = xi2_mouse_x; + mouse_y = xi2_mouse_y; + XWindowAttributes winattrib{}; + if (XGetWindowAttributes(disp, main_window->winId(), &winattrib)) + { + auto globalPoint = main_window->mapToGlobal(QPoint(main_window->width() / 2, main_window->height() / 2)); + XWarpPointer(disp, XRootWindow(disp, XScreenNumberOfScreen(winattrib.screen)), XRootWindow(disp, XScreenNumberOfScreen(winattrib.screen)), 0, 0, 0, 0, globalPoint.x(), globalPoint.y()); + XFlush(disp); + } + } + xi2_mouse_x = 0; + xi2_mouse_y = 0; +}