Skip to content

Commit

Permalink
chore(widget): replace normal widget methods with rotable
Browse files Browse the repository at this point in the history
Signed-off-by: Xu Xingliang <xuxingliang@xiaomi.com>
  • Loading branch information
XuNeo committed Jul 21, 2024
1 parent 8b49c8a commit 0b774eb
Show file tree
Hide file tree
Showing 8 changed files with 238 additions and 228 deletions.
17 changes: 9 additions & 8 deletions src/anim.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "luavgl.h"
#include "private.h"
#include "rotable.h"

/**
* Different to lvgl anim, anim in lua can be restarted after it's done.
Expand Down Expand Up @@ -319,15 +320,15 @@ static const luaL_Reg luavgl_anim_meta[] = {
{NULL, NULL },
};

static const luaL_Reg luavgl_anim_methods[] = {
{"set", luavgl_anim_set },
{"start", luavgl_anim_start },
static const rotable_Reg luavgl_anim_methods[] = {
{"set", LUA_TFUNCTION, {luavgl_anim_set} },
{"start", LUA_TFUNCTION, {luavgl_anim_start} },

/* in lua anim, stopped anim can be restarted. */
{"stop", luavgl_anim_stop },
{"delete", luavgl_anim_delete},
/* in lua anim, stopped anim can be restarted. */
{"stop", LUA_TFUNCTION, {luavgl_anim_stop} },
{"delete", LUA_TFUNCTION, {luavgl_anim_delete}},

{NULL, NULL }
{0, 0, {0} }
};

static void luavgl_anim_init(lua_State *L)
Expand All @@ -336,7 +337,7 @@ static void luavgl_anim_init(lua_State *L)
luaL_newmetatable(L, "lv_anim");
luaL_setfuncs(L, luavgl_anim_meta, 0);

luaL_newlib(L, luavgl_anim_methods);
rotable_newlib(L, luavgl_anim_methods);
lua_setfield(L, -2, "__index");

lua_pop(L, 1); /* pop metatable */
Expand Down
42 changes: 21 additions & 21 deletions src/disp.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "luavgl.h"
#include "private.h"
#include "rotable.h"

