Merge branch 'Alexays:master' into drawer-state

This commit is contained in:
OliMoli
2026-05-08 06:35:35 +02:00
committed by GitHub
8 changed files with 233 additions and 83 deletions
+14
View File
@@ -4,6 +4,7 @@
#include <filesystem> #include <filesystem>
#include <list> #include <list>
#include <mutex> #include <mutex>
#include <optional>
#include <string> #include <string>
#include <thread> #include <thread>
#include <utility> #include <utility>
@@ -35,9 +36,22 @@ class IPC {
Json::Value getSocket1JsonReply(const std::string& rq); Json::Value getSocket1JsonReply(const std::string& rq);
static std::filesystem::path getSocketFolder(const char* instanceSig); static std::filesystem::path getSocketFolder(const char* instanceSig);
/// Dispatch a Hyprland command. Automatically uses the correct protocol
/// (legacy text or Lua-based) depending on the running Hyprland version.
static std::string dispatch(const std::string& dispatcher, const std::string& arg);
/// Build a Lua-format dispatch command string.
static std::string buildLuaDispatch(const std::string& dispatcher, const std::string& arg);
protected: protected:
static std::filesystem::path socketFolder_; static std::filesystem::path socketFolder_;
/// Detect whether the running Hyprland uses the Lua-based IPC protocol.
/// Returns true for Hyprland >= 0.54 (Lua config), false for older versions.
static bool isLuaProtocol();
static std::optional<bool> s_luaProtocolDetected_; // cached detection result
private: private:
void socketListener(); void socketListener();
void parseIPC(const std::string&); void parseIPC(const std::string&);
+13 -13
View File
@@ -33,21 +33,21 @@ std::string toLowerCase(const std::string& input) {
std::optional<std::string> getFileBySuffix(const std::string& dir, const std::string& suffix, std::optional<std::string> getFileBySuffix(const std::string& dir, const std::string& suffix,
bool check_lower_case) { bool check_lower_case) {
if (!std::filesystem::exists(dir)) { try {
return {}; for (const auto& entry : std::filesystem::recursive_directory_iterator(dir)) {
} if (entry.is_regular_file()) {
for (const auto& entry : std::filesystem::recursive_directory_iterator(dir)) { std::string filename = entry.path().filename().string();
if (entry.is_regular_file()) { if (filename.size() < suffix.size()) {
std::string filename = entry.path().filename().string(); continue;
if (filename.size() < suffix.size()) { }
continue; if ((filename.compare(filename.size() - suffix.size(), suffix.size(), suffix) == 0) ||
} (check_lower_case && filename.compare(filename.size() - suffix.size(), suffix.size(),
if ((filename.compare(filename.size() - suffix.size(), suffix.size(), suffix) == 0) || toLowerCase(suffix)) == 0)) {
(check_lower_case && filename.compare(filename.size() - suffix.size(), suffix.size(), return entry.path().string();
toLowerCase(suffix)) == 0)) { }
return entry.path().string();
} }
} }
} catch (const std::filesystem::filesystem_error&) {
} }
return {}; return {};
+42 -50
View File
@@ -117,24 +117,16 @@ void waybar::modules::Battery::refreshBatteries() {
if (((bat_defined && dir_name == config_["bat"].asString()) || !bat_defined) && if (((bat_defined && dir_name == config_["bat"].asString()) || !bat_defined) &&
(fs::exists(node.path() / "capacity") || fs::exists(node.path() / "charge_now")) && (fs::exists(node.path() / "capacity") || fs::exists(node.path() / "charge_now")) &&
fs::exists(node.path() / "uevent") && fs::exists(node.path() / "uevent") &&
(fs::exists(node.path() / "status") || bat_compatibility) && (fs::exists(node.path() / "status") || bat_compatibility)) {
fs::exists(node.path() / "type")) {
std::string type; std::string type;
std::ifstream(node.path() / "type") >> type; if (std::ifstream{node.path() / "type"} >> type && !type.compare("Battery")) {
if (!type.compare("Battery")) {
// Ignore non-system power supplies unless explicitly requested // Ignore non-system power supplies unless explicitly requested
if (!bat_defined && fs::exists(node.path() / "scope")) { if (!bat_defined) {
std::string scope; std::string scope;
try { // for hotplug-in device, access it is always unstable because you may remove the
// for hotplug-in device, access it is always unstable because you may remove the // device anytime so just allow failure happen and do nothing
// device anytime so just allow failure happen and do nothing if (std::ifstream{node.path() / "scope"} >> scope &&
std::ifstream(node.path() / "scope") >> scope; g_ascii_strcasecmp(scope.data(), "device") == 0) {
} catch (const std::ifstream::failure& e) {
scope.clear();
continue;
}
if (g_ascii_strcasecmp(scope.data(), "device") == 0) {
continue; continue;
} }
} }
@@ -282,11 +274,11 @@ waybar::modules::Battery::getInfos() {
auto bat = item.first; auto bat = item.first;
std::string _status; std::string _status;
/* Check for adapter status if battery is not available */ {
if (!std::ifstream(bat / "status")) { std::ifstream f{bat / "status"};
std::getline(std::ifstream(adapter_ / "status"), _status); if (!std::getline(f, _status)) {
} else { std::getline(std::ifstream(adapter_ / "status"), _status);
std::getline(std::ifstream(bat / "status"), _status); }
} }
// Some battery will report current and charge in μA/μAh. // Some battery will report current and charge in μA/μAh.
@@ -295,64 +287,64 @@ waybar::modules::Battery::getInfos() {
uint32_t current_now = 0; uint32_t current_now = 0;
int32_t _current_now_int = 0; int32_t _current_now_int = 0;
bool current_now_exists = false; bool current_now_exists = false;
if (fs::exists(bat / "current_now")) { if (std::ifstream current_now_f{bat / "current_now"}) {
current_now_exists = true; current_now_exists = true;
std::ifstream(bat / "current_now") >> _current_now_int; current_now_f >> _current_now_int;
} else if (fs::exists(bat / "current_avg")) { } else if (std::ifstream current_avg_f{bat / "current_avg"}) {
current_now_exists = true; current_now_exists = true;
std::ifstream(bat / "current_avg") >> _current_now_int; current_avg_f >> _current_now_int;
} }
// Documentation ABI allows a negative value when discharging, positive // Documentation ABI allows a negative value when discharging, positive
// value when charging. // value when charging.
current_now = std::abs(_current_now_int); current_now = std::abs(_current_now_int);
if (fs::exists(bat / "time_to_empty_now")) { if (std::ifstream f{bat / "time_to_empty_now"}) {
time_to_empty_now_exists = true; time_to_empty_now_exists = true;
std::ifstream(bat / "time_to_empty_now") >> time_to_empty_now; f >> time_to_empty_now;
} }
if (fs::exists(bat / "time_to_full_now")) { if (std::ifstream f{bat / "time_to_full_now"}) {
time_to_full_now_exists = true; time_to_full_now_exists = true;
std::ifstream(bat / "time_to_full_now") >> time_to_full_now; f >> time_to_full_now;
} }
uint32_t voltage_now = 0; uint32_t voltage_now = 0;
bool voltage_now_exists = false; bool voltage_now_exists = false;
if (fs::exists(bat / "voltage_now")) { if (std::ifstream voltage_now_f{bat / "voltage_now"}) {
voltage_now_exists = true; voltage_now_exists = true;
std::ifstream(bat / "voltage_now") >> voltage_now; voltage_now_f >> voltage_now;
} else if (fs::exists(bat / "voltage_avg")) { } else if (std::ifstream voltage_avg_f{bat / "voltage_avg"}) {
voltage_now_exists = true; voltage_now_exists = true;
std::ifstream(bat / "voltage_avg") >> voltage_now; voltage_avg_f >> voltage_now;
} }
uint32_t charge_full = 0; uint32_t charge_full = 0;
bool charge_full_exists = false; bool charge_full_exists = false;
if (fs::exists(bat / "charge_full")) { if (std::ifstream f{bat / "charge_full"}) {
charge_full_exists = true; charge_full_exists = true;
std::ifstream(bat / "charge_full") >> charge_full; f >> charge_full;
} }
uint32_t charge_full_design = 0; uint32_t charge_full_design = 0;
bool charge_full_design_exists = false; bool charge_full_design_exists = false;
if (fs::exists(bat / "charge_full_design")) { if (std::ifstream f{bat / "charge_full_design"}) {
charge_full_design_exists = true; charge_full_design_exists = true;
std::ifstream(bat / "charge_full_design") >> charge_full_design; f >> charge_full_design;
} }
uint32_t charge_now = 0; uint32_t charge_now = 0;
bool charge_now_exists = false; bool charge_now_exists = false;
if (fs::exists(bat / "charge_now")) { if (std::ifstream f{bat / "charge_now"}) {
charge_now_exists = true; charge_now_exists = true;
std::ifstream(bat / "charge_now") >> charge_now; f >> charge_now;
} }
uint32_t power_now = 0; uint32_t power_now = 0;
int32_t _power_now_int = 0; int32_t _power_now_int = 0;
bool power_now_exists = false; bool power_now_exists = false;
if (fs::exists(bat / "power_now")) { if (std::ifstream f{bat / "power_now"}) {
power_now_exists = true; power_now_exists = true;
std::ifstream(bat / "power_now") >> _power_now_int; f >> _power_now_int;
} }
// Some drivers (example: Qualcomm) exposes use a negative value when // Some drivers (example: Qualcomm) exposes use a negative value when
// discharging, positive value when charging. // discharging, positive value when charging.
@@ -360,28 +352,28 @@ waybar::modules::Battery::getInfos() {
uint32_t energy_now = 0; uint32_t energy_now = 0;
bool energy_now_exists = false; bool energy_now_exists = false;
if (fs::exists(bat / "energy_now")) { if (std::ifstream f{bat / "energy_now"}) {
energy_now_exists = true; energy_now_exists = true;
std::ifstream(bat / "energy_now") >> energy_now; f >> energy_now;
} }
uint32_t energy_full = 0; uint32_t energy_full = 0;
bool energy_full_exists = false; bool energy_full_exists = false;
if (fs::exists(bat / "energy_full")) { if (std::ifstream f{bat / "energy_full"}) {
energy_full_exists = true; energy_full_exists = true;
std::ifstream(bat / "energy_full") >> energy_full; f >> energy_full;
} }
uint32_t energy_full_design = 0; uint32_t energy_full_design = 0;
bool energy_full_design_exists = false; bool energy_full_design_exists = false;
if (fs::exists(bat / "energy_full_design")) { if (std::ifstream f{bat / "energy_full_design"}) {
energy_full_design_exists = true; energy_full_design_exists = true;
std::ifstream(bat / "energy_full_design") >> energy_full_design; f >> energy_full_design;
} }
uint16_t cycleCount = 0; uint16_t cycleCount = 0;
if (fs::exists(bat / "cycle_count")) { if (std::ifstream f{bat / "cycle_count"}) {
std::ifstream(bat / "cycle_count") >> cycleCount; f >> cycleCount;
} }
if (charge_full_design >= largestDesignCapacity) { if (charge_full_design >= largestDesignCapacity) {
largestDesignCapacity = charge_full_design; largestDesignCapacity = charge_full_design;
@@ -411,9 +403,9 @@ waybar::modules::Battery::getInfos() {
} else if (energy_now_exists && energy_full_exists && energy_full != 0) { } else if (energy_now_exists && energy_full_exists && energy_full != 0) {
capacity_exists = true; capacity_exists = true;
capacity = 100 * (uint64_t)energy_now / (uint64_t)energy_full; capacity = 100 * (uint64_t)energy_now / (uint64_t)energy_full;
} else if (fs::exists(bat / "capacity")) { } else if (std::ifstream f{bat / "capacity"}) {
capacity_exists = true; capacity_exists = true;
std::ifstream(bat / "capacity") >> capacity; f >> capacity;
} }
if (!voltage_now_exists) { if (!voltage_now_exists) {
+8 -10
View File
@@ -23,23 +23,21 @@ std::vector<float> waybar::modules::CpuFrequency::parseCpuFrequencies() {
if (frequencies.size() <= 0) { if (frequencies.size() <= 0) {
std::string cpufreq_dir = "/sys/devices/system/cpu/cpufreq"; std::string cpufreq_dir = "/sys/devices/system/cpu/cpufreq";
if (std::filesystem::exists(cpufreq_dir)) { try {
std::vector<std::string> frequency_files = {"/cpuinfo_min_freq", "/cpuinfo_max_freq"}; std::vector<std::string> frequency_files = {"/cpuinfo_min_freq", "/cpuinfo_max_freq"};
for (auto& p : std::filesystem::directory_iterator(cpufreq_dir)) { for (auto& p : std::filesystem::directory_iterator(cpufreq_dir)) {
for (const auto& freq_file : frequency_files) { for (const auto& freq_file : frequency_files) {
std::string freq_file_path = p.path().string() + freq_file; std::string freq_file_path = p.path().string() + freq_file;
if (std::filesystem::exists(freq_file_path)) { std::string freq_value;
std::string freq_value; std::ifstream freq(freq_file_path);
std::ifstream freq(freq_file_path); if (freq.is_open()) {
if (freq.is_open()) { getline(freq, freq_value);
getline(freq, freq_value); float frequency = std::strtol(freq_value.c_str(), nullptr, 10);
float frequency = std::strtol(freq_value.c_str(), nullptr, 10); frequencies.push_back(frequency / 1000);
frequencies.push_back(frequency / 1000);
freq.close();
}
} }
} }
} }
} catch (const std::filesystem::filesystem_error&) {
} }
} }
+67
View File
@@ -13,6 +13,7 @@
#include <cerrno> #include <cerrno>
#include <cstring> #include <cstring>
#include <filesystem> #include <filesystem>
#include <optional>
#include <string> #include <string>
#include "util/scoped_fd.hpp" #include "util/scoped_fd.hpp"
@@ -20,6 +21,7 @@
namespace waybar::modules::hyprland { namespace waybar::modules::hyprland {
std::filesystem::path IPC::socketFolder_; std::filesystem::path IPC::socketFolder_;
std::optional<bool> IPC::s_luaProtocolDetected_;
std::filesystem::path IPC::getSocketFolder(const char* instanceSig) { std::filesystem::path IPC::getSocketFolder(const char* instanceSig) {
static std::mutex folderMutex; static std::mutex folderMutex;
@@ -290,4 +292,69 @@ Json::Value IPC::getSocket1JsonReply(const std::string& rq) {
return parser_.parse(reply); return parser_.parse(reply);
} }
bool IPC::isLuaProtocol() {
if (s_luaProtocolDetected_.has_value()) {
return *s_luaProtocolDetected_;
}
// Probe: send a harmless old-style dispatch and check the error.
// In Lua-based Hyprland (>= 0.54) the error contains "hl.dispatch".
// In older versions it returns "ok" or a different error.
auto reply = getSocket1Reply("dispatch workspace __waybar_probe__");
bool luaProto = reply.find("hl.dispatch") != std::string::npos;
if (luaProto) {
spdlog::info("Hyprland IPC: detected Lua-based dispatch protocol (Hyprland >= 0.54)");
} else {
spdlog::info("Hyprland IPC: detected legacy dispatch protocol");
}
s_luaProtocolDetected_ = luaProto;
return luaProto;
}
std::string IPC::buildLuaDispatch(const std::string& dispatcher, const std::string& arg) {
// Map old-style dispatchers to the new Lua hl.dsp API.
//
// Old format: dispatch workspace 1
// New format: /dispatch hl.dsp.focus({ workspace = "1" })
//
// Old format: dispatch focusworkspaceoncurrentmonitor 2
// New format: /dispatch hl.dsp.focus({ workspace = "2", monitor = "current" })
//
// Old format: dispatch togglespecialworkspace name
// New format: /dispatch hl.dsp.workspace.toggle_special("name")
if (dispatcher == "workspace") {
return "/dispatch hl.dsp.focus({ workspace = \"" + arg + "\" })";
}
if (dispatcher == "focusworkspaceoncurrentmonitor") {
return "/dispatch hl.dsp.focus({ workspace = \"" + arg + "\", monitor = \"current\" })";
}
if (dispatcher == "togglespecialworkspace") {
if (arg.empty()) {
return "/dispatch hl.dsp.workspace.toggle_special()";
}
return "/dispatch hl.dsp.workspace.toggle_special(\"" + arg + "\")";
}
// Fallback for any other dispatcher: try the old format wrapped in dispatch().
// This may not work for all dispatchers, but it's a reasonable default.
spdlog::warn("Hyprland IPC: unknown dispatcher '{}' in Lua mode, attempting generic format",
dispatcher);
return "/dispatch hl.dsp." + dispatcher + "(\"" + arg + "\")";
}
std::string IPC::dispatch(const std::string& dispatcher, const std::string& arg) {
if (isLuaProtocol()) {
return getSocket1Reply(buildLuaDispatch(dispatcher, arg));
}
// Legacy format: "dispatch <dispatcher> <arg>"
std::string cmd = "dispatch " + dispatcher;
if (!arg.empty()) {
cmd += " " + arg;
}
return getSocket1Reply(cmd);
}
} // namespace waybar::modules::hyprland } // namespace waybar::modules::hyprland
+6 -6
View File
@@ -71,20 +71,20 @@ bool Workspace::handleClicked(GdkEventButton* bt) const {
try { try {
if (id() > 0) { // normal if (id() > 0) { // normal
if (m_workspaceManager.moveToMonitor()) { if (m_workspaceManager.moveToMonitor()) {
m_ipc.getSocket1Reply("dispatch focusworkspaceoncurrentmonitor " + std::to_string(id())); IPC::dispatch("focusworkspaceoncurrentmonitor", std::to_string(id()));
} else { } else {
m_ipc.getSocket1Reply("dispatch workspace " + std::to_string(id())); IPC::dispatch("workspace", std::to_string(id()));
} }
} else if (!isSpecial()) { // named (this includes persistent) } else if (!isSpecial()) { // named (this includes persistent)
if (m_workspaceManager.moveToMonitor()) { if (m_workspaceManager.moveToMonitor()) {
m_ipc.getSocket1Reply("dispatch focusworkspaceoncurrentmonitor name:" + name()); IPC::dispatch("focusworkspaceoncurrentmonitor", "name:" + name());
} else { } else {
m_ipc.getSocket1Reply("dispatch workspace name:" + name()); IPC::dispatch("workspace", "name:" + name());
} }
} else if (id() != -99) { // named special } else if (id() != -99) { // named special
m_ipc.getSocket1Reply("dispatch togglespecialworkspace " + name()); IPC::dispatch("togglespecialworkspace", name());
} else { // special } else { // special
m_ipc.getSocket1Reply("dispatch togglespecialworkspace"); IPC::dispatch("togglespecialworkspace", "");
} }
return true; return true;
} catch (const std::exception& e) { } catch (const std::exception& e) {
+4 -4
View File
@@ -1195,15 +1195,15 @@ bool Workspaces::handleScroll(GdkEventScroll* e) {
if (dir == SCROLL_DIR::DOWN || dir == SCROLL_DIR::RIGHT) { if (dir == SCROLL_DIR::DOWN || dir == SCROLL_DIR::RIGHT) {
if (allOutputs()) { if (allOutputs()) {
m_ipc.getSocket1Reply("dispatch workspace e+1"); IPC::dispatch("workspace", "e+1");
} else { } else {
m_ipc.getSocket1Reply("dispatch workspace m+1"); IPC::dispatch("workspace", "m+1");
} }
} else if (dir == SCROLL_DIR::UP || dir == SCROLL_DIR::LEFT) { } else if (dir == SCROLL_DIR::UP || dir == SCROLL_DIR::LEFT) {
if (allOutputs()) { if (allOutputs()) {
m_ipc.getSocket1Reply("dispatch workspace e-1"); IPC::dispatch("workspace", "e-1");
} else { } else {
m_ipc.getSocket1Reply("dispatch workspace m-1"); IPC::dispatch("workspace", "m-1");
} }
} }
+79
View File
@@ -15,6 +15,10 @@ namespace {
class IPCTestHelper : public hyprland::IPC { class IPCTestHelper : public hyprland::IPC {
public: public:
static void resetSocketFolder() { socketFolder_.clear(); } static void resetSocketFolder() { socketFolder_.clear(); }
static void resetLuaProtocolDetection() { s_luaProtocolDetected_.reset(); }
static void setLuaProtocolDetected(bool value) { s_luaProtocolDetected_ = value; }
using hyprland::IPC::buildLuaDispatch;
using hyprland::IPC::isLuaProtocol;
}; };
std::size_t countOpenFds() { std::size_t countOpenFds() {
@@ -133,3 +137,78 @@ TEST_CASE("getSocket1Reply failure paths do not leak fds", "[getSocket1Reply][fd
REQUIRE(after_connect_failures == baseline); REQUIRE(after_connect_failures == baseline);
} }
#endif #endif
// --- Tests for new Lua IPC dispatch functions ---
TEST_CASE("buildLuaDispatch workspace", "[buildLuaDispatch]") {
SECTION("numeric workspace") {
auto result = IPCTestHelper::buildLuaDispatch("workspace", "1");
REQUIRE(result == "/dispatch hl.dsp.focus({ workspace = \"1\" })");
}
SECTION("named workspace") {
auto result = IPCTestHelper::buildLuaDispatch("workspace", "name:term");
REQUIRE(result == "/dispatch hl.dsp.focus({ workspace = \"name:term\" })");
}
SECTION("relative workspace") {
auto result = IPCTestHelper::buildLuaDispatch("workspace", "e+1");
REQUIRE(result == "/dispatch hl.dsp.focus({ workspace = \"e+1\" })");
}
}
TEST_CASE("buildLuaDispatch focusworkspaceoncurrentmonitor", "[buildLuaDispatch]") {
auto result =
IPCTestHelper::buildLuaDispatch("focusworkspaceoncurrentmonitor", "3");
REQUIRE(
result ==
"/dispatch hl.dsp.focus({ workspace = \"3\", monitor = \"current\" })");
}
TEST_CASE("buildLuaDispatch togglespecialworkspace", "[buildLuaDispatch]") {
SECTION("with name") {
auto result =
IPCTestHelper::buildLuaDispatch("togglespecialworkspace", "scratchpad");
REQUIRE(result ==
"/dispatch hl.dsp.workspace.toggle_special(\"scratchpad\")");
}
SECTION("empty arg") {
auto result =
IPCTestHelper::buildLuaDispatch("togglespecialworkspace", "");
REQUIRE(result == "/dispatch hl.dsp.workspace.toggle_special()");
}
}
TEST_CASE("buildLuaDispatch unknown dispatcher fallback", "[buildLuaDispatch]") {
auto result =
IPCTestHelper::buildLuaDispatch("unknown_dispatcher", "some_arg");
REQUIRE(result ==
"/dispatch hl.dsp.unknown_dispatcher(\"some_arg\")");
}
TEST_CASE("dispatch throws when Hyprland is not running", "[dispatch]") {
unsetenv("HYPRLAND_INSTANCE_SIGNATURE");
IPCTestHelper::resetSocketFolder();
IPCTestHelper::resetLuaProtocolDetection();
CHECK_THROWS(hyprland::IPC::dispatch("workspace", "1"));
}
TEST_CASE("isLuaProtocol uses cached value and avoids socket call",
"[isLuaProtocol]") {
unsetenv("HYPRLAND_INSTANCE_SIGNATURE");
IPCTestHelper::resetSocketFolder();
SECTION("cached false") {
IPCTestHelper::setLuaProtocolDetected(false);
// Should return false without throwing (no socket call needed)
REQUIRE(IPCTestHelper::isLuaProtocol() == false);
}
SECTION("cached true") {
IPCTestHelper::setLuaProtocolDetected(true);
// Should return true without throwing (no socket call needed)
REQUIRE(IPCTestHelper::isLuaProtocol() == true);
}
// Cleanup: reset detection so other tests aren't affected
IPCTestHelper::resetLuaProtocolDetection();
}