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 <i3ipc++/ipc.hpp>
#include "config.hpp"
#include "components/config.hpp" #include "components/config.hpp"
#include "config.hpp"
#include "drawtypes/iconset.hpp" #include "drawtypes/iconset.hpp"
#include "drawtypes/label.hpp" #include "drawtypes/label.hpp"
#include "modules/meta.hpp" #include "modules/meta.hpp"
@ -44,18 +44,6 @@ namespace modules {
public: public:
using event_module::event_module; 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() { void setup() {
// Load configuration values {{{ // Load configuration values {{{
@ -106,24 +94,22 @@ namespace modules {
// }}} // }}}
} }
bool has_event() { void stop() {
// Wait for ipc events {{{ // Shutdown ipc connection when before stopping the module {{{
try { shutdown(m_ipc.get_event_socket_fd(), SHUT_RD);
m_ipc.handle_event(); shutdown(m_ipc.get_main_socket_fd(), SHUT_RD);
return true; event_module::stop();
} 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;
}
// }}} // }}}
} }
bool has_event() {
if (!m_ipc.handle_event())
throw module_error("Socket connection closed...");
return true;
}
bool update() { bool update() {
// Refresh workspace data {{{ // Refresh workspace data {{{
@ -159,7 +145,8 @@ namespace modules {
flag = i3_flag::WORKSPACE_FOCUSED; flag = i3_flag::WORKSPACE_FOCUSED;
else if (workspace->urgent) else if (workspace->urgent)
flag = i3_flag::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; flag = i3_flag::WORKSPACE_UNFOCUSED;
else else
flag = i3_flag::WORKSPACE_VISIBLE; flag = i3_flag::WORKSPACE_VISIBLE;
@ -184,7 +171,8 @@ namespace modules {
label->replace_token("%name%", wsname); label->replace_token("%name%", wsname);
label->replace_token("%icon%", icon->m_text); label->replace_token("%icon%", icon->m_text);
label->replace_token("%index%", to_string(workspace->num)); 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; return true;

View File

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