From b8bbcfdb9677130dc0667651df72053ebc788c71 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Barnab=C3=A1s=20P=C5=91cze?= Date: Wed, 23 Jul 2025 14:23:00 +0200 Subject: [PATCH] spa: libcamera: source: unify control range logic If libcamera does not provide a default value, then the average of the minimum and maximum values is taken. The same logic is duplicated for `float` and `int32_t`, so move it into a function template. (cherry picked from commit 8d9e469e09ab2a6bf25a74b756eb7303a3cf2dbb) --- spa/plugins/libcamera/libcamera-source.cpp | 37 ++++++++++++---------- 1 file changed, 21 insertions(+), 16 deletions(-) diff --git a/spa/plugins/libcamera/libcamera-source.cpp b/spa/plugins/libcamera/libcamera-source.cpp index 058d1a159..e61f30a17 100644 --- a/spa/plugins/libcamera/libcamera-source.cpp +++ b/spa/plugins/libcamera/libcamera-source.cpp @@ -4,10 +4,12 @@ /* SPDX-FileCopyrightText: Copyright © 2021 Wim Taymans */ /* SPDX-License-Identifier: MIT */ +#include #include #include #include #include +#include #include #include @@ -739,6 +741,23 @@ uint32_t prop_id_to_control(uint32_t prop_id) return SPA_ID_INVALID; } +template +[[nodiscard]] +std::array control_info_to_range(const libcamera::ControlInfo& cinfo) +{ + static_assert(std::is_arithmetic_v); + + auto min = cinfo.min().get(); + auto max = cinfo.max().get(); + spa_assert(min <= max); + + auto def = !cinfo.def().isNone() + ? cinfo.def().get() + : (min + ((max - min) / 2)); + + return {{ min, max, def }}; +} + [[nodiscard]] spa_pod *control_details_to_pod(spa_pod_builder& b, const libcamera::ControlId& cid, const libcamera::ControlInfo& cinfo) @@ -773,14 +792,7 @@ spa_pod *control_details_to_pod(spa_pod_builder& b, spa_pod_builder_pop(&b, &f); } break; case ControlTypeFloat: { - float min = cinfo.min().get(); - float max = cinfo.max().get(); - float def; - - if (cinfo.def().isNone()) - def = (min + max) / 2; - else - def = cinfo.def().get(); + auto [ min, max, def ] = control_info_to_range(cinfo); spa_pod_builder_add(&b, SPA_PROP_INFO_type, SPA_POD_CHOICE_RANGE_Float( @@ -788,14 +800,7 @@ spa_pod *control_details_to_pod(spa_pod_builder& b, 0); } break; case ControlTypeInteger32: { - int32_t min = cinfo.min().get(); - int32_t max = cinfo.max().get(); - int32_t def; - - if (cinfo.def().isNone()) - def = (min + max) / 2; - else - def = cinfo.def().get(); + auto [ min, max, def ] = control_info_to_range(cinfo); spa_pod_builder_add(&b, SPA_PROP_INFO_type, SPA_POD_CHOICE_RANGE_Int(