Skip to content

Commit

Permalink
Merge pull request #11762 from jbosboom/xinput2-raw-event-query
Browse files Browse the repository at this point in the history
Xinput2: use raw events and queries
  • Loading branch information
AdmiralCurtiss committed Jun 6, 2023
2 parents 2d56daf + 620955d commit 38b033a
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 98 deletions.
157 changes: 63 additions & 94 deletions Source/Core/InputCommon/ControllerInterface/Xlib/XInput2.cpp
Expand Up @@ -5,12 +5,14 @@

#include <X11/XKBlib.h>

#include <X11/extensions/XInput2.h>
#include <cmath>
#include <cstdlib>
#include <cstring>

#include <fmt/format.h>

#include "Common/Logging/Log.h"
#include "Common/StringUtil.h"

#include "Core/Host.h"
Expand Down Expand Up @@ -54,6 +56,14 @@
// more cleanly separate each scroll wheel click, but risks dropping some inputs
#define SCROLL_AXIS_DECAY 1.1f

namespace
{
// We need XInput 2.1 to get raw events on the root window even while another
// client has a grab. If we request 2.2 or later, the server will not generate
// emulated button presses from touch events, so we want exactly 2.1.
constexpr int XINPUT_MAJOR = 2, XINPUT_MINOR = 1;
} // namespace

namespace ciface::XInput2
{
// This function will add zero or more KeyboardMouse objects to devices.
Expand All @@ -67,13 +77,18 @@ void PopulateDevices(void* const hwnd)

// verify that the XInput extension is available
if (!XQueryExtension(dpy, "XInputExtension", &xi_opcode, &event, &error))
{
WARN_LOG_FMT(CONTROLLERINTERFACE, "XInput extension not available (XQueryExtension)");
return;
}

// verify that the XInput extension is at at least version 2.0
int major = 2, minor = 0;

if (XIQueryVersion(dpy, &major, &minor) != Success)
int major = XINPUT_MAJOR, minor = XINPUT_MINOR;
if (XIQueryVersion(dpy, &major, &minor) != Success || major < XINPUT_MAJOR ||
(major == XINPUT_MAJOR && minor < XINPUT_MINOR))
{
WARN_LOG_FMT(CONTROLLERINTERFACE, "XInput extension not available (XIQueryVersion)");
return;
}

// register all master devices with Dolphin

Expand Down Expand Up @@ -115,39 +130,6 @@ void PopulateDevices(void* const hwnd)
XIFreeDeviceInfo(all_masters);
}

// Apply the event mask to the device and all its slaves. Only used in the
// constructor. Remember, each KeyboardMouse has its own copy of the event
// stream, which is how multiple event masks can "coexist."
void KeyboardMouse::SelectEventsForDevice(XIEventMask* mask, int deviceid)
{
// Set the event mask for the master device.
mask->deviceid = deviceid;
XISelectEvents(m_display, DefaultRootWindow(m_display), mask, 1);

// Query all the master device's slaves and set the same event mask for
// those too. There are two reasons we want to do this. For mouse devices,
// we want the raw motion events, and only slaves (i.e. physical hardware
// devices) emit those. For keyboard devices, selecting slaves avoids
// dealing with key focus.

int num_slaves;
XIDeviceInfo* const all_slaves = XIQueryDevice(m_display, XIAllDevices, &num_slaves);

for (int i = 0; i < num_slaves; i++)
{
XIDeviceInfo* const slave = &all_slaves[i];
if ((slave->use != XISlavePointer && slave->use != XISlaveKeyboard) ||
slave->attachment != deviceid)
{
continue;
}
mask->deviceid = slave->deviceid;
XISelectEvents(m_display, DefaultRootWindow(m_display), mask, 1);
}

XIFreeDeviceInfo(all_slaves);
}

