Lua: fix some issues with rocklib

git-svn-id: svn://svn.rockbox.org/rockbox/trunk@21022 a1c6a512-1295-4272-9138-f99709370657
This commit is contained in:
Maurus Cuelenaere 2009-05-21 19:42:14 +00:00
parent 7a04a54c52
commit a9b2d1b5fa

View file

@ -28,6 +28,16 @@
#include "lauxlib.h" #include "lauxlib.h"
#include "rocklib.h" #include "rocklib.h"
/*
* http://www.lua.org/manual/5.1/manual.html#lua_CFunction
*
* In order to communicate properly with Lua, a C function must use the following protocol,
* which defines the way parameters and results are passed: a C function receives its arguments
* from Lua in its stack in direct order (the first argument is pushed first). To return values to Lua,
* a C function just pushes them onto the stack, in direct order (the first result is pushed first),
* and returns the number of results. Any other value in the stack below the results will be properly
* discarded by Lua. Like a Lua function, a C function called by Lua can also return many results.
*/
#define RB_WRAP(M) static int rock_##M(lua_State *L) #define RB_WRAP(M) static int rock_##M(lua_State *L)
RB_WRAP(splash) RB_WRAP(splash)
@ -35,21 +45,21 @@ RB_WRAP(splash)
int ticks = luaL_checkint(L, 1); int ticks = luaL_checkint(L, 1);
const char *s = luaL_checkstring(L, 2); const char *s = luaL_checkstring(L, 2);
rb->splash(ticks, s); rb->splash(ticks, s);
return 1; return 0;
} }
RB_WRAP(lcd_update) RB_WRAP(lcd_update)
{ {
(void)L; (void)L;
rb->lcd_update(); rb->lcd_update();
return 1; return 0;
} }
RB_WRAP(lcd_clear_display) RB_WRAP(lcd_clear_display)
{ {
(void)L; (void)L;
rb->lcd_clear_display(); rb->lcd_clear_display();
return 1; return 0;
} }
RB_WRAP(lcd_putsxy) RB_WRAP(lcd_putsxy)
@ -58,7 +68,7 @@ RB_WRAP(lcd_putsxy)
int y = luaL_checkint(L, 2); int y = luaL_checkint(L, 2);
const char* string = luaL_checkstring(L, 3); const char* string = luaL_checkstring(L, 3);
rb->lcd_putsxy(x, y, string); rb->lcd_putsxy(x, y, string);
return 1; return 0;
} }
RB_WRAP(lcd_puts) RB_WRAP(lcd_puts)
@ -67,7 +77,7 @@ RB_WRAP(lcd_puts)
int y = luaL_checkint(L, 2); int y = luaL_checkint(L, 2);
const char* string = luaL_checkstring(L, 3); const char* string = luaL_checkstring(L, 3);
rb->lcd_puts(x, y, string); rb->lcd_puts(x, y, string);
return 1; return 0;
} }
RB_WRAP(lcd_puts_scroll) RB_WRAP(lcd_puts_scroll)
@ -76,14 +86,14 @@ RB_WRAP(lcd_puts_scroll)
int y = luaL_checkint(L, 2); int y = luaL_checkint(L, 2);
const char* string = luaL_checkstring(L, 3); const char* string = luaL_checkstring(L, 3);
rb->lcd_puts_scroll(x, y, string); rb->lcd_puts_scroll(x, y, string);
return 1; return 0;
} }
RB_WRAP(lcd_stop_scroll) RB_WRAP(lcd_stop_scroll)
{ {
(void)L; (void)L;
rb->lcd_stop_scroll(); rb->lcd_stop_scroll();
return 1; return 0;
} }
#ifdef HAVE_LCD_BITMAP #ifdef HAVE_LCD_BITMAP
@ -91,7 +101,7 @@ RB_WRAP(lcd_set_drawmode)
{ {
int drawmode = luaL_checkint(L, 1); int drawmode = luaL_checkint(L, 1);
rb->lcd_set_drawmode(drawmode); rb->lcd_set_drawmode(drawmode);
return 1; return 0;
} }
RB_WRAP(lcd_get_drawmode) RB_WRAP(lcd_get_drawmode)
@ -105,7 +115,7 @@ RB_WRAP(lcd_setfont)
{ {
int font = luaL_checkint(L, 1); int font = luaL_checkint(L, 1);
rb->lcd_setfont(font); rb->lcd_setfont(font);
return 1; return 0;
} }
RB_WRAP(lcd_drawpixel) RB_WRAP(lcd_drawpixel)
@ -114,8 +124,7 @@ RB_WRAP(lcd_drawpixel)
int y = luaL_checkint(L, 2); int y = luaL_checkint(L, 2);
rb->lcd_drawpixel(x, y); rb->lcd_drawpixel(x, y);
return 0;
return 1;
} }
RB_WRAP(lcd_drawline) RB_WRAP(lcd_drawline)
@ -126,8 +135,7 @@ RB_WRAP(lcd_drawline)
int y2 = luaL_checkint(L, 4); int y2 = luaL_checkint(L, 4);
rb->lcd_drawline(x1, y1, x2, y2); rb->lcd_drawline(x1, y1, x2, y2);
return 0;
return 1;
} }
RB_WRAP(lcd_hline) RB_WRAP(lcd_hline)
@ -137,8 +145,7 @@ RB_WRAP(lcd_hline)
int y = luaL_checkint(L, 3); int y = luaL_checkint(L, 3);
rb->lcd_hline(x1, x2, y); rb->lcd_hline(x1, x2, y);
return 0;
return 1;
} }
RB_WRAP(lcd_vline) RB_WRAP(lcd_vline)
@ -148,8 +155,7 @@ RB_WRAP(lcd_vline)
int y2 = luaL_checkint(L, 3); int y2 = luaL_checkint(L, 3);
rb->lcd_vline(x, y1, y2); rb->lcd_vline(x, y1, y2);
return 0;
return 1;
} }
RB_WRAP(lcd_drawrect) RB_WRAP(lcd_drawrect)
@ -160,8 +166,7 @@ RB_WRAP(lcd_drawrect)
int height = luaL_checkint(L, 4); int height = luaL_checkint(L, 4);
rb->lcd_drawrect(x, y, width, height); rb->lcd_drawrect(x, y, width, height);
return 0;
return 1;
} }
RB_WRAP(lcd_fillrect) RB_WRAP(lcd_fillrect)
@ -172,8 +177,7 @@ RB_WRAP(lcd_fillrect)
int height = luaL_checkint(L, 4); int height = luaL_checkint(L, 4);
rb->lcd_fillrect(x, y, width, height); rb->lcd_fillrect(x, y, width, height);
return 0;
return 1;
} }
#endif #endif
@ -181,14 +185,14 @@ RB_WRAP(yield)
{ {
(void)L; (void)L;
rb->yield(); rb->yield();
return 1; return 0;
} }
RB_WRAP(sleep) RB_WRAP(sleep)
{ {
int ticks = luaL_checkint(L, 1); int ticks = luaL_checkint(L, 1);
rb->sleep(ticks); rb->sleep(ticks);
return 1; return 0;
} }
RB_WRAP(current_tick) RB_WRAP(current_tick)
@ -268,21 +272,21 @@ RB_WRAP(backlight_on)
{ {
(void)L; (void)L;
rb->backlight_on(); rb->backlight_on();
return 1; return 0;
} }
RB_WRAP(backlight_off) RB_WRAP(backlight_off)
{ {
(void)L; (void)L;
rb->backlight_off(); rb->backlight_off();
return 1; return 0;
} }
RB_WRAP(backlight_set_timeout) RB_WRAP(backlight_set_timeout)
{ {
int val = luaL_checkint(L, 1); int val = luaL_checkint(L, 1);
rb->backlight_set_timeout(val); rb->backlight_set_timeout(val);
return 1; return 0;
} }
#ifdef HAVE_BACKLIGHT_BRIGHTNESS #ifdef HAVE_BACKLIGHT_BRIGHTNESS
@ -290,13 +294,13 @@ RB_WRAP(backlight_set_brightness)
{ {
int val = luaL_checkint(L, 1); int val = luaL_checkint(L, 1);
rb->backlight_set_brightness(val); rb->backlight_set_brightness(val);
return 1; return 0;
} }
#endif #endif
#define R(NAME) {#NAME, rock_##NAME} #define R(NAME) {#NAME, rock_##NAME}
static const luaL_Reg rocklib[] = { static const luaL_Reg rocklib[] =
{
/* Graphics */ /* Graphics */
R(lcd_clear_display), R(lcd_clear_display),
R(lcd_update), R(lcd_update),
@ -355,6 +359,7 @@ static const luaL_Reg rocklib[] = {
LUALIB_API int luaopen_rock(lua_State *L) LUALIB_API int luaopen_rock(lua_State *L)
{ {
luaL_register(L, LUA_ROCKLIBNAME, rocklib); luaL_register(L, LUA_ROCKLIBNAME, rocklib);
RB_CONSTANT(HZ); RB_CONSTANT(HZ);
RB_CONSTANT(LCD_WIDTH); RB_CONSTANT(LCD_WIDTH);
RB_CONSTANT(LCD_HEIGHT); RB_CONSTANT(LCD_HEIGHT);