summaryrefslogtreecommitdiff
path: root/lib/luavgl/src/luavgl.c
blob: f0f388278db0279d1ca401e1dc98e7a06c1f8c60 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
#include "luavgl.h"
#include "private.h"

#include "anim.c"
#include "constants.c"
#include "disp.c"
#include "font.c"
#include "fs.c"
#include "group.c"
#include "indev.c"
#include "obj.c"
#include "timer.c"
#include "util.c"

static const struct luaL_Reg luavgl_methods[] = {
    {"Timer", luavgl_timer_create}, /* timer.c */
    {"Font",  luavgl_font_create }, /* font.c */
    {"Style", luavgl_style_create}, /* style.c */
    {"Anim",  luavgl_anim_create }, /* anim.c */
    {"ImgData",  luavgl_imgdata_create}, /* imgdata.c */

    {NULL,    NULL               },
};

LUALIB_API luavgl_ctx_t *luavgl_context(lua_State *L)
{
  static const char *luavglgl_key = "luavgl_key";

  luavgl_ctx_t *ctx;
  lua_pushstring(L, luavglgl_key);
  lua_rawget(L, LUA_REGISTRYINDEX);

  /**
   * ctx = registry[luavgl_key]
   * if ctx == nil then
   *    ctx = new ctx
   * else
   *    return ctx
   * end
   */

  if (lua_isnil(L, -1)) {
    /* create it if not exist in registry */
    lua_pushstring(L, luavglgl_key);
    ctx = (luavgl_ctx_t *)lua_newuserdata(L, sizeof(*ctx));
    lv_memset(ctx, 0, sizeof(*ctx));
    lua_rawset(L, LUA_REGISTRYINDEX);
  } else {
    ctx = (luavgl_ctx_t *)lua_touserdata(L, -1);
  }

  lua_pop(L, 1);
  return ctx;
}

LUALIB_API void luavgl_set_pcall(lua_State *L, luavgl_pcall_t pcall)
{
  luavgl_ctx_t *ctx = luavgl_context(L);
  ctx->pcall = pcall;
}

LUALIB_API void luavgl_set_root(lua_State *L, lv_obj_t *root)
{
  luavgl_ctx_t *ctx = luavgl_context(L);
  ctx->root = root;
}

LUALIB_API void luavgl_set_font_extension(lua_State *L, make_font_cb make,
                                          delete_font_cb d)
{
  luavgl_ctx_t *ctx = luavgl_context(L);
  ctx->make_font = make;
  ctx->delete_font = d;
}

static int root_gc(lua_State *L)
{
  LV_LOG_INFO("enter");
  luavgl_ctx_t *ctx = luavgl_context(L);
  lv_obj_del(ctx->root);
  return 0;
}

static int root_clean(lua_State *L)
{
  LV_LOG_INFO("enter");
  luavgl_ctx_t *ctx = luavgl_context(L);
  lv_obj_clean(ctx->root);
  return 0;
}

LUALIB_API int luaopen_lvgl(lua_State *L)
{
  luavgl_ctx_t *ctx = luavgl_context(L);

  luaL_newlib(L, luavgl_methods);

  luaL_newmetatable(L, "root.meta");
  lua_pushstring(L, "__gc");
  lua_pushcfunction(L, ctx->root ? root_clean : root_gc);
  lua_settable(L, -3);
  lua_pop(L, 1);

  lv_obj_t *root = ctx->root;
  if (root == NULL) {
    LV_LOG_INFO("create root obj for lua");
    root = lv_obj_create(lv_scr_act());
    ctx->root = root;
  }

  if (ctx->pcall == NULL)
    ctx->pcall = luavgl_pcall;

  lua_pushstring(L, "_root");
  *(void **)lua_newuserdata(L, sizeof(void *)) = root;
  luaL_getmetatable(L, "root.meta");
  lua_setmetatable(L, -2);

  /*
   * create a ref to root, avoid __gc early
   * this puts the root userdata into the _root key
   * in the returned luv table
   * */
  lua_rawset(L, -3);

  lv_obj_remove_style_all(root);
  lv_obj_set_size(root, LV_HOR_RES, LV_VER_RES);
  lv_obj_clear_flag(root, LV_OBJ_FLAG_CLICKABLE | LV_OBJ_FLAG_SCROLLABLE);

  luavgl_obj_init(L);
  luavgl_widgets_init(L);
  luavgl_anim_init(L);
  luavgl_timer_init(L);
  luavgl_style_init(L);
  luavgl_fs_init(L);
  luavgl_indev_init(L);
  luavgl_group_init(L);
  luavgl_disp_init(L);

  luavgl_constants_init(L);

  /* Methods to create widget locate in widgets table, check `luavgl_obj_init`
   */
  luaL_getmetatable(L, "widgets");
  lua_setmetatable(L, -2);

  (void)dumpstack;
  (void)dumptable;
  (void)dumpvalue;
  return 1;
}