Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/apps/launcher/launcher.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ static const char* const WALLPAPER_PATHS[] =
{"/sd/wallpaper.gif", "/sd/wallpaper.png", "/sd/wallpaper.jpg", "/sd/wallpaper.jpeg", "/sd/wallpaper.bmp"};

LauncherApp::LauncherApp() : App("Launcher"), autorunEnabled(ScriptService::getAutorunEnabled()) {
setktStackSize(8192); // Yeah, this one is heavy as fuck
setktStackSize(16384); // Heavy run() frame plus Lua wallpaper modules (http, fs, socket)
}

void LauncherApp::run() {
Expand Down
40 changes: 40 additions & 0 deletions src/apps/lua/luawallpaper.cpp
Original file line number Diff line number Diff line change
@@ -1,12 +1,22 @@
#include "luawallpaper.h"

#include <stdio.h>
#include <unistd.h>
#include <lilka.h>
#include "luarunner.h"
#include "lualilka_display.h"
#include "lualilka_resources.h"
#include "lualilka_math.h"
#include "lualilka_geometry.h"
#include "lualilka_ws2812.h"
#include "lualilka_sdcard.h"
#include "lualilka_fs.h"
#include "lualilka_wifi.h"
#include "lualilka_http.h"
#include "lualilka_audio.h"
#include "lualilka_mqtt.h"
#include "lualilka_socket.h"
#include "lualilka_state.h"
#include "keira/ksound/sound.h"

LuaWallpaper::~LuaWallpaper() {
Expand Down Expand Up @@ -46,6 +56,15 @@ bool LuaWallpaper::open(const char* path, App* app) {
lualilka_resources_register(L);
lualilka_math_register(L);
lualilka_geometry_register(L);
lualilka_ws2812_register(L);
lualilka_sdcard_register(L);
lualilka_fs_register(L);
lualilka_wifi_register(L);
lualilka_http_register(L);
lualilka_audio_register(L);
lualilka_mqtt_register(L);
lualilka_socket_register(L);
lualilka_state_register(L);

lua_newtable(L);
lua_setfield(L, LUA_REGISTRYINDEX, "images");
Expand All @@ -55,6 +74,14 @@ bool LuaWallpaper::open(const char* path, App* app) {
lua_newtable(L);
lua_setglobal(L, "lilka");

// State is kept next to the script, same as for Lua apps
String statePath = String(path).substring(0, String(path).lastIndexOf('.')) + ".state";
lua_pushstring(L, statePath.c_str());
lua_setfield(L, LUA_REGISTRYINDEX, "state_path");
if (access(statePath.c_str(), F_OK) != -1) {
lualilka_state_load(L, statePath.c_str());
}

if (luaL_loadfile(L, path) != LUA_OK || lua_pcall(L, 0, 0, 0) != LUA_OK) {
lilka::serial.err("Lua wallpaper: %s", lua_tostring(L, -1));
close();
Expand All @@ -75,6 +102,17 @@ bool LuaWallpaper::open(const char* path, App* app) {
void LuaWallpaper::close() {
if (L == nullptr) return;

// Save state table if the script has one
lua_getglobal(L, "state");
bool hasState = lua_istable(L, -1);
lua_pop(L, 1);
if (hasState) {
lua_getfield(L, LUA_REGISTRYINDEX, "state_path");
String statePath = lua_tostring(L, -1);
lua_pop(L, 1);
lualilka_state_save(L, statePath.c_str());
}

// Free images and sounds loaded by the script
lua_getfield(L, LUA_REGISTRYINDEX, "images");
lua_pushnil(L);
Expand All @@ -84,6 +122,8 @@ void LuaWallpaper::close() {
}
lua_pop(L, 1);

// Stop audio playback before freeing sounds
lualilka_audio_cleanup();
lua_getfield(L, LUA_REGISTRYINDEX, "sounds");
lua_pushnil(L);
while (lua_next(L, -2) != 0) {
Expand Down
3 changes: 2 additions & 1 deletion src/apps/lua/luawallpaper.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@

// Home screen wallpaper driven by a Lua script.
// Script uses the same lilka.init() / lilka.update(delta) / lilka.draw() callbacks as regular Lua apps,
// but only has access to display, resources, math and geometry modules.
// but only has access to display, resources, math, geometry, ws2812, sdcard, fs, wifi, http, audio, mqtt,
// socket and state modules. State is loaded on open and saved on close, same as for Lua apps.
// Buttons are not available to the script - they are handled by the launcher.
class LuaWallpaper {
public:
Expand Down
Loading