Rockbox open source high quality audio player as a Music Player Daemon
mpris rockbox mpd libadwaita audio rust zig deno
2
fork

Configure Feed

Select the types of activity you want to include in your feed.

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

+82 -82
+71 -79
apps/plugins/lua/rocklib.c
··· 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 58 #define SIMPLE_VOID_WRAPPER(func) RB_WRAP(func) { (void)L; func(); return 0; } 59 59 60 60 /* Helper function for opt_viewport */ ··· 66 66 { 67 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 71 if(is_unsigned) 72 72 *(unsigned*)res = (unsigned) val; ··· 123 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 126 RB_WRAP(kbd_input) 140 127 { 141 128 luaL_Buffer b; 142 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 132 char *buffer = luaL_prepbuffer(&b); 146 133 147 134 if(input != NULL) ··· 161 148 } 162 149 163 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 162 RB_WRAP(touchscreen_set_mode) 165 163 { 166 164 enum touchscreen_mode mode = luaL_checkint(L, 1); 167 165 rb->touchscreen_set_mode(mode); 168 166 return 0; 169 167 } 168 + 170 169 RB_WRAP(touchscreen_get_mode) 171 170 { 172 171 lua_pushinteger(L, rb->touchscreen_get_mode()); ··· 210 209 int i; 211 210 luaL_checktype(L, pos, LUA_TTABLE); 212 211 int n = luaL_getn(L, pos); 213 - const char **lines = (const char**) tlsf_malloc(n * sizeof(const char*)); 214 - if(lines == NULL) 215 - luaL_error(L, ERR_NO_ALLOC_DBYTES, n * sizeof(const char*)); 212 + 213 + const char **lines = (const char**) lua_newuserdata(L, n * sizeof(const char*)); 214 + 216 215 for(i=1; i<=n; i++) 217 216 { 218 217 lua_rawgeti(L, pos, i); ··· 228 227 struct text_message main_message, yes_message, no_message; 229 228 struct text_message *yes = NULL, *no = NULL; 230 229 230 + lua_settop(L, 3); /* newuserdata will be pushed onto stack after args*/ 231 231 fill_text_message(L, &main_message, 1); 232 232 if(!lua_isnoneornil(L, 2)) 233 233 fill_text_message(L, (yes = &yes_message), 2); ··· 236 236 237 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 239 lua_pushinteger(L, result); 246 240 return 1; 247 241 } ··· 256 250 257 251 title = luaL_checkstring(L, 1); 258 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 255 n = luaL_getn(L, 2); 262 - items = (const char**) tlsf_malloc(n * sizeof(const char*)); 263 - if(items == NULL) 264 - luaL_error(L, ERR_NO_ALLOC_DBYTES, n * sizeof(const char*)); 256 + 257 + /* newuserdata will be pushed onto stack after args*/ 258 + items = (const char**) lua_newuserdata(L, n * sizeof(const char*)); 259 + 265 260 for(i=1; i<=n; i++) 266 261 { 267 262 lua_rawgeti(L, 2, i); /* Push item on the stack */ ··· 274 269 menu_desc.desc = (unsigned char*) title; 275 270 276 271 int result = rb->do_menu(&menu, &start_selected, NULL, false); 277 - 278 - tlsf_free(items); 279 272 280 273 lua_pushinteger(L, result); 281 274 return 1; ··· 336 329 337 330 SIMPLE_VOID_WRAPPER(backlight_force_on); 338 331 SIMPLE_VOID_WRAPPER(backlight_use_settings); 332 + 339 333 #ifdef HAVE_REMOTE_LCD 340 334 SIMPLE_VOID_WRAPPER(remote_backlight_force_on); 341 335 SIMPLE_VOID_WRAPPER(remote_backlight_use_settings); 342 336 #endif 337 + 343 338 #ifdef HAVE_BUTTON_LIGHT 344 339 SIMPLE_VOID_WRAPPER(buttonlight_force_on); 345 340 SIMPLE_VOID_WRAPPER(buttonlight_use_settings); 346 341 #endif 342 + 347 343 #ifdef HAVE_BACKLIGHT_BRIGHTNESS 348 344 RB_WRAP(backlight_brightness_set) 349 345 { ··· 360 356 static const struct button_mapping *m1[] = { pla_main_ctx }; 361 357 int timeout = luaL_checkint(L, 1); 362 358 int btn; 359 + 363 360 #ifdef HAVE_REMOTE_LCD 364 361 static const struct button_mapping *m2[] = { pla_main_ctx, pla_remote_ctx }; 365 362 bool with_remote = luaL_optint(L, 2, 0); ··· 373 370 return 1; 374 371 } 375 372 376 - #define R(NAME) {#NAME, rock_##NAME} 373 + #define RB_FUNC(func) {#func, rock_##func} 377 374 static const luaL_Reg rocklib[] = 378 375 { 379 376 /* Kernel */ 380 - R(current_tick), 377 + RB_FUNC(current_tick), 381 378 382 379 /* Buttons */ 383 380 #ifdef HAVE_TOUCHSCREEN 384 - R(action_get_touchscreen_press), 385 - R(touchscreen_set_mode), 386 - R(touchscreen_get_mode), 381 + RB_FUNC(action_get_touchscreen_press), 382 + RB_FUNC(touchscreen_set_mode), 383 + RB_FUNC(touchscreen_get_mode), 387 384 #endif 388 - R(kbd_input), 389 385 390 - R(font_getstringsize), 391 - R(set_viewport), 392 - R(clear_viewport), 393 - R(current_path), 394 - R(gui_syncyesno_run), 395 - R(playlist_sync), 396 - R(playlist_remove_all_tracks), 397 - R(playlist_insert_track), 398 - R(playlist_insert_directory), 399 - R(do_menu), 386 + RB_FUNC(kbd_input), 387 + 388 + RB_FUNC(font_getstringsize), 389 + RB_FUNC(set_viewport), 390 + RB_FUNC(clear_viewport), 391 + RB_FUNC(current_path), 392 + RB_FUNC(gui_syncyesno_run), 393 + RB_FUNC(playlist_sync), 394 + RB_FUNC(playlist_remove_all_tracks), 395 + RB_FUNC(playlist_insert_track), 396 + RB_FUNC(playlist_insert_directory), 397 + RB_FUNC(do_menu), 400 398 401 399 /* Backlight helper */ 402 - R(backlight_force_on), 403 - R(backlight_use_settings), 400 + RB_FUNC(backlight_force_on), 401 + RB_FUNC(backlight_use_settings), 402 + 404 403 #ifdef HAVE_REMOTE_LCD 405 - R(remote_backlight_force_on), 406 - R(remote_backlight_use_settings), 404 + RB_FUNC(remote_backlight_force_on), 405 + RB_FUNC(remote_backlight_use_settings), 407 406 #endif 407 + 408 408 #ifdef HAVE_BUTTON_LIGHT 409 - R(buttonlight_force_on), 410 - R(buttonlight_use_settings), 409 + RB_FUNC(buttonlight_force_on), 410 + RB_FUNC(buttonlight_use_settings), 411 411 #endif 412 + 412 413 #ifdef HAVE_BACKLIGHT_BRIGHTNESS 413 - R(backlight_brightness_set), 414 - R(backlight_brightness_use_setting), 414 + RB_FUNC(backlight_brightness_set), 415 + RB_FUNC(backlight_brightness_use_setting), 415 416 #endif 416 - R(get_plugin_action), 417 + 418 + RB_FUNC(get_plugin_action), 417 419 418 420 {NULL, NULL} 419 421 }; 420 - #undef R 422 + #undef RB_FUNC 423 + 421 424 extern const luaL_Reg rocklib_aux[]; 422 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 428 ** Open Rockbox library 439 429 */ ··· 448 438 /* useful integer constants */ 449 439 RB_CONSTANT(HZ), 450 440 441 + RB_CONSTANT(LCD_DEPTH), 442 + RB_CONSTANT(LCD_HEIGHT), 451 443 RB_CONSTANT(LCD_WIDTH), 452 - RB_CONSTANT(LCD_HEIGHT), 453 - RB_CONSTANT(LCD_DEPTH), 454 444 455 445 RB_CONSTANT(FONT_SYSFIXED), 456 446 RB_CONSTANT(FONT_UI), 457 447 458 - RB_CONSTANT(PLAYLIST_PREPEND), 459 448 RB_CONSTANT(PLAYLIST_INSERT), 460 449 RB_CONSTANT(PLAYLIST_INSERT_LAST), 461 450 RB_CONSTANT(PLAYLIST_INSERT_FIRST), 451 + RB_CONSTANT(PLAYLIST_INSERT_LAST_SHUFFLED), 462 452 RB_CONSTANT(PLAYLIST_INSERT_SHUFFLED), 453 + RB_CONSTANT(PLAYLIST_PREPEND), 463 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 457 RB_CONSTANT(SCREEN_MAIN), 472 458 #ifdef HAVE_REMOTE_LCD 473 459 RB_CONSTANT(SCREEN_REMOTE), 474 460 #endif 461 + 462 + #ifdef HAVE_TOUCHSCREEN 463 + RB_CONSTANT(TOUCHSCREEN_POINT), 464 + RB_CONSTANT(TOUCHSCREEN_BUTTON), 465 + #endif 466 + 475 467 {NULL, 0} 476 468 }; 477 469 ··· 484 476 static const struct lua_str_reg rlib_const_str[] = 485 477 { 486 478 /* some useful paths constants */ 487 - RB_STRING_CONSTANT(ROCKBOX_DIR), 488 479 RB_STRING_CONSTANT(HOME_DIR), 489 480 RB_STRING_CONSTANT(PLUGIN_DIR), 490 481 RB_STRING_CONSTANT(PLUGIN_APPS_DATA_DIR), 491 482 RB_STRING_CONSTANT(PLUGIN_GAMES_DATA_DIR), 492 483 RB_STRING_CONSTANT(PLUGIN_DATA_DIR), 484 + RB_STRING_CONSTANT(ROCKBOX_DIR), 493 485 RB_STRING_CONSTANT(VIEWERS_DATA_DIR), 494 486 {NULL,NULL} 495 487 };
+11 -3
apps/plugins/lua/rocklib.h
··· 32 32 #define ERR_DATA_OVF "data overflow" 33 33 #endif 34 34 35 - #ifndef ERR_NO_ALLOC_DBYTES 36 - #define ERR_NO_ALLOC_DBYTES "Can't allocate %d bytes!" 37 - #endif 35 + #define RB_CONSTANT(x) {#x, x} 36 + #define RB_STRING_CONSTANT(x) RB_CONSTANT(x) 38 37 38 + struct lua_int_reg { 39 + char const* name; 40 + const int value; 41 + }; 42 + 43 + struct lua_str_reg { 44 + char const* name; 45 + char const* value; 46 + }; 39 47 40 48 LUALIB_API int (luaopen_rock) (lua_State *L); 41 49 const char* get_current_path(lua_State *L, int level);