JKSV/source/keyboard/keyboard.cpp
2025-11-29 10:54:51 -05:00

63 lines
2.1 KiB
C++

#include "keyboard/keyboard.hpp"
#include "error.hpp"
#include <string>
bool keyboard::get_input(SwkbdType keyboardType,
std::string_view defaultText,
std::string_view header,
char *stringOut,
size_t stringLength,
std::optional<std::reference_wrapper<keyboard::Dictionary>> dictionary)
{
// Swkbd config.
SwkbdConfig keyboard{};
// Cache instead of repeated calls.
const bool hasDictionary = dictionary.has_value();
// This changes whether or not we're passed a dictionary here.
bool createError{};
if (hasDictionary)
{
// Getting this out of the optional looks confusing...
const size_t wordCount = dictionary->get().get_count();
createError = error::libnx(swkbdCreate(&keyboard, wordCount));
}
else { createError = error::libnx(swkbdCreate(&keyboard, 0)); }
// Just return false if we can't init the keyboard.
if (createError) { return false; }
// Standard swkbd init.
swkbdConfigSetBlurBackground(&keyboard, true);
swkbdConfigSetInitialText(&keyboard, defaultText.data());
swkbdConfigSetHeaderText(&keyboard, header.data());
swkbdConfigSetGuideText(&keyboard, header.data());
swkbdConfigSetType(&keyboard, keyboardType);
swkbdConfigSetStringLenMax(&keyboard, stringLength);
swkbdConfigSetKeySetDisableBitmask(&keyboard, SwkbdKeyDisableBitmask_Backslash);
// Add the dictionary if one was passed.
if (hasDictionary)
{
// These are easier to read and work with.
const SwkbdDictWord *words = reinterpret_cast<const SwkbdDictWord *>(dictionary->get().get_words());
const size_t wordCount = dictionary->get().get_count();
swkbdConfigSetDictionary(&keyboard, words, wordCount);
}
const bool swkbdError = error::libnx(swkbdShow(&keyboard, stringOut, stringLength));
const bool empty = std::char_traits<char>::length(stringOut) == 0;
if (swkbdError || empty)
{
swkbdClose(&keyboard);
return false;
}
swkbdClose(&keyboard);
return true;
}