Skip to content

Commit

Permalink
chore: remove stdlib and string dependency (#43)
Browse files Browse the repository at this point in the history
Use lvgl's lib instead

Signed-off-by: Xu Xingliang <[email protected]>
  • Loading branch information
XuNeo authored May 26, 2024
1 parent 2e5853a commit b8eacea
Show file tree
Hide file tree
Showing 14 changed files with 114 additions and 64 deletions.
2 changes: 1 addition & 1 deletion deps/lvgl
Submodule lvgl updated 847 files
17 changes: 17 additions & 0 deletions examples/test.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
local function Object(t)
local info = debug.getinfo(2, "nS") -- 2 refers to the caller of the function
print("Called from: " .. info.short_src .. ", function " .. (info.name or 'unknown'))
print("table content:")
obj = {table.unpack(t)}
for k, v in pairs(t) do
print(k, v)
end
return obj
end

Object({
a = "789",
Object({
a = "101112",
})
})
4 changes: 3 additions & 1 deletion simulator/lv_conf.h
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@
*LV_LOG_LEVEL_ERROR Only critical issue, when the system may fail
*LV_LOG_LEVEL_USER Only logs added by the user
*LV_LOG_LEVEL_NONE Do not log anything*/
#define LV_LOG_LEVEL LV_LOG_LEVEL_WARN
#define LV_LOG_LEVEL LV_LOG_LEVEL_INFO

/*1: Print the log with 'printf';
*0: User need to register a callback with `lv_log_register_print_cb()`*/
Expand Down Expand Up @@ -664,7 +664,9 @@

/* libjpeg-turbo decoder library.
* Supports complete JPEG specifications and high-performance JPEG decoding. */
#ifndef LV_USE_LIBJPEG_TURBO
#define LV_USE_LIBJPEG_TURBO 0
#endif

/*GIF decoder library*/
#define LV_USE_GIF 1
Expand Down
1 change: 1 addition & 0 deletions simulator/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include "widgets/widgets.h"

#include <string.h>
#include <unistd.h>

#if defined(WIN32) || defined(_WIN32) || defined(_WIN32_) || defined(WIN64) || defined(_WIN64) || defined(_WIN64_)
#include <direct.h>
Expand Down
14 changes: 7 additions & 7 deletions src/anim.c
Original file line number Diff line number Diff line change
Expand Up @@ -94,19 +94,19 @@ static void _lv_anim_set_path(void *obj, lua_State *L)
}

