devices: refactor class member vars (#10206)

This commit is contained in:
davc0n 2025-04-29 19:51:07 +02:00 committed by GitHub
parent 40147d3a3f
commit e9c3fcbb64
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
27 changed files with 585 additions and 578 deletions

View file

@ -28,38 +28,38 @@ eHIDType IKeyboard::getType() {
}
IKeyboard::~IKeyboard() {
events.destroy.emit();
m_events.destroy.emit();
clearManuallyAllocd();
}
void IKeyboard::clearManuallyAllocd() {
if (xkbStaticState)
xkb_state_unref(xkbStaticState);
if (m_xkbStaticState)
xkb_state_unref(m_xkbStaticState);
if (xkbState)
xkb_state_unref(xkbState);
if (m_xkbState)
xkb_state_unref(m_xkbState);
if (xkbKeymap)
xkb_keymap_unref(xkbKeymap);
if (m_xkbKeymap)
xkb_keymap_unref(m_xkbKeymap);
if (xkbSymState)
xkb_state_unref(xkbSymState);
if (m_xkbSymState)
xkb_state_unref(m_xkbSymState);
xkbSymState = nullptr;
xkbKeymap = nullptr;
xkbState = nullptr;
xkbStaticState = nullptr;
xkbKeymapFD.reset();
m_xkbSymState = nullptr;
m_xkbKeymap = nullptr;
m_xkbState = nullptr;
m_xkbStaticState = nullptr;
m_xkbKeymapFD.reset();
}
void IKeyboard::setKeymap(const SStringRuleNames& rules) {
if (keymapOverridden) {
if (m_keymapOverridden) {
Debug::log(LOG, "Ignoring setKeymap: keymap is overridden");
return;
}
currentRules = rules;
m_currentRules = rules;
xkb_rule_names XKBRULES = {
.rules = rules.rules.c_str(),
.model = rules.model.c_str(),
@ -80,21 +80,21 @@ void IKeyboard::setKeymap(const SStringRuleNames& rules) {
Debug::log(LOG, "Attempting to create a keymap for layout {} with variant {} (rules: {}, model: {}, options: {})", rules.layout, rules.variant, rules.rules, rules.model,
rules.options);
if (!xkbFilePath.empty()) {
auto path = absolutePath(xkbFilePath, g_pConfigManager->m_configCurrentPath);
if (!m_xkbFilePath.empty()) {
auto path = absolutePath(m_xkbFilePath, g_pConfigManager->m_configCurrentPath);
if (FILE* const KEYMAPFILE = fopen(path.c_str(), "r"); !KEYMAPFILE)
Debug::log(ERR, "Cannot open input:kb_file= file for reading");
else {
xkbKeymap = xkb_keymap_new_from_file(CONTEXT, KEYMAPFILE, XKB_KEYMAP_FORMAT_TEXT_V1, XKB_KEYMAP_COMPILE_NO_FLAGS);
m_xkbKeymap = xkb_keymap_new_from_file(CONTEXT, KEYMAPFILE, XKB_KEYMAP_FORMAT_TEXT_V1, XKB_KEYMAP_COMPILE_NO_FLAGS);
fclose(KEYMAPFILE);
}
}
if (!xkbKeymap)
xkbKeymap = xkb_keymap_new_from_names(CONTEXT, &XKBRULES, XKB_KEYMAP_COMPILE_NO_FLAGS);
if (!m_xkbKeymap)
m_xkbKeymap = xkb_keymap_new_from_names(CONTEXT, &XKBRULES, XKB_KEYMAP_COMPILE_NO_FLAGS);
if (!xkbKeymap) {
if (!m_xkbKeymap) {
g_pConfigManager->addParseError("Invalid keyboard layout passed. ( rules: " + rules.rules + ", model: " + rules.model + ", variant: " + rules.variant +
", options: " + rules.options + ", layout: " + rules.layout + " )");
@ -102,38 +102,38 @@ void IKeyboard::setKeymap(const SStringRuleNames& rules) {
rules.options);
memset(&XKBRULES, 0, sizeof(XKBRULES));
currentRules.rules = "";
currentRules.model = "";
currentRules.variant = "";
currentRules.options = "";
currentRules.layout = "us";
m_currentRules.rules = "";
m_currentRules.model = "";
m_currentRules.variant = "";
m_currentRules.options = "";
m_currentRules.layout = "us";
xkbKeymap = xkb_keymap_new_from_names(CONTEXT, &XKBRULES, XKB_KEYMAP_COMPILE_NO_FLAGS);
m_xkbKeymap = xkb_keymap_new_from_names(CONTEXT, &XKBRULES, XKB_KEYMAP_COMPILE_NO_FLAGS);
}
updateXKBTranslationState(xkbKeymap);
updateXKBTranslationState(m_xkbKeymap);
const auto NUMLOCKON = g_pConfigManager->getDeviceInt(hlName, "numlock_by_default", "input:numlock_by_default");
const auto NUMLOCKON = g_pConfigManager->getDeviceInt(m_hlName, "numlock_by_default", "input:numlock_by_default");
if (NUMLOCKON == 1) {
// lock numlock
const auto IDX = xkb_map_mod_get_index(xkbKeymap, XKB_MOD_NAME_NUM);
const auto IDX = xkb_map_mod_get_index(m_xkbKeymap, XKB_MOD_NAME_NUM);
if (IDX != XKB_MOD_INVALID)
modifiersState.locked |= (uint32_t)1 << IDX;
m_modifiersState.locked |= (uint32_t)1 << IDX;
// 0 to avoid mods getting stuck if depressed during reload
updateModifiers(0, 0, modifiersState.locked, modifiersState.group);
updateModifiers(0, 0, m_modifiersState.locked, m_modifiersState.group);
}
for (size_t i = 0; i < std::min(LEDNAMES.size(), ledIndexes.size()); ++i) {
ledIndexes[i] = xkb_map_led_get_index(xkbKeymap, LEDNAMES[i]);
Debug::log(LOG, "xkb: LED index {} (name {}) got index {}", i, LEDNAMES[i], ledIndexes[i]);
for (size_t i = 0; i < std::min(LEDNAMES.size(), m_ledIndexes.size()); ++i) {
m_ledIndexes[i] = xkb_map_led_get_index(m_xkbKeymap, LEDNAMES[i]);
Debug::log(LOG, "xkb: LED index {} (name {}) got index {}", i, LEDNAMES[i], m_ledIndexes[i]);
}
for (size_t i = 0; i < std::min(MODNAMES.size(), modIndexes.size()); ++i) {
modIndexes[i] = xkb_map_mod_get_index(xkbKeymap, MODNAMES[i]);
Debug::log(LOG, "xkb: Mod index {} (name {}) got index {}", i, MODNAMES[i], modIndexes[i]);
for (size_t i = 0; i < std::min(MODNAMES.size(), m_modIndexes.size()); ++i) {
m_modIndexes[i] = xkb_map_mod_get_index(m_xkbKeymap, MODNAMES[i]);
Debug::log(LOG, "xkb: Mod index {} (name {}) got index {}", i, MODNAMES[i], m_modIndexes[i]);
}
updateKeymapFD();
@ -144,59 +144,59 @@ void IKeyboard::setKeymap(const SStringRuleNames& rules) {
}
void IKeyboard::updateKeymapFD() {
Debug::log(LOG, "Updating keymap fd for keyboard {}", deviceName);
Debug::log(LOG, "Updating keymap fd for keyboard {}", m_deviceName);
if (xkbKeymapFD.isValid())
xkbKeymapFD.reset();
if (m_xkbKeymapFD.isValid())
m_xkbKeymapFD.reset();
auto cKeymapStr = xkb_keymap_get_as_string(xkbKeymap, XKB_KEYMAP_FORMAT_TEXT_V1);
xkbKeymapString = cKeymapStr;
auto cKeymapStr = xkb_keymap_get_as_string(m_xkbKeymap, XKB_KEYMAP_FORMAT_TEXT_V1);
m_xkbKeymapString = cKeymapStr;
free(cKeymapStr);
CFileDescriptor rw, ro;
if (!allocateSHMFilePair(xkbKeymapString.length() + 1, rw, ro))
if (!allocateSHMFilePair(m_xkbKeymapString.length() + 1, rw, ro))
Debug::log(ERR, "IKeyboard: failed to allocate shm pair for the keymap");
else {
auto keymapFDDest = mmap(nullptr, xkbKeymapString.length() + 1, PROT_READ | PROT_WRITE, MAP_SHARED, rw.get(), 0);
auto keymapFDDest = mmap(nullptr, m_xkbKeymapString.length() + 1, PROT_READ | PROT_WRITE, MAP_SHARED, rw.get(), 0);
rw.reset();
if (keymapFDDest == MAP_FAILED) {
Debug::log(ERR, "IKeyboard: failed to mmap a shm pair for the keymap");
ro.reset();
} else {
memcpy(keymapFDDest, xkbKeymapString.c_str(), xkbKeymapString.length());
munmap(keymapFDDest, xkbKeymapString.length() + 1);
xkbKeymapFD = std::move(ro);
memcpy(keymapFDDest, m_xkbKeymapString.c_str(), m_xkbKeymapString.length());
munmap(keymapFDDest, m_xkbKeymapString.length() + 1);
m_xkbKeymapFD = std::move(ro);
}
}
Debug::log(LOG, "Updated keymap fd to {}", xkbKeymapFD.get());
Debug::log(LOG, "Updated keymap fd to {}", m_xkbKeymapFD.get());
}
void IKeyboard::updateXKBTranslationState(xkb_keymap* const keymap) {
if (xkbStaticState)
xkb_state_unref(xkbStaticState);
if (m_xkbStaticState)
xkb_state_unref(m_xkbStaticState);
if (xkbState)
xkb_state_unref(xkbState);
if (m_xkbState)
xkb_state_unref(m_xkbState);
if (xkbSymState)
xkb_state_unref(xkbSymState);
if (m_xkbSymState)
xkb_state_unref(m_xkbSymState);
xkbState = nullptr;
xkbStaticState = nullptr;
xkbSymState = nullptr;
m_xkbState = nullptr;
m_xkbStaticState = nullptr;
m_xkbSymState = nullptr;
if (keymap) {
Debug::log(LOG, "Updating keyboard {:x}'s translation state from a provided keymap", (uintptr_t)this);
xkbStaticState = xkb_state_new(keymap);
xkbState = xkb_state_new(keymap);
xkbSymState = xkb_state_new(keymap);
m_xkbStaticState = xkb_state_new(keymap);
m_xkbState = xkb_state_new(keymap);
m_xkbSymState = xkb_state_new(keymap);
return;
}
const auto KEYMAP = xkbKeymap;
const auto STATE = xkbState;
const auto KEYMAP = m_xkbKeymap;
const auto STATE = m_xkbState;
const auto LAYOUTSNUM = xkb_keymap_num_layouts(KEYMAP);
const auto PCONTEXT = xkb_context_new(XKB_CONTEXT_NO_FLAGS);
@ -205,9 +205,9 @@ void IKeyboard::updateXKBTranslationState(xkb_keymap* const keymap) {
if (xkb_state_layout_index_is_active(STATE, i, XKB_STATE_LAYOUT_EFFECTIVE) == 1) {
Debug::log(LOG, "Updating keyboard {:x}'s translation state from an active index {}", (uintptr_t)this, i);
CVarList keyboardLayouts(currentRules.layout, 0, ',');
CVarList keyboardModels(currentRules.model, 0, ',');
CVarList keyboardVariants(currentRules.variant, 0, ',');
CVarList keyboardLayouts(m_currentRules.layout, 0, ',');
CVarList keyboardModels(m_currentRules.model, 0, ',');
CVarList keyboardVariants(m_currentRules.variant, 0, ',');
xkb_rule_names rules = {.rules = "", .model = "", .layout = "", .variant = "", .options = ""};
@ -235,9 +235,9 @@ void IKeyboard::updateXKBTranslationState(xkb_keymap* const keymap) {
KEYMAP = xkb_keymap_new_from_names(PCONTEXT, &rules, XKB_KEYMAP_COMPILE_NO_FLAGS);
}
xkbState = xkb_state_new(KEYMAP);
xkbStaticState = xkb_state_new(KEYMAP);
xkbSymState = xkb_state_new(KEYMAP);
m_xkbState = xkb_state_new(KEYMAP);
m_xkbStaticState = xkb_state_new(KEYMAP);
m_xkbSymState = xkb_state_new(KEYMAP);
xkb_keymap_unref(KEYMAP);
xkb_context_unref(PCONTEXT);
@ -249,26 +249,26 @@ void IKeyboard::updateXKBTranslationState(xkb_keymap* const keymap) {
Debug::log(LOG, "Updating keyboard {:x}'s translation state from an unknown index", (uintptr_t)this);
xkb_rule_names rules = {
.rules = currentRules.rules.c_str(),
.model = currentRules.model.c_str(),
.layout = currentRules.layout.c_str(),
.variant = currentRules.variant.c_str(),
.options = currentRules.options.c_str(),
.rules = m_currentRules.rules.c_str(),
.model = m_currentRules.model.c_str(),
.layout = m_currentRules.layout.c_str(),
.variant = m_currentRules.variant.c_str(),
.options = m_currentRules.options.c_str(),
};
const auto NEWKEYMAP = xkb_keymap_new_from_names(PCONTEXT, &rules, XKB_KEYMAP_COMPILE_NO_FLAGS);
xkbState = xkb_state_new(NEWKEYMAP);
xkbStaticState = xkb_state_new(NEWKEYMAP);
xkbSymState = xkb_state_new(NEWKEYMAP);
m_xkbState = xkb_state_new(NEWKEYMAP);
m_xkbStaticState = xkb_state_new(NEWKEYMAP);
m_xkbSymState = xkb_state_new(NEWKEYMAP);
xkb_keymap_unref(NEWKEYMAP);
xkb_context_unref(PCONTEXT);
}
std::string IKeyboard::getActiveLayout() {
const auto KEYMAP = xkbKeymap;
const auto STATE = xkbState;
const auto KEYMAP = m_xkbKeymap;
const auto STATE = m_xkbState;
const auto LAYOUTSNUM = xkb_keymap_num_layouts(KEYMAP);
for (uint32_t i = 0; i < LAYOUTSNUM; ++i) {
@ -285,12 +285,12 @@ std::string IKeyboard::getActiveLayout() {
}
std::optional<uint32_t> IKeyboard::getLEDs() {
if (xkbState == nullptr)
if (m_xkbState == nullptr)
return {};
uint32_t leds = 0;
for (uint32_t i = 0; i < std::min((size_t)LED_COUNT, ledIndexes.size()); ++i) {
if (xkb_state_led_index_is_active(xkbState, ledIndexes[i]))
for (uint32_t i = 0; i < std::min((size_t)LED_COUNT, m_ledIndexes.size()); ++i) {
if (xkb_state_led_index_is_active(m_xkbState, m_ledIndexes[i]))
leds |= (1 << i);
}
@ -307,10 +307,10 @@ void IKeyboard::updateLEDs() {
}
void IKeyboard::updateLEDs(uint32_t leds) {
if (!xkbState)
if (!m_xkbState)
return;
if (isVirtual() && g_pInputManager->shouldIgnoreVirtualKeyboard(self.lock()))
if (isVirtual() && g_pInputManager->shouldIgnoreVirtualKeyboard(m_self.lock()))
return;
if (!aq())
@ -320,13 +320,13 @@ void IKeyboard::updateLEDs(uint32_t leds) {
}
uint32_t IKeyboard::getModifiers() {
uint32_t modMask = modifiersState.depressed | modifiersState.latched;
uint32_t modMask = m_modifiersState.depressed | m_modifiersState.latched;
uint32_t mods = 0;
for (size_t i = 0; i < modIndexes.size(); ++i) {
if (modIndexes[i] == XKB_MOD_INVALID)
for (size_t i = 0; i < m_modIndexes.size(); ++i) {
if (m_modIndexes[i] == XKB_MOD_INVALID)
continue;
if (!(modMask & (1 << modIndexes[i])))
if (!(modMask & (1 << m_modIndexes[i])))
continue;
mods |= (1 << i);
@ -336,50 +336,50 @@ uint32_t IKeyboard::getModifiers() {
}
void IKeyboard::updateModifiers(uint32_t depressed, uint32_t latched, uint32_t locked, uint32_t group) {
if (!xkbState)
if (!m_xkbState)
return;
xkb_state_update_mask(xkbState, depressed, latched, locked, 0, 0, group);
xkb_state_update_mask(m_xkbState, depressed, latched, locked, 0, 0, group);
if (xkbSymState)
xkb_state_update_mask(xkbSymState, 0, 0, 0, 0, 0, group);
if (m_xkbSymState)
xkb_state_update_mask(m_xkbSymState, 0, 0, 0, 0, 0, group);
if (!updateModifiersState())
return;
keyboardEvents.modifiers.emit(SModifiersEvent{
.depressed = modifiersState.depressed,
.latched = modifiersState.latched,
.locked = modifiersState.locked,
.group = modifiersState.group,
m_keyboardEvents.modifiers.emit(SModifiersEvent{
.depressed = m_modifiersState.depressed,
.latched = m_modifiersState.latched,
.locked = m_modifiersState.locked,
.group = m_modifiersState.group,
});
updateLEDs();
}
bool IKeyboard::updateModifiersState() {
if (!xkbState)
if (!m_xkbState)
return false;
auto depressed = xkb_state_serialize_mods(xkbState, XKB_STATE_MODS_DEPRESSED);
auto latched = xkb_state_serialize_mods(xkbState, XKB_STATE_MODS_LATCHED);
auto locked = xkb_state_serialize_mods(xkbState, XKB_STATE_MODS_LOCKED);
auto group = xkb_state_serialize_layout(xkbState, XKB_STATE_LAYOUT_EFFECTIVE);
auto depressed = xkb_state_serialize_mods(m_xkbState, XKB_STATE_MODS_DEPRESSED);
auto latched = xkb_state_serialize_mods(m_xkbState, XKB_STATE_MODS_LATCHED);
auto locked = xkb_state_serialize_mods(m_xkbState, XKB_STATE_MODS_LOCKED);
auto group = xkb_state_serialize_layout(m_xkbState, XKB_STATE_LAYOUT_EFFECTIVE);
if (depressed == modifiersState.depressed && latched == modifiersState.latched && locked == modifiersState.locked && group == modifiersState.group)
if (depressed == m_modifiersState.depressed && latched == m_modifiersState.latched && locked == m_modifiersState.locked && group == m_modifiersState.group)
return false;
modifiersState.depressed = depressed;
modifiersState.latched = latched;
modifiersState.locked = locked;
modifiersState.group = group;
m_modifiersState.depressed = depressed;
m_modifiersState.latched = latched;
m_modifiersState.locked = locked;
m_modifiersState.group = group;
return true;
}
void IKeyboard::updateXkbStateWithKey(uint32_t xkbKey, bool pressed) {
const auto contains = std::find(pressedXKB.begin(), pressedXKB.end(), xkbKey) != pressedXKB.end();
const auto contains = std::find(m_pressedXKB.begin(), m_pressedXKB.end(), xkbKey) != m_pressedXKB.end();
if (contains && pressed)
return;
@ -387,21 +387,21 @@ void IKeyboard::updateXkbStateWithKey(uint32_t xkbKey, bool pressed) {
return;
if (contains)
std::erase(pressedXKB, xkbKey);
std::erase(m_pressedXKB, xkbKey);
else
pressedXKB.emplace_back(xkbKey);
m_pressedXKB.emplace_back(xkbKey);
xkb_state_update_key(xkbState, xkbKey, pressed ? XKB_KEY_DOWN : XKB_KEY_UP);
xkb_state_update_key(m_xkbState, xkbKey, pressed ? XKB_KEY_DOWN : XKB_KEY_UP);
if (updateModifiersState()) {
if (xkbSymState)
xkb_state_update_mask(xkbSymState, 0, 0, 0, 0, 0, modifiersState.group);
if (m_xkbSymState)
xkb_state_update_mask(m_xkbSymState, 0, 0, 0, 0, 0, m_modifiersState.group);
keyboardEvents.modifiers.emit(SModifiersEvent{
.depressed = modifiersState.depressed,
.latched = modifiersState.latched,
.locked = modifiersState.locked,
.group = modifiersState.group,
m_keyboardEvents.modifiers.emit(SModifiersEvent{
.depressed = m_modifiersState.depressed,
.latched = m_modifiersState.latched,
.locked = m_modifiersState.locked,
.group = m_modifiersState.group,
});
}
}