pbsplash/src/pbsplash.c

248 lines
6.7 KiB
C
Raw Normal View History

#include <stdio.h>
#include <stdlib.h>
2022-02-22 16:10:24 -09:00
#include <signal.h>
#include <unistd.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <tfblib/tfblib.h>
#include <tfblib/tfb_colors.h>
2022-01-22 13:27:56 -09:00
#include <string.h>
#include <math.h>
#define NANOSVG_ALL_COLOR_KEYWORDS // Include full list of color keywords.
#define NANOSVG_IMPLEMENTATION // Expands implementation
#include "nanosvg.h"
#define NANOSVGRAST_IMPLEMENTATION
#include "nanosvgrast.h"
#define MSG_MAX_LEN 4096
2022-01-23 14:54:01 -09:00
#define DEFAULT_FONT_PATH "/usr/share/pbsplash/OpenSans-Regular.svg"
2022-02-22 16:10:24 -09:00
#define LOGO_SIZE_MAX_MM 90
#define FONT_SIZE_MM 5
volatile sig_atomic_t terminate = 0;
bool debug = false;
#define LOG(fmt, ...) do { if (debug) fprintf(stdout, fmt, ##__VA_ARGS__); } while (0)
2022-01-22 13:27:56 -09:00
int usage()
{
fprintf(stderr, "pbsplash: a simple fbsplash tool\n");
fprintf(stderr, "--------------------------------\n");
2022-02-22 16:10:24 -09:00
fprintf(stderr, "pbsplash [-h] [-d] [-f font] [-s splash image] [-m message]\n\n");
fprintf(stderr, " -v enable verbose logging\n");
fprintf(stderr, " -h show this help\n");
2022-02-22 16:10:24 -09:00
fprintf(stderr, " -f path to SVG font file (default: %s)\n", DEFAULT_FONT_PATH);
fprintf(stderr, " -s path to splash image to display\n");
fprintf(stderr, " -m message to show under the splash image\n");
return 1;
}
2022-02-22 16:10:24 -09:00
void term(int signum)
2022-01-22 13:27:56 -09:00
{
2022-02-22 16:10:24 -09:00
printf("Caught\n");
terminate = 1;
2022-01-22 13:27:56 -09:00
}
// Blit an SVG to the framebuffer using tfblib. The image is
// scaled based on the target width and height.
2022-02-22 16:10:24 -09:00
static void draw_svg(NSVGimage *image, int x, int y, int largest_bound)
2022-01-22 13:27:56 -09:00
{
//fprintf(stdout, "size: %f x %f\n", image->width, image->height);
2022-02-22 16:10:24 -09:00
float sz = (float)largest_bound / (image->width > image->height ? image->width : image->height);
int w = image->width * sz + 0.5;
int h = image->height * sz + 0.5;
//fprintf(stdout, "scale: %f\n", sz);
NSVGrasterizer *rast = nsvgCreateRasterizer();
unsigned char *img = malloc(w * h * 4);
nsvgRasterize(rast, image, 0, 0, sz, img, w, h, w * 4);
for (size_t i = 0; i < w; i++)
{
for (size_t j = 0; j < h; j++)
{
unsigned int col = tfb_make_color(img[(j * w + i) * 4 + 0], img[(j * w + i) * 4 + 1], img[(j * w + i) * 4 + 2]);
tfb_draw_pixel(x + i, y + j, col);
}
}
free(img);
}
2022-02-22 16:10:24 -09:00
/**
* Get the dimensions of a string in pixels.
* based on the font size and the font SVG file.
*/
static void getTextDimensions(NSVGimage *font, char *text, float scale, int *width, int *height)
{
int i = 0;
*width = 0;
2022-02-22 16:10:24 -09:00
// The height is simply the height of the font * the scale factor
*height = (font->fontAscent - font->fontDescent) * scale;
2022-02-22 16:10:24 -09:00
if (text == NULL)
return;
NSVGshape **shapes = nsvgGetTextShapes(font, text, strlen(text));
2022-02-22 16:10:24 -09:00
// Iterate over every glyph in the string to get the total width
for (i = 0; i < strlen(text); i++)
{
NSVGshape *shape = shapes[i];
if (shape) {
*width += shapes[i]->horizAdvX * scale + 1;
} else {
*width += font->defaultHorizAdv * scale;
}
}
}
2022-02-22 16:10:24 -09:00
static void draw_text(NSVGimage *font, char *text, int x, int y, int width, int height, float scale, unsigned int tfb_col)
{
2022-02-22 16:10:24 -09:00
LOG("text '%s'\nfontsz=%f, x=%d, y=%d, dimensions: %d x %d\n", text,
scale, x, y, width, height);
NSVGshape **shapes = nsvgGetTextShapes(font, text, strlen(text));
unsigned char *img = malloc(width * height * 4);
NSVGrasterizer *rast = nsvgCreateRasterizer();
nsvgRasterizeText(rast, font, 0, 0, scale, img, width, height, width * 4, text);
2022-01-22 13:27:56 -09:00
for (size_t i = 0; i < width; i++)
2022-01-22 13:27:56 -09:00
{
for (size_t j = 0; j < height; j++)
2022-01-22 13:27:56 -09:00
{
unsigned int col = tfb_make_color(img[(j * width + i) * 4 + 0], img[(j * width + i) * 4 + 1], img[(j * width + i) * 4 + 2]);
2022-02-22 16:10:24 -09:00
if (col != tfb_black)
tfb_draw_pixel(x + i, y + height - j, tfb_col);
2022-01-22 13:27:56 -09:00
}
}
free(img);
free(shapes);
2022-01-22 13:27:56 -09:00
}
int main(int argc, char **argv)
{
int rc = 0;
2022-02-22 16:10:24 -09:00
char *message = NULL;
char *splash_image = NULL;
NSVGimage *image;
NSVGimage *font;
2022-02-22 16:10:24 -09:00
struct sigaction action;
memset(&action, 0, sizeof(action));
action.sa_handler = term;
sigaction(SIGTERM, &action, NULL);
sigaction(SIGINT, &action, NULL);
for (int i = 1; i < argc; i++)
{
2022-01-22 13:27:56 -09:00
if (strcmp(argv[i], "-h") == 0)
{
rc = usage();
return rc;
}
2022-02-22 16:10:24 -09:00
if (strcmp(argv[i], "-v") == 0)
{
debug = true;
continue;
}
if (strcmp(argv[i], "-s") == 0 && i + 1 < argc)
2022-01-22 13:27:56 -09:00
{
splash_image = argv[i + 1];
//printf("splash_image path: %s\n", splash_image);
continue;
}
2022-01-22 13:27:56 -09:00
if (strcmp(argv[i], "-m") == 0)
{
message = argv[i + 1];
continue;
}
}
2022-02-22 16:10:24 -09:00
if ((rc = tfb_acquire_fb(0, "/dev/fb0", "/dev/tty1")) != TFB_SUCCESS)
2022-01-22 13:27:56 -09:00
{
fprintf(stderr, "tfb_acquire_fb() failed with error code: %d\n", rc);
rc = 1;
return rc;
}
2022-01-22 13:27:56 -09:00
int w = (int)tfb_screen_width();
int h = (int)tfb_screen_height();
2022-02-22 16:10:24 -09:00
int w_mm = tfb_screen_width_mm();
int h_mm = tfb_screen_height_mm();
int pixels_per_milli = (float)w / (float)w_mm;
float logo_size_px = (float)(w < h ? w : h) * 0.75f;
if (w_mm > 0 && h_mm > 0) {
if (w_mm < h_mm) {
if (w_mm > (float)LOGO_SIZE_MAX_MM * 1.2f)
logo_size_px = (float)LOGO_SIZE_MAX_MM * pixels_per_milli;
} else {
if (h_mm > (float)LOGO_SIZE_MAX_MM * 1.2f)
logo_size_px = (float)LOGO_SIZE_MAX_MM * pixels_per_milli;
}
}
LOG("%dx%d @ %dx%dmm, logo_size_px=%f\n", w, h, w_mm, h_mm, logo_size_px);
float x = (float)w / 2 - logo_size_px / 2;
float y = (float)h / 2 - logo_size_px / 2;
2022-01-22 13:27:56 -09:00
image = nsvgParseFromFile(splash_image, "px", 96);
if (!image)
{
fprintf(stderr, "failed to load SVG image\n");
rc = 1;
2022-01-23 14:54:01 -09:00
goto release_fb;
}
2022-01-23 14:54:01 -09:00
font = nsvgParseFromFile(DEFAULT_FONT_PATH, "px", 500);
if (!font || !font->shapes)
{
fprintf(stderr, "failed to load SVG font\n");
rc = 1;
goto free_image;
}
tfb_clear_screen(tfb_black);
2022-02-22 16:10:24 -09:00
draw_svg(image, x, y, logo_size_px);
2022-02-22 16:10:24 -09:00
if (message) {
int textWidth, textHeight;
float fontsz = pixels_per_milli / (float)(font->fontAscent - font->fontDescent)
* FONT_SIZE_MM;
2022-02-22 16:10:24 -09:00
getTextDimensions(font, message, fontsz, &textWidth, &textHeight);
2022-02-22 16:10:24 -09:00
int tx = w / 2.f - textWidth / 2.f;
int ty = y + logo_size_px * 0.5f + textHeight * 0.5f;
2022-02-22 16:10:24 -09:00
draw_text(font, message, tx, ty, textWidth, textHeight, fontsz, tfb_gray);
}
2022-01-22 13:27:56 -09:00
tfb_flush_window();
tfb_flush_fb();
2022-02-22 16:10:24 -09:00
while (!terminate)
{
sleep(1);
}
nsvgDelete(font);
free_image:
2022-01-22 13:27:56 -09:00
nsvgDelete(image);
2022-01-23 14:54:01 -09:00
release_fb:
2022-02-22 16:10:24 -09:00
// The TTY might end up in a weird state if this
// is not called!
2022-01-23 14:54:01 -09:00
tfb_release_fb();
return rc;
}