/**1* @file lv_theme.c2*3*/45/*********************6* INCLUDES7*********************/8#include "lv_theme.h"9#include "../lv_core/lv_obj.h"1011/*********************12* DEFINES13*********************/1415/**********************16* TYPEDEFS17**********************/1819/**********************20* STATIC PROTOTYPES21**********************/2223/**********************24* STATIC VARIABLES25**********************/2627#if LV_THEME_LIVE_UPDATE == 028static lv_theme_t * current_theme;29#else30/* If live update is used then a big `lv_style_t` array is used to store the real styles of the theme not only pointers.31* On `lv_theme_set_current` the styles of the theme are copied to this array.32* The pointers in `current_theme` are initialized to point to the styles in the array.33* This way the theme styles will always point to the same memory address even after theme is change.34* (The pointers in the theme points to the styles declared by the theme itself) */3536/* Store the styles in this array.37* Can't determine the size in compile time because sizeof is not evaluated (should be `sizeof(lv_theme_t) / sizeof(lv_style_t*)`).38* Error will be generated in run time if too small.*/39static lv_style_t th_styles[120];40static bool inited = false;41static lv_theme_t current_theme;42#endif4344/**********************45* MACROS46**********************/4748/**********************49* GLOBAL FUNCTIONS50**********************/5152/**53* Set a theme for the system.54* From now, all the created objects will use styles from this theme by default55* @param th pointer to theme (return value of: 'lv_theme_init_xxx()')56*/57void lv_theme_set_current(lv_theme_t * th)58{59#if LV_THEME_LIVE_UPDATE == 060current_theme = th;61#else62uint32_t style_num = sizeof(lv_theme_t) / sizeof(lv_style_t *); /*Number of styles in a theme*/6364if(!inited) {65/*It's not sure `th_styles` is big enough. Check it now!*/66if(style_num > sizeof(th_styles) / sizeof(lv_style_t)) {67LV_LOG_ERROR("Themes: th_styles array is too small. Increase it's size!");68while(1);69}7071/*Initialize the style pointers `current_theme` to point to the `th_styles` style array */72uint16_t i;73lv_style_t ** cur_th_style_p = (lv_style_t **) ¤t_theme;74for(i = 0; i < style_num; i++) {75uintptr_t adr = (uintptr_t)&th_styles[i];76memcpy(&cur_th_style_p[i], &adr, sizeof(lv_style_t *));77}78inited = true;79}808182/*Copy the styles pointed by the new theme to the `th_styles` style array*/83uint16_t i;84lv_style_t ** th_style = (lv_style_t **) th;85for(i = 0; i < style_num; i++) {86uintptr_t s = (uintptr_t)th_style[i];87if(s) memcpy(&th_styles[i], (void *)s, sizeof(lv_style_t));88}8990/*Let the object know their style might change*/91lv_obj_report_style_mod(NULL);92#endif93}9495/**96* Get the current system theme.97* @return pointer to the current system theme. NULL if not set.98*/99lv_theme_t * lv_theme_get_current(void)100{101#if LV_THEME_LIVE_UPDATE == 0102return current_theme;103#else104if(!inited) return NULL;105else return ¤t_theme;106#endif107}108109/**********************110* STATIC FUNCTIONS111**********************/112113114