diff options
author | Maurus Cuelenaere <mcuelenaere@gmail.com> | 2009-05-24 01:54:15 +0000 |
---|---|---|
committer | Maurus Cuelenaere <mcuelenaere@gmail.com> | 2009-05-24 01:54:15 +0000 |
commit | 86fe1e8b5cdfd536fb19018fc15d9088e13bf676 (patch) | |
tree | abc5b8a5fc41cb54df5b2292a40293d167caca09 /apps/plugins/lua | |
parent | ce7738e6176fa45eeff85cecea2a11734188427f (diff) | |
download | rockbox-86fe1e8b5cdfd536fb19018fc15d9088e13bf676.tar.gz rockbox-86fe1e8b5cdfd536fb19018fc15d9088e13bf676.zip |
Lua: add image handling + some other wrappers
git-svn-id: svn://svn.rockbox.org/rockbox/trunk@21063 a1c6a512-1295-4272-9138-f99709370657
Diffstat (limited to 'apps/plugins/lua')
-rw-r--r-- | apps/plugins/lua/rocklib.c | 282 |
1 files changed, 281 insertions, 1 deletions
diff --git a/apps/plugins/lua/rocklib.c b/apps/plugins/lua/rocklib.c index c99400c938..7daed0c93d 100644 --- a/apps/plugins/lua/rocklib.c +++ b/apps/plugins/lua/rocklib.c | |||
@@ -38,6 +38,136 @@ | |||
38 | * and returns the number of results. Any other value in the stack below the results will be properly | 38 | * and returns the number of results. Any other value in the stack below the results will be properly |
39 | * discarded by Lua. Like a Lua function, a C function called by Lua can also return many results. | 39 | * discarded by Lua. Like a Lua function, a C function called by Lua can also return many results. |
40 | */ | 40 | */ |
41 | |||
42 | |||
43 | /* | ||
44 | * ----------------------------- | ||
45 | * | ||
46 | * Rockbox Lua image wrapper | ||
47 | * | ||
48 | * ----------------------------- | ||
49 | */ | ||
50 | |||
51 | #define ROCKLUA_IMAGE "rb.image" | ||
52 | struct rocklua_image | ||
53 | { | ||
54 | int width; | ||
55 | int height; | ||
56 | fb_data *data; | ||
57 | }; | ||
58 | |||
59 | static void rli_wrap(lua_State *L, fb_data *src, int width, int height) | ||
60 | { | ||
61 | struct rocklua_image *a = (struct rocklua_image *)lua_newuserdata(L, sizeof(struct rocklua_image)); | ||
62 | |||
63 | luaL_getmetatable(L, ROCKLUA_IMAGE); | ||
64 | lua_setmetatable(L, -2); | ||
65 | |||
66 | a->width = width; | ||
67 | a->height = height; | ||
68 | a->data = src; | ||
69 | } | ||
70 | |||
71 | static int rli_new(lua_State *L) | ||
72 | { | ||
73 | int width = luaL_checkint(L, 1); | ||
74 | int height = luaL_checkint(L, 2); | ||
75 | size_t nbytes = sizeof(struct rocklua_image) + (width - 1)*(height - 1)*sizeof(fb_data); | ||
76 | struct rocklua_image *a = (struct rocklua_image *)lua_newuserdata(L, nbytes); | ||
77 | |||
78 | luaL_getmetatable(L, ROCKLUA_IMAGE); | ||
79 | lua_setmetatable(L, -2); | ||
80 | |||
81 | a->width = width; | ||
82 | a->height = height; | ||
83 | return 1; | ||
84 | } | ||
85 | |||
86 | static struct rocklua_image* rli_checktype(lua_State *L, int arg) | ||
87 | { | ||
88 | void *ud = luaL_checkudata(L, arg, ROCKLUA_IMAGE); | ||
89 | luaL_argcheck(L, ud != NULL, arg, "'" ROCKLUA_IMAGE "' expected"); | ||
90 | return (struct rocklua_image*) ud; | ||
91 | } | ||
92 | |||
93 | static int rli_width(lua_State *L) | ||
94 | { | ||
95 | struct rocklua_image *a = rli_checktype(L, 1); | ||
96 | lua_pushnumber(L, a->width); | ||
97 | return 1; | ||
98 | } | ||
99 | |||
100 | static int rli_height(lua_State *L) | ||
101 | { | ||
102 | struct rocklua_image *a = rli_checktype(L, 1); | ||
103 | lua_pushnumber(L, a->height); | ||
104 | return 1; | ||
105 | } | ||
106 | |||
107 | static fb_data* rli_element(lua_State *L) | ||
108 | { | ||
109 | struct rocklua_image *a = rli_checktype(L, 1); | ||
110 | int x = luaL_checkint(L, 2); | ||
111 | int y = luaL_checkint(L, 3); | ||
112 | |||
113 | luaL_argcheck(L, 1 <= x && x <= a->width, 2, | ||
114 | "index out of range"); | ||
115 | luaL_argcheck(L, 1 <= y && y <= a->height, 3, | ||
116 | "index out of range"); | ||
117 | |||
118 | /* return element address */ | ||
119 | return &a->data[a->height * (y - 1) + (x - 1)]; | ||
120 | } | ||
121 | |||
122 | static int rli_set(lua_State *L) | ||
123 | { | ||
124 | fb_data newvalue = (fb_data) luaL_checknumber(L, 4); | ||
125 | *rli_element(L) = newvalue; | ||
126 | return 0; | ||
127 | } | ||
128 | |||
129 | static int rli_get(lua_State *L) | ||
130 | { | ||
131 | lua_pushnumber(L, *rli_element(L)); | ||
132 | return 1; | ||
133 | } | ||
134 | |||
135 | static int rli_tostring(lua_State *L) | ||
136 | { | ||
137 | struct rocklua_image *a = rli_checktype(L, 1); | ||
138 | lua_pushfstring(L, ROCKLUA_IMAGE ": %dx%d", a->width, a->height); | ||
139 | return 1; | ||
140 | } | ||
141 | |||
142 | static const struct luaL_reg rli_lib [] = | ||
143 | { | ||
144 | {"__tostring", rli_tostring}, | ||
145 | {"set", rli_set}, | ||
146 | {"get", rli_get}, | ||
147 | {"width", rli_width}, | ||
148 | {"height", rli_height}, | ||
149 | {NULL, NULL} | ||
150 | }; | ||
151 | |||
152 | static inline void rli_init(lua_State *L) | ||
153 | { | ||
154 | luaL_newmetatable(L, ROCKLUA_IMAGE); | ||
155 | |||
156 | lua_pushstring(L, "__index"); | ||
157 | lua_pushvalue(L, -2); /* pushes the metatable */ | ||
158 | lua_settable(L, -3); /* metatable.__index = metatable */ | ||
159 | |||
160 | luaL_register(L, NULL, rli_lib); | ||
161 | } | ||
162 | |||
163 | /* | ||
164 | * ----------------------------- | ||
165 | * | ||
166 | * Rockbox wrappers start here! | ||
167 | * | ||
168 | * ----------------------------- | ||
169 | */ | ||
170 | |||
41 | #define RB_WRAP(M) static int rock_##M(lua_State *L) | 171 | #define RB_WRAP(M) static int rock_##M(lua_State *L) |
42 | 172 | ||
43 | RB_WRAP(splash) | 173 | RB_WRAP(splash) |
@@ -55,6 +185,16 @@ RB_WRAP(lcd_update) | |||
55 | return 0; | 185 | return 0; |
56 | } | 186 | } |
57 | 187 | ||
188 | RB_WRAP(lcd_update_rect) | ||
189 | { | ||
190 | int x = luaL_checkint(L, 1); | ||
191 | int y = luaL_checkint(L, 2); | ||
192 | int width = luaL_checkint(L, 3); | ||
193 | int height = luaL_checkint(L, 4); | ||
194 | rb->lcd_update_rect(x, y, width, height); | ||
195 | return 0; | ||
196 | } | ||
197 | |||
58 | RB_WRAP(lcd_clear_display) | 198 | RB_WRAP(lcd_clear_display) |
59 | { | 199 | { |
60 | (void)L; | 200 | (void)L; |
@@ -97,6 +237,12 @@ RB_WRAP(lcd_stop_scroll) | |||
97 | } | 237 | } |
98 | 238 | ||
99 | #ifdef HAVE_LCD_BITMAP | 239 | #ifdef HAVE_LCD_BITMAP |
240 | RB_WRAP(lcd_framebuffer) | ||
241 | { | ||
242 | rli_wrap(L, rb->lcd_framebuffer, LCD_WIDTH, LCD_HEIGHT); | ||
243 | return 1; | ||
244 | } | ||
245 | |||
100 | RB_WRAP(lcd_set_drawmode) | 246 | RB_WRAP(lcd_set_drawmode) |
101 | { | 247 | { |
102 | int drawmode = luaL_checkint(L, 1); | 248 | int drawmode = luaL_checkint(L, 1); |
@@ -179,7 +325,106 @@ RB_WRAP(lcd_fillrect) | |||
179 | rb->lcd_fillrect(x, y, width, height); | 325 | rb->lcd_fillrect(x, y, width, height); |
180 | return 0; | 326 | return 0; |
181 | } | 327 | } |
182 | #endif | 328 | |
329 | #if LCD_DEPTH > 1 | ||
330 | RB_WRAP(lcd_set_foreground) | ||
331 | { | ||
332 | unsigned foreground = luaL_checkint(L, 1); | ||
333 | rb->lcd_set_foreground(foreground); | ||
334 | return 0; | ||
335 | } | ||
336 | |||
337 | RB_WRAP(lcd_get_foreground) | ||
338 | { | ||
339 | unsigned result = rb->lcd_get_foreground(); | ||
340 | lua_pushinteger(L, result); | ||
341 | return 1; | ||
342 | } | ||
343 | |||
344 | RB_WRAP(lcd_set_background) | ||
345 | { | ||
346 | unsigned background = luaL_checkint(L, 1); | ||
347 | rb->lcd_set_background(background); | ||
348 | return 0; | ||
349 | } | ||
350 | |||
351 | RB_WRAP(lcd_get_background) | ||
352 | { | ||
353 | unsigned result = rb->lcd_get_background(); | ||
354 | lua_pushinteger(L, result); | ||
355 | return 1; | ||
356 | } | ||
357 | |||
358 | RB_WRAP(lcd_bitmap_part) | ||
359 | { | ||
360 | struct rocklua_image *src = rli_checktype(L, 1); | ||
361 | int src_x = luaL_checkint(L, 2); | ||
362 | int src_y = luaL_checkint(L, 3); | ||
363 | int stride = luaL_checkint(L, 4); | ||
364 | int x = luaL_checkint(L, 5); | ||
365 | int y = luaL_checkint(L, 6); | ||
366 | int width = luaL_checkint(L, 7); | ||
367 | int height = luaL_checkint(L, 8); | ||
368 | |||
369 | rb->lcd_bitmap_part(src->data, src_x, src_y, stride, x, y, width, height); | ||
370 | return 0; | ||
371 | } | ||
372 | |||
373 | RB_WRAP(lcd_bitmap) | ||
374 | { | ||
375 | struct rocklua_image *src = rli_checktype(L, 1); | ||
376 | int x = luaL_checkint(L, 2); | ||
377 | int y = luaL_checkint(L, 3); | ||
378 | int width = luaL_checkint(L, 4); | ||
379 | int height = luaL_checkint(L, 5); | ||
380 | |||
381 | rb->lcd_bitmap(src->data, x, y, width, height); | ||
382 | return 0; | ||
383 | } | ||
384 | |||
385 | RB_WRAP(lcd_get_backdrop) | ||
386 | { | ||
387 | fb_data* backdrop = rb->lcd_get_backdrop(); | ||
388 | if(backdrop == NULL) | ||
389 | return 0; | ||
390 | else | ||
391 | { | ||
392 | rli_wrap(L, backdrop, LCD_WIDTH, LCD_HEIGHT); | ||
393 | return 1; | ||
394 | } | ||
395 | } | ||
396 | #endif /* LCD_DEPTH > 1 */ | ||
397 | |||
398 | #if LCD_DEPTH == 16 | ||
399 | RB_WRAP(lcd_bitmap_transparent_part) | ||
400 | { | ||
401 | struct rocklua_image *src = rli_checktype(L, 1); | ||
402 | int src_x = luaL_checkint(L, 2); | ||
403 | int src_y = luaL_checkint(L, 3); | ||
404 | int stride = luaL_checkint(L, 4); | ||
405 | int x = luaL_checkint(L, 5); | ||
406 | int y = luaL_checkint(L, 6); | ||
407 | int width = luaL_checkint(L, 7); | ||
408 | int height = luaL_checkint(L, 8); | ||
409 | |||
410 | rb->lcd_bitmap_transparent_part(src->data, src_x, src_y, stride, x, y, width, height); | ||
411 | return 0; | ||
412 | } | ||
413 | |||
414 | RB_WRAP(lcd_bitmap_transparent) | ||
415 | { | ||
416 | struct rocklua_image *src = rli_checktype(L, 1); | ||
417 | int x = luaL_checkint(L, 2); | ||
418 | int y = luaL_checkint(L, 3); | ||
419 | int width = luaL_checkint(L, 4); | ||
420 | int height = luaL_checkint(L, 5); | ||
421 | |||
422 | rb->lcd_bitmap_transparent(src->data, x, y, width, height); | ||
423 | return 0; | ||
424 | } | ||
425 | #endif /* LCD_DEPTH == 16 */ | ||
426 | |||
427 | #endif /* defined(LCD_BITMAP) */ | ||
183 | 428 | ||
184 | RB_WRAP(yield) | 429 | RB_WRAP(yield) |
185 | { | 430 | { |
@@ -432,18 +677,34 @@ RB_WRAP(file_exists) | |||
432 | return 1; | 677 | return 1; |
433 | } | 678 | } |
434 | 679 | ||
680 | RB_WRAP(font_getstringsize) | ||
681 | { | ||
682 | const unsigned char* str = luaL_checkstring(L, 1); | ||
683 | int fontnumber = luaL_checkint(L, 2); | ||
684 | int w, h; | ||
685 | |||
686 | int result = rb->font_getstringsize(str, &w, &h, fontnumber); | ||
687 | lua_pushinteger(L, result); | ||
688 | lua_pushinteger(L, w); | ||
689 | lua_pushinteger(L, h); | ||
690 | |||
691 | return 3; | ||
692 | } | ||
693 | |||
435 | #define R(NAME) {#NAME, rock_##NAME} | 694 | #define R(NAME) {#NAME, rock_##NAME} |
436 | static const luaL_Reg rocklib[] = | 695 | static const luaL_Reg rocklib[] = |
437 | { | 696 | { |
438 | /* Graphics */ | 697 | /* Graphics */ |
439 | R(lcd_clear_display), | 698 | R(lcd_clear_display), |
440 | R(lcd_update), | 699 | R(lcd_update), |
700 | R(lcd_update_rect), | ||
441 | R(lcd_puts), | 701 | R(lcd_puts), |
442 | R(lcd_putsxy), | 702 | R(lcd_putsxy), |
443 | R(lcd_puts_scroll), | 703 | R(lcd_puts_scroll), |
444 | R(lcd_stop_scroll), | 704 | R(lcd_stop_scroll), |
445 | R(splash), | 705 | R(splash), |
446 | #ifdef HAVE_LCD_BITMAP | 706 | #ifdef HAVE_LCD_BITMAP |
707 | R(lcd_framebuffer), | ||
447 | R(lcd_set_drawmode), | 708 | R(lcd_set_drawmode), |
448 | R(lcd_get_drawmode), | 709 | R(lcd_get_drawmode), |
449 | R(lcd_setfont), | 710 | R(lcd_setfont), |
@@ -453,6 +714,19 @@ static const luaL_Reg rocklib[] = | |||
453 | R(lcd_vline), | 714 | R(lcd_vline), |
454 | R(lcd_drawrect), | 715 | R(lcd_drawrect), |
455 | R(lcd_fillrect), | 716 | R(lcd_fillrect), |
717 | #if LCD_DEPTH > 1 | ||
718 | R(lcd_set_foreground), | ||
719 | R(lcd_get_foreground), | ||
720 | R(lcd_set_background), | ||
721 | R(lcd_get_background), | ||
722 | R(lcd_get_backdrop), | ||
723 | R(lcd_bitmap_part), | ||
724 | R(lcd_bitmap), | ||
725 | #endif | ||
726 | #if LCD_DEPTH == 16 | ||
727 | R(lcd_bitmap_transparent_part), | ||
728 | R(lcd_bitmap_transparent), | ||
729 | #endif | ||
456 | #endif | 730 | #endif |
457 | 731 | ||
458 | /* File handling */ | 732 | /* File handling */ |
@@ -498,6 +772,10 @@ static const luaL_Reg rocklib[] = | |||
498 | R(backlight_set_brightness), | 772 | R(backlight_set_brightness), |
499 | #endif | 773 | #endif |
500 | 774 | ||
775 | R(font_getstringsize), | ||
776 | |||
777 | {"new_image", rli_new}, | ||
778 | |||
501 | {NULL, NULL} | 779 | {NULL, NULL} |
502 | }; | 780 | }; |
503 | #undef R | 781 | #undef R |
@@ -525,6 +803,8 @@ LUALIB_API int luaopen_rock(lua_State *L) | |||
525 | RB_CONSTANT(SEEK_CUR); | 803 | RB_CONSTANT(SEEK_CUR); |
526 | RB_CONSTANT(SEEK_END); | 804 | RB_CONSTANT(SEEK_END); |
527 | 805 | ||
806 | rli_init(L); | ||
807 | |||
528 | return 1; | 808 | return 1; |
529 | } | 809 | } |
530 | 810 | ||