diff --git a/include/x11/extensions/randr.hpp b/include/x11/extensions/randr.hpp index 515a059e..29954159 100644 --- a/include/x11/extensions/randr.hpp +++ b/include/x11/extensions/randr.hpp @@ -36,6 +36,7 @@ struct randr_output { short int y{0}; xcb_randr_output_t output; backlight_values backlight; + bool primary{false}; bool match(const string& o, bool exact = true) const; bool match(const position& p) const; @@ -48,7 +49,8 @@ namespace randr_util { bool check_monitor_support(); - monitor_t make_monitor(xcb_randr_output_t randr, string name, unsigned short int w, unsigned short int h, short int x, short int y); + monitor_t make_monitor(xcb_randr_output_t randr, string name, unsigned short int w, unsigned short int h, short int x, short int y, + bool primary); vector get_monitors(connection& conn, xcb_window_t root, bool connected_only = false, bool realloc = false); monitor_t match_monitor(vector monitors, const string& name, bool exact_match); diff --git a/src/components/bar.cpp b/src/components/bar.cpp index 79e1321a..42c5e559 100644 --- a/src/components/bar.cpp +++ b/src/components/bar.cpp @@ -80,6 +80,17 @@ bar::bar(connection& conn, signal_emitter& emitter, const config& config, const throw application_error("No monitors found"); } + // if monitor_name is not defined, first check for primary monitor + if (monitor_name.empty()) { + for (auto&& mon : monitors) { + if (mon->primary) { + monitor_name = mon->name; + break; + } + } + } + + // if still not found (and not strict matching), get first connected monitor if (monitor_name.empty() && !m_opts.monitor_strict) { auto connected_monitors = randr_util::get_monitors(m_connection, m_connection.screen()->root, true); if (!connected_monitors.empty()) { @@ -88,11 +99,13 @@ bar::bar(connection& conn, signal_emitter& emitter, const config& config, const } } + // if still not found, get first monitor if (monitor_name.empty()) { monitor_name = monitors[0]->name; m_log.warn("No monitor specified, using \"%s\"", monitor_name); } + // get the monitor data based on the name m_opts.monitor = randr_util::match_monitor(monitors, monitor_name, m_opts.monitor_exact); monitor_t fallback{}; diff --git a/src/main.cpp b/src/main.cpp index 14de301a..7df263ae 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -78,9 +78,10 @@ int main(int argc, char** argv) { if (cli->has("list-monitors")) { for (auto&& mon : randr_util::get_monitors(conn, conn.root(), true)) { if (WITH_XRANDR_MONITORS && mon->output == XCB_NONE) { - printf("%s: %ix%i+%i+%i (XRandR monitor)\n", mon->name.c_str(), mon->w, mon->h, mon->x, mon->y); + printf("%s: %ix%i+%i+%i (XRandR monitor%s)\n", mon->name.c_str(), mon->w, mon->h, mon->x, mon->y, + mon->primary ? ", primary" : ""); } else { - printf("%s: %ix%i+%i+%i\n", mon->name.c_str(), mon->w, mon->h, mon->x, mon->y); + printf("%s: %ix%i+%i+%i%s\n", mon->name.c_str(), mon->w, mon->h, mon->x, mon->y, mon->primary ? " (primary)" : ""); } } return EXIT_SUCCESS; diff --git a/src/x11/extensions/randr.cpp b/src/x11/extensions/randr.cpp index bd11806d..5dfbd1cd 100644 --- a/src/x11/extensions/randr.cpp +++ b/src/x11/extensions/randr.cpp @@ -64,7 +64,8 @@ namespace randr_util { * Define monitor */ monitor_t make_monitor( - xcb_randr_output_t randr, string name, unsigned short int w, unsigned short int h, short int x, short int y) { + xcb_randr_output_t randr, string name, unsigned short int w, unsigned short int h, short int x, short int y, + bool primary) { monitor_t mon{new monitor_t::element_type{}}; mon->output = randr; mon->name = move(name); @@ -72,6 +73,7 @@ namespace randr_util { mon->y = y; mon->h = h; mon->w = w; + mon->primary = primary; return mon; } @@ -92,13 +94,17 @@ namespace randr_util { for (auto&& mon : conn.get_monitors(root, true).monitors()) { try { auto name = conn.get_atom_name(mon.name).name(); - monitors.emplace_back(make_monitor(XCB_NONE, move(name), mon.width, mon.height, mon.x, mon.y)); + monitors.emplace_back(make_monitor(XCB_NONE, move(name), mon.width, mon.height, mon.x, mon.y, mon.primary)); } catch (const exception&) { // silently ignore output } } } #endif + auto primary_output = conn.get_output_primary(root).output(); + auto primary_info = conn.get_output_info(primary_output); + auto name_iter = primary_info.name(); + string primary_name = {name_iter.begin(), name_iter.end()}; for (auto&& output : conn.get_screen_resources(root).outputs()) { try { @@ -124,7 +130,8 @@ namespace randr_util { #endif auto crtc = conn.get_crtc_info(info->crtc); - monitors.emplace_back(make_monitor(output, move(name), crtc->width, crtc->height, crtc->x, crtc->y)); + auto primary = (primary_name == name); + monitors.emplace_back(make_monitor(output, move(name), crtc->width, crtc->height, crtc->x, crtc->y, primary)); } catch (const exception&) { // silently ignore output }