Raw input, DirectInput, Wayland, SDL3 and Android built their touches
on the stack at each poll and the frontend copied them in, Wayland
sixteen of them every poll whether or not any screen was touched. They
write the places the frontend lends them now.
SDL, DirectInput, Wayland, win32 and Android built their mouse on the
stack at each poll and the frontend copied it in. They write the frame
it lends them now, as X11 and raw input do.
Every input driver's state call took a flag, keyboard_mapping_blocked:
whether the keys are kept from the binds. It mattered when drivers
answered the binds themselves. The frontend answers them now, from the
keys it asks a driver for once a poll, and applies the flag itself; a
driver's state call answers the keyboard, the mouse and the pointer,
and none of the twenty-three read the flag. (udev passed it to its
touch code, which did not read it either.)
The parameter goes: from the interface, the twenty-three drivers, the
frontend's own stand-in driver and its nine calls. The frontend's own
functions keep theirs - that is where it is used.
Measured against this same master built without it, the frame's input
work over 300 frames (X11): 3,690,770 instructions to 3,690,476 with
no controller, 4,354,940 to 4,354,646 with one. An argument a call.
Checked: the frontend harness passes every lane; the X11 keyboard
session logs the same as before, and the hotkey and quit-combination
sessions what master's binary logged; the raw input keyboards test
and the plain Win32 input test, which call a driver's state function
themselves, pass under Wine. Built: Linux in full; DirectInput, raw
input and the plain Win32 driver with a mingw compiler; the Android
unity build with the NDK; Wayland; the Visual C++ 6 project and the
QNX driver on their stand-ins; a build with the optional features
off. Not compiled, there being no compiler for them here: Cocoa, UWP,
the web driver, SDL 1 and the console drivers (DOS, Wii, PS3, PS4,
PSP and Vita, Switch, Wii U); theirs is the same one line, taken out
by script.
The second of three pieces before steps 3 and 4 of the mapping
compiler (plan, section 21). The joypad, its info and the binds are
handed to the same call, and two drivers use any of it: the Wii's
reads one field of the info, the Wii U's asks the joypad for its
touch screen. With those two reworked the call would take the port
and what is asked, and nothing else.
input_driver_t::input_state was handed two joypad drivers: the one
there is, and the secondary, "for secondary input devices (eg MFi
controllers)". The frontend no longer has a secondary and passed NULL
for it at each of its nine calls; no driver did anything with it but
silence the warning about not using it.
The parameter is out of the interface, of the twenty-nine functions
that implement it - every input driver's, the null driver's and the
test stand-ins' in samples/ - and of the nine calls, and of the one
call a sample makes of a driver's function directly
(winraw_keyboards_test, which the sample link check caught). Nothing
else changes in any of them.
Every parameter list of that call in the tree was checked by script
for the new shape, including the drivers that cannot be compiled
here. Compiled: the frontend and the Linux drivers (X11, udev,
Wayland, SDL 1/2/3, linuxraw, test) with a table entry of the wrong
shape made an error; Windows (DirectInput, raw input); Android; the
Cocoa driver for macOS; PS4 and QNX against their stand-in headers.
Measured, the frame's input work over 300 frames (X11): 4,019,788
instructions to 4,017,937 with no controller, 4,690,524 to 4,688,673
with one and a hotkey enabler.
Checked: the X11 keyboard session's log is the same as before; the
hotkey and quit-combination sessions log the same lines on the
binary before this and the one after; the frontend harness passes
every lane; tools/sample_link_check.sh builds the nine samples that
link the changed files, and winraw_keyboards_test passes under Wine.
With this the secondary joypad is gone from the tree.
The X11, raw input, DirectInput and SDL3 drivers each read the
lightgun's buttons from what they are bound to - a pad's button or
axis, then a key, then a mouse button - in fifty lines of their own.
The frontend does it, from the keys and the mouse it already has for
the port, and the four copies are deleted. A driver says its lightgun
buttons are bound ones when it publishes its mice
(INPUT_POINTERS_GUN_BUTTONS_BOUND); the Wayland and SDL1 and 2 drivers
do not, and go on giving the lightgun the mouse's buttons themselves.
Which mouse a lightgun's button reads is kept as each driver had it:
the port's as the driver lists them - the one at its Mouse Index (raw
input, DirectInput), or the one there is (X11) - and with SDL3 the one
mouse whatever the port (INPUT_POINTERS_GUN_BUTTONS_EVERY_PORT).
One thing changes, under raw input only: a lightgun button bound to a
key was read whether or not the window had the keyboard, where every
other key a port's controls are bound to is not read in the
background. It follows the same rule now.
A scripted session - the trigger on a mouse button, four buttons on
keys, two ports, a key with the mouse button - reads the same in a
core before and after with X11, raw input, DirectInput and SDL3.
The seven drivers that give the frontend their keys each still worked
out RETRO_DEVICE_ANALOG themselves, in the same thirty lines: a key
bound to one way of a stick's axis reads all the way that way, to the
other the other, and both held read neither. The frontend answers it
from the keys it already has for the port - no call into the driver -
and the seven copies are deleted.
Raw input's rule that a port's controls answer to the port's own
keyboard, and not at all in the background while the menu is up,
holds as before: it is in its keys_down, and the stick's binds are a
port's controls.
A scripted keyboard session - each way of a stick, both ways at once,
the other stick, a stick with a pad key - reads the same in a core
before and after.
The Wayland, Windows raw input, DirectInput, SDL1, SDL2 and SDL3
drivers each get keys_down - which of a list of keys are down - and
lose their own code for RETRO_DEVICE_JOYPAD, as the X11 driver did: the
frontend answers the RetroPad's buttons and the hotkeys bound to keys
from one call a port a poll.
keys_down is given the bind each key is for. Raw input needs it: a
port's controls answer to the one keyboard the port was given, a hotkey
to every keyboard, and in the background keys play the game only. That
is its own rule and stays in its keys_down.
Which mouse a port's button binds read follows how the driver lists
its mice: where it lists them (raw input, DirectInput, SDL), the one at
the port's Mouse Index; where it has the one (X11, Wayland), the port
whose index is 0.
One thing changes: Wayland, SDL1, SDL2 and SDL3 never read a control
bound to a mouse button - Wayland's code said TODO - and now do, as
X11 and the Windows drivers always have.
A scripted keyboard session - each pad key, a three-key chord, a bind
to a mouse button, the reset and pause hotkeys - reads the same in a
core before and after with raw input and DirectInput under Wine and
with X11; with Wayland, SDL2 and SDL3 it differs only in the mouse
button's bind being read.
winraw_keyboards_test read the RetroPad from the driver's state
function, which no longer answers for it; it asks winraw_keys_down()
now, a bind at a time and a port's sixteen at once, and holds what it
held: a port's controls on its own keyboard, a hotkey on any.
The pointer store took a touch to be there when its packed position
was not 0. A contact at the window's origin packs to 0, so a finger
down at 0,0 was not a touch: the pointer read the mouse and was not
pressed, and a lightgun aiming at the first touch aimed at the mouse.
DirectInput, raw input and SDL3 went by whether the contact existed
before they published, so for them this was a regression.
Where a contact is, whether there is one and whether it is down are
three things, and input_driver_publish_touches() takes them
separately: a position, a bit in @present, a bit in @down. A contact
at 0,0 is one; one that has lifted may keep its place, there and not
down - a finger's last place under Wayland, a pen in the air under
SDL3. DirectInput, raw input and SDL3's fingers say each contact they
list is there. Wayland keeps the rule it had, which is its own and is
now written where it belongs: a place at 0,0 is how the desktop reports
a mouse button, and is not a touch.
pointer_frame_test: a touch down at the origin, one lifted there, a
place with none, the viewport's edges, sparse places, the mouse's
fallback, the lightgun's choice, a pen in the air at the origin.
The DirectInput driver hands the frontend its mouse's frame and its
touches, in the order they went down, once a poll, and its own answers
for the mouse, the pointer and the lightgun's aim are deleted.
Two things DirectInput does that the store now does for it, each a
flag a driver sets:
- the mouse is the port's whose Mouse Index is 0, while the pointer and
the lightgun's aim are every port's (INPUT_POINTERS_AIM_EVERY_PORT);
- the lightgun aims where the first touch is while there is one
(INPUT_POINTERS_GUN_AT_TOUCH), placed once a poll.
A wheel notch was kept until something read it, and the first read
used it up. It is the frame's now, as with the other drivers: cleared
at the next poll, and the same for every reader, a control bound to
the wheel included.
pointer_frame_test covers both flags.
Each of these was a pair of ints, written and read one axis at a time;
each is now one VIDEO_POS_PACK word:
- DirectInput: the mouse's motion and position;
- raw input: the pointer;
- SDL1 and SDL2: the mouse's motion and position, read from SDL into
locals and stored once per poll;
- PSP and Switch: the mouse's position and motion;
- GameCube/Wii: the mouse's position and last position, read into
locals and stored once;
- Wayland: the pointer's position, last position and motion;
- the web: the mouse's position and its two motions;
- Android: the mouse's position and motion.
A pair set together is stored once; a single axis is set with
VIDEO_POS_PUT_X/Y and a running total moved with VIDEO_POS_ADD.
video_driver_translate_coord_viewport() returned its result through
four 16-bit outputs - x, y, screen x, screen y - so every input driver
declared four locals and passed four pointers, and some packed the
results again straight after. It returns the position and the screen
position as two VIDEO_POS_PACK words now, and its two wrappers take
the same two.
Every caller is converted: the X11, Wayland, udev, raw input,
DirectInput, SDL1/2/3, Cocoa, Android, QNX, web, PSP, PS3, Switch and
GameCube/Wii drivers. Where a driver kept the results packed, it
passes its own fields straight in (Android, QNX, Cocoa, udev, and
Switch, whose four touch arrays become two). udev's mouse pointer
helper returns a packed position too. As before, a translation that
fails leaves its outputs as they were. The samples that stand in for
the function take the new signature.
The overlay kept its touch points, the pointers that missed every
hitbox, the full-screen pointer and its previous position as pairs of
16-bit coordinates, read with two calls and stored one at a time.
Each is now one VIDEO_POS_PACK word, read into it directly.
dinput.c and cocoa_input.h each had their own copy of the same
packing; both use VIDEO_POS now.
The DirectInput driver is handed some of its input by the window
procedure (dinput_handle_message()), which runs on the thread that
owns the window - with threaded video, the video thread, not the one
that polls. What it handed over it wrote straight into the driver's
state, under the poll's feet:
- touches were a list of malloc()ed nodes, added to and freed by the
window procedure while the poll walked it: the poll could read a
node that had just been freed;
- the wheel's notches, a focusing click, a title-bar double click and
the Shift and Alt keys held were bits in a flags word both threads
read, changed and wrote back, so either could undo the other's
change - a wheel notch lost, a focusing click let through, a
Shift release sent twice or not at all;
- WM_MOUSEMOVE wrote two positions nothing read.
Now:
- touches are sixteen fixed slots. The window procedure, the only
writer, fills a slot's position and order and then publishes it by
storing its id; it frees one by storing 0. The poll reads the id
first. Touches are given out in the order they went down, as the
list kept them;
- the wheel, focusing click, title-bar click and the Shift and Alt
keys are in a word of their own that both threads change only with
atomic operations. The poll takes the wheel and click bits once, at
its start, into its own flags, so everything after reads as it did.
A Shift or Alt release the OS does not send is still sent by the
poll, and a key-up the window procedure sees after that is still
swallowed - the bit is tested and cleared in one step either side;
- the unread positions are gone.
No lock was added. Run in the Wine smoke test's DirectInput scenario,
threaded video on: keys, a click, a controller plugged in, a
fullscreen toggle and quit, as before. Touch and the wheel are not
exercised there, and there is no Windows touchscreen here.
A driver that needed a setting took all of them and picked the field
out:
if (config_get_ptr()->uints.input_mouse_index[port] == 0)
wr->nowinkey = config_get_ptr()->bools.input_nowinkey_enable;
Thirteen named calls on the frontend answer instead, each for one
setting or one question:
if (input_config_get_mouse_index(port) == 0)
wr->nowinkey = input_config_get_nowinkey_enable();
They join the input_config_get_...() calls that were there. What the
drivers depend on is that list; the layout of the settings is the
frontend's business. Each call sits under the condition its setting
has, and answers with the default before the settings are loaded,
which is what the drivers' own checks for that did.
Twenty-eight reads are converted, in thirteen files: the mouse index
in the SDL 1.2, SDL2, SDL3, DirectInput and X11 input drivers; the
Windows key, background keyboard, overlay and joypad driver in the raw
input and DirectInput drivers; XInput and Player Lights in the raw
input joypad driver; the rumble gain in the udev, SDL3 and Dingux
joypad drivers; the profile directory and system keyboard for SDL3;
the keyboard layout for xkb; the test drivers' scripts.
The samples that link a driver without the frontend get the calls from
one shared header, samples/input/input_config_stubs.h, which reads
their own settings.
Left, in tools/input_state_grab_check.py: 35 in 12 files - Android,
Apple, the consoles, udev's touch options, the replay code, and the
SDL2 input driver taking the video driver's window.
Built for Linux, Windows and Android; the bridge, udev, X11 and raw
input tests and the frontend harness pass.
WM_DEVICECHANGE arrives once per HID interface, so a single composite
device - a USB headset with volume keys, a keyboard with a media or
vendor collection - delivers a burst of arrivals. The dinput and
winraw input drivers reinitialised the joypad driver on every one of
them: a full destroy, init and DirectInput EnumDevices() per event,
back to back. Because destroy() joins an enumeration still in flight,
each event after the first also parked the window thread until the
previous walk of the HID/PnP tree finished.
Arm a one-shot window timer on each event instead. Re-arming restarts
it, so the reinit runs once, 250 ms after the last event of the burst,
woken by the timer rather than by polling or by re-sampling a clock.
When the timer fires while an enumeration from the previous reinit is
still running (a slow Bluetooth stack after boot), do not block on it:
re-arm and look again one settle period later - the same "skip the
pass, retry on a later wake" the audio drivers use in place of an
open-ended wait. The hotplug path can no longer block the window
thread at all.
The dinput and winraw wndprocs route the timer to their input driver's
handler, which only ever sees its own live driver data. Any other
wndproc that receives it kills it in wnd_proc_common, so a timer armed
by a driver that has since gone away cannot tick forever.
This is not the EnumDevices()-every-frame problem some games have (the
one DInput8_CacheFix works around); RetroArch never enumerated outside
init and hotplug. It removes the remaining burst case.
The keybind accessors reach the remaining call sites: the input
drivers for Android, Cocoa, DirectInput, DOS, PS3, QNX, RWebInput,
SDL, SDL3, udev, UWP, Wayland, WinRaw and X11, and the Qt user-binds
page. A build that enables any of those reads enum_idx, key and valid
through the accessors like the rest of the tree.
dinput_poll() reads the mouse in two steps: GetDeviceState() on the
DirectInput mouse, then GetCursorPos() + ScreenToClient() to derive
absolute coordinates for the menu and RETRO_DEVICE_POINTER.
The DirectInput device is opened with DISCL_FOREGROUND, so the first
step correctly fails whenever the window is not in the foreground and
buttons/deltas are zeroed. The second step ran unconditionally, and
GetCursorPos() does not care about focus, so mouse_x/mouse_y kept
tracking the desktop cursor through a minimized window or with the
Qt desktop menu on top. The menu saw a moving hover position and kept
playing the focus sound while keyboard and buttons were blocked.
Only update the absolute position while the DirectInput mouse is
acquired. The position freezes at its last in-focus value and resumes
on the first poll after focus returns.
Reported by sonninnos.
dinput_poll() cleared di->state with a pointer loop, leaving kb_state
at di->state + 256. The clear on the GetDeviceState() failure path then
reused that same pointer:
for (; kb_state < di->state + 256; kb_state++)
*kb_state = 0;
The condition is false on entry, so the body never ran. The intent -
zero the state when the device cannot be read - was not happening.
It has not caused visible trouble because the first loop already
cleared the buffer before the call. That only holds if a failing
GetDeviceState() leaves the buffer untouched, which DirectInput does
not promise; a partial write before DIERR_INPUTLOST would be read as
live key state and, since dinput_poll() runs once per frame with no
other writer, stay that way until the device is reacquired.
dinput_joypad_poll() in the sibling driver gets this right: it memsets
joy_state on the same failure and re-centres rgdwPOV, with a comment
about D-pad directions latching across focus loss. The keyboard path
now does the same thing.
Both clears become memset, which is what the loop was open-coding, and
kb_state goes away with them.
dinput_poll() called GetAsyncKeyState four times to decide whether the
latched shift and alt keys had been released. VK_LSHIFT, VK_RSHIFT,
VK_LMENU and VK_RMENU are all at or above 0x20, which is the cutoff for
user32's async key cache, so each one was an unconditional transition
into win32u!NtUserGetAsyncKeyState on every poll where the matching
flag was set.
di->state already holds the answer. IDirectInputDevice8_GetDeviceState
fills it a few lines earlier in the same block, unconditionally, and it
is a DIK-indexed 256-byte array with 0x80 as the down bit - the form
every other read in this file already uses. The same four DIK codes are
used a little further down to set these flags in the first place.
Four syscalls become four array reads with no additional call.
One behavioural difference is worth stating. The device is acquired
DISCL_NONEXCLUSIVE | DISCL_FOREGROUND, so it is unacquired when the
window leaves the foreground; the reacquire then fails and the block
above zeroes di->state. The alt release therefore fires at focus loss
rather than when alt is physically let go afterwards. The shift pair is
unaffected, since the case it exists for - the missing WM_KEYUP when
both shift keys are held and one is released - happens while focused
and acquired.
Five copies of the same block queried GetKeyState once per modifier and
tested the result with 0x80 (down) and 0x81 (down or toggled).
Neither mask is documented. GetKeyState returns a SHORT in which only
bit 15 is contractual; 0x80 happens to work because user32 builds the
down value as 0xFF80, and on the slow path the encoding comes straight
out of win32k and is not guaranteed at all. GetKeyboardState fills the
documented 256-byte form of the same per-thread table, 0x80 down and
0x01 toggled, which is stable across every Windows and under Wine.
0x81 also conflated the two states: holding CapsLock while the lock was
off reported RETROKMOD_CAPSLOCK. The toggle keys now test 0x01 only.
The single call is cheaper as well. user32's GetKeyState cache covers
virtual-key codes below 0x20 and nothing else, so VK_NUMLOCK, VK_SCROLL,
VK_LWIN and VK_RWIN each took an unconditional kernel transition on
every query - four syscalls per keyboard event, in every copy.
GetKeyboardState is a bare thunk that returns all 256 keys at once.
Consolidated into win32_get_keyboard_mods() in win32_common.c and used
from wnd_proc_common, wnd_proc_common_internal,
wnd_proc_common_dinput_internal, winraw_callback and dinput_poll.
dinput_get_active_keyboard_mods() is removed as a duplicate.
The GetAsyncKeyState calls in dinput_poll are left alone: they read the
global physical state, which GetKeyboardState does not expose.
Adapt the sanitized pointer handling, discussed at libretro#17196 :
winraw and dinput driver specific changes:
make sure pointer position is always within [-0x7fff,0x7fff] by using the confined wrapper
remove extra "inside" checks, general simplification
enable pointer offscreen reporting
use common functions for edge detection and lightgun button ID conversion
sdl driver specific changes:
pointer handling aligned with the other input drivers, as above
added TODO for lightgun part - no suitable test env at the moment where SDL input can be used
Because shift keys were ignored in the event message and only issued during polling, a quick key press and release between polling would get ignored.
This change also fixes left alt up key events getting issued (and sent to the core) twice.
- Add missing numlock mod to dinput
- Add missing scrolllock mod to x11
- Add missing capslock, numlock, scrolllock and meta mods to android
- Add missing scrolllock mod to sdl
- Add missing capslock, numlock, scrolllock and meta mods to switch
- Add missing numlock mod to winraw
- Add missing numlock mod to uwp
Repurpose vibrate_on_keypress to enable device's standard keypress feedback on overlay key/button state changes
- Add keypress_vibrate function ptr to input_driver_t (only implemented on Android for now)
- (Android) Remove APP_CMD_VIBRATE_KEYPRESS
- (Android) Add doHapticFeedback, called directly to avoid latency