2021-10-03 16:52:24 -08:00
|
|
|
#include <stdio.h>
|
2021-10-07 13:17:14 -08:00
|
|
|
#include <stdlib.h>
|
2022-02-22 16:10:24 -09:00
|
|
|
#include <signal.h>
|
2022-03-10 17:21:50 -09:00
|
|
|
#include <time.h>
|
2021-10-07 13:17:14 -08:00
|
|
|
#include <unistd.h>
|
2022-01-22 19:41:15 -09:00
|
|
|
#include <sys/stat.h>
|
|
|
|
|
#include <fcntl.h>
|
2021-10-03 16:52:24 -08:00
|
|
|
#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"
|
2022-01-22 19:41:15 -09:00
|
|
|
#define NANOSVGRAST_IMPLEMENTATION
|
|
|
|
|
#include "nanosvgrast.h"
|
2021-10-03 16:52:24 -08:00
|
|
|
|
2022-03-10 17:21:50 -09:00
|
|
|
#include "pbsplash.h"
|
|
|
|
|
|
2021-10-07 13:17:14 -08:00
|
|
|
#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
|
2022-02-27 13:50:20 -09:00
|
|
|
#define FONT_SIZE_PT 13
|
|
|
|
|
#define PT_TO_MM 0.38f
|
2022-02-27 15:49:26 -09:00
|
|
|
#define TTY_PATH_LEN 11
|
|
|
|
|
|
2022-03-10 17:21:50 -09:00
|
|
|
#define DEBUGRENDER 0
|
2022-03-09 15:43:35 -09:00
|
|
|
|
2022-02-22 16:10:24 -09:00
|
|
|
volatile sig_atomic_t terminate = 0;
|
|
|
|
|
|
2022-02-27 15:49:26 -09:00
|
|
|
bool debug = true;
|
2022-03-10 17:21:50 -09:00
|
|
|
struct col background_color = {.r = 0, .g = 0, .b = 0, .a = 255};
|
2022-02-22 16:10:24 -09:00
|
|
|
|
|
|
|
|
#define LOG(fmt, ...) do { if (debug) fprintf(stdout, fmt, ##__VA_ARGS__); } while (0)
|
2021-10-07 13:17:14 -08:00
|
|
|
|
2022-01-22 13:27:56 -09:00
|
|
|
int usage()
|
|
|
|
|
{
|
2022-02-27 13:50:20 -09:00
|
|
|
fprintf(stderr, "pbsplash: postmarketOS bootsplash generator\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");
|
2022-02-27 13:50:20 -09:00
|
|
|
fprintf(stderr, " -v|--verbose enable verbose logging\n");
|
|
|
|
|
fprintf(stderr, " -h|--help show this help\n");
|
|
|
|
|
fprintf(stderr, " -f|--font path to SVG font file (default: %s)\n", DEFAULT_FONT_PATH);
|
|
|
|
|
fprintf(stderr, " -s|--splash-image path to splash image to display\n");
|
|
|
|
|
fprintf(stderr, " -m|--message message to show under the splash image\n");
|
|
|
|
|
fprintf(stderr, " -p|--font-size font size in pt (default: %d)\n", FONT_SIZE_PT);
|
|
|
|
|
fprintf(stderr, " -d|--dpi custom DPI (for testing)\n");
|
2021-10-07 13:17:14 -08:00
|
|
|
|
|
|
|
|
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
|
|
|
terminate = 1;
|
2022-01-22 13:27:56 -09:00
|
|
|
}
|
|
|
|
|
|
2022-03-10 17:21:50 -09:00
|
|
|
static void blit_buf(unsigned char* buf, int x, int y, int w, int h, bool vflip, bool redraw)
|
2022-01-22 13:27:56 -09:00
|
|
|
{
|
2022-03-10 17:21:50 -09:00
|
|
|
unsigned int bg_col = tfb_make_color(background_color.r, background_color.g, background_color.b);
|
|
|
|
|
unsigned int col;
|
2022-01-22 19:41:15 -09:00
|
|
|
for (size_t i = 0; i < w; i++)
|
|
|
|
|
{
|
|
|
|
|
for (size_t j = 0; j < h; j++)
|
|
|
|
|
{
|
2022-03-09 15:43:35 -09:00
|
|
|
#if DEBUGRENDER == 1
|
2022-02-27 15:49:26 -09:00
|
|
|
if (i == 0 || i == w - 1 || j == 0 || j == h-1) {
|
|
|
|
|
tfb_draw_pixel(x + i, y + h - j, tfb_red);
|
|
|
|
|
continue;
|
|
|
|
|
}
|
2022-03-09 15:43:35 -09:00
|
|
|
#endif
|
2022-03-10 17:21:50 -09:00
|
|
|
struct col rgba = *(struct col *)(buf + (j * w + i) * 4);
|
|
|
|
|
if (!redraw && rgba.a == 0)
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
if (redraw && rgba.a == 0) {
|
|
|
|
|
col = bg_col;
|
|
|
|
|
} else {
|
|
|
|
|
rgba.r = (rgba.r * rgba.a + background_color.r * (255 - rgba.a)) >> 8;
|
|
|
|
|
rgba.g = (rgba.g * rgba.a + background_color.g * (255 - rgba.a)) >> 8;
|
|
|
|
|
rgba.b = (rgba.b * rgba.a + background_color.b * (255 - rgba.a)) >> 8;
|
|
|
|
|
col = tfb_make_color(rgba.r, rgba.g, rgba.b);
|
|
|
|
|
}
|
|
|
|
|
if (vflip)
|
|
|
|
|
tfb_draw_pixel(x + i, y + h - j, col);
|
|
|
|
|
else
|
|
|
|
|
tfb_draw_pixel(x + i, y + j, col);
|
2022-01-22 19:41:15 -09:00
|
|
|
}
|
|
|
|
|
}
|
2022-03-10 17:21:50 -09:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void draw_svg(NSVGimage *image, int x, int y, int w, int h)
|
|
|
|
|
{
|
|
|
|
|
float sz = (int)((float)w / (float)image->width * 100.f) / 100.f;
|
|
|
|
|
LOG("draw_svg: %dx%d, %dx%d, %f\n", x, y, w, h, sz);
|
|
|
|
|
//sz = 1.5;
|
|
|
|
|
NSVGrasterizer *rast = nsvgCreateRasterizer();
|
|
|
|
|
unsigned char *img = malloc(w * h * 4);
|
|
|
|
|
nsvgRasterize(rast, image, 0, 0, sz, img, w, h, w * 4);
|
|
|
|
|
|
|
|
|
|
blit_buf(img, x, y, w, h, false, false);
|
2022-01-22 19:41:15 -09:00
|
|
|
|
|
|
|
|
free(img);
|
2022-03-10 17:21:50 -09:00
|
|
|
nsvgDeleteRasterizer(rast);
|
2022-01-22 19:41:15 -09:00
|
|
|
}
|
|
|
|
|
|
2022-03-10 17:21:50 -09:00
|
|
|
static void draw_text(NSVGimage *font, char *text, int x, int y, int width, int height, float scale, unsigned int tfb_col)
|
|
|
|
|
{
|
|
|
|
|
LOG("text '%s': fontsz=%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);
|
|
|
|
|
|
|
|
|
|
blit_buf(img, x, y, width, height, true, false);
|
|
|
|
|
|
|
|
|
|
free(img);
|
|
|
|
|
free(shapes);
|
|
|
|
|
nsvgDeleteRasterizer(rast);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
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.
|
|
|
|
|
*/
|
2022-01-23 12:25:19 -09:00
|
|
|
static void getTextDimensions(NSVGimage *font, char *text, float scale, int *width, int *height)
|
2022-01-22 19:41:15 -09:00
|
|
|
{
|
2022-01-23 12:25:19 -09:00
|
|
|
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
|
2022-01-23 12:25:19 -09:00
|
|
|
*height = (font->fontAscent - font->fontDescent) * scale;
|
2022-02-22 16:10:24 -09:00
|
|
|
if (text == NULL)
|
|
|
|
|
return;
|
|
|
|
|
|
2022-01-22 19:41:15 -09:00
|
|
|
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++)
|
2022-01-22 19:41:15 -09:00
|
|
|
{
|
2022-01-23 12:25:19 -09:00
|
|
|
NSVGshape *shape = shapes[i];
|
|
|
|
|
if (shape) {
|
2022-03-09 15:43:35 -09:00
|
|
|
*width += (float)shapes[i]->horizAdvX * scale + 0.5;
|
2022-01-23 12:25:19 -09:00
|
|
|
} else {
|
|
|
|
|
*width += font->defaultHorizAdv * scale;
|
|
|
|
|
}
|
2022-01-22 13:27:56 -09:00
|
|
|
}
|
2022-01-22 19:41:15 -09:00
|
|
|
|
2022-01-23 12:25:19 -09:00
|
|
|
free(shapes);
|
2022-01-22 13:27:56 -09:00
|
|
|
}
|
|
|
|
|
|
2021-10-03 16:52:24 -08:00
|
|
|
int main(int argc, char **argv)
|
|
|
|
|
{
|
2021-10-07 13:17:14 -08:00
|
|
|
int rc = 0;
|
2022-02-22 16:10:24 -09:00
|
|
|
char *message = NULL;
|
|
|
|
|
char *splash_image = NULL;
|
2022-02-27 13:50:20 -09:00
|
|
|
char *font_path = DEFAULT_FONT_PATH;
|
2022-03-10 17:21:50 -09:00
|
|
|
char active_tty[TTY_PATH_LEN + 1];
|
2022-01-22 19:41:15 -09:00
|
|
|
NSVGimage *image;
|
|
|
|
|
NSVGimage *font;
|
2022-02-22 16:10:24 -09:00
|
|
|
struct sigaction action;
|
2022-02-27 13:50:20 -09:00
|
|
|
float font_size = FONT_SIZE_PT;
|
2022-03-09 15:43:35 -09:00
|
|
|
int optflag;
|
2022-02-27 13:50:20 -09:00
|
|
|
long dpi = 0;
|
2022-02-22 16:10:24 -09:00
|
|
|
|
2022-02-27 15:49:26 -09:00
|
|
|
memset(active_tty, '\0', TTY_PATH_LEN);
|
|
|
|
|
strcat(active_tty, "/dev/");
|
|
|
|
|
|
|
|
|
|
|
2022-02-22 16:10:24 -09:00
|
|
|
memset(&action, 0, sizeof(action));
|
|
|
|
|
action.sa_handler = term;
|
|
|
|
|
sigaction(SIGTERM, &action, NULL);
|
|
|
|
|
sigaction(SIGINT, &action, NULL);
|
2021-10-07 13:17:14 -08:00
|
|
|
|
2022-03-09 15:43:35 -09:00
|
|
|
while ((optflag = getopt(argc, argv, "hvf:s:m:p:d:")) != -1)
|
2021-10-07 13:17:14 -08:00
|
|
|
{
|
2022-02-27 13:50:20 -09:00
|
|
|
char *end = NULL;
|
2022-03-09 15:43:35 -09:00
|
|
|
switch (optflag)
|
2022-02-22 16:10:24 -09:00
|
|
|
{
|
2022-02-27 13:50:20 -09:00
|
|
|
case 'h':
|
|
|
|
|
return usage();
|
|
|
|
|
case 'v':
|
2022-02-22 16:10:24 -09:00
|
|
|
debug = true;
|
2022-02-27 13:50:20 -09:00
|
|
|
break;
|
|
|
|
|
case 'f':
|
|
|
|
|
font_path = optarg;
|
|
|
|
|
break;
|
|
|
|
|
case 's':
|
|
|
|
|
splash_image = optarg;
|
|
|
|
|
break;
|
|
|
|
|
case 'm':
|
|
|
|
|
message = optarg;
|
|
|
|
|
break;
|
|
|
|
|
case 'p':
|
|
|
|
|
font_size = strtof(optarg, &end);
|
|
|
|
|
if (end == optarg)
|
|
|
|
|
{
|
|
|
|
|
fprintf(stderr, "Invalid font size: %s\n", optarg);
|
|
|
|
|
return usage();
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
case 'd':
|
2022-02-27 15:49:26 -09:00
|
|
|
if (!optarg)
|
|
|
|
|
{
|
|
|
|
|
fprintf(stderr, "--dpi requires an argument\n");
|
|
|
|
|
return usage();
|
|
|
|
|
}
|
|
|
|
|
dpi = strtol(optarg, &end, 10);
|
2022-02-27 13:50:20 -09:00
|
|
|
if (end == optarg)
|
|
|
|
|
{
|
|
|
|
|
fprintf(stderr, "Invalid font size: %s\n", optarg);
|
|
|
|
|
return usage();
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
return usage();
|
2021-10-07 13:17:14 -08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-02-27 15:49:26 -09:00
|
|
|
{
|
2022-03-10 17:21:50 -09:00
|
|
|
FILE *fp = fopen("/sys/devices/virtual/tty/tty0/active", "r");
|
|
|
|
|
int len = strlen(active_tty);
|
|
|
|
|
char *ptr = active_tty + len;
|
|
|
|
|
if(fp != NULL)
|
|
|
|
|
{
|
|
|
|
|
fgets(ptr, TTY_PATH_LEN - len, fp);
|
|
|
|
|
*(ptr + strlen(ptr) - 1) = '\0';
|
|
|
|
|
fclose(fp);
|
|
|
|
|
}
|
2022-02-27 15:49:26 -09:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
LOG("active tty: '%s'\n", active_tty);
|
|
|
|
|
|
2022-03-10 17:21:50 -09:00
|
|
|
if ((rc = tfb_acquire_fb(/*TFB_FL_USE_DOUBLE_BUFFER*/ 0, "/dev/fb0", active_tty)) != TFB_SUCCESS)
|
2022-01-22 13:27:56 -09:00
|
|
|
{
|
2021-10-03 16:52:24 -08:00
|
|
|
fprintf(stderr, "tfb_acquire_fb() failed with error code: %d\n", rc);
|
2021-10-07 13:17:14 -08:00
|
|
|
rc = 1;
|
2022-01-23 12:25:19 -09:00
|
|
|
return rc;
|
2021-10-03 16:52:24 -08:00
|
|
|
}
|
|
|
|
|
|
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();
|
2022-02-27 13:50:20 -09:00
|
|
|
// If DPI is specified on cmdline then calculate display size from it
|
|
|
|
|
// otherwise calculate the dpi based on the display size.
|
|
|
|
|
if (dpi > 0)
|
|
|
|
|
{
|
|
|
|
|
w_mm = w / (float)dpi * 25.4;
|
|
|
|
|
h_mm = h / (float)dpi * 25.4;
|
|
|
|
|
} else {
|
|
|
|
|
dpi = (float)w / (float)w_mm * 25.4;
|
|
|
|
|
}
|
2022-02-22 16:10:24 -09:00
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-03-09 15:43:35 -09:00
|
|
|
LOG("%dx%d @ %dx%dmm, dpi=%ld, logo_size_px=%f\n", w, h, w_mm, h_mm, dpi, logo_size_px);
|
2022-01-22 13:27:56 -09:00
|
|
|
|
2022-03-10 17:21:50 -09:00
|
|
|
image = nsvgParseFromFile(splash_image, "", logo_size_px);
|
2022-01-22 19:41:15 -09:00
|
|
|
if (!image)
|
|
|
|
|
{
|
|
|
|
|
fprintf(stderr, "failed to load SVG image\n");
|
|
|
|
|
rc = 1;
|
2022-03-09 15:43:35 -09:00
|
|
|
goto out;
|
2022-01-22 19:41:15 -09:00
|
|
|
}
|
2021-10-03 16:52:24 -08:00
|
|
|
|
2022-03-09 15:43:35 -09:00
|
|
|
float sz = (float)logo_size_px / (image->width > image->height ? image->width : image->height);
|
|
|
|
|
int image_w = image->width * sz + 0.5;
|
|
|
|
|
int image_h = image->height * sz + 0.5;
|
|
|
|
|
float x = (float)w / 2;
|
|
|
|
|
float y = (float)h / 2;
|
|
|
|
|
// Center the image
|
|
|
|
|
x -= image_w * 0.5f;
|
|
|
|
|
y -= image_h * 0.5f;
|
|
|
|
|
|
2022-03-10 17:21:50 -09:00
|
|
|
tfb_clear_screen(tfb_make_color(background_color.r, background_color.g, background_color.b));
|
2021-10-03 16:52:24 -08:00
|
|
|
|
2022-03-09 15:43:35 -09:00
|
|
|
draw_svg(image, x, y, image_w, image_h);
|
2022-01-23 12:25:19 -09:00
|
|
|
|
2022-02-22 16:10:24 -09:00
|
|
|
if (message) {
|
|
|
|
|
int textWidth, textHeight;
|
2022-02-27 15:49:26 -09:00
|
|
|
|
2022-03-09 15:43:35 -09:00
|
|
|
font = nsvgParseFromFile(font_path, "px", 512);
|
2022-02-27 15:49:26 -09:00
|
|
|
if (!font || !font->shapes)
|
|
|
|
|
{
|
|
|
|
|
fprintf(stderr, "failed to load SVG font\n");
|
|
|
|
|
rc = 1;
|
2022-03-09 15:43:35 -09:00
|
|
|
goto out;
|
2022-02-27 15:49:26 -09:00
|
|
|
}
|
2022-01-23 12:25:19 -09:00
|
|
|
|
2022-03-09 15:43:35 -09:00
|
|
|
float fontsz = ((float)font_size * PT_TO_MM) / (font->fontAscent - font->fontDescent) * pixels_per_milli;
|
|
|
|
|
|
|
|
|
|
//fontsz = 0.1;
|
|
|
|
|
|
|
|
|
|
getTextDimensions(font, message, fontsz, &textWidth, &textHeight);
|
2022-01-23 12:25:19 -09:00
|
|
|
|
2022-02-22 16:10:24 -09:00
|
|
|
int tx = w / 2.f - textWidth / 2.f;
|
2022-03-09 15:43:35 -09:00
|
|
|
int ty = y + image_h + textHeight * 0.5f;
|
2022-01-23 12:25:19 -09:00
|
|
|
|
2022-03-09 15:43:35 -09:00
|
|
|
draw_text(font, message, tx, ty, textWidth, textHeight, fontsz, tfb_gray);
|
2022-02-22 16:10:24 -09:00
|
|
|
}
|
2022-01-22 13:27:56 -09:00
|
|
|
|
2021-10-07 13:17:14 -08:00
|
|
|
tfb_flush_window();
|
|
|
|
|
tfb_flush_fb();
|
2022-03-10 17:21:50 -09:00
|
|
|
#define ANIM_HEIGHT 600
|
|
|
|
|
//unsigned char* animation_buf = malloc(w * ANIM_HEIGHT * 4);
|
|
|
|
|
int frame = 0;
|
2022-02-22 16:10:24 -09:00
|
|
|
while (!terminate)
|
|
|
|
|
{
|
2022-03-10 17:21:50 -09:00
|
|
|
//printf("FRAME: %d\n", frame);
|
|
|
|
|
animate_frame(frame++, w, y + image_h + 500);
|
|
|
|
|
//blit_buf(animation_buf, 0, y + image_h + 200, w, ANIM_HEIGHT, false, true);
|
|
|
|
|
//memset(animation_buf, 0, w * ANIM_HEIGHT * 4);
|
|
|
|
|
//tfb_flush_window();
|
|
|
|
|
tfb_flush_fb();
|
|
|
|
|
//usleep(1666);
|
2022-02-22 16:10:24 -09:00
|
|
|
}
|
2022-03-09 15:43:35 -09:00
|
|
|
|
2022-03-10 17:21:50 -09:00
|
|
|
//free(animation_buf);
|
|
|
|
|
|
2022-03-09 15:43:35 -09:00
|
|
|
out:
|
|
|
|
|
nsvgDelete(font);
|
2022-01-22 13:27:56 -09:00
|
|
|
nsvgDelete(image);
|
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();
|
2021-10-07 13:17:14 -08:00
|
|
|
return rc;
|
2021-10-03 16:52:24 -08:00
|
|
|
}
|