summaryrefslogtreecommitdiff
path: root/apps/plugin.c
diff options
context:
space:
mode:
Diffstat (limited to 'apps/plugin.c')
-rw-r--r--apps/plugin.c221
1 files changed, 221 insertions, 0 deletions
diff --git a/apps/plugin.c b/apps/plugin.c
new file mode 100644
index 0000000000..6366580a56
--- /dev/null
+++ b/apps/plugin.c
@@ -0,0 +1,221 @@
1/***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
9 *
10 * Copyright (C) 2002 Björn Stenberg
11 *
12 * All files in this archive are subject to the GNU General Public License.
13 * See the file COPYING in the source tree root for full license agreement.
14 *
15 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
16 * KIND, either express or implied.
17 *
18 ****************************************************************************/
19#include <stdbool.h>
20#include <string.h>
21#include <stdio.h>
22#include "button.h"
23#include "lcd.h"
24#include "dir.h"
25#include "file.h"
26#include "kernel.h"
27#include "sprintf.h"
28#include "screens.h"
29#include "misc.h"
30#include "mas.h"
31#include "plugin.h"
32#include "lang.h"
33
34#ifdef SIMULATOR
35#include <dlfcn.h>
36#define PREFIX(_x_) x11_ ## _x_
37#else
38#define PREFIX(_x_) _x_
39#endif
40
41static int plugin_test(int api_version, int model);
42
43static struct plugin_api rockbox_api = {
44 PLUGIN_API_VERSION,
45
46 plugin_test,
47
48 /* lcd */
49 lcd_clear_display,
50 lcd_puts,
51 lcd_puts_scroll,
52 lcd_stop_scroll,
53#ifdef HAVE_LCD_CHARCELLS
54 lcd_define_pattern,
55#else
56 lcd_putsxy,
57 lcd_bitmap,
58 lcd_drawline,
59 lcd_clearline,
60 lcd_drawpixel,
61 lcd_clearpixel,
62 lcd_setfont,
63 lcd_clearrect,
64 lcd_fillrect,
65 lcd_drawrect,
66 lcd_invertrect,
67 lcd_getstringsize,
68 lcd_update,
69 lcd_update_rect,
70#ifndef SIMULATOR
71 lcd_roll,
72#endif
73#endif
74
75 /* button */
76 button_get,
77 button_get_w_tmo,
78
79 /* file */
80 PREFIX(open),
81 PREFIX(close),
82 read,
83 lseek,
84 PREFIX(creat),
85 write,
86 remove,
87 rename,
88 ftruncate,
89 PREFIX(filesize),
90 fprintf,
91 read_line,
92
93 /* dir */
94 PREFIX(opendir),
95 PREFIX(closedir),
96 PREFIX(readdir),
97
98 /* kernel */
99 PREFIX(sleep),
100 usb_screen,
101 &current_tick,
102
103 /* strings and memory */
104 snprintf,
105 strcpy,
106 strlen,
107 memset,
108 memcpy,
109
110 /* sound */
111#ifndef SIMULATOR
112#ifdef HAVE_MAS3587F
113 mas_codec_readreg,
114#endif
115#endif
116
117 /* misc */
118 srand,
119 rand,
120 splash,
121};
122
123int plugin_load(char* plugin, void* parameter)
124{
125 enum plugin_status (*plugin_start)(struct plugin_api* api, void* param);
126 int rc;
127 char buf[64];
128#ifdef SIMULATOR
129 void* pd;
130 char path[256];
131#else
132 extern unsigned char pluginbuf[];
133 int fd;
134#endif
135
136 lcd_clear_display();
137#ifdef HAVE_LCD_BITMAP
138 lcd_setmargins(0,0);
139 lcd_update();
140#endif
141#ifdef SIMULATOR
142 snprintf(path, sizeof path, "archos%s", plugin);
143 pd = dlopen(path, RTLD_NOW);
144 if (!pd) {
145 snprintf(buf, sizeof buf, "Can't open %s", plugin);
146 splash(HZ*2, 0, true, buf);
147 dlclose(pd);
148 return -1;
149 }
150
151 plugin_start = dlsym(pd, "plugin_start");
152 if (!plugin_start) {
153 plugin_start = dlsym(pd, "_plugin_start");
154 if (!plugin_start) {
155 splash(HZ*2, 0, true, "Can't find entry point");
156 dlclose(pd);
157 return -1;
158 }
159 }
160#else
161 fd = open(plugin, O_RDONLY);
162 if (fd < 0) {
163 snprintf(buf, sizeof buf, str(LANG_PLUGIN_CANT_OPEN), plugin);
164 splash(HZ*2, 0, true, buf);
165 return fd;
166 }
167
168 plugin_start = (void*)&pluginbuf;
169 rc = read(fd, plugin_start, 0x8000);
170 close(fd);
171 if (rc < 0) {
172 /* read error */
173 snprintf(buf, sizeof buf, str(LANG_READ_FAILED), plugin);
174 splash(HZ*2, 0, true, buf);
175 return -1;
176 }
177 if (rc == 0) {
178 /* loaded a 0-byte plugin, implying it's not for this model */
179 splash(HZ*2, 0, true, str(LANG_PLUGIN_WRONG_MODEL));
180 return -1;
181 }
182#endif
183
184 rc = plugin_start(&rockbox_api, parameter);
185 switch (rc) {
186 case PLUGIN_OK:
187 break;
188
189 case PLUGIN_USB_CONNECTED:
190 return PLUGIN_USB_CONNECTED;
191
192 case PLUGIN_WRONG_API_VERSION:
193 splash(HZ*2, 0, true, str(LANG_PLUGIN_WRONG_VERSION));
194 break;
195
196 case PLUGIN_WRONG_MODEL:
197 splash(HZ*2, 0, true, str(LANG_PLUGIN_WRONG_MODEL));
198 break;
199
200 default:
201 splash(HZ*2, 0, true, str(LANG_PLUGIN_ERROR));
202 break;
203 }
204
205#ifdef SIMULATOR
206 dlclose(pd);
207#endif
208
209 return PLUGIN_OK;
210}
211
212int plugin_test(int api_version, int model)
213{
214 if (api_version != PLUGIN_API_VERSION)
215 return PLUGIN_WRONG_API_VERSION;
216
217 if (model != MODEL)
218 return PLUGIN_WRONG_MODEL;
219
220 return PLUGIN_OK;
221}