feat(libSceKeyboard): open a second keyboard port with index 1 #18119

Merged
eliasbruno124-dev merged 2 commits from upstream-pr/18119 into main 2026-11-14 17:40:33 +00:00
eliasbruno124-dev commented 2026-11-10 14:05:17 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by eliasbruno124-dev on 2026-11-10T14:05:17Z.


What

The Last of Us Part II Remastered (PPSA15508, 01.002.000) opens two keyboard ports for the initial user at start-up and stops when the second open fails:

*** ASSERTION: m_keyboardHandle1 > 0
***   Message: Keyboard library failed to open keyboard1!! Error Code: -2133196799 User Code: 268435456 Ret Value: 0

-2133196799 is 0x80da0001, the INVALID_ARG that sceKeyboardOpen returns here for every index other than 0. At the title's call sites (0xd76403 and 0xd76427) both calls pass the same user, type 0 and a null parameter; the first passes index 0 and the second index 1, and each result has to be above 0.

Give index 1 its own handle, 2, for a port with no keyboard attached. The host keyboard stays on index 0. On the second handle sceKeyboardRead returns no samples, sceKeyboardReadState returns a disconnected sample of length 1 (the shape the first port reports after a disconnect), sceKeyboardGetConnection reports false, sceKeyboardGetKey2Char converts as for the first port, since it uses no device state, and sceKeyboardDebugGetDeviceId returns INVALID_HANDLE, as for any handle without a device. Opening index 1 twice returns ALREADY_OPENED, and each port is closed on its own. The order of the checks is unchanged, so a bad type or index still comes before the user check, and any other index keeps INVALID_ARG.

The only source is that call site, which shows what the title expects and not what the console's library does: the library's code was not read (no copy of it was available), and shadPS4's sceKeyboardOpen is a stub that returns 1 whatever it is passed. The range of index and what a port without a keyboard answers on a console are recorded as unverified in docs/dev/debt/libSceKeyboard.md.

Tested

Windows 11 Pro 10.0.26300, MSYS2 UCRT64 GCC 16.2.0-6, Release. The title needs other open pull requests to reach this stop, so the build is an integration: main 39bac5d055 with #18086 (MSYS2 configuration), #18116, #11630 (b17933dfd4), #17570 (06457e01f1) and #3965 (8029f87e58) merged, with and without this change.

  • keyboard_input, with the extended Keyboard.cpp, and the five other tests matching keyboard pass. The new cases cover: INVALID_ARG for index 2, index -1 and type 1 with either index; the user checks for index 1; the second handle refused before it is opened; a second open of index 1 returning ALREADY_OPENED; the read, state, connection, conversion and device-id answers described above, including untouched output on an empty read; closing the second port twice; and the first port still open and connected afterwards. main returns INVALID_ARG for index 1 before it looks at the user, so the new index 1 cases fail there.
  • PPSA15508 without this change: the assertion above 46 s after the start, then the title's break (int 0x41, exit 0xC0000005).
  • PPSA15508 with this change: no assertion and no stop within the 200 s limit. The window is still black then and no frame was presented, so this shows the assertion gone, not a later screen. No input was sent.
  • python tools/check_conventions.py --base origin/main and git diff --check pass.

Not tested: this change alone on main with the title (it stops earlier there, at sceHttp2SendRequestAsync), a real second keyboard, Linux and macOS, a console. No menu or gameplay result is claimed.

Overlap: of the open pull request heads fetched on 2026-11-10, four change a keyboard file. #17948 guards the output buffers of sceKeyboardRead and sceKeyboardReadState in the lines this branch extends for the second handle, so the two conflict in keyboard_impl.cpp; it does not open a second port, and I will rebase onto it if it lands first. #17934 merges cleanly with this branch. #13486 and #14212 already conflict with main in other files. tools/pr_overlap.py --branch did not finish on this partial clone (dropped connections), so this comes from the file lists of the fetched heads and trial merges. The branch merges cleanly into main 3c9276b78f, which was not built here.

