{"id":4069,"url":"https://patchwork.libcamera.org/api/1.1/patches/4069/?format=json","web_url":"https://patchwork.libcamera.org/patch/4069/","project":{"id":1,"url":"https://patchwork.libcamera.org/api/1.1/projects/1/?format=json","name":"libcamera","link_name":"libcamera","list_id":"libcamera_core","list_email":"libcamera-devel@lists.libcamera.org","web_url":"","scm_url":"","webscm_url":""},"msgid":"<20200616194523.23268-6-email@uajain.com>","date":"2020-06-16T19:45:38","name":"[libcamera-devel,v5,5/6] qcam: main_window: Introduce initial hotplug support","commit_ref":"7055e96ecc9144390a259e4e23be7fb77ca93364","pull_url":null,"state":"accepted","archived":false,"hash":"0cfae39d6d7dc6c0adc9ed57d865b4534245f793","submitter":{"id":1,"url":"https://patchwork.libcamera.org/api/1.1/people/1/?format=json","name":"Umang Jain","email":"email@uajain.com"},"delegate":null,"mbox":"https://patchwork.libcamera.org/patch/4069/mbox/","series":[{"id":1009,"url":"https://patchwork.libcamera.org/api/1.1/series/1009/?format=json","web_url":"https://patchwork.libcamera.org/project/libcamera/list/?series=1009","date":"2020-06-16T19:45:33","name":"Introduce UVC hotplugging support","version":5,"mbox":"https://patchwork.libcamera.org/series/1009/mbox/"}],"comments":"https://patchwork.libcamera.org/api/patches/4069/comments/","check":"pending","checks":"https://patchwork.libcamera.org/api/patches/4069/checks/","tags":{},"headers":{"Return-Path":"<bounces+15657259-5c31-libcamera-devel=lists.libcamera.org@em7280.uajain.com>","Received":["from o1.f.az.sendgrid.net (o1.f.az.sendgrid.net [208.117.55.132])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id CB66D61F24\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tTue, 16 Jun 2020 21:45:39 +0200 (CEST)","by filter0104p3las1.sendgrid.net with SMTP id\n\tfilter0104p3las1-31629-5EE92161-132\n\t2020-06-16 19:45:38.316073928 +0000 UTC m=+696655.239765817","from mail.uajain.com (unknown)\n\tby ismtpd0001p1maa1.sendgrid.net (SG) with ESMTP\n\tid Nedy3N-4RE-Rf1Vu5vXEXA Tue, 16 Jun 2020 19:45:37.797 +0000 (UTC)"],"Authentication-Results":"lancelot.ideasonboard.com; dkim=pass (1024-bit key; \n\tunprotected) header.d=uajain.com\n\theader.i=@uajain.com header.b=\"BUwHJwKf\"; \n\tdkim-atps=neutral","DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/relaxed; d=uajain.com;\n\th=from:subject:in-reply-to:references:mime-version:to:cc:\n\tcontent-transfer-encoding:content-type;\n\ts=s1; bh=a09DxRVCdx2Juyi1912x8Ji3Vtfs0ty9OppLXC61jBU=;\n\tb=BUwHJwKf7bZwXvvE9ULEI3dqiG6hE/iU3VI0xYjXAFpuDBZ7rCEChxJBOK6TyY/fE1s3\n\tGNsopYyY6jzxvBE7pVDLMsU6jwYe0KxK/A2VFEhiZhojcGbTnaVjYLp6q4WTunwaKj/Uj0\n\tN7EG13km5YyhSSIQmloJsJI4j6Ysie7aI=","From":"Umang Jain <email@uajain.com>","Date":"Tue, 16 Jun 2020 19:45:38 +0000 (UTC)","Message-Id":"<20200616194523.23268-6-email@uajain.com>","In-Reply-To":"<20200616194523.23268-1-email@uajain.com>","References":"<20200616194523.23268-1-email@uajain.com>","Mime-Version":"1.0","X-SG-EID":"1Q40EQ7YGir8a9gjSIAdTjhngY657NMk9ckeo4dbHZDiOpywc/L3L9rFqlwE4KPcHIuSzkf5/M5SksOT1pntG8LZHPMsqXV31uFoZfh8NyQ1mvi2JGiTrcfWSqIMI8j0KquW2MH6aIEnkoN7gUAHjmyCKl4og9A4/KNXnVW+11FuaP4xODobxRLrV0REhw/D2+W2JFRAsgQ7CAvTUcS2jJUcOPtyF1cfOi0fF0MjCxp0LfPp75ju9BR6uSLHqau2","To":"laurent.pinchart@ideasonboard.com, kieran.bingham@ideasonboard.com,\n\tlibcamera-devel@lists.libcamera.org","Content-Transfer-Encoding":"7bit","Content-Type":"text/plain; charset=us-ascii","Subject":"[libcamera-devel] [PATCH v5 5/6] qcam: main_window: Introduce\n\tinitial hotplug support","X-BeenThere":"libcamera-devel@lists.libcamera.org","X-Mailman-Version":"2.1.29","Precedence":"list","List-Id":"<libcamera-devel.lists.libcamera.org>","List-Unsubscribe":"<https://lists.libcamera.org/options/libcamera-devel>,\n\t<mailto:libcamera-devel-request@lists.libcamera.org?subject=unsubscribe>","List-Archive":"<https://lists.libcamera.org/pipermail/libcamera-devel/>","List-Post":"<mailto:libcamera-devel@lists.libcamera.org>","List-Help":"<mailto:libcamera-devel-request@lists.libcamera.org?subject=help>","List-Subscribe":"<https://lists.libcamera.org/listinfo/libcamera-devel>,\n\t<mailto:libcamera-devel-request@lists.libcamera.org?subject=subscribe>","X-List-Received-Date":"Tue, 16 Jun 2020 19:45:40 -0000"},"content":"Hook up various QCam UI bits with hotplug support introduced\nin previous commits. This looks good-enough as first steps\nto see how the hotplugging functionality is turning out to be\nfrom application point-of-view.\n\nOne can still think of few edge case nuances not yet covered\nunder this implementation especially around having only one\ncamera in the system and hotplugging/hot-unplugging it.\nHence, those are intentionally kept out of scope for now. It\nmight require some thinking on how to handle it on application\nlevel having additional time on hand.\n\nSigned-off-by: Umang Jain <email@uajain.com>\nReviewed-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>\nReviewed-by: Kieran Bingham <kieran.bingham@ideasonboard.com>\n---\n src/qcam/main_window.cpp | 76 ++++++++++++++++++++++++++++++++++++++++\n src/qcam/main_window.h   |  6 ++++\n 2 files changed, 82 insertions(+)","diff":"diff --git a/src/qcam/main_window.cpp b/src/qcam/main_window.cpp\nindex 2960259..7bc1360 100644\n--- a/src/qcam/main_window.cpp\n+++ b/src/qcam/main_window.cpp\n@@ -59,6 +59,36 @@ public:\n \t}\n };\n \n+/**\n+ * \\brief Custom QEvent to signal hotplug or unplug\n+ */\n+class HotplugEvent : public QEvent\n+{\n+public:\n+\tenum PlugEvent {\n+\t\tHotPlug,\n+\t\tHotUnplug\n+\t};\n+\n+\tHotplugEvent(std::shared_ptr<Camera> camera, PlugEvent event)\n+\t\t: QEvent(type()), camera_(std::move(camera)), plugEvent_(event)\n+\t{\n+\t}\n+\n+\tstatic Type type()\n+\t{\n+\t\tstatic int type = QEvent::registerEventType();\n+\t\treturn static_cast<Type>(type);\n+\t}\n+\n+\tPlugEvent hotplugEvent() const { return plugEvent_; }\n+\tCamera *camera() const { return camera_.get(); }\n+\n+private:\n+\tstd::shared_ptr<Camera> camera_;\n+\tPlugEvent plugEvent_;\n+};\n+\n MainWindow::MainWindow(CameraManager *cm, const OptionsParser::Options &options)\n \t: saveRaw_(nullptr), options_(options), cm_(cm), allocator_(nullptr),\n \t  isCapturing_(false), captureRaw_(false)\n@@ -81,6 +111,10 @@ MainWindow::MainWindow(CameraManager *cm, const OptionsParser::Options &options)\n \tsetCentralWidget(viewfinder_);\n \tadjustSize();\n \n+\t/* Hotplug/unplug support */\n+\tcm_->cameraAdded.connect(this, &MainWindow::addCamera);\n+\tcm_->cameraRemoved.connect(this, &MainWindow::removeCamera);\n+\n \t/* Open the camera and start capture. */\n \tret = openCamera();\n \tif (ret < 0) {\n@@ -105,6 +139,9 @@ bool MainWindow::event(QEvent *e)\n \tif (e->type() == CaptureEvent::type()) {\n \t\tprocessCapture();\n \t\treturn true;\n+\t} else if (e->type() == HotplugEvent::type()) {\n+\t\tprocessHotplug(static_cast<HotplugEvent *>(e));\n+\t\treturn true;\n \t}\n \n \treturn QMainWindow::event(e);\n@@ -535,6 +572,45 @@ void MainWindow::stopCapture()\n \tsetWindowTitle(title_);\n }\n \n+/* -----------------------------------------------------------------------------\n+ * Camera hotplugging support\n+ */\n+\n+void MainWindow::processHotplug(HotplugEvent *e)\n+{\n+\tCamera *camera = e->camera();\n+\tHotplugEvent::PlugEvent event = e->hotplugEvent();\n+\n+\tif (event == HotplugEvent::HotPlug) {\n+\t\tcameraCombo_->addItem(QString::fromStdString(camera->name()));\n+\t} else if (event == HotplugEvent::HotUnplug) {\n+\t\t/* Check if the currently-streaming camera is removed. */\n+\t\tif (camera == camera_.get()) {\n+\t\t\ttoggleCapture(false);\n+\t\t\tcameraCombo_->setCurrentIndex(0);\n+\t\t}\n+\n+\t\tint camIndex = cameraCombo_->findText(QString::fromStdString(camera->name()));\n+\t\tcameraCombo_->removeItem(camIndex);\n+\t}\n+}\n+\n+void MainWindow::addCamera(std::shared_ptr<Camera> camera)\n+{\n+\tqInfo() << \"Adding new camera:\" << camera->name().c_str();\n+\tQCoreApplication::postEvent(this,\n+\t\t\t\t    new HotplugEvent(std::move(camera),\n+\t\t\t\t\t\t     HotplugEvent::HotPlug));\n+}\n+\n+void MainWindow::removeCamera(std::shared_ptr<Camera> camera)\n+{\n+\tqInfo() << \"Removing camera:\" << camera->name().c_str();\n+\tQCoreApplication::postEvent(this,\n+\t\t\t\t    new HotplugEvent(std::move(camera),\n+\t\t\t\t\t\t     HotplugEvent::HotUnplug));\n+}\n+\n /* -----------------------------------------------------------------------------\n  * Image Save\n  */\ndiff --git a/src/qcam/main_window.h b/src/qcam/main_window.h\nindex 59fa2d9..4606fe4 100644\n--- a/src/qcam/main_window.h\n+++ b/src/qcam/main_window.h\n@@ -32,6 +32,8 @@ using namespace libcamera;\n class QAction;\n class QComboBox;\n \n+class HotplugEvent;\n+\n enum {\n \tOptCamera = 'c',\n \tOptHelp = 'h',\n@@ -87,8 +89,12 @@ private:\n \tint startCapture();\n \tvoid stopCapture();\n \n+\tvoid addCamera(std::shared_ptr<Camera> camera);\n+\tvoid removeCamera(std::shared_ptr<Camera> camera);\n+\n \tvoid requestComplete(Request *request);\n \tvoid processCapture();\n+\tvoid processHotplug(HotplugEvent *e);\n \tvoid processViewfinder(FrameBuffer *buffer);\n \n \t/* UI elements */\n","prefixes":["libcamera-devel","v5","5/6"]}