label: fix crashes when keymap is a nullptr after suspend (#699)

This commit is contained in:
Maximilian Seidler 2025-03-06 08:37:43 +01:00 committed by GitHub
parent cb1c504b38
commit 78ad1d46b5
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 33 additions and 7 deletions

View file

@ -134,6 +134,25 @@ void CSeatManager::registerCursorShape(SP<CCWpCursorShapeManagerV1> shape) {
m_pCursorShape = makeUnique<CCursorShape>(shape); m_pCursorShape = makeUnique<CCursorShape>(shape);
} }
std::string CSeatManager::getActiveKbLayoutName() {
if (!m_pXKBState || !m_pXKBKeymap)
return "error";
const auto LAYOUTSNUM = xkb_keymap_num_layouts(m_pXKBKeymap);
for (uint32_t i = 0; i < LAYOUTSNUM; ++i) {
if (xkb_state_layout_index_is_active(m_pXKBState, i, XKB_STATE_LAYOUT_EFFECTIVE) == 1) {
const auto LAYOUTNAME = xkb_keymap_layout_get_name(m_pXKBKeymap, i);
if (LAYOUTNAME)
return std::string{LAYOUTNAME};
return "error";
}
}
return "none";
}
bool CSeatManager::registered() { bool CSeatManager::registered() {
return m_pSeat; return m_pSeat;
} }

View file

@ -14,6 +14,7 @@ class CSeatManager {
void registerSeat(SP<CCWlSeat> seat); void registerSeat(SP<CCWlSeat> seat);
void registerCursorShape(SP<CCWpCursorShapeManagerV1> shape); void registerCursorShape(SP<CCWpCursorShapeManagerV1> shape);
bool registered(); bool registered();
std::string getActiveKbLayoutName();
SP<CCWlKeyboard> m_pKeeb; SP<CCWlKeyboard> m_pKeeb;
SP<CCWlPointer> m_pPointer; SP<CCWlPointer> m_pPointer;

View file

@ -67,6 +67,8 @@ class CHyprlock {
size_t getPasswordBufferLen(); size_t getPasswordBufferLen();
size_t getPasswordBufferDisplayLen(); size_t getPasswordBufferDisplayLen();
std::string getActiveKeyboardLayout();
SP<CCExtSessionLockManagerV1> getSessionLockMgr(); SP<CCExtSessionLockManagerV1> getSessionLockMgr();
SP<CCExtSessionLockV1> getSessionLock(); SP<CCExtSessionLockV1> getSessionLock();
SP<CCWlCompositor> getCompositor(); SP<CCWlCompositor> getCompositor();

View file

@ -102,20 +102,24 @@ static void replaceAllAttempts(std::string& str) {
static void replaceAllLayout(std::string& str) { static void replaceAllLayout(std::string& str) {
const auto LAYOUTIDX = g_pHyprlock->m_uiActiveLayout; const auto LAYOUTIDX = g_pHyprlock->m_uiActiveLayout;
const auto LAYOUTNAME = xkb_keymap_layout_get_name(g_pSeatManager->m_pXKBKeymap, LAYOUTIDX); const auto LAYOUTNAME = g_pSeatManager->getActiveKbLayoutName();
const std::string STR = LAYOUTNAME ? LAYOUTNAME : "error";
size_t pos = 0; size_t pos = 0;
while ((pos = str.find("$LAYOUT", pos)) != std::string::npos) { while ((pos = str.find("$LAYOUT", pos)) != std::string::npos) {
if (str.substr(pos, 8).ends_with('[') && str.substr(pos).contains(']')) { if (str.substr(pos, 8).ends_with('[') && str.substr(pos).contains(']')) {
const std::string REPL = str.substr(pos + 8, str.find_first_of(']', pos) - 8 - pos); const std::string REPL = str.substr(pos + 8, str.find_first_of(']', pos) - 8 - pos);
const CVarList LANGS(REPL); const CVarList LANGS(REPL);
const std::string LANG = LANGS[LAYOUTIDX].empty() ? STR : LANGS[LAYOUTIDX] == "!" ? "" : LANGS[LAYOUTIDX]; if (LAYOUTIDX >= LANGS.size()) {
Debug::log(ERR, "Layout index {} out of bounds. Max is {}.", LAYOUTIDX, LANGS.size() - 1);
continue;
}
const std::string LANG = LANGS[LAYOUTIDX].empty() ? LAYOUTNAME : LANGS[LAYOUTIDX] == "!" ? "" : LANGS[LAYOUTIDX];
str.replace(pos, 9 + REPL.length(), LANG); str.replace(pos, 9 + REPL.length(), LANG);
pos += LANG.length(); pos += LANG.length();
} else { } else {
str.replace(pos, 7, STR); str.replace(pos, 7, LAYOUTNAME);
pos += STR.length(); pos += LAYOUTNAME.length();
} }
} }
} }