KeyboardMouse::KeyboardMouse(Window window, int opcode, int pointer, int keyboard,
double scroll_increment_)
: m_window(window), xi_opcode(opcode), pointer_deviceid(pointer), keyboard_deviceid(keyboard),
Expand All @@ -160,6 +142,9 @@ KeyboardMouse::KeyboardMouse(Window window, int opcode, int pointer, int keyboar
// "context."
m_display = XOpenDisplay(nullptr);

int major = XINPUT_MAJOR, minor = XINPUT_MINOR;
XIQueryVersion(m_display, &major, &minor);

// should always be 1
int unused;
XIDeviceInfo* const pointer_device = XIQueryDevice(m_display, pointer_deviceid, &unused);
Expand All @@ -172,28 +157,28 @@ KeyboardMouse::KeyboardMouse(Window window, int opcode, int pointer, int keyboar

{
unsigned char mask_buf[(XI_LASTEVENT + 7) / 8] = {};
XISetMask(mask_buf, XI_ButtonPress);
XISetMask(mask_buf, XI_ButtonRelease);
XISetMask(mask_buf, XI_RawButtonPress);
XISetMask(mask_buf, XI_RawButtonRelease);
XISetMask(mask_buf, XI_RawMotion);

XIEventMask mask;
mask.mask = mask_buf;
mask.mask_len = sizeof(mask_buf);

SelectEventsForDevice(&mask, pointer_deviceid);
mask.deviceid = pointer_deviceid;
XISelectEvents(m_display, DefaultRootWindow(m_display), &mask, 1);
}

{
unsigned char mask_buf[(XI_LASTEVENT + 7) / 8] = {};
XISetMask(mask_buf, XI_KeyPress);
XISetMask(mask_buf, XI_KeyRelease);
XISetMask(mask_buf, XI_FocusOut);
XISetMask(mask_buf, XI_RawKeyPress);
XISetMask(mask_buf, XI_RawKeyRelease);

XIEventMask mask;
mask.mask = mask_buf;
mask.mask_len = sizeof(mask_buf);

SelectEventsForDevice(&mask, keyboard_deviceid);
mask.deviceid = keyboard_deviceid;
XISelectEvents(m_display, DefaultRootWindow(m_display), &mask, 1);
}

// Keyboard Keys
Expand Down Expand Up @@ -254,29 +239,35 @@ void KeyboardMouse::UpdateCursor(bool should_center_mouse)
const auto win_width = std::max(win_attribs.width, 1);
const auto win_height = std::max(win_attribs.height, 1);

if (should_center_mouse)
{
win_x = win_width / 2;
win_y = win_height / 2;

XIWarpPointer(m_display, pointer_deviceid, None, m_window, 0.0, 0.0, 0, 0, win_x, win_y);

g_controller_interface.SetMouseCenteringRequested(false);
}
else
{
// unused-- we're not interested in button presses here, as those are
// updated using events
XIButtonState button_state;
XIModifierState mods;
XIGroupState group;

// Get the absolute position of the mouse pointer and the button state.
XIQueryPointer(m_display, pointer_deviceid, m_window, &root, &child, &root_x, &root_y, &win_x,
&win_y, &button_state, &mods, &group);

// X buttons are 1-indexed, so to get 32 button bits we need a larger type
// for the shift.
u64 buttons_zero_indexed = 0;
std::memcpy(&buttons_zero_indexed, button_state.mask,
std::min<size_t>(button_state.mask_len, sizeof(m_state.buttons)));
m_state.buttons = buttons_zero_indexed >> 1;

free(button_state.mask);
}

if (should_center_mouse)
{
win_x = win_width / 2;
win_y = win_height / 2;

XIWarpPointer(m_display, pointer_deviceid, None, m_window, 0.0, 0.0, 0, 0, win_x, win_y);

g_controller_interface.SetMouseCenteringRequested(false);
}

const auto window_scale = g_controller_interface.GetWindowInputScale();

// the mouse position as a range from -1 to 1
Expand All @@ -291,10 +282,10 @@ void KeyboardMouse::UpdateInput()
// for the axis controls
float delta_x = 0.0f, delta_y = 0.0f, delta_z = 0.0f;
double delta_delta;
bool mouse_moved = false;
bool update_mouse = false, update_keyboard = false;

// Iterate through the event queue - update the axis controls, mouse
// button controls, and keyboard controls.
// Iterate through the event queue, processing raw pointer motion events and
// noting whether the button or key state has changed.
XEvent event;
while (XPending(m_display))
{
Expand All @@ -307,28 +298,21 @@ void KeyboardMouse::UpdateInput()
if (!XGetEventData(m_display, &event.xcookie))
continue;

// only one of these will get used
XIDeviceEvent* dev_event = (XIDeviceEvent*)event.xcookie.data;
XIRawEvent* raw_event = (XIRawEvent*)event.xcookie.data;

switch (event.xcookie.evtype)
{
case XI_ButtonPress:
m_state.buttons |= 1 << (dev_event->detail - 1);
break;
case XI_ButtonRelease:
m_state.buttons &= ~(1 << (dev_event->detail - 1));
case XI_RawButtonPress:
case XI_RawButtonRelease:
update_mouse = true;
break;
case XI_KeyPress:
m_state.keyboard[dev_event->detail / 8] |= 1 << (dev_event->detail % 8);
break;
case XI_KeyRelease:
m_state.keyboard[dev_event->detail / 8] &= ~(1 << (dev_event->detail % 8));
case XI_RawKeyPress:
case XI_RawKeyRelease:
update_keyboard = true;
break;
case XI_RawMotion:
{
mouse_moved = true;
update_mouse = true;

XIRawEvent* raw_event = (XIRawEvent*)event.xcookie.data;
float values[4] = {};
size_t value_idx = 0;

Expand Down Expand Up @@ -359,10 +343,6 @@ void KeyboardMouse::UpdateInput()

break;
}
case XI_FocusOut:
// Clear keyboard state on FocusOut as we will not be receiving KeyRelease events.
m_state.keyboard.fill(0);
break;
}

XFreeEventData(m_display, &event.xcookie);
Expand All @@ -382,23 +362,13 @@ void KeyboardMouse::UpdateInput()
m_state.axis.z += delta_z;
m_state.axis.z /= SCROLL_AXIS_DECAY;

// Get the absolute position of the mouse pointer
const bool should_center_mouse =
g_controller_interface.IsMouseCenteringRequested() && Host_RendererHasFocus();
if (mouse_moved || should_center_mouse)
if (update_mouse || should_center_mouse)
UpdateCursor(should_center_mouse);

// KeyRelease and FocusOut events are sometimes not received.
// Cycling Alt-Tab and landing on the same window results in a stuck "Alt" key.
// Unpressed keys are released here.
// Because we called XISetClientPointer in the constructor, XQueryKeymap
// will return the state of the associated keyboard, even if it isn't the
// first master keyboard. (XInput2 doesn't provide a function to query
// keyboard state.)
std::array<char, 32> keyboard;
XQueryKeymap(m_display, keyboard.data());
for (size_t i = 0; i != keyboard.size(); ++i)
m_state.keyboard[i] &= keyboard[i];
if (update_keyboard)
XQueryKeymap(m_display, m_state.keyboard.data());
}

std::string KeyboardMouse::GetName() const
Expand Down Expand Up @@ -441,8 +411,7 @@ ControlState KeyboardMouse::Key::GetState() const
return (m_keyboard[m_keycode / 8] & (1 << (m_keycode % 8))) != 0;
}

KeyboardMouse::Button::Button(unsigned int index, unsigned int* buttons)
: m_buttons(buttons), m_index(index)
KeyboardMouse::Button::Button(unsigned int index, u32* buttons) : m_buttons(buttons), m_index(index)
{
name = fmt::format("Click {}", m_index + 1);
}
Expand Down
8 changes: 4 additions & 4 deletions Source/Core/InputCommon/ControllerInterface/Xlib/XInput2.h
Expand Up @@ -13,6 +13,7 @@ extern "C" {
#include <X11/keysym.h>
}

#include "Common/CommonTypes.h"
#include "Common/Matrix.h"
#include "InputCommon/ControllerInterface/ControllerInterface.h"

Expand All @@ -26,7 +27,7 @@ class KeyboardMouse : public Core::Device
struct State
{
std::array<char, 32> keyboard;
unsigned int buttons;
u32 buttons;
Common::Vec2 cursor;
Common::Vec3 axis;
Common::Vec3 relative_mouse;
Expand All @@ -52,11 +53,11 @@ class KeyboardMouse : public Core::Device
{
public:
std::string GetName() const override { return name; }
Button(unsigned int index, unsigned int* buttons);
Button(unsigned int index, u32* buttons);
ControlState GetState() const override;

private:
const unsigned int* m_buttons;
const u32* m_buttons;
const unsigned int m_index;
std::string name;
};
Expand Down Expand Up @@ -107,7 +108,6 @@ class KeyboardMouse : public Core::Device
};

private:
void SelectEventsForDevice(XIEventMask* mask, int deviceid);
void UpdateCursor(bool should_center_mouse);

public:
Expand Down

0 comments on commit 38b033a

Please sign in to comment.