Skip to content

Commit 6ce881a

Browse files
authored
Merge pull request #3826 from DreamMaoMao/fix-hyprland-window-crash
fix: hyprland/window get empty ipc json data
2 parents dbb708c + 01ae117 commit 6ce881a

File tree

1 file changed

+19
-9
lines changed

1 file changed

+19
-9
lines changed

src/modules/hyprland/window.cpp

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,38 +12,47 @@
1212
#include "util/rewrite_string.hpp"
1313
#include "util/sanitize_str.hpp"
1414

15+
#include <shared_mutex>
16+
#include <thread>
17+
1518
namespace waybar::modules::hyprland {
1619

20+
std::shared_mutex windowIpcSmtx;
21+
1722
Window::Window(const std::string& id, const Bar& bar, const Json::Value& config)
1823
: AAppIconLabel(config, "window", id, "{title}", 0, true), bar_(bar) {
24+
25+
std::unique_lock<std::shared_mutex> windowIpcUniqueLock(windowIpcSmtx);
26+
1927
modulesReady = true;
2028
separateOutputs_ = config["separate-outputs"].asBool();
2129

2230
if (!gIPC) {
2331
gIPC = std::make_unique<IPC>();
2432
}
2533

26-
queryActiveWorkspace();
27-
update();
28-
dp.emit();
29-
3034
// register for hyprland ipc
3135
gIPC->registerForIPC("activewindow", this);
3236
gIPC->registerForIPC("closewindow", this);
3337
gIPC->registerForIPC("movewindow", this);
3438
gIPC->registerForIPC("changefloatingmode", this);
3539
gIPC->registerForIPC("fullscreen", this);
40+
41+
windowIpcUniqueLock.unlock();
42+
43+
queryActiveWorkspace();
44+
update();
45+
dp.emit();
3646
}
3747

3848
Window::~Window() {
49+
std::unique_lock<std::shared_mutex> windowIpcUniqueLock(windowIpcSmtx);
3950
gIPC->unregisterForIPC(this);
40-
// wait for possible event handler to finish
41-
std::lock_guard<std::mutex> lg(mutex_);
4251
}
4352

4453
auto Window::update() -> void {
45-
// fix ampersands
46-
std::lock_guard<std::mutex> lg(mutex_);
54+
55+
std::shared_lock<std::shared_mutex> windowIpcShareLock(windowIpcSmtx);
4756

4857
std::string windowName = waybar::util::sanitize_string(workspace_.last_window_title);
4958
std::string windowAddress = workspace_.last_window;
@@ -144,7 +153,8 @@ auto Window::WindowData::parse(const Json::Value& value) -> Window::WindowData {
144153
}
145154

146155
void Window::queryActiveWorkspace() {
147-
std::lock_guard<std::mutex> lg(mutex_);
156+
157+
std::shared_lock<std::shared_mutex> windowIpcShareLock(windowIpcSmtx);
148158

149159
if (separateOutputs_) {
150160
workspace_ = getActiveWorkspace(this->bar_.output->name);

0 commit comments

Comments
 (0)