Checklist

  • Based on current main; no other open PR implements the same functions
  • One topic per PR; follow-ups go in a new PR
  • No comments in code except technical debt
  • Unimplemented paths throw (NotImplemented_nid_no_patch); silent stubs are listed under Silent stubs in their library's technical debt file
  • No notes, investigation .md files or images added to the repository (attach them to this PR)
  • New third-party code is a submodule built from source
  • Generic behaviour, not specific to one title
  • Depends on: none (the title run above also needs #11630, #17570 and #3965)
  • Fixes: none
  • Unblocks: PPSA15508 #5468 (the keyboard assertion in the integration described above; not playability)
  • GPUs tested: n/a (libSceKeyboard; the title run used an NVIDIA GeForce RTX 3060 Laptop GPU, driver 621.01, Windows 11)
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5242643964 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/18119). Originally posted by [eliasbruno124-dev](https://github.com/eliasbruno124-dev) on 2026-11-10T14:05:17Z. --- ### What The Last of Us Part II Remastered (PPSA15508, 01.002.000) opens two keyboard ports for the initial user at start-up and stops when the second open fails: ``` *** ASSERTION: m_keyboardHandle1 > 0 *** Message: Keyboard library failed to open keyboard1!! Error Code: -2133196799 User Code: 268435456 Ret Value: 0 ``` `-2133196799` is `0x80da0001`, the `INVALID_ARG` that `sceKeyboardOpen` returns here for every index other than 0. At the title's call sites (`0xd76403` and `0xd76427`) both calls pass the same user, type 0 and a null parameter; the first passes index 0 and the second index 1, and each result has to be above 0. Give index 1 its own handle, 2, for a port with no keyboard attached. The host keyboard stays on index 0. On the second handle `sceKeyboardRead` returns no samples, `sceKeyboardReadState` returns a disconnected sample of length 1 (the shape the first port reports after a disconnect), `sceKeyboardGetConnection` reports false, `sceKeyboardGetKey2Char` converts as for the first port, since it uses no device state, and `sceKeyboardDebugGetDeviceId` returns `INVALID_HANDLE`, as for any handle without a device. Opening index 1 twice returns `ALREADY_OPENED`, and each port is closed on its own. The order of the checks is unchanged, so a bad type or index still comes before the user check, and any other index keeps `INVALID_ARG`. The only source is that call site, which shows what the title expects and not what the console's library does: the library's code was not read (no copy of it was available), and shadPS4's [`sceKeyboardOpen`](https://github.com/shadps4-emu/shadPS4/blob/4788f0b108a6faaae5105f8ac88ef8e1935e2b22/src/core/libraries/keyboard/keyboard.cpp#L70-L73) is a stub that returns 1 whatever it is passed. The range of `index` and what a port without a keyboard answers on a console are recorded as unverified in `docs/dev/debt/libSceKeyboard.md`. ### Tested <!-- OS, title or homebrew, what was checked. "Not tested" is a valid answer. A performance change: the summary lines of main and of this PR from the same machine and scene, with the traces attached, captured as the Performance report issue form describes. --> Windows 11 Pro 10.0.26300, MSYS2 UCRT64 GCC 16.2.0-6, Release. The title needs other open pull requests to reach this stop, so the build is an integration: `main` `39bac5d055` with #18086 (MSYS2 configuration), #18116, #11630 (`b17933dfd4`), #17570 (`06457e01f1`) and #3965 (`8029f87e58`) merged, with and without this change. - `keyboard_input`, with the extended `Keyboard.cpp`, and the five other tests matching `keyboard` pass. The new cases cover: `INVALID_ARG` for index 2, index -1 and type 1 with either index; the user checks for index 1; the second handle refused before it is opened; a second open of index 1 returning `ALREADY_OPENED`; the read, state, connection, conversion and device-id answers described above, including untouched output on an empty read; closing the second port twice; and the first port still open and connected afterwards. `main` returns `INVALID_ARG` for index 1 before it looks at the user, so the new index 1 cases fail there. - PPSA15508 without this change: the assertion above 46 s after the start, then the title's break (`int 0x41`, exit `0xC0000005`). - PPSA15508 with this change: no assertion and no stop within the 200 s limit. The window is still black then and no frame was presented, so this shows the assertion gone, not a later screen. No input was sent. - `python tools/check_conventions.py --base origin/main` and `git diff --check` pass. Not tested: this change alone on `main` with the title (it stops earlier there, at `sceHttp2SendRequestAsync`), a real second keyboard, Linux and macOS, a console. No menu or gameplay result is claimed. Overlap: of the open pull request heads fetched on 2026-11-10, four change a keyboard file. #17948 guards the output buffers of `sceKeyboardRead` and `sceKeyboardReadState` in the lines this branch extends for the second handle, so the two conflict in `keyboard_impl.cpp`; it does not open a second port, and I will rebase onto it if it lands first. #17934 merges cleanly with this branch. #13486 and #14212 already conflict with `main` in other files. `tools/pr_overlap.py --branch` did not finish on this partial clone (dropped connections), so this comes from the file lists of the fetched heads and trial merges. The branch merges cleanly into `main` `3c9276b78f`, which was not built here. ### Checklist <!-- Rules behind each item: https://github.com/boykopovar/AnyPS5/blob/main/CONTRIBUTING.md --> - [x] Based on current `main`; no other open PR implements the same functions - [x] One topic per PR; follow-ups go in a new PR - [x] No comments in code except [technical debt](https://github.com/boykopovar/AnyPS5/blob/main/docs/dev/CONVENTIONS.md) - [x] Unimplemented paths throw (`NotImplemented_nid_no_patch`); silent stubs are listed under Silent stubs in their library's [technical debt](https://github.com/boykopovar/AnyPS5/tree/main/docs/dev/debt) file - [x] No notes, investigation `.md` files or images added to the repository (attach them to this PR) - [x] New third-party code is a submodule built from source - [x] Generic behaviour, not specific to one title - [x] Depends on: none (the title run above also needs #11630, #17570 and #3965) <!-- #PR, or none --> - [x] Fixes: none <!-- issue it closes as Fixes #N, or none --> - [x] Unblocks: PPSA15508 #5468 (the keyboard assertion in the integration described above; not playability) <!-- title ID and Game bring-up discussion, e.g. PPSA01325 #3232, or none --> - [x] GPUs tested: n/a (libSceKeyboard; the title run used an NVIDIA GeForce RTX 3060 Laptop GPU, driver 621.01, Windows 11) <!-- for AGC, shader recompiler or VideoOut changes: vendor, model, driver and OS of each GPU the GPU tests ran on; otherwise n/a --> - [x] AI-assisted: yes <!-- yes / no -->
PrivacyOnion commented 2026-11-10 21:47:43 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by PrivacyOnion on 2026-11-10T21:47:43Z.

Review: CHANGES_REQUESTED


Verified on Linux (Ubuntu 24.04, GCC 13.3, Ninja, Release) against 06d7ed43c2:

  • Conventions: tools/check_conventions.py passed; git diff --check clean.
  • Build: keyboard_tests built cleanly.
  • CTest (keyboard_input, keyboard_dialog):
    1/2 Test #3382: keyboard_dialog ..................***Not Run   0.00 sec
    2/2 Test #3381: keyboard_input ...................   Passed    0.10 sec
    50% tests passed, 1 tests failed out of 2
    
    The following tests FAILED:
    
  • Note: the AI-assisted checkbox can stay.

Requesting changes for the points above.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5849970241 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/18119#pullrequestreview-5849970241). Originally posted by [PrivacyOnion](https://github.com/PrivacyOnion) on 2026-11-10T21:47:43Z. **Review:** CHANGES_REQUESTED --- Verified on Linux (Ubuntu 24.04, GCC 13.3, Ninja, Release) against `06d7ed43c2`: - **Conventions**: `tools/check_conventions.py` passed; `git diff --check` clean. - **Build**: `keyboard_tests` built cleanly. - **CTest** (`keyboard_input`, `keyboard_dialog`): ``` 1/2 Test #3382: keyboard_dialog ..................***Not Run 0.00 sec 2/2 Test #3381: keyboard_input ................... Passed 0.10 sec 50% tests passed, 1 tests failed out of 2 ``` ``` The following tests FAILED: ``` - Note: the AI-assisted checkbox can stay. Requesting changes for the points above.
PrivacyOnion commented 2026-11-11 18:56:37 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by PrivacyOnion on 2026-11-11T18:56:37Z.

Review: APPROVED


Verified on Linux (Ubuntu 24.04, GCC 13.3, Ninja, Release) against 06d7ed43c2:

  • Conventions: tools/check_conventions.py passed; git diff --check clean.
  • Build: keyboard_tests built cleanly.
  • CTest (keyboard_input, keyboard_dialog):
    1/2 Test #3382: keyboard_dialog ..................   Passed    0.08 sec
    2/2 Test #3381: keyboard_input ...................   Passed    0.07 sec
    100% tests passed, 0 tests failed out of 2
    
  • Negative control: with core/libs/prx/libSceKeyboard/include/keyboard_structs.h, core/libs/prx/libSceKeyboard/src/keyboard_impl.cpp reverted to main, the new tests do not build/link without the change; restoring it makes them build and pass.
  • Code reading:
    • core/libs/prx/libSceKeyboard/src/keyboard_impl.cpp: Verified Open() validates arguments in order, returning KEYBOARD_ERROR_INVALID_ARG for index < 0 or index > 1 and type != 0, checking user IDs, returning KEYBOARD_ERROR_ALREADY_OPENED on duplicate opens, and returning KEYBOARD_SECOND_PORT_HANDLE (2) for index 1.
    • core/libs/prx/libSceKeyboard/src/keyboard_impl.cpp: Verified Close() correctly transitions secondPortOpened to false and returns KEYBOARD_OK without clobbering host keyboard keys or event history, and returns KEYBOARD_ERROR_INVALID_HANDLE when called on an unopened or closed handle 2.
    • core/libs/prx/libSceKeyboard/src/keyboard_impl.cpp: Verified Read(), ReadState(), GetConnection(), and GetKey2Char() check handle validity and validate caller pointers (nullptr checks and num bounds [1, 16]) returning KEYBOARD_ERROR_INVALID_ARG before writing to destination buffers.
    • core/libs/prx/libSceKeyboard/src/keyboard_impl.cpp: Verified port 1 query behavior on KEYBOARD_SECOND_PORT_HANDLE, ensuring Read() returns 0 leaving buffers untouched, ReadState() returns zeroed data with length 1 and a valid timestamp, GetConnection() sets *isConnected = false, and DebugGetDeviceId() returns KEYBOARD_ERROR_INVALID_HANDLE with *deviceId = -1.
    • core/libs/prx/libSceKeyboard/src/keyboard_impl.cpp: Verified thread safety across secondPortOpened access and mutations, ensuring keyboardMutex is consistently held across all state reads, writes, and openSecondPort() invocations.
    • core/libs/prx/libSceKeyboard/tests/Keyboard.cpp: Verified unit test cases rigorously cover out-of-bounds indices (-1, 2), invalid types, invalid users, double open, nullptr checks, empty reads, state reads, key translations, double close, and preservation of port 0 connection state.
    • docs/dev/debt/libSceKeyboard.md & AGENTS.md: Verified AGENTS.md compliance with zero code comments added to C++ source and header files, and proper documentation of index 1 behavior in technical debt.
  • Note: the AI-assisted checkbox can stay.

Looks Good To Me

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5863938801 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/18119#pullrequestreview-5863938801). Originally posted by [PrivacyOnion](https://github.com/PrivacyOnion) on 2026-11-11T18:56:37Z. **Review:** APPROVED --- Verified on Linux (Ubuntu 24.04, GCC 13.3, Ninja, Release) against `06d7ed43c2`: - **Conventions**: `tools/check_conventions.py` passed; `git diff --check` clean. - **Build**: `keyboard_tests` built cleanly. - **CTest** (`keyboard_input`, `keyboard_dialog`): ``` 1/2 Test #3382: keyboard_dialog .................. Passed 0.08 sec 2/2 Test #3381: keyboard_input ................... Passed 0.07 sec 100% tests passed, 0 tests failed out of 2 ``` - **Negative control**: with `core/libs/prx/libSceKeyboard/include/keyboard_structs.h`, `core/libs/prx/libSceKeyboard/src/keyboard_impl.cpp` reverted to `main`, the new tests do not build/link without the change; restoring it makes them build and pass. - **Code reading**: - core/libs/prx/libSceKeyboard/src/keyboard_impl.cpp: Verified Open() validates arguments in order, returning KEYBOARD_ERROR_INVALID_ARG for index < 0 or index > 1 and type != 0, checking user IDs, returning KEYBOARD_ERROR_ALREADY_OPENED on duplicate opens, and returning KEYBOARD_SECOND_PORT_HANDLE (2) for index 1. - core/libs/prx/libSceKeyboard/src/keyboard_impl.cpp: Verified Close() correctly transitions secondPortOpened to false and returns KEYBOARD_OK without clobbering host keyboard keys or event history, and returns KEYBOARD_ERROR_INVALID_HANDLE when called on an unopened or closed handle 2. - core/libs/prx/libSceKeyboard/src/keyboard_impl.cpp: Verified Read(), ReadState(), GetConnection(), and GetKey2Char() check handle validity and validate caller pointers (nullptr checks and num bounds [1, 16]) returning KEYBOARD_ERROR_INVALID_ARG before writing to destination buffers. - core/libs/prx/libSceKeyboard/src/keyboard_impl.cpp: Verified port 1 query behavior on KEYBOARD_SECOND_PORT_HANDLE, ensuring Read() returns 0 leaving buffers untouched, ReadState() returns zeroed data with length 1 and a valid timestamp, GetConnection() sets *isConnected = false, and DebugGetDeviceId() returns KEYBOARD_ERROR_INVALID_HANDLE with *deviceId = -1. - core/libs/prx/libSceKeyboard/src/keyboard_impl.cpp: Verified thread safety across secondPortOpened access and mutations, ensuring keyboardMutex is consistently held across all state reads, writes, and openSecondPort() invocations. - core/libs/prx/libSceKeyboard/tests/Keyboard.cpp: Verified unit test cases rigorously cover out-of-bounds indices (-1, 2), invalid types, invalid users, double open, nullptr checks, empty reads, state reads, key translations, double close, and preservation of port 0 connection state. - docs/dev/debt/libSceKeyboard.md & AGENTS.md: Verified AGENTS.md compliance with zero code comments added to C++ source and header files, and proper documentation of index 1 behavior in technical debt. - Note: the AI-assisted checkbox can stay. Looks Good To Me
Sign in to join this conversation.
No reviewers
No labels
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
boykopovar/AnyPS5!18119
No description provided.