support load custom keys from assets

This commit is contained in:
lixianjing 2022-10-17 17:55:18 +08:00
parent b62a5c5cfa
commit 162c2e8b9b
7 changed files with 109 additions and 9 deletions

View File

@ -0,0 +1,6 @@
{
"CUSTOM1" : {
"type" : 0,
"value" : 13
}
}

View File

@ -1,4 +1,8 @@
# 最新动态
2022/10/17
* 增加获取自定义键值表函数接口(感谢兆坤提供补丁)
2022/10/15
* 增加str\_from\_xxx函数。

View File

@ -57,6 +57,7 @@ END_C_DECLS
#define APP_RES_ROOT NULL
#endif /*APP_RES_ROOT*/
#include "base/custom_keys.inc"
#include "base/asset_loader_zip.h"
#ifdef USE_GUI_MAIN
@ -200,8 +201,6 @@ int main(int argc, char* argv[]) {
}
#endif /*WITH_LCD_PORTRAIT*/
#ifdef APP_LCD_ORIENTATION
if (system_info()->app_type != APP_SIMULATOR) {
#if defined(APP_ENABLE_FAST_LCD_PORTRAIT)
@ -236,9 +235,12 @@ int main(int argc, char* argv[]) {
#if defined(APP_DEFAULT_LANGUAGE) && defined(APP_DEFAULT_COUNTRY)
locale_info_change(locale_info(), APP_DEFAULT_LANGUAGE, APP_DEFAULT_COUNTRY);
#endif /*APP_DEFAULT_LANGUAGE and APP_DEFAULT_LANGUAGE*/
custom_keys_init();
application_init();
tk_run();
application_exit();
custom_keys_deinit();
GLOBAL_EXIT();
tk_exit();

79
src/base/custom_keys.inc Normal file
View File

@ -0,0 +1,79 @@
#include "tkc.h"
#include "enums.h"
#include "conf_io/conf_json.h"
#if defined(WITH_SDL) || defined(LINUX)
#ifndef CUSTOM_KEYS_FILEPATH
#define CUSTOM_KEYS_FILEPATH "asset://custom_keys.json"
#endif/*CUSTOM_KEYS_FILEPATH*/
#endif /*WITH_SDL*/
#ifdef CUSTOM_KEYS_FILEPATH
static uint32_t s_custom_keys_nr = 0;
static key_type_value_t* s_custom_keys = NULL;
static key_type_value_t* keys_type_custom_keys_load(uint32_t* nr) {
key_type_value_t* ret = NULL;
tk_object_t* conf = NULL;
return_value_if_fail(nr != NULL, NULL);
conf = conf_json_load(CUSTOM_KEYS_FILEPATH, FALSE);
return_value_if_fail(conf != NULL, NULL);
*nr = tk_object_get_prop_uint32(conf, "#size", 0);
if (*nr > 0) {
uint32_t i = 0;
char key[TK_NAME_LEN + 1] = {0};
ret = TKMEM_ZALLOCN(key_type_value_t, *nr);
for (i = 0; i < *nr; i++) {
tk_snprintf(key, sizeof(key), "[%d].#name", i);
ret[i].name = tk_strdup(tk_object_get_prop_str(conf, key));
tk_snprintf(key, sizeof(key), "[%d].type", i);
ret[i].type = tk_object_get_prop_uint32(conf, key, 0);
tk_snprintf(key, sizeof(key), "[%d].value", i);
ret[i].value = tk_object_get_prop_uint32(conf, key, 0);
}
}
TK_OBJECT_UNREF(conf);
return ret;
}
static ret_t keys_type_custom_keys_unload(key_type_value_t* table, uint32_t nr) {
return_value_if_fail(table != NULL || nr == 0, RET_BAD_PARAMS);
if (table != NULL) {
uint32_t i = 0;
for (i = 0; i < nr; i++) {
TKMEM_FREE(table[i].name);
}
TKMEM_FREE(table);
}
return RET_OK;
}
static ret_t custom_keys_init(void) {
s_custom_keys = keys_type_custom_keys_load(&s_custom_keys_nr);
keys_type_set_custom_keys(s_custom_keys, s_custom_keys_nr);
return RET_OK;
}
static ret_t custom_keys_deinit(void) {
keys_type_set_custom_keys(NULL, 0);
keys_type_custom_keys_unload(s_custom_keys, s_custom_keys_nr);
return RET_OK;
}
#else
#define custom_keys_init()
#define custom_keys_deinit()
#endif /*CUSTOM_KEYS_FILEPATH*/

View File

@ -26,6 +26,7 @@
#include "base/input_method.h"
#include "base/window_animator.h"
#include "base/assets_manager.h"
#include "conf_io/conf_json.h"
static const key_type_value_t window_closable_name_value[] = {
{"yes", 0, WINDOW_CLOSABLE_YES},
@ -404,15 +405,14 @@ const key_type_value_t* keys_type_find(const char* name) {
return_value_if_fail(name != NULL, NULL);
memset(fixed_name, 0x00, sizeof(fixed_name));
tk_normalize_key_name(name, fixed_name);
if (s_custom_keys_type_name_value != NULL) {
ret = find_item(s_custom_keys_type_name_value, s_custom_keys_type_name_value_nr,
tk_normalize_key_name(name, fixed_name));
ret = find_item(s_custom_keys_type_name_value, s_custom_keys_type_name_value_nr, fixed_name);
}
if (ret == NULL) {
ret = find_item(keys_type_name_value, ARRAY_SIZE(keys_type_name_value),
tk_normalize_key_name(name, fixed_name));
ret = find_item(keys_type_name_value, ARRAY_SIZE(keys_type_name_value), fixed_name);
}
return ret;
@ -436,7 +436,7 @@ const key_type_value_t* keys_type_find_by_value(uint32_t value) {
ret_t keys_type_set_custom_keys(const key_type_value_t* table, uint32_t nr) {
s_custom_keys_type_name_value = table;
s_custom_keys_type_name_value_nr = nr;
log_debug("Set custom keys : %p, nr = %d\n", table, nr);
return RET_OK;
}

View File

@ -27,7 +27,7 @@
BEGIN_C_DECLS
typedef struct _key_type_value_t {
const char* name;
char* name;
uint32_t type;
uint32_t value;
} key_type_value_t;

View File

@ -2,6 +2,7 @@
#include "base/enums.h"
#include "gtest/gtest.h"
#include "base/assets_manager.h"
#include "base/custom_keys.inc"
TEST(Enums, basic) {
ASSERT_EQ(asset_type_find("style"), asset_type_find_by_value(ASSET_TYPE_STYLE));
@ -19,7 +20,7 @@ TEST(Enums, keys) {
TEST(Enums, custom_keys) {
static const key_type_value_t custom_keys[] = {
{"CUSTOM1", 0, TK_KEY_RETURN},
{(char*)"CUSTOM1", 0, TK_KEY_RETURN},
};
ASSERT_EQ(keys_type_find("CUSTOM1") == NULL, TRUE);
@ -29,3 +30,11 @@ TEST(Enums, custom_keys) {
keys_type_set_custom_keys(NULL, 0);
ASSERT_EQ(keys_type_find("CUSTOM1") == NULL, TRUE);
}
TEST(Enums, custom_keys_load) {
uint32_t nr = 0;
key_type_value_t* custom_keys = keys_type_custom_keys_load(&nr);
ASSERT_EQ(custom_keys != NULL || nr == 0, TRUE);
keys_type_custom_keys_unload(custom_keys, nr);
}