35 lines
1.2 KiB
C++
35 lines
1.2 KiB
C++
// SPDX-License-Identifier: LGPL-2.1-or-later
|
|
#pragma once
|
|
#include <windows.h>
|
|
#include <algorithm>
|
|
#include <string>
|
|
|
|
constexpr size_t max_accessible_units = 4096;
|
|
constexpr size_t max_controller_units = 65536;
|
|
constexpr size_t max_reader_event_bytes = 131072;
|
|
constexpr size_t max_reader_queue_bytes = 1048576;
|
|
|
|
inline std::string bounded_accessible_text(const WCHAR *text, size_t length) {
|
|
if (!text) return {};
|
|
length = std::min(length, max_accessible_units);
|
|
// MSAA strings can include padding past their first terminating NUL.
|
|
size_t used = 0;
|
|
while (used < length && text[used]) ++used;
|
|
if (used && text[used - 1] >= 0xd800 && text[used - 1] <= 0xdbff) --used;
|
|
if (!used) return {};
|
|
int size = WideCharToMultiByte(CP_UTF8, WC_ERR_INVALID_CHARS, text,
|
|
static_cast<int>(used), nullptr, 0, nullptr, nullptr);
|
|
if (size <= 0) return {};
|
|
std::string result(size, '\0');
|
|
WideCharToMultiByte(CP_UTF8, WC_ERR_INVALID_CHARS, text,
|
|
static_cast<int>(used), result.data(), size, nullptr, nullptr);
|
|
return result;
|
|
}
|
|
|
|
inline bool controller_text_fits(const WCHAR *text) {
|
|
if (!text) return false;
|
|
for (size_t i = 0; i <= max_controller_units; ++i)
|
|
if (!text[i]) return true;
|
|
return false;
|
|
}
|