aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorVaxry <[email protected]>2024-08-28 14:05:31 +0200
committerVaxry <[email protected]>2024-08-28 14:05:31 +0200
commitd105c7403c2b700e4572149ecadd21e1d1ad24d3 (patch)
tree3ad5d2d434ccd6348cfa0287f563ea53da73e47e
parent00ee1cf98e3d8ef7cf71a9a505ba8d1382697e35 (diff)
downloadHyprland-d105c7403c2b700e4572149ecadd21e1d1ad24d3.tar.gz
Hyprland-d105c7403c2b700e4572149ecadd21e1d1ad24d3.zip
hyprctl: add next and all to switchxkblayout
fixes #7555
-rw-r--r--src/debug/HyprCtl.cpp87
1 files changed, 59 insertions, 28 deletions
diff --git a/src/debug/HyprCtl.cpp b/src/debug/HyprCtl.cpp
index ddd1c1ab..683665be 100644
--- a/src/debug/HyprCtl.cpp
+++ b/src/debug/HyprCtl.cpp
@@ -1136,46 +1136,77 @@ std::string dispatchSetCursor(eHyprCtlOutputFormat format, std::string request)
}
std::string switchXKBLayoutRequest(eHyprCtlOutputFormat format, std::string request) {
- CVarList vars(request, 0, ' ');
+ CVarList vars(request, 0, ' ');
- const auto KB = vars[1];
- const auto CMD = vars[2];
+ const auto KB = vars[1];
+ const auto CMD = vars[2];
- // get kb
- const auto PKEYBOARD = std::find_if(g_pInputManager->m_vKeyboards.begin(), g_pInputManager->m_vKeyboards.end(),
- [&](const auto& other) { return other->hlName == g_pInputManager->deviceNameToInternalString(KB); });
+ SP<IKeyboard> pKeyboard;
- if (PKEYBOARD == g_pInputManager->m_vKeyboards.end())
- return "device not found";
+ auto updateKeyboard = [](const SP<IKeyboard> KEEB, const std::string& CMD) -> std::optional<std::string> {
+ const auto LAYOUTS = xkb_keymap_num_layouts(KEEB->xkbKeymap);
+ xkb_layout_index_t activeLayout = 0;
+ while (activeLayout < LAYOUTS) {
+ if (xkb_state_layout_index_is_active(KEEB->xkbState, activeLayout, XKB_STATE_LAYOUT_EFFECTIVE) == 1)
+ break;
- const auto KEEB = *PKEYBOARD;
+ activeLayout++;
+ }
- const auto LAYOUTS = xkb_keymap_num_layouts(KEEB->xkbKeymap);
- xkb_layout_index_t activeLayout = 0;
- while (activeLayout < LAYOUTS) {
- if (xkb_state_layout_index_is_active(KEEB->xkbState, activeLayout, XKB_STATE_LAYOUT_EFFECTIVE) == 1)
- break;
+ if (CMD == "next")
+ KEEB->updateModifiers(KEEB->modifiersState.depressed, KEEB->modifiersState.latched, KEEB->modifiersState.locked, activeLayout > LAYOUTS ? 0 : activeLayout + 1);
+ else if (CMD == "prev")
+ KEEB->updateModifiers(KEEB->modifiersState.depressed, KEEB->modifiersState.latched, KEEB->modifiersState.locked, activeLayout == 0 ? LAYOUTS - 1 : activeLayout - 1);
+ else {
+ int requestedLayout = 0;
+ try {
+ requestedLayout = std::stoi(CMD);
+ } catch (std::exception& e) { return "invalid arg 2"; }
+
+ if (requestedLayout < 0 || (uint64_t)requestedLayout > LAYOUTS - 1) {
+ return "layout idx out of range of " + std::to_string(LAYOUTS);
+ }
- activeLayout++;
- }
+ KEEB->updateModifiers(KEEB->modifiersState.depressed, KEEB->modifiersState.latched, KEEB->modifiersState.locked, requestedLayout);
+ }
- if (CMD == "next")
- KEEB->updateModifiers(KEEB->modifiersState.depressed, KEEB->modifiersState.latched, KEEB->modifiersState.locked, activeLayout > LAYOUTS ? 0 : activeLayout + 1);
- else if (CMD == "prev")
- KEEB->updateModifiers(KEEB->modifiersState.depressed, KEEB->modifiersState.latched, KEEB->modifiersState.locked, activeLayout == 0 ? LAYOUTS - 1 : activeLayout - 1);
- else {
- int requestedLayout = 0;
- try {
- requestedLayout = std::stoi(CMD);
- } catch (std::exception& e) { return "invalid arg 2"; }
+ return std::nullopt;
+ };
+
+ if (KB == "main" || KB == "active" || KB == "current") {
+ for (auto const& k : g_pInputManager->m_vKeyboards) {
+ if (!k->active)
+ continue;
- if (requestedLayout < 0 || (uint64_t)requestedLayout > LAYOUTS - 1) {
- return "layout idx out of range of " + std::to_string(LAYOUTS);
+ pKeyboard = k;
+ break;
+ }
+ } else if (KB == "all") {
+ std::string result = "";
+ for (auto const& k : g_pInputManager->m_vKeyboards) {
+ auto res = updateKeyboard(k, CMD);
+ if (res.has_value())
+ result += *res + "\n";
}
+ return result.empty() ? "ok" : result;
+ } else {
+ auto k = std::find_if(g_pInputManager->m_vKeyboards.begin(), g_pInputManager->m_vKeyboards.end(),
+ [&](const auto& other) { return other->hlName == g_pInputManager->deviceNameToInternalString(KB); });
+
+ if (k == g_pInputManager->m_vKeyboards.end())
+ return "device not found";
- KEEB->updateModifiers(KEEB->modifiersState.depressed, KEEB->modifiersState.latched, KEEB->modifiersState.locked, requestedLayout);
+ pKeyboard = *k;
}
+ if (!pKeyboard)
+ return "no device";
+
+ auto result = updateKeyboard(pKeyboard, CMD);
+
+ if (result.has_value())
+ return *result;
+
return "ok";
}