Files
RetroArch/input/drivers/android_poll_changes.h
T
c6046da20f Android: read the input queue on a thread of its own (#19923)
* Android: read the input queue on a thread of its own

Android sends a window its next key only once the window has finished
the last one, and a NativeActivity gets every key twice: before the
IME, marked AKEY_EVENT_FLAG_PREDISPATCH, and after it. Read on the
main thread once a frame, with the first copy finished unhandled for
the IME, each key took two polls, about 30 keys a second at 60 Hz. A
pad mashed faster than that queued up in the system, and its presses
kept arriving seconds after it was let go.

A thread with its own looper now reads the queue. It copies each event
into an SPSC ring and finishes it at once, so Android sends the next
one straight away. The copy before the IME is the one read, unless the
system keyboard is open, when keys still go by the IME first. Volume
keys are left to the system and read once, after the IME. Until the
first input driver starts, events are finished unread. With the ring
full, the reader leaves events in the queue until the poll makes room.

The poll applies the copies in order through the same handlers. A key,
a finger or a mouse button can now go down and back up between two
polls, so the poll stops at the first event that would undo what an
earlier one did in the same poll and leaves the rest for the next
(android_poll_changes.h): a press shorter than a frame is still seen
for one. Mouse moves in one poll are summed.

The reader answers APP_CMD_INPUT_CHANGED once it has let go of the old
queue. The AMotionEvent calls looked up with dlsym are looked up once,
before the reader starts, since it reads them too.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01YTBjv1trXRxAXvNwCt89Ka

* Android input reader: records of their own size, read where they lie

Each event went through the input reader's ring as a 312-byte record,
whatever it was, built on the reader's stack, copied into the ring and
copied out again by the poll. A key needs 44 bytes of it and a stick's
move 88.

A record is now as long as its event: a key ends before the stick
axes, a stick's move before the touch fields, and a touch, mouse or
pen carries only the pointers it has. The reader builds each where the
poll will read it, and the poll applies it there
(android_input_ring.h); nothing is copied after the event leaves the
input queue. A record never crosses the ring's end: the rest of a lap
is a pad the poll steps over. The ring is 64 KiB, some 1500 keys.

A stick's moves are its state: the poll keeps only the last of a frame.
The reader publishes what it writes once the queue is empty, and a
stick's move written over its unpublished move before it, as long as
the hat stays where it was, so a fast pad no longer fills the ring and
holds back keys behind it.

A hat, the D-pad of many pads, tapped between two polls was lost: the
poll's hold-back covered keys, fingers and mouse buttons only. Its four
directions are kept back the same way now.

android_input_ring_test holds the ring to reading in place, records
whole across laps, merging only unpublished moves, and two threads;
android_poll_changes_test has a hat lane.

---------

Co-authored-by: Claude Opus 5.5 <[email protected]>
Co-authored-by: LibretroAdmin <[email protected]>
2026-10-27 04:10:15 +01:00

172 lines
5.9 KiB
C

/* RetroArch - A frontend for libretro.
*
* RetroArch is free software: you can redistribute it and/or modify it under the terms
* of the GNU General Public License as published by the Free Software Found-
* ation, either version 3 of the License, or (at your option) any later version.
*
* RetroArch is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
* PURPOSE. See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with RetroArch.
* If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef _ANDROID_POLL_CHANGES_H
#define _ANDROID_POLL_CHANGES_H
#include <stddef.h>
#include <stdint.h>
#include <string.h>
#include <boolean.h>
#include <retro_inline.h>
#include <retro_miscellaneous.h>
/* What one input poll (input/drivers/android_input.c) has changed.
*
* The input reader hands a poll every event since the last one, so a
* key, a finger or a mouse button can go down and back up in between.
* The core reads the state once a frame, after the poll, and would
* never see that press. So the poll applies events in order until one
* would undo a change an earlier one made, and leaves it and the rest
* for the next poll: a press shorter than a frame is shown for one
* frame, and nothing waits when nothing has to. A key event that
* changes nothing, such as a repeat, never waits. A pad's hat, its
* D-pad on many pads, is four keys the same way. */
#define ANDROID_POLL_CHANGES_ROWS 9 /* 8 pad rows and the keyboard's */
#define ANDROID_POLL_CHANGES_KEY_BYTES 32 /* keycodes 0-255 */
#define ANDROID_POLL_CHANGES_POINTERS 32
typedef struct
{
int32_t pointer_device[ANDROID_POLL_CHANGES_POINTERS];
int32_t pointer_id[ANDROID_POLL_CHANGES_POINTERS];
unsigned pointers;
unsigned mouse_down; /* buttons pressed this poll */
unsigned mouse_up; /* buttons let go this poll */
uint8_t down[ANDROID_POLL_CHANGES_ROWS][ANDROID_POLL_CHANGES_KEY_BYTES];
uint8_t up[ANDROID_POLL_CHANGES_ROWS][ANDROID_POLL_CHANGES_KEY_BYTES];
uint8_t pointer_down[ANDROID_POLL_CHANGES_POINTERS];
uint8_t hat_down[ANDROID_POLL_CHANGES_ROWS]; /* directions, one bit each */
uint8_t hat_up[ANDROID_POLL_CHANGES_ROWS];
} android_poll_changes_t;
static INLINE void android_poll_changes_begin(android_poll_changes_t *c)
{
memset(c, 0, sizeof(*c));
}
/* A press (@down) or release of @keycode on @row, where it is held
* (@held) or not: false if the key went the other way earlier in this
* poll. */
static INLINE bool android_poll_changes_key(android_poll_changes_t *c,
unsigned row, unsigned keycode, bool held, bool down)
{
uint8_t *mine;
uint8_t *other;
if ( row >= ANDROID_POLL_CHANGES_ROWS
|| keycode >= ANDROID_POLL_CHANGES_KEY_BYTES * 8
|| held == down)
return true;
mine = down ? c->down[row] : c->up[row];
other = down ? c->up[row] : c->down[row];
if (BIT_GET(other, keycode))
return false;
BIT_SET(mine, keycode);
return true;
}
/* The id of the @i-th pointer, ids @stride bytes apart */
static INLINE int32_t android_poll_changes_id(const int32_t *ids,
size_t stride, unsigned i)
{
return *(const int32_t*)((const char*)ids + (size_t)i * stride);
}
/* @count pointers of @device going down (@down) or up together, their
* ids @stride bytes apart: false if one of them went the other way
* earlier in this poll, or if this poll can keep track of no more
* pointers. */
static INLINE bool android_poll_changes_pointers(android_poll_changes_t *c,
int32_t device, const int32_t *ids, size_t stride, unsigned count,
bool down)
{
unsigned i, j;
unsigned fresh = 0;
for (i = 0; i < count; i++)
{
int32_t id = android_poll_changes_id(ids, stride, i);
for (j = 0; j < c->pointers; j++)
if ( c->pointer_device[j] == device
&& c->pointer_id[j] == id)
break;
if (j == c->pointers)
fresh++;
else if ((c->pointer_down[j] != 0) != down)
return false;
}
if (c->pointers + fresh > ANDROID_POLL_CHANGES_POINTERS)
return false;
for (i = 0; i < count; i++)
{
int32_t id = android_poll_changes_id(ids, stride, i);
for (j = 0; j < c->pointers; j++)
if ( c->pointer_device[j] == device
&& c->pointer_id[j] == id)
break;
if (j < c->pointers)
continue;
c->pointer_device[j] = device;
c->pointer_id[j] = id;
c->pointer_down[j] = down ? 1 : 0;
c->pointers++;
}
return true;
}
/* The mouse buttons going from @was to @now, one bit a button: false if
* one of them went the other way earlier in this poll. */
static INLINE bool android_poll_changes_mouse(android_poll_changes_t *c,
unsigned was, unsigned now)
{
unsigned pressed = now & ~was;
unsigned released = was & ~now;
if ((pressed & c->mouse_up) || (released & c->mouse_down))
return false;
c->mouse_down |= pressed;
c->mouse_up |= released;
return true;
}
/* A hat at @x, @y (-1, 0 or 1 each) as four direction bits */
static INLINE unsigned android_poll_changes_hat_bits(int x, int y)
{
return (x < 0 ? 1u : 0u) | (x > 0 ? 2u : 0u)
| (y < 0 ? 4u : 0u) | (y > 0 ? 8u : 0u);
}
/* The hat of @row going from directions @was to @now: false if one of
* them went the other way earlier in this poll. */
static INLINE bool android_poll_changes_hat(android_poll_changes_t *c,
unsigned row, unsigned was, unsigned now)
{
unsigned pressed = now & ~was;
unsigned released = was & ~now;
if (row >= ANDROID_POLL_CHANGES_ROWS)
return true;
if ((pressed & c->hat_up[row]) || (released & c->hat_down[row]))
return false;
c->hat_down[row] |= (uint8_t)pressed;
c->hat_up[row] |= (uint8_t)released;
return true;
}
#endif