From 17a77114d0a5dd3eb5a628e75e0787e1e0408690 Mon Sep 17 00:00:00 2001 From: Damjan Adamic Date: Sat, 4 Feb 2017 15:35:00 +0100 Subject: [PATCH] Re #3882: parse theme/widget options depending on option type (#4391) --- radio/src/lua/widgets.cpp | 46 ++++++++++++++++++++++++++++++++------- 1 file changed, 38 insertions(+), 8 deletions(-) diff --git a/radio/src/lua/widgets.cpp b/radio/src/lua/widgets.cpp index 4ae351021..f7142c1cd 100644 --- a/radio/src/lua/widgets.cpp +++ b/radio/src/lua/widgets.cpp @@ -82,21 +82,51 @@ ZoneOption * createOptionsArray(int reference) 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); + if (option->type > ZoneOption::Color) { + // wrong type + option->type = ZoneOption::Integer; + } + if (option->type == ZoneOption::Integer) { + // set some sensible defaults (only Integer actually uses them) + option->deflt.signedValue = 0; + option->min.signedValue = -100; + option->max.signedValue = 100; + } break; case 2: luaL_checktype(lsWidgets, -2, LUA_TNUMBER); // key is number - luaL_checktype(lsWidgets, -1, LUA_TNUMBER); // value is number - option->deflt.signedValue = lua_tointeger(lsWidgets, -1); + if (option->type == ZoneOption::Integer) { + luaL_checktype(lsWidgets, -1, LUA_TNUMBER); // value is number + option->deflt.signedValue = lua_tointeger(lsWidgets, -1); + } + else if (option->type == ZoneOption::Source || + option->type == ZoneOption::TextSize || + option->type == ZoneOption::Source || + option->type == ZoneOption::Color) { + luaL_checktype(lsWidgets, -1, LUA_TNUMBER); // value is number + option->deflt.unsignedValue = lua_tounsigned(lsWidgets, -1); + } + else if (option->type == ZoneOption::Bool) { + luaL_checktype(lsWidgets, -1, LUA_TNUMBER); // value is number + option->deflt.boolValue = (lua_tounsigned(lsWidgets, -1) != 0); + } + else if (option->type == ZoneOption::String) { + strncpy(option->deflt.stringValue, lua_tostring(lsWidgets, -1), sizeof(option->deflt.stringValue)-1); + } break; case 3: - luaL_checktype(lsWidgets, -2, LUA_TNUMBER); // key is number - luaL_checktype(lsWidgets, -1, LUA_TNUMBER); // value is number - option->min.signedValue = lua_tointeger(lsWidgets, -1); + if (option->type == ZoneOption::Integer) { + luaL_checktype(lsWidgets, -2, LUA_TNUMBER); // key is number + luaL_checktype(lsWidgets, -1, LUA_TNUMBER); // value is number + option->min.signedValue = lua_tointeger(lsWidgets, -1); + } break; case 4: - luaL_checktype(lsWidgets, -2, LUA_TNUMBER); // key is number - luaL_checktype(lsWidgets, -1, LUA_TNUMBER); // value is number - option->max.signedValue = lua_tointeger(lsWidgets, -1); + if (option->type == ZoneOption::Integer) { + luaL_checktype(lsWidgets, -2, LUA_TNUMBER); // key is number + luaL_checktype(lsWidgets, -1, LUA_TNUMBER); // value is number + option->max.signedValue = lua_tointeger(lsWidgets, -1); + } break; } }