diff options
author | William Wilgus <me.theuser@yahoo.com> | 2018-09-17 19:28:10 +0200 |
---|---|---|
committer | William Wilgus <me.theuser@yahoo.com> | 2018-10-08 23:15:16 +0200 |
commit | e4b843335bc34810c11ed405d473e6f7c94a153f (patch) | |
tree | 5ebfe0f33aaf026d55a7962ae4f710cd5d846628 /apps/plugins/lua/rocklib.c | |
parent | ebaddc671e69f12c1933d107b5400002c2364479 (diff) | |
download | rockbox-e4b843335bc34810c11ed405d473e6f7c94a153f.tar.gz rockbox-e4b843335bc34810c11ed405d473e6f7c94a153f.zip |
lua rocklib cleanup
removes tslf allocations from do_menu and gui_syncyesno_run in favor of
lua_newuserdata
removes some luaL_opt functions in favor of equivalent lua_to functions
moves some definitions to the rocklib.h file
Change-Id: Iaacc3249b8f1af2c220ce59dead0050c66cb3b04
Diffstat (limited to 'apps/plugins/lua/rocklib.c')
-rw-r--r-- | apps/plugins/lua/rocklib.c | 152 |
1 files changed, 72 insertions, 80 deletions
diff --git a/apps/plugins/lua/rocklib.c b/apps/plugins/lua/rocklib.c index ad8577cb43..57165a5d15 100644 --- a/apps/plugins/lua/rocklib.c +++ b/apps/plugins/lua/rocklib.c | |||
@@ -54,7 +54,7 @@ | |||
54 | * ----------------------------- | 54 | * ----------------------------- |
55 | */ | 55 | */ |
56 | 56 | ||
57 | #define RB_WRAP(M) static int rock_##M(lua_State UNUSED_ATTR *L) | 57 | #define RB_WRAP(func) static int rock_##func(lua_State UNUSED_ATTR *L) |
58 | #define SIMPLE_VOID_WRAPPER(func) RB_WRAP(func) { (void)L; func(); return 0; } | 58 | #define SIMPLE_VOID_WRAPPER(func) RB_WRAP(func) { (void)L; func(); return 0; } |
59 | 59 | ||
60 | /* Helper function for opt_viewport */ | 60 | /* Helper function for opt_viewport */ |
@@ -66,7 +66,7 @@ static void check_tablevalue(lua_State *L, | |||
66 | { | 66 | { |
67 | lua_getfield(L, tablepos, key); /* Find table[key] */ | 67 | lua_getfield(L, tablepos, key); /* Find table[key] */ |
68 | 68 | ||
69 | int val = luaL_optint(L, -1, 0); | 69 | int val = lua_tointeger(L, -1); |
70 | 70 | ||
71 | if(is_unsigned) | 71 | if(is_unsigned) |
72 | *(unsigned*)res = (unsigned) val; | 72 | *(unsigned*)res = (unsigned) val; |
@@ -123,25 +123,12 @@ RB_WRAP(current_tick) | |||
123 | return 1; | 123 | return 1; |
124 | } | 124 | } |
125 | 125 | ||
126 | #ifdef HAVE_TOUCHSCREEN | ||
127 | RB_WRAP(action_get_touchscreen_press) | ||
128 | { | ||
129 | short x, y; | ||
130 | int result = rb->action_get_touchscreen_press(&x, &y); | ||
131 | |||
132 | lua_pushinteger(L, result); | ||
133 | lua_pushinteger(L, x); | ||
134 | lua_pushinteger(L, y); | ||
135 | return 3; | ||
136 | } | ||
137 | #endif | ||
138 | |||
139 | RB_WRAP(kbd_input) | 126 | RB_WRAP(kbd_input) |
140 | { | 127 | { |
141 | luaL_Buffer b; | 128 | luaL_Buffer b; |
142 | luaL_buffinit(L, &b); | 129 | luaL_buffinit(L, &b); |
143 | 130 | ||
144 | const char *input = luaL_optstring(L, 1, NULL); | 131 | const char *input = lua_tostring(L, 1); |
145 | char *buffer = luaL_prepbuffer(&b); | 132 | char *buffer = luaL_prepbuffer(&b); |
146 | 133 | ||
147 | if(input != NULL) | 134 | if(input != NULL) |
@@ -161,12 +148,24 @@ RB_WRAP(kbd_input) | |||
161 | } | 148 | } |
162 | 149 | ||
163 | #ifdef HAVE_TOUCHSCREEN | 150 | #ifdef HAVE_TOUCHSCREEN |
151 | RB_WRAP(action_get_touchscreen_press) | ||
152 | { | ||
153 | short x, y; | ||
154 | int result = rb->action_get_touchscreen_press(&x, &y); | ||
155 | |||
156 | lua_pushinteger(L, result); | ||
157 | lua_pushinteger(L, x); | ||
158 | lua_pushinteger(L, y); | ||
159 | return 3; | ||
160 | } | ||
161 | |||
164 | RB_WRAP(touchscreen_set_mode) | 162 | RB_WRAP(touchscreen_set_mode) |
165 | { | 163 | { |
166 | enum touchscreen_mode mode = luaL_checkint(L, 1); | 164 | enum touchscreen_mode mode = luaL_checkint(L, 1); |
167 | rb->touchscreen_set_mode(mode); | 165 | rb->touchscreen_set_mode(mode); |
168 | return 0; | 166 | return 0; |
169 | } | 167 | } |
168 | |||
170 | RB_WRAP(touchscreen_get_mode) | 169 | RB_WRAP(touchscreen_get_mode) |
171 | { | 170 | { |
172 | lua_pushinteger(L, rb->touchscreen_get_mode()); | 171 | lua_pushinteger(L, rb->touchscreen_get_mode()); |
@@ -210,9 +209,9 @@ static void fill_text_message(lua_State *L, struct text_message * message, | |||
210 | int i; | 209 | int i; |
211 | luaL_checktype(L, pos, LUA_TTABLE); | 210 | luaL_checktype(L, pos, LUA_TTABLE); |
212 | int n = luaL_getn(L, pos); | 211 | int n = luaL_getn(L, pos); |
213 | const char **lines = (const char**) tlsf_malloc(n * sizeof(const char*)); | 212 | |
214 | if(lines == NULL) | 213 | const char **lines = (const char**) lua_newuserdata(L, n * sizeof(const char*)); |
215 | luaL_error(L, ERR_NO_ALLOC_DBYTES, n * sizeof(const char*)); | 214 | |
216 | for(i=1; i<=n; i++) | 215 | for(i=1; i<=n; i++) |
217 | { | 216 | { |
218 | lua_rawgeti(L, pos, i); | 217 | lua_rawgeti(L, pos, i); |
@@ -228,6 +227,7 @@ RB_WRAP(gui_syncyesno_run) | |||
228 | struct text_message main_message, yes_message, no_message; | 227 | struct text_message main_message, yes_message, no_message; |
229 | struct text_message *yes = NULL, *no = NULL; | 228 | struct text_message *yes = NULL, *no = NULL; |
230 | 229 | ||
230 | lua_settop(L, 3); /* newuserdata will be pushed onto stack after args*/ | ||
231 | fill_text_message(L, &main_message, 1); | 231 | fill_text_message(L, &main_message, 1); |
232 | if(!lua_isnoneornil(L, 2)) | 232 | if(!lua_isnoneornil(L, 2)) |
233 | fill_text_message(L, (yes = &yes_message), 2); | 233 | fill_text_message(L, (yes = &yes_message), 2); |
@@ -236,12 +236,6 @@ RB_WRAP(gui_syncyesno_run) | |||
236 | 236 | ||
237 | enum yesno_res result = rb->gui_syncyesno_run(&main_message, yes, no); | 237 | enum yesno_res result = rb->gui_syncyesno_run(&main_message, yes, no); |
238 | 238 | ||
239 | tlsf_free(main_message.message_lines); | ||
240 | if(yes) | ||
241 | tlsf_free(yes_message.message_lines); | ||
242 | if(no) | ||
243 | tlsf_free(no_message.message_lines); | ||
244 | |||
245 | lua_pushinteger(L, result); | 239 | lua_pushinteger(L, result); |
246 | return 1; | 240 | return 1; |
247 | } | 241 | } |
@@ -256,12 +250,13 @@ RB_WRAP(do_menu) | |||
256 | 250 | ||
257 | title = luaL_checkstring(L, 1); | 251 | title = luaL_checkstring(L, 1); |
258 | luaL_checktype(L, 2, LUA_TTABLE); | 252 | luaL_checktype(L, 2, LUA_TTABLE); |
259 | start_selected = luaL_optint(L, 3, 0); | 253 | start_selected = lua_tointeger(L, 3); |
260 | 254 | ||
261 | n = luaL_getn(L, 2); | 255 | n = luaL_getn(L, 2); |
262 | items = (const char**) tlsf_malloc(n * sizeof(const char*)); | 256 | |
263 | if(items == NULL) | 257 | /* newuserdata will be pushed onto stack after args*/ |
264 | luaL_error(L, ERR_NO_ALLOC_DBYTES, n * sizeof(const char*)); | 258 | items = (const char**) lua_newuserdata(L, n * sizeof(const char*)); |
259 | |||
265 | for(i=1; i<=n; i++) | 260 | for(i=1; i<=n; i++) |
266 | { | 261 | { |
267 | lua_rawgeti(L, 2, i); /* Push item on the stack */ | 262 | lua_rawgeti(L, 2, i); /* Push item on the stack */ |
@@ -275,8 +270,6 @@ RB_WRAP(do_menu) | |||
275 | 270 | ||
276 | int result = rb->do_menu(&menu, &start_selected, NULL, false); | 271 | int result = rb->do_menu(&menu, &start_selected, NULL, false); |
277 | 272 | ||
278 | tlsf_free(items); | ||
279 | |||
280 | lua_pushinteger(L, result); | 273 | lua_pushinteger(L, result); |
281 | return 1; | 274 | return 1; |
282 | } | 275 | } |
@@ -336,14 +329,17 @@ RB_WRAP(playlist_insert_directory) | |||
336 | 329 | ||
337 | SIMPLE_VOID_WRAPPER(backlight_force_on); | 330 | SIMPLE_VOID_WRAPPER(backlight_force_on); |
338 | SIMPLE_VOID_WRAPPER(backlight_use_settings); | 331 | SIMPLE_VOID_WRAPPER(backlight_use_settings); |
332 | |||
339 | #ifdef HAVE_REMOTE_LCD | 333 | #ifdef HAVE_REMOTE_LCD |
340 | SIMPLE_VOID_WRAPPER(remote_backlight_force_on); | 334 | SIMPLE_VOID_WRAPPER(remote_backlight_force_on); |
341 | SIMPLE_VOID_WRAPPER(remote_backlight_use_settings); | 335 | SIMPLE_VOID_WRAPPER(remote_backlight_use_settings); |
342 | #endif | 336 | #endif |
337 | |||
343 | #ifdef HAVE_BUTTON_LIGHT | 338 | #ifdef HAVE_BUTTON_LIGHT |
344 | SIMPLE_VOID_WRAPPER(buttonlight_force_on); | 339 | SIMPLE_VOID_WRAPPER(buttonlight_force_on); |
345 | SIMPLE_VOID_WRAPPER(buttonlight_use_settings); | 340 | SIMPLE_VOID_WRAPPER(buttonlight_use_settings); |
346 | #endif | 341 | #endif |
342 | |||
347 | #ifdef HAVE_BACKLIGHT_BRIGHTNESS | 343 | #ifdef HAVE_BACKLIGHT_BRIGHTNESS |
348 | RB_WRAP(backlight_brightness_set) | 344 | RB_WRAP(backlight_brightness_set) |
349 | { | 345 | { |
@@ -360,6 +356,7 @@ RB_WRAP(get_plugin_action) | |||
360 | static const struct button_mapping *m1[] = { pla_main_ctx }; | 356 | static const struct button_mapping *m1[] = { pla_main_ctx }; |
361 | int timeout = luaL_checkint(L, 1); | 357 | int timeout = luaL_checkint(L, 1); |
362 | int btn; | 358 | int btn; |
359 | |||
363 | #ifdef HAVE_REMOTE_LCD | 360 | #ifdef HAVE_REMOTE_LCD |
364 | static const struct button_mapping *m2[] = { pla_main_ctx, pla_remote_ctx }; | 361 | static const struct button_mapping *m2[] = { pla_main_ctx, pla_remote_ctx }; |
365 | bool with_remote = luaL_optint(L, 2, 0); | 362 | bool with_remote = luaL_optint(L, 2, 0); |
@@ -373,67 +370,60 @@ RB_WRAP(get_plugin_action) | |||
373 | return 1; | 370 | return 1; |
374 | } | 371 | } |
375 | 372 | ||
376 | #define R(NAME) {#NAME, rock_##NAME} | 373 | #define RB_FUNC(func) {#func, rock_##func} |
377 | static const luaL_Reg rocklib[] = | 374 | static const luaL_Reg rocklib[] = |
378 | { | 375 | { |
379 | /* Kernel */ | 376 | /* Kernel */ |
380 | R(current_tick), | 377 | RB_FUNC(current_tick), |
381 | 378 | ||
382 | /* Buttons */ | 379 | /* Buttons */ |
383 | #ifdef HAVE_TOUCHSCREEN | 380 | #ifdef HAVE_TOUCHSCREEN |
384 | R(action_get_touchscreen_press), | 381 | RB_FUNC(action_get_touchscreen_press), |
385 | R(touchscreen_set_mode), | 382 | RB_FUNC(touchscreen_set_mode), |
386 | R(touchscreen_get_mode), | 383 | RB_FUNC(touchscreen_get_mode), |
387 | #endif | 384 | #endif |
388 | R(kbd_input), | 385 | |
389 | 386 | RB_FUNC(kbd_input), | |
390 | R(font_getstringsize), | 387 | |
391 | R(set_viewport), | 388 | RB_FUNC(font_getstringsize), |
392 | R(clear_viewport), | 389 | RB_FUNC(set_viewport), |
393 | R(current_path), | 390 | RB_FUNC(clear_viewport), |
394 | R(gui_syncyesno_run), | 391 | RB_FUNC(current_path), |
395 | R(playlist_sync), | 392 | RB_FUNC(gui_syncyesno_run), |
396 | R(playlist_remove_all_tracks), | 393 | RB_FUNC(playlist_sync), |
397 | R(playlist_insert_track), | 394 | RB_FUNC(playlist_remove_all_tracks), |
398 | R(playlist_insert_directory), | 395 | RB_FUNC(playlist_insert_track), |
399 | R(do_menu), | 396 | RB_FUNC(playlist_insert_directory), |
397 | RB_FUNC(do_menu), | ||
400 | 398 | ||
401 | /* Backlight helper */ | 399 | /* Backlight helper */ |
402 | R(backlight_force_on), | 400 | RB_FUNC(backlight_force_on), |
403 | R(backlight_use_settings), | 401 | RB_FUNC(backlight_use_settings), |
402 | |||
404 | #ifdef HAVE_REMOTE_LCD | 403 | #ifdef HAVE_REMOTE_LCD |
405 | R(remote_backlight_force_on), | 404 | RB_FUNC(remote_backlight_force_on), |
406 | R(remote_backlight_use_settings), | 405 | RB_FUNC(remote_backlight_use_settings), |
407 | #endif | 406 | #endif |
407 | |||
408 | #ifdef HAVE_BUTTON_LIGHT | 408 | #ifdef HAVE_BUTTON_LIGHT |
409 | R(buttonlight_force_on), | 409 | RB_FUNC(buttonlight_force_on), |
410 | R(buttonlight_use_settings), | 410 | RB_FUNC(buttonlight_use_settings), |
411 | #endif | 411 | #endif |
412 | |||
412 | #ifdef HAVE_BACKLIGHT_BRIGHTNESS | 413 | #ifdef HAVE_BACKLIGHT_BRIGHTNESS |
413 | R(backlight_brightness_set), | 414 | RB_FUNC(backlight_brightness_set), |
414 | R(backlight_brightness_use_setting), | 415 | RB_FUNC(backlight_brightness_use_setting), |
415 | #endif | 416 | #endif |
416 | R(get_plugin_action), | 417 | |
418 | RB_FUNC(get_plugin_action), | ||
417 | 419 | ||
418 | {NULL, NULL} | 420 | {NULL, NULL} |
419 | }; | 421 | }; |
420 | #undef R | 422 | #undef RB_FUNC |
423 | |||
421 | extern const luaL_Reg rocklib_aux[]; | 424 | extern const luaL_Reg rocklib_aux[]; |
422 | extern const luaL_Reg rocklib_img[]; | 425 | extern const luaL_Reg rocklib_img[]; |
423 | 426 | ||
424 | #define RB_CONSTANT(x) {#x, x} | ||
425 | #define RB_STRING_CONSTANT(x) {#x, x} | ||
426 | |||
427 | struct lua_int_reg { | ||
428 | char const* name; | ||
429 | int value; | ||
430 | }; | ||
431 | |||
432 | struct lua_str_reg { | ||
433 | char const* name; | ||
434 | char const* value; | ||
435 | }; | ||
436 | |||
437 | /* | 427 | /* |
438 | ** Open Rockbox library | 428 | ** Open Rockbox library |
439 | */ | 429 | */ |
@@ -448,30 +438,32 @@ LUALIB_API int luaopen_rock(lua_State *L) | |||
448 | /* useful integer constants */ | 438 | /* useful integer constants */ |
449 | RB_CONSTANT(HZ), | 439 | RB_CONSTANT(HZ), |
450 | 440 | ||
451 | RB_CONSTANT(LCD_WIDTH), | ||
452 | RB_CONSTANT(LCD_HEIGHT), | ||
453 | RB_CONSTANT(LCD_DEPTH), | 441 | RB_CONSTANT(LCD_DEPTH), |
442 | RB_CONSTANT(LCD_HEIGHT), | ||
443 | RB_CONSTANT(LCD_WIDTH), | ||
454 | 444 | ||
455 | RB_CONSTANT(FONT_SYSFIXED), | 445 | RB_CONSTANT(FONT_SYSFIXED), |
456 | RB_CONSTANT(FONT_UI), | 446 | RB_CONSTANT(FONT_UI), |
457 | 447 | ||
458 | RB_CONSTANT(PLAYLIST_PREPEND), | ||
459 | RB_CONSTANT(PLAYLIST_INSERT), | 448 | RB_CONSTANT(PLAYLIST_INSERT), |
460 | RB_CONSTANT(PLAYLIST_INSERT_LAST), | 449 | RB_CONSTANT(PLAYLIST_INSERT_LAST), |
461 | RB_CONSTANT(PLAYLIST_INSERT_FIRST), | 450 | RB_CONSTANT(PLAYLIST_INSERT_FIRST), |
451 | RB_CONSTANT(PLAYLIST_INSERT_LAST_SHUFFLED), | ||
462 | RB_CONSTANT(PLAYLIST_INSERT_SHUFFLED), | 452 | RB_CONSTANT(PLAYLIST_INSERT_SHUFFLED), |
453 | RB_CONSTANT(PLAYLIST_PREPEND), | ||
463 | RB_CONSTANT(PLAYLIST_REPLACE), | 454 | RB_CONSTANT(PLAYLIST_REPLACE), |
464 | RB_CONSTANT(PLAYLIST_INSERT_LAST_SHUFFLED), | ||
465 | 455 | ||
466 | #ifdef HAVE_TOUCHSCREEN | ||
467 | RB_CONSTANT(TOUCHSCREEN_POINT), | ||
468 | RB_CONSTANT(TOUCHSCREEN_BUTTON), | ||
469 | #endif | ||
470 | 456 | ||
471 | RB_CONSTANT(SCREEN_MAIN), | 457 | RB_CONSTANT(SCREEN_MAIN), |
472 | #ifdef HAVE_REMOTE_LCD | 458 | #ifdef HAVE_REMOTE_LCD |
473 | RB_CONSTANT(SCREEN_REMOTE), | 459 | RB_CONSTANT(SCREEN_REMOTE), |
474 | #endif | 460 | #endif |
461 | |||
462 | #ifdef HAVE_TOUCHSCREEN | ||
463 | RB_CONSTANT(TOUCHSCREEN_POINT), | ||
464 | RB_CONSTANT(TOUCHSCREEN_BUTTON), | ||
465 | #endif | ||
466 | |||
475 | {NULL, 0} | 467 | {NULL, 0} |
476 | }; | 468 | }; |
477 | 469 | ||
@@ -484,12 +476,12 @@ LUALIB_API int luaopen_rock(lua_State *L) | |||
484 | static const struct lua_str_reg rlib_const_str[] = | 476 | static const struct lua_str_reg rlib_const_str[] = |
485 | { | 477 | { |
486 | /* some useful paths constants */ | 478 | /* some useful paths constants */ |
487 | RB_STRING_CONSTANT(ROCKBOX_DIR), | ||
488 | RB_STRING_CONSTANT(HOME_DIR), | 479 | RB_STRING_CONSTANT(HOME_DIR), |
489 | RB_STRING_CONSTANT(PLUGIN_DIR), | 480 | RB_STRING_CONSTANT(PLUGIN_DIR), |
490 | RB_STRING_CONSTANT(PLUGIN_APPS_DATA_DIR), | 481 | RB_STRING_CONSTANT(PLUGIN_APPS_DATA_DIR), |
491 | RB_STRING_CONSTANT(PLUGIN_GAMES_DATA_DIR), | 482 | RB_STRING_CONSTANT(PLUGIN_GAMES_DATA_DIR), |
492 | RB_STRING_CONSTANT(PLUGIN_DATA_DIR), | 483 | RB_STRING_CONSTANT(PLUGIN_DATA_DIR), |
484 | RB_STRING_CONSTANT(ROCKBOX_DIR), | ||
493 | RB_STRING_CONSTANT(VIEWERS_DATA_DIR), | 485 | RB_STRING_CONSTANT(VIEWERS_DATA_DIR), |
494 | {NULL,NULL} | 486 | {NULL,NULL} |
495 | }; | 487 | }; |