Commit caeb6002 authored by alex@thinkpad's avatar alex@thinkpad
Browse files

Lua: fix error checking in menu.get (also API test)

--HG--
branch : lua_fix
parent b63c9057a0c0
......@@ -187,7 +187,7 @@ static int get_index_for_choices(struct menu_entry * menu_entry, const char * va
// @tparam string menu name of the parent menu ('Audio', 'Expo', 'Overlay', 'Shoot', 'Movie', etc)
// @tparam string entry name of the menu entry
// @tparam[opt] string as_string pass empty string "" to get the result as string (default is int)
// @treturn ?int|string the value of the menu entry (current selection)
// @treturn ?int|string|nil the current value of the requested menu entry (nil if menu entry not found)
// @function get
static int luaCB_menu_get(lua_State * L)
{
......@@ -199,13 +199,14 @@ static int luaCB_menu_get(lua_State * L)
{
struct menu_display_info info;
char * str = menu_get_str_value_from_script(menu, entry, &info);
if (!str) return luaL_error(L, "menu not found");
lua_pushstring(L, str);
if (!str) lua_pushnil(L);
else lua_pushstring(L, str);
}
else
{
/* fixme: error checking is not done */
lua_pushinteger(L, menu_get_value_from_script(menu, entry));
int val = menu_get_value_from_script(menu, entry);
if (val == INT_MIN) lua_pushnil(L);
else lua_pushinteger(L, val);
}
return 1;
}
......@@ -214,24 +215,30 @@ static int luaCB_menu_get(lua_State * L)
// @tparam string menu name of the parent menu ('Audio', 'Expo', 'Overlay', 'Shoot', 'Movie', etc).
// @tparam string entry name of the menu entry.
// @tparam ?int|string value the value to set.
// @treturn bool whether or not the call was sucessful.
// @treturn ?bool|nil whether or not the call was sucessful, or nil if the requested menu entry was not found.
// @function set
static int luaCB_menu_set(lua_State * L)
{
LUA_PARAM_STRING(menu, 1);
LUA_PARAM_STRING(entry, 2);
int result = INT_MIN;
if(lua_isinteger(L, 3))
{
LUA_PARAM_INT(value, 3);
lua_pushboolean(L, menu_set_value_from_script(menu, entry, value));
result = menu_set_value_from_script(menu, entry, value);
lua_pushboolean(L, result);
}
else
{
LUA_PARAM_STRING(value, 3);
char * copy = copy_string(value);
lua_pushboolean(L, menu_set_str_value_from_script(menu, entry, copy, INT_MIN));
result = menu_set_str_value_from_script(menu, entry, copy, INT_MIN);
free(copy);
}
if (result == INT_MIN) lua_pushnil(L);
else lua_pushboolean(L, result);
return 1;
}
......
......@@ -257,7 +257,7 @@ function test_menu()
assert(menu.set("Expo", "Picture Style", "Landscape"))
assert(menu.get("Expo", "Picture Style", "") == "Landscape")
msleep(1000)
assert(not menu.set("Expo", "Picture Style", 1234)) -- should fail, out of range
assert(menu.set("Expo", "Picture Style", 1234) == false) -- should fail, out of range
assert(menu.get("Expo", "Picture Style", "") == "Landscape") -- old selection should not change
msleep(1000)
......@@ -295,7 +295,7 @@ function test_menu()
-- enter the submenu
-- TODO: menu.select should handle submenus as well
assert(not menu.select("Intervalometer", "Take a pic every"))
assert(menu.select("Intervalometer", "Take a pic every") == false)
key.press(KEY.Q)
msleep(1000)
......@@ -322,7 +322,7 @@ function test_menu()
msleep(1000)
-- out of range, should fail
assert(not menu.set("Intervalometer", "Take a pic every", 7000000))
assert(menu.set("Intervalometer", "Take a pic every", 7000000) == false)
assert(menu.get("Intervalometer", "Take a pic every") == 70)
assert(menu.get("Intervalometer", "Take a pic every", "") == "1m10s")
msleep(1000)
......@@ -332,8 +332,14 @@ function test_menu()
msleep(1000)
-- non-existent menus; should fail
assert(not menu.select("Dinosaur"))
assert(not menu.select("Shoot", "Crocodile"))
assert(menu.select("Dinosaur") == false)
assert(menu.select("Shoot", "Crocodile") == false)
-- menu.get/set return nil if the menu was not found
assert(menu.get("Shoot", "Introvolometer") == nil)
assert(menu.get("Shoot", "Brack", "") == nil)
assert(menu.set("Shoot", "Introvolometer", 1) == nil)
assert(menu.set("Shoot", "Introvolometer", "OFF") == nil)
menu.close()
......
......@@ -5932,7 +5932,11 @@ end:
int menu_get_value_from_script(const char* name, const char* entry_name)
{
struct menu_entry * entry = entry_find_by_name(name, entry_name);
if (!entry) { printf("Menu not found: %s -> %s\n", name, entry_name); return 0; }
if (!entry)
{
printf("Menu not found: %s -> %s\n", name, entry_name);
return INT_MIN;
}
return CURRENT_VALUE;
}
......@@ -5941,7 +5945,11 @@ int menu_get_value_from_script(const char* name, const char* entry_name)
static char* menu_get_str_value_from_script_do(const char* name, const char* entry_name, struct menu_display_info * info)
{
struct menu_entry * entry = entry_find_by_name(name, entry_name);
if (!entry) { printf("Menu not found: %s -> %s\n", name, entry_name); return 0; }
if (!entry)
{
printf("Menu not found: %s -> %s\n", name, entry_name);
return NULL;
}
/* not thread-safe; must be guarded by menu_sem */
entry_default_display_info(entry, info);
......@@ -5961,7 +5969,11 @@ char* menu_get_str_value_from_script(const char* name, const char* entry_name, s
int menu_set_str_value_from_script(const char* name, const char* entry_name, char* value, int value_int)
{
struct menu_entry * entry = entry_find_by_name(name, entry_name);
if (!entry) { printf("Menu not found: %s -> %s\n", name, entry_name); return 0; }
if (!entry)
{
printf("Menu not found: %s -> %s\n", name, entry_name);
return INT_MIN;
}
/* if the menu item has multiple choices defined,
* or just a valid min/max range, it's easy */
......@@ -6117,7 +6129,11 @@ ok:
int menu_set_value_from_script(const char* name, const char* entry_name, int value)
{
struct menu_entry * entry = entry_find_by_name(name, entry_name);
if (!entry) { printf("Menu not found: %s -> %s\n", name, entry_name); return 0; }
if (!entry)
{
printf("Menu not found: %s -> %s\n", name, entry_name);
return INT_MIN;
}
if( entry->select ) // special item, we need some heuristics
{
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment