Waybar/src/modules/hyprland/language.cpp

129 lines
3.2 KiB
C++
Raw Normal View History

2022-08-18 16:00:27 +00:00
#include "modules/hyprland/language.hpp"
#include <spdlog/spdlog.h>
2022-08-18 16:59:34 +00:00
#include <xkbcommon/xkbcommon.h>
2022-08-18 17:02:46 +00:00
#include <xkbcommon/xkbregistry.h>
2022-08-18 16:00:27 +00:00
#include <util/sanitize_str.hpp>
2022-08-18 16:00:27 +00:00
#include "modules/hyprland/backend.hpp"
namespace waybar::modules::hyprland {
Language::Language(const std::string& id, const Bar& bar, const Json::Value& config)
2022-11-24 11:28:52 +00:00
: ALabel(config, "language", id, "{}", 0, true), bar_(bar) {
2022-08-18 16:00:27 +00:00
modulesReady = true;
if (!gIPC.get()) {
gIPC = std::make_unique<IPC>();
}
// get the active layout when open
initLanguage();
2022-11-24 11:28:52 +00:00
label_.hide();
ALabel::update();
2022-08-18 16:00:27 +00:00
// register for hyprland ipc
2022-11-09 08:34:19 +00:00
gIPC->registerForIPC("activelayout", this);
}
Language::~Language() {
gIPC->unregisterForIPC(this);
// wait for possible event handler to finish
std::lock_guard<std::mutex> lg(mutex_);
2022-08-18 16:00:27 +00:00
}
auto Language::update() -> void {
std::lock_guard<std::mutex> lg(mutex_);
if (!format_.empty()) {
2022-11-24 11:28:52 +00:00
label_.show();
label_.set_markup(layoutName_);
2022-08-18 16:00:27 +00:00
} else {
2022-11-24 11:28:52 +00:00
label_.hide();
2022-08-18 16:00:27 +00:00
}
2022-11-24 11:28:52 +00:00
ALabel::update();
2022-08-18 16:00:27 +00:00
}
void Language::onEvent(const std::string& ev) {
std::lock_guard<std::mutex> lg(mutex_);
auto layoutName = ev.substr(ev.find_last_of(',') + 1);
auto keebName = ev.substr(0, ev.find_last_of(','));
keebName = keebName.substr(keebName.find_first_of('>') + 2);
if (config_.isMember("keyboard-name") && keebName != config_["keyboard-name"].asString())
2022-08-18 16:04:39 +00:00
return; // ignore
2022-08-18 16:00:27 +00:00
2022-08-18 16:59:34 +00:00
const auto BRIEFNAME = getShortFrom(layoutName);
if (config_.isMember("format-" + BRIEFNAME)) {
const auto PROPNAME = "format-" + BRIEFNAME;
layoutName = fmt::format(format_, config_[PROPNAME].asString());
} else {
layoutName = fmt::format(format_, layoutName);
}
layoutName = waybar::util::sanitize_string(layoutName);
2022-08-18 16:00:27 +00:00
if (layoutName == layoutName_) return;
layoutName_ = layoutName;
spdlog::debug("hyprland language onevent with {}", layoutName);
dp.emit();
}
void Language::initLanguage() {
2022-08-18 16:04:39 +00:00
const auto INPUTDEVICES = gIPC->getSocket1Reply("devices");
2022-08-18 16:00:27 +00:00
2022-08-18 16:05:40 +00:00
if (!config_.isMember("keyboard-name")) return;
2022-08-18 16:00:27 +00:00
2022-08-18 16:04:39 +00:00
const auto KEEBNAME = config_["keyboard-name"].asString();
2022-08-18 16:00:27 +00:00
2022-08-18 16:04:39 +00:00
try {
auto searcher = INPUTDEVICES.substr(INPUTDEVICES.find(KEEBNAME) + KEEBNAME.length());
searcher = searcher.substr(searcher.find("keymap:") + 7);
searcher = searcher.substr(0, searcher.find_first_of("\n\t"));
2022-08-18 16:00:27 +00:00
2022-08-18 16:04:39 +00:00
layoutName_ = searcher;
2022-08-18 16:00:27 +00:00
2022-08-18 16:04:39 +00:00
spdlog::debug("hyprland language initLanguage found {}", layoutName_);
2022-08-18 16:00:27 +00:00
2022-08-18 16:04:39 +00:00
dp.emit();
2022-08-18 16:00:27 +00:00
2022-08-18 16:04:39 +00:00
} catch (std::exception& e) {
spdlog::error("hyprland language initLanguage failed with {}", e.what());
}
2022-08-18 16:00:27 +00:00
}
2022-08-18 16:59:34 +00:00
std::string Language::getShortFrom(const std::string& fullName) {
const auto CONTEXT = rxkb_context_new(RXKB_CONTEXT_LOAD_EXOTIC_RULES);
rxkb_context_parse_default_ruleset(CONTEXT);
std::string foundName = "";
rxkb_layout* layout = rxkb_layout_first(CONTEXT);
while (layout) {
std::string nameOfLayout = rxkb_layout_get_description(layout);
if (nameOfLayout != fullName) {
layout = rxkb_layout_next(layout);
continue;
}
std::string briefName = rxkb_layout_get_brief(layout);
rxkb_context_unref(CONTEXT);
return briefName;
}
2022-08-18 17:02:46 +00:00
2022-08-18 16:59:34 +00:00
rxkb_context_unref(CONTEXT);
return "";
}
} // namespace waybar::modules::hyprland