fix(i3): Shutdown on socket disconnect

This commit is contained in:
Michael Carlberg 2016-10-18 11:49:13 +02:00
parent df93cabf22
commit 5fd8c50dde
2 changed files with 21 additions and 32 deletions

View File

@ -2,8 +2,8 @@
#include <i3ipc++/ipc.hpp>
#include "config.hpp"
#include "components/config.hpp"
#include "config.hpp"
#include "drawtypes/iconset.hpp"
#include "drawtypes/label.hpp"
#include "modules/meta.hpp"
@ -44,18 +44,6 @@ namespace modules {
public:
using event_module::event_module;
~i3_module() {
// Shutdown ipc connection {{{
try {
shutdown(m_ipc.get_event_socket_fd(), SHUT_RD);
shutdown(m_ipc.get_main_socket_fd(), SHUT_RD);
} catch (const std::exception& err) {
}
// }}}
}
void setup() {
// Load configuration values {{{
@ -106,24 +94,22 @@ namespace modules {
// }}}
}
bool has_event() {
// Wait for ipc events {{{
void stop() {
// Shutdown ipc connection when before stopping the module {{{
try {
m_ipc.handle_event();
return true;
} catch (const std::exception& err) {
if (enabled()) {
m_log.err("%s: Error while handling ipc event, stopping module...", name());
m_log.err("%s: %s", name(), err.what());
stop();
}
return false;
}
shutdown(m_ipc.get_event_socket_fd(), SHUT_RD);
shutdown(m_ipc.get_main_socket_fd(), SHUT_RD);
event_module::stop();
// }}}
}
bool has_event() {
if (!m_ipc.handle_event())
throw module_error("Socket connection closed...");
return true;
}
bool update() {
// Refresh workspace data {{{
@ -159,7 +145,8 @@ namespace modules {
flag = i3_flag::WORKSPACE_FOCUSED;
else if (workspace->urgent)
flag = i3_flag::WORKSPACE_URGENT;
else if (!workspace->visible || (workspace->visible && workspace->output != focused_output))
else if (!workspace->visible ||
(workspace->visible && workspace->output != focused_output))
flag = i3_flag::WORKSPACE_UNFOCUSED;
else
flag = i3_flag::WORKSPACE_VISIBLE;
@ -184,7 +171,8 @@ namespace modules {
label->replace_token("%name%", wsname);
label->replace_token("%icon%", icon->m_text);
label->replace_token("%index%", to_string(workspace->num));
m_workspaces.emplace_back(make_unique<i3_workspace>(workspace->num, flag, std::move(label)));
m_workspaces.emplace_back(
make_unique<i3_workspace>(workspace->num, flag, std::move(label)));
}
return true;

View File

@ -210,13 +210,14 @@ namespace modules {
}
void stop() {
wakeup();
std::lock_guard<threading_util::spin_lock> lck(this->update_lock);
{
if (!enabled())
return;
m_log.trace("%s: Stop", name());
enable(false);
wakeup();
if (!on_stop.empty())
on_stop.emit(name());
}
@ -391,7 +392,7 @@ namespace modules {
CAST_MODULE(Impl)->broadcast();
CAST_MODULE(Impl)->sleep(m_interval);
}
} catch (const application_error& err) {
} catch (const std::exception& err) {
this->m_log.err("%s: %s", this->name(), err.what());
this->m_log.warn("Stopping '%s'...", this->name());
CAST_MODULE(Impl)->stop();
@ -435,7 +436,7 @@ namespace modules {
lck.unlock();
CAST_MODULE(Impl)->idle();
}
} catch (const application_error& err) {
} catch (const std::exception& err) {
this->m_log.err("%s: %s", this->name(), err.what());
this->m_log.warn("Stopping '%s'...", this->name());
CAST_MODULE(Impl)->stop();
@ -467,7 +468,7 @@ namespace modules {
std::lock_guard<threading_util::spin_lock> lck(this->update_lock);
CAST_MODULE(Impl)->poll_events();
}
} catch (const application_error& err) {
} catch (const std::exception& err) {
this->m_log.err("%s: %s", this->name(), err.what());
this->m_log.warn("Stopping '%s'...", this->name());
CAST_MODULE(Impl)->stop();