From patchwork Mon Jul 12 21:56:41 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Laurent Pinchart X-Patchwork-Id: 12939 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 DEC72C3228 for ; Mon, 12 Jul 2021 21:57:58 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id 7780D6853C; Mon, 12 Jul 2021 23:57:58 +0200 (CEST) Authentication-Results: lancelot.ideasonboard.com; dkim=fail reason="signature verification failed" (1024-bit key; unprotected) header.d=ideasonboard.com header.i=@ideasonboard.com header.b="LQHZrTe/"; dkim-atps=neutral Received: from perceval.ideasonboard.com (perceval.ideasonboard.com [IPv6:2001:4b98:dc2:55:216:3eff:fef7:d647]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id 8F2D96853B for ; Mon, 12 Jul 2021 23:57:44 +0200 (CEST) Received: from pendragon.lan (62-78-145-57.bb.dnainternet.fi [62.78.145.57]) by perceval.ideasonboard.com (Postfix) with ESMTPSA id 2EDCA56B for ; Mon, 12 Jul 2021 23:57:44 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com; s=mail; t=1626127064; bh=hQPmp3148aQvFoWcHfvQGluM84QoY7DMKKMP68IFNpw=; h=From:To:Subject:Date:In-Reply-To:References:From; b=LQHZrTe/DTjty4mJN78bCSuOPmlCyi48f+P69FJOHhrdpnaq9oARLegxRYl3gjxvU fYbycqNd5GjsEKOJexnalQRqHBHuVQxjSR29iKaRJ/oUUAvU2EotbUHKVZHQiRGV9o 2o1Lnvv1XxJlg4RMmi4DmLVYz8c21daylk16gCjc= From: Laurent Pinchart To: libcamera-devel@lists.libcamera.org Date: Tue, 13 Jul 2021 00:56:41 +0300 Message-Id: <20210712215645.30478-27-laurent.pinchart@ideasonboard.com> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20210712215645.30478-1-laurent.pinchart@ideasonboard.com> References: <20210712215645.30478-1-laurent.pinchart@ideasonboard.com> MIME-Version: 1.0 Subject: [libcamera-devel] [PATCH v2 26/30] cam: Reorganize run() function and merge the two event loops 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" Reorganize the run() function to make it more readable, and merge the two event loops into one as capture and hotplug monitoring don't have to be mutually exclusive. Signed-off-by: Laurent Pinchart Reviewed-by: Kieran Bingham --- src/cam/main.cpp | 60 ++++++++++++++++++++++++------------------------ 1 file changed, 30 insertions(+), 30 deletions(-) diff --git a/src/cam/main.cpp b/src/cam/main.cpp index 380c14e02cf0..a3f19d0fa61b 100644 --- a/src/cam/main.cpp +++ b/src/cam/main.cpp @@ -5,6 +5,7 @@ * main.cpp - cam - The libcamera swiss army knife */ +#include #include #include #include @@ -48,6 +49,7 @@ private: std::unique_ptr cm_; + std::atomic_uint loopUsers_; EventLoop loop_; }; @@ -164,14 +166,15 @@ void CamApp::cameraRemoved(std::shared_ptr cam) void CamApp::captureDone() { - EventLoop::instance()->exit(0); + if (--loopUsers_ == 0) + EventLoop::instance()->exit(0); } int CamApp::run() { - std::unique_ptr session; int ret; + /* 1. List all cameras. */ if (options_.isSet(OptList)) { std::cout << "Available cameras:" << std::endl; @@ -182,6 +185,9 @@ int CamApp::run() } } + /* 2. Create the camera session. */ + std::unique_ptr session; + if (options_.isSet(OptCamera)) { session = std::make_unique(cm_.get(), options_); if (!session->isValid()) { @@ -195,36 +201,25 @@ int CamApp::run() session->captureDone.connect(this, &CamApp::captureDone); } - if (options_.isSet(OptListControls)) { + /* 3. Print camera information. */ + if (options_.isSet(OptListControls) || + options_.isSet(OptListProperties) || + options_.isSet(OptInfo)) { if (!session) { - std::cout << "Cannot list controls without a camera" + std::cout << "Cannot print camera information without a camera" << std::endl; return -EINVAL; } - session->listControls(); - } - - if (options_.isSet(OptListProperties)) { - if (!session) { - std::cout << "Cannot list properties without a camera" - << std::endl; - return -EINVAL; - } - - session->listProperties(); - } - - if (options_.isSet(OptInfo)) { - if (!session) { - std::cout << "Cannot print stream information without a camera" - << std::endl; - return -EINVAL; - } - - session->infoConfiguration(); + if (options_.isSet(OptListControls)) + session->listControls(); + if (options_.isSet(OptListProperties)) + session->listProperties(); + if (options_.isSet(OptInfo)) + session->infoConfiguration(); } + /* 4. Start capture. */ if (options_.isSet(OptCapture)) { if (!session) { std::cout << "Can't capture without a camera" << std::endl; @@ -237,12 +232,10 @@ int CamApp::run() return ret; } - loop_.exec(); - - session->stop(); - return 0; + loopUsers_++; } + /* 5. Enable hotplug monitoring. */ if (options_.isSet(OptMonitor)) { std::cout << "Monitoring new hotplug and unplug events" << std::endl; std::cout << "Press Ctrl-C to interrupt" << std::endl; @@ -250,9 +243,16 @@ int CamApp::run() cm_->cameraAdded.connect(this, &CamApp::cameraAdded); cm_->cameraRemoved.connect(this, &CamApp::cameraRemoved); - loop_.exec(); + loopUsers_++; } + if (loopUsers_) + loop_.exec(); + + /* 6. Stop capture. */ + if (options_.isSet(OptCapture)) + session->stop(); + return 0; }