{"id":16503,"url":"https://patchwork.libcamera.org/api/patches/16503/?format=json","web_url":"https://patchwork.libcamera.org/patch/16503/","project":{"id":1,"url":"https://patchwork.libcamera.org/api/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":"<20220701084521.31831-13-tomi.valkeinen@ideasonboard.com>","date":"2022-07-01T08:45:16","name":"[libcamera-devel,v3,12/17] py: cam.py: Use new events support","commit_ref":null,"pull_url":null,"state":"superseded","archived":false,"hash":"adb546ae305d5337d0e625ce0c965d328a94e44e","submitter":{"id":109,"url":"https://patchwork.libcamera.org/api/people/109/?format=json","name":"Tomi Valkeinen","email":"tomi.valkeinen@ideasonboard.com"},"delegate":null,"mbox":"https://patchwork.libcamera.org/patch/16503/mbox/","series":[{"id":3241,"url":"https://patchwork.libcamera.org/api/series/3241/?format=json","web_url":"https://patchwork.libcamera.org/project/libcamera/list/?series=3241","date":"2022-07-01T08:45:04","name":"Python bindings event handling","version":3,"mbox":"https://patchwork.libcamera.org/series/3241/mbox/"}],"comments":"https://patchwork.libcamera.org/api/patches/16503/comments/","check":"pending","checks":"https://patchwork.libcamera.org/api/patches/16503/checks/","tags":{},"headers":{"Return-Path":"<libcamera-devel-bounces@lists.libcamera.org>","X-Original-To":"parsemail@patchwork.libcamera.org","Delivered-To":"parsemail@patchwork.libcamera.org","Received":["from lancelot.ideasonboard.com (lancelot.ideasonboard.com\n\t[92.243.16.209])\n\tby patchwork.libcamera.org (Postfix) with ESMTPS id BECF8BD808\n\tfor <parsemail@patchwork.libcamera.org>;\n\tFri,  1 Jul 2022 08:45:55 +0000 (UTC)","from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id 86C7365650;\n\tFri,  1 Jul 2022 10:45:55 +0200 (CEST)","from perceval.ideasonboard.com (perceval.ideasonboard.com\n\t[213.167.242.64])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id 6DBA96565C\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tFri,  1 Jul 2022 10:45:46 +0200 (CEST)","from deskari.lan (91-158-154-79.elisa-laajakaista.fi\n\t[91.158.154.79])\n\tby perceval.ideasonboard.com (Postfix) with ESMTPSA id E6A6F6BB;\n\tFri,  1 Jul 2022 10:45:45 +0200 (CEST)"],"DKIM-Signature":["v=1; a=rsa-sha256; c=relaxed/simple; d=libcamera.org;\n\ts=mail; t=1656665155;\n\tbh=v0U9bNrGi3HWYfGUSCLofRQkPLlgOPEtaHgIn/WwQb8=;\n\th=To:Date:In-Reply-To:References:Subject:List-Id:List-Unsubscribe:\n\tList-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To:\n\tFrom;\n\tb=id/mCrqJ1NxImEI39n68OSAjMiV35D4jSExQ7v25ZdTVUwAt2MbfrNvt+pWwNw0l6\n\t3S2z2Rj5XFpC2OrGDIWHmjPuovO+uq8KgXKoZllQVCH5ql+Iil5w4tiTVia5rlfIbU\n\tscXkRz5xjZKmoPdJEbkIimgpT2hN0BU5aMRWA0V4g/yMVl4c/+9HPOrN2vbRZPdP3Z\n\tMK1pLHFHlLqUB+VFqxzAnchadr8nQib4f0YZuVw8tkGkPjVP5qgF6o6ck2iqlQ7Apd\n\ta/ItgIvkAEt/AXWgrQXu/Wfz7QqdqOb3qU1vv4sM53JnHOs6uDrrBn1mZy4ztSn1LO\n\tG38Kh3OwFG/EA==","v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com;\n\ts=mail; t=1656665146;\n\tbh=v0U9bNrGi3HWYfGUSCLofRQkPLlgOPEtaHgIn/WwQb8=;\n\th=From:To:Cc:Subject:Date:In-Reply-To:References:From;\n\tb=hvgjkFTxoY7D7NaVhUScMX03FM143nxoeWjfTHPPSoQtJkbMMGnA6jjs9Pr7bTXbK\n\txp96p6z3r1mnFTrvgG+pmCkUYOegPr71+C7+GAhGhw3k+WMxdjyL7ZzWfvyqDd5pIG\n\tTiFfoYzj+qSw0GRBz+IYi0rRaWKLbL1Ct1zv/JOc="],"Authentication-Results":"lancelot.ideasonboard.com; dkim=pass (1024-bit key; \n\tunprotected) header.d=ideasonboard.com\n\theader.i=@ideasonboard.com\n\theader.b=\"hvgjkFTx\"; dkim-atps=neutral","To":"libcamera-devel@lists.libcamera.org,\n\tDavid Plowman <david.plowman@raspberrypi.com>,\n\tKieran Bingham <kieran.bingham@ideasonboard.com>,\n\tLaurent Pinchart <laurent.pinchart@ideasonboard.com>,\n\tJacopo Mondi <jacopo@jmondi.org>","Date":"Fri,  1 Jul 2022 11:45:16 +0300","Message-Id":"<20220701084521.31831-13-tomi.valkeinen@ideasonboard.com>","X-Mailer":"git-send-email 2.34.1","In-Reply-To":"<20220701084521.31831-1-tomi.valkeinen@ideasonboard.com>","References":"<20220701084521.31831-1-tomi.valkeinen@ideasonboard.com>","MIME-Version":"1.0","Content-Transfer-Encoding":"8bit","Subject":"[libcamera-devel] [PATCH v3 12/17] py: cam.py: Use new events\n\tsupport","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>","From":"Tomi Valkeinen via libcamera-devel\n\t<libcamera-devel@lists.libcamera.org>","Reply-To":"Tomi Valkeinen <tomi.valkeinen@ideasonboard.com>","Errors-To":"libcamera-devel-bounces@lists.libcamera.org","Sender":"\"libcamera-devel\" <libcamera-devel-bounces@lists.libcamera.org>"},"content":"Convert cam.py to use the new event dispatching. In addition to handling\nthe request-completed event, handle also disconnect, camera-added and\ncamera-removed events (which only do a simple print).\n\nSigned-off-by: Tomi Valkeinen <tomi.valkeinen@ideasonboard.com>\n---\n src/py/cam/cam.py | 31 +++++++++++++++++++++++++------\n 1 file changed, 25 insertions(+), 6 deletions(-)","diff":"diff --git a/src/py/cam/cam.py b/src/py/cam/cam.py\nindex 6b6b678b..f19bad57 100755\n--- a/src/py/cam/cam.py\n+++ b/src/py/cam/cam.py\n@@ -230,11 +230,20 @@ class CaptureState:\n     # Called from renderer when there is a libcamera event\n     def event_handler(self):\n         try:\n-            reqs = self.cm.get_ready_requests()\n-\n-            for req in reqs:\n-                ctx = next(ctx for ctx in self.contexts if ctx.idx == req.cookie)\n-                self.__request_handler(ctx, req)\n+            evs = self.cm.get_events()\n+            for ev in evs:\n+                type = ev.type\n+\n+                if type == libcam.Event.Type.Undefined:\n+                    raise RuntimeError(\"Bad event type\")\n+                elif type == libcam.Event.Type.CameraAdded:\n+                    print('Camera added:', ev.camera)\n+                elif type == libcam.Event.Type.CameraRemoved:\n+                    print('Camera added:', ev.camera)\n+                elif type == libcam.Event.Type.Disconnect:\n+                    self.__disconnect_handler(ev.camera)\n+                elif type == libcam.Event.Type.RequestCompleted:\n+                    self.__request_handler(ev.camera, ev.request)\n \n             running = any(ctx.reqs_completed < ctx.opt_capture for ctx in self.contexts)\n             return running\n@@ -242,7 +251,9 @@ class CaptureState:\n             traceback.print_exc()\n             return False\n \n-    def __request_handler(self, ctx, req):\n+    def __request_handler(self, cam, req):\n+        ctx = next(ctx for ctx in self.contexts if ctx.camera == cam)\n+\n         if req.status != libcam.Request.Status.Complete:\n             raise Exception('{}: Request failed: {}'.format(ctx.id, req.status))\n \n@@ -297,6 +308,9 @@ class CaptureState:\n             ctx.camera.queue_request(req)\n             ctx.reqs_queued += 1\n \n+    def __disconnect_handler(self, cam):\n+        print(f'Camera {cam} disconnected')\n+\n     def __capture_init(self):\n         for ctx in self.contexts:\n             ctx.acquire()\n@@ -447,6 +461,11 @@ def main():\n \n         state.do_cmd_capture()\n \n+        # This is not strictly needed, but it helps to do a proper cleanup as we\n+        # drop any unhandled events, and so makes it easier to use memory leak\n+        # detectors.\n+        cm.get_events()\n+\n     return 0\n \n \n","prefixes":["libcamera-devel","v3","12/17"]}