From patchwork Mon Jun 17 10:09:10 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Laurent Pinchart X-Patchwork-Id: 1448 Return-Path: Received: from perceval.ideasonboard.com (perceval.ideasonboard.com [213.167.242.64]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id 829106487A for ; Mon, 17 Jun 2019 12:09:34 +0200 (CEST) Received: from pendragon.bb.dnainternet.fi (dfj612yhrgyx302h3jwwy-3.rev.dnainternet.fi [IPv6:2001:14ba:21f5:5b00:ce28:277f:58d7:3ca4]) by perceval.ideasonboard.com (Postfix) with ESMTPSA id 201E25D for ; Mon, 17 Jun 2019 12:09:34 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com; s=mail; t=1560766174; bh=+EvNd0VY5ljHHRNHj1t7SREbP6JatLOgESexCleUqf4=; h=From:To:Subject:Date:From; b=X7Dl7FiRTRZJXFkgSfcpH+oox5O7gW2kbmkUN+6kPdRJg4Inu1iEx+t/W9+mSTuSP 0gisKBYbINw3vSCGzlJ9N8Pi6TnEOJWp5xq559ZUpkP4tg67agK5WXXLZ3e5I1p8tj TRoFja1MzevjX63fE8/0E2zhSnytI9U9TU/VesiA= From: Laurent Pinchart To: libcamera-devel@lists.libcamera.org Date: Mon, 17 Jun 2019 13:09:10 +0300 Message-Id: <20190617100910.10879-1-laurent.pinchart@ideasonboard.com> X-Mailer: git-send-email 2.21.0 MIME-Version: 1.0 Subject: [libcamera-devel] [PATCH] cam: Allow selecting cameras by index X-BeenThere: libcamera-devel@lists.libcamera.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Mon, 17 Jun 2019 10:09:34 -0000 As camera names can be cumbersome to type, selection of cameras by index from a list can be useful. Print the list of detected cameras with an index for each item, and interpret the camera name as an index if it is a numerical value in the range from 1 to the number of cameras. Signed-off-by: Laurent Pinchart Reviewed-by: Jacopo Mondi Reviewed-by: Niklas Söderlund Acked-by: Kieran Bingham --- src/cam/main.cpp | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/src/cam/main.cpp b/src/cam/main.cpp index f03a9faf87fa..0e7610b1befd 100644 --- a/src/cam/main.cpp +++ b/src/cam/main.cpp @@ -73,7 +73,14 @@ int CamApp::init(int argc, char **argv) } if (options_.isSet(OptCamera)) { - camera_ = cm_->get(options_[OptCamera]); + const std::string &cameraName = options_[OptCamera]; + char *endptr; + unsigned long index = strtoul(cameraName.c_str(), &endptr, 10); + if (*endptr == '\0' && index > 0 && index <= cm_->cameras().size()) + camera_ = cm_->cameras()[index - 1]; + else + camera_ = cm_->get(cameraName); + if (!camera_) { std::cout << "Camera " << std::string(options_[OptCamera]) @@ -172,8 +179,12 @@ int CamApp::run() { if (options_.isSet(OptList)) { std::cout << "Available cameras:" << std::endl; - for (const std::shared_ptr &cam : cm_->cameras()) - std::cout << "- " << cam->name() << std::endl; + + unsigned int index = 1; + for (const std::shared_ptr &cam : cm_->cameras()) { + std::cout << index << ": " << cam->name() << std::endl; + index++; + } } if (options_.isSet(OptCapture)) {