refactor(i3): Cleanup and configurable actions

- Do not throw on handle_event failure
- Add settings to disable mouse scroll/click actions
- Add fold markers
- Misc cleanup
This commit is contained in:
Michael Carlberg 2016-11-11 23:57:12 +01:00
parent d9e3f8a2a7
commit 2740e69a38
5 changed files with 58 additions and 76 deletions

View File

@ -46,7 +46,9 @@ namespace modules {
string get_output(); string get_output();
bool build(builder* builder, string tag) const; bool build(builder* builder, string tag) const;
bool handle_event(string cmd); bool handle_event(string cmd);
bool receive_events() const; bool receive_events() const {
return true;
}
private: private:
static constexpr auto DEFAULT_WS_ICON = "ws-icon-default"; static constexpr auto DEFAULT_WS_ICON = "ws-icon-default";

View File

@ -50,7 +50,9 @@ namespace modules {
bool update(); bool update();
bool build(builder* builder, string tag) const; bool build(builder* builder, string tag) const;
bool handle_event(string cmd); bool handle_event(string cmd);
bool receive_events() const; bool receive_events() const {
return true;
}
private: private:
static constexpr auto DEFAULT_WS_ICON = "ws-icon-default"; static constexpr auto DEFAULT_WS_ICON = "ws-icon-default";
@ -66,6 +68,8 @@ namespace modules {
vector<i3_workspace_t> m_workspaces; vector<i3_workspace_t> m_workspaces;
iconset_t m_icons; iconset_t m_icons;
bool m_click = true;
bool m_scroll = true;
bool m_indexsort = false; bool m_indexsort = false;
bool m_pinworkspaces = false; bool m_pinworkspaces = false;
bool m_strip_wsnumbers = false; bool m_strip_wsnumbers = false;

View File

@ -271,7 +271,9 @@ namespace modules {
m_log.warn("%s: No handler, ignoring broadcast...", name()); m_log.warn("%s: No handler, ignoring broadcast...", name());
} }
void idle() {} void idle() {
CAST_MOD(Impl)->sleep(25ms);
}
void sleep(chrono::duration<double> sleep_duration) { void sleep(chrono::duration<double> sleep_duration) {
std::unique_lock<std::mutex> lck(m_sleeplock); std::unique_lock<std::mutex> lck(m_sleeplock);
@ -431,11 +433,10 @@ namespace modules {
break; break;
if (!CAST_MOD(Impl)->update()) if (!CAST_MOD(Impl)->update())
continue; continue;
}
if (CONST_MOD(Impl).running())
CAST_MOD(Impl)->broadcast(); CAST_MOD(Impl)->broadcast();
} }
}
} catch (const module_error& err) { } catch (const module_error& err) {
CAST_MOD(Impl)->halt(err.what()); CAST_MOD(Impl)->halt(err.what());
} catch (const std::exception& err) { } catch (const std::exception& err) {

View File

@ -359,10 +359,6 @@ namespace modules {
return true; return true;
} // }}} } // }}}
bool bspwm_module::receive_events() const { // {{{
return true;
} // }}}
} }
LEMONBUDDY_NS_END LEMONBUDDY_NS_END

View File

