The AButton class is designed as full a substitute to ALabel. The GtkButton attribute 'button_' is initialized with a label. This label can the be referenced by the subsequent inheritors of AButton instead of the GtkLabel attribute 'label_' of ALabel. For convenience a GtkLabel* 'label_' attribute is added to AButton. If the button cannot be clicked it is disabled, effectively acting like its label predecessor. GtkButton seems to catch one-click mouse events regardless of the flags set on it. Therefore, 'signal_pressed' is connected to a function creating a fake GdkEventButton* and calling 'handleToggle' (for details on this possible bug in GTK see: https://stackoverflow.com/questions/45334911 ) In accordance with other GtkButtons (i.e. the sway/workspace ones) set_relief(Gtk::RELIEF_NONE) is called on the 'button_' instance.
132 lines
3.4 KiB
C++
132 lines
3.4 KiB
C++
#include "modules/hyprland/language.hpp"
|
|
|
|
#include <spdlog/spdlog.h>
|
|
#include <xkbcommon/xkbcommon.h>
|
|
#include <xkbcommon/xkbregistry.h>
|
|
|
|
#include "modules/hyprland/backend.hpp"
|
|
|
|
namespace waybar::modules::hyprland {
|
|
|
|
Language::Language(const std::string& id, const Bar& bar, const Json::Value& config)
|
|
: AButton(config, "language", id, "{}", 0, true), bar_(bar) {
|
|
modulesReady = true;
|
|
|
|
if (!gIPC.get()) {
|
|
gIPC = std::make_unique<IPC>();
|
|
}
|
|
|
|
// get the active layout when open
|
|
initLanguage();
|
|
|
|
button_.hide();
|
|
AButton::update();
|
|
|
|
// register for hyprland ipc
|
|
gIPC->registerForIPC("activelayout", [&](const std::string& ev) { this->onEvent(ev); });
|
|
}
|
|
|
|
auto Language::update() -> void {
|
|
std::lock_guard<std::mutex> lg(mutex_);
|
|
|
|
if (!format_.empty()) {
|
|
button_.show();
|
|
label_->set_markup(layoutName_);
|
|
} else {
|
|
button_.hide();
|
|
}
|
|
|
|
AButton::update();
|
|
}
|
|
|
|
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())
|
|
return; // ignore
|
|
|
|
auto replaceAll = [](std::string str, const std::string& from,
|
|
const std::string& to) -> std::string {
|
|
size_t start_pos = 0;
|
|
while ((start_pos = str.find(from, start_pos)) != std::string::npos) {
|
|
str.replace(start_pos, from.length(), to);
|
|
start_pos += to.length();
|
|
}
|
|
return str;
|
|
};
|
|
|
|
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 = replaceAll(layoutName, "&", "&");
|
|
|
|
if (layoutName == layoutName_) return;
|
|
|
|
layoutName_ = layoutName;
|
|
|
|
spdlog::debug("hyprland language onevent with {}", layoutName);
|
|
|
|
dp.emit();
|
|
}
|
|
|
|
void Language::initLanguage() {
|
|
const auto INPUTDEVICES = gIPC->getSocket1Reply("devices");
|
|
|
|
if (!config_.isMember("keyboard-name")) return;
|
|
|
|
const auto KEEBNAME = config_["keyboard-name"].asString();
|
|
|
|
try {
|
|
auto searcher = INPUTDEVICES.substr(INPUTDEVICES.find(KEEBNAME) + KEEBNAME.length());
|
|
searcher = searcher.substr(searcher.find("Keyboard at"));
|
|
searcher = searcher.substr(searcher.find("keymap:") + 7);
|
|
searcher = searcher.substr(0, searcher.find_first_of("\n\t"));
|
|
|
|
layoutName_ = searcher;
|
|
|
|
spdlog::debug("hyprland language initLanguage found {}", layoutName_);
|
|
|
|
dp.emit();
|
|
|
|
} catch (std::exception& e) {
|
|
spdlog::error("hyprland language initLanguage failed with {}", e.what());
|
|
}
|
|
}
|
|
|
|
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;
|
|
}
|
|
|
|
rxkb_context_unref(CONTEXT);
|
|
|
|
return "";
|
|
}
|
|
|
|
} // namespace waybar::modules::hyprland
|