2020-10-10 16:09:18 +00:00
|
|
|
#include "modules/sway/language.hpp"
|
2021-07-13 01:33:12 +00:00
|
|
|
|
|
|
|
#include <fmt/core.h>
|
2021-07-24 14:24:37 +00:00
|
|
|
#include <json/json.h>
|
2020-10-10 16:09:18 +00:00
|
|
|
#include <spdlog/spdlog.h>
|
2021-07-13 01:33:12 +00:00
|
|
|
#include <xkbcommon/xkbregistry.h>
|
|
|
|
|
|
|
|
#include <cstring>
|
|
|
|
#include <string>
|
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
#include "modules/sway/ipc/ipc.hpp"
|
|
|
|
#include "util/string.hpp"
|
2020-10-10 16:09:18 +00:00
|
|
|
|
|
|
|
namespace waybar::modules::sway {
|
|
|
|
|
2021-07-13 01:33:12 +00:00
|
|
|
const std::string Language::XKB_LAYOUT_NAMES_KEY = "xkb_layout_names";
|
|
|
|
const std::string Language::XKB_ACTIVE_LAYOUT_NAME_KEY = "xkb_active_layout_name";
|
|
|
|
|
2020-10-10 16:09:18 +00:00
|
|
|
Language::Language(const std::string& id, const Json::Value& config)
|
|
|
|
: ALabel(config, "language", id, "{}", 0, true) {
|
2021-07-13 01:33:12 +00:00
|
|
|
is_variant_displayed = format_.find("{variant}") != std::string::npos;
|
2021-08-19 22:09:16 +00:00
|
|
|
if (format_.find("{}") != std::string::npos || format_.find("{short}") != std::string::npos) {
|
|
|
|
displayed_short_flag |= static_cast<std::byte>(DispayedShortFlag::ShortName);
|
|
|
|
}
|
|
|
|
if (format_.find("{shortDescription}") != std::string::npos) {
|
|
|
|
displayed_short_flag |= static_cast<std::byte>(DispayedShortFlag::ShortDescription);
|
|
|
|
}
|
2021-07-24 14:24:37 +00:00
|
|
|
if (config.isMember("tooltip-format")) {
|
|
|
|
tooltip_format_ = config["tooltip-format"].asString();
|
|
|
|
}
|
2020-10-10 16:09:18 +00:00
|
|
|
ipc_.subscribe(R"(["input"])");
|
|
|
|
ipc_.signal_event.connect(sigc::mem_fun(*this, &Language::onEvent));
|
|
|
|
ipc_.signal_cmd.connect(sigc::mem_fun(*this, &Language::onCmd));
|
|
|
|
ipc_.sendCmd(IPC_GET_INPUTS);
|
|
|
|
// Launch worker
|
|
|
|
ipc_.setWorker([this] {
|
|
|
|
try {
|
|
|
|
ipc_.handleEvent();
|
|
|
|
} catch (const std::exception& e) {
|
|
|
|
spdlog::error("Language: {}", e.what());
|
|
|
|
}
|
|
|
|
});
|
|
|
|
dp.emit();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Language::onCmd(const struct Ipc::ipc_response& res) {
|
2021-07-13 01:33:12 +00:00
|
|
|
if (res.type != static_cast<uint32_t>(IPC_GET_INPUTS)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-10-10 16:09:18 +00:00
|
|
|
try {
|
2021-07-13 01:33:12 +00:00
|
|
|
std::lock_guard<std::mutex> lock(mutex_);
|
|
|
|
auto payload = parser_.parse(res.payload);
|
|
|
|
std::vector<std::string> used_layouts;
|
|
|
|
// Display current layout of a device with a maximum count of layouts, expecting that all will
|
|
|
|
// be OK
|
|
|
|
Json::ArrayIndex max_id = 0, max = 0;
|
|
|
|
for (Json::ArrayIndex i = 0; i < payload.size(); i++) {
|
|
|
|
auto size = payload[i][XKB_LAYOUT_NAMES_KEY].size();
|
|
|
|
if (size > max) {
|
|
|
|
max = size;
|
|
|
|
max_id = i;
|
2020-10-10 16:09:18 +00:00
|
|
|
}
|
|
|
|
}
|
2021-07-13 01:33:12 +00:00
|
|
|
|
|
|
|
for (const auto& layout : payload[max_id][XKB_LAYOUT_NAMES_KEY]) {
|
|
|
|
used_layouts.push_back(layout.asString());
|
|
|
|
}
|
|
|
|
|
|
|
|
init_layouts_map(used_layouts);
|
|
|
|
set_current_layout(payload[max_id][XKB_ACTIVE_LAYOUT_NAME_KEY].asString());
|
2020-10-10 16:09:18 +00:00
|
|
|
dp.emit();
|
|
|
|
} catch (const std::exception& e) {
|
|
|
|
spdlog::error("Language: {}", e.what());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Language::onEvent(const struct Ipc::ipc_response& res) {
|
2021-07-13 01:33:12 +00:00
|
|
|
if (res.type != static_cast<uint32_t>(IPC_EVENT_INPUT)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-10-10 16:09:18 +00:00
|
|
|
try {
|
|
|
|
std::lock_guard<std::mutex> lock(mutex_);
|
2021-07-13 01:33:12 +00:00
|
|
|
auto payload = parser_.parse(res.payload)["input"];
|
2020-10-10 16:09:18 +00:00
|
|
|
if (payload["type"].asString() == "keyboard") {
|
2021-07-13 01:33:12 +00:00
|
|
|
set_current_layout(payload[XKB_ACTIVE_LAYOUT_NAME_KEY].asString());
|
2020-10-10 16:09:18 +00:00
|
|
|
}
|
|
|
|
dp.emit();
|
|
|
|
} catch (const std::exception& e) {
|
|
|
|
spdlog::error("Language: {}", e.what());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
auto Language::update() -> void {
|
2021-08-20 15:06:35 +00:00
|
|
|
std::lock_guard<std::mutex> lock(mutex_);
|
2021-07-13 01:33:12 +00:00
|
|
|
auto display_layout = trim(fmt::format(format_,
|
|
|
|
fmt::arg("short", layout_.short_name),
|
2021-08-17 02:28:41 +00:00
|
|
|
fmt::arg("shortDescription", layout_.short_description),
|
2021-07-13 01:33:12 +00:00
|
|
|
fmt::arg("long", layout_.full_name),
|
|
|
|
fmt::arg("variant", layout_.variant)));
|
|
|
|
label_.set_markup(display_layout);
|
|
|
|
if (tooltipEnabled()) {
|
2021-07-24 14:24:37 +00:00
|
|
|
if (tooltip_format_ != "") {
|
|
|
|
auto tooltip_display_layout = trim(fmt::format(tooltip_format_,
|
|
|
|
fmt::arg("short", layout_.short_name),
|
2021-08-17 02:28:41 +00:00
|
|
|
fmt::arg("shortDescription", layout_.short_description),
|
2021-07-24 14:24:37 +00:00
|
|
|
fmt::arg("long", layout_.full_name),
|
|
|
|
fmt::arg("variant", layout_.variant)));
|
|
|
|
label_.set_tooltip_markup(tooltip_display_layout);
|
|
|
|
} else {
|
|
|
|
label_.set_tooltip_markup(display_layout);
|
|
|
|
}
|
2020-10-10 16:09:18 +00:00
|
|
|
}
|
2021-07-13 01:33:12 +00:00
|
|
|
|
|
|
|
event_box_.show();
|
|
|
|
|
2020-10-10 16:09:18 +00:00
|
|
|
// Call parent update
|
|
|
|
ALabel::update();
|
|
|
|
}
|
|
|
|
|
2021-07-13 01:33:12 +00:00
|
|
|
auto Language::set_current_layout(std::string current_layout) -> void {
|
|
|
|
layout_ = layouts_map_[current_layout];
|
|
|
|
}
|
|
|
|
|
|
|
|
auto Language::init_layouts_map(const std::vector<std::string>& used_layouts) -> void {
|
|
|
|
std::map<std::string, std::vector<Layout*>> found_by_short_names;
|
2021-08-20 15:06:35 +00:00
|
|
|
XKBContext xkb_context;
|
|
|
|
auto layout = xkb_context.next_layout();
|
|
|
|
for (; layout != nullptr; layout = xkb_context.next_layout()) {
|
2021-07-13 01:33:12 +00:00
|
|
|
if (std::find(used_layouts.begin(), used_layouts.end(), layout->full_name) ==
|
|
|
|
used_layouts.end()) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!is_variant_displayed) {
|
|
|
|
auto short_name = layout->short_name;
|
|
|
|
if (found_by_short_names.count(short_name) > 0) {
|
|
|
|
found_by_short_names[short_name].push_back(layout);
|
|
|
|
} else {
|
|
|
|
found_by_short_names[short_name] = {layout};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
layouts_map_.emplace(layout->full_name, *layout);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (is_variant_displayed || found_by_short_names.size() == 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::map<std::string, int> short_name_to_number_map;
|
|
|
|
for (const auto& used_layout_name : used_layouts) {
|
|
|
|
auto used_layout = &layouts_map_.find(used_layout_name)->second;
|
|
|
|
auto layouts_with_same_name_list = found_by_short_names[used_layout->short_name];
|
|
|
|
if (layouts_with_same_name_list.size() < 2) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (short_name_to_number_map.count(used_layout->short_name) == 0) {
|
|
|
|
short_name_to_number_map[used_layout->short_name] = 1;
|
|
|
|
}
|
2021-08-19 22:09:16 +00:00
|
|
|
|
|
|
|
if (displayed_short_flag != static_cast<std::byte>(0)) {
|
|
|
|
int& number = short_name_to_number_map[used_layout->short_name];
|
|
|
|
used_layout->short_name =
|
|
|
|
used_layout->short_name + std::to_string(number);
|
|
|
|
used_layout->short_description =
|
|
|
|
used_layout->short_description + std::to_string(number);
|
|
|
|
++number;
|
|
|
|
}
|
2021-07-13 01:33:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Language::XKBContext::XKBContext() {
|
|
|
|
context_ = rxkb_context_new(RXKB_CONTEXT_NO_DEFAULT_INCLUDES);
|
|
|
|
rxkb_context_include_path_append_default(context_);
|
|
|
|
rxkb_context_parse_default_ruleset(context_);
|
|
|
|
}
|
|
|
|
|
|
|
|
auto Language::XKBContext::next_layout() -> Layout* {
|
|
|
|
if (xkb_layout_ == nullptr) {
|
|
|
|
xkb_layout_ = rxkb_layout_first(context_);
|
|
|
|
} else {
|
|
|
|
xkb_layout_ = rxkb_layout_next(xkb_layout_);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (xkb_layout_ == nullptr) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto description = std::string(rxkb_layout_get_description(xkb_layout_));
|
|
|
|
auto name = std::string(rxkb_layout_get_name(xkb_layout_));
|
|
|
|
auto variant_ = rxkb_layout_get_variant(xkb_layout_);
|
|
|
|
std::string variant = variant_ == nullptr ? "" : std::string(variant_);
|
2021-08-17 02:28:41 +00:00
|
|
|
auto short_description_ = rxkb_layout_get_brief(xkb_layout_);
|
|
|
|
std::string short_description;
|
|
|
|
if (short_description_ != nullptr) {
|
|
|
|
short_description = std::string(short_description_);
|
|
|
|
base_layouts_by_name_.emplace(name, xkb_layout_);
|
|
|
|
} else {
|
|
|
|
auto base_layout = base_layouts_by_name_[name];
|
|
|
|
short_description = base_layout == nullptr ? "" : std::string(rxkb_layout_get_brief(base_layout));
|
|
|
|
}
|
2021-08-20 15:06:35 +00:00
|
|
|
delete layout_;
|
2021-08-17 02:28:41 +00:00
|
|
|
layout_ = new Layout{description, name, variant, short_description};
|
2021-07-13 01:33:12 +00:00
|
|
|
return layout_;
|
|
|
|
}
|
|
|
|
|
2021-08-20 15:06:35 +00:00
|
|
|
Language::XKBContext::~XKBContext() {
|
|
|
|
rxkb_context_unref(context_);
|
|
|
|
delete layout_;
|
|
|
|
}
|
2020-10-10 16:09:18 +00:00
|
|
|
} // namespace waybar::modules::sway
|