a->path_cb = lv_anim_path_linear;
if (path == NULL || strcmp(path, "linear") == 0) {
if (path == NULL || lv_strcmp(path, "linear") == 0) {
; /* use default linear path */
} else if (strcmp(path, "ease_in") == 0) {
} else if (lv_strcmp(path, "ease_in") == 0) {
a->path_cb = lv_anim_path_ease_in;
} else if (strcmp(path, "ease_out") == 0) {
} else if (lv_strcmp(path, "ease_out") == 0) {
a->path_cb = lv_anim_path_ease_out;
} else if (strcmp(path, "ease_in_out") == 0) {
} else if (lv_strcmp(path, "ease_in_out") == 0) {
a->path_cb = lv_anim_path_ease_in_out;
} else if (strcmp(path, "overshoot") == 0) {
} else if (lv_strcmp(path, "overshoot") == 0) {
a->path_cb = lv_anim_path_overshoot;
} else if (strcmp(path, "bounce") == 0) {
} else if (lv_strcmp(path, "bounce") == 0) {
a->path_cb = lv_anim_path_bounce;
} else if (strcmp(path, "step") == 0) {
} else if (lv_strcmp(path, "step") == 0) {
a->path_cb = lv_anim_path_step;
}
}
Expand Down
32 changes: 16 additions & 16 deletions src/disp.c
Original file line number Diff line number Diff line change
Expand Up @@ -113,38 +113,38 @@ static int luavgl_disp_load_scr(lua_State *L)
const char *str;
lua_getfield(L, 2, "anim");
str = lua_tostring(L, -1);
if (str == NULL || strcmp(str, "none") == 0) {
if (str == NULL || lv_strcmp(str, "none") == 0) {
; /* use default */
} else if (strcmp(str, "over_left") == 0) {
} else if (lv_strcmp(str, "over_left") == 0) {
anim = LV_SCR_LOAD_ANIM_OVER_LEFT;
} else if (strcmp(str, "over_right") == 0) {
} else if (lv_strcmp(str, "over_right") == 0) {
anim = LV_SCR_LOAD_ANIM_OVER_RIGHT;
} else if (strcmp(str, "over_top") == 0) {
} else if (lv_strcmp(str, "over_top") == 0) {
anim = LV_SCR_LOAD_ANIM_OVER_TOP;
} else if (strcmp(str, "over_botto") == 0) {
} else if (lv_strcmp(str, "over_botto") == 0) {
anim = LV_SCR_LOAD_ANIM_OVER_BOTTOM;
} else if (strcmp(str, "move_left") == 0) {
} else if (lv_strcmp(str, "move_left") == 0) {
anim = LV_SCR_LOAD_ANIM_MOVE_LEFT;
} else if (strcmp(str, "move_right") == 0) {
} else if (lv_strcmp(str, "move_right") == 0) {
anim = LV_SCR_LOAD_ANIM_MOVE_RIGHT;
} else if (strcmp(str, "move_top") == 0) {
} else if (lv_strcmp(str, "move_top") == 0) {
anim = LV_SCR_LOAD_ANIM_MOVE_TOP;
} else if (strcmp(str, "move_botto") == 0) {
} else if (lv_strcmp(str, "move_botto") == 0) {
anim = LV_SCR_LOAD_ANIM_MOVE_BOTTOM;
} else if (strcmp(str, "fade_on") == 0) {
} else if (lv_strcmp(str, "fade_on") == 0) {
anim = LV_SCR_LOAD_ANIM_FADE_ON;
#if LV_VERSION_CHECK(8, 3, 0)
} else if (strcmp(str, "fade_in") == 0) {
} else if (lv_strcmp(str, "fade_in") == 0) {
anim = LV_SCR_LOAD_ANIM_FADE_IN;
} else if (strcmp(str, "fade_out") == 0) {
} else if (lv_strcmp(str, "fade_out") == 0) {
anim = LV_SCR_LOAD_ANIM_FADE_OUT;
} else if (strcmp(str, "out_left") == 0) {
} else if (lv_strcmp(str, "out_left") == 0) {
anim = LV_SCR_LOAD_ANIM_OUT_LEFT;
} else if (strcmp(str, "out_right") == 0) {
} else if (lv_strcmp(str, "out_right") == 0) {
anim = LV_SCR_LOAD_ANIM_OUT_RIGHT;
} else if (strcmp(str, "out_top") == 0) {
} else if (lv_strcmp(str, "out_top") == 0) {
anim = LV_SCR_LOAD_ANIM_OUT_TOP;
} else if (strcmp(str, "out_bottom") == 0) {
} else if (lv_strcmp(str, "out_bottom") == 0) {
anim = LV_SCR_LOAD_ANIM_OUT_BOTTOM;
#endif
}
Expand Down
29 changes: 20 additions & 9 deletions src/font.c
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ static int luavgl_get_named_weight(const char *name)
}

