#ifdef UNSAFE_BUFFERS_BUILD
#pragma allow_unsafe_buffers
#endif
#include "ui/events/ozone/layout/xkb/xkb_keyboard_layout_engine.h"
#include <stddef.h>
#include <xkbcommon/xkbcommon-names.h>
#include <algorithm>
#include <string_view>
#include <utility>
#include "base/containers/span.h"
#include "base/functional/bind.h"
#include "base/functional/callback_forward.h"
#include "base/functional/callback_helpers.h"
#include "base/location.h"
#include "base/logging.h"
#include "base/memory/free_deleter.h"
#include "base/memory/raw_ptr_exclusion.h"
#include "base/strings/string_util.h"
#include "base/task/single_thread_task_runner.h"
#include "base/task/task_runner.h"
#include "base/task/thread_pool.h"
#include "build/build_config.h"
#include "build/chromeos_buildflags.h"
#include "ui/events/event_constants.h"
#include "ui/events/keycodes/dom/dom_code.h"
#include "ui/events/keycodes/dom/dom_key.h"
#include "ui/events/keycodes/dom/keycode_converter.h"
#include "ui/events/keycodes/keyboard_code_conversion.h"
#include "ui/events/keycodes/keyboard_code_conversion_xkb.h"
namespace ui {
namespace {
LoadKeymapCallback;
KeyboardCode AlphanumericKeyboardCode(xkb_keysym_t xkb_keysym,
char16_t character) { … }
struct PrintableSubEntry { … };
const char16_t kNone = …;
const char16_t kAny = …;
const PrintableSubEntry kU0021[] = …;
const PrintableSubEntry kU0022[] = …;
const PrintableSubEntry kU0023[] = …;
const PrintableSubEntry kU0024[] = …;
const PrintableSubEntry kU0027[] = …;
const PrintableSubEntry kU0028[] = …;
const PrintableSubEntry kU0029[] = …;
const PrintableSubEntry kU002A[] = …;
const PrintableSubEntry kU002B[] = …;
const PrintableSubEntry kU002C[] = …;
const PrintableSubEntry kU002D[] = …;
const PrintableSubEntry kU002E[] = …;
const PrintableSubEntry kU002F[] = …;
const PrintableSubEntry kU003A[] = …;
const PrintableSubEntry kU003B[] = …;
const PrintableSubEntry kU003D[] = …;
const PrintableSubEntry kU003F[] = …;
const PrintableSubEntry kU0040[] = …;
const PrintableSubEntry kU005B[] = …;
const PrintableSubEntry kU005C[] = …;
const PrintableSubEntry kU005D[] = …;
const PrintableSubEntry kU005F[] = …;
const PrintableSubEntry kU0060[] = …;
const PrintableSubEntry kU00A7[] = …;
const PrintableSubEntry kU00AB[] = …;
const PrintableSubEntry kU00B0[] = …;
const PrintableSubEntry kU00BA[] = …;
const PrintableSubEntry kU00E0[] = …;
const PrintableSubEntry kU00E1[] = …;
const PrintableSubEntry kU00E2[] = …;
const PrintableSubEntry kU00E4[] = …;
const PrintableSubEntry kU00E6[] = …;
const PrintableSubEntry kU00E7[] = …;
const PrintableSubEntry kU00E8[] = …;
const PrintableSubEntry kU00E9[] = …;
const PrintableSubEntry kU00ED[] = …;
const PrintableSubEntry kU00F0[] = …;
const PrintableSubEntry kU00F3[] = …;
const PrintableSubEntry kU00F4[] = …;
const PrintableSubEntry kU00F6[] = …;
const PrintableSubEntry kU00F8[] = …;
const PrintableSubEntry kU00F9[] = …;
const PrintableSubEntry kU00FA[] = …;
const PrintableSubEntry kU00FC[] = …;
const PrintableSubEntry kU0103[] = …;
const PrintableSubEntry kU0105[] = …;
const PrintableSubEntry kU010D[] = …;
const PrintableSubEntry kU0111[] = …;
const PrintableSubEntry kU0117[] = …;
const PrintableSubEntry kU0119[] = …;
const PrintableSubEntry kU012F[] = …;
const PrintableSubEntry kU0142[] = …;
const PrintableSubEntry kU015F[] = …;
const PrintableSubEntry kU0161[] = …;
const PrintableSubEntry kU016B[] = …;
const PrintableSubEntry kU0173[] = …;
const PrintableSubEntry kU017C[] = …;
const PrintableSubEntry kU017E[] = …;
struct PrintableMultiEntry { … };
const PrintableMultiEntry kMultiMap[] = …;
struct PrintableSimpleEntry { … };
const PrintableSimpleEntry kSimpleMap[] = …;
#if BUILDFLAG(IS_CHROMEOS_ASH)
void LoadKeymap(const std::string& layout_name,
scoped_refptr<base::SingleThreadTaskRunner> reply_runner,
LoadKeymapCallback reply_callback) {
std::string layout_id;
std::string layout_variant;
XkbKeyboardLayoutEngine::ParseLayoutName(layout_name, &layout_id,
&layout_variant);
xkb_rule_names names = {.rules = NULL,
.model = "pc101",
.layout = layout_id.c_str(),
.variant = layout_variant.c_str(),
.options = ""};
std::unique_ptr<xkb_context, XkbContextDeleter> context;
context.reset(xkb_context_new(XKB_CONTEXT_NO_DEFAULT_INCLUDES));
xkb_context_include_path_append(context.get(), "/usr/share/X11/xkb");
std::unique_ptr<xkb_keymap, XkbKeymapDeleter> keymap;
keymap.reset(xkb_keymap_new_from_names(context.get(), &names,
XKB_KEYMAP_COMPILE_NO_FLAGS));
if (keymap) {
std::unique_ptr<char, base::FreeDeleter> keymap_str(
xkb_keymap_get_as_string(keymap.get(), XKB_KEYMAP_FORMAT_TEXT_V1));
reply_runner->PostTask(
FROM_HERE, base::BindOnce(std::move(reply_callback), layout_name,
std::move(keymap_str)));
} else {
LOG(FATAL) << "Keymap file failed to load: " << layout_name;
}
}
#endif
bool IsControlCharacter(uint32_t character) { … }
}
XkbKeyCodeConverter::XkbKeyCodeConverter() { … }
XkbKeyCodeConverter::~XkbKeyCodeConverter() { … }
XkbKeyboardLayoutEngine::XkbKeyboardLayoutEngine(
const XkbKeyCodeConverter& converter)
: … { … }
XkbKeyboardLayoutEngine::~XkbKeyboardLayoutEngine() { … }
std::string_view XkbKeyboardLayoutEngine::GetLayoutName() const { … }
bool XkbKeyboardLayoutEngine::CanSetCurrentLayout() const { … }
void XkbKeyboardLayoutEngine::SetCurrentLayoutByName(
const std::string& layout_name,
base::OnceCallback<void(bool)> callback) { … }
void XkbKeyboardLayoutEngine::OnKeymapLoaded(
base::OnceCallback<void(bool)> callback,
const std::string& layout_name,
std::unique_ptr<char, base::FreeDeleter> keymap_str) { … }
bool XkbKeyboardLayoutEngine::UsesISOLevel5Shift() const { … }
bool XkbKeyboardLayoutEngine::UsesAltGr() const { … }
bool XkbKeyboardLayoutEngine::Lookup(DomCode dom_code,
int flags,
DomKey* dom_key,
KeyboardCode* key_code) const { … }
void XkbKeyboardLayoutEngine::SetInitCallbackForTest(
base::OnceClosure closure) { … }
bool XkbKeyboardLayoutEngine::SetCurrentLayoutFromBuffer(
const char* keymap_string,
size_t size) { … }
void XkbKeyboardLayoutEngine::SetKeymap(xkb_keymap* keymap) { … }
xkb_mod_mask_t XkbKeyboardLayoutEngine::EventFlagsToXkbFlags(
int ui_flags) const { … }
int XkbKeyboardLayoutEngine::UpdateModifiers(uint32_t depressed,
uint32_t latched,
uint32_t locked,
uint32_t group) { … }
DomCode XkbKeyboardLayoutEngine::GetDomCodeByKeysym(
uint32_t keysym,
const std::optional<std::vector<std::string_view>>& modifiers) const { … }
bool XkbKeyboardLayoutEngine::XkbLookup(xkb_keycode_t xkb_keycode,
xkb_mod_mask_t xkb_flags,
xkb_keysym_t* xkb_keysym,
uint32_t* character) const { … }
KeyboardCode XkbKeyboardLayoutEngine::DifficultKeyboardCode(
DomCode dom_code,
int ui_flags,
xkb_keycode_t xkb_keycode,
xkb_mod_mask_t xkb_flags,
xkb_keysym_t xkb_keysym,
char16_t character) const { … }
char16_t XkbKeyboardLayoutEngine::XkbSubCharacter(xkb_keycode_t xkb_keycode,
xkb_mod_mask_t base_flags,
char16_t base_character,
xkb_mod_mask_t flags) const { … }
void XkbKeyboardLayoutEngine::ParseLayoutName(const std::string& layout_name,
std::string* layout_id,
std::string* layout_variant) { … }
}