diff options
author | Maurus Cuelenaere <mcuelenaere@gmail.com> | 2009-05-21 19:01:41 +0000 |
---|---|---|
committer | Maurus Cuelenaere <mcuelenaere@gmail.com> | 2009-05-21 19:01:41 +0000 |
commit | cf87597226f5d6b269f1f2c4d6f402aa1eccb852 (patch) | |
tree | 4ba1f3ae53b3bd9cae0e2c6c4dd57836b43a5ece /apps/plugins/lua/rocklua.c | |
parent | c483efadc63eaed35b5fb5e4e02c2282daf32470 (diff) | |
download | rockbox-cf87597226f5d6b269f1f2c4d6f402aa1eccb852.tar.gz rockbox-cf87597226f5d6b269f1f2c4d6f402aa1eccb852.zip |
Commit FS#9174: Lua scripting language by Dan Everton
git-svn-id: svn://svn.rockbox.org/rockbox/trunk@21020 a1c6a512-1295-4272-9138-f99709370657
Diffstat (limited to 'apps/plugins/lua/rocklua.c')
-rw-r--r-- | apps/plugins/lua/rocklua.c | 105 |
1 files changed, 105 insertions, 0 deletions
diff --git a/apps/plugins/lua/rocklua.c b/apps/plugins/lua/rocklua.c new file mode 100644 index 0000000000..4bb89ae29b --- /dev/null +++ b/apps/plugins/lua/rocklua.c | |||
@@ -0,0 +1,105 @@ | |||
1 | /*************************************************************************** | ||
2 | * __________ __ ___. | ||
3 | * Open \______ \ ____ ____ | | _\_ |__ _______ ___ | ||
4 | * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ / | ||
5 | * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < < | ||
6 | * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \ | ||
7 | * \/ \/ \/ \/ \/ | ||
8 | * $Id$ | ||
9 | * | ||
10 | * Copyright (C) 2008 Dan Everton (safetydan) | ||
11 | * | ||
12 | * This program is free software; you can redistribute it and/or | ||
13 | * modify it under the terms of the GNU General Public License | ||
14 | * as published by the Free Software Foundation; either version 2 | ||
15 | * of the License, or (at your option) any later version. | ||
16 | * | ||
17 | * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY | ||
18 | * KIND, either express or implied. | ||
19 | * | ||
20 | ****************************************************************************/ | ||
21 | |||
22 | #include "plugin.h" | ||
23 | #include "lib/pluginlib_exit.h" | ||
24 | #include "lua.h" | ||
25 | #include "lauxlib.h" | ||
26 | #include "lualib.h" | ||
27 | #include "rocklib.h" | ||
28 | #include "rockmalloc.h" | ||
29 | |||
30 | PLUGIN_HEADER | ||
31 | |||
32 | static const luaL_Reg lualibs[] = { | ||
33 | {"", luaopen_base}, | ||
34 | {LUA_TABLIBNAME, luaopen_table}, | ||
35 | {LUA_STRLIBNAME, luaopen_string}, | ||
36 | {LUA_ROCKLIBNAME, luaopen_rock}, | ||
37 | {NULL, NULL} | ||
38 | }; | ||
39 | |||
40 | static void rocklua_openlibs(lua_State *L) { | ||
41 | const luaL_Reg *lib = lualibs; | ||
42 | for (; lib->func; lib++) { | ||
43 | lua_pushcfunction(L, lib->func); | ||
44 | lua_pushstring(L, lib->name); | ||
45 | lua_call(L, 1, 0); | ||
46 | } | ||
47 | } | ||
48 | |||
49 | char curpath[MAX_PATH]; | ||
50 | static void fill_curpath(const char* filename) | ||
51 | { | ||
52 | char* pos = rb->strrchr(filename, '/'); | ||
53 | |||
54 | if(pos != NULL) | ||
55 | { | ||
56 | int len = (int)(pos - filename); | ||
57 | |||
58 | if(len > 0) | ||
59 | memcpy(curpath, filename, len); | ||
60 | |||
61 | curpath[len] = '\0'; | ||
62 | } | ||
63 | } | ||
64 | |||
65 | |||
66 | /***************** Plugin Entry Point *****************/ | ||
67 | enum plugin_status plugin_start(const void* parameter) | ||
68 | { | ||
69 | const char* filename; | ||
70 | int status; | ||
71 | |||
72 | PLUGINLIB_EXIT_INIT | ||
73 | |||
74 | if (parameter == NULL) | ||
75 | { | ||
76 | rb->splash(HZ, "Play a .lua file!"); | ||
77 | return PLUGIN_ERROR; | ||
78 | } | ||
79 | else | ||
80 | { | ||
81 | filename = (char*) parameter; | ||
82 | fill_curpath(filename); | ||
83 | |||
84 | lua_State *L = luaL_newstate(); | ||
85 | |||
86 | rocklua_openlibs(L); | ||
87 | status = luaL_loadfile(L, filename); | ||
88 | if (!status) { | ||
89 | rb->lcd_clear_display(); | ||
90 | status = lua_pcall(L, 0, 0, 0); | ||
91 | } | ||
92 | |||
93 | dlmalloc_stats(); | ||
94 | |||
95 | if (status) { | ||
96 | rb->splashf(5 * HZ, "%s", lua_tostring(L, -1)); | ||
97 | lua_pop(L, 1); | ||
98 | } | ||
99 | |||
100 | lua_close(L); | ||
101 | } | ||
102 | |||
103 | return PLUGIN_OK; | ||
104 | } | ||
105 | |||