2022-02-15 02:34:13 +06:00
|
|
|
/*
|
2023-01-06 15:36:05 -05:00
|
|
|
* 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.
|
2022-02-15 02:34:13 +06:00
|
|
|
*
|
2023-01-06 15:36:05 -05:00
|
|
|
* This file is part of the 86Box distribution.
|
2022-02-15 02:34:13 +06:00
|
|
|
*
|
2023-01-06 15:36:05 -05:00
|
|
|
* X11 Xinput2 mouse input module.
|
2022-02-15 02:34:13 +06:00
|
|
|
*
|
|
|
|
|
*
|
|
|
|
|
*
|
2023-01-06 15:36:05 -05:00
|
|
|
* Authors: Cacodemon345
|
2023-01-31 18:29:29 -03:00
|
|
|
* RichardG <richardg867@gmail.com>
|
2022-02-15 02:34:13 +06:00
|
|
|
*
|
2023-01-31 18:29:29 -03:00
|
|
|
* Copyright 2022 Cacodemon345.
|
|
|
|
|
* Copyright 2023 RichardG.
|
2022-02-15 02:34:13 +06:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include <QDebug>
|
|
|
|
|
#include <QThread>
|
|
|
|
|
#include <QProcess>
|
|
|
|
|
#include <QApplication>
|
|
|
|
|
#include <QAbstractNativeEventFilter>
|
|
|
|
|
|
|
|
|
|
#include "qt_mainwindow.hpp"
|
2022-11-19 08:49:04 -05:00
|
|
|
extern MainWindow *main_window;
|
2022-02-15 02:34:13 +06:00
|
|
|
|
|
|
|
|
#include <stdint.h>
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
#include <stdio.h>
|
|
|
|
|
|
|
|
|
|
#include <atomic>
|
|
|
|
|
|
2022-11-19 08:49:04 -05:00
|
|
|
extern "C" {
|
2022-02-15 02:34:13 +06:00
|
|
|
#include <X11/Xlib.h>
|
2022-02-16 00:10:30 +06:00
|
|
|
#include <X11/keysym.h>
|
2022-02-15 02:34:13 +06:00
|
|
|
#include <X11/extensions/XInput2.h>
|
|
|
|
|
#include <unistd.h>
|
|
|
|
|
#include <fcntl.h>
|
|
|
|
|
|
|
|
|
|
#include <86box/86box.h>
|
|
|
|
|
#include <86box/mouse.h>
|
|
|
|
|
#include <86box/plat.h>
|
|
|
|
|
}
|
|
|
|
|
|
2022-11-19 08:49:04 -05:00
|
|
|
static Display *disp = nullptr;
|
|
|
|
|
static QThread *procThread = nullptr;
|
|
|
|
|
static XIEventMask ximask;
|
|
|
|
|
static std::atomic<bool> exitfromthread = false;
|
2022-02-16 00:10:30 +06:00
|
|
|
static std::atomic<double> xi2_mouse_x = 0, xi2_mouse_y = 0, xi2_mouse_abs_x = 0, xi2_mouse_abs_y = 0;
|
2023-01-31 18:29:29 -03:00
|
|
|
static int xi2opcode = 0;
|
|
|
|
|
static double prev_coords[2] = { 0.0 };
|
|
|
|
|
static Time prev_time = 0;
|
2022-02-15 02:34:13 +06:00
|
|
|
|
2023-01-31 18:29:29 -03:00
|
|
|
/* Based on SDL2. */
|
2022-11-19 08:49:04 -05:00
|
|
|
static void
|
2023-01-31 18:29:29 -03:00
|
|
|
parse_valuators(const double *input_values,
|
|
|
|
|
const unsigned char *mask, int mask_len,
|
2022-11-19 08:49:04 -05:00
|
|
|
double *output_values, int output_values_len)
|
|
|
|
|
{
|
|
|
|
|
int i = 0, z = 0;
|
2022-02-15 02:34:13 +06:00
|
|
|
int top = mask_len * 8;
|
|
|
|
|
if (top > 16)
|
|
|
|
|
top = 16;
|
|
|
|
|
|
2023-01-31 18:29:29 -03:00
|
|
|
memset(output_values, 0, output_values_len * sizeof(output_values[0]));
|
2022-02-15 02:34:13 +06:00
|
|
|
for (; i < top && z < output_values_len; i++) {
|
|
|
|
|
if (XIMaskIsSet(mask, i)) {
|
2023-01-31 18:29:29 -03:00
|
|
|
output_values[z] = *input_values;
|
2022-02-15 02:34:13 +06:00
|
|
|
input_values++;
|
|
|
|
|
}
|
|
|
|
|
z++;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static bool exitthread = false;
|
|
|
|
|
|
2022-11-19 08:49:04 -05:00
|
|
|
void
|
|
|
|
|
xinput2_proc()
|
2022-02-15 02:34:13 +06:00
|
|
|
{
|
|
|
|
|
Window win;
|
|
|
|
|
win = DefaultRootWindow(disp);
|
|
|
|
|
|
2022-08-12 13:24:29 +06:00
|
|
|
ximask.deviceid = XIAllMasterDevices;
|
2022-02-15 02:34:13 +06:00
|
|
|
ximask.mask_len = XIMaskLen(XI_LASTEVENT);
|
2022-11-19 08:49:04 -05:00
|
|
|
ximask.mask = (unsigned char *) calloc(ximask.mask_len, sizeof(unsigned char));
|
2022-02-15 02:34:13 +06:00
|
|
|
|
|
|
|
|
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);
|
2023-01-31 18:29:29 -03:00
|
|
|
XISetMask(ximask.mask, XI_Motion);
|
2022-02-15 02:34:13 +06:00
|
|
|
|
|
|
|
|
XISelectEvents(disp, win, &ximask, 1);
|
|
|
|
|
|
|
|
|
|
XSync(disp, False);
|
2022-11-19 08:49:04 -05:00
|
|
|
while (true) {
|
|
|
|
|
XEvent ev;
|
|
|
|
|
XGenericEventCookie *cookie = (XGenericEventCookie *) &ev.xcookie;
|
|
|
|
|
XNextEvent(disp, (XEvent *) &ev);
|
2022-02-15 02:34:13 +06:00
|
|
|
|
2023-01-31 18:29:29 -03:00
|
|
|
if (XGetEventData(disp, cookie) && (cookie->type == GenericEvent) && (cookie->extension == xi2opcode)) {
|
|
|
|
|
const XIRawEvent *rawev = (const XIRawEvent *) cookie->data;
|
|
|
|
|
double coords[2] = { 0.0 };
|
|
|
|
|
|
2022-02-15 02:34:13 +06:00
|
|
|
switch (cookie->evtype) {
|
2023-01-31 18:29:29 -03:00
|
|
|
case XI_Motion:
|
|
|
|
|
{
|
|
|
|
|
const XIDeviceEvent *devev = (const XIDeviceEvent *) cookie->data;
|
|
|
|
|
parse_valuators(devev->valuators.values, devev->valuators.mask, devev->valuators.mask_len, coords, 2);
|
|
|
|
|
|
|
|
|
|
/* XIDeviceEvent and XIRawEvent share the XIEvent base struct, which
|
|
|
|
|
doesn't contain deviceid, but that's at the same offset on both. */
|
|
|
|
|
goto common_motion;
|
|
|
|
|
}
|
|
|
|
|
|
2022-11-19 08:49:04 -05:00
|
|
|
case XI_RawMotion:
|
|
|
|
|
{
|
2023-01-31 18:29:29 -03:00
|
|
|
parse_valuators(rawev->raw_values, rawev->valuators.mask, rawev->valuators.mask_len, coords, 2);
|
|
|
|
|
common_motion:
|
|
|
|
|
/* Ignore duplicated events. */
|
|
|
|
|
if ((rawev->time == prev_time) && (coords[0] == prev_coords[0]) && (coords[1] == prev_coords[1]))
|
|
|
|
|
break;
|
2022-11-19 08:49:04 -05:00
|
|
|
|
2023-01-31 18:29:29 -03:00
|
|
|
/* SDL2 queries the device on every event, so doing that should be fine. */
|
|
|
|
|
int i;
|
|
|
|
|
XIDeviceInfo *xidevinfo = XIQueryDevice(disp, rawev->deviceid, &i);
|
|
|
|
|
if (xidevinfo) {
|
|
|
|
|
/* Process the device's axes. */
|
|
|
|
|
int axis = 0;
|
|
|
|
|
for (i = 0; i < xidevinfo->num_classes; i++) {
|
|
|
|
|
const XIValuatorClassInfo *v = (const XIValuatorClassInfo *) xidevinfo->classes[i];
|
|
|
|
|
if (v->type == XIValuatorClass) {
|
|
|
|
|
/* Is this an absolute or relative axis? */
|
|
|
|
|
if (v->mode == XIModeRelative) {
|
|
|
|
|
/* Set relative coordinates. */
|
|
|
|
|
if (axis == 0)
|
|
|
|
|
xi2_mouse_x = xi2_mouse_x + coords[axis];
|
|
|
|
|
else
|
|
|
|
|
xi2_mouse_y = xi2_mouse_y + coords[axis];
|
|
|
|
|
} else {
|
|
|
|
|
/* Convert absolute value range to pixel granularity, then to relative coordinates. */
|
|
|
|
|
int disp_screen = XDefaultScreen(disp), abs_conv;
|
|
|
|
|
double abs_div;
|
|
|
|
|
if (axis == 0) {
|
|
|
|
|
abs_div = (v->max - v->min) / (double) XDisplayWidth(disp, disp_screen);
|
|
|
|
|
if (abs_div <= 0)
|
|
|
|
|
abs_div = 1;
|
|
|
|
|
abs_conv = (coords[axis] - v->min) / abs_div;
|
|
|
|
|
|
|
|
|
|
if (xi2_mouse_abs_x != 0)
|
|
|
|
|
xi2_mouse_x = xi2_mouse_x + (abs_conv - xi2_mouse_abs_x);
|
|
|
|
|
xi2_mouse_abs_x = abs_conv;
|
|
|
|
|
} else {
|
|
|
|
|
abs_div = (v->max - v->min) / (double) XDisplayHeight(disp, disp_screen);
|
|
|
|
|
if (abs_div <= 0)
|
|
|
|
|
abs_div = 1;
|
|
|
|
|
abs_conv = (coords[axis] - v->min) / abs_div;
|
|
|
|
|
|
|
|
|
|
if (xi2_mouse_abs_y != 0)
|
|
|
|
|
xi2_mouse_y = xi2_mouse_y + (abs_conv - xi2_mouse_abs_y);
|
|
|
|
|
xi2_mouse_abs_y = abs_conv;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
prev_coords[axis] = coords[axis];
|
|
|
|
|
if (++axis >= 2) /* stop after X and Y processed */
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-11-19 08:49:04 -05:00
|
|
|
}
|
2023-01-31 18:29:29 -03:00
|
|
|
|
|
|
|
|
prev_time = rawev->time;
|
2022-11-19 08:49:04 -05:00
|
|
|
if (!mouse_capture)
|
|
|
|
|
break;
|
|
|
|
|
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);
|
|
|
|
|
}
|
2023-01-31 18:29:29 -03:00
|
|
|
|
|
|
|
|
break;
|
2022-02-16 00:10:30 +06:00
|
|
|
}
|
2022-02-15 02:34:13 +06:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
XFreeEventData(disp, cookie);
|
2022-11-19 08:49:04 -05:00
|
|
|
if (exitthread)
|
|
|
|
|
break;
|
2022-02-15 02:34:13 +06:00
|
|
|
}
|
|
|
|
|
XCloseDisplay(disp);
|
|
|
|
|
}
|
|
|
|
|
|
2022-11-19 08:49:04 -05:00
|
|
|
void
|
|
|
|
|
xinput2_exit()
|
2022-02-15 02:34:13 +06:00
|
|
|
{
|
2022-11-19 08:49:04 -05:00
|
|
|
if (!exitthread) {
|
2022-02-17 11:45:42 +06:00
|
|
|
exitthread = true;
|
|
|
|
|
procThread->wait(5000);
|
|
|
|
|
procThread->terminate();
|
|
|
|
|
}
|
2022-02-15 02:34:13 +06:00
|
|
|
}
|
|
|
|
|
|
2022-11-19 08:49:04 -05:00
|
|
|
void
|
|
|
|
|
xinput2_init()
|
2022-02-15 02:34:13 +06:00
|
|
|
{
|
|
|
|
|
disp = XOpenDisplay(nullptr);
|
2022-11-19 08:49:04 -05:00
|
|
|
if (!disp) {
|
2022-02-15 02:34:13 +06:00
|
|
|
qWarning() << "Cannot open current X11 display";
|
|
|
|
|
return;
|
|
|
|
|
}
|
2022-08-12 13:24:29 +06:00
|
|
|
auto event = 0, err = 0, minor = 1, major = 2;
|
2022-11-19 08:49:04 -05:00
|
|
|
if (XQueryExtension(disp, "XInputExtension", &xi2opcode, &event, &err)) {
|
|
|
|
|
if (XIQueryVersion(disp, &major, &minor) == Success) {
|
2022-02-15 02:34:13 +06:00
|
|
|
procThread = QThread::create(xinput2_proc);
|
|
|
|
|
procThread->start();
|
|
|
|
|
atexit(xinput2_exit);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-11-19 08:49:04 -05:00
|
|
|
void
|
|
|
|
|
xinput2_poll()
|
2022-02-15 02:34:13 +06:00
|
|
|
{
|
2022-11-19 08:49:04 -05:00
|
|
|
if (procThread && mouse_capture) {
|
2022-02-15 02:34:13 +06:00
|
|
|
mouse_x = xi2_mouse_x;
|
|
|
|
|
mouse_y = xi2_mouse_y;
|
|
|
|
|
}
|
|
|
|
|
xi2_mouse_x = 0;
|
|
|
|
|
xi2_mouse_y = 0;
|
|
|
|
|
}
|