From e72f85079fdf177b0ba260822240a459533156ed Mon Sep 17 00:00:00 2001 From: NBonaparte <98007b33@opayq.com> Date: Tue, 13 Dec 2016 19:33:43 -0800 Subject: [PATCH] feat(monitor): Add monitor-fallback option #243 Adds new parameter monitor-fallback, which allows the bar to revert to a specified monitor. Polybar should now be compatible with dynamic multi-monitor setups when using screenchange-reload = true (no need to stop and restart the bar with a different environment variable). Closes #243 --- src/components/bar.cpp | 24 ++++++++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-) diff --git a/src/components/bar.cpp b/src/components/bar.cpp index c6af6ac4..5d25cfa6 100644 --- a/src/components/bar.cpp +++ b/src/components/bar.cpp @@ -61,26 +61,42 @@ bar::bar(connection& conn, signal_emitter& emitter, const config& config, const // Get available RandR outputs auto monitor_name = m_conf.get(bs, "monitor", ""); + auto monitor_name_fallback = m_conf.get(bs, "monitor-fallback", ""); auto monitor_strictmode = m_conf.get(bs, "monitor-strict", false); auto monitors = randr_util::get_monitors(m_connection, m_connection.screen()->root, monitor_strictmode); if (monitors.empty()) { throw application_error("No monitors found"); - } else if (monitor_name.empty()) { + } + + if (monitor_name.empty()) { monitor_name = monitors[0]->name; m_log.warn("No monitor specified, using \"%s\"", monitor_name); } - // Match against the defined monitor name + bool name_found{false}; + bool fallback_found{monitor_name_fallback.empty()}; + monitor_t fallback{}; + for (auto&& monitor : monitors) { - if (monitor->match(monitor_name, monitor_strictmode)) { + if (!name_found && (name_found = monitor->match(monitor_name, monitor_strictmode))) { m_opts.monitor = move(monitor); + } else if (!fallback_found && (fallback_found = monitor->match(monitor_name_fallback, monitor_strictmode))) { + fallback = move(monitor); + } + + if (name_found && fallback_found) { break; } } if (!m_opts.monitor) { - throw application_error("Monitor \"" + monitor_name + "\" not found or disconnected"); + if (fallback) { + m_opts.monitor = move(fallback); + m_log.warn("Monitor \"%s\" not found, reverting to fallback \"%s\"", monitor_name, monitor_name_fallback); + } else { + throw application_error("Monitor \"" + monitor_name + "\" not found or disconnected"); + } } m_log.trace("bar: Loaded monitor %s (%ix%i+%i+%i)", m_opts.monitor->name, m_opts.monitor->w, m_opts.monitor->h,