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
|
|
|
|
2022-10-09 17:13:54 +00:00
|
|
|
#include <util/sanitize_str.hpp>
|
|
|
|
|
2023-03-19 15:12:11 +00:00
|
|
|
#include "util/string.hpp"
|
2022-08-18 16:00:27 +00:00
|
|
|
|
|
|
|
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_);
|
|
|
|
|
2023-03-19 15:12:11 +00:00
|
|
|
std::string layoutName = std::string{};
|
|
|
|
if (config_.isMember("format-" + layout_.short_description)) {
|
|
|
|
const auto propName = "format-" + layout_.short_description;
|
|
|
|
layoutName = fmt::format(fmt::runtime(format_), config_[propName].asString());
|
|
|
|
} else {
|
|
|
|
layoutName = trim(fmt::format(fmt::runtime(format_), fmt::arg("long", layout_.full_name),
|
|
|
|
fmt::arg("short", layout_.short_name),
|
|
|
|
fmt::arg("shortDescription", layout_.short_description),
|
|
|
|
fmt::arg("variant", layout_.variant)));
|
|
|
|
}
|
|
|
|
|
2022-08-18 16:00:27 +00:00
|
|
|
if (!format_.empty()) {
|
2022-11-24 11:28:52 +00:00
|
|
|
label_.show();
|
2023-03-19 15:12:11 +00:00
|
|
|
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_);
|
2023-01-29 12:14:05 +00:00
|
|
|
std::string kbName(begin(ev) + ev.find_last_of('>') + 1, begin(ev) + ev.find_first_of(','));
|
2022-12-27 14:28:20 +00:00
|
|
|
auto layoutName = ev.substr(ev.find_first_of(',') + 1);
|
2022-08-18 16:00:27 +00:00
|
|
|
|
2022-12-16 09:01:58 +00:00
|
|
|
if (config_.isMember("keyboard-name") && kbName != config_["keyboard-name"].asString())
|
2022-08-18 16:04:39 +00:00
|
|
|
return; // ignore
|
2022-08-18 16:00:27 +00:00
|
|
|
|
2023-01-13 15:28:34 +00:00
|
|
|
layoutName = waybar::util::sanitize_string(layoutName);
|
|
|
|
|
2023-03-21 19:30:35 +00:00
|
|
|
layout_ = getLayout(layoutName);
|
2022-08-18 16:00:27 +00:00
|
|
|
|
|
|
|
spdlog::debug("hyprland language onevent with {}", layoutName);
|
|
|
|
|
|
|
|
dp.emit();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Language::initLanguage() {
|
2022-12-16 09:01:58 +00:00
|
|
|
const auto inputDevices = gIPC->getSocket1Reply("devices");
|
2022-08-18 16:00:27 +00:00
|
|
|
|
2022-12-16 09:01:58 +00:00
|
|
|
const auto kbName = config_["keyboard-name"].asString();
|
2022-08-18 16:00:27 +00:00
|
|
|
|
2022-08-18 16:04:39 +00:00
|
|
|
try {
|
2022-12-16 09:01:58 +00:00
|
|
|
auto searcher = kbName.empty()
|
|
|
|
? inputDevices
|
|
|
|
: inputDevices.substr(inputDevices.find(kbName) + kbName.length());
|
2022-12-15 00:48:14 +00:00
|
|
|
searcher = searcher.substr(searcher.find("keymap:") + 8);
|
2022-08-18 16:04:39 +00:00
|
|
|
searcher = searcher.substr(0, searcher.find_first_of("\n\t"));
|
2022-08-18 16:00:27 +00:00
|
|
|
|
2023-01-13 15:28:34 +00:00
|
|
|
searcher = waybar::util::sanitize_string(searcher);
|
|
|
|
|
2023-03-21 19:30:35 +00:00
|
|
|
layout_ = getLayout(searcher);
|
2022-12-15 00:48:14 +00:00
|
|
|
|
2023-03-19 15:12:11 +00:00
|
|
|
spdlog::debug("hyprland language initLanguage found {}", layout_.full_name);
|
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
|
|
|
}
|
|
|
|
|
2023-03-21 19:30:35 +00:00
|
|
|
auto Language::getLayout(const std::string& fullName) -> Layout {
|
2022-08-18 16:59:34 +00:00
|
|
|
const auto CONTEXT = rxkb_context_new(RXKB_CONTEXT_LOAD_EXOTIC_RULES);
|
|
|
|
rxkb_context_parse_default_ruleset(CONTEXT);
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2023-03-19 15:12:11 +00:00
|
|
|
auto name = std::string(rxkb_layout_get_name(layout));
|
|
|
|
auto variant_ = rxkb_layout_get_variant(layout);
|
|
|
|
std::string variant = variant_ == nullptr ? "" : std::string(variant_);
|
|
|
|
|
|
|
|
auto short_description_ = rxkb_layout_get_brief(layout);
|
|
|
|
std::string short_description =
|
|
|
|
short_description_ == nullptr ? "" : std::string(short_description_);
|
|
|
|
|
2023-03-21 19:30:35 +00:00
|
|
|
Layout info = Layout{nameOfLayout, name, variant, short_description};
|
2022-08-18 16:59:34 +00:00
|
|
|
|
|
|
|
rxkb_context_unref(CONTEXT);
|
|
|
|
|
2023-03-19 15:12:11 +00:00
|
|
|
return info;
|
2022-08-18 16:59:34 +00:00
|
|
|
}
|
2022-08-18 17:02:46 +00:00
|
|
|
|
2022-08-18 16:59:34 +00:00
|
|
|
rxkb_context_unref(CONTEXT);
|
|
|
|
|
2023-03-21 19:30:35 +00:00
|
|
|
spdlog::debug("hyprland language didn't find matching layout");
|
|
|
|
|
|
|
|
return Layout {"", "", "", ""};
|
2022-08-18 16:59:34 +00:00
|
|
|
}
|
|
|
|
|
2021-05-27 13:40:54 +00:00
|
|
|
} // namespace waybar::modules::hyprland
|