1 /**
2 * @file lv_obj_id.c
3 *
4 */
5
6 /*********************
7 * INCLUDES
8 *********************/
9 #include "lv_obj_class_private.h"
10 #include "lv_obj_private.h"
11 #include "lv_global.h"
12 #include "../osal/lv_os.h"
13 #include "../stdlib/lv_sprintf.h"
14
15 /*********************
16 * DEFINES
17 *********************/
18
19 /**********************
20 * TYPEDEFS
21 **********************/
22
23 /**********************
24 * STATIC PROTOTYPES
25 **********************/
26
27 typedef struct _class_info_t {
28 const lv_obj_class_t * class_p;
29 uint32_t obj_count;
30 } class_info_t;
31
32 /**********************
33 * STATIC VARIABLES
34 **********************/
35
36 /**********************
37 * MACROS
38 **********************/
39
40 /**********************
41 * GLOBAL FUNCTIONS
42 **********************/
43
44 #if LV_USE_OBJ_ID && LV_USE_OBJ_ID_BUILTIN
45
lv_obj_assign_id(const lv_obj_class_t * class_p,lv_obj_t * obj)46 void lv_obj_assign_id(const lv_obj_class_t * class_p, lv_obj_t * obj)
47 {
48 LV_ASSERT(obj && class_p);
49
50 uint32_t i;
51 uint32_t id = 0;
52 lv_global_t * global = LV_GLOBAL_DEFAULT();
53 class_info_t * info = NULL;
54
55 if(obj == NULL || class_p == NULL) return;
56 if(global == NULL) return;
57
58 obj->id = NULL;
59
60 for(i = 0; i < global->objid_count; i ++) {
61 info = ((class_info_t *)global->objid_array) + i;
62 if(class_p == info->class_p) break;
63 }
64
65 /*Resize array*/
66 if(i == global->objid_count) {
67 void * array = lv_realloc(global->objid_array, sizeof(class_info_t) * (global->objid_count + 1));
68 LV_ASSERT_MALLOC(array);
69 if(array == NULL) return;
70 global->objid_array = array;
71 global->objid_count ++;
72 info = ((class_info_t *)global->objid_array) + i;
73 info->obj_count = 0;
74 info->class_p = class_p;
75 }
76
77 id = ++info->obj_count;
78
79 obj->id = (void *)(lv_uintptr_t)id;
80 }
81
lv_obj_set_id(lv_obj_t * obj,void * id)82 void lv_obj_set_id(lv_obj_t * obj, void * id)
83 {
84 LV_ASSERT_NULL(obj);
85 if(obj->id) lv_obj_free_id(obj);
86 obj->id = id;
87 }
88
lv_obj_free_id(lv_obj_t * obj)89 void lv_obj_free_id(lv_obj_t * obj)
90 {
91 LV_UNUSED(obj);
92 obj->id = NULL;
93 }
94
lv_obj_stringify_id(lv_obj_t * obj,char * buf,uint32_t len)95 const char * lv_obj_stringify_id(lv_obj_t * obj, char * buf, uint32_t len)
96 {
97 const char * name;
98 if(obj == NULL || obj->class_p == NULL) return NULL;
99 if(buf == NULL) return NULL;
100
101 name = obj->class_p->name;
102 if(name == NULL) name = "nameless";
103
104 lv_snprintf(buf, len, "%s%" LV_PRIu32 "", name, (uint32_t)(lv_uintptr_t)obj->id);
105 return buf;
106 }
107
lv_objid_builtin_destroy(void)108 void lv_objid_builtin_destroy(void)
109 {
110 lv_global_t * global = LV_GLOBAL_DEFAULT();
111 if(global == NULL) return;
112
113 lv_free(global->objid_array);
114 global->objid_count = 0;
115 }
116
lv_obj_id_compare(const void * id1,const void * id2)117 int lv_obj_id_compare(const void * id1, const void * id2)
118 {
119 return id1 == id2 ? 0 : 1;
120 }
121
122 #endif /*LV_USE_OBJ_ID_BUILTIN*/
123