From 8d6b8786f7eb408c4d0ccd8c9f536929478d8080 Mon Sep 17 00:00:00 2001 From: Infernoss2 Date: Thu, 9 Oct 2025 18:05:07 +0300 Subject: [PATCH] updated gitIgnore to include keyboard_lib. --- .gitignore | 2 +- src/kbinput/keyboard_lib.cpp | 60 ++++++++++++++++++++++++++++++++++++ 2 files changed, 61 insertions(+), 1 deletion(-) create mode 100644 src/kbinput/keyboard_lib.cpp diff --git a/.gitignore b/.gitignore index b0c6e45..815f062 100644 --- a/.gitignore +++ b/.gitignore @@ -7,7 +7,7 @@ filter !/src/search search -!/src/kbinput kbinput +!/src/kbinput !/src/tools tools diff --git a/src/kbinput/keyboard_lib.cpp b/src/kbinput/keyboard_lib.cpp new file mode 100644 index 0000000..da096b7 --- /dev/null +++ b/src/kbinput/keyboard_lib.cpp @@ -0,0 +1,60 @@ +#include "../../include/keyboard/keyboard.h" +#include "../common/display.hpp" +#include "keyboard.hpp" + +#include +#include +#include +#include // optional (only if you keep error logs) + +static char* dup_cstr(const std::string& s) { + char* out = static_cast(std::malloc(s.size() + 1)); + if (!out) return nullptr; + std::memcpy(out, s.c_str(), s.size() + 1); + return out; +} + +// C ABI function callable from Onion (C) +extern "C" const char* launch_keyboard(const char* initial_value, const char* title) { + Display* display = nullptr; + Keyboard* kb = nullptr; + char* ret = nullptr; + + try { + display = new Display(); + // defensively handle nullptr inputs + const char* init = initial_value ? initial_value : ""; + const char* lab = title ? title : ""; + + kb = new Keyboard(display, init, lab); + display->lib_mode = true; + + int quit = 0; + std::string result; + + auto input_handler = [&kb](SDLKey key, Uint8 type, int repeating) { + return kb->handleKeyPress(key, type, repeating); + }; + auto frame_handler = [display, input_handler]() { + return display->onInputEvent(input_handler); + }; + + while (!quit) quit = display->requestFrame(frame_handler); + + if (!kb->cancelled) result = kb->getValue(); + + ret = dup_cstr(result); // empty string if cancelled + } + catch (const std::exception& e) { + optional: std::cerr << "launch_keyboard error: " << e.what() << '\n'; + } + + // Destroy in safe order (kb may reference display) + delete kb; + delete display; + return ret; +} + +extern "C" void keyboard_free(const char* p) { + std::free(const_cast(p)); +}