We have a 3.9 release, update builds.pm
[maemo-rb.git] / apps / plugins / lua / rocklib.c
blob46572014f0c283e3ec74a9a71b0834b01c9c3e62
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
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 ****************************************************************************/
23 #define lrocklib_c
24 #define LUA_LIB
26 #include "lua.h"
28 #include "lauxlib.h"
29 #include "rocklib.h"
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"
57 struct rocklua_image
59 int width;
60 int height;
61 fb_data *data;
62 fb_data dummy[1][1];
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);
72 a->width = width;
73 a->height = height;
74 a->data = src;
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);
85 a->width = width;
86 a->height = height;
87 a->data = &a->dummy[0][0];
89 return a->data;
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);
99 return 1;
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);
113 return 1;
116 static int rli_height(lua_State *L)
118 struct rocklua_image *a = rli_checktype(L, 1);
119 lua_pushnumber(L, a->height);
120 return 1;
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;
142 return 0;
145 static int rli_get(lua_State *L)
147 lua_pushnumber(L, *rli_element(L));
148 return 1;
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);
155 return 1;
158 static const struct luaL_reg rli_lib [] =
160 {"__tostring", rli_tostring},
161 {"set", rli_set},
162 {"get", rli_get},
163 {"width", rli_width},
164 {"height", rli_height},
165 {NULL, NULL}
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))
197 if(unsigned_val)
198 *(unsigned*)res = luaL_checkint(L, -1);
199 else
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))
209 return alt;
211 int tablepos = lua_gettop(L);
212 struct viewport *vp;
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) */
223 else
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);
238 #endif
239 #if LCD_DEPTH > 1
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);
246 #endif
247 #endif
249 return vp;
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);
257 return 0;
260 RB_WRAP(clear_viewport)
262 int screen = luaL_optint(L, 1, SCREEN_MAIN);
263 rb->screens[screen]->clear_viewport();
264 return 0;
267 #ifdef HAVE_LCD_BITMAP
268 RB_WRAP(lcd_framebuffer)
270 rli_wrap(L, rb->lcd_framebuffer, LCD_WIDTH, LCD_HEIGHT);
271 return 1;
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);
287 return 0;
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);
300 return 0;
303 #if LCD_DEPTH > 1
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);
317 return 0;
320 RB_WRAP(lcd_bitmap)
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);
330 return 0;
333 RB_WRAP(lcd_get_backdrop)
335 fb_data* backdrop = rb->lcd_get_backdrop();
336 if(backdrop == NULL)
337 lua_pushnil(L);
338 else
339 rli_wrap(L, backdrop, LCD_WIDTH, LCD_HEIGHT);
341 return 1;
343 #endif /* LCD_DEPTH > 1 */
345 #if LCD_DEPTH == 16
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);
359 return 0;
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);
372 return 0;
374 #endif /* LCD_DEPTH == 16 */
376 #endif /* defined(LCD_BITMAP) */
378 RB_WRAP(current_tick)
380 lua_pushinteger(L, *rb->current_tick);
381 return 1;
384 #ifdef HAVE_TOUCHSCREEN
385 RB_WRAP(action_get_touchscreen_press)
387 short x, y;
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);
393 return 3;
395 #endif
397 RB_WRAP(kbd_input)
399 luaL_Buffer b;
400 luaL_buffinit(L, &b);
402 const char *input = luaL_optstring(L, 1, NULL);
403 char *buffer = luaL_prepbuffer(&b);
405 if(input != NULL)
406 rb->strlcpy(buffer, input, LUAL_BUFFERSIZE);
407 else
408 buffer[0] = '\0';
410 if(!rb->kbd_input(buffer, LUAL_BUFFERSIZE))
412 luaL_addsize(&b, strlen(buffer));
413 luaL_pushresult(&b);
415 else
416 lua_pushnil(L);
418 return 1;
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);
426 return 0;
428 #endif
430 RB_WRAP(font_getstringsize)
432 const unsigned char* str = luaL_checkstring(L, 1);
433 int fontnumber = luaL_checkint(L, 2);
434 int w, h;
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);
441 return 3;
444 #ifdef HAVE_LCD_COLOR
445 RB_WRAP(lcd_rgbpack)
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);
452 return 1;
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));
461 return 3;
463 #endif
465 RB_WRAP(read_bmp_file)
467 struct bitmap bm;
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;
473 if(dither)
474 format |= FORMAT_DITHER;
476 if(transparent)
477 format |= FORMAT_TRANSPARENT;
479 int result = rb->read_bmp_file(filename, &bm, 0, format | FORMAT_RETURN_SIZE, NULL);
481 if(result > 0)
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 */
487 lua_pop(L, 1);
488 lua_pushnil(L);
491 else
492 lua_pushnil(L);
494 return 1;
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);
502 else
503 lua_pushnil(L);
505 return 1;
508 static void fill_text_message(lua_State *L, struct text_message * message,
509 int pos)
511 int i;
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*));
515 if(lines == NULL)
516 luaL_error(L, "Can't allocate %d bytes!", n * sizeof(const char*));
517 for(i=1; i<=n; i++)
519 lua_rawgeti(L, pos, i);
520 lines[i-1] = luaL_checkstring(L, -1);
521 lua_pop(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);
541 if(yes)
542 dlfree(yes_message.message_lines);
543 if(no)
544 dlfree(no_message.message_lines);
546 lua_pushinteger(L, result);
547 return 1;
550 RB_WRAP(do_menu)
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);
562 n = luaL_getn(L, 2);
563 items = (const char**) dlmalloc(n * sizeof(const char*));
564 if(items == NULL)
565 luaL_error(L, "Can't allocate %d bytes!", n * sizeof(const char*));
566 for(i=1; i<=n; i++)
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);
579 dlfree(items);
581 lua_pushinteger(L, result);
582 return 1;
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);
590 #endif
591 #ifdef HAVE_BUTTON_LIGHT
592 SIMPLE_VOID_WRAPPER(buttonlight_force_on);
593 SIMPLE_VOID_WRAPPER(buttonlight_use_settings);
594 #endif
595 #ifdef HAVE_BACKLIGHT_BRIGHTNESS
596 RB_WRAP(backlight_brightness_set)
598 int brightness = luaL_checkint(L, 1);
599 backlight_brightness_set(brightness);
601 return 0;
603 SIMPLE_VOID_WRAPPER(backlight_brightness_use_setting);
604 #endif
606 #define R(NAME) {#NAME, rock_##NAME}
607 static const luaL_Reg rocklib[] =
609 /* Graphics */
610 #ifdef HAVE_LCD_BITMAP
611 R(lcd_framebuffer),
612 R(lcd_mono_bitmap_part),
613 R(lcd_mono_bitmap),
614 #if LCD_DEPTH > 1
615 R(lcd_get_backdrop),
616 R(lcd_bitmap_part),
617 R(lcd_bitmap),
618 #endif
619 #if LCD_DEPTH == 16
620 R(lcd_bitmap_transparent_part),
621 R(lcd_bitmap_transparent),
622 #endif
623 #endif
624 #ifdef HAVE_LCD_COLOR
625 R(lcd_rgbpack),
626 R(lcd_rgbunpack),
627 #endif
629 /* Kernel */
630 R(current_tick),
632 /* Buttons */
633 #ifdef HAVE_TOUCHSCREEN
634 R(action_get_touchscreen_press),
635 R(touchscreen_set_mode),
636 #endif
637 R(kbd_input),
639 R(font_getstringsize),
640 R(read_bmp_file),
641 R(set_viewport),
642 R(clear_viewport),
643 R(current_path),
644 R(gui_syncyesno_run),
645 R(do_menu),
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),
653 #endif
654 #ifdef HAVE_BUTTON_LIGHT
655 R(buttonlight_force_on),
656 R(buttonlight_use_settings),
657 #endif
658 #ifdef HAVE_BACKLIGHT_BRIGHTNESS
659 R(backlight_brightness_set),
660 R(backlight_brightness_use_setting),
661 #endif
663 {"new_image", rli_new},
665 {NULL, NULL}
667 #undef R
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);
680 RB_CONSTANT(HZ);
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);
692 #endif
694 RB_CONSTANT(SCREEN_MAIN);
695 #ifdef HAVE_REMOTE_LCD
696 RB_CONSTANT(SCREEN_REMOTE);
697 #endif
699 rli_init(L);
701 return 1;