feat(monitor): use primary monitor as default (#1426)

If the monitor option isn't set, the primary monitor (if connected) is chosen as the default display.
This commit is contained in:
NBonaparte 2019-01-20 11:20:30 -08:00 committed by GitHub
parent b03f14400a
commit 1145681cf5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 29 additions and 6 deletions

View File

@ -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<monitor_t> get_monitors(connection& conn, xcb_window_t root, bool connected_only = false, bool realloc = false);
monitor_t match_monitor(vector<monitor_t> monitors, const string& name, bool exact_match);

View File

@ -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{};

View File

@ -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;

View File

@ -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
}