Support config ventoy_left/ventoy_top/ventoy_color in theme.txt.

The configuration must be the first line in theme.txt and must be in the following format.
ventoy_left_top_color: "@5%@95%@#0000ff@"

The format is very strict:
1. ventoy_left_top_color must start with no space in front of.
2. left/top/color options must be around with 4 @
This commit is contained in:
longpanda 2022-12-23 15:52:43 +08:00
parent 4707022ef9
commit a377dd6172
5 changed files with 171 additions and 83 deletions

View File

@ -295,6 +295,8 @@ theme_set_string (grub_gfxmenu_view_t view,
if (! view->title_text) if (! view->title_text)
return grub_errno; return grub_errno;
} }
else if (! grub_strcmp ("ventoy_left_top_color", name))
return grub_errno;
else else
{ {
return grub_error (GRUB_ERR_BAD_ARGUMENT, return grub_error (GRUB_ERR_BAD_ARGUMENT,

View File

@ -280,6 +280,121 @@ static int ventoy_hwinfo_init(void)
return 0; return 0;
} }
static global_var_cfg g_global_vars[] =
{
{ "gfxmode", "1024x768", NULL },
{ ventoy_left_key, "5%", NULL },
{ ventoy_top_key, "95%", NULL },
{ ventoy_color_key, "#0000ff", NULL },
{ NULL, NULL, NULL }
};
static const char * ventoy_global_var_read_hook(struct grub_env_var *var, const char *val)
{
int i;
for (i = 0; g_global_vars[i].name; i++)
{
if (grub_strcmp(g_global_vars[i].name, var->name) == 0)
{
return g_global_vars[i].value;
}
}
return val;
}
static char * ventoy_global_var_write_hook(struct grub_env_var *var, const char *val)
{
int i;
for (i = 0; g_global_vars[i].name; i++)
{
if (grub_strcmp(g_global_vars[i].name, var->name) == 0)
{
grub_check_free(g_global_vars[i].value);
g_global_vars[i].value = grub_strdup(val);
break;
}
}
return grub_strdup(val);
}
int ventoy_global_var_init(void)
{
int i;
for (i = 0; g_global_vars[i].name; i++)
{
g_global_vars[i].value = grub_strdup(g_global_vars[i].defval);
ventoy_env_export(g_global_vars[i].name, g_global_vars[i].defval);
grub_register_variable_hook(g_global_vars[i].name, ventoy_global_var_read_hook, ventoy_global_var_write_hook);
}
return 0;
}
static ctrl_var_cfg g_ctrl_vars[] =
{
{ "VTOY_WIN11_BYPASS_CHECK", 0 },
{ "VTOY_LINUX_REMOUNT", 0 },
{ "VTOY_SECONDARY_BOOT_MENU", 1 },
{ NULL, 0 }
};
static const char * ventoy_ctrl_var_read_hook(struct grub_env_var *var, const char *val)
{
int i;
for (i = 0; g_ctrl_vars[i].name; i++)
{
if (grub_strcmp(g_ctrl_vars[i].name, var->name) == 0)
{
return g_ctrl_vars[i].value ? "1" : "0";
}
}
return val;
}
static char * ventoy_ctrl_var_write_hook(struct grub_env_var *var, const char *val)
{
int i;
for (i = 0; g_ctrl_vars[i].name; i++)
{
if (grub_strcmp(g_ctrl_vars[i].name, var->name) == 0)
{
if (val && val[0] == '1' && val[1] == 0)
{
g_ctrl_vars[i].value = 1;
return grub_strdup("1");
}
else
{
g_ctrl_vars[i].value = 0;
return grub_strdup("0");
}
}
}
return grub_strdup(val);
}
int ventoy_ctrl_var_init(void)
{
int i;
for (i = 0; g_ctrl_vars[i].name; i++)
{
ventoy_env_export(g_ctrl_vars[i].name, g_ctrl_vars[i].value ? "1" : "0");
grub_register_variable_hook(g_ctrl_vars[i].name, ventoy_ctrl_var_read_hook, ventoy_ctrl_var_write_hook);
}
return 0;
}
GRUB_MOD_INIT(ventoy) GRUB_MOD_INIT(ventoy)
{ {
ventoy_hwinfo_init(); ventoy_hwinfo_init();

View File

@ -148,8 +148,6 @@ static char g_iso_vd_id_application[130];
static int g_pager_flag = 0; static int g_pager_flag = 0;
static char g_old_pager[32]; static char g_old_pager[32];
static char g_vtoy_gfxmode[64];
const char *g_menu_class[img_type_max] = const char *g_menu_class[img_type_max] =
{ {
"vtoyiso", "vtoywim", "vtoyefi", "vtoyimg", "vtoyvhd", "vtoyvtoy" "vtoyiso", "vtoywim", "vtoyefi", "vtoyimg", "vtoyvhd", "vtoyvtoy"
@ -364,6 +362,42 @@ static int ventoy_enum_video_mode(void)
VENTOY_CMD_RETURN(GRUB_ERR_NONE); VENTOY_CMD_RETURN(GRUB_ERR_NONE);
} }
static int ventoy_pre_parse_data(char *src, int size)
{
char c;
char *pos = NULL;
char buf[256];
if (size < 20 || grub_strncmp(src, "ventoy_left_top_color", 21))
{
return 0;
}
pos = src + 21;
while (*pos && *pos != '\r' && *pos != '\n')
{
pos++;
}
c = *pos;
*pos = 0;
if (grub_strlen(src) > 200)
{
goto end;
}
grub_snprintf(buf, sizeof(buf),
"regexp -s 1:%s -s 2:%s -s 3:%s \"@([^@]*)@([^@]*)@([^@]*)@\" \"%s\"",
ventoy_left_key, ventoy_top_key, ventoy_color_key, src);
grub_script_execute_sourcecode(buf);
end:
*pos = c;
return 0;
}
static grub_file_t ventoy_wrapper_open(grub_file_t rawFile, enum grub_file_type type) static grub_file_t ventoy_wrapper_open(grub_file_t rawFile, enum grub_file_type type)
{ {
int len; int len;
@ -397,6 +431,7 @@ static grub_file_t ventoy_wrapper_open(grub_file_t rawFile, enum grub_file_type
} }
grub_file_read(rawFile, file->data, rawFile->size); grub_file_read(rawFile, file->data, rawFile->size);
ventoy_pre_parse_data((char *)file->data, (int)rawFile->size);
len = ventoy_fill_data(4096, (char *)file->data + rawFile->size); len = ventoy_fill_data(4096, (char *)file->data + rawFile->size);
g_old_file = rawFile; g_old_file = rawFile;
@ -6231,82 +6266,6 @@ static const char * ventoy_menu_lang_read_hook(struct grub_env_var *var, const c
return ventoy_get_vmenu_title(val); return ventoy_get_vmenu_title(val);
} }
static const char * ventoy_gfxmode_read_hook(struct grub_env_var *var, const char *val)
{
(void)var;
(void)val;
return g_vtoy_gfxmode;
}
static char * ventoy_gfxmode_write_hook(struct grub_env_var *var, const char *val)
{
(void)var;
grub_strncpy(g_vtoy_gfxmode, val, sizeof(g_vtoy_gfxmode) - 1);
return grub_strdup(val);
}
static ctrl_var_cfg g_ctrl_vars[] =
{
{ "VTOY_WIN11_BYPASS_CHECK", 0 },
{ "VTOY_LINUX_REMOUNT", 0 },
{ "VTOY_SECONDARY_BOOT_MENU", 1 },
{ NULL, 0 }
};
static const char * ventoy_ctrl_var_read_hook(struct grub_env_var *var, const char *val)
{
int i;
for (i = 0; g_ctrl_vars[i].name; i++)
{
if (grub_strcmp(g_ctrl_vars[i].name, var->name) == 0)
{
return g_ctrl_vars[i].value ? "1" : "0";
}
}
return val;
}
static char * ventoy_ctrl_var_write_hook(struct grub_env_var *var, const char *val)
{
int i;
for (i = 0; g_ctrl_vars[i].name; i++)
{
if (grub_strcmp(g_ctrl_vars[i].name, var->name) == 0)
{
if (val && val[0] == '1' && val[1] == 0)
{
g_ctrl_vars[i].value = 1;
return grub_strdup("1");
}
else
{
g_ctrl_vars[i].value = 0;
return grub_strdup("0");
}
}
}
return grub_strdup(val);
}
static int ventoy_ctrl_var_init(void)
{
int i;
for (i = 0; g_ctrl_vars[i].name; i++)
{
ventoy_env_export(g_ctrl_vars[i].name, g_ctrl_vars[i].value ? "1" : "0");
grub_register_variable_hook(g_ctrl_vars[i].name, ventoy_ctrl_var_read_hook, ventoy_ctrl_var_write_hook);
}
return 0;
}
int ventoy_env_init(void) int ventoy_env_init(void)
{ {
int i; int i;
@ -6314,10 +6273,9 @@ int ventoy_env_init(void)
grub_env_set("vtdebug_flag", ""); grub_env_set("vtdebug_flag", "");
grub_register_variable_hook("gfxmode", ventoy_gfxmode_read_hook, ventoy_gfxmode_write_hook);
grub_register_vtoy_menu_lang_hook(ventoy_menu_lang_read_hook); grub_register_vtoy_menu_lang_hook(ventoy_menu_lang_read_hook);
ventoy_ctrl_var_init(); ventoy_ctrl_var_init();
ventoy_global_var_init();
g_part_list_buf = grub_malloc(VTOY_PART_BUF_LEN); g_part_list_buf = grub_malloc(VTOY_PART_BUF_LEN);
g_tree_script_buf = grub_malloc(VTOY_MAX_SCRIPT_BUF); g_tree_script_buf = grub_malloc(VTOY_MAX_SCRIPT_BUF);

View File

@ -77,6 +77,10 @@
#define VTOY_ARCH_CPIO "ventoy_x86.cpio" #define VTOY_ARCH_CPIO "ventoy_x86.cpio"
#endif #endif
#define ventoy_left_key "VTLE_LFT"
#define ventoy_top_key "VTLE_TOP"
#define ventoy_color_key "VTLE_CLR"
#define ventoy_varg_4(arg) arg[0], arg[1], arg[2], arg[3] #define ventoy_varg_4(arg) arg[0], arg[1], arg[2], arg[3]
#define ventoy_varg_8(arg) arg[0], arg[1], arg[2], arg[3], arg[4], arg[5], arg[6], arg[7] #define ventoy_varg_8(arg) arg[0], arg[1], arg[2], arg[3], arg[4], arg[5], arg[6], arg[7]
@ -1256,6 +1260,13 @@ typedef struct systemd_menu_ctx
int len; int len;
}systemd_menu_ctx; }systemd_menu_ctx;
typedef struct global_var_cfg
{
const char *name;
const char *defval;
char *value;
}global_var_cfg;
typedef struct ctrl_var_cfg typedef struct ctrl_var_cfg
{ {
const char *name; const char *name;
@ -1284,6 +1295,8 @@ int ventoy_plugin_load_menu_lang(int init, const char *lang);
const char *ventoy_get_vmenu_title(const char *vMenu); const char *ventoy_get_vmenu_title(const char *vMenu);
grub_err_t ventoy_cmd_cur_menu_lang(grub_extcmd_context_t ctxt, int argc, char **args); grub_err_t ventoy_cmd_cur_menu_lang(grub_extcmd_context_t ctxt, int argc, char **args);
extern int ventoy_menu_push_key(int code); extern int ventoy_menu_push_key(int code);
int ventoy_ctrl_var_init(void);
int ventoy_global_var_init(void);
#endif /* __VENTOY_DEF_H__ */ #endif /* __VENTOY_DEF_H__ */

View File

@ -421,19 +421,19 @@ static int ventoy_plugin_theme_entry(VTOY_JSON *json, const char *isodisk)
value = vtoy_json_get_string_ex(json->pstChild, "ventoy_left"); value = vtoy_json_get_string_ex(json->pstChild, "ventoy_left");
if (value) if (value)
{ {
ventoy_env_export("VTLE_LFT", value); ventoy_env_export(ventoy_left_key, value);
} }
value = vtoy_json_get_string_ex(json->pstChild, "ventoy_top"); value = vtoy_json_get_string_ex(json->pstChild, "ventoy_top");
if (value) if (value)
{ {
ventoy_env_export("VTLE_TOP", value); ventoy_env_export(ventoy_top_key, value);
} }
value = vtoy_json_get_string_ex(json->pstChild, "ventoy_color"); value = vtoy_json_get_string_ex(json->pstChild, "ventoy_color");
if (value) if (value)
{ {
ventoy_env_export("VTLE_CLR", value); ventoy_env_export(ventoy_color_key, value);
} }
node = vtoy_json_find_item(json->pstChild, JSON_TYPE_ARRAY, "fonts"); node = vtoy_json_find_item(json->pstChild, JSON_TYPE_ARRAY, "fonts");