@ -5,36 +5,31 @@
LEMONBUDDY_NS LEMONBUDDY_NS
namespace modules { namespace modules {
void i3_module::setup() { void i3_module::setup() { // {{{
// Load configuration values {{{ // Load configuration values
GET_CONFIG_VALUE(name(), m_click, "enable-click");
GET_CONFIG_VALUE(name(), m_scroll, "enable-scroll");
GET_CONFIG_VALUE(name(), m_indexsort, "index-sort"); GET_CONFIG_VALUE(name(), m_indexsort, "index-sort");
GET_CONFIG_VALUE(name(), m_pinworkspaces, "pin-workspaces"); GET_CONFIG_VALUE(name(), m_pinworkspaces, "pin-workspaces");
GET_CONFIG_VALUE(name(), m_strip_wsnumbers, "strip-wsnumbers"); GET_CONFIG_VALUE(name(), m_strip_wsnumbers, "strip-wsnumbers");
GET_CONFIG_VALUE(name(), m_wsname_maxlen, "wsname-maxlen"); GET_CONFIG_VALUE(name(), m_wsname_maxlen, "wsname-maxlen");
// }}} // Add formats and create components
// Add formats and create components {{{
m_formatter->add(DEFAULT_FORMAT, TAG_LABEL_STATE, {TAG_LABEL_STATE}); m_formatter->add(DEFAULT_FORMAT, TAG_LABEL_STATE, {TAG_LABEL_STATE});
if (m_formatter->has(TAG_LABEL_STATE)) { if (m_formatter->has(TAG_LABEL_STATE)) {
m_statelabels.insert(make_pair(i3_flag::WORKSPACE_FOCUSED, m_statelabels.insert(make_pair(
load_optional_label(m_conf, name(), "label-focused", DEFAULT_WS_LABEL))); i3_flag::WORKSPACE_FOCUSED, load_optional_label(m_conf, name(), "label-focused", DEFAULT_WS_LABEL)));
m_statelabels.insert(make_pair(i3_flag::WORKSPACE_UNFOCUSED, m_statelabels.insert(make_pair(
load_optional_label(m_conf, name(), "label-unfocused", DEFAULT_WS_LABEL))); i3_flag::WORKSPACE_UNFOCUSED, load_optional_label(m_conf, name(), "label-unfocused", DEFAULT_WS_LABEL)));
m_statelabels.insert(make_pair(i3_flag::WORKSPACE_VISIBLE, m_statelabels.insert(make_pair(
load_optional_label(m_conf, name(), "label-visible", DEFAULT_WS_LABEL))); i3_flag::WORKSPACE_VISIBLE, load_optional_label(m_conf, name(), "label-visible", DEFAULT_WS_LABEL)));
m_statelabels.insert(make_pair(i3_flag::WORKSPACE_URGENT, m_statelabels.insert(
load_optional_label(m_conf, name(), "label-urgent", DEFAULT_WS_LABEL))); make_pair(i3_flag::WORKSPACE_URGENT, load_optional_label(m_conf, name(), "label-urgent", DEFAULT_WS_LABEL)));
} }
m_icons = iconset_t{new iconset()}; m_icons = iconset_t{new iconset()};
m_icons->add( m_icons->add(DEFAULT_WS_ICON, icon_t{new icon(m_conf.get<string>(name(), DEFAULT_WS_ICON, ""))});
DEFAULT_WS_ICON, icon_t{new icon(m_conf.get<string>(name(), DEFAULT_WS_ICON, ""))});
// }}}
// Add formats and create components {{{
for (auto workspace : m_conf.get_list<string>(name(), "ws-icon", {})) { for (auto workspace : m_conf.get_list<string>(name(), "ws-icon", {})) {
auto vec = string_util::split(workspace, ';'); auto vec = string_util::split(workspace, ';');
@ -42,44 +37,30 @@ namespace modules {
m_icons->add(vec[0], icon_t{new icon{vec[1]}}); m_icons->add(vec[0], icon_t{new icon{vec[1]}});
} }
// }}}
// Subscribe to ipc events {{{
try { try {
m_ipc.subscribe(i3ipc::ET_WORKSPACE); m_ipc.subscribe(i3ipc::ET_WORKSPACE);
m_ipc.prepare_to_event_handling(); m_ipc.prepare_to_event_handling();
} catch (std::runtime_error& err) {
throw module_error(err.what());
} catch (std::exception& err) { } catch (std::exception& err) {
throw module_error(err.what()); throw module_error(err.what());
} }
} // }}}
// }}} void i3_module::stop() { // {{{
}
void i3_module::stop() {
// Shutdown ipc connection when stopping the module {{{
try { try {
shutdown(m_ipc.get_event_socket_fd(), SHUT_RD); m_log.info("%s: Disconnecting from sockets", name());
shutdown(m_ipc.get_main_socket_fd(), SHUT_RD); shutdown(m_ipc.get_event_socket_fd(), SHUT_RDWR);
shutdown(m_ipc.get_main_socket_fd(), SHUT_RDWR);
} catch (...) { } catch (...) {
} }
event_module::stop(); event_module::stop();
} // }}}
// }}} bool i3_module::has_event() { // {{{
} return m_ipc.handle_event();
} // }}}
bool i3_module::has_event() {
if (!m_ipc.handle_event())
throw module_error("Socket connection closed...");
return true;
}
bool i3_module::update() {
// Refresh workspace data {{{
bool i3_module::update() { // {{{
m_workspaces.clear(); m_workspaces.clear();
i3_util::connection_t ipc; i3_util::connection_t ipc;
@ -138,8 +119,7 @@ namespace modules {
label->replace_token("%name%", wsname); label->replace_token("%name%", wsname);
label->replace_token("%icon%", icon->get()); label->replace_token("%icon%", icon->get());
label->replace_token("%index%", to_string(workspace->num)); label->replace_token("%index%", to_string(workspace->num));
m_workspaces.emplace_back( m_workspaces.emplace_back(make_unique<i3_workspace>(workspace->num, flag, std::move(label)));
make_unique<i3_workspace>(workspace->num, flag, std::move(label)));
} }
return true; return true;
@ -147,31 +127,36 @@ namespace modules {
m_log.err("%s: %s", name(), err.what()); m_log.err("%s: %s", name(), err.what());
return false; return false;
} }
} // }}}
// }}} bool i3_module::build(builder* builder, string tag) const { // {{{
}
bool i3_module::build(builder* builder, string tag) const {
// Output workspace info {{{
if (tag != TAG_LABEL_STATE) if (tag != TAG_LABEL_STATE)
return false; return false;
for (auto&& ws : m_workspaces) { if (m_scroll) {
builder->cmd(mousebtn::SCROLL_DOWN, EVENT_SCROLL_DOWN); builder->cmd(mousebtn::SCROLL_DOWN, EVENT_SCROLL_DOWN);
builder->cmd(mousebtn::SCROLL_UP, EVENT_SCROLL_UP); builder->cmd(mousebtn::SCROLL_UP, EVENT_SCROLL_UP);
}
for (auto&& ws : m_workspaces) {
if (m_click) {
builder->cmd(mousebtn::LEFT, string{EVENT_CLICK} + to_string(ws.get()->index)); builder->cmd(mousebtn::LEFT, string{EVENT_CLICK} + to_string(ws.get()->index));
builder->node(ws.get()->label); builder->node(ws.get()->label);
builder->cmd_close(true); builder->cmd_close(true);
} else {
builder->node(ws.get()->label);
} }
}
if (m_scroll) {
builder->cmd_close(true);
builder->cmd_close(true);
}
return true; return true;
} // }}}
// }}} bool i3_module::handle_event(string cmd) { // {{{
}
bool i3_module::handle_event(string cmd) {
// Send ipc commands {{{
if (cmd.compare(0, 2, EVENT_PREFIX) != 0) if (cmd.compare(0, 2, EVENT_PREFIX) != 0)
return false; return false;
@ -193,13 +178,7 @@ namespace modules {
} }
return true; return true;
} // }}}
// }}}
}
bool i3_module::receive_events() const {
return true;
}
} }
LEMONBUDDY_NS_END LEMONBUDDY_NS_END