This commit introduces a new executable, spinner, which supports two types of animations for boot sequences:
1 rotates square PNG images per frame 2 shows a sequence of square images from a strip of PNG images. it supports read configuration from a conf file. the default path of conf is /usr/share/platsch/spinner.conf the dir of the conf can be set via env platsch_directory here is an example of conf: backdrop="/usr/share/platsch/splash.png" symbol="/usr/share/platsch/Spinner.png" fps=20 frames=0 text="Now loading..." text_x=350 text_y=400 text_font="Sans" textsize=30 Signed-off-by: LI Qingwu <qing-wu...@leica-geosystems.com.cn> --- README.rst | 76 ++++++++++++- meson.build | 30 +++++- meson_options.txt | 1 + spinner.c | 266 ++++++++++++++++++++++++++++++++++++++++++++++ spinner.conf | 13 +++ spinner_conf.c | 66 ++++++++++++ spinner_conf.h | 39 +++++++ 7 files changed, 487 insertions(+), 4 deletions(-) create mode 100644 spinner.c create mode 100644 spinner.conf create mode 100644 spinner_conf.c create mode 100644 spinner_conf.h diff --git a/README.rst b/README.rst index 2af29e4..ee0da5f 100644 --- a/README.rst +++ b/README.rst @@ -45,7 +45,7 @@ RGB565 This generates a 1920x1080 splash image in ``RGB565`` format from a png file:: #!/bin/bash - magick \ + convert \ /path/to/source.png \ -resize 1920x1080\! \ -flip \ @@ -62,7 +62,7 @@ This generates a 1920x1080 splash image in ``XRGB8888`` format from a png file:: #!/bin/bash - magick \ + convert \ /path/to/source.png \ -resize 1920x1080\! \ -flip \ @@ -149,9 +149,42 @@ To cross-compile the project, use the following commands: .. code-block:: shell - meson build --cross-file=<path-to-meson-cross-file> + meson setup --cross-file=<path-to-meson-cross-file> ninja -C build +Here are sample cross commands: + +.. code-block:: shell + + meson setup ./build -DHAVE_CAIRO=true --cross-file ./meson.cross + ninja -C build + +Here is a sample cross file: + +.. code-block:: ini + + [binaries] + c = ${CC} + cpp = ${CXX} + cython = 'cython3' + ar = '${AR}' + nm = '${NM}' + strip = '${STRIP}' + readelf = '${READELF}' + objcopy = '${OBJCOPY}' + pkgconfig = '${pkgconfig}' + + [properties] + needs_exe_wrapper = true + + + [target_machine] + system = 'linux' + cpu_family = 'aarch64' + cpu = 'aarch64' + endian = 'little' + + Build options ------------- @@ -168,3 +201,40 @@ The following build options are available: - true, false - true - Enable Cairo support + * - SPINNER + - true, false + - false + - Enable spinner + +Spinner - Splash Screen with Animation +====================================== + +The `spinner` executable is designed to provide boot animations. It supports two types of animations: + +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. + +Example Configuration File +-------------------------- + +Here is an example of a configuration file (`spinner.conf`): + +.. code-block:: ini + + backdrop="/usr/share/platsch/splash.png" + symbol="/usr/share/platsch/Spinner.png" + fps=20 + # frames=0 means infinite + frames=0 + text="text to display" + text_x=350 + text_y=400 + text_font="Sans" + text_size=30 diff --git a/meson.build b/meson.build index 8c15fb9..f12b2da 100644 --- a/meson.build +++ b/meson.build @@ -1,6 +1,13 @@ project('platsch', 'c') -have_cairo = get_option('HAVE_CAIRO') + +# If SPINNER is true, force HAVE_CAIRO to be true +if get_option('SPINNER') + message('SPINNER is enabled, forcing HAVE_CAIRO to be true') + have_cairo = true +else + have_cairo = get_option('HAVE_CAIRO') +endif # Define dependencies conditionally based on the HAVE_CAIRO option platsch_dep = [dependency('libdrm', required: true)] @@ -33,3 +40,24 @@ executable('platsch', install: true, include_directories: include_directories('.') ) + +# Create the spinner executable if SPINNER true +if get_option('SPINNER') + spinner_dep = [ + dependency('cairo', required: true), + dependency('libdrm', required: true) + ] + + spinner_src = [ + 'spinner.c', + 'spinner_conf.c' + ] + executable('spinner', + spinner_src, + dependencies: spinner_dep, + link_with: libplatsch, + c_args: args, + install: true, + include_directories: include_directories('.') + ) +endif \ No newline at end of file diff --git a/meson_options.txt b/meson_options.txt index 1adfef3..78c4fba 100644 --- a/meson_options.txt +++ b/meson_options.txt @@ -1 +1,2 @@ option('HAVE_CAIRO', type: 'boolean', value: true, description: 'Enable Cairo support') +option('SPINNER', type: 'boolean', value: false, description: 'Enable spinner') diff --git a/spinner.c b/spinner.c new file mode 100644 index 0000000..9a68be7 --- /dev/null +++ b/spinner.c @@ -0,0 +1,266 @@ +#include "libplatsch.h" +#include "spinner_conf.h" +#include <cairo.h> +#include <math.h> +#include <sys/time.h> + +typedef struct spinner { + cairo_format_t fmt; + cairo_surface_t *background_surface; + cairo_surface_t *icon_surface; + cairo_surface_t *image_surface; + cairo_surface_t *drawing_surface; + cairo_t *cr_background; + cairo_t *cr_drawing; + cairo_t *device_cr; + int background_height; + int background_width; + int display_height; + int display_width; + int icon_height; + int icon_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->icon_width / data->icon_height; + int frame_width = data->icon_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->icon_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->icon_width / 2, -data->icon_height / 2); + cairo_set_source_surface(cr, data->icon_surface, 0, 0); + cairo_paint(cr); + cairo_restore(cr); + angle += 0.1; + if (angle > 2 * M_PI) + angle = 0.0; +} + +int main(int argc, char *argv[]) +{ + bool pid1 = getpid() == 1; + char filename[128]; + Config config = DEFAULT_CONFIG; + const char *base = "splash"; + const char *dir = "/usr/share/platsch"; + const char *env; + int frames; + 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)) { + error("Failed to fit filename\n"); + return EXIT_FAILURE; + } + + parseConfig(filename, &config); + + struct modeset_dev *modeset_list = init(); + + if (!modeset_list) { + fprintf(stderr, "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) { + fprintf(stderr, "Failed to allocate memory for spinner_node\n"); + return EXIT_FAILURE; + } + memset(spinner_node, 0, sizeof(*spinner_node)); + printf("spinner_node=%p\n", spinner_node); + + spinner_node->device_cr = cairo_init(iter, dir, base); + if (!spinner_node->device_cr) + return EXIT_FAILURE; + + cairo_surface_t *surface = cairo_get_target(spinner_node->device_cr); + + if (cairo_surface_status(surface) != CAIRO_STATUS_SUCCESS) { + fprintf(stderr, "Failed to get cairo surface\n"); + return EXIT_FAILURE; + } + spinner_node->display_width = cairo_image_surface_get_width(surface); + spinner_node->display_height = cairo_image_surface_get_height(surface); + spinner_node->fmt = cairo_image_surface_get_format(surface); + + 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) + != CAIRO_STATUS_SUCCESS) { + fprintf(stderr, "Failed to load splash.png\n"); + return EXIT_FAILURE; + } + + spinner_node->image_surface = cairo_image_surface_create_from_png(config.backdrop); + if (cairo_surface_status(spinner_node->image_surface) != CAIRO_STATUS_SUCCESS) { + error("Failed to create cairo surface from %s\n", config.backdrop); + return EXIT_FAILURE; + } + + int image_width = cairo_image_surface_get_width(spinner_node->image_surface); + int image_height = cairo_image_surface_get_height(spinner_node->image_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, + spinner_node->image_surface, 0, 0); + + cairo_paint(spinner_node->cr_background); + + cairo_select_font_face(spinner_node->cr_background, config.text_font, + CAIRO_FONT_SLANT_NORMAL, CAIRO_FONT_WEIGHT_NORMAL); + cairo_set_font_size(spinner_node->cr_background, (double)config.text_size); + cairo_set_source_rgb(spinner_node->cr_background, 0, 0, 0); + cairo_move_to(spinner_node->cr_background, config.text_x, config.text_y); + cairo_show_text(spinner_node->cr_background, config.text); + + 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); + printf("spinner_node->background_width=%d, spinner_node->background_height=%d\n", + spinner_node->background_width, spinner_node->background_height); + + spinner_node->icon_surface = cairo_image_surface_create_from_png(config.symbol); + if (cairo_surface_status(spinner_node->icon_surface) != CAIRO_STATUS_SUCCESS) { + fprintf(stderr, "Failed to load %s\n", config.symbol); + return EXIT_FAILURE; + } + spinner_node->icon_width = cairo_image_surface_get_width( + spinner_node->icon_surface); + spinner_node->icon_height = cairo_image_surface_get_height( + spinner_node->icon_surface); + printf("spinner_node->icon_width=%d, spinner_node->icon_height=%d\n", + spinner_node->icon_width, spinner_node->icon_height); + + 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) != CAIRO_STATUS_SUCCESS) { + error("Failed to create drawing surface\n"); + return EXIT_FAILURE; + } + spinner_node->cr_drawing = cairo_create(spinner_node->drawing_surface); + + cairo_set_source_surface( + spinner_node->device_cr, + spinner_node->drawing_surface, 0, 0); + update_display(iter); + + spinner_node->next = spinner_list; + spinner_list = spinner_node; + } + + if (pid1) { + char **initsargv; + + ret = fork(); + printf("fork ret=%d\n", ret); + if (ret < 0) + error("failed to fork for init: %m\n"); + else if (ret == 0) + goto drawing; + + initsargv = calloc(sizeof(argv[0]), argc + 1); + + if (!initsargv) { + 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); + + error("failed to exec init: %m\n"); + + return EXIT_FAILURE; + } + +drawing: + printf("drawing\n"); + frames = config.frames; + if (config.frames == 0) + frames = 1; + + while (frames) { + gettimeofday(&start, NULL); + for (spinner_iter = spinner_list; spinner_iter; spinner_iter = spinner_iter->next) { + if (spinner_node->icon_width / spinner_node->icon_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->device_cr, + spinner_iter->drawing_surface, 0, 0); + cairo_paint(spinner_iter->device_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); + + if (config.frames > 0) + frames--; + } + + return 0; +} diff --git a/spinner.conf b/spinner.conf new file mode 100644 index 0000000..c95a850 --- /dev/null +++ b/spinner.conf @@ -0,0 +1,13 @@ + +backdrop="/mnt/data/platsch/splash.png" +#symbol="/mnt/data/platsch/Spider.png" +symbol="/mnt/data/platsch/Spinner.png" + +fps=1 +#frames=0 for infinite +frames=0 +text="hello" +text_x=350 +text_y=400 +text_font="Sans" +text_size=30 diff --git a/spinner_conf.c b/spinner_conf.c new file mode 100644 index 0000000..c50a08c --- /dev/null +++ b/spinner_conf.c @@ -0,0 +1,66 @@ +#include "spinner_conf.h" +#include <errno.h> +#include <string.h> + +int parseConfig(const char *filename, Config *config) +{ + FILE *file; + char line[MAX_LINE_LENGTH*2]; + char key[MAX_LINE_LENGTH]; + char value[MAX_LINE_LENGTH+1]; + char *value_start; + char *value_end; + + 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--; + } + + strncpy(value, value_start, value_end - value_start + 1); + value[value_end - value_start + 1] = '\0'; + value[sizeof(value) - 1] = '\0'; + + if (strcmp(key, "backdrop") == 0) { + strncpy(config->backdrop, value, MAX_LINE_LENGTH); + config->backdrop[sizeof(config->backdrop) - 1] = '\0'; + } else if (strcmp(key, "symbol") == 0) { + strncpy(config->symbol, value, MAX_LINE_LENGTH); + config->symbol[sizeof(config->symbol) - 1] = '\0'; + } else if (strcmp(key, "fps") == 0) { + config->fps = atoi(value); + } else if (strcmp(key, "frames") == 0) { + config->frames = 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); + config->text_font[sizeof(config->text_font) - 1] = '\0'; + } 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..e797f3e --- /dev/null +++ b/spinner_conf.h @@ -0,0 +1,39 @@ +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <ctype.h> + +#ifndef __SPINNER_CONF_H__ +#define __SPINNER_CONF_H__ + + +#define MAX_LINE_LENGTH 128 + +typedef struct { + char backdrop[MAX_LINE_LENGTH]; + char symbol[MAX_LINE_LENGTH]; + char type[MAX_LINE_LENGTH]; + int fps; + int frames; + 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 { \ + .backdrop = "/usr/share/platsch/splash.png", \ + .symbol = "/usr/share/platsch/spinner.png", \ + .type = "Rotation", \ + .fps = 20, \ + .frames = 0, \ + .text_x = 100, \ + .text_y = 100, \ + .text_font = "Sans", \ + .text_size = 30, \ + .text = "Now loading..." \ +} +#endif -- 2.34.1