From 5dc4ac2bb2ae5e0040675e76218e8f536885b977 Mon Sep 17 00:00:00 2001 From: erodozer Date: Thu, 24 Apr 2025 17:00:15 -0400 Subject: [PATCH] linux: respect order of device list when enumerating camera feeds --- modules/camera/camera_linux.cpp | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/modules/camera/camera_linux.cpp b/modules/camera/camera_linux.cpp index c95069442e..4991f0238e 100644 --- a/modules/camera/camera_linux.cpp +++ b/modules/camera/camera_linux.cpp @@ -61,23 +61,23 @@ void CameraLinux::_update_devices() { } } - DIR *devices = opendir("/dev"); + struct dirent **devices; + int count = scandir("/dev", &devices, nullptr, alphasort); - if (devices) { - struct dirent *device; - - while ((device = readdir(devices)) != nullptr) { - if (strncmp(device->d_name, "video", 5) != 0) { - continue; - } - String device_name = String("/dev/") + String(device->d_name); - if (!_has_device(device_name)) { - _add_device(device_name); + if (count != -1) { + for (int i = 0; i < count; i++) { + struct dirent *device = devices[i]; + if (strncmp(device->d_name, "video", 5) == 0) { + String device_name = String("/dev/") + String(device->d_name); + if (!_has_device(device_name)) { + _add_device(device_name); + } } + free(device); } } - closedir(devices); + free(devices); } usleep(1000000);