typedef struct luavgl_disp_s {
lv_disp_t *disp;
Expand Down Expand Up @@ -255,30 +256,29 @@ static int luavgl_disp_set_rotation(lua_State *L)
/**
* luavgl.disp lib
*/
static const luaL_Reg disp_lib[] = {
{"get_default", luavgl_disp_get_default },
{"get_scr_act", luavgl_disp_get_scr_act },
{"get_scr_prev", luavgl_disp_get_scr_prev},
{"get_next", luavgl_disp_get_next },
{"load_scr", luavgl_disp_load_scr },

{NULL, NULL },
static const rotable_Reg disp_lib[] = {
{"get_default", LUA_TFUNCTION, {luavgl_disp_get_default} },
{"get_scr_act", LUA_TFUNCTION, {luavgl_disp_get_scr_act} },
{"get_scr_prev", LUA_TFUNCTION, {luavgl_disp_get_scr_prev}},
{"get_next", LUA_TFUNCTION, {luavgl_disp_get_next} },
{"load_scr", LUA_TFUNCTION, {luavgl_disp_load_scr} },

{0, 0, {0} },
};

/*
** methods for disp handles
*/
static const luaL_Reg disp_methods[] = {
{"get_layer_top", luavgl_disp_get_layer_top},
{"get_layer_sys", luavgl_disp_get_layer_sys},
{"get_next", luavgl_disp_get_next },
{"set_rotation", luavgl_disp_set_rotation },
{"get_res", luavgl_disp_get_res },

{"get_scr_act", luavgl_disp_get_scr_act },
{"get_scr_prev", luavgl_disp_get_scr_prev },

{NULL, NULL },
static const rotable_Reg disp_methods[] = {
{"get_layer_top", LUA_TFUNCTION, {luavgl_disp_get_layer_top}},
{"get_layer_sys", LUA_TFUNCTION, {luavgl_disp_get_layer_sys}},
{"get_next", LUA_TFUNCTION, {luavgl_disp_get_next} },
{"set_rotation", LUA_TFUNCTION, {luavgl_disp_set_rotation} },
{"get_res", LUA_TFUNCTION, {luavgl_disp_get_res} },
{"get_scr_act", LUA_TFUNCTION, {luavgl_disp_get_scr_act} },
{"get_scr_prev", LUA_TFUNCTION, {luavgl_disp_get_scr_prev} },

{0, 0, {0} },
};

static const luaL_Reg disp_meta[] = {
Expand All @@ -293,12 +293,12 @@ static void luavgl_disp_init(lua_State *L)
luaL_newmetatable(L, "lv_disp");
luaL_setfuncs(L, disp_meta, 0);

luaL_newlib(L, disp_methods);
rotable_newlib(L, disp_methods);
lua_setfield(L, -2, "__index");

lua_pop(L, 1); /* pop metatable */

/* luavgl.disp lib */
luaL_newlib(L, disp_lib);
rotable_newlib(L, disp_lib);
lua_setfield(L, -2, "disp");
}
35 changes: 18 additions & 17 deletions src/fs.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "luavgl.h"
#include "private.h"
#include "rotable.h"

typedef struct luavgl_fs_s {
lv_fs_file_t file;
Expand Down Expand Up @@ -310,23 +311,23 @@ static int luavgl_dir_gc(lua_State *L)
* luavgl.fs lib
*
*/
static const luaL_Reg fs_lib[] = {
{"open_file", luavgl_fs_open },
{"open_dir", luavgl_dir_open},
static const rotable_Reg fs_lib[] = {
{"open_file", LUA_TFUNCTION, {luavgl_fs_open} },
{"open_dir", LUA_TFUNCTION, {luavgl_dir_open}},

{NULL, NULL },
{0, 0, {0} },
};

/*
** methods for file handles
*/
static const luaL_Reg fs_methods[] = {
{"read", luavgl_fs_read },
{"write", luavgl_fs_write},
{"close", luavgl_fs_close},
{"seek", luavgl_fs_seek },
static const rotable_Reg fs_methods[] = {
{"read", LUA_TFUNCTION, {luavgl_fs_read} },
{"write", LUA_TFUNCTION, {luavgl_fs_write}},
{"close", LUA_TFUNCTION, {luavgl_fs_close}},
{"seek", LUA_TFUNCTION, {luavgl_fs_seek} },

{NULL, NULL },
{0, 0, {0} },
};

static const luaL_Reg fs_meta[] = {
Expand All @@ -341,11 +342,11 @@ static const luaL_Reg fs_meta[] = {
/*
** methods for dir handles
*/
static const luaL_Reg dir_methods[] = {
{"read", luavgl_dir_read },
{"close", luavgl_dir_close},
static const rotable_Reg dir_methods[] = {
{"read", LUA_TFUNCTION, {luavgl_dir_read} },
{"close", LUA_TFUNCTION, {luavgl_dir_close}},

{NULL, NULL },
{0, 0, {0} },
};

static const luaL_Reg dir_meta[] = {
Expand All @@ -363,7 +364,7 @@ static void luavgl_fs_init(lua_State *L)
luaL_newmetatable(L, "lv_fs");
luaL_setfuncs(L, fs_meta, 0);

luaL_newlib(L, fs_methods);
rotable_newlib(L, fs_methods);
lua_setfield(L, -2, "__index");

lua_pop(L, 1); /* pop metatable */
Expand All @@ -372,12 +373,12 @@ static void luavgl_fs_init(lua_State *L)
luaL_newmetatable(L, "lv_dir");
luaL_setfuncs(L, dir_meta, 0);

luaL_newlib(L, dir_methods);
rotable_newlib(L, dir_methods);
lua_setfield(L, -2, "__index");

lua_pop(L, 1); /* pop metatable */

/* luavgl.fs lib */
luaL_newlib(L, fs_lib);
rotable_newlib(L, fs_lib);
lua_setfield(L, -2, "fs");
}
61 changes: 31 additions & 30 deletions src/group.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "luavgl.h"
#include "private.h"
#include "rotable.h"

typedef struct luavgl_group_s {
lv_group_t *group;
Expand Down Expand Up @@ -288,39 +289,39 @@ static int luavgl_group_gc(lua_State *L)
/**
* luavgl.group lib
*/
static const luaL_Reg group_lib[] = {
{"create", luavgl_group_create },
{"get_default", luavgl_group_get_default},
{"remove_obj", luavgl_group_remove_obj },
{"remove_objs", luavgl_group_remove_objs},

{"focus_obj", luavgl_group_focus_obj },
{NULL, NULL },
static const rotable_Reg group_lib[] = {
{"create", LUA_TFUNCTION, {luavgl_group_create} },
{"get_default", LUA_TFUNCTION, {luavgl_group_get_default}},
{"remove_obj", LUA_TFUNCTION, {luavgl_group_remove_obj} },
{"remove_objs", LUA_TFUNCTION, {luavgl_group_remove_objs}},

{"focus_obj", LUA_TFUNCTION, {luavgl_group_focus_obj} },
{0, 0, {0} },
};

/*
** methods for file handles
*/
static const luaL_Reg group_methods[] = {
{"delete", luavgl_group_delete },
{"set_default", luavgl_group_set_default },
{"add_obj", luavgl_group_add_obj },
{"remove_obj", luavgl_group_remove_obj },
{"focus_next", luavgl_group_focus_next },
{"focus_prev", luavgl_group_focus_prev },
{"focus_freeze", luavgl_group_focus_freeze },
{"send_data", luavgl_group_send_data },
{"set_focus_cb", luavgl_group_set_focus_cb },
{"set_edge_cb", luavgl_group_set_edge_cb },
{"get_focus_cb", luavgl_group_get_focus_cb },
{"get_edge_cb", luavgl_group_get_edge_cb },
{"set_editing", luavgl_group_set_editing },
{"set_wrap", luavgl_group_set_wrap },
{"get_wrap", luavgl_group_get_wrap },
{"get_obj_count", luavgl_group_get_obj_count},
{"get_focused", luavgl_group_get_focused },

{NULL, NULL },
static const rotable_Reg group_methods[] = {
{"delete", LUA_TFUNCTION, {luavgl_group_delete} },
{"set_default", LUA_TFUNCTION, {luavgl_group_set_default} },
{"add_obj", LUA_TFUNCTION, {luavgl_group_add_obj} },
{"remove_obj", LUA_TFUNCTION, {luavgl_group_remove_obj} },
{"focus_next", LUA_TFUNCTION, {luavgl_group_focus_next} },
{"focus_prev", LUA_TFUNCTION, {luavgl_group_focus_prev} },
{"focus_freeze", LUA_TFUNCTION, {luavgl_group_focus_freeze} },
{"send_data", LUA_TFUNCTION, {luavgl_group_send_data} },
{"set_focus_cb", LUA_TFUNCTION, {luavgl_group_set_focus_cb} },
{"set_edge_cb", LUA_TFUNCTION, {luavgl_group_set_edge_cb} },
{"get_focus_cb", LUA_TFUNCTION, {luavgl_group_get_focus_cb} },
{"get_edge_cb", LUA_TFUNCTION, {luavgl_group_get_edge_cb} },
{"set_editing", LUA_TFUNCTION, {luavgl_group_set_editing} },
{"set_wrap", LUA_TFUNCTION, {luavgl_group_set_wrap} },
{"get_wrap", LUA_TFUNCTION, {luavgl_group_get_wrap} },
{"get_obj_count", LUA_TFUNCTION, {luavgl_group_get_obj_count}},
{"get_focused", LUA_TFUNCTION, {luavgl_group_get_focused} },

{0, 0, {0} },
};

static const luaL_Reg group_meta[] = {
Expand All @@ -336,12 +337,12 @@ static void luavgl_group_init(lua_State *L)
luaL_newmetatable(L, "lv_group");
luaL_setfuncs(L, group_meta, 0);

luaL_newlib(L, group_methods);
rotable_newlib(L, group_methods);
lua_setfield(L, -2, "__index");

lua_pop(L, 1); /* pop metatable */

/* luavgl.indev lib */
luaL_newlib(L, group_lib);
rotable_newlib(L, group_lib);
lua_setfield(L, -2, "group");
}
45 changes: 23 additions & 22 deletions src/indev.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "luavgl.h"
#include "private.h"
#include "rotable.h"

typedef struct luavgl_indev_s {
lv_indev_t *indev;
Expand Down Expand Up @@ -294,36 +295,36 @@ static int luavgl_indev_gc(lua_State *L)
/**
* luavgl.indev lib
*/
static const luaL_Reg indev_lib[] = {
{"get_act", luavgl_indev_get_act },
static const rotable_Reg indev_lib[] = {
{"get_act", LUA_TFUNCTION, {luavgl_indev_get_act} },
#if 0
{"get_obj_act", luavgl_indev_get_obj_act},
{"get_obj_act", LUA_TFUNCTION, {luavgl_indev_get_obj_act}},
#endif
{"get_next", luavgl_indev_get_next },
{"get_next", LUA_TFUNCTION, {luavgl_indev_get_next}},

{NULL, NULL },
{0, 0, {0} },
};

/*
** methods for file handles
*/
static const luaL_Reg methods[] = {
{"get_type", luavgl_indev_get_type },
{"reset", luavgl_indev_reset },
{"reset_long_press", luavgl_indev_reset_long_press},
{"set_cursor", luavgl_indev_set_cursor },
{"set_group", luavgl_indev_set_group },
{"get_point", luavgl_indev_get_point },
{"get_gesture_dir", luavgl_indev_get_gesture_dir },
{"get_key", luavgl_indev_get_key },
{"get_scroll_dir", luavgl_indev_get_scroll_dir },
{"get_scroll_obj", luavgl_indev_get_scroll_obj },
{"get_vect", luavgl_indev_get_vect },
{"wait_release", luavgl_indev_wait_release },
static const rotable_Reg methods[] = {
{"get_type", LUA_TFUNCTION, {luavgl_indev_get_type} },
{"reset", LUA_TFUNCTION, {luavgl_indev_reset} },
{"reset_long_press", LUA_TFUNCTION, {luavgl_indev_reset_long_press}},
{"set_cursor", LUA_TFUNCTION, {luavgl_indev_set_cursor} },
{"set_group", LUA_TFUNCTION, {luavgl_indev_set_group} },
{"get_point", LUA_TFUNCTION, {luavgl_indev_get_point} },
{"get_gesture_dir", LUA_TFUNCTION, {luavgl_indev_get_gesture_dir} },
{"get_key", LUA_TFUNCTION, {luavgl_indev_get_key} },
{"get_scroll_dir", LUA_TFUNCTION, {luavgl_indev_get_scroll_dir} },
{"get_scroll_obj", LUA_TFUNCTION, {luavgl_indev_get_scroll_obj} },
{"get_vect", LUA_TFUNCTION, {luavgl_indev_get_vect} },
{"wait_release", LUA_TFUNCTION, {luavgl_indev_wait_release} },
#if 0
{"on_event", luavgl_indev_on_event },
{"on_event", LUA_TFUNCTION, {luavgl_indev_on_event} },
#endif
{NULL, NULL },
{0, 0, {0} },
};

static const luaL_Reg meta[] = {
Expand All @@ -340,12 +341,12 @@ static void luavgl_indev_init(lua_State *L)
luaL_newmetatable(L, "lv_indev");
luaL_setfuncs(L, meta, 0);

luaL_newlib(L, methods);
rotable_newlib(L, methods);
lua_setfield(L, -2, "__index");

lua_pop(L, 1); /* pop metatable */

/* luavgl.indev lib */
luaL_newlib(L, indev_lib);
rotable_newlib(L, indev_lib);
lua_setfield(L, -2, "indev");
}
11 changes: 7 additions & 4 deletions src/rotable.c
Original file line number Diff line number Diff line change
Expand Up @@ -144,8 +144,11 @@ static int rotable_udata_index( lua_State* L ) {
p = find_key( p, t->n, s );
if( p )
rotable_pushvalue( L, p );
else
lua_pushnil( L );
else {
lua_getmetatable(L, 1); // Get the metatable of the table (stack: [1] table, [2] key, [3] metatable)
lua_pushvalue(L, 2); // Push the key onto the stack (stack: [1] table, [2] key, [3] metatable, [4] key)
return lua_rawget(L, -2); // Look up the key in the metatable (stack: [1] table, [2] key, [3] metatable, [4] value)
}
return 1;
}

Expand Down Expand Up @@ -242,9 +245,9 @@ ROTABLE_EXPORT void rotable_newlib( lua_State* L, void const* v ) {

ROTABLE_EXPORT void rotable_newidx( lua_State* L, void const* v ) {
rotable_Reg const* reg = (rotable_Reg const*)v;
int i = 0;
int i = 1;
lua_pushlightuserdata( L, (void*)v);
for( ; reg[ i ].func; ++i ) {
for( ; reg[ i ].name; ++i ) {
if( lv_strcmp( reg[ i-1 ].name, reg[ i ].name ) >= 0 ) {
i = 0;
break;
Expand Down
Loading

0 comments on commit 0b774eb

Please sign in to comment.