summaryrefslogtreecommitdiff
path: root/src/tangara/lua/bridge.cpp
blob: 0f1e65acb4d3f7445e33b778e727d211b83e5023 (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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
/*
 * Copyright 2023 jacqueline <me@jacqueline.id.au>
 *
 * SPDX-License-Identifier: GPL-3.0-only
 */

#include "lua/bridge.hpp"
#include <stdint.h>
#include <stdio.h>
#include <sys/unistd.h>

#include <cstdint>
#include <memory>
#include <string>

#include "database/database.hpp"
#include "database/index.hpp"
#include "esp_heap_caps.h"
#include "esp_log.h"
#include "font/lv_binfont_loader.h"
#include "lauxlib.h"
#include "lua.h"
#include "lua.hpp"
#include "lua/lua_controls.hpp"
#include "lua/lua_database.hpp"
#include "lua/lua_filesystem.hpp"
#include "lua/lua_queue.hpp"
#include "lua/lua_screen.hpp"
#include "lua/lua_testing.hpp"
#include "lua/lua_theme.hpp"
#include "lua/lua_version.hpp"
#include "lvgl.h"

#include "luavgl.h"

#include "events/event_queue.hpp"
#include "lua/property.hpp"
#include "misc/lv_fs.h"
#include "system_fsm/service_locator.hpp"
#include "ui/ui_events.hpp"

extern "C" {
int luaopen_linenoise(lua_State* L);
int luaopen_term_core(lua_State* L);
}

namespace lua {

[[maybe_unused]] static constexpr char kTag[] = "lua_bridge";

static constexpr char kBridgeKey[] = "bridge";

static auto make_font_cb(const char* name) -> const lv_font_t* {
  // Most Lua file paths start with "//" in order to deal with LVGL's Windows-y
  // approach to paths. Try to handle such paths correctly so that paths in Lua
  // code look a bit more consistent.
  {
    std::string name_str = name;
    if (name_str.starts_with("//")) {
      name++;
    }
  }

  // This following is a bit C-brained. Sorry.

  ESP_LOGI(kTag, "load font '%s'", name);
  FILE* f = fopen(name, "r");
  if (!f) {
    return NULL;
  }

  uint8_t* data = NULL;
  long len = 0;
  lv_font_t* font = NULL;

  if (fseek(f, 0, SEEK_END)) {
    goto fail_with_file;
  }

  len = ftell(f);
  if (len <= 0) {
    goto fail_with_file;
  }

  if (fseek(f, 0, SEEK_SET)) {
    goto fail_with_file;
  }

  data = reinterpret_cast<uint8_t*>(heap_caps_malloc(len, MALLOC_CAP_SPIRAM));
  if (!data) {
    goto fail_with_buffer;
  }

  if (fread(data, 1, len, f) < len) {
    goto fail_with_buffer;
  }

  // We can finally start parsing the font!
  font = lv_binfont_create_from_buffer(data, len);

fail_with_buffer:
  // LVGL copies the font data out of the buffer, so we don't need to big raw
  // data alloc after this function returns.
  heap_caps_free(data);

fail_with_file:
  fclose(f);

  return font;
}

static auto delete_font_cb(const lv_font_t* font) -> void {
  // FIXME: luavgl never actually calls this?
}

auto Bridge::Get(lua_State* state) -> Bridge* {
  lua_pushstring(state, kBridgeKey);
  lua_gettable(state, LUA_REGISTRYINDEX);
  return reinterpret_cast<Bridge*>(lua_touserdata(state, -1));
}

Bridge::Bridge(system_fsm::ServiceLocator& services) : services_(services) {}

auto Bridge::installBaseModules(lua_State* L) -> void {
  lua_pushstring(L, kBridgeKey);
  lua_pushlightuserdata(L, this);
  lua_settable(L, LUA_REGISTRYINDEX);

  bindings_.install(L);

  luaL_requiref(L, "linenoise", luaopen_linenoise, true);
  lua_pop(L, 1);

  luaL_requiref(L, "term.core", luaopen_term_core, true);
  lua_pop(L, 1);

  RegisterControlsModule(L);
  RegisterDatabaseModule(L);
  RegisterQueueModule(L);
  RegisterTestingModule(L);
  RegisterFileSystemModule(L);
  RegisterVersionModule(L);
  RegisterThemeModule(L);
  RegisterScreenModule(L);
}

auto Bridge::installLvgl(lua_State* L) -> void {
  luavgl_set_pcall(L, CallProtected);
  luavgl_set_font_extension(L, make_font_cb, delete_font_cb);
  luaL_requiref(L, "lvgl", luaopen_lvgl, true);
  lua_pop(L, 1);
}

static auto new_property_module(lua_State* state) -> int {
  const char* name = luaL_checkstring(state, 1);
  luaL_newmetatable(state, name);

  lua_pushstring(state, "__index");
  lua_pushvalue(state, -2);
  lua_settable(state, -3);  // metatable.__index = metatable

  return 1;
}

template <class... Ts>
inline constexpr bool always_false_v = false;

auto Bridge::installPropertyModule(
    lua_State* L,
    const std::string& name,
    std::vector<std::pair<std::string, std::variant<LuaFunction, Property*>>>&
        props) -> void {
  // Create the module (or retrieve it if one with this name already exists)
  luaL_requiref(L, name.c_str(), new_property_module, true);

  for (const auto& prop : props) {
    lua_pushstring(L, prop.first.c_str());
    std::visit(
        [&](auto&& arg) {
          using T = std::decay_t<decltype(arg)>;
          if constexpr (std::is_same_v<T, LuaFunction>) {
            bindings_.Register(L, arg);
          } else if constexpr (std::is_same_v<T, Property*>) {
            bindings_.Register(L, arg);
          } else {
            static_assert(always_false_v<T>, "missing case");
          }
        },
        prop.second);

    lua_settable(L, -3);  // metatable.propname = property
  }

  lua_pop(L, 1);  // pop the module off the stack
}

}  // namespace lua