[PATCH i-g-t v2 05/26] lib/igt_kms: Add helper to wait for new connectors
Louis Chauvet
louis.chauvet at bootlin.com
Thu Jul 17 18:46:25 UTC 2025
Add list comparison utilities and a new function to wait for new
connectors to appear on the system.
---
lib/igt_kms.c | 93 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
lib/igt_kms.h | 6 ++++
2 files changed, 99 insertions(+)
diff --git a/lib/igt_kms.c b/lib/igt_kms.c
index 27326eec49047bc521eadf39b9f25671ff1e9e26..c79f9b946ca84c7c374ca514c05def2c34f81383 100644
--- a/lib/igt_kms.c
+++ b/lib/igt_kms.c
@@ -7722,3 +7722,96 @@ uint32_t igt_get_connector_id_from_mst_path(int drm_fd, const void *mst_path)
return 0;
}
+
+/**
+ * list_contains() - Search an element in the list
+ *
+ * @list: Pointer to the list to search into
+ * @list_len: Length of the list
+ * @value: Value to search in the list
+ *
+ * Returns true if @list contains @value
+ */
+static bool list_contains(const uint32_t *list, int list_len, uint32_t value)
+{
+ igt_assert((list == NULL && list_len == 0) || list);
+ for (int i = 0; i < list_len; i++) {
+ if (list[i] == value)
+ return true;
+ }
+ return false;
+}
+
+/**
+ * get_list_diff() - Compute and return the difference between two lists
+ *
+ * @list_a: Pointer to the first list to compare
+ * @list_a_len: Length of the first list
+ * @list_b: Pointer to the second list to compare
+ * @list_b_len: Length of the second list
+ * @diff: Out pointer for the difference. Can be null to only count new elements.
+ *
+ * Returns the number of element which are in @list_a but not in @list_b.
+ */
+int
+get_list_diff(const uint32_t *list_a, int list_a_len, const uint32_t *list_b, int list_b_len,
+ uint32_t **diff)
+{
+ int diff_len = 0;
+
+ igt_assert((list_a == NULL && list_a_len == 0) || list_a);
+ igt_assert((list_b == NULL && list_b_len == 0) || list_b);
+
+ if (diff)
+ *diff = malloc(0);
+
+ for (int i = 0; i < list_a_len; i++) {
+ if (!list_contains(list_b, list_b_len, list_a[i])) {
+ if (diff) {
+ *diff = reallocarray(*diff, diff_len + 1, sizeof(**diff));
+ igt_assert(*diff);
+ (*diff)[diff_len] = list_a[i];
+ }
+
+ diff_len++;
+ }
+ }
+
+ return diff_len;
+}
+
+/**
+ * kms_wait_for_new_connectors() - Wait for new connector to appear
+ *
+ * @list_a: Pointer to the first list to compare
+ * @list_a_len: Length of the first list
+ * @list_b: Pointer to the second list to compare
+ * @list_b_len: Length of the second list
+ * @diff: Out pointer for the difference. Can be null.
+ *
+ * Returns the number of element which differ between the two lists.
+ */
+int kms_wait_for_new_connectors(uint32_t **newly_connected,
+ const uint32_t *already_connected,
+ int already_connected_count, int drm_fd)
+{
+ int newly_connected_count;
+ struct timespec start, end;
+
+ igt_assert(newly_connected);
+ igt_assert(drm_fd);
+
+ clock_gettime(CLOCK_MONOTONIC, &start);
+ clock_gettime(CLOCK_MONOTONIC, &end);
+ do {
+ if (*newly_connected)
+ free(*newly_connected);
+ newly_connected_count = igt_get_connected_connectors(drm_fd, newly_connected);
+ clock_gettime(CLOCK_MONOTONIC, &end);
+ } while (get_list_diff(*newly_connected, newly_connected_count,
+ already_connected, already_connected_count,
+ NULL) == 0 &&
+ igt_time_elapsed(&start, &end) <= igt_default_display_detect_timeout());
+
+ return newly_connected_count;
+}
diff --git a/lib/igt_kms.h b/lib/igt_kms.h
index 8939ed84bd016ab03b6f165621b62664039bca9e..2149dbbb85f44edeb2c7da2baf2d57cb2d890554 100644
--- a/lib/igt_kms.h
+++ b/lib/igt_kms.h
@@ -1298,4 +1298,10 @@ drmModeConnectorPtr igt_get_connector_from_name(int drm_fd, const char *port_nam
uint32_t igt_get_connector_id_from_name(int drm_fd, const char *port_name);
uint32_t igt_get_connector_id_from_mst_path(int drm_fd, const void *mst_path);
+int kms_wait_for_new_connectors(uint32_t **newly_connected,
+ const uint32_t *already_connected,
+ int already_connected_count, int drm_fd);
+int
+get_list_diff(const uint32_t *list_a, int list_a_len, const uint32_t *list_b, int list_b_len,
+ uint32_t **diff);
#endif /* __IGT_KMS_H__ */
--
2.50.0
More information about the igt-dev
mailing list