mirror of
https://github.com/rad4day/Waybar.git
synced 2023-12-21 10:22:59 +01:00
added a basic hyprland/language module
This commit is contained in:
parent
bcee4e15d3
commit
16d5619f3b
@ -24,6 +24,7 @@
|
|||||||
#ifdef HAVE_HYPRLAND
|
#ifdef HAVE_HYPRLAND
|
||||||
#include "modules/hyprland/backend.hpp"
|
#include "modules/hyprland/backend.hpp"
|
||||||
#include "modules/hyprland/window.hpp"
|
#include "modules/hyprland/window.hpp"
|
||||||
|
#include "modules/hyprland/language.hpp"
|
||||||
#endif
|
#endif
|
||||||
#if defined(__linux__) && !defined(NO_FILESYSTEM)
|
#if defined(__linux__) && !defined(NO_FILESYSTEM)
|
||||||
#include "modules/battery.hpp"
|
#include "modules/battery.hpp"
|
||||||
|
28
include/modules/hyprland/language.hpp
Normal file
28
include/modules/hyprland/language.hpp
Normal file
@ -0,0 +1,28 @@
|
|||||||
|
#include <fmt/format.h>
|
||||||
|
|
||||||
|
#include "ALabel.hpp"
|
||||||
|
#include "bar.hpp"
|
||||||
|
#include "modules/hyprland/backend.hpp"
|
||||||
|
#include "util/json.hpp"
|
||||||
|
|
||||||
|
namespace waybar::modules::hyprland {
|
||||||
|
|
||||||
|
class Language : public waybar::ALabel {
|
||||||
|
public:
|
||||||
|
Language(const std::string&, const waybar::Bar&, const Json::Value&);
|
||||||
|
~Language() = default;
|
||||||
|
|
||||||
|
auto update() -> void;
|
||||||
|
|
||||||
|
private:
|
||||||
|
void onEvent(const std::string&);
|
||||||
|
|
||||||
|
void initLanguage();
|
||||||
|
|
||||||
|
std::mutex mutex_;
|
||||||
|
const Bar& bar_;
|
||||||
|
util::JsonParser parser_;
|
||||||
|
std::string layoutName_;
|
||||||
|
};
|
||||||
|
|
||||||
|
}
|
@ -205,6 +205,7 @@ if true
|
|||||||
add_project_arguments('-DHAVE_HYPRLAND', language: 'cpp')
|
add_project_arguments('-DHAVE_HYPRLAND', language: 'cpp')
|
||||||
src_files += 'src/modules/hyprland/backend.cpp'
|
src_files += 'src/modules/hyprland/backend.cpp'
|
||||||
src_files += 'src/modules/hyprland/window.cpp'
|
src_files += 'src/modules/hyprland/window.cpp'
|
||||||
|
src_files += 'src/modules/hyprland/language.cpp'
|
||||||
endif
|
endif
|
||||||
|
|
||||||
if libnl.found() and libnlgen.found()
|
if libnl.found() and libnlgen.found()
|
||||||
|
@ -61,6 +61,9 @@ waybar::AModule* waybar::Factory::makeModule(const std::string& name) const {
|
|||||||
if (ref == "hyprland/window") {
|
if (ref == "hyprland/window") {
|
||||||
return new waybar::modules::hyprland::Window(id, bar_, config_[name]);
|
return new waybar::modules::hyprland::Window(id, bar_, config_[name]);
|
||||||
}
|
}
|
||||||
|
if (ref == "hyprland/language") {
|
||||||
|
return new waybar::modules::hyprland::Language(id, bar_, config_[name]);
|
||||||
|
}
|
||||||
#endif
|
#endif
|
||||||
if (ref == "idle_inhibitor") {
|
if (ref == "idle_inhibitor") {
|
||||||
return new waybar::modules::IdleInhibitor(id, bar_, config_[name]);
|
return new waybar::modules::IdleInhibitor(id, bar_, config_[name]);
|
||||||
|
97
src/modules/hyprland/language.cpp
Normal file
97
src/modules/hyprland/language.cpp
Normal file
@ -0,0 +1,97 @@
|
|||||||
|
#include "modules/hyprland/language.hpp"
|
||||||
|
|
||||||
|
#include <spdlog/spdlog.h>
|
||||||
|
|
||||||
|
#include "modules/hyprland/backend.hpp"
|
||||||
|
|
||||||
|
namespace waybar::modules::hyprland {
|
||||||
|
|
||||||
|
Language::Language(const std::string& id, const Bar& bar, const Json::Value& config)
|
||||||
|
: ALabel(config, "language", id, "{}", 0, true), bar_(bar) {
|
||||||
|
modulesReady = true;
|
||||||
|
|
||||||
|
if (!gIPC.get()) {
|
||||||
|
gIPC = std::make_unique<IPC>();
|
||||||
|
}
|
||||||
|
|
||||||
|
// get the active layout when open
|
||||||
|
initLanguage();
|
||||||
|
|
||||||
|
label_.hide();
|
||||||
|
ALabel::update();
|
||||||
|
|
||||||
|
// register for hyprland ipc
|
||||||
|
gIPC->registerForIPC("activelayout", [&](const std::string& ev) { this->onEvent(ev); });
|
||||||
|
}
|
||||||
|
|
||||||
|
auto Language::update() -> void {
|
||||||
|
// fix ampersands
|
||||||
|
std::lock_guard<std::mutex> lg(mutex_);
|
||||||
|
|
||||||
|
if (!format_.empty()) {
|
||||||
|
label_.show();
|
||||||
|
label_.set_markup(fmt::format(format_, layoutName_));
|
||||||
|
} else {
|
||||||
|
label_.hide();
|
||||||
|
}
|
||||||
|
|
||||||
|
ALabel::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;
|
||||||
|
};
|
||||||
|
|
||||||
|
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());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace waybar::modules::hyprland
|
Loading…
Reference in New Issue
Block a user