From 91f0600e6b769f61df330c1e65cce9c5e2c9e4af Mon Sep 17 00:00:00 2001 From: mintsuki Date: Fri, 9 Apr 2021 01:26:39 +0200 Subject: [PATCH] image: Add support for centering images --- CONFIG.md | 5 ++++- stage23/lib/bmp.c | 16 ++++++++++++++-- stage23/lib/image.c | 10 ++++++++++ stage23/lib/image.h | 10 ++++++++++ stage23/menu.c | 16 ++++++++++++++++ test/limine.cfg | 2 ++ 6 files changed, 56 insertions(+), 3 deletions(-) diff --git a/CONFIG.md b/CONFIG.md index fc13600b..0cac8b30 100644 --- a/CONFIG.md +++ b/CONFIG.md @@ -51,7 +51,7 @@ Some keys take *URIs* as values; these are described in the next section. *Globally assignable* keys are: * `TIMEOUT` - Specifies the timeout in seconds before the first *entry* is automatically booted. If set to `no`, disable automatic boot. If set to `0`, boots default entry instantly (see `DEFAULT_ENTRY` key). * `DEFAULT_ENTRY` - 1-based entry index of the entry which will be automatically selected at startup. If unspecified, it is `1`. -* `GRAPHICS` - If set to `yes`, do use graphical VESA framebuffer for the boot menu, else use text mode. Ignored with Limine UEFI. +* `GRAPHICS` - If set to `yes`, use a graphical framebuffer for the boot menu, else use text mode. Ignored with Limine UEFI, forced to `yes`. * `MENU_RESOLUTION` - Specify screen resolution to be used by the Limine menu in the form `x`. This will *only* affect the menu, not any booted OS. If not specified, Limine will pick a resolution automatically. If the resolution is not available, Limine will pick another one automatically. Ignored if `GRAPHICS` is not `yes`. * `MENU_BRANDING` - A string that will be displayed on top of the Limine menu. * `MENU_FONT` - URI path to a font file to be used instead of the default one for the menu. The font file must be a code page 437 character set comprised of 256 consecutive 8x16 glyphs bitmaps (4096 byte font file). Each glyph's bitmap must be expressed left to right (1 byte per row), and top to bottom (16 bytes per whole glyph). @@ -60,6 +60,9 @@ Some keys take *URIs* as values; these are described in the next section. * `THEME_MARGIN` - Set the amount of margin around the terminal. Ignored if `GRAPHICS` is not `yes`. * `THEME_MARGIN_GRADIENT` - Set the thickness in pixel for the gradient around the terminal. Ignored if `GRAPHICS` is not `yes`. * `BACKGROUND_PATH` - URI where to find the background .BMP file. Ignored if `GRAPHICS` is not `yes`. +* `BACKGROUND_STYLE` - The style which will be used to display the background image. Either `tiled` or `centered`. Default is `tiled`. +* `BACKDROP_COLOUR` - When the background style is `centered`, this specifies the colour of the backdrop for parts of the screen not covered by the background image, in RRGGBB format. +* `BACKDROP_COLOR` - Alias of `BACKDROP_COLOUR`. *Locally assignable (non protocol specific)* keys are: * `PROTOCOL` - The boot protocol that will be used to boot the kernel. Valid protocols are: `linux`, `stivale`, `stivale2`, `chainload`. diff --git a/stage23/lib/bmp.c b/stage23/lib/bmp.c index 5c7a1265..266dac13 100644 --- a/stage23/lib/bmp.c +++ b/stage23/lib/bmp.c @@ -38,8 +38,20 @@ static uint32_t get_pixel(struct image *this, int x, int y) { struct bmp_local *local = this->local; struct bmp_header *header = &local->header; - x %= header->bi_width; - y %= header->bi_height; + switch (this->type) { + case IMAGE_TILED: { + x %= header->bi_width; + y %= header->bi_height; + break; + } + case IMAGE_CENTERED: { + x -= this->x_displacement; + y -= this->y_displacement; + if (x < 0 || y < 0 || x >= this->x_size || y >= this->y_size) + return this->back_colour; + break; + } + } size_t pixel_offset = local->pitch * (header->bi_height - y - 1) + x * (header->bi_bpp / 8); diff --git a/stage23/lib/image.c b/stage23/lib/image.c index 40d83c3f..f104fc9f 100644 --- a/stage23/lib/image.c +++ b/stage23/lib/image.c @@ -5,11 +5,21 @@ #include #include +void image_make_centered(struct image *image, int frame_x_size, int frame_y_size, uint32_t back_colour) { + image->type = IMAGE_CENTERED; + + image->x_displacement = frame_x_size / 2 - image->x_size / 2; + image->y_displacement = frame_y_size / 2 - image->y_size / 2; + image->back_colour = back_colour; +} + int open_image(struct image *image, struct file_handle *file) { image->file = file; if (!bmp_open_image(image, file)) return 0; + image->type = IMAGE_TILED; + return -1; } diff --git a/stage23/lib/image.h b/stage23/lib/image.h index 09af1228..7b74acf9 100644 --- a/stage23/lib/image.h +++ b/stage23/lib/image.h @@ -8,10 +8,20 @@ struct image { struct file_handle *file; int x_size; int y_size; + int type; + int x_displacement; + int y_displacement; + uint32_t back_colour; uint32_t (*get_pixel)(struct image *this, int x, int y); void *local; }; +enum { + IMAGE_TILED, + IMAGE_CENTERED +}; + +void image_make_centered(struct image *image, int frame_x_size, int frame_y_size, uint32_t back_colour); int open_image(struct image *image, struct file_handle *file); #endif diff --git a/stage23/menu.c b/stage23/menu.c index e58a0f66..a8d31ec3 100644 --- a/stage23/menu.c +++ b/stage23/menu.c @@ -7,6 +7,7 @@ #include #include #include +#include #include #include #include @@ -431,6 +432,21 @@ char *menu(char **cmdline) { bg = NULL; nobg: + term_vbe(colourscheme, margin, margin_gradient, NULL); + + if (bg != NULL) { + char *background_layout = config_get_value(NULL, 0, "BACKGROUND_STYLE"); + if (background_layout != NULL && strcmp(background_layout, "centered") == 0) { + char *background_colour = config_get_value(NULL, 0, "BACKDROP_COLOUR"); + if (background_colour == NULL) + background_colour = config_get_value(NULL, 0, "BACKDROP_COLOR"); + if (background_colour == NULL) + background_colour = "0"; + uint32_t bg_col = strtoui(background_colour, NULL, 16); + image_make_centered(bg, fbinfo.framebuffer_width, fbinfo.framebuffer_height, bg_col); + } + } + term_vbe(colourscheme, margin, margin_gradient, bg); } diff --git a/test/limine.cfg b/test/limine.cfg index 19ab86d6..e9f09aa7 100644 --- a/test/limine.cfg +++ b/test/limine.cfg @@ -7,6 +7,8 @@ THEME_COLOURS=000000;aa0000;00aaff;aa5500;0000aa;aa00aa;9076de;aaaaaa;60000000;a THEME_MARGIN=64 BACKGROUND_PATH=boot:///boot/bg.bmp +BACKGROUND_STYLE=centered +BACKDROP_COLOUR=008080 :Stivale2 Test