[libcamera-devel] [PATCH 3/4] qcam: main_window: Introduce hotplug support

Umang Jain email at uajain.com
Wed May 6 12:33:55 CEST 2020


Signed-off-by: Umang Jain <email at uajain.com>
---
 src/qcam/main_window.cpp | 31 +++++++++++++++++++++++++++++++
 src/qcam/main_window.h   |  3 +++
 2 files changed, 34 insertions(+)

diff --git a/src/qcam/main_window.cpp b/src/qcam/main_window.cpp
index 7de0895..9db1647 100644
--- a/src/qcam/main_window.cpp
+++ b/src/qcam/main_window.cpp
@@ -71,6 +71,10 @@ MainWindow::MainWindow(CameraManager *cm, const OptionsParser::Options &options)
 	setCentralWidget(viewfinder_);
 	adjustSize();
 
+	/* Hotplug/unplug support */
+	cm_->cameraAdded.connect(this, &MainWindow::addNewCamera);
+	cm_->cameraRemoved.connect(this, &MainWindow::removeCamera);
+
 	/* Open the camera and start capture. */
 	ret = openCamera();
 	if (ret < 0) {
@@ -525,6 +529,33 @@ void MainWindow::stopCapture()
 	setWindowTitle(title_);
 }
 
+/* -----------------------------------------------------------------------------
+ * Camera hotplugging support
+ */
+
+void MainWindow::addNewCamera(Camera *cam)
+{
+	qInfo() << "Adding new camera: " << cam->name().c_str();
+	cameraCombo_->addItem(QString::fromStdString(cam->name()));
+}
+
+void MainWindow::removeCamera(Camera *cam)
+{
+	int camIndex = cameraCombo_->findText(QString::fromStdString(cam->name()));
+
+	/* Check if the currently-streaming camera is removed.
+	 *
+	 * \todo Also analyse the edge-case where the only available
+	 * camera is removed. */
+	if (camIndex == cameraCombo_->currentIndex()) {
+		toggleCapture(false);
+		cameraCombo_->setCurrentIndex(0);
+	}
+
+	qInfo() << "Removing camera: " << cam->name().c_str();
+	cameraCombo_->removeItem(camIndex);
+}
+
 /* -----------------------------------------------------------------------------
  * Image Save
  */
diff --git a/src/qcam/main_window.h b/src/qcam/main_window.h
index 59fa2d9..cb2fa26 100644
--- a/src/qcam/main_window.h
+++ b/src/qcam/main_window.h
@@ -87,6 +87,9 @@ private:
 	int startCapture();
 	void stopCapture();
 
+	void addNewCamera(Camera *camera);
+	void removeCamera(Camera *camera);
+
 	void requestComplete(Request *request);
 	void processCapture();
 	void processViewfinder(FrameBuffer *buffer);
-- 
2.26.2



More information about the libcamera-devel mailing list