From mboxrd@z Thu Jan 1 00:00:00 1970 Delivery-date: Wed, 19 Jun 2024 14:58:03 +0200 Received: from metis.whiteo.stw.pengutronix.de ([2a0a:edc0:2:b01:1d::104]) by lore.white.stw.pengutronix.de with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.96) (envelope-from ) id 1sJut1-008hHe-1V for lore@lore.pengutronix.de; Wed, 19 Jun 2024 14:58:03 +0200 Received: from localhost ([127.0.0.1] helo=metis.whiteo.stw.pengutronix.de) by metis.whiteo.stw.pengutronix.de with esmtp (Exim 4.92) (envelope-from ) id 1sJusr-0005WO-Lg; Wed, 19 Jun 2024 14:57:53 +0200 Received: from mail-am7eur03on2084.outbound.protection.outlook.com ([40.107.105.84] helo=EUR03-AM7-obe.outbound.protection.outlook.com) by metis.whiteo.stw.pengutronix.de with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1sJsSb-00013l-7O; Wed, 19 Jun 2024 12:22:38 +0200 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=J/WYMr842oQhhKEP2+kV6NeszRWlfEKB9TsRK3AWBlK3pKrwmhx6yxfzq5tIChAqm6XCaxzdJxJ5s4mJ9Awtg7LYbUhRQckroxsO4OUxnWiJKWF/IaMNgTp4tO1srTa/AZRt/LDry0Nvi8Z4Vkc5gMetQ7f0hQ9ZrL2YmDcERFFQ9xiXFQ7StJpOW5UzFYcN147Mprk5N9/4GnTtDKHHC4sN5WydDv/xaoVYZjAINXV3kTi1VuPs+tPTOzyHksp694YCr7UOve64wi2O3vY1JPj/fTT2wQokBl0e2Y9IVGF81zm1ZNPpOArQ2Hv5vZckL5SFuiGRsUuq7+YPtKhBMQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=etNCyl68iTXNPnU5y6ScFhY/DRF/gKofCKDGZ6Oo3IE=; b=GckwOm1fMoNkNE2vvJcJJ6qsBp6qFmuDbSNjta7RxYLndiOiUqKqL1Bz37biAK2vJha9hL717wfArGTIc4uhxrcpIroccIMf/SFcWAbenHLCRffxBQYn2pM+aDOlqgCaHW0Mn0fPRYDFcnwaQtMR4xJ+aYMDQ8JnlguSqlvQ2rMbG0xxMb878y+sElUUHC98Pvsf45YWRhF0jtTpR2Qyuh0LnphDun9MnSxF+N/qve/sKl2+GpuzcmzDL7YVmZOjI0NW47R9r35yUGPI/vY4b9grRiakHKat8lOXlAfPALKdYP3H+wdh6iHY8mHk3e0mbsCY8gnbcdKKLIYCsubeSg== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 193.8.40.94) smtp.rcpttodomain=pengutronix.de smtp.mailfrom=leica-geosystems.com.cn; dmarc=pass (p=reject sp=reject pct=100) action=none header.from=leica-geosystems.com.cn; dkim=none (message not signed); arc=none (0) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=leica-geosystems.com.cn; s=selector1; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=etNCyl68iTXNPnU5y6ScFhY/DRF/gKofCKDGZ6Oo3IE=; b=w0h+OSqIDNcpntuJCQjVtuTbA2tOPcmGuDLqqHN1g3W+i8zhq+iofFNU4XGz7NFjnX0QMcDN/UCpryTUPR2cY+V1fl4dqc9FvG4FRhCXhaWvIY7tvTEITWavZS6o3HKLxEgb3Pnu13+kHN/SKEPa19dSdSnHj4rPcKwDyAbaQmE= Received: from DUZPR01CA0006.eurprd01.prod.exchangelabs.com (2603:10a6:10:3c3::13) by AM9PR06MB8001.eurprd06.prod.outlook.com (2603:10a6:20b:3a4::7) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7677.31; Wed, 19 Jun 2024 10:22:34 +0000 Received: from DU2PEPF0001E9C3.eurprd03.prod.outlook.com (2603:10a6:10:3c3:cafe::9a) by DUZPR01CA0006.outlook.office365.com (2603:10a6:10:3c3::13) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7677.33 via Frontend Transport; Wed, 19 Jun 2024 10:22:34 +0000 X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 193.8.40.94) smtp.mailfrom=leica-geosystems.com.cn; dkim=none (message not signed) header.d=none;dmarc=pass action=none header.from=leica-geosystems.com.cn; Received-SPF: Pass (protection.outlook.com: domain of leica-geosystems.com.cn designates 193.8.40.94 as permitted sender) receiver=protection.outlook.com; client-ip=193.8.40.94; helo=hexagon.com; pr=C Received: from hexagon.com (193.8.40.94) by DU2PEPF0001E9C3.mail.protection.outlook.com (10.167.8.72) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7677.15 via Frontend Transport; Wed, 19 Jun 2024 10:22:33 +0000 Received: from aherlnxbspsrv01.lgs-net.com ([10.60.34.116]) by hexagon.com with Microsoft SMTPSVC(10.0.17763.1697); Wed, 19 Jun 2024 12:22:30 +0200 From: LI Qingwu To: Qing-wu.Li@leica-geosystems.com.cn, oss-tools@pengutronix.de, m.felsch@pengutronix.de Date: Wed, 19 Jun 2024 12:22:27 +0200 Message-Id: <20240619102227.2013556-5-Qing-wu.Li@leica-geosystems.com.cn> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240619102227.2013556-1-Qing-wu.Li@leica-geosystems.com.cn> References: <20240619102227.2013556-1-Qing-wu.Li@leica-geosystems.com.cn> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-OriginalArrivalTime: 19 Jun 2024 10:22:30.0885 (UTC) FILETIME=[97A00150:01DAC232] X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: DU2PEPF0001E9C3:EE_|AM9PR06MB8001:EE_ Content-Type: text/plain X-MS-Office365-Filtering-Correlation-Id: daa53c35-c99e-4c93-5b03-08dc9049bc01 X-SET-LOWER-SCL-SCANNER: YES X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; ARA:13230037|1800799021|82310400023|36860700010|376011; X-Microsoft-Antispam-Message-Info: =?us-ascii?Q?G5wUCzVGJ+NAyCWJrCypAnQ5PZetq5yN75SEwdGYoWvpCxZcSKCE2yVi3w0r?= =?us-ascii?Q?EFiv2c/rTa7cTzh6s5Gd5ArUZI0sZrxPOOhaZJpcjkEl0e9iLgBuRPlYLqpW?= =?us-ascii?Q?vrY67oFxfUdxGdkcjaR/nfmE721YW3/NTBx5fH37GWvAnt5fVDdWS9djyoqE?= =?us-ascii?Q?vXO/Gs+7Izfnx1bpYtkhGceFD+xvzn3micMWyc7+VfMbKjzW5EhJixf4te9F?= =?us-ascii?Q?xhdTUpLgZ3pf2CLTgTEcSjS2fI3JLrGusCz1E+aXE+cLiUHY7BMFY9AgjV9m?= =?us-ascii?Q?OsEx25msk6QSvnH9Ap/ax0otfaSm7udFzsKYq6ZpePkG2cZftPShJGT+hc0u?= =?us-ascii?Q?21TXSYrMIgBZhAlUSj8zQGxBuDOB7YBclvKKUNh14dLOcFFgNCnOnPikEN6h?= =?us-ascii?Q?dvRs/jtdyh6RpV//ybxaxFCscq1ie+lcB0qfpdhnfRk9a6aOfZmpei47gKa4?= =?us-ascii?Q?xKnM1FJtNhNkt3Y0WhlqillZznhCT6OedppsySxCCGzn79VHujm48CmyAhPU?= =?us-ascii?Q?zkjaeOycpv0IXI4V9263N01snNJtwyJ8IZwOOuoztBO5s/Zi3EMkBohhkq8f?= =?us-ascii?Q?5P7OJ5m7rOkth2akaZDlLrvWjxV2BgSZ1iIFI8cijJil4pvrAWNLfHA6PxYk?= =?us-ascii?Q?w+wF2ZDtQ6BRmhHBfacUQVz3PUfMHY7NdVbCxOoAZ7SVP4BKRLN3z1xLi/PF?= =?us-ascii?Q?wckY8pCYUbNpNPi2iLaWiHq9obMnaAiLvAh0mvIYwuRcqw9zuiUOpXteDFJx?= =?us-ascii?Q?c/7AiMk9eXECBBwDDRWA1iXDMiY9qGyX5VgIqZMOxJ3M/uD2+vAvFLRLJSdP?= =?us-ascii?Q?qvFLXL4UVkC7zrD0tYxyBLEgrjRtQrnZjNhVFbA45qwCfyY9mXND14frmL9N?= =?us-ascii?Q?B0CenbW8sMevH/O1uUWFuaYOjS4vGmmgmWYBfrBfdGicVkKZMgqM9OmWXTrl?= =?us-ascii?Q?stDnGR1kx32HBxbs3pj9BCLKputWfOVSt+qkPjYKXN30jFSknvN0ddbE24GO?= =?us-ascii?Q?YNeUkEWD4lpohUVaZAMq8RD9MAbV5pLzlX9DZVPt9PqTdphIVpoCNnzDVTdy?= =?us-ascii?Q?NLzVkLyjxS2ZAzTdbHiuC5TRB0NVTW1BuYtAxvnTuAFNlCsRlj7iX3GyPo/E?= =?us-ascii?Q?V1xAM8BUdVqY6O7cFhWVgVte1OAgKQKJmiBT4XypQm9QNu7hD+Ygfdvh/dYO?= =?us-ascii?Q?pROsNI24qC6hEmPKqTrwIZ1ZKdXitOrWmAdLgp4s/BdwM1OlwOSBw70BnlBd?= =?us-ascii?Q?nz3rVsk3XK+mI7PcITMu/Vyedd0LAl50GLJpNBJwjAqn6wbmcIhrmRSqgKnQ?= =?us-ascii?Q?CeMiobRYHo/f/fnCsUmTQgDY7Aahym2koxNgClsUGsVnvKDOZH2yDzym14Yj?= =?us-ascii?Q?EJ2yVyDMlqkKps5uaiUm7MGDXEGtSv1fi0qL23EMYs45QcxR4Q=3D=3D?= X-Forefront-Antispam-Report: CIP:193.8.40.94; CTRY:CH; LANG:en; SCL:1; SRV:; IPV:CAL; SFV:NSPM; H:hexagon.com; PTR:ahersrvdom50.leica-geosystems.com; CAT:NONE; SFS:(13230037)(1800799021)(82310400023)(36860700010)(376011); DIR:OUT; SFP:1101; X-OriginatorOrg: leica-geosystems.com.cn X-MS-Exchange-CrossTenant-OriginalArrivalTime: 19 Jun 2024 10:22:33.9579 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: daa53c35-c99e-4c93-5b03-08dc9049bc01 X-MS-Exchange-CrossTenant-Id: 1b16ab3e-b8f6-4fe3-9f3e-2db7fe549f6a X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=1b16ab3e-b8f6-4fe3-9f3e-2db7fe549f6a; Ip=[193.8.40.94]; Helo=[hexagon.com] X-MS-Exchange-CrossTenant-AuthSource: DU2PEPF0001E9C3.eurprd03.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: AM9PR06MB8001 X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on metis.whiteo.stw.pengutronix.de X-Spam-Level: X-Spam-Status: No, score=-2.8 required=4.0 tests=AWL,BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_MSPIKE_H2,SPF_HELO_PASS, SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.2 X-Mailman-Approved-At: Wed, 19 Jun 2024 14:57:50 +0200 Subject: [OSS-Tools] [PATCH platsch V5 5/5] Add spinner executable for boot animation and text show X-BeenThere: oss-tools@pengutronix.de X-Mailman-Version: 2.1.29 Precedence: list List-Id: Pengutronix Public Open-Source-Development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: bsp-development.geo@leica-geosystems.com Sender: "OSS-Tools" X-SA-Exim-Connect-IP: 127.0.0.1 X-SA-Exim-Mail-From: oss-tools-bounces@pengutronix.de X-SA-Exim-Scanned: No (on metis.whiteo.stw.pengutronix.de); SAEximRunCond expanded to false This commit introduces a new executable, spinner, which supports two types of animations for boot sequences: 1 static PNG and text support. 2 rotates square PNG images per frame 3 shows a sequence of square images from a strip of PNG images. Signed-off-by: LI Qingwu --- README.rst | 37 ++++++ meson.build | 21 ++++ meson_options.txt | 1 + spinner.c | 299 ++++++++++++++++++++++++++++++++++++++++++++++ spinner_conf.c | 67 +++++++++++ spinner_conf.h | 33 +++++ 6 files changed, 458 insertions(+) create mode 100644 meson_options.txt create mode 100644 spinner.c create mode 100644 spinner_conf.c create mode 100644 spinner_conf.h diff --git a/README.rst b/README.rst index f1c0812..a0f7f7e 100644 --- a/README.rst +++ b/README.rst @@ -149,3 +149,40 @@ Compiling Instructions meson setup build meson compile -C build + + +Spinner - Splash Screen with Animation +====================================== + +The `spinner` executable is designed to provide boot animations. It supports two types of animations: + +1. **static PNG and text support**: Show a static png image and text. +1. **Square PNG Rotation Animation**: Rotates a square PNG image. +2. **Sequence Move Rectangle Animation**: Displays a sequence of square images from a strip of PNG images. + +spinner Configuration +--------------------- + +The configuration for the `spinner` executable is read from a configuration file, +with a default path of `/usr/share/platsch/spinner.conf`. +The directory of the configuration file can be set via the `platsch_directory` environment variable. + + +spinner Configuration +--------------------- + +Here is a sample of a configuration file (`spinner.conf`): + +.. code-block:: ini + + symbol="/usr/share/platsch/Spinner.png" + fps=20 + text="" + text_x=350 + text_y=400 + text_font="Sans" + text_size=30 + +Set text to empty if you don't want to display any text. +Set the symbol to empty if you don't want to display any animation. +The background image is indicated by ``--directory`` and ``--basename``. diff --git a/meson.build b/meson.build index ccc7a66..936e7ca 100644 --- a/meson.build +++ b/meson.build @@ -22,3 +22,24 @@ executable('platsch', install: true, install_dir : 'sbin' ) + + +if get_option('SPINNER') + spinner_dep = [ + dependency('cairo', version: '>= 1.0', static: true), + dep_libdrm, + ] + + spinner_src = [ + 'spinner.c', + 'spinner_conf.c' + ] + + executable('spinner', + spinner_src, + dependencies: spinner_dep, + link_with: libplatsch, + install: true, + install_dir : 'sbin' + ) +endif diff --git a/meson_options.txt b/meson_options.txt new file mode 100644 index 0000000..d1149fe --- /dev/null +++ b/meson_options.txt @@ -0,0 +1 @@ +option('SPINNER', type: 'boolean', value: false, description: 'Enable spinner build') diff --git a/spinner.c b/spinner.c new file mode 100644 index 0000000..dbf8a4b --- /dev/null +++ b/spinner.c @@ -0,0 +1,299 @@ + +#include +#include +#include + +#include "libplatsch.h" +#include "spinner_conf.h" + +typedef struct spinner { + cairo_format_t fmt; + cairo_surface_t *background_surface; + cairo_surface_t *symbol_surface; + cairo_surface_t *drawing_surface; + cairo_t *cr_background; + cairo_t *cr_drawing; + cairo_t *disp_cr; + int background_height; + int background_width; + int display_height; + int display_width; + int symbol_height; + int symbol_width; + struct modeset_dev *dev; + struct spinner *next; +} spinner_t; + +void on_draw_Sequence_animation(cairo_t *cr, spinner_t *data) +{ + static int current_frame; + int num_frames = data->symbol_width / data->symbol_height; + int frame_width = data->symbol_height; + + cairo_set_source_surface(cr, data->background_surface, 0, 0); + cairo_paint(cr); + + cairo_save(cr); + + cairo_translate(cr, data->display_width / 2, data->display_height / 2); + + cairo_set_source_surface(cr, data->symbol_surface, + -frame_width / 2 - current_frame * frame_width, -frame_width / 2); + + cairo_rectangle(cr, -frame_width / 2, -frame_width / 2, frame_width, frame_width); + cairo_clip(cr); + cairo_paint(cr); + + cairo_restore(cr); + + current_frame = (current_frame + 1) % num_frames; +} + +void on_draw_rotation_animation(cairo_t *cr, spinner_t *data) +{ + static float angle = 0.0; + + cairo_set_source_surface(cr, data->background_surface, 0, 0); + cairo_paint(cr); + cairo_save(cr); + cairo_translate(cr, data->background_width / 2, data->background_height / 2); + cairo_rotate(cr, angle); + cairo_translate(cr, -data->symbol_width / 2, -data->symbol_height / 2); + cairo_set_source_surface(cr, data->symbol_surface, 0, 0); + cairo_paint(cr); + cairo_restore(cr); + angle += 0.1; + if (angle > 2 * M_PI) + angle = 0.0; +} + +static uint32_t convert_to_cairo_format(uint32_t format) +{ + switch (format) { + case DRM_FORMAT_RGB565: + return CAIRO_FORMAT_RGB16_565; + case DRM_FORMAT_XRGB8888: + return CAIRO_FORMAT_ARGB32; + } + return CAIRO_FORMAT_INVALID; +} + +static void cairo_draw_text(cairo_t *cr, Config config) +{ + if (strlen(config.text) > 0) { + cairo_select_font_face(cr, config.text_font, CAIRO_FONT_SLANT_NORMAL, + CAIRO_FONT_WEIGHT_NORMAL); + cairo_set_font_size(cr, (double)config.text_size); + cairo_move_to(cr, config.text_x, config.text_y); + cairo_set_source_rgb(cr, 0, 0, 0); + cairo_show_text(cr, config.text); + } +} + +static int cairo_create_display_surface(struct modeset_dev *dev, spinner_t *spinner_node) +{ + cairo_surface_t *disp_surface = cairo_image_surface_create_for_data( + dev->map, convert_to_cairo_format(dev->format->format), dev->width, dev->height, + dev->stride); + if (cairo_surface_status(disp_surface)) { + platsch_error("Failed to create cairo surface\n"); + return EXIT_FAILURE; + } + spinner_node->display_width = cairo_image_surface_get_width(disp_surface); + spinner_node->display_height = cairo_image_surface_get_height(disp_surface); + spinner_node->fmt = cairo_image_surface_get_format(disp_surface); + spinner_node->disp_cr = cairo_create(disp_surface); + return EXIT_SUCCESS; +} + +// create a surface and paint background image +static int cairo_create_background_surface(spinner_t *spinner_node, Config config, const char *dir, + const char *base) +{ + char filename[128]; + int ret; + + ret = snprintf(filename, sizeof(filename), "%s/%s.png", dir, base); + if (ret >= sizeof(filename)) { + platsch_error("Failed to fit filename into buffer\n"); + return EXIT_FAILURE; + } + + spinner_node->background_surface = cairo_image_surface_create( + spinner_node->fmt, spinner_node->display_width, spinner_node->display_height); + if (cairo_surface_status(spinner_node->background_surface)) { + platsch_error("Failed to create background surface\n"); + return EXIT_FAILURE; + } + + cairo_surface_t *bk_img_surface = cairo_image_surface_create_from_png(filename); + + if (cairo_surface_status(bk_img_surface)) { + platsch_error("Failed to create cairo surface from %s\n", filename); + return EXIT_FAILURE; + } + + int image_width = cairo_image_surface_get_width(bk_img_surface); + int image_height = cairo_image_surface_get_height(bk_img_surface); + double scale_x = (double)spinner_node->display_width / image_width; + double scale_y = (double)spinner_node->display_height / image_height; + + spinner_node->cr_background = cairo_create(spinner_node->background_surface); + cairo_scale(spinner_node->cr_background, scale_x, scale_y); + cairo_set_source_surface(spinner_node->cr_background, bk_img_surface, 0, 0); + + spinner_node->background_width = + cairo_image_surface_get_width(spinner_node->background_surface); + spinner_node->background_height = + cairo_image_surface_get_height(spinner_node->background_surface); + + cairo_paint(spinner_node->cr_background); + cairo_draw_text(spinner_node->cr_background, config); + return EXIT_SUCCESS; +} + +static int cairo_create_symbol_surface(spinner_t *spinner_node, Config config) +{ + if (strlen(config.symbol) == 0) + return EXIT_FAILURE; + + spinner_node->symbol_surface = cairo_image_surface_create_from_png(config.symbol); + if (cairo_surface_status(spinner_node->symbol_surface)) { + platsch_error("Failed loading %s\n", config.symbol); + spinner_node->symbol_surface = NULL; + return EXIT_FAILURE; + } + spinner_node->symbol_width = cairo_image_surface_get_width(spinner_node->symbol_surface); + spinner_node->symbol_height = cairo_image_surface_get_height(spinner_node->symbol_surface); + return EXIT_SUCCESS; +} + +int main(int argc, char *argv[]) +{ + bool pid1 = getpid() == 1; + char **initsargv; + char filename[128]; + Config config = DEFAULT_CONFIG; + const char *base = "splash"; + const char *dir = "/usr/share/platsch"; + const char *env; + int ret; + long elapsed_time; + + spinner_t *spinner_list = NULL, *spinner_node = NULL, *spinner_iter = NULL; + struct modeset_dev *iter; + struct timeval start, end; + + env = getenv("platsch_directory"); + if (env) + dir = env; + + env = getenv("platsch_basename"); + if (env) + base = env; + + ret = snprintf(filename, sizeof(filename), "%s/spinner.conf", dir); + if (ret >= sizeof(filename)) { + platsch_error("Failed to fit filename\n"); + return EXIT_FAILURE; + } + + parseConfig(filename, &config); + + struct modeset_dev *modeset_list = platsch_init(); + + if (!modeset_list) { + platsch_error("Failed to initialize modeset\n"); + return EXIT_FAILURE; + } + + for (iter = modeset_list; iter; iter = iter->next) { + spinner_node = (spinner_t *)malloc(sizeof(spinner_t)); + if (!spinner_node) { + platsch_error("Failed to allocate memory for spinner_node\n"); + return EXIT_FAILURE; + } + memset(spinner_node, 0, sizeof(*spinner_node)); + + if (cairo_create_display_surface(iter, spinner_node)) + return EXIT_FAILURE; + + if (cairo_create_background_surface(spinner_node, config, dir, base)) + return EXIT_FAILURE; + + cairo_create_symbol_surface(spinner_node, config); + + spinner_node->drawing_surface = cairo_image_surface_create( + spinner_node->fmt, spinner_node->display_width, spinner_node->display_height); + if (cairo_surface_status(spinner_node->drawing_surface)) { + platsch_error("Failed to create drawing surface\n"); + return EXIT_FAILURE; + } + // create cairo context for drawing surface to avoid frlickering + spinner_node->cr_drawing = cairo_create(spinner_node->drawing_surface); + // draw static image with text as first frame + cairo_set_source_surface(spinner_node->disp_cr, spinner_node->background_surface, 0, + 0); + cairo_paint(spinner_node->disp_cr); + platsch_setup_display(iter); + + spinner_node->next = spinner_list; + spinner_list = spinner_node; + } + + platsch_drmDropMaster(); + + ret = fork(); + if (ret < 0) + platsch_error("failed to fork for init: %m\n"); + else if (ret == 0) + /* + * Always fork to make sure we got the required + * resources for drawing the animation in the child. + */ + goto drawing; + + if (pid1) { + initsargv = calloc(sizeof(argv[0]), argc + 1); + + if (!initsargv) { + platsch_error("failed to allocate argv for init\n"); + return EXIT_FAILURE; + } + memcpy(initsargv, argv, argc * sizeof(argv[0])); + initsargv[0] = "/sbin/init"; + initsargv[argc] = NULL; + + execv("/sbin/init", initsargv); + + platsch_error("failed to exec init: %m\n"); + + return EXIT_FAILURE; + } + return EXIT_SUCCESS; + +drawing: + while (true) { + gettimeofday(&start, NULL); + for (spinner_iter = spinner_list; spinner_iter; spinner_iter = spinner_iter->next) { + if (spinner_node->symbol_surface == NULL) + usleep(1000000 / config.fps); + if (spinner_node->symbol_width / spinner_node->symbol_height > 2) + on_draw_Sequence_animation(spinner_iter->cr_drawing, spinner_iter); + else + on_draw_rotation_animation(spinner_iter->cr_drawing, spinner_iter); + + cairo_set_source_surface(spinner_iter->disp_cr, + spinner_iter->drawing_surface, 0, 0); + cairo_paint(spinner_iter->disp_cr); + } + gettimeofday(&end, NULL); + elapsed_time = + (end.tv_sec - start.tv_sec) * 1000000 + (end.tv_usec - start.tv_usec); + + long sleep_time = (1000000 / config.fps) - elapsed_time; + + if (sleep_time > 0) + usleep(sleep_time); + } +} diff --git a/spinner_conf.c b/spinner_conf.c new file mode 100644 index 0000000..491b1c3 --- /dev/null +++ b/spinner_conf.c @@ -0,0 +1,67 @@ +#include "spinner_conf.h" +#include + +int parseConfig(const char *filename, Config *config) +{ + char *value_end; + char *value_start; + char key[MAX_LINE_LENGTH]; + char line[MAX_LINE_LENGTH]; + char value[MAX_LINE_LENGTH]; + FILE *file; + + file = fopen(filename, "r"); + if (file == NULL) { + fprintf(stderr, "Unable to open file: %s\n", filename); + return -EFAULT; + } + + while (fgets(line, sizeof(line), file)) { + if (strlen(line) > MAX_LINE_LENGTH) { + fprintf(stderr, "conf string too long\n"); + continue; + } + if (line[0] != '#' && sscanf(line, "%[^=]=%[^\n]", key, value) == 2) { + value_start = strchr(line, '=') + 1; + value_end = line + strlen(line) - 1; + + while (isspace(*value_start)) + value_start++; + while (isspace(*value_end) || *value_end == '"') + value_end--; + + if (*value_start == '"') + value_start++; + if (*value_end == '"') + value_end--; + + if (value_end < value_start) { + memset(value, 0, sizeof(value)); + continue; + } else { + strncpy(value, value_start, value_end - value_start + 1); + value[value_end - value_start + 1] = '\0'; + value[sizeof(value) - 1] = '\0'; + } + value[MAX_LINE_LENGTH - 1] = '\0'; + + if (strcmp(key, "symbol") == 0) { + strncpy(config->symbol, value, MAX_LINE_LENGTH); + } else if (strcmp(key, "text") == 0) { + strncpy(config->text, value, MAX_LINE_LENGTH); + } else if (strcmp(key, "fps") == 0) { + config->fps = atoi(value); + } else if (strcmp(key, "text_x") == 0) { + config->text_x = atoi(value); + } else if (strcmp(key, "text_y") == 0) { + config->text_y = atoi(value); + } else if (strcmp(key, "text_font") == 0) { + strncpy(config->text_font, value, MAX_LINE_LENGTH); + } else if (strcmp(key, "text_size") == 0) { + config->text_size = atoi(value); + } + } + } + fclose(file); + return 0; +} diff --git a/spinner_conf.h b/spinner_conf.h new file mode 100644 index 0000000..4f8501b --- /dev/null +++ b/spinner_conf.h @@ -0,0 +1,33 @@ +#include +#include +#include +#include + +#ifndef __SPINNER_CONF_H__ +#define __SPINNER_CONF_H__ + + +#define MAX_LINE_LENGTH 128 + +typedef struct { + char symbol[MAX_LINE_LENGTH]; + int fps; + int text_x; + int text_y; + char text_font[MAX_LINE_LENGTH]; + int text_size; + char text[MAX_LINE_LENGTH]; +} Config; + +int parseConfig(const char *filename, Config *config); + +#define DEFAULT_CONFIG { \ + .symbol = "", \ + .fps = 20, \ + .text_x = 0, \ + .text_y = 0, \ + .text_font = "Sans", \ + .text_size = 30, \ + .text = "" \ +} +#endif -- 2.34.1