From mboxrd@z Thu Jan 1 00:00:00 1970 Delivery-date: Thu, 04 Jul 2024 11:59:04 +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 1sPJF0-001Sv0-21 for lore@lore.pengutronix.de; Thu, 04 Jul 2024 11:59:04 +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 1sPJEz-0003Ia-8s; Thu, 04 Jul 2024 11:59:01 +0200 Received: from mail-vi1eur05on2046.outbound.protection.outlook.com ([40.107.21.46] helo=EUR05-VI1-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 1sPJ2z-0002V4-Og; Thu, 04 Jul 2024 11:46:39 +0200 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=JKOWwvIsLFdprUZVuiwiNbzr9hkmKdu42pbX0FgKXl1gLwLW7nbrIaQX6Cf1S+AuooLpKDpQNg2WMP1iowt98zLd0zo2aHhiNbzdUTDEA9o/fHmEkx2OXaxKuHIcn/q8wUYPa+/WIRfj3lkPO3s22/J8GQLl1rusN21hdfcRlM4z2NmTIlKdz5773Bg7NNNqjG44wqVUyx7j+FT1HSYaUBuA29WCj7dwxTSensvDD1eQsY1cfxNZYZA0V9eFFkXhhodkaqGhhs8ynM74haby9a68F4XrzLviHsfDbv/HaPLtxLOljOxao6jZ6XkPFcswokAk7D9XtJob4VDjn4JnFw== 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=yeKT+evHOqsXlqWZE8gBSgaxAilXONsBVJnN77+v9ns=; b=SYQDJv1p19dOdm92Tye2jmnTxFw53P0qc+mlDsrxLhg1kaSp3ZBaCHPzRdcpG95UzfqYHfh7pCiGCpYVMsR15/0Uu3r2OkyQ+7NDxY2blPXL0PcARz0Hv26oEVDaYVYJYWif3HTlyufamx+hHjmn+rbqCmlRJwvW8b3cezuSEn/W8zmvdztN4vDHdF0D0SeAAM5y+hzc0CXFsCnkdNfcMnsH0dlxhUvFakne8b2ujKgaPHDQ64pWCY30KUKKa6EFpeF3xNrOo8gAK8dnd+WdylZIJmEknwwodueg1nxiYHghCPK5m5JvDRCkTeYyPWTdp5XyHOa7e0MvCB4HYpW7zQ== 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=yeKT+evHOqsXlqWZE8gBSgaxAilXONsBVJnN77+v9ns=; b=ClB4I6PAvsDOGagl6bUustpIRFpYnAXhow+9D3V/NDQHO5YCNz9cxsYKE254LIXjr5FW8GbnkV9rGMZ7QYq4mh2NXe1ddzEh1tZHVt2hdw4VZ3lzGSO12ze047evHz0i+jeDBRHD1RfPmoOXHh/oV6wsWd0BTG0YrTQvgsVEwvg= Received: from AM0PR02CA0008.eurprd02.prod.outlook.com (2603:10a6:208:3e::21) by AS8PR06MB7544.eurprd06.prod.outlook.com (2603:10a6:20b:339::13) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7741.29; Thu, 4 Jul 2024 09:46:35 +0000 Received: from AM3PEPF0000A79C.eurprd04.prod.outlook.com (2603:10a6:208:3e:cafe::f7) by AM0PR02CA0008.outlook.office365.com (2603:10a6:208:3e::21) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7719.34 via Frontend Transport; Thu, 4 Jul 2024 09:46:35 +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 AM3PEPF0000A79C.mail.protection.outlook.com (10.167.16.107) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7741.18 via Frontend Transport; Thu, 4 Jul 2024 09:46:35 +0000 Received: from aherlnxbspsrv01.lgs-net.com ([10.60.34.116]) by hexagon.com with Microsoft SMTPSVC(10.0.17763.1697); Thu, 4 Jul 2024 11:46:32 +0200 From: LI Qingwu To: Qing-wu.Li@leica-geosystems.com.cn, jie.gu@leica-geosystems.com.cn, oss-tools@pengutronix.de, m.felsch@pengutronix.de Date: Thu, 4 Jul 2024 11:46:30 +0200 Message-Id: <20240704094630.1216852-5-Qing-wu.Li@leica-geosystems.com.cn> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240704094630.1216852-1-Qing-wu.Li@leica-geosystems.com.cn> References: <20240704094630.1216852-1-Qing-wu.Li@leica-geosystems.com.cn> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-OriginalArrivalTime: 04 Jul 2024 09:46:32.0987 (UTC) FILETIME=[0D9D26B0:01DACDF7] X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: AM3PEPF0000A79C:EE_|AS8PR06MB7544:EE_ Content-Type: text/plain X-MS-Office365-Filtering-Correlation-Id: 653f3325-6575-4fc2-73b7-08dc9c0e31cf X-SET-LOWER-SCL-SCANNER: YES X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; ARA:13230040|82310400026|36860700013|1800799024|376014; X-Microsoft-Antispam-Message-Info: =?us-ascii?Q?nuInu0vSOTcjjEcuPnfaDbsL6jAh809dXwjq6odmr0Lhi0RRaVewUMcTI8vH?= =?us-ascii?Q?WyeRib7kNo86g8IE4jbseiaId7/TLCMX75rQjS+9+PlmB71KfS6vH3ST+ntL?= =?us-ascii?Q?j3C8L6CU6LS6JLPBBmCH9se3/4V7RxSDCyNgv/6/lYhCFlGX5ERgCibhN0Xk?= =?us-ascii?Q?5Q2dcQuim5qoNg9VcWvB7XilR0V/ihJYJMUEy5cg1mJ+7QCfm3oOmwuXQXbq?= =?us-ascii?Q?L54nRsq4GPe60Mk2U03Qb1aTQlJVUm5Y1e1IXMGTgtwXMr6pVXiJEUTIHMvC?= =?us-ascii?Q?5Vb9KywPcABdGSQauL3NIWaX7x1QJK6HjfWLW6Be2/4FNEEwgy00M7dv6KFb?= =?us-ascii?Q?KI5JoGBAxNZ+MtQOp7XNw2yhKeHld6uMH0NxmXKW4drPZf3ZXhRWIkyHvxjz?= =?us-ascii?Q?1cV52TJr0Ke/Bmh1FBre7hho6kTPXp494GU+9c9FbQEsCt90qkhCIsEHP8aG?= =?us-ascii?Q?96s+XgpWK0V3Bgg08SCjLraj33ezOpJ5bH8+i9Fl8OkU/k7OWsADRB4qp8/v?= =?us-ascii?Q?P3RF0jda+UmHbicYgGigAvPG4My/3yAuolsuqI/frBBWabqDWrzhHJv1IDCQ?= =?us-ascii?Q?a/R8vwi/17Qn8FzaCl1dOR/p27esCLa/ONPALvg76wvjIOXrCi5UdhGYVPOp?= =?us-ascii?Q?PHlj8EdymtG3GXciH3p8OXUKxXH4fQJ9qAVBlxW/N9i2L7x1YCCHB6VCdo9f?= =?us-ascii?Q?mj/rQfQwI9KDn5h8gkYDIN5J1vuyk8EfDf14W0inQiCSk03Cl6mfHdPbhP54?= =?us-ascii?Q?0uEOkCxEsa9ISbW6Ul1K43ZA5UCD1c8VsDSCgtNgJjHfoqMBaWRtombPcZxQ?= =?us-ascii?Q?ttbyQqT9wdfkdYqr2rjl6qQbkbgKcm4RCztrMiFp6GYvgFOCC2S0c2a5Vn3N?= =?us-ascii?Q?bz39FJWMLUeA1KZSsEMwWD/JQoK9rmS0mwd6zmZSVoVDDxO0T55QPW/lzM5f?= =?us-ascii?Q?BZfyCWil2UBN1GRiJy0IUf4Xa3NZh/7/2uKs+WK8PjAseTZrEspgyanUP30l?= =?us-ascii?Q?i6QG6CsJSEJK5Nw4ywVFQH8qs97eqhM5EMdA30HE3gg6MWYEp9A/hoIrMIke?= =?us-ascii?Q?UwPKIV9Y6VEYgxOl/8U2b4d92ED+Dfmy0pbazTuCEuxJmvDJA7mdJyFmvfI/?= =?us-ascii?Q?xRoyppDC3fxEIX/ANwPVPLvktRcVIumuKd/W2Sm6ZXMLcKUecei9R0bqWC0k?= =?us-ascii?Q?RdcYUcJr6w3WiiaEws13XihTmBYXaYhF6XSYgZnhTFb1ix0dYuGYWizzxgAH?= =?us-ascii?Q?OOMxLrK4umOWs6lBFSS6alLcJW7CPRX8nZInh+1+SMDLibcS30t8bbwpJZGq?= =?us-ascii?Q?vq4Lq70hNelBUj44CDVpvHbdnVYsUc6UIY/r9uZ2DC6Ww8PCLxPyFy/Do7op?= =?us-ascii?Q?UctnUUAUhDsnPm8PcvDlF2BH+9jWqUqV+YABf8jjx08Zjx1QjaF9IwWpBviy?= =?us-ascii?Q?oD746Uc1Id0TRvFal2CtPzEz5nbFvqLr?= 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:(13230040)(82310400026)(36860700013)(1800799024)(376014); DIR:OUT; SFP:1101; X-OriginatorOrg: leica-geosystems.com.cn X-MS-Exchange-CrossTenant-OriginalArrivalTime: 04 Jul 2024 09:46:35.8140 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 653f3325-6575-4fc2-73b7-08dc9c0e31cf 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: AM3PEPF0000A79C.eurprd04.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: AS8PR06MB7544 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.7 required=4.0 tests=AWL,BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H2,SPF_HELO_PASS,SPF_PASS autolearn=ham autolearn_force=no version=3.4.2 X-Mailman-Approved-At: Thu, 04 Jul 2024 11:58:58 +0200 Subject: [OSS-Tools] [PATCH platsch V6 RESEND 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 | 49 +++++++ meson.build | 20 +++ meson_options.txt | 1 + spinner.c | 323 ++++++++++++++++++++++++++++++++++++++++++++++ spinner_conf.c | 73 +++++++++++ spinner_conf.h | 39 ++++++ 6 files changed, 505 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 e905437..27765ac 100644 --- a/README.rst +++ b/README.rst @@ -151,3 +151,52 @@ Compiling Instructions meson compile -C build To ensure fast startup, ``platsch`` prefers using static libraries. + +Spinner - Splash Screen with Animation +====================================== + +The ``spinner`` executable is designed to provide boot animations. It supports +three types of animations: + +1. **Static PNG and Text Support**: Displays a static PNG image and text. +2. **Square PNG Rotation Animation**: Rotates a square PNG image. +3. **Sequence Move Rectangle Animation**: Displays a sequence of square images + from a strip of PNG images. + +To build the ``spinner`` executable, use the following commands: + +.. code-block:: shell + + meson setup -Dspinner=true -Dprefer_static=true build + meson compile -C build + +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. + +Here is a sample 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 + text=qwqewrqr + symbol_x=-1 + symbol_y=-1 + rotate_step=0.1 + +- Set ``text`` to empty if you don't want to display any text. +- Set ``symbol`` to empty if you don't want to display any animation. +- The background image is indicated by ``--directory`` and ``--basename``. +- Set ``symbol_x`` and ``symbol_y`` to negative values for automatic centering. + +Please stop the spinner process after the system has booted, otherwise it keeps causing unnecessary CPU load. diff --git a/meson.build b/meson.build index ccc3d2d..c9c0e76 100644 --- a/meson.build +++ b/meson.build @@ -20,3 +20,23 @@ executable('platsch', install: true, install_dir: 'sbin' ) + +if get_option('spinner') + spinner_dep = [ + dependency('cairo', version: '>= 1.0'), + 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..607ecaf --- /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..dc4a191 --- /dev/null +++ b/spinner.c @@ -0,0 +1,323 @@ + +#include +#include +#include +#include + +#include "libplatsch.h" +#include "spinner_conf.h" + +typedef struct spinner { + cairo_format_t format; + cairo_surface_t *background_surface; + cairo_surface_t *symbol_surface; + cairo_t *disp_cr; + int display_height; + int display_width; + int symbol_frames; + int symbol_height; + int symbol_width; + int symbol_x; + int symbol_y; + float rotate_step; + struct modeset_dev *dev; + struct spinner *next; +} spinner_t; + +void draw_background(cairo_t *cr, spinner_t *data) +{ + cairo_set_source_surface(cr, data->background_surface, -data->symbol_x, -data->symbol_y); + cairo_paint(cr); + cairo_save(cr); +} + +void on_draw_sequence_animation(cairo_t *cr, spinner_t *data) +{ + static int frame; + + int x = -data->symbol_width / 2; + int y = -data->symbol_height / 2; + + int width = cairo_image_surface_get_width(data->symbol_surface); + int height = cairo_image_surface_get_height(data->symbol_surface); + + if (width > height) + x = -data->symbol_width / 2 - frame * data->symbol_width; + else + y = -data->symbol_height / 2 - frame * data->symbol_height; + + draw_background(cr, data); + cairo_translate(cr, data->symbol_width / 2, data->symbol_height / 2); + cairo_set_source_surface(cr, data->symbol_surface, x, y); + cairo_paint(cr); + cairo_restore(cr); + frame = (frame + 1) % data->symbol_frames; +} + +void on_draw_rotation_animation(cairo_t *cr, spinner_t *data) +{ + static float angle = 0.0; + + draw_background(cr, data); + cairo_translate(cr, data->symbol_width / 2, data->symbol_height / 2); + cairo_rotate(cr, angle); + cairo_set_source_surface(cr, data->symbol_surface, -data->symbol_width / 2, + -data->symbol_height / 2); + cairo_paint(cr); + cairo_restore(cr); + angle += data->rotate_step; + 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 *data) +{ + 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; + } + data->display_width = cairo_image_surface_get_width(disp_surface); + data->display_height = cairo_image_surface_get_height(disp_surface); + data->format = cairo_image_surface_get_format(disp_surface); + data->disp_cr = cairo_create(disp_surface); + return EXIT_SUCCESS; +} + +// create a surface and paint background image +static int cairo_create_background_surface(spinner_t *data, Config config, const char *dir, + const char *base) +{ + char file_name[PLATSCH_MAX_FILENAME_LENGTH]; + int ret; + + ret = snprintf(file_name, sizeof(file_name), "%s/%s.png", dir, base); + if (ret >= sizeof(file_name)) { + platsch_error("Failed to fit file_name into buffer\n"); + return EXIT_FAILURE; + } + + data->background_surface = + cairo_image_surface_create(data->format, data->display_width, data->display_height); + if (cairo_surface_status(data->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(file_name); + + if (cairo_surface_status(bk_img_surface)) { + platsch_error("Failed to create cairo surface from %s\n", file_name); + 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)data->display_width / image_width; + double scale_y = (double)data->display_height / image_height; + + cairo_t *cr_background = cairo_create(data->background_surface); + cairo_scale(cr_background, scale_x, scale_y); + cairo_set_source_surface(cr_background, bk_img_surface, 0, 0); + + cairo_paint(cr_background); + cairo_draw_text(cr_background, config); + return EXIT_SUCCESS; +} + +static int cairo_create_symbol_surface(spinner_t *data, Config config) +{ + if (strlen(config.symbol) == 0) + return EXIT_FAILURE; + + data->symbol_surface = cairo_image_surface_create_from_png(config.symbol); + if (cairo_surface_status(data->symbol_surface)) { + platsch_error("Failed loading %s\n", config.symbol); + data->symbol_surface = NULL; + return EXIT_FAILURE; + } + data->symbol_width = cairo_image_surface_get_width(data->symbol_surface); + data->symbol_height = cairo_image_surface_get_height(data->symbol_surface); + + if (data->symbol_width > data->symbol_height) { + data->symbol_frames = data->symbol_width / data->symbol_height; + data->symbol_width = data->symbol_height; + } else { + data->symbol_frames = data->symbol_height / data->symbol_width; + data->symbol_height = data->symbol_width; + } + + data->rotate_step = config.rotate_step; + + if (config.symbol_x <0) + data->symbol_x = data->display_width / 2 - data->symbol_width / 2; + else + data->symbol_x = config.symbol_x; + + if(config.symbol_y <0) + data->symbol_y = data->display_height / 2 - data->symbol_height / 2; + else + data->symbol_y = config.symbol_y; + + return EXIT_SUCCESS; +} + +int main(int argc, char *argv[]) +{ + bool pid1 = getpid() == 1; + char **initsargv; + char file_name[PLATSCH_MAX_FILENAME_LENGTH]; + Config config = DEFAULT_CONFIG; + const char *base = "splash"; + const char *dir = "/usr/share/platsch"; + const char *env; + int ret; + + spinner_t *spinner_list = NULL, *spinner_node = NULL, *spinner_iter = NULL; + struct modeset_dev *iter; + struct timespec start, end; + + env = getenv("platsch_directory"); + if (env) + dir = env; + + env = getenv("platsch_basename"); + if (env) + base = env; + + ret = snprintf(file_name, sizeof(file_name), "%s/spinner.conf", dir); + if (ret >= sizeof(file_name)) { + platsch_error("Failed to fit file_name\n"); + return EXIT_FAILURE; + } + + parseConfig(file_name, &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); + + 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: + // create cairo surface for drawing symbol to avoid frlickering. + cairo_surface_t *drawing_surface = cairo_image_surface_create( + spinner_node->format, spinner_node->symbol_width, spinner_node->symbol_height); + if (cairo_surface_status(drawing_surface)) { + platsch_error("Failed to create drawing surface\n"); + return EXIT_FAILURE; + } + + cairo_t *cr_drawing = cairo_create(drawing_surface); + + while (true) { + clock_gettime(CLOCK_MONOTONIC, &start); + + for (spinner_iter = spinner_list; spinner_iter; spinner_iter = spinner_iter->next) { + if (spinner_node->symbol_surface == NULL) { + sleep(10); + continue; + } + + if (spinner_node->symbol_frames > 1) + on_draw_sequence_animation(cr_drawing, spinner_iter); + else + on_draw_rotation_animation(cr_drawing, spinner_iter); + + cairo_set_source_surface(spinner_iter->disp_cr, drawing_surface, + spinner_iter->symbol_x, spinner_iter->symbol_y); + cairo_paint(spinner_iter->disp_cr); + } + + clock_gettime(CLOCK_MONOTONIC, &end); + long elapsed_time = + (end.tv_sec - start.tv_sec) * 1000000L + (end.tv_nsec - start.tv_nsec) / 1000; + 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..b867811 --- /dev/null +++ b/spinner_conf.c @@ -0,0 +1,73 @@ +#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); + } else if (strcmp(key, "rotate_step") == 0) { + config->rotate_step = atof(value); + } else if (strcmp(key, "symbol_x") == 0) { + config->symbol_x = atoi(value); + } else if (strcmp(key, "symbol_y") == 0) { + config->symbol_y = atoi(value); + } + } + } + fclose(file); + return 0; +} diff --git a/spinner_conf.h b/spinner_conf.h new file mode 100644 index 0000000..c397b9a --- /dev/null +++ b/spinner_conf.h @@ -0,0 +1,39 @@ +#include +#include +#include +#include + +#ifndef __SPINNER_CONF_H__ +#define __SPINNER_CONF_H__ + + +#define MAX_LINE_LENGTH 128 + +typedef struct { + char symbol[MAX_LINE_LENGTH]; + char text_font[MAX_LINE_LENGTH]; + char text[MAX_LINE_LENGTH]; + float rotate_step; + int fps; + int symbol_x; + int symbol_y; + int text_size; + int text_x; + int text_y; +} 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 = "", \ + .rotate_step = 0.1, \ + .symbol_x = -1, \ + .symbol_y = -1 \ +} +#endif -- 2.34.1