From patchwork Sat Jun 3 07:56:06 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tomi Valkeinen X-Patchwork-Id: 18689 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 22E73C32AA for ; Sat, 3 Jun 2023 07:57:18 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id F37046288D; Sat, 3 Jun 2023 09:57:16 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=libcamera.org; s=mail; t=1685779037; bh=9HixovCG5rJ3dB8lCPbW0/trjk63jicDwxhovla84yA=; h=To:Date:In-Reply-To:References:Subject:List-Id:List-Unsubscribe: List-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To: From; b=fXFZ4CwQO1VNMMFRXK4BTBQ3vanNnQU36fQsoQv5HEusNEz95prbTNCiYtWUIrjsB 5J5zLR+ldcQXOMU+OyC8ZI+gRK31mF5x0KTBbZvTd1Rkks6JSibIcQRQOFAgtPg31E JD5kjp15BgigoFfy+g1sfdZId8SqINlf9o2ZUYo1jQVt+c4sa0XFodCU+HJYY5/70W ud6/eP5abuBPEQoCpCIKrBBUE3a4QQpz9bKWnfI5SY8+06roLppqBUjGEBHTzLozNs NWQRNP3osP55AhTOGLMMgr2uff/gT7py/f10wU5g2GW8kfQn5uehBF+lo1GNOZdlhG 7yy6YlcARaPHQ== Received: from perceval.ideasonboard.com (perceval.ideasonboard.com [213.167.242.64]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id 6E4CF62728 for ; Sat, 3 Jun 2023 09:57:11 +0200 (CEST) Authentication-Results: lancelot.ideasonboard.com; dkim=pass (1024-bit key; unprotected) header.d=ideasonboard.com header.i=@ideasonboard.com header.b="KilaxKSI"; dkim-atps=neutral Received: from desky.lan (91-154-35-171.elisa-laajakaista.fi [91.154.35.171]) by perceval.ideasonboard.com (Postfix) with ESMTPSA id 9871C468; Sat, 3 Jun 2023 09:56:47 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com; s=mail; t=1685779008; bh=9HixovCG5rJ3dB8lCPbW0/trjk63jicDwxhovla84yA=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=KilaxKSIEOEtCQ0fRJax9WIoUdktS63Sg6XnvLJrGKAseww/kYCup44n3YUF88j/W y42QqxJnX91++1LjBRsyi9+dw18F7WpaL6lSKOtlJQVKv+nuwmgHw7+yvDJyHyU5wL mUMMmWtm0sXYuOZwlRqNsHSI4+1LGXBQoPsBMcxs= To: libcamera-devel@lists.libcamera.org Date: Sat, 3 Jun 2023 10:56:06 +0300 Message-Id: <20230603075615.20663-5-tomi.valkeinen@ideasonboard.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230603075615.20663-1-tomi.valkeinen@ideasonboard.com> References: <20230603075615.20663-1-tomi.valkeinen@ideasonboard.com> MIME-Version: 1.0 Subject: [libcamera-devel] [PATCH v5 04/13] py: cam.py: Use new events support 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: , X-Patchwork-Original-From: Tomi Valkeinen via libcamera-devel From: Tomi Valkeinen Reply-To: Tomi Valkeinen Errors-To: libcamera-devel-bounces@lists.libcamera.org Sender: "libcamera-devel" Convert cam.py to use the new event dispatching. In addition to handling the request-completed event, handle also disconnect, camera-added and camera-removed events (which only do a simple print). Signed-off-by: Tomi Valkeinen Reviewed-by: Jacopo Mondi Reviewed-by: Laurent Pinchart --- src/py/cam/cam.py | 27 +++++++++++++++++++++------ 1 file changed, 21 insertions(+), 6 deletions(-) diff --git a/src/py/cam/cam.py b/src/py/cam/cam.py index a2a115c1..1e2d1f69 100755 --- a/src/py/cam/cam.py +++ b/src/py/cam/cam.py @@ -230,11 +230,19 @@ class CaptureState: # Called from renderer when there is a libcamera event def event_handler(self): try: - reqs = self.cm.get_ready_requests() - - for req in reqs: - ctx = next(ctx for ctx in self.contexts if ctx.idx == req.cookie) - self.__request_handler(ctx, req) + for ev in self.cm.get_events(): + type = ev.type + + if type == libcam.Event.Type.CameraAdded: + print(f'Camera {ev.camera} added') + elif type == libcam.Event.Type.CameraRemoved: + print(f'Camera {ev.camera} removed') + elif type == libcam.Event.Type.Disconnect: + print(f'Camera {ev.camera} disconnected') + elif type == libcam.Event.Type.RequestCompleted: + self.__request_handler(ev.camera, ev.request) + else: + raise RuntimeError("Bad event type") running = any(ctx.reqs_completed < ctx.opt_capture for ctx in self.contexts) return running @@ -242,7 +250,9 @@ class CaptureState: traceback.print_exc() return False - def __request_handler(self, ctx, req): + def __request_handler(self, cam, req): + ctx = next(ctx for ctx in self.contexts if ctx.camera == cam) + if req.status != libcam.Request.Status.Complete: raise Exception('{}: Request failed: {}'.format(ctx.id, req.status)) @@ -447,6 +457,11 @@ def main(): state.do_cmd_capture() + # This is not strictly needed, but it helps to do a proper cleanup as we + # drop any unhandled events, and so makes it easier to use memory leak + # detectors. + cm.get_events() + return 0