summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--apps/plugins/lua/lauxlib.c48
-rw-r--r--apps/plugins/lua/rocklib.c34
-rw-r--r--apps/plugins/lua/rocklib.h1
3 files changed, 61 insertions, 22 deletions
diff --git a/apps/plugins/lua/lauxlib.c b/apps/plugins/lua/lauxlib.c
index fd71e07217..3f6d8e6f1b 100644
--- a/apps/plugins/lua/lauxlib.c
+++ b/apps/plugins/lua/lauxlib.c
@@ -560,24 +560,35 @@ static int errfile (lua_State *L, const char *what, int fnameindex) {
560 return LUA_ERRFILE; 560 return LUA_ERRFILE;
561} 561}
562 562
563static void make_path(char* dest, size_t dest_size, char* curfile, char* newfile) 563bool get_cur_path(lua_State *L, char* dest, size_t dest_size)
564{ 564{
565 char* pos = rb->strrchr(curfile, '/'); 565 lua_Debug ar;
566 if(pos != NULL) 566 if(lua_getstack(L, 1, &ar))
567 { 567 {
568 unsigned int len = (unsigned int)(pos - curfile); 568 /* Try determining the base path of the current Lua chunk
569 len = len + 1 > dest_size ? dest_size - 1 : len; 569 and write it to dest. */
570 570 lua_getinfo(L, "S", &ar);
571 if(len > 0) 571
572 memcpy(dest, curfile, len); 572 char* curfile = (char*) &ar.source[1];
573 573 char* pos = rb->strrchr(curfile, '/');
574 dest[len] = '/'; 574 if(pos != NULL)
575 dest[len+1] = '\0'; 575 {
576 unsigned int len = (unsigned int)(pos - curfile);
577 len = len + 1 > dest_size ? dest_size - 1 : len;
578
579 if(len > 0)
580 memcpy(dest, curfile, len);
581
582 dest[len] = '/';
583 dest[len+1] = '\0';
584
585 return true;
586 }
587 else
588 return false;
576 } 589 }
577 else 590 else
578 dest[0] = '\0'; 591 return false;
579
580 strncat(dest, newfile, dest_size - strlen(dest));
581} 592}
582 593
583LUALIB_API int luaL_loadfile (lua_State *L, const char *filename) { 594LUALIB_API int luaL_loadfile (lua_State *L, const char *filename) {
@@ -590,13 +601,8 @@ LUALIB_API int luaL_loadfile (lua_State *L, const char *filename) {
590 if(lf.f < 0) { 601 if(lf.f < 0) {
591 /* Fallback */ 602 /* Fallback */
592 603
593 lua_Debug ar; 604 if(get_cur_path(L, buffer, sizeof(buffer))) {
594 if(lua_getstack(L, 1, &ar)) { 605 strncat(buffer, filename, sizeof(buffer) - strlen(buffer));
595 lua_getinfo(L, "S", &ar);
596
597 /* Try determining the base path of the current Lua chunk
598 and prepend it to filename in buffer. */
599 make_path(buffer, sizeof(buffer), (char*)&ar.source[1], (char*)filename);
600 lf.f = rb->open(buffer, O_RDONLY); 606 lf.f = rb->open(buffer, O_RDONLY);
601 } 607 }
602 608
diff --git a/apps/plugins/lua/rocklib.c b/apps/plugins/lua/rocklib.c
index 22e86f91b7..d501694b46 100644
--- a/apps/plugins/lua/rocklib.c
+++ b/apps/plugins/lua/rocklib.c
@@ -619,6 +619,15 @@ RB_WRAP(kbd_input)
619 return 1; 619 return 1;
620} 620}
621 621
622#ifdef HAVE_TOUCHSCREEN
623RB_WRAP(touchscreen_set_mode)
624{
625 enum touchscreen_mode mode = luaL_checkint(L, 1);
626 rb->touchscreen_set_mode(mode);
627 return 0;
628}
629#endif
630
622RB_WRAP(backlight_on) 631RB_WRAP(backlight_on)
623{ 632{
624 (void)L; 633 (void)L;
@@ -827,6 +836,18 @@ RB_WRAP(read_bmp_file)
827 return 0; 836 return 0;
828} 837}
829 838
839RB_WRAP(current_path)
840{
841 char buffer[MAX_PATH];
842 if(get_cur_path(L, buffer, sizeof(buffer)))
843 {
844 lua_pushstring(L, buffer);
845 return 1;
846 }
847 else
848 return 0;
849}
850
830#define R(NAME) {#NAME, rock_##NAME} 851#define R(NAME) {#NAME, rock_##NAME}
831static const luaL_Reg rocklib[] = 852static const luaL_Reg rocklib[] =
832{ 853{
@@ -901,6 +922,7 @@ static const luaL_Reg rocklib[] =
901 R(action_userabort), 922 R(action_userabort),
902#ifdef HAVE_TOUCHSCREEN 923#ifdef HAVE_TOUCHSCREEN
903 R(action_get_touchscreen_press), 924 R(action_get_touchscreen_press),
925 R(touchscreen_set_mode),
904#endif 926#endif
905 R(kbd_input), 927 R(kbd_input),
906 928
@@ -916,6 +938,7 @@ static const luaL_Reg rocklib[] =
916 R(read_bmp_file), 938 R(read_bmp_file),
917 R(set_viewport), 939 R(set_viewport),
918 R(clear_viewport), 940 R(clear_viewport),
941 R(current_path),
919 942
920 {"new_image", rli_new}, 943 {"new_image", rli_new},
921 944
@@ -945,10 +968,19 @@ LUALIB_API int luaopen_rock(lua_State *L)
945 RB_CONSTANT(SEEK_SET); 968 RB_CONSTANT(SEEK_SET);
946 RB_CONSTANT(SEEK_CUR); 969 RB_CONSTANT(SEEK_CUR);
947 RB_CONSTANT(SEEK_END); 970 RB_CONSTANT(SEEK_END);
948 971
949 RB_CONSTANT(FONT_SYSFIXED); 972 RB_CONSTANT(FONT_SYSFIXED);
950 RB_CONSTANT(FONT_UI); 973 RB_CONSTANT(FONT_UI);
951 974
975#ifdef HAVE_TOUCHSCREEN
976 RB_CONSTANT(TOUCHSCREEN_POINT);
977 RB_CONSTANT(TOUCHSCREEN_BUTTON);
978 RB_CONSTANT(BUTTON_TOUCHSCREEN);
979#endif
980
981 RB_CONSTANT(BUTTON_REL);
982 RB_CONSTANT(BUTTON_REPEAT);
983
952 rli_init(L); 984 rli_init(L);
953 985
954 return 1; 986 return 1;
diff --git a/apps/plugins/lua/rocklib.h b/apps/plugins/lua/rocklib.h
index b5817f2f57..4a1e79cc8a 100644
--- a/apps/plugins/lua/rocklib.h
+++ b/apps/plugins/lua/rocklib.h
@@ -24,6 +24,7 @@
24 24
25#define LUA_ROCKLIBNAME "rb" 25#define LUA_ROCKLIBNAME "rb"
26LUALIB_API int (luaopen_rock) (lua_State *L); 26LUALIB_API int (luaopen_rock) (lua_State *L);
27bool get_cur_path(lua_State *L, char* dest, size_t dest_size);
27 28
28#endif /* _ROCKLIB_H_ */ 29#endif /* _ROCKLIB_H_ */
29 30