rework system info

This commit is contained in:
xianjimli 2019-03-07 15:25:11 +08:00
parent 216bca57a2
commit f8416152dd
13 changed files with 315 additions and 27 deletions

Binary file not shown.

View File

@ -1,8 +1,11 @@
# 最新动态 # 最新动态
* 2019/03/06-07 * 2019/03/07
* 增加str\_expand\_vars函数。
* 重新实现system\_info。
* 2019/03/06
* slide view支持设置不同的切换动画。 * slide view支持设置不同的切换动画。
* 修改single fb的flush函数相关问题(感谢陈谭提供补丁)。 * 修改single fb的flush函数相关问题(感谢陈谭提供补丁)。
* 增加str\_expand\_vars函数。
* 2019/03/05 * 2019/03/05
* 更新文档。 * 更新文档。

View File

@ -102,6 +102,7 @@ ret_t tk_init_internal(void) {
#endif /*WITH_TRUETYPE_FONT*/ #endif /*WITH_TRUETYPE_FONT*/
return_value_if_fail(platform_prepare() == RET_OK, RET_FAIL); return_value_if_fail(platform_prepare() == RET_OK, RET_FAIL);
#ifdef WITH_WIDGET_POOL #ifdef WITH_WIDGET_POOL
return_value_if_fail(widget_pool_set(widget_pool_create(WITH_WIDGET_POOL)) == RET_OK, RET_FAIL); return_value_if_fail(widget_pool_set(widget_pool_create(WITH_WIDGET_POOL)) == RET_OK, RET_FAIL);
#endif /*WITH_WIDGET_POOL*/ #endif /*WITH_WIDGET_POOL*/
@ -124,7 +125,7 @@ ret_t tk_init_internal(void) {
} }
ret_t tk_init(wh_t w, wh_t h, app_type_t app_type, const char* app_name, const char* app_root) { ret_t tk_init(wh_t w, wh_t h, app_type_t app_type, const char* app_name, const char* app_root) {
system_info_init(app_type, app_name, app_root); ENSURE(system_info_init(app_type, app_name, app_root) == RET_OK);
return_value_if_fail(tk_init_internal() == RET_OK, RET_FAIL); return_value_if_fail(tk_init_internal() == RET_OK, RET_FAIL);
return main_loop_init(w, h) != NULL ? RET_OK : RET_FAIL; return main_loop_init(w, h) != NULL ? RET_OK : RET_FAIL;
@ -172,6 +173,8 @@ ret_t tk_deinit_internal(void) {
assets_manager_destroy(assets_manager()); assets_manager_destroy(assets_manager());
assets_manager_set(NULL); assets_manager_set(NULL);
system_info_deinit();
return RET_OK; return RET_OK;
} }
@ -209,7 +212,7 @@ ret_t tk_set_lcd_orientation(lcd_orientation_t orientation) {
wh_t h = info->lcd_h; wh_t h = info->lcd_h;
lcd_t* lcd = loop->canvas.lcd; lcd_t* lcd = loop->canvas.lcd;
info->lcd_orientation = orientation; system_info_set_lcd_orientation(info, orientation);
if (orientation == LCD_ORIENTATION_90 || orientation == LCD_ORIENTATION_270) { if (orientation == LCD_ORIENTATION_90 || orientation == LCD_ORIENTATION_270) {
w = info->lcd_h; w = info->lcd_h;
h = info->lcd_w; h = info->lcd_w;

View File

@ -23,6 +23,7 @@
#define TK_LOCALE_H #define TK_LOCALE_H
#include "tkc/emitter.h" #include "tkc/emitter.h"
#include "base/events.h"
#include "base/assets_manager.h" #include "base/assets_manager.h"
BEGIN_C_DECLS BEGIN_C_DECLS

View File

@ -19,20 +19,143 @@
* *
*/ */
#include "tkc/object.h"
#include "base/system_info.h" #include "base/system_info.h"
static system_info_t info; static system_info_t* s_system_info = NULL;
system_info_t* system_info() { system_info_t* system_info() {
return &info; return s_system_info;
} }
ret_t system_info_init(app_type_t app_type, const char* app_name, const char* app_root) { ret_t system_info_set_app_info(system_info_t* info, app_type_t app_type, const char* app_name,
memset(&info, 0x00, sizeof(info)); const char* app_root) {
return_value_if_fail(info != NULL, RET_BAD_PARAMS);
info.app_type = app_type; info->app_type = app_type;
info.app_root = app_root ? app_root : "./"; info->app_root = app_root ? app_root : "./";
info.app_name = app_name ? app_name : "AWTK Simulator"; info->app_name = app_name ? app_name : "AWTK Simulator";
return RET_OK;
}
ret_t system_info_init(app_type_t app_type, const char* app_name, const char* app_root) {
if (s_system_info == NULL) {
s_system_info = system_info_create(app_type, app_name, app_root);
return_value_if_fail(s_system_info != NULL, RET_BAD_PARAMS);
} else {
system_info_set_app_info(s_system_info, app_type, app_name, app_root);
}
return RET_OK;
}
ret_t system_info_deinit(void) {
return_value_if_fail(s_system_info != NULL, RET_BAD_PARAMS);
object_unref(OBJECT(s_system_info));
s_system_info = NULL;
return RET_OK;
}
static ret_t system_info_get_prop(object_t* obj, const char* name, value_t* v) {
system_info_t* info = SYSTEM_INFO(obj);
if (tk_str_eq(name, SYSTEM_INFO_PROP_LCD_W)) {
value_set_int(v, info->lcd_w);
} else if (tk_str_eq(name, SYSTEM_INFO_PROP_LCD_H)) {
value_set_int(v, info->lcd_h);
} else if (tk_str_eq(name, SYSTEM_INFO_PROP_LCD_TYPE)) {
value_set_int(v, info->lcd_type);
} else if (tk_str_eq(name, SYSTEM_INFO_PROP_LCD_ORIENTATION)) {
value_set_int(v, info->lcd_orientation);
} else if (tk_str_eq(name, SYSTEM_INFO_PROP_LCD_ORIENTATION_NAME)) {
int32_t w = info->lcd_w;
int32_t h = info->lcd_h;
lcd_orientation_t orientation = info->lcd_orientation;
if (orientation == LCD_ORIENTATION_90 || orientation == LCD_ORIENTATION_270) {
w = info->lcd_h;
h = info->lcd_w;
}
value_set_str(v, w < h ? "portrait" : "landscape");
} else if (tk_str_eq(name, SYSTEM_INFO_PROP_DEVICE_PIXEL_RATIO)) {
value_set_int(v, info->device_pixel_ratio);
} else if (tk_str_eq(name, SYSTEM_INFO_PROP_APP_TYPE)) {
value_set_int(v, info->app_type);
} else if (tk_str_eq(name, SYSTEM_INFO_PROP_APP_NAME)) {
value_set_str(v, info->app_name);
} else if (tk_str_eq(name, SYSTEM_INFO_PROP_APP_ROOT)) {
value_set_str(v, info->app_root);
} else {
value_set_int(v, 0);
return RET_FOUND;
}
return RET_OK;
}
static const object_vtable_t s_system_info_vtable = {.type = "system_info",
.desc = "system_info",
.size = sizeof(system_info_t),
.get_prop = system_info_get_prop};
system_info_t* system_info_create(app_type_t app_type, const char* app_name, const char* app_root) {
object_t* obj = object_create(&s_system_info_vtable);
system_info_t* info = SYSTEM_INFO(obj);
return_value_if_fail(info != NULL, NULL);
info->font_scale = 1;
info->device_pixel_ratio = 1;
system_info_set_app_info(info, app_type, app_name, app_root);
return info;
}
ret_t system_info_set_font_scale(system_info_t* info, float_t font_scale) {
return_value_if_fail(info != NULL && font_scale >= 0.5 && font_scale <= 2, RET_BAD_PARAMS);
info->font_scale = font_scale;
return RET_OK;
}
ret_t system_info_set_lcd_w(system_info_t* info, uint32_t lcd_w) {
return_value_if_fail(info != NULL, RET_BAD_PARAMS);
info->lcd_w = lcd_w;
return RET_OK;
}
ret_t system_info_set_lcd_h(system_info_t* info, uint32_t lcd_h) {
return_value_if_fail(info != NULL, RET_BAD_PARAMS);
info->lcd_h = lcd_h;
return RET_OK;
}
ret_t system_info_set_lcd_type(system_info_t* info, lcd_type_t lcd_type) {
return_value_if_fail(info != NULL, RET_BAD_PARAMS);
info->lcd_type = lcd_type;
return RET_OK;
}
ret_t system_info_set_lcd_orientation(system_info_t* info, lcd_orientation_t lcd_orientation) {
return_value_if_fail(info != NULL, RET_BAD_PARAMS);
info->lcd_orientation = lcd_orientation;
return RET_OK;
}
ret_t system_info_set_device_pixel_ratio(system_info_t* info, float_t device_pixel_ratio) {
return_value_if_fail(info != NULL, RET_BAD_PARAMS);
info->device_pixel_ratio = device_pixel_ratio;
return RET_OK; return RET_OK;
} }

View File

@ -22,15 +22,19 @@
#ifndef TK_SYSTEM_INFO_H #ifndef TK_SYSTEM_INFO_H
#define TK_SYSTEM_INFO_H #define TK_SYSTEM_INFO_H
#include "tkc/object.h"
#include "base/lcd.h" #include "base/lcd.h"
BEGIN_C_DECLS BEGIN_C_DECLS
/** /**
* @class system_info_t * @class system_info_t
* @parent object
* *
*/ */
typedef struct _system_info_t { typedef struct _system_info_t {
object_t object;
/** /**
* @property {uint32_t} lcd_w * @property {uint32_t} lcd_w
* @annotation ["readable"] * @annotation ["readable"]
@ -59,6 +63,13 @@ typedef struct _system_info_t {
*/ */
float_t device_pixel_ratio; float_t device_pixel_ratio;
/**
* @property {float_t} font_scale
* @annotation ["readable"]
*
*/
float_t font_scale;
/** /**
* @property {lcd_orientation_t} lcd_orientation * @property {lcd_orientation_t} lcd_orientation
* @annotation ["readable"] * @annotation ["readable"]
@ -66,6 +77,12 @@ typedef struct _system_info_t {
*/ */
lcd_orientation_t lcd_orientation; lcd_orientation_t lcd_orientation;
/**
* @property {const char*} lcd_orientation_name
* @annotation ["readable", "fake"]
* "portrait""landscape"
*/
/** /**
* @property {app_type_t} app_type * @property {app_type_t} app_type
* @annotation ["readable"] * @annotation ["readable"]
@ -91,6 +108,7 @@ typedef struct _system_info_t {
/** /**
* @method system_info * @method system_info
* system_info对象 * system_info对象
* @alias system_info_instance
* @annotation ["constructor"] * @annotation ["constructor"]
* @return {system_info_t*} system_info对象 * @return {system_info_t*} system_info对象
*/ */
@ -98,7 +116,8 @@ system_info_t* system_info(void);
/** /**
* @method system_info_init * @method system_info_init
* system_info对象(PC软件才需调用) * system_info对象
* @annotation ["static"]
* @param {app_type_t} app_type * @param {app_type_t} app_type
* @param {const char*} app_name * @param {const char*} app_name
* @param {const char*} app_root * @param {const char*} app_root
@ -107,6 +126,99 @@ system_info_t* system_info(void);
*/ */
ret_t system_info_init(app_type_t app_type, const char* app_name, const char* app_root); ret_t system_info_init(app_type_t app_type, const char* app_name, const char* app_root);
/**
* @method system_info_deinit
* @annotation ["static"]
* system_info对象
*
* @return {ret_t} RET_OK表示成功
*/
ret_t system_info_deinit(void);
/**
* @method system_info_set_font_scale
*
*
* @param {system_info_t* info} info system_info对象
* @param {float_t} font_scale
*
* @return {ret_t} RET_OK表示成功
*/
ret_t system_info_set_font_scale(system_info_t* info, float_t font_scale);
/**
* @method system_info_set_lcd_w
* LCD宽度
*
* @param {system_info_t* info} info system_info对象
* @param {uint32_t} lcd_w LCD宽度
*
* @return {ret_t} RET_OK表示成功
*/
ret_t system_info_set_lcd_w(system_info_t* info, uint32_t lcd_w);
/**
* @method system_info_set_lcd_h
* LCD高度
*
* @param {system_info_t* info} info system_info对象
* @param {uint32_t} lcd_h LCD高度
*
* @return {ret_t} RET_OK表示成功
*/
ret_t system_info_set_lcd_h(system_info_t* info, uint32_t lcd_h);
/**
* @method system_info_set_lcd_type
* LCD类型
*
* @param {system_info_t* info} info system_info对象
* @param {lcd_type_t} lcd_type LCD类型
*
* @return {ret_t} RET_OK表示成功
*/
ret_t system_info_set_lcd_type(system_info_t* info, lcd_type_t lcd_type);
/**
* @method system_info_set_lcd_orientation
* LCD类型
*
* @param {system_info_t* info} info system_info对象
* @param {lcd_orientation_t} lcd_orientation LCD类型
*
* @return {ret_t} RET_OK表示成功
*/
ret_t system_info_set_lcd_orientation(system_info_t* info, lcd_orientation_t lcd_orientation);
/**
* @method system_info_set_device_pixel_ratio
* LCD密度
*
* @param {system_info_t* info} info system_info对象
* @param {float_t} device_pixel_ratio LCD密度
*
* @return {ret_t} RET_OK表示成功
*/
ret_t system_info_set_device_pixel_ratio(system_info_t* info, float_t device_pixel_ratio);
#define SYSTEM_INFO(obj) ((system_info_t*)(obj))
#define SYSTEM_INFO_PROP_LCD_W "lcd_w"
#define SYSTEM_INFO_PROP_LCD_H "lcd_h"
#define SYSTEM_INFO_PROP_LCD_TYPE "lcd_type"
#define SYSTEM_INFO_PROP_LCD_ORIENTATION "lcd_orientation"
#define SYSTEM_INFO_PROP_DEVICE_PIXEL_RATIO "device_pixel_ratio"
#define SYSTEM_INFO_PROP_LCD_ORIENTATION_NAME "lcd_orientation_name"
#define SYSTEM_INFO_PROP_APP_TYPE "app_type"
#define SYSTEM_INFO_PROP_APP_NAME "app_name"
#define SYSTEM_INFO_PROP_APP_ROOT "app_root"
/*public for test*/
system_info_t* system_info_create(app_type_t app_type, const char* app_name, const char* app_root);
ret_t system_info_set_app_info(system_info_t* info, app_type_t app_type, const char* app_name,
const char* app_root);
END_C_DECLS END_C_DECLS
#endif /*TK_SYSTEM_INFO_H*/ #endif /*TK_SYSTEM_INFO_H*/

View File

@ -394,10 +394,10 @@ static lcd_t* lcd_mem_create(wh_t w, wh_t h, bool_t alloc) {
lcd->line_length = w * bpp; lcd->line_length = w * bpp;
lcd->format = LCD_FORMAT; lcd->format = LCD_FORMAT;
info->lcd_w = base->w; system_info_set_lcd_w(info, base->w);
info->lcd_h = base->h; system_info_set_lcd_h(info, base->h);
info->lcd_type = base->type; system_info_set_lcd_type(info, base->type);
info->device_pixel_ratio = 1; system_info_set_device_pixel_ratio(info, 1);
return base; return base;
} }

View File

@ -262,10 +262,11 @@ lcd_t* lcd_reg_create(wh_t w, wh_t h) {
lcd->h = h; lcd->h = h;
lcd->ratio = 1; lcd->ratio = 1;
lcd->type = LCD_REGISTER; lcd->type = LCD_REGISTER;
info->lcd_w = lcd->w;
info->lcd_h = lcd->h; system_info_set_lcd_w(info, lcd->w);
info->lcd_type = lcd->type; system_info_set_lcd_h(info, lcd->h);
info->device_pixel_ratio = 1; system_info_set_lcd_type(info, lcd->type);
system_info_set_device_pixel_ratio(info, 1);
lcd->begin_frame = lcd_reg_begin_frame; lcd->begin_frame = lcd_reg_begin_frame;
lcd->draw_vline = lcd_reg_draw_vline; lcd->draw_vline = lcd_reg_draw_vline;

View File

@ -253,10 +253,10 @@ lcd_t* lcd_vgcanvas_init(wh_t w, wh_t h, vgcanvas_t* canvas) {
base->type = LCD_VGCANVAS; base->type = LCD_VGCANVAS;
base->support_dirty_rect = FALSE; base->support_dirty_rect = FALSE;
info->lcd_w = base->w; system_info_set_lcd_w(info, base->w);
info->lcd_h = base->h; system_info_set_lcd_h(info, base->h);
info->lcd_type = base->type; system_info_set_lcd_type(info, base->type);
info->device_pixel_ratio = canvas->ratio; system_info_set_device_pixel_ratio(info, canvas->ratio);
lcd.canvas = canvas; lcd.canvas = canvas;

View File

@ -22,7 +22,7 @@
#ifndef TK_EMITTER_H #ifndef TK_EMITTER_H
#define TK_EMITTER_H #define TK_EMITTER_H
#include "base/events.h" #include "tkc/event.h"
BEGIN_C_DECLS BEGIN_C_DECLS

View File

@ -45,6 +45,7 @@ GTEST_API_ int main(int argc, char** argv) {
assets_init(); assets_init();
tk_init_assets(); tk_init_assets();
RUN_ALL_TESTS(); RUN_ALL_TESTS();
tk_deinit_internal(); tk_deinit_internal();

View File

@ -241,11 +241,11 @@ TEST(Str, expand_vars) {
str_set(s, ""); str_set(s, "");
ASSERT_EQ(str_expand_vars(s, "123${abc}456", vars), RET_OK); ASSERT_EQ(str_expand_vars(s, "123${abc}456", vars), RET_OK);
ASSERT_STREQ(s->str, "123456"); ASSERT_STREQ(s->str, "123456");
str_set(s, ""); str_set(s, "");
ASSERT_EQ(str_expand_vars(s, "123${}456", vars), RET_OK); ASSERT_EQ(str_expand_vars(s, "123${}456", vars), RET_OK);
ASSERT_STREQ(s->str, "123456"); ASSERT_STREQ(s->str, "123456");
str_set(s, ""); str_set(s, "");
ASSERT_EQ(str_expand_vars(s, "123${abc+$x}456", vars), RET_OK); ASSERT_EQ(str_expand_vars(s, "123${abc+$x}456", vars), RET_OK);
ASSERT_STREQ(s->str, "123456"); ASSERT_STREQ(s->str, "123456");

44
tests/system_info_test.cc Normal file
View File

@ -0,0 +1,44 @@
#include "base/system_info.h"
#include "gtest/gtest.h"
TEST(SystemInfo, basic) {
system_info_t* info = system_info_create(APP_DESKTOP, "awtk", "awtk_dir");
ASSERT_STREQ(info->app_name, "awtk");
ASSERT_STREQ(info->app_root, "awtk_dir");
ASSERT_EQ(info->app_type, APP_DESKTOP);
ASSERT_STREQ(info->app_name, "awtk");
ASSERT_STREQ(info->app_root, "awtk_dir");
ASSERT_EQ(system_info_set_lcd_w(info, 100), RET_OK);
ASSERT_EQ(info->lcd_w, 100);
ASSERT_EQ(info->lcd_w, object_get_prop_int(OBJECT(info), SYSTEM_INFO_PROP_LCD_W, 0));
ASSERT_EQ(system_info_set_lcd_h(info, 200), RET_OK);
ASSERT_EQ(info->lcd_h, 200);
ASSERT_EQ(info->lcd_h, object_get_prop_int(OBJECT(info), SYSTEM_INFO_PROP_LCD_H, 0));
ASSERT_EQ(system_info_set_lcd_type(info, LCD_FRAMEBUFFER), RET_OK);
ASSERT_EQ(info->lcd_type, LCD_FRAMEBUFFER);
ASSERT_EQ((int32_t)(info->lcd_type),
object_get_prop_int(OBJECT(info), SYSTEM_INFO_PROP_LCD_TYPE, 111));
ASSERT_EQ(system_info_set_device_pixel_ratio(info, 2), RET_OK);
ASSERT_EQ(info->device_pixel_ratio, 2);
ASSERT_EQ(info->device_pixel_ratio,
object_get_prop_float(OBJECT(info), SYSTEM_INFO_PROP_DEVICE_PIXEL_RATIO, 0));
ASSERT_EQ(system_info_set_lcd_orientation(info, LCD_ORIENTATION_180), RET_OK);
ASSERT_EQ(info->lcd_orientation, LCD_ORIENTATION_180);
ASSERT_EQ((int32_t)(info->lcd_orientation),
object_get_prop_int(OBJECT(info), SYSTEM_INFO_PROP_LCD_ORIENTATION, 111));
ASSERT_STREQ("portrait",
object_get_prop_str(OBJECT(info), SYSTEM_INFO_PROP_LCD_ORIENTATION_NAME));
ASSERT_EQ(system_info_set_lcd_w(info, 1000), RET_OK);
ASSERT_STREQ("landscape",
object_get_prop_str(OBJECT(info), SYSTEM_INFO_PROP_LCD_ORIENTATION_NAME));
object_unref(OBJECT(info));
}