Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
CTCaer
GitHub Repository: CTCaer/hekate
Path: blob/master/bdk/libs/lvgl/lv_themes/lv_theme.c
1476 views
1
/**
2
* @file lv_theme.c
3
*
4
*/
5
6
/*********************
7
* INCLUDES
8
*********************/
9
#include "lv_theme.h"
10
#include "../lv_core/lv_obj.h"
11
12
/*********************
13
* DEFINES
14
*********************/
15
16
/**********************
17
* TYPEDEFS
18
**********************/
19
20
/**********************
21
* STATIC PROTOTYPES
22
**********************/
23
24
/**********************
25
* STATIC VARIABLES
26
**********************/
27
28
#if LV_THEME_LIVE_UPDATE == 0
29
static lv_theme_t * current_theme;
30
#else
31
/* 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.
32
* On `lv_theme_set_current` the styles of the theme are copied to this array.
33
* The pointers in `current_theme` are initialized to point to the styles in the array.
34
* This way the theme styles will always point to the same memory address even after theme is change.
35
* (The pointers in the theme points to the styles declared by the theme itself) */
36
37
/* Store the styles in this array.
38
* Can't determine the size in compile time because sizeof is not evaluated (should be `sizeof(lv_theme_t) / sizeof(lv_style_t*)`).
39
* Error will be generated in run time if too small.*/
40
static lv_style_t th_styles[120];
41
static bool inited = false;
42
static lv_theme_t current_theme;
43
#endif
44
45
/**********************
46
* MACROS
47
**********************/
48
49
/**********************
50
* GLOBAL FUNCTIONS
51
**********************/
52
53
/**
54
* Set a theme for the system.
55
* From now, all the created objects will use styles from this theme by default
56
* @param th pointer to theme (return value of: 'lv_theme_init_xxx()')
57
*/
58
void lv_theme_set_current(lv_theme_t * th)
59
{
60
#if LV_THEME_LIVE_UPDATE == 0
61
current_theme = th;
62
#else
63
uint32_t style_num = sizeof(lv_theme_t) / sizeof(lv_style_t *); /*Number of styles in a theme*/
64
65
if(!inited) {
66
/*It's not sure `th_styles` is big enough. Check it now!*/
67
if(style_num > sizeof(th_styles) / sizeof(lv_style_t)) {
68
LV_LOG_ERROR("Themes: th_styles array is too small. Increase it's size!");
69
while(1);
70
}
71
72
/*Initialize the style pointers `current_theme` to point to the `th_styles` style array */
73
uint16_t i;
74
lv_style_t ** cur_th_style_p = (lv_style_t **) &current_theme;
75
for(i = 0; i < style_num; i++) {
76
uintptr_t adr = (uintptr_t)&th_styles[i];
77
memcpy(&cur_th_style_p[i], &adr, sizeof(lv_style_t *));
78
}
79
inited = true;
80
}
81
82
83
/*Copy the styles pointed by the new theme to the `th_styles` style array*/
84
uint16_t i;
85
lv_style_t ** th_style = (lv_style_t **) th;
86
for(i = 0; i < style_num; i++) {
87
uintptr_t s = (uintptr_t)th_style[i];
88
if(s) memcpy(&th_styles[i], (void *)s, sizeof(lv_style_t));
89
}
90
91
/*Let the object know their style might change*/
92
lv_obj_report_style_mod(NULL);
93
#endif
94
}
95
96
/**
97
* Get the current system theme.
98
* @return pointer to the current system theme. NULL if not set.
99
*/
100
lv_theme_t * lv_theme_get_current(void)
101
{
102
#if LV_THEME_LIVE_UPDATE == 0
103
return current_theme;
104
#else
105
if(!inited) return NULL;
106
else return &current_theme;
107
#endif
108
}
109
110
/**********************
111
* STATIC FUNCTIONS
112
**********************/
113
114