From 4f90ae9c51989a42f8b04cbc9e952b14cc8f6a16 Mon Sep 17 00:00:00 2001 From: patchzyy <64382339+patchzyy@users.noreply.github.com> Date: Wed, 21 Oct 2026 21:18:39 +0200 Subject: [PATCH] paste friend codes from clipboard (#329) --- runtime/src/friend_code_keyboard.cpp | 41 +++++++++++++++++++++++++--- 1 file changed, 37 insertions(+), 4 deletions(-) diff --git a/runtime/src/friend_code_keyboard.cpp b/runtime/src/friend_code_keyboard.cpp index b4cc810..a32b0bf 100644 --- a/runtime/src/friend_code_keyboard.cpp +++ b/runtime/src/friend_code_keyboard.cpp @@ -7,6 +7,7 @@ #include "memory.h" #include "runtime_product.h" +#include #include #include @@ -27,11 +28,32 @@ constexpr uint32_t kPageStateActive = 4; constexpr uint32_t kVtableGetTypeInfoOffset = 0x60; constexpr int kBackspace = -1; constexpr size_t kMaxPendingKeys = 32; +constexpr size_t kFriendCodeDigits = 12; std::mutex g_pendingMutex; std::vector g_pending; std::atomic_bool g_pageFocused{false}; +bool IsPaste(const SDL_KeyboardEvent& key) { + return !key.repeat && key.key == SDLK_V && (key.mod & (SDL_KMOD_CTRL | SDL_KMOD_GUI)) != 0 && + (key.mod & SDL_KMOD_ALT) == 0; +} + +void AppendClipboardDigits(std::vector& keys) { + char* text = SDL_GetClipboardText(); + if (!text) { + return; + } + size_t digits = 0; + for (const char* c = text; *c && digits < kFriendCodeDigits; ++c) { + if (*c >= '0' && *c <= '9') { + keys.push_back(*c - '0'); + ++digits; + } + } + SDL_free(text); +} + std::optional KeyValue(const SDL_KeyboardEvent& key) { if ((key.mod & (SDL_KMOD_CTRL | SDL_KMOD_ALT | SDL_KMOD_GUI)) != 0) { return std::nullopt; @@ -98,16 +120,27 @@ void HandleEvents(const AuroraEvent* events) noexcept { if (!events || !g_pageFocused.load(std::memory_order_acquire) || InputBindings::InputBlocked()) { return; } - std::lock_guard lock(g_pendingMutex); + std::vector keys; for (const AuroraEvent* ev = events; ev->type != AURORA_NONE; ++ev) { if (ev->type != AURORA_SDL_EVENT || ev->sdl.type != SDL_EVENT_KEY_DOWN) { continue; } - const std::optional key = KeyValue(ev->sdl.key); - if (key && g_pending.size() < kMaxPendingKeys) { - g_pending.push_back(*key); + if (IsPaste(ev->sdl.key)) { + AppendClipboardDigits(keys); + } else if (const std::optional key = KeyValue(ev->sdl.key)) { + keys.push_back(*key); } } + if (keys.empty()) { + return; + } + std::lock_guard lock(g_pendingMutex); + for (const int key : keys) { + if (g_pending.size() >= kMaxPendingKeys) { + break; + } + g_pending.push_back(key); + } } } // namespace FriendCodeKeyboard