1 /***************************************************************************
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
10 * Copyright (C) 2008 Dan Everton (safetydan)
11 * Copyright (C) 2009 Maurus Cuelenaere
13 * This program is free software; you can redistribute it and/or
14 * modify it under the terms of the GNU General Public License
15 * as published by the Free Software Foundation; either version 2
16 * of the License, or (at your option) any later version.
18 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19 * KIND, either express or implied.
21 ****************************************************************************/
30 #include "lib/helper.h"
33 * http://www.lua.org/manual/5.1/manual.html#lua_CFunction
35 * In order to communicate properly with Lua, a C function must use the following protocol,
36 * which defines the way parameters and results are passed: a C function receives its arguments
37 * from Lua in its stack in direct order (the first argument is pushed first). To return values to Lua,
38 * a C function just pushes them onto the stack, in direct order (the first result is pushed first),
39 * and returns the number of results. Any other value in the stack below the results will be properly
40 * discarded by Lua. Like a Lua function, a C function called by Lua can also return many results.
42 * When porting new functions, don't forget to check rocklib_aux.pl whether it automatically creates
43 * wrappers for the function and if so, add the function names to @forbidden_functions. This is to
44 * prevent namespace collisions and adding duplicate wrappers.
49 * -----------------------------
51 * Rockbox Lua image wrapper
53 * -----------------------------
56 #define ROCKLUA_IMAGE "rb.image"
65 static void rli_wrap(lua_State
*L
, fb_data
*src
, int width
, int height
)
67 struct rocklua_image
*a
= (struct rocklua_image
*)lua_newuserdata(L
, sizeof(struct rocklua_image
));
69 luaL_getmetatable(L
, ROCKLUA_IMAGE
);
70 lua_setmetatable(L
, -2);
77 static fb_data
* rli_alloc(lua_State
*L
, int width
, int height
)
79 size_t nbytes
= sizeof(struct rocklua_image
) + ((width
*height
) - 1) * sizeof(fb_data
);
80 struct rocklua_image
*a
= (struct rocklua_image
*)lua_newuserdata(L
, nbytes
);
82 luaL_getmetatable(L
, ROCKLUA_IMAGE
);
83 lua_setmetatable(L
, -2);
87 a
->data
= &a
->dummy
[0][0];
92 static int rli_new(lua_State
*L
)
94 int width
= luaL_checkint(L
, 1);
95 int height
= luaL_checkint(L
, 2);
97 rli_alloc(L
, width
, height
);
102 static struct rocklua_image
* rli_checktype(lua_State
*L
, int arg
)
104 void *ud
= luaL_checkudata(L
, arg
, ROCKLUA_IMAGE
);
105 luaL_argcheck(L
, ud
!= NULL
, arg
, "'" ROCKLUA_IMAGE
"' expected");
106 return (struct rocklua_image
*) ud
;
109 static int rli_width(lua_State
*L
)
111 struct rocklua_image
*a
= rli_checktype(L
, 1);
112 lua_pushnumber(L
, a
->width
);
116 static int rli_height(lua_State
*L
)
118 struct rocklua_image
*a
= rli_checktype(L
, 1);
119 lua_pushnumber(L
, a
->height
);
123 static fb_data
* rli_element(lua_State
*L
)
125 struct rocklua_image
*a
= rli_checktype(L
, 1);
126 int x
= luaL_checkint(L
, 2);
127 int y
= luaL_checkint(L
, 3);
129 luaL_argcheck(L
, 1 <= x
&& x
<= a
->width
, 2,
130 "index out of range");
131 luaL_argcheck(L
, 1 <= y
&& y
<= a
->height
, 3,
132 "index out of range");
134 /* return element address */
135 return &a
->data
[a
->width
* (y
- 1) + (x
- 1)];
138 static int rli_set(lua_State
*L
)
140 fb_data newvalue
= (fb_data
) luaL_checknumber(L
, 4);
141 *rli_element(L
) = newvalue
;
145 static int rli_get(lua_State
*L
)
147 lua_pushnumber(L
, *rli_element(L
));
151 static int rli_tostring(lua_State
*L
)
153 struct rocklua_image
*a
= rli_checktype(L
, 1);
154 lua_pushfstring(L
, ROCKLUA_IMAGE
": %dx%d", a
->width
, a
->height
);
158 static const struct luaL_reg rli_lib
[] =
160 {"__tostring", rli_tostring
},
163 {"width", rli_width
},
164 {"height", rli_height
},
168 static inline void rli_init(lua_State
*L
)
170 luaL_newmetatable(L
, ROCKLUA_IMAGE
);
172 lua_pushstring(L
, "__index");
173 lua_pushvalue(L
, -2); /* pushes the metatable */
174 lua_settable(L
, -3); /* metatable.__index = metatable */
176 luaL_register(L
, NULL
, rli_lib
);
180 * -----------------------------
182 * Rockbox wrappers start here!
184 * -----------------------------
187 #define RB_WRAP(M) static int rock_##M(lua_State *L)
188 #define SIMPLE_VOID_WRAPPER(func) RB_WRAP(func) { (void)L; func(); return 0; }
190 /* Helper function for opt_viewport */
191 static void check_tablevalue(lua_State
*L
, const char* key
, int tablepos
, void* res
, bool unsigned_val
)
193 lua_getfield(L
, tablepos
, key
); /* Find table[key] */
195 if(!lua_isnoneornil(L
, -1))
198 *(unsigned*)res
= luaL_checkint(L
, -1);
200 *(int*)res
= luaL_checkint(L
, -1);
203 lua_pop(L
, 1); /* Pop the value off the stack */
206 static struct viewport
* opt_viewport(lua_State
*L
, int narg
, struct viewport
* alt
)
208 if(lua_isnoneornil(L
, narg
))
211 int tablepos
= lua_gettop(L
);
214 lua_getfield(L
, tablepos
, "vp"); /* get table['vp'] */
215 if(lua_isnoneornil(L
, -1))
217 lua_pop(L
, 1); /* Pop nil off stack */
219 vp
= (struct viewport
*) lua_newuserdata(L
, sizeof(struct viewport
)); /* Allocate memory and push it as udata on the stack */
220 memset(vp
, 0, sizeof(struct viewport
)); /* Init viewport values to 0 */
221 lua_setfield(L
, tablepos
, "vp"); /* table['vp'] = vp (pops value off the stack) */
225 vp
= (struct viewport
*) lua_touserdata(L
, -1); /* Reuse viewport struct */
226 lua_pop(L
, 1); /* We don't need the value on stack */
229 luaL_checktype(L
, narg
, LUA_TTABLE
);
231 check_tablevalue(L
, "x", tablepos
, &vp
->x
, false);
232 check_tablevalue(L
, "y", tablepos
, &vp
->y
, false);
233 check_tablevalue(L
, "width", tablepos
, &vp
->width
, false);
234 check_tablevalue(L
, "height", tablepos
, &vp
->height
, false);
235 #ifdef HAVE_LCD_BITMAP
236 check_tablevalue(L
, "font", tablepos
, &vp
->font
, false);
237 check_tablevalue(L
, "drawmode", tablepos
, &vp
->drawmode
, false);
240 check_tablevalue(L
, "fg_pattern", tablepos
, &vp
->fg_pattern
, true);
241 check_tablevalue(L
, "bg_pattern", tablepos
, &vp
->bg_pattern
, true);
242 #ifdef HAVE_LCD_COLOR
243 check_tablevalue(L
, "lss_pattern", tablepos
, &vp
->lss_pattern
, true);
244 check_tablevalue(L
, "lse_pattern", tablepos
, &vp
->lse_pattern
, true);
245 check_tablevalue(L
, "lst_pattern", tablepos
, &vp
->lst_pattern
, true);
252 RB_WRAP(set_viewport
)
254 struct viewport
*vp
= opt_viewport(L
, 1, NULL
);
255 int screen
= luaL_optint(L
, 2, SCREEN_MAIN
);
256 rb
->screens
[screen
]->set_viewport(vp
);
260 RB_WRAP(clear_viewport
)
262 int screen
= luaL_optint(L
, 1, SCREEN_MAIN
);
263 rb
->screens
[screen
]->clear_viewport();
267 #ifdef HAVE_LCD_BITMAP
268 RB_WRAP(lcd_framebuffer
)
270 rli_wrap(L
, rb
->lcd_framebuffer
, LCD_WIDTH
, LCD_HEIGHT
);
274 RB_WRAP(lcd_mono_bitmap_part
)
276 struct rocklua_image
*src
= rli_checktype(L
, 1);
277 int src_x
= luaL_checkint(L
, 2);
278 int src_y
= luaL_checkint(L
, 3);
279 int stride
= luaL_checkint(L
, 4);
280 int x
= luaL_checkint(L
, 5);
281 int y
= luaL_checkint(L
, 6);
282 int width
= luaL_checkint(L
, 7);
283 int height
= luaL_checkint(L
, 8);
284 int screen
= luaL_optint(L
, 9, SCREEN_MAIN
);
286 rb
->screens
[screen
]->mono_bitmap_part((const unsigned char *)src
->data
, src_x
, src_y
, stride
, x
, y
, width
, height
);
290 RB_WRAP(lcd_mono_bitmap
)
292 struct rocklua_image
*src
= rli_checktype(L
, 1);
293 int x
= luaL_checkint(L
, 2);
294 int y
= luaL_checkint(L
, 3);
295 int width
= luaL_checkint(L
, 4);
296 int height
= luaL_checkint(L
, 5);
297 int screen
= luaL_optint(L
, 6, SCREEN_MAIN
);
299 rb
->screens
[screen
]->mono_bitmap((const unsigned char *)src
->data
, x
, y
, width
, height
);
304 RB_WRAP(lcd_bitmap_part
)
306 struct rocklua_image
*src
= rli_checktype(L
, 1);
307 int src_x
= luaL_checkint(L
, 2);
308 int src_y
= luaL_checkint(L
, 3);
309 int stride
= luaL_checkint(L
, 4);
310 int x
= luaL_checkint(L
, 5);
311 int y
= luaL_checkint(L
, 6);
312 int width
= luaL_checkint(L
, 7);
313 int height
= luaL_checkint(L
, 8);
314 int screen
= luaL_optint(L
, 9, SCREEN_MAIN
);
316 rb
->screens
[screen
]->bitmap_part(src
->data
, src_x
, src_y
, stride
, x
, y
, width
, height
);
322 struct rocklua_image
*src
= rli_checktype(L
, 1);
323 int x
= luaL_checkint(L
, 2);
324 int y
= luaL_checkint(L
, 3);
325 int width
= luaL_checkint(L
, 4);
326 int height
= luaL_checkint(L
, 5);
327 int screen
= luaL_optint(L
, 6, SCREEN_MAIN
);
329 rb
->screens
[screen
]->bitmap(src
->data
, x
, y
, width
, height
);
333 RB_WRAP(lcd_get_backdrop
)
335 fb_data
* backdrop
= rb
->lcd_get_backdrop();
339 rli_wrap(L
, backdrop
, LCD_WIDTH
, LCD_HEIGHT
);
343 #endif /* LCD_DEPTH > 1 */
346 RB_WRAP(lcd_bitmap_transparent_part
)
348 struct rocklua_image
*src
= rli_checktype(L
, 1);
349 int src_x
= luaL_checkint(L
, 2);
350 int src_y
= luaL_checkint(L
, 3);
351 int stride
= luaL_checkint(L
, 4);
352 int x
= luaL_checkint(L
, 5);
353 int y
= luaL_checkint(L
, 6);
354 int width
= luaL_checkint(L
, 7);
355 int height
= luaL_checkint(L
, 8);
356 int screen
= luaL_optint(L
, 9, SCREEN_MAIN
);
358 rb
->screens
[screen
]->transparent_bitmap_part(src
->data
, src_x
, src_y
, stride
, x
, y
, width
, height
);
362 RB_WRAP(lcd_bitmap_transparent
)
364 struct rocklua_image
*src
= rli_checktype(L
, 1);
365 int x
= luaL_checkint(L
, 2);
366 int y
= luaL_checkint(L
, 3);
367 int width
= luaL_checkint(L
, 4);
368 int height
= luaL_checkint(L
, 5);
369 int screen
= luaL_optint(L
, 6, SCREEN_MAIN
);
371 rb
->screens
[screen
]->transparent_bitmap(src
->data
, x
, y
, width
, height
);
374 #endif /* LCD_DEPTH == 16 */
376 #endif /* defined(LCD_BITMAP) */
378 RB_WRAP(current_tick
)
380 lua_pushinteger(L
, *rb
->current_tick
);
384 #ifdef HAVE_TOUCHSCREEN
385 RB_WRAP(action_get_touchscreen_press
)
388 int result
= rb
->action_get_touchscreen_press(&x
, &y
);
390 lua_pushinteger(L
, result
);
391 lua_pushinteger(L
, x
);
392 lua_pushinteger(L
, y
);
400 luaL_buffinit(L
, &b
);
402 const char *input
= luaL_optstring(L
, 1, NULL
);
403 char *buffer
= luaL_prepbuffer(&b
);
406 rb
->strlcpy(buffer
, input
, LUAL_BUFFERSIZE
);
410 if(!rb
->kbd_input(buffer
, LUAL_BUFFERSIZE
))
412 luaL_addsize(&b
, strlen(buffer
));
421 #ifdef HAVE_TOUCHSCREEN
422 RB_WRAP(touchscreen_set_mode
)
424 enum touchscreen_mode mode
= luaL_checkint(L
, 1);
425 rb
->touchscreen_set_mode(mode
);
430 RB_WRAP(font_getstringsize
)
432 const unsigned char* str
= luaL_checkstring(L
, 1);
433 int fontnumber
= luaL_checkint(L
, 2);
436 int result
= rb
->font_getstringsize(str
, &w
, &h
, fontnumber
);
437 lua_pushinteger(L
, result
);
438 lua_pushinteger(L
, w
);
439 lua_pushinteger(L
, h
);
444 #ifdef HAVE_LCD_COLOR
447 int r
= luaL_checkint(L
, 1);
448 int g
= luaL_checkint(L
, 2);
449 int b
= luaL_checkint(L
, 3);
450 int result
= LCD_RGBPACK(r
, g
, b
);
451 lua_pushinteger(L
, result
);
455 RB_WRAP(lcd_rgbunpack
)
457 int rgb
= luaL_checkint(L
, 1);
458 lua_pushinteger(L
, RGB_UNPACK_RED(rgb
));
459 lua_pushinteger(L
, RGB_UNPACK_GREEN(rgb
));
460 lua_pushinteger(L
, RGB_UNPACK_BLUE(rgb
));
465 RB_WRAP(read_bmp_file
)
468 const char* filename
= luaL_checkstring(L
, 1);
469 bool dither
= luaL_optboolean(L
, 2, true);
470 bool transparent
= luaL_optboolean(L
, 3, false);
471 int format
= FORMAT_NATIVE
;
474 format
|= FORMAT_DITHER
;
477 format
|= FORMAT_TRANSPARENT
;
479 int result
= rb
->read_bmp_file(filename
, &bm
, 0, format
| FORMAT_RETURN_SIZE
, NULL
);
483 bm
.data
= (unsigned char*) rli_alloc(L
, bm
.width
, bm
.height
);
484 if(rb
->read_bmp_file(filename
, &bm
, result
, format
, NULL
) < 0)
486 /* Error occured, drop newly allocated image from stack */
497 RB_WRAP(current_path
)
499 const char *current_path
= get_current_path(L
, 1);
500 if(current_path
!= NULL
)
501 lua_pushstring(L
, current_path
);
508 static void fill_text_message(lua_State
*L
, struct text_message
* message
,
512 luaL_checktype(L
, pos
, LUA_TTABLE
);
513 int n
= luaL_getn(L
, pos
);
514 const char **lines
= (const char**) dlmalloc(n
* sizeof(const char*));
516 luaL_error(L
, "Can't allocate %d bytes!", n
* sizeof(const char*));
519 lua_rawgeti(L
, pos
, i
);
520 lines
[i
-1] = luaL_checkstring(L
, -1);
523 message
->message_lines
= lines
;
524 message
->nb_lines
= n
;
527 RB_WRAP(gui_syncyesno_run
)
529 struct text_message main_message
, yes_message
, no_message
;
530 struct text_message
*yes
= NULL
, *no
= NULL
;
532 fill_text_message(L
, &main_message
, 1);
533 if(!lua_isnoneornil(L
, 2))
534 fill_text_message(L
, (yes
= &yes_message
), 2);
535 if(!lua_isnoneornil(L
, 3))
536 fill_text_message(L
, (no
= &no_message
), 3);
538 enum yesno_res result
= rb
->gui_syncyesno_run(&main_message
, yes
, no
);
540 dlfree(main_message
.message_lines
);
542 dlfree(yes_message
.message_lines
);
544 dlfree(no_message
.message_lines
);
546 lua_pushinteger(L
, result
);
552 struct menu_callback_with_desc menu_desc
= {NULL
, NULL
, Icon_NOICON
};
553 struct menu_item_ex menu
= {MT_RETURN_ID
| MENU_HAS_DESC
, {.strings
= NULL
},
554 {.callback_and_desc
= &menu_desc
}};
555 int i
, n
, start_selected
;
556 const char **items
, *title
;
558 title
= luaL_checkstring(L
, 1);
559 luaL_checktype(L
, 2, LUA_TTABLE
);
560 start_selected
= luaL_optint(L
, 3, 0);
563 items
= (const char**) dlmalloc(n
* sizeof(const char*));
565 luaL_error(L
, "Can't allocate %d bytes!", n
* sizeof(const char*));
568 lua_rawgeti(L
, 2, i
); /* Push item on the stack */
569 items
[i
-1] = luaL_checkstring(L
, -1);
570 lua_pop(L
, 1); /* Pop it */
573 menu
.strings
= items
;
574 menu
.flags
|= MENU_ITEM_COUNT(n
);
575 menu_desc
.desc
= (unsigned char*) title
;
577 int result
= rb
->do_menu(&menu
, &start_selected
, NULL
, false);
581 lua_pushinteger(L
, result
);
585 SIMPLE_VOID_WRAPPER(backlight_force_on
);
586 SIMPLE_VOID_WRAPPER(backlight_use_settings
);
587 #ifdef HAVE_REMOTE_LCD
588 SIMPLE_VOID_WRAPPER(remote_backlight_force_on
);
589 SIMPLE_VOID_WRAPPER(remote_backlight_use_settings
);
591 #ifdef HAVE_BUTTON_LIGHT
592 SIMPLE_VOID_WRAPPER(buttonlight_force_on
);
593 SIMPLE_VOID_WRAPPER(buttonlight_use_settings
);
595 #ifdef HAVE_BACKLIGHT_BRIGHTNESS
596 RB_WRAP(backlight_brightness_set
)
598 int brightness
= luaL_checkint(L
, 1);
599 backlight_brightness_set(brightness
);
603 SIMPLE_VOID_WRAPPER(backlight_brightness_use_setting
);
606 #define R(NAME) {#NAME, rock_##NAME}
607 static const luaL_Reg rocklib
[] =
610 #ifdef HAVE_LCD_BITMAP
612 R(lcd_mono_bitmap_part
),
620 R(lcd_bitmap_transparent_part
),
621 R(lcd_bitmap_transparent
),
624 #ifdef HAVE_LCD_COLOR
633 #ifdef HAVE_TOUCHSCREEN
634 R(action_get_touchscreen_press
),
635 R(touchscreen_set_mode
),
639 R(font_getstringsize
),
644 R(gui_syncyesno_run
),
647 /* Backlight helper */
648 R(backlight_force_on
),
649 R(backlight_use_settings
),
650 #ifdef HAVE_REMOTE_LCD
651 R(remote_backlight_force_on
),
652 R(remote_backlight_use_settings
),
654 #ifdef HAVE_BUTTON_LIGHT
655 R(buttonlight_force_on
),
656 R(buttonlight_use_settings
),
658 #ifdef HAVE_BACKLIGHT_BRIGHTNESS
659 R(backlight_brightness_set
),
660 R(backlight_brightness_use_setting
),
663 {"new_image", rli_new
},
668 extern const luaL_Reg rocklib_aux
[];
671 #define RB_CONSTANT(x) lua_pushinteger(L, x); lua_setfield(L, -2, #x);
673 ** Open Rockbox library
675 LUALIB_API
int luaopen_rock(lua_State
*L
)
677 luaL_register(L
, LUA_ROCKLIBNAME
, rocklib
);
678 luaL_register(L
, LUA_ROCKLIBNAME
, rocklib_aux
);
682 RB_CONSTANT(LCD_WIDTH
);
683 RB_CONSTANT(LCD_HEIGHT
);
684 RB_CONSTANT(LCD_DEPTH
);
686 RB_CONSTANT(FONT_SYSFIXED
);
687 RB_CONSTANT(FONT_UI
);
689 #ifdef HAVE_TOUCHSCREEN
690 RB_CONSTANT(TOUCHSCREEN_POINT
);
691 RB_CONSTANT(TOUCHSCREEN_BUTTON
);
694 RB_CONSTANT(SCREEN_MAIN
);
695 #ifdef HAVE_REMOTE_LCD
696 RB_CONSTANT(SCREEN_REMOTE
);