for (int i = 0; i < _ARRAY_LEN(g_named_weight); i++) {
if (strcmp(name, g_named_weight[i].name) == 0) {
if (lv_strcmp(name, g_named_weight[i].name) == 0) {
return g_named_weight[i].value;
}
}
Expand All @@ -180,7 +180,7 @@ static const lv_font_t *_luavgl_font_create(lua_State *L, const char *name,
int size, int weight)
{
/* check builtin font firstly. */
if (strcmp(name, "montserrat") == 0) {
if (lv_strcmp(name, "montserrat") == 0) {
if (FONT_WEIGHT_NORMAL != weight)
return NULL;

Expand All @@ -189,7 +189,7 @@ static const lv_font_t *_luavgl_font_create(lua_State *L, const char *name,
return g_builtin_montserrat[i].font;
}
}
} else if (strcmp(name, "unscii") == 0) {
} else if (lv_strcmp(name, "unscii") == 0) {
if (FONT_WEIGHT_NORMAL != weight)
return NULL;
#if LV_FONT_UNSCII_8
Expand All @@ -203,14 +203,14 @@ static const lv_font_t *_luavgl_font_create(lua_State *L, const char *name,
#endif
}
#if LV_FONT_DEJAVU_16_PERSIAN_HEBREW
else if (strcmp(name, "dejavu_persian_hebrew") == 0) {
else if (lv_strcmp(name, "dejavu_persian_hebrew") == 0) {
if (size == 16)
return &lv_font_dejavu_16_persian_hebrew;
}
#endif

#if LV_FONT_SIMSUN_16_CJK
else if (strcmp(name, "dejavu_persian_hebrew") == 0) {
else if (lv_strcmp(name, "dejavu_persian_hebrew") == 0) {
if (size == 16)
return &lv_font_simsun_16_cjk;
}
Expand All @@ -225,6 +225,17 @@ static const lv_font_t *_luavgl_font_create(lua_State *L, const char *name,
return NULL;
}

static char *luavgl_strchr(const char *s, char c)
{
while (*s) {
if (c == *s) {
return (char *)s;
}
s++;
}
return NULL;
}

/**
* Dynamic font family fallback is not supported.
* The fallback only happen when font creation fails and continue to try next
Expand Down Expand Up @@ -254,7 +265,7 @@ static int luavgl_font_create(lua_State *L)
weight = lua_tointeger(L, 3);
} else {
char *luastr = (char *)lua_tostring(L, 3);
int len = strlen(luastr);
int len = lv_strlen(luastr);
if (len > 128) {
/* not likely to happen */
return luaL_argerror(L, 3, "too long");
Expand All @@ -271,7 +282,7 @@ static int luavgl_font_create(lua_State *L)
weight = FONT_WEIGHT_NORMAL;
}

str = strdup(lua_tostring(L, 1));
str = lv_strdup(lua_tostring(L, 1));
if (str == NULL) {
return luaL_error(L, "no memory");
}
Expand All @@ -283,11 +294,11 @@ static int luavgl_font_create(lua_State *L)
continue;
}

char *end = strchr(name, ',');
char *end = luavgl_strchr(name, ',');
if (end != NULL) {
*end = '\0';
} else {
end = name + strlen(name);
end = name + lv_strlen(name);
}

char *trim = end - 1;
Expand Down
4 changes: 2 additions & 2 deletions src/fs.c
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,10 @@ static int luavgl_fs_open(lua_State *L)
f->closed = false;

lv_fs_mode_t lmode = 0;
if (strchr(mode, 'r'))
if (luavgl_strchr(mode, 'r'))
lmode |= LV_FS_MODE_RD;

if (strchr(mode, 'w'))
if (luavgl_strchr(mode, 'w'))
lmode |= LV_FS_MODE_WR;

lv_fs_res_t res = lv_fs_open(&f->file, path, lmode);
Expand Down
2 changes: 1 addition & 1 deletion src/luavgl.c
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ LUALIB_API luavgl_ctx_t *luavgl_context(lua_State *L)
/* create it if not exist in registry */
lua_pushstring(L, luavglgl_key);
ctx = (luavgl_ctx_t *)lua_newuserdata(L, sizeof(*ctx));
memset(ctx, 0, sizeof(*ctx));
lv_memset(ctx, 0, sizeof(*ctx));
lua_rawset(L, LUA_REGISTRYINDEX);
} else {
ctx = (luavgl_ctx_t *)lua_touserdata(L, -1);
Expand Down
2 changes: 1 addition & 1 deletion src/obj.c
Original file line number Diff line number Diff line change
Expand Up @@ -866,7 +866,7 @@ LUALIB_API luavgl_obj_t *luavgl_add_lobj(lua_State *L, lv_obj_t *obj)

lua_setmetatable(L, -2);

memset(lobj, 0, sizeof(*lobj));
lv_memset(lobj, 0, sizeof(*lobj));
lobj->obj = obj;

/* Init event array to store events added from lua. */
Expand Down
1 change: 0 additions & 1 deletion src/private.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

#include <stdbool.h>
#include <stdint.h>
#include <stdlib.h>

static void dumpstack(lua_State *L);
static void dumptable(lua_State *L, int index);
Expand Down
48 changes: 34 additions & 14 deletions src/style.c
Original file line number Diff line number Diff line change
Expand Up @@ -198,27 +198,47 @@ static lv_flex_align_t luavgl_to_flex_align(lua_State *L, int idx)
return LV_FLEX_ALIGN_START;

const char *str = lua_tostring(L, idx);
if (strcmp("flex-start", str) == 0)
if (lv_strcmp("flex-start", str) == 0)
return LV_FLEX_ALIGN_START;

if (strcmp("flex-end", str) == 0)
if (lv_strcmp("flex-end", str) == 0)
return LV_FLEX_ALIGN_END;

if (strcmp("center", str) == 0)
if (lv_strcmp("center", str) == 0)
return LV_FLEX_ALIGN_CENTER;

if (strcmp("space-evenly", str) == 0)
if (lv_strcmp("space-evenly", str) == 0)
return LV_FLEX_ALIGN_SPACE_EVENLY;

if (strcmp("space-around", str) == 0)
if (lv_strcmp("space-around", str) == 0)
return LV_FLEX_ALIGN_SPACE_AROUND;

if (strcmp("space-between", str) == 0)
if (lv_strcmp("space-between", str) == 0)
return LV_FLEX_ALIGN_SPACE_BETWEEN;

return LV_FLEX_ALIGN_START;
}

static char *luavgl_strstr(const char *haystack, const char *needle) {
while (*haystack != '\0') {
const char *h = haystack;
const char *n = needle;

while (*n != '\0' && *h == *n) {
h++;
n++;
}

if (*n == '\0') {
return (char *)haystack;
}

haystack++;
}

return NULL;
}

static int luavgl_set_flex_layout_kv(lua_State *L, style_set_cb_t cb,
void *args)
{
Expand All @@ -239,14 +259,14 @@ static int luavgl_set_flex_layout_kv(lua_State *L, style_set_cb_t cb,
if (lua_type(L, -1) == LUA_TSTRING) {
str = lua_tostring(L, -1);
/* starts with */
if (strncmp("row", str, 3) == 0) {
if (lv_strcmp("row", str) == 0) {
flow = LV_FLEX_FLOW_ROW;
} else if (strncmp("column", str, 3) == 0) {
} else if (lv_strcmp("column", str) == 0) {
flow = LV_FLEX_FLOW_COLUMN;
}

/* if reverse presents */
if (strstr(str, "-reverse")) {
if (luavgl_strstr(str, "-reverse")) {
flow |= _LV_FLEX_REVERSE;
}
}
Expand All @@ -259,9 +279,9 @@ static int luavgl_set_flex_layout_kv(lua_State *L, style_set_cb_t cb,
lua_getfield(L, -1, "flex_wrap");
if (lua_type(L, -1) == LUA_TSTRING) {
str = lua_tostring(L, -1);
if (strcmp("wrap", str) == 0) {
if (lv_strcmp("wrap", str) == 0) {
flow |= _LV_FLEX_WRAP;
} else if (strcmp("wrap-reverse", str) == 0) {
} else if (lv_strcmp("wrap-reverse", str) == 0) {
flow |= _LV_FLEX_WRAP | _LV_FLEX_REVERSE;
}
/* else: normal */
Expand Down Expand Up @@ -321,7 +341,7 @@ static inline bool luavgl_is_style_inherit(lua_State *L)
{
const char *str;
return (lua_type(L, -1) == LUA_TSTRING) && (str = lua_tostring(L, -1)) &&
(strcmp(str, "inherit") == 0);
(lv_strcmp(str, "inherit") == 0);
}

/**
Expand All @@ -343,7 +363,7 @@ static int luavgl_set_style_kv(lua_State *L, style_set_cb_t cb, void *args)
lv_style_value_t value = {0};
const struct style_map_s *p = NULL;
for (int i = 0; i < STYLE_MAP_LEN; i++) {
if (strcmp(key, g_style_map[i].name) == 0) {
if (lv_strcmp(key, g_style_map[i].name) == 0) {
p = &g_style_map[i];
break;
}
Expand Down Expand Up @@ -548,7 +568,7 @@ static int luavgl_style_remove_prop(lua_State *L)

for (int i = 0; i < STYLE_MAP_LEN; i++) {
const struct style_map_s *p = &g_style_map[i];
if (strcmp(name, p->name) == 0) {
if (lv_strcmp(name, p->name) == 0) {
lv_style_remove_prop(&s->style, p->prop);
return 0;
}
Expand Down
4 changes: 2 additions & 2 deletions src/util.c
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,7 @@ LUALIB_API lv_color_t luavgl_tocolor(lua_State *L, int idx)
return color;
}

int len = strlen(s);
int len = lv_strlen(s);
if (len == 4 && s[0] == '#') {
/* #RGB */
int r = to_int(s[1]);
Expand Down Expand Up @@ -370,7 +370,7 @@ LUALIB_API int luavgl_set_property_array(lua_State *L, void *obj,

for (int i = 0; i < len; i++) {
const luavgl_value_setter_t *p = &table[i];
if (strcmp(key, p->key))
if (lv_strcmp(key, p->key))
continue;

if (p->type == SETTER_TYPE_INT) {
Expand Down
Loading

0 comments on commit b8eacea

Please sign in to comment.