From patchwork Thu Nov 19 16:39:43 2026 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jacopo Mondi X-Patchwork-Id: 29041 Return-Path: X-Original-To: parsemail@patchwork.libcamera.org Delivered-To: parsemail@patchwork.libcamera.org Received: from lancelot.ideasonboard.com (lancelot.ideasonboard.com [92.243.16.209]) by patchwork.libcamera.org (Postfix) with ESMTPS id A4FC8BDE17 for ; Thu, 19 Nov 2026 16:40:01 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id 647C66A930; Thu, 19 Nov 2026 17:39:57 +0100 (CET) Authentication-Results: lancelot.ideasonboard.com; dkim=pass (1024-bit key; unprotected) header.d=ideasonboard.com header.i=@ideasonboard.com header.b="EfVwtqxN"; dkim-atps=neutral Received: from perceval.ideasonboard.com (perceval.ideasonboard.com [213.167.242.64]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id DABDB6A922 for ; Thu, 19 Nov 2026 17:39:53 +0100 (CET) Received: from [192.168.1.7] (unknown [93.65.100.155]) by perceval.ideasonboard.com (Postfix) with ESMTPSA id 892B11F10; Thu, 19 Nov 2026 17:37:24 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com; s=mail; t=1795106244; bh=dOWUB+yXzRftj3oIpSJg9OwBOmSl/b2KwGl8YSKLPj8=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=EfVwtqxNJfK0Zs0zFE4vTQQLw4ldbVnJcTPEd464C9CZ8rc7KYwIzr2sqSey935G2 FJJwsHUA0OJddiWSoDmpJtdy8fG/kFsAysTcNN9PqAc8FNMZX1jBuDA2nNlqxQ0WYu NK7qwZzZi+e7cD9937ilb4Hc/rQXASt88ZFUW11A= From: Jacopo Mondi Date: Thu, 19 Nov 2026 17:39:43 +0100 Subject: [PATCH 1/6] libcamera: Move DeviceMatch::match() to MediaDevice MIME-Version: 1.0 Message-Id: <20261119-multicontext-prep-v1-1-f68ea7ef960f@ideasonboard.com> References: <20261119-multicontext-prep-v1-0-f68ea7ef960f@ideasonboard.com> In-Reply-To: <20261119-multicontext-prep-v1-0-f68ea7ef960f@ideasonboard.com> To: libcamera-devel@lists.libcamera.org Cc: Jacopo Mondi X-Mailer: b4 0.16.0 X-Developer-Signature: v=1; a=openpgp-sha256; l=8080; i=jacopo.mondi@ideasonboard.com; h=from:subject:message-id; bh=dOWUB+yXzRftj3oIpSJg9OwBOmSl/b2KwGl8YSKLPj8=; b=owEBbQKS/ZANAwAKAXI0Bo8WoVY8AcsmYgBq/yZYB91532Uh8aC66fYY8vfGgBAE1RT/O+yLY bNjD/WXiJuJAjMEAAEKAB0WIQS1xD1IgJogio9YOMByNAaPFqFWPAUCav8mWAAKCRByNAaPFqFW PAGmD/9ywBQNOLJC1D3lXk+HoZd/0SSEGKO13FeFgPkQD0mvtVjbqJ0zvWRR1Jx1mtuRZmtdu/N 43pR39StuJmeY09MX1jyZPcdjcw9WWL+oE4d2VegebMTmY5v0T91Lvt9eT1kMyTGgTF8X3CT4GI 2wPCD/jmEL3ReFSIMdwAU7CHcEJw9ubaB6ZpJqGkgPe2hEHBLzZYIg33P6eO07kjrLne6rhno0F VaI1e25YYznr1qSrTiJ1Mu7937flN3S0zrmHyOhiJLtJcj/7zUtaqHJd55zEYC8LbWUhZarlaxf djpNI72eOBCk/43brl+JBTi+deDX8uNR0lWrwJAGgaykrAY7fMGWHGcXdxrWApWtIBjE3o4ur/U ys12qooWRCW2rHBjx2dOsmIo06BBAIHw2UsjtJzJ2sYdfETixU1gcFFyjF2jcRiFNYo5+b9QGUl cUcp6IKgy+jxr4g3tBp0F1Shicmj9zU5im02vsPzFvG/ZyXEpgsMDBHWctAn4CWcJ8xMOHmOimz YPB6z+OhYsYImTOPzrQ5OtG+cW0Ct6cMEGNhF9eQitCWHK9ZR4TAPdslc/xGiZjLy6oA3u4V/ox QedjPZntIMNwJyA5p2bbxQGlfxxSBNZXwk3VscwwKw7fBYNmcddRqUW6IEAcXWxC5oNShya40tk oC8MU5f++cS23HQ== X-Developer-Key: i=jacopo.mondi@ideasonboard.com; a=openpgp; fpr=72392EDC88144A65C701EA9BA5826A2587AD026B X-BeenThere: libcamera-devel@lists.libcamera.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: libcamera-devel-bounces@lists.libcamera.org Sender: "libcamera-devel" The DeviceMatch class offers a match() function that compares its search criteria with the MediaDevice driver name and all its entities names. To prepare to remove all users of the entities() function from the DeviceEnumerator and MediaDevice users, move the match() function to the MediaDevice class. Add accessors to the DeviceMatch class so that the MediaDevice can compare itself to the search patterns. While at it perform a little rename of the DeviceMatch class members to allow to have more expressive function names, to maintain the convention that accessors have the same name as the fields they return. Signed-off-by: Jacopo Mondi --- include/libcamera/internal/device_enumerator.h | 10 +-- include/libcamera/internal/media_device.h | 4 ++ src/libcamera/device_enumerator.cpp | 89 ++++++-------------------- src/libcamera/media_device.cpp | 69 ++++++++++++++++++++ 4 files changed, 100 insertions(+), 72 deletions(-) diff --git a/include/libcamera/internal/device_enumerator.h b/include/libcamera/internal/device_enumerator.h index 5e1aec9924c5..5d142c2e0b31 100644 --- a/include/libcamera/internal/device_enumerator.h +++ b/include/libcamera/internal/device_enumerator.h @@ -23,15 +23,17 @@ class DeviceMatch public: DeviceMatch(const std::string &driver); + const std::string &driver() const { return driver_; } + const std::vector &entitiesNames() const { return entitiesNames_; } + const std::vector &entitiesRegexs() const { return entitiesRegexs_; } + void add(const std::string &entity); void add(std::regex entity); - bool match(const MediaDevice *device) const; - private: std::string driver_; - std::vector entities_; - std::vector entityRegexs_; + std::vector entitiesNames_; + std::vector entitiesRegexs_; }; class DeviceEnumerator diff --git a/include/libcamera/internal/media_device.h b/include/libcamera/internal/media_device.h index 5c1f14b63823..c1405142f726 100644 --- a/include/libcamera/internal/media_device.h +++ b/include/libcamera/internal/media_device.h @@ -24,6 +24,8 @@ namespace libcamera { +class DeviceMatch; + class MediaDevice : protected Loggable { public: @@ -40,6 +42,8 @@ public: int populate(); bool isValid() const { return valid_; } + bool match(const DeviceMatch &device) const; + const std::string &driver() const { return driver_; } const std::string &deviceNode() const { return deviceNode_; } const std::string &model() const { return model_; } diff --git a/src/libcamera/device_enumerator.cpp b/src/libcamera/device_enumerator.cpp index 0ce65e89a67f..2b1056425f61 100644 --- a/src/libcamera/device_enumerator.cpp +++ b/src/libcamera/device_enumerator.cpp @@ -73,13 +73,31 @@ DeviceMatch::DeviceMatch(const std::string &driver) { } +/** + * \fn DeviceMatch::driver() + * \brief Retrieve the target media device driver name + * \return The media device driver name + */ + +/** + * \fn DeviceMatch::entitiesNames() + * \brief Retrieve the list of target media entities names + * \return A vector of media entities names + */ + +/** + * \fn DeviceMatch::entitiesRegexs() + * \brief Retrieve the list of target media entities search patterns + * \return A vector of media entities search patterns + */ + /** * \brief Add a media entity name to the search pattern * \param[in] entity The name of the entity in the media graph */ void DeviceMatch::add(const std::string &entity) { - entities_.push_back(entity); + entitiesNames_.push_back(entity); } /** @@ -88,72 +106,7 @@ void DeviceMatch::add(const std::string &entity) */ void DeviceMatch::add(std::regex entity) { - entityRegexs_.push_back(std::move(entity)); -} - -/** - * \brief Compare a search pattern with a media device - * \param[in] device The media device - * - * Matching is performed on the Linux device driver name and entity names from - * the media graph. A match is found if both the driver name matches and the - * media device contains all the entities listed in the search pattern. - * - * \return true if the media device matches the search pattern, false otherwise - */ -bool DeviceMatch::match(const MediaDevice *device) const -{ - if (driver_ != device->driver()) - return false; - - for (const std::string &name : entities_) { - bool found = false; - - for (const MediaEntity *entity : device->entities()) { - if (name == entity->name()) { - if (!entity->deviceNode().empty()) { - found = true; - break; - } else { - LOG(DeviceEnumerator, Debug) - << "Skip " << entity->name() - << ": no device node"; - } - } - } - - if (!found) - return false; - } - - for (const std::regex &nameRegex : entityRegexs_) { - bool found = false; - - for (const MediaEntity *entity : device->entities()) { - if (!std::regex_search(entity->name(), nameRegex)) - continue; - - if (found) { - LOG(DeviceEnumerator, Error) - << "Multiple entities match regex"; - return false; - } - - if (entity->deviceNode().empty()) { - LOG(DeviceEnumerator, Debug) - << "Skip " << entity->name() - << ": no device node"; - continue; - } - - found = true; - } - - if (!found) - return false; - } - - return true; + entitiesRegexs_.push_back(std::move(entity)); } /** @@ -351,7 +304,7 @@ std::shared_ptr DeviceEnumerator::search(const DeviceMatch &dm) if (media->busy()) continue; - if (dm.match(media.get())) { + if (media->match(dm)) { LOG(DeviceEnumerator, Debug) << "Successful match for media device \"" << media->driver() << "\""; diff --git a/src/libcamera/media_device.cpp b/src/libcamera/media_device.cpp index 3f974eaabd67..b50d7328e47c 100644 --- a/src/libcamera/media_device.cpp +++ b/src/libcamera/media_device.cpp @@ -20,6 +20,7 @@ #include #include +#include "libcamera/internal/device_enumerator.h" #include "libcamera/internal/v4l2_request.h" /** @@ -286,6 +287,74 @@ done: * \return true if the media graph is valid, false otherwise */ +/** + * \brief Compare a media device with a search pattern + * \param[in] device The media device search pattern + * + * Matching is performed on the Linux device driver name and entity names from + * the \a device search pattern. A match is found if both the driver name + * matches and the media device contains all the entities listed in the search + * pattern. + * + * \return True if the media device matches the search pattern, false otherwise + */ +bool MediaDevice::match(const DeviceMatch &device) const +{ + ASSERT(isValid()); + + if (driver_ != device.driver()) + return false; + + for (const std::string &name : device.entitiesNames()) { + bool found = false; + + for (const MediaEntity *entity : entities()) { + if (name == entity->name()) { + if (!entity->deviceNode().empty()) { + found = true; + break; + } else { + LOG(MediaDevice, Debug) + << "Skip " << entity->name() + << ": no device node"; + } + } + } + + if (!found) + return false; + } + + for (const std::regex &nameRegex : device.entitiesRegexs()) { + bool found = false; + + for (const MediaEntity *entity : entities()) { + if (!std::regex_search(entity->name(), nameRegex)) + continue; + + if (found) { + LOG(MediaDevice, Error) + << "Multiple entities match regex"; + return false; + } + + if (entity->deviceNode().empty()) { + LOG(MediaDevice, Debug) + << "Skip " << entity->name() + << ": no device node"; + continue; + } + + found = true; + } + + if (!found) + return false; + } + + return true; +} + /** * \fn MediaDevice::driver() * \brief Retrieve the media device driver name