mirror of
https://gitee.com/zlgopen/awtk.git
synced 2024-11-30 02:58:26 +08:00
rework system info
This commit is contained in:
parent
216bca57a2
commit
f8416152dd
BIN
demos/SDL2.dll
BIN
demos/SDL2.dll
Binary file not shown.
@ -1,8 +1,11 @@
|
||||
# 最新动态
|
||||
* 2019/03/06-07
|
||||
* 2019/03/07
|
||||
* 增加str\_expand\_vars函数。
|
||||
* 重新实现system\_info。
|
||||
|
||||
* 2019/03/06
|
||||
* slide view支持设置不同的切换动画。
|
||||
* 修改single fb的flush函数相关问题(感谢陈谭提供补丁)。
|
||||
* 增加str\_expand\_vars函数。
|
||||
|
||||
* 2019/03/05
|
||||
* 更新文档。
|
||||
|
@ -102,6 +102,7 @@ ret_t tk_init_internal(void) {
|
||||
#endif /*WITH_TRUETYPE_FONT*/
|
||||
|
||||
return_value_if_fail(platform_prepare() == RET_OK, RET_FAIL);
|
||||
|
||||
#ifdef WITH_WIDGET_POOL
|
||||
return_value_if_fail(widget_pool_set(widget_pool_create(WITH_WIDGET_POOL)) == RET_OK, RET_FAIL);
|
||||
#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) {
|
||||
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 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_set(NULL);
|
||||
|
||||
system_info_deinit();
|
||||
|
||||
return RET_OK;
|
||||
}
|
||||
|
||||
@ -209,7 +212,7 @@ ret_t tk_set_lcd_orientation(lcd_orientation_t orientation) {
|
||||
wh_t h = info->lcd_h;
|
||||
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) {
|
||||
w = info->lcd_h;
|
||||
h = info->lcd_w;
|
||||
|
@ -23,6 +23,7 @@
|
||||
#define TK_LOCALE_H
|
||||
|
||||
#include "tkc/emitter.h"
|
||||
#include "base/events.h"
|
||||
#include "base/assets_manager.h"
|
||||
|
||||
BEGIN_C_DECLS
|
||||
|
@ -19,20 +19,143 @@
|
||||
*
|
||||
*/
|
||||
|
||||
#include "tkc/object.h"
|
||||
#include "base/system_info.h"
|
||||
|
||||
static system_info_t info;
|
||||
static system_info_t* s_system_info = NULL;
|
||||
|
||||
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) {
|
||||
memset(&info, 0x00, sizeof(info));
|
||||
ret_t system_info_set_app_info(system_info_t* info, app_type_t app_type, const char* app_name,
|
||||
const char* app_root) {
|
||||
return_value_if_fail(info != NULL, RET_BAD_PARAMS);
|
||||
|
||||
info.app_type = app_type;
|
||||
info.app_root = app_root ? app_root : "./";
|
||||
info.app_name = app_name ? app_name : "AWTK Simulator";
|
||||
info->app_type = app_type;
|
||||
info->app_root = app_root ? app_root : "./";
|
||||
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;
|
||||
}
|
||||
|
@ -22,15 +22,19 @@
|
||||
#ifndef TK_SYSTEM_INFO_H
|
||||
#define TK_SYSTEM_INFO_H
|
||||
|
||||
#include "tkc/object.h"
|
||||
#include "base/lcd.h"
|
||||
|
||||
BEGIN_C_DECLS
|
||||
|
||||
/**
|
||||
* @class system_info_t
|
||||
* @parent object
|
||||
* 当前系统的信息。
|
||||
*/
|
||||
typedef struct _system_info_t {
|
||||
object_t object;
|
||||
|
||||
/**
|
||||
* @property {uint32_t} lcd_w
|
||||
* @annotation ["readable"]
|
||||
@ -59,6 +63,13 @@ typedef struct _system_info_t {
|
||||
*/
|
||||
float_t device_pixel_ratio;
|
||||
|
||||
/**
|
||||
* @property {float_t} font_scale
|
||||
* @annotation ["readable"]
|
||||
* 字体缩放比例,用于实现字体整体放大。
|
||||
*/
|
||||
float_t font_scale;
|
||||
|
||||
/**
|
||||
* @property {lcd_orientation_t} lcd_orientation
|
||||
* @annotation ["readable"]
|
||||
@ -66,6 +77,12 @@ typedef struct _system_info_t {
|
||||
*/
|
||||
lcd_orientation_t lcd_orientation;
|
||||
|
||||
/**
|
||||
* @property {const char*} lcd_orientation_name
|
||||
* @annotation ["readable", "fake"]
|
||||
* 显示屏的方向名称,可选值:"portrait"表示竖屏,"landscape"表示横屏。
|
||||
*/
|
||||
|
||||
/**
|
||||
* @property {app_type_t} app_type
|
||||
* @annotation ["readable"]
|
||||
@ -91,6 +108,7 @@ typedef struct _system_info_t {
|
||||
/**
|
||||
* @method system_info
|
||||
* 获取system_info对象。
|
||||
* @alias system_info_instance
|
||||
* @annotation ["constructor"]
|
||||
* @return {system_info_t*} 返回system_info对象。
|
||||
*/
|
||||
@ -98,7 +116,8 @@ system_info_t* system_info(void);
|
||||
|
||||
/**
|
||||
* @method system_info_init
|
||||
* 初始化system_info对象(PC软件才需调用)。
|
||||
* 初始化system_info对象。
|
||||
* @annotation ["static"]
|
||||
* @param {app_type_t} app_type 应用程序的类型。
|
||||
* @param {const char*} app_name 应用程序的名称。
|
||||
* @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);
|
||||
|
||||
/**
|
||||
* @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
|
||||
|
||||
#endif /*TK_SYSTEM_INFO_H*/
|
||||
|
@ -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->format = LCD_FORMAT;
|
||||
|
||||
info->lcd_w = base->w;
|
||||
info->lcd_h = base->h;
|
||||
info->lcd_type = base->type;
|
||||
info->device_pixel_ratio = 1;
|
||||
system_info_set_lcd_w(info, base->w);
|
||||
system_info_set_lcd_h(info, base->h);
|
||||
system_info_set_lcd_type(info, base->type);
|
||||
system_info_set_device_pixel_ratio(info, 1);
|
||||
|
||||
return base;
|
||||
}
|
||||
|
@ -262,10 +262,11 @@ lcd_t* lcd_reg_create(wh_t w, wh_t h) {
|
||||
lcd->h = h;
|
||||
lcd->ratio = 1;
|
||||
lcd->type = LCD_REGISTER;
|
||||
info->lcd_w = lcd->w;
|
||||
info->lcd_h = lcd->h;
|
||||
info->lcd_type = lcd->type;
|
||||
info->device_pixel_ratio = 1;
|
||||
|
||||
system_info_set_lcd_w(info, lcd->w);
|
||||
system_info_set_lcd_h(info, lcd->h);
|
||||
system_info_set_lcd_type(info, lcd->type);
|
||||
system_info_set_device_pixel_ratio(info, 1);
|
||||
|
||||
lcd->begin_frame = lcd_reg_begin_frame;
|
||||
lcd->draw_vline = lcd_reg_draw_vline;
|
||||
|
@ -253,10 +253,10 @@ lcd_t* lcd_vgcanvas_init(wh_t w, wh_t h, vgcanvas_t* canvas) {
|
||||
base->type = LCD_VGCANVAS;
|
||||
base->support_dirty_rect = FALSE;
|
||||
|
||||
info->lcd_w = base->w;
|
||||
info->lcd_h = base->h;
|
||||
info->lcd_type = base->type;
|
||||
info->device_pixel_ratio = canvas->ratio;
|
||||
system_info_set_lcd_w(info, base->w);
|
||||
system_info_set_lcd_h(info, base->h);
|
||||
system_info_set_lcd_type(info, base->type);
|
||||
system_info_set_device_pixel_ratio(info, canvas->ratio);
|
||||
|
||||
lcd.canvas = canvas;
|
||||
|
||||
|
@ -22,7 +22,7 @@
|
||||
#ifndef TK_EMITTER_H
|
||||
#define TK_EMITTER_H
|
||||
|
||||
#include "base/events.h"
|
||||
#include "tkc/event.h"
|
||||
|
||||
BEGIN_C_DECLS
|
||||
|
||||
|
@ -45,6 +45,7 @@ GTEST_API_ int main(int argc, char** argv) {
|
||||
|
||||
assets_init();
|
||||
tk_init_assets();
|
||||
|
||||
RUN_ALL_TESTS();
|
||||
|
||||
tk_deinit_internal();
|
||||
|
@ -241,11 +241,11 @@ TEST(Str, expand_vars) {
|
||||
str_set(s, "");
|
||||
ASSERT_EQ(str_expand_vars(s, "123${abc}456", vars), RET_OK);
|
||||
ASSERT_STREQ(s->str, "123456");
|
||||
|
||||
|
||||
str_set(s, "");
|
||||
ASSERT_EQ(str_expand_vars(s, "123${}456", vars), RET_OK);
|
||||
ASSERT_STREQ(s->str, "123456");
|
||||
|
||||
|
||||
str_set(s, "");
|
||||
ASSERT_EQ(str_expand_vars(s, "123${abc+$x}456", vars), RET_OK);
|
||||
ASSERT_STREQ(s->str, "123456");
|
||||
|
44
tests/system_info_test.cc
Normal file
44
tests/system_info_test.cc
Normal 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));
|
||||
}
|
Loading…
Reference in New Issue
Block a user