+ * randr_query_outputs_15 uses RandR ≥ 1.5 to update outputs.
+ *
+ */
+static bool randr_query_outputs_15(void) {
+#if XCB_RANDR_MINOR_VERSION < 5
+ return false;
+#else
+ /* RandR 1.5 available at compile-time, i.e. libxcb is new enough */
+ if (!has_randr_1_5) {
+ return false;
+ }
+ /* RandR 1.5 available at run-time (supported by the server and not
+ * disabled by the user) */
+ DLOG("Querying outputs using RandR 1.5\n");
+ xcb_generic_error_t *err;
+ xcb_randr_get_monitors_reply_t *monitors =
+ xcb_randr_get_monitors_reply(
+ conn, xcb_randr_get_monitors(conn, root, true), &err);
+ if (err != NULL) {
+ ELOG("Could not get RandR monitors: X11 error code %d\n", err->error_code);
+ free(err);
+ /* Fall back to RandR ≤ 1.4 */
+ return false;
+ }
+
+ /* Mark all outputs as to_be_disabled, since xcb_randr_get_monitors() will
+ * only return active outputs. */
+ Output *output;
+ TAILQ_FOREACH(output, &outputs, outputs) {
+ if (output != root_output) {
+ output->to_be_disabled = true;
+ }
+ }
+
+ DLOG("%d RandR monitors found (timestamp %d)\n",
+ xcb_randr_get_monitors_monitors_length(monitors),
+ monitors->timestamp);
+
+ xcb_randr_monitor_info_iterator_t iter;
+ for (iter = xcb_randr_get_monitors_monitors_iterator(monitors);
+ iter.rem;
+ xcb_randr_monitor_info_next(&iter)) {
+ const xcb_randr_monitor_info_t *monitor_info = iter.data;
+ xcb_get_atom_name_reply_t *atom_reply =
+ xcb_get_atom_name_reply(
+ conn, xcb_get_atom_name(conn, monitor_info->name), &err);
+ if (err != NULL) {
+ ELOG("Could not get RandR monitor name: X11 error code %d\n", err->error_code);
+ free(err);
+ continue;
+ }
+ char *name;
+ sasprintf(&name, "%.*s",
+ xcb_get_atom_name_name_length(atom_reply),
+ xcb_get_atom_name_name(atom_reply));
+ free(atom_reply);
+
+ Output *new = get_output_by_name(name, false);
+ if (new == NULL) {
+ new = scalloc(1, sizeof(Output));
+
+ SLIST_INIT(&new->names_head);
+
+ /* Register associated output names in addition to the monitor name */
+ xcb_randr_output_t *randr_outputs = xcb_randr_monitor_info_outputs(monitor_info);
+ int randr_output_len = xcb_randr_monitor_info_outputs_length(monitor_info);
+ for (int i = 0; i < randr_output_len; i++) {
+ xcb_randr_output_t randr_output = randr_outputs[i];
+
+ xcb_randr_get_output_info_reply_t *info =
+ xcb_randr_get_output_info_reply(conn,
+ xcb_randr_get_output_info(conn, randr_output, monitors->timestamp),
+ NULL);
+
+ if (info != NULL && info->crtc != XCB_NONE) {
+ char *oname;
+ sasprintf(&oname, "%.*s",
+ xcb_randr_get_output_info_name_length(info),
+ xcb_randr_get_output_info_name(info));
+
+ if (strcmp(name, oname) != 0) {
+ struct output_name *output_name = scalloc(1, sizeof(struct output_name));
+ output_name->name = sstrdup(oname);
+ SLIST_INSERT_HEAD(&new->names_head, output_name, names);
+ } else {
+ free(oname);
+ }
+ }
+ FREE(info);
+ }
+
+ /* Insert the monitor name last, so that it's used as the primary name */
+ struct output_name *output_name = scalloc(1, sizeof(struct output_name));
+ output_name->name = sstrdup(name);
+ SLIST_INSERT_HEAD(&new->names_head, output_name, names);
+
+ if (monitor_info->primary) {
+ TAILQ_INSERT_HEAD(&outputs, new, outputs);
+ } else {
+ TAILQ_INSERT_TAIL(&outputs, new, outputs);
+ }
+ }
+ /* We specified get_active == true in xcb_randr_get_monitors(), so we
+ * will only receive active outputs. */
+ new->active = true;
+ new->to_be_disabled = false;
+
+ new->primary = monitor_info->primary;
+
+ new->changed =
+ update_if_necessary(&(new->rect.x), monitor_info->x) |
+ update_if_necessary(&(new->rect.y), monitor_info->y) |
+ update_if_necessary(&(new->rect.width), monitor_info->width) |
+ update_if_necessary(&(new->rect.height), monitor_info->height);
+
+ DLOG("name %s, x %d, y %d, width %d px, height %d px, width %d mm, height %d mm, primary %d, automatic %d\n",
+ name,
+ monitor_info->x, monitor_info->y, monitor_info->width, monitor_info->height,
+ monitor_info->width_in_millimeters, monitor_info->height_in_millimeters,
+ monitor_info->primary, monitor_info->automatic);
+ free(name);
+ }
+ free(monitors);
+ return true;
+#endif
+}
+
+/*
+ * Gets called by randr_query_outputs_14() for each output. The function adds
+ * new outputs to the list of outputs, checks if the mode of existing outputs
+ * has been changed or if an existing output has been disabled. It will then
+ * change either the "changed" or the "to_be_deleted" flag of the output, if