mirror of
https://github.com/Metabolix/HackBGRT.git
synced 2025-12-06 17:15:42 -08:00
Make coordinates relative to center
Relative coordinates make it easier to center the image. The value "auto" is now replaced with simply 0. The value "native" is replaced with "keep" for consistency. The value is clamped, so x=-9999 will align to the left border etc.
This commit is contained in:
14
config.txt
14
config.txt
@@ -8,11 +8,11 @@ boot=MS
|
||||
# Multiple image lines may be present, in which case one will be picked by random.
|
||||
# The image line may contain the following parts:
|
||||
# Any of the following:
|
||||
# - "n=[0-9]+", a weight for this image in the randomization process. Default: n=1.
|
||||
# - "x={auto|native|[0-9]+}", the x coordinate. Default: x=auto.
|
||||
# - "y={auto|native|[0-9]+}", the y coordinate. Default: y=auto.
|
||||
# - "n=(number)", a weight for this image in the randomization process. Default: n=1.
|
||||
# - "x=(number)" or "x=keep", the x offset from the center. Default: x=0.
|
||||
# - "y=(number)" or "y=keep", the y offset from the center. Default: y=0.
|
||||
# One of the following:
|
||||
# - "keep" to keep the firmware logo. Sets also x=native,y=native by default.
|
||||
# - "keep" to keep the firmware logo. Also keeps coordinates by default.
|
||||
# - "remove" to remove the BGRT. Makes x and y meaningless.
|
||||
# - "black" to use only a black image. Makes x and y meaningless.
|
||||
# - "path=..." to read a BMP file.
|
||||
@@ -22,17 +22,17 @@ boot=MS
|
||||
# Examples:
|
||||
# - image=remove
|
||||
# - image=black
|
||||
# - image=x=auto,y=0,path=\EFI\HackBGRT\topimage.bmp
|
||||
# - image= x=0 y=-200 path=\EFI\HackBGRT\topimage.bmp
|
||||
# - image=n=1,path=\EFI\HackBGRT\splash.bmp
|
||||
# - image=n=50,path=\EFI\HackBGRT\probable.bmp
|
||||
# The above examples together would produce
|
||||
# - 1/54 chance for the default OS logo
|
||||
# - 1/54 chance for black screen
|
||||
# - 1/54 chance for topimage.bmp, centered at the top of the screen
|
||||
# - 1/54 chance for topimage.bmp, 200 px above the center of the screen
|
||||
# - 1/54 chance for splash.bmp, automatically positioned
|
||||
# - 50/54 chance for probable.bmp, automatically positioned
|
||||
# Default: just one image.
|
||||
image=path=\EFI\HackBGRT\splash.bmp
|
||||
image= y=-200 path=\EFI\HackBGRT\splash.bmp
|
||||
|
||||
# Preferred resolution. Use 0x0 for maximum and -1x-1 for original.
|
||||
resolution=0x0
|
||||
|
||||
10
src/config.c
10
src/config.c
@@ -86,13 +86,13 @@ static void SetBMPWithRandom(struct HackBGRT_config* config, int weight, enum Ha
|
||||
}
|
||||
|
||||
static int ParseCoordinate(const CHAR16* str, enum HackBGRT_action action) {
|
||||
if (str && L'0' <= str[0] && str[0] <= L'9') {
|
||||
return Atoi(str);
|
||||
if (str && ((L'0' <= str[0] && str[0] <= L'9') || str[0] == L'-')) {
|
||||
return str[0] == L'-' ? -(int)Atoi(str+1) : (int)Atoi(str);
|
||||
}
|
||||
if ((str && StrnCmp(str, L"native", 6) == 0) || action == HackBGRT_KEEP) {
|
||||
return HackBGRT_coord_native;
|
||||
if ((str && StrnCmp(str, L"keep", 4) == 0) || action == HackBGRT_KEEP) {
|
||||
return HackBGRT_coord_keep;
|
||||
}
|
||||
return HackBGRT_coord_auto;
|
||||
return 0;
|
||||
}
|
||||
|
||||
static void ReadConfigImage(struct HackBGRT_config* config, const CHAR16* line) {
|
||||
|
||||
@@ -14,8 +14,7 @@ enum HackBGRT_action {
|
||||
* @see struct HackBGRT_config
|
||||
*/
|
||||
enum HackBGRT_coordinate {
|
||||
HackBGRT_coord_auto = 0x10000001,
|
||||
HackBGRT_coord_native = 0x10000002
|
||||
HackBGRT_coord_keep = -999999
|
||||
};
|
||||
|
||||
/**
|
||||
@@ -30,6 +29,8 @@ struct HackBGRT_config {
|
||||
int image_weight_sum;
|
||||
int resolution_x;
|
||||
int resolution_y;
|
||||
int old_resolution_x;
|
||||
int old_resolution_y;
|
||||
const CHAR16* boot_path;
|
||||
};
|
||||
|
||||
|
||||
66
src/main.c
66
src/main.c
@@ -43,12 +43,14 @@ static EFI_GRAPHICS_OUTPUT_PROTOCOL* GOP(void) {
|
||||
static void SetResolution(int w, int h) {
|
||||
EFI_GRAPHICS_OUTPUT_PROTOCOL* gop = GOP();
|
||||
if (!gop) {
|
||||
config.old_resolution_x = config.resolution_x = 0;
|
||||
config.old_resolution_y = config.resolution_y = 0;
|
||||
Debug(L"GOP not found!\n");
|
||||
return;
|
||||
}
|
||||
UINTN best_i = gop->Mode->Mode;
|
||||
int best_w = gop->Mode->Info->HorizontalResolution;
|
||||
int best_h = gop->Mode->Info->VerticalResolution;
|
||||
int best_w = config.old_resolution_x = gop->Mode->Info->HorizontalResolution;
|
||||
int best_h = config.old_resolution_y = gop->Mode->Info->VerticalResolution;
|
||||
w = (w <= 0 ? w < 0 ? best_w : 0x7fffffff : w);
|
||||
h = (h <= 0 ? h < 0 ? best_h : 0x7fffffff : h);
|
||||
|
||||
@@ -86,29 +88,13 @@ static void SetResolution(int w, int h) {
|
||||
best_i = i;
|
||||
}
|
||||
Debug(L"Found resolution %dx%d.\n", best_w, best_h);
|
||||
config.resolution_x = best_w;
|
||||
config.resolution_y = best_h;
|
||||
if (best_i != gop->Mode->Mode) {
|
||||
gop->SetMode(gop, best_i);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Select the correct coordinate (manual, automatic, native)
|
||||
*
|
||||
* @param value The configured coordinate value; has special values for automatic and native.
|
||||
* @param automatic The automatically calculated alternative.
|
||||
* @param native The original coordinate.
|
||||
* @see enum HackBGRT_coordinate
|
||||
*/
|
||||
static int SelectCoordinate(int value, int automatic, int native) {
|
||||
if (value == HackBGRT_coord_auto) {
|
||||
return automatic;
|
||||
}
|
||||
if (value == HackBGRT_coord_native) {
|
||||
return native;
|
||||
}
|
||||
return value;
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new XSDT with the given number of entries.
|
||||
*
|
||||
@@ -259,14 +245,11 @@ void HackBgrt(EFI_FILE_HANDLE root_dir) {
|
||||
// KEEP/REPLACE: first get the old BGRT entry.
|
||||
ACPI_BGRT* bgrt = HandleAcpiTables(HackBGRT_KEEP, 0);
|
||||
|
||||
// Get the old BMP and position, if possible.
|
||||
BMP* old_bmp = 0;
|
||||
int old_x = 0, old_y = 0;
|
||||
if (bgrt && VerifyAcpiSdtChecksum(bgrt)) {
|
||||
old_bmp = (BMP*) (UINTN) bgrt->image_address;
|
||||
old_x = bgrt->image_offset_x;
|
||||
old_y = bgrt->image_offset_y;
|
||||
}
|
||||
// Get the old BMP and position (relative to screen center), if possible.
|
||||
const int old_valid = bgrt && VerifyAcpiSdtChecksum(bgrt);
|
||||
BMP* old_bmp = old_valid ? (BMP*) (UINTN) bgrt->image_address : 0;
|
||||
const int old_x = old_bmp ? bgrt->image_offset_x + (old_bmp->width - config.old_resolution_x) / 2 : 0;
|
||||
const int old_y = old_bmp ? bgrt->image_offset_y + (old_bmp->height - config.old_resolution_y) / 2 : 0;
|
||||
|
||||
// Missing BGRT?
|
||||
if (!bgrt) {
|
||||
@@ -303,20 +286,21 @@ void HackBgrt(EFI_FILE_HANDLE root_dir) {
|
||||
|
||||
bgrt->image_address = (UINTN) new_bmp;
|
||||
|
||||
// Calculate the automatically centered position for the image.
|
||||
int auto_x = 0, auto_y = 0;
|
||||
if (GOP()) {
|
||||
auto_x = max(0, ((int)GOP()->Mode->Info->HorizontalResolution - (int)new_bmp->width) / 2);
|
||||
auto_y = max(0, ((int)GOP()->Mode->Info->VerticalResolution * 2/3 - (int)new_bmp->height) / 2);
|
||||
} else if (old_bmp) {
|
||||
auto_x = max(0, old_x + ((int)old_bmp->width - (int)new_bmp->width) / 2);
|
||||
auto_y = max(0, old_y + ((int)old_bmp->height - (int)new_bmp->height) / 2);
|
||||
}
|
||||
// New center coordinates.
|
||||
const int new_x = config.image_x == HackBGRT_coord_keep ? old_x : config.image_x;
|
||||
const int new_y = config.image_y == HackBGRT_coord_keep ? old_y : config.image_y;
|
||||
|
||||
// Set the position (manual, automatic, original).
|
||||
bgrt->image_offset_x = SelectCoordinate(config.image_x, auto_x, old_x);
|
||||
bgrt->image_offset_y = SelectCoordinate(config.image_y, auto_y, old_y);
|
||||
Debug(L"HackBGRT: BMP at (%d, %d).\n", (int) bgrt->image_offset_x, (int) bgrt->image_offset_y);
|
||||
// Calculate absolute position.
|
||||
const int max_x = config.resolution_x - new_bmp->width;
|
||||
const int max_y = config.resolution_y - new_bmp->height;
|
||||
bgrt->image_offset_x = max(0, min(max_x, new_x + (config.resolution_x - new_bmp->width) / 2));
|
||||
bgrt->image_offset_y = max(0, min(max_y, new_y + (config.resolution_y - new_bmp->height) / 2));
|
||||
|
||||
Debug(
|
||||
L"HackBGRT: BMP at (%d, %d), center (%d, %d), resolution (%d, %d).\n",
|
||||
(int) bgrt->image_offset_x, (int) bgrt->image_offset_y,
|
||||
new_x, new_y, config.resolution_x, config.resolution_y
|
||||
);
|
||||
|
||||
// Store this BGRT in the ACPI tables.
|
||||
SetAcpiSdtChecksum(bgrt);
|
||||
|
||||
Reference in New Issue
Block a user