From d162f96a63600d5b45bb8294afdb84efd85833f9 Mon Sep 17 00:00:00 2001 From: Florian RICHER Date: Sat, 13 Jan 2024 18:24:47 +0100 Subject: [PATCH] Find white torch --- quicksettings/flashlight/flashlightutil.cpp | 53 +++++++++++++++------ 1 file changed, 39 insertions(+), 14 deletions(-) diff --git a/quicksettings/flashlight/flashlightutil.cpp b/quicksettings/flashlight/flashlightutil.cpp index 49859159..cd085a68 100644 --- a/quicksettings/flashlight/flashlightutil.cpp +++ b/quicksettings/flashlight/flashlightutil.cpp @@ -76,24 +76,45 @@ void FlashlightUtil::findTorchDevice() udev_enumerate_scan_devices(enumerate); struct udev_list_entry *devices = udev_enumerate_get_list_entry(enumerate); - struct udev_list_entry *entry = udev_list_entry_get_next(devices); + struct udev_list_entry *entry = nullptr; - if (entry == nullptr) { - qWarning() << "No flashlight found"; - return; + struct udev_device *device = nullptr; + + udev_list_entry_foreach(entry, devices) + { + const char *path = udev_list_entry_get_name(entry); + + if (path == nullptr) { + continue; + } + + if (device != nullptr) { + udev_device_unref(device); // Use to free memory from previous loop iteration + } + + device = udev_device_new_from_syspath(udev, path); + + if (device == nullptr) { + continue; + } + + qInfo() << "Found flashlight device : " << path; + + const char *color = udev_device_get_sysattr_value(device, "color"); + + if (color == nullptr) { + continue; + } + + qInfo() << "Flash color : " << color; + + if (std::strcmp(color, "white") == 0) { + break; + } } - const char *path = udev_list_entry_get_name(entry); - - if (path == nullptr) { - qWarning() << "Failed to get path from udev entry"; - return; - } - - struct udev_device *device = udev_device_new_from_syspath(udev, path); - if (device == nullptr) { - qWarning() << "Failed to get udev device"; + qWarning() << "No flashlight device found"; return; } @@ -104,6 +125,8 @@ void FlashlightUtil::findTorchDevice() return; } + qInfo() << "Flash maxBrightness : " << maxBrightness; + const char *brightness = udev_device_get_sysattr_value(device, "brightness"); if (brightness == nullptr) { @@ -111,6 +134,8 @@ void FlashlightUtil::findTorchDevice() return; } + qInfo() << "Flash brightness : " << brightness; + m_maxBrightness = maxBrightness; m_device = device; m_isAvailable = true;