diff --git a/src/core/lv_global.h b/src/core/lv_global.h index 15ccf42b84..6043dac0f0 100644 --- a/src/core/lv_global.h +++ b/src/core/lv_global.h @@ -259,6 +259,10 @@ typedef struct _lv_global_t { lv_evdev_discovery_t * evdev_discovery; #endif +#if LV_USE_XML + const char * xml_path_prefix; +#endif + void * user_data; } lv_global_t; diff --git a/src/lv_init.c b/src/lv_init.c index 8f9e5e5b2e..141c402f4a 100644 --- a/src/lv_init.c +++ b/src/lv_init.c @@ -511,8 +511,8 @@ void lv_deinit(void) lv_objid_builtin_destroy(); #endif -#if LV_USE_XML && LV_USE_TEST - lv_xml_test_unregister(); +#if LV_USE_XML + lv_xml_deinit(); #endif #if LV_USE_TRANSLATION diff --git a/src/others/xml/lv_xml.c b/src/others/xml/lv_xml.c index fe8ba8ba9e..c1ef11fca6 100644 --- a/src/others/xml/lv_xml.c +++ b/src/others/xml/lv_xml.c @@ -42,10 +42,12 @@ #include "parsers/lv_xml_calendar_parser.h" #include "../../libs/expat/expat.h" #include "../../draw/lv_draw_image.h" +#include "../../core/lv_global.h" /********************* * DEFINES *********************/ +#define xml_path_prefix LV_GLOBAL_DEFAULT()->xml_path_prefix /********************** * TYPEDEFS @@ -71,6 +73,8 @@ static void view_end_element_handler(void * user_data, const char * name); void lv_xml_init(void) { + xml_path_prefix = lv_strdup(""); + lv_xml_component_init(); lv_xml_register_font(NULL, "lv_font_default", lv_font_get_default()); @@ -142,6 +146,15 @@ void lv_xml_init(void) lv_xml_widget_register("lv_obj-bind_state_if_le", lv_obj_xml_bind_state_create, lv_obj_xml_bind_state_apply); } +void lv_xml_deinit(void) +{ +#if LV_USE_TEST + lv_xml_test_unregister(); +#endif + + lv_free((void *)xml_path_prefix); +} + void * lv_xml_create_in_scope(lv_obj_t * parent, lv_xml_component_scope_t * parent_scope, lv_xml_component_scope_t * scope, const char ** attrs) @@ -264,6 +277,15 @@ void * lv_xml_create(lv_obj_t * parent, const char * name, const char ** attrs) return NULL; } + +void lv_xml_set_default_asset_path(const char * path_prefix) +{ + lv_free((void *)xml_path_prefix); + if(path_prefix == NULL) path_prefix = ""; + xml_path_prefix = lv_strdup(path_prefix); +} + + lv_result_t lv_xml_register_font(lv_xml_component_scope_t * scope, const char * name, const lv_font_t * font) { @@ -433,7 +455,9 @@ lv_result_t lv_xml_register_image(lv_xml_component_scope_t * scope, const char * lv_memzero(img, sizeof(*img)); img->name = lv_strdup(name); if(lv_image_src_get_type(src) == LV_IMAGE_SRC_FILE) { - img->src = lv_strdup(src); + char buf[LV_XML_MAX_PATH_LENGTH]; + lv_snprintf(buf, sizeof(buf), "%s%s", xml_path_prefix, src); + img->src = lv_strdup(buf); } else { img->src = src; diff --git a/src/others/xml/lv_xml.h b/src/others/xml/lv_xml.h index 794b43d306..748c449b85 100644 --- a/src/others/xml/lv_xml.h +++ b/src/others/xml/lv_xml.h @@ -26,6 +26,8 @@ extern "C" { * DEFINES *********************/ +#define LV_XML_MAX_PATH_LENGTH 256 + /********************** * TYPEDEFS **********************/ @@ -36,12 +38,25 @@ extern "C" { void lv_xml_init(void); +void lv_xml_deinit(void); + void * lv_xml_create(lv_obj_t * parent, const char * name, const char ** attrs); void * lv_xml_create_in_scope(lv_obj_t * parent, lv_xml_component_scope_t * parent_ctx, lv_xml_component_scope_t * scope, const char ** attrs); +/** + * Set a path to prefix the image and font file source paths. + * + * In globals.xml usually the source path is like "images/logo.png". + * But on the actual device it can be located at e.g. "A:ui/assets/images/logo.png". + * By setting "A:ui/assets/" the path set in the XML files will be prefixed accordingly. + * + * @param path_prefix the path to be used as prefix + */ +void lv_xml_set_default_asset_path(const char * path_prefix); + lv_result_t lv_xml_register_font(lv_xml_component_scope_t * scope, const char * name, const lv_font_t * font); const lv_font_t * lv_xml_get_font(lv_xml_component_scope_t * scope, const char * name); diff --git a/src/others/xml/lv_xml_component.c b/src/others/xml/lv_xml_component.c index 77f6b86d0b..4422e3a6f7 100644 --- a/src/others/xml/lv_xml_component.c +++ b/src/others/xml/lv_xml_component.c @@ -19,11 +19,13 @@ #include "parsers/lv_xml_obj_parser.h" #include "../../libs/expat/expat.h" #include "../../misc/lv_fs.h" +#include "../../core/lv_global.h" #include /********************* * DEFINES *********************/ +#define xml_path_prefix LV_GLOBAL_DEFAULT()->xml_path_prefix /********************** * TYPEDEFS @@ -63,7 +65,6 @@ void lv_xml_component_init(void) lv_xml_component_scope_init(global_scope); global_scope->name = lv_strdup("globals"); - } void lv_xml_component_scope_init(lv_xml_component_scope_t * scope) @@ -350,6 +351,9 @@ static void process_font_element(lv_xml_parser_state_t * state, const char * typ return; } + char src_path_full[LV_XML_MAX_PATH_LENGTH]; + lv_snprintf(src_path_full, sizeof(src_path_full), "%s%s", xml_path_prefix, src_path); + lv_xml_font_t * f; LV_LL_READ(&state->scope.font_ll, f) { if(lv_streq(f->name, name)) { @@ -366,7 +370,7 @@ static void process_font_element(lv_xml_parser_state_t * state, const char * typ return; } #if LV_TINY_TTF_FILE_SUPPORT - lv_font_t * font = lv_tiny_ttf_create_file(src_path, lv_xml_atoi(size)); + lv_font_t * font = lv_tiny_ttf_create_file(src_path_full, lv_xml_atoi(size)); if(font == NULL) { LV_LOG_WARN("Couldn't load `%s` tiny_ttf font", name); return; @@ -393,7 +397,7 @@ static void process_font_element(lv_xml_parser_state_t * state, const char * typ #endif } else if(lv_streq(type, "bin")) { - lv_font_t * font = lv_binfont_create(src_path); + lv_font_t * font = lv_binfont_create(src_path_full); if(font == NULL) { LV_LOG_WARN("Couldn't load `%s` bin font", name); return;