From 8375304d618384d5e8e0c78940df92050a896349 Mon Sep 17 00:00:00 2001 From: Damjan Adamic Date: Tue, 21 Mar 2017 07:42:00 +0100 Subject: [PATCH] Several fixes for Lua Widgets and Themes options (#4648) --- radio/src/gui/480x272/widget.h | 1 + radio/src/lua/widgets.cpp | 24 +++++++++++++++++++----- 2 files changed, 20 insertions(+), 5 deletions(-) diff --git a/radio/src/gui/480x272/widget.h b/radio/src/gui/480x272/widget.h index d13b79d7d..d5a14156e 100644 --- a/radio/src/gui/480x272/widget.h +++ b/radio/src/gui/480x272/widget.h @@ -106,6 +106,7 @@ class WidgetFactory if (options) { int i = 0; for (const ZoneOption * option = options; option->name; option++) { + TRACE("WidgetFactory::initPersistentData() setting option '%s'", option->name); // TODO compiler bug? The CPU freezes ... persistentData->options[i++] = option->deflt; memcpy(&persistentData->options[i++], &option->deflt, sizeof(ZoneOptionValue)); } diff --git a/radio/src/lua/widgets.cpp b/radio/src/lua/widgets.cpp index 5f9d30a28..fcedeaefd 100644 --- a/radio/src/lua/widgets.cpp +++ b/radio/src/lua/widgets.cpp @@ -47,9 +47,10 @@ void exec(int function, int nresults=0) } } -ZoneOption * createOptionsArray(int reference) +ZoneOption * createOptionsArray(int reference, uint8_t maxOptions) { if (reference == 0) { + // TRACE("createOptionsArray() no options"); return NULL; } @@ -59,6 +60,12 @@ ZoneOption * createOptionsArray(int reference) count++; } + // TRACE("we have %d options", count); + if (count > maxOptions) { + count = maxOptions; + // TRACE("limited to %d options", count); + } + ZoneOption * options = (ZoneOption *)malloc(sizeof(ZoneOption) * (count+1)); if (!options) { return NULL; @@ -67,7 +74,8 @@ ZoneOption * createOptionsArray(int reference) PROTECT_LUA() { lua_rawgeti(lsWidgets, LUA_REGISTRYINDEX, reference); ZoneOption * option = options; - for (lua_pushnil(lsWidgets); lua_next(lsWidgets, -2); lua_pop(lsWidgets, 1)) { + for (lua_pushnil(lsWidgets); lua_next(lsWidgets, -2), count-- > 0; lua_pop(lsWidgets, 1)) { + // TRACE("parsing option %d", count); luaL_checktype(lsWidgets, -2, LUA_TNUMBER); // key is number luaL_checktype(lsWidgets, -1, LUA_TTABLE); // value is table uint8_t field = 0; @@ -77,11 +85,13 @@ ZoneOption * createOptionsArray(int reference) luaL_checktype(lsWidgets, -2, LUA_TNUMBER); // key is number luaL_checktype(lsWidgets, -1, LUA_TSTRING); // value is string option->name = lua_tostring(lsWidgets, -1); + // TRACE("name = %s", option->name); break; case 1: luaL_checktype(lsWidgets, -2, LUA_TNUMBER); // key is number luaL_checktype(lsWidgets, -1, LUA_TNUMBER); // value is number option->type = (ZoneOption::Type)lua_tointeger(lsWidgets, -1); + // TRACE("type = %d", option->type); if (option->type > ZoneOption::Color) { // wrong type option->type = ZoneOption::Integer; @@ -98,6 +108,7 @@ ZoneOption * createOptionsArray(int reference) if (option->type == ZoneOption::Integer) { luaL_checktype(lsWidgets, -1, LUA_TNUMBER); // value is number option->deflt.signedValue = lua_tointeger(lsWidgets, -1); + // TRACE("default signed = %d", option->deflt.signedValue); } else if (option->type == ZoneOption::Source || option->type == ZoneOption::TextSize || @@ -105,13 +116,16 @@ ZoneOption * createOptionsArray(int reference) option->type == ZoneOption::Color) { luaL_checktype(lsWidgets, -1, LUA_TNUMBER); // value is number option->deflt.unsignedValue = lua_tounsigned(lsWidgets, -1); + // TRACE("default unsigned = %u", option->deflt.unsignedValue); } else if (option->type == ZoneOption::Bool) { luaL_checktype(lsWidgets, -1, LUA_TNUMBER); // value is number option->deflt.boolValue = (lua_tounsigned(lsWidgets, -1) != 0); + // TRACE("default bool = %d", (int)(option->deflt.boolValue)); } else if (option->type == ZoneOption::String) { - strncpy(option->deflt.stringValue, lua_tostring(lsWidgets, -1), sizeof(option->deflt.stringValue)-1); + str2zchar(option->deflt.stringValue, lua_tostring(lsWidgets, -1), sizeof(option->deflt.stringValue)); // stringValue is ZCHAR + // TRACE("default string = %s", lua_tostring(lsWidgets, -1)); } break; case 3: @@ -219,7 +233,7 @@ void luaLoadThemeCallback() } if (name) { - ZoneOption * options = createOptionsArray(themeOptions); + ZoneOption * options = createOptionsArray(themeOptions, MAX_THEME_OPTIONS); if (options) { LuaTheme * theme = new LuaTheme(name, options); theme->loadFunction = loadFunction; @@ -426,7 +440,7 @@ void luaLoadWidgetCallback() } if (name && createFunction) { - ZoneOption * options = createOptionsArray(widgetOptions); + ZoneOption * options = createOptionsArray(widgetOptions, MAX_WIDGET_OPTIONS); if (options) { LuaWidgetFactory * factory = new LuaWidgetFactory(name, options, createFunction); factory->updateFunction = updateFunction;