From patchwork Mon Jun 27 12:28:05 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Daniel Semkowicz X-Patchwork-Id: 16377 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 366F3BD808 for ; Mon, 27 Jun 2022 12:28:18 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id E51CF6563A; Mon, 27 Jun 2022 14:28:17 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=libcamera.org; s=mail; t=1656332897; bh=CQ9RMIxFQUOg3K/BFZMjNATowJhw/L2haohCUgYfEIk=; 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=IvCIlT9Entmw2GI23EQj382j0/TTLXCkdsVyv/KxIIUX1nNVNvuzbUDyjxtvVMkPb 54Zq0saRvf95ZhGx7vKM4lIteyBSzL8QNBP8FZaNSn37szjkvDN/j2xW7CLHGkC21A Z5tg24o0OyCPgHTRXHKjJBchLOXGQUR3jJTXOPAx1QByDurxqKzzbv+qCraPonxUxJ 5qwci5NAUmdEQSoEmQ4+brJDBfnFbed321g22Rw6YGAFR7MwELJhqSwWDXH3JNsHqR ahwg1OBM+c5d1eXuYravcYEkFI0PYb0lJqxWDmoEShqAtI2ALLmcVALGAwg9SzYb3e UQn602q02LwvA== Received: from mail-wr1-x436.google.com (mail-wr1-x436.google.com [IPv6:2a00:1450:4864:20::436]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id 80D6B65635 for ; Mon, 27 Jun 2022 14:28:15 +0200 (CEST) Authentication-Results: lancelot.ideasonboard.com; dkim=pass (2048-bit key; unprotected) header.d=thaumatec-com.20210112.gappssmtp.com header.i=@thaumatec-com.20210112.gappssmtp.com header.b="A1XGr4v0"; dkim-atps=neutral Received: by mail-wr1-x436.google.com with SMTP id v14so12831211wra.5 for ; Mon, 27 Jun 2022 05:28:15 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=thaumatec-com.20210112.gappssmtp.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=yWDJfTSEdTrKf2vV9jyi8eKOYzdjg03uXZ4gqhzX0Vo=; b=A1XGr4v00WbvdnMcrPxIQfnz4ik0r1zFbKOfsM39fZGcA7bN+XrgOZj0f1BnR5CE7+ DQPjQSgSlaUg9q2E0zQy/JSJMgqOFyPW6mMNku5FP9hKqS+Bu/GDGc3QrHihmDbiP0DX LyYn0zT1cpzIXAaqy4pp11o/rCQV7QALgis01EvP5VVMSLOU6boZRqwpcxYGfHqrB6qX mNtnL60yaXbK85rpIBW64ZSmn7Iz5+H6PhBvqRQx1PAcs0UcoW+Rhs5Ru8Md+PGzTqfu VhLCByyKdvYMyzhch+FhkB0p8kLWKy6d9nHtS10NIIMAvgria4CkpJDjzwmd/tSwznMp yyqA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=yWDJfTSEdTrKf2vV9jyi8eKOYzdjg03uXZ4gqhzX0Vo=; b=U6B6lnYs2dd2enNU+zFD0wrBgRW4KpIP1KYE0vbNfO0ejbleB5VybrYy/5PV4cYihs 35DCznERvABf9NVYrXnxU1fGyg4jmWH5l/sbu31vyRPfYTdHIOk2V3txv1474IdoYrL0 i3J/5XcUywXRTI4FPipy20mibS4c2OZYbU94W5HCdDuqqv+ogTaMVdBkz7/fgNlV9jcW WYwEOv2HRA0SuJjGwCiQ2gAv63nZz6O3/EK661abzPdF0yY0CWg//TqaSwzzK1RQkWqB Isr7OVrEDLDZJyGz68nTCc3oCGEGXYS/mr8N+mQI+ZlDWrArLeVGby0uDHLLXemvtqQU B2Ww== X-Gm-Message-State: AJIora+Cm8962jJSUUlSc3Um0riSG43783q9HFjgQApgt5q5yR1za15T L/JWK77Z1YmaOBXt7bWXmSCsUja2DIIJkQ== X-Google-Smtp-Source: AGRyM1swN+RQ697VE6ZdjHL7Ad7Ox+q3vEm3V2fmMH15DFnzo97zH/lulgoP4xWb9AtApRcMDvtl/A== X-Received: by 2002:a5d:4c86:0:b0:21b:988f:20cf with SMTP id z6-20020a5d4c86000000b0021b988f20cfmr12401298wrs.299.1656332895205; Mon, 27 Jun 2022 05:28:15 -0700 (PDT) Received: from localhost.localdomain (ip092042140082.rev.nessus.at. [92.42.140.82]) by smtp.gmail.com with ESMTPSA id m18-20020a05600c4f5200b0039748be12dbsm17836401wmq.47.2022.06.27.05.28.14 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 27 Jun 2022 05:28:14 -0700 (PDT) To: libcamera-devel@lists.libcamera.org Date: Mon, 27 Jun 2022 14:28:05 +0200 Message-Id: <20220627122806.29586-2-dse@thaumatec.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20220627122806.29586-1-dse@thaumatec.com> References: <20220627122806.29586-1-dse@thaumatec.com> MIME-Version: 1.0 Subject: [libcamera-devel] [PATCH v3 1/2] cam: Add Rectangle type parsing in capture script 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: Daniel Semkowicz via libcamera-devel From: Daniel Semkowicz Reply-To: Daniel Semkowicz Errors-To: libcamera-devel-bounces@lists.libcamera.org Sender: "libcamera-devel" This change is required for AfWindows control from capture script. Parser expects array of arrays of parameters, so it is possible to specify multiple rectangles. Signed-off-by: Daniel Semkowicz Reviewed-by: Jacopo Mondi Reviewed-by: Paul Elder --- src/cam/capture_script.cpp | 145 ++++++++++++++++++++++++++++++++++--- src/cam/capture_script.h | 7 +- 2 files changed, 139 insertions(+), 13 deletions(-) diff --git a/src/cam/capture_script.cpp b/src/cam/capture_script.cpp index 9f22d5f7..6278a152 100644 --- a/src/cam/capture_script.cpp +++ b/src/cam/capture_script.cpp @@ -232,12 +232,15 @@ int CaptureScript::parseControl(EventPtr event, ControlList &controls) return -EINVAL; } - std::string value = parseScalar(); - if (value.empty()) + const ControlId *controlId = it->second; + + ControlValue val = unpackControl(controlId); + if (val.isNone()) { + std::cerr << "Error unpacking control '" << name << "'" + << std::endl; return -EINVAL; + } - const ControlId *controlId = it->second; - ControlValue val = unpackControl(controlId, value); controls.set(controlId->id(), val); return 0; @@ -252,6 +255,104 @@ std::string CaptureScript::parseScalar() return eventScalarValue(event); } +ControlValue CaptureScript::parseRectangles() +{ + std::vector rectangles; + + std::vector> arrays = parseArrays(); + if (arrays.empty()) + return {}; + + for (const std::vector &values : arrays) { + if (values.size() != 4) { + std::cerr << "Error parsing Rectangle: expected " + "array with 4 parameters" << std::endl; + return {}; + } + + Rectangle rect = unpackRectangle(values); + rectangles.push_back(rect); + } + + ControlValue controlValue; + controlValue.set(Span(rectangles)); + + return controlValue; +} + +std::vector> CaptureScript::parseArrays() +{ + EventPtr event = nextEvent(YAML_SEQUENCE_START_EVENT); + if (!event) + return {}; + + event = nextEvent(); + if (!event) + return {}; + + std::vector> valueArrays; + + /* Parse single array. */ + if (event->type == YAML_SCALAR_EVENT) { + std::string firstValue = eventScalarValue(event); + if (firstValue.empty()) + return {}; + + std::vector remaining = parseSingleArray(); + + std::vector values = { firstValue }; + values.insert(std::end(values), + std::begin(remaining), std::end(remaining)); + valueArrays.push_back(values); + + return valueArrays; + } + + /* Parse array of arrays. */ + while (1) { + switch (event->type) { + case YAML_SEQUENCE_START_EVENT: { + std::vector values = parseSingleArray(); + valueArrays.push_back(values); + break; + } + case YAML_SEQUENCE_END_EVENT: + return valueArrays; + default: + return {}; + } + + event = nextEvent(); + if (!event) + return {}; + } +} + +std::vector CaptureScript::parseSingleArray() +{ + std::vector values; + + while (1) { + EventPtr event = nextEvent(); + if (!event) + return {}; + + switch (event->type) { + case YAML_SCALAR_EVENT: { + std::string value = eventScalarValue(event); + if (value.empty()) + return {}; + values.push_back(value); + break; + } + case YAML_SEQUENCE_END_EVENT: + return values; + default: + return {}; + } + } +} + void CaptureScript::unpackFailure(const ControlId *id, const std::string &repr) { static const std::map typeNames = { @@ -277,9 +378,24 @@ void CaptureScript::unpackFailure(const ControlId *id, const std::string &repr) << typeName << " control " << id->name() << std::endl; } -ControlValue CaptureScript::unpackControl(const ControlId *id, - const std::string &repr) +ControlValue CaptureScript::unpackControl(const ControlId *id) { + /* Parse complex types. */ + switch (id->type()) { + case ControlTypeRectangle: + return parseRectangles(); + case ControlTypeSize: + /* \todo Parse Sizes. */ + return {}; + default: + break; + } + + /* Parse basic types represented by a single scalar. */ + const std::string repr = parseScalar(); + if (repr.empty()) + return {}; + ControlValue value{}; switch (id->type()) { @@ -324,13 +440,20 @@ ControlValue CaptureScript::unpackControl(const ControlId *id, value.set(repr); break; } - case ControlTypeRectangle: - /* \todo Parse rectangles. */ - break; - case ControlTypeSize: - /* \todo Parse Sizes. */ + default: + std::cerr << "Unsupported control type" << std::endl; break; } return value; } + +libcamera::Rectangle CaptureScript::unpackRectangle(const std::vector &strVec) +{ + int x = strtol(strVec[0].c_str(), NULL, 10); + int y = strtol(strVec[1].c_str(), NULL, 10); + unsigned int width = strtoul(strVec[2].c_str(), NULL, 10); + unsigned int height = strtoul(strVec[3].c_str(), NULL, 10); + + return Rectangle(x, y, width, height); +} diff --git a/src/cam/capture_script.h b/src/cam/capture_script.h index 8b4f8f62..fffe67e5 100644 --- a/src/cam/capture_script.h +++ b/src/cam/capture_script.h @@ -54,9 +54,12 @@ private: int parseControl(EventPtr event, libcamera::ControlList &controls); std::string parseScalar(); + libcamera::ControlValue parseRectangles(); + std::vector> parseArrays(); + std::vector parseSingleArray(); void unpackFailure(const libcamera::ControlId *id, const std::string &repr); - libcamera::ControlValue unpackControl(const libcamera::ControlId *id, - const std::string &repr); + libcamera::ControlValue unpackControl(const libcamera::ControlId *id); + libcamera::Rectangle unpackRectangle(const std::vector &strVec); };