Accept FS#7228 by Dagni McPhee enable pitchscreen on sansa
[Rockbox.git] / apps / filetypes.c
blob3e03c0bda1d84fb7edba93084cb064830c91d4f0
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
8 * $Id$
10 * Copyright (C) 2007 Jonathan Gordon
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.
15 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
16 * KIND, either express or implied.
18 ****************************************************************************/
20 #include <stdio.h>
21 #include <string.h>
22 #include <stdlib.h>
23 #include <stdbool.h>
24 #include "string.h"
25 #include "atoi.h"
26 #include <ctype.h>
28 #include "sprintf.h"
29 #include "settings.h"
30 #include "debug.h"
31 #include "lang.h"
32 #include "language.h"
33 #include "kernel.h"
34 #include "plugin.h"
35 #include "filetypes.h"
36 #include "screens.h"
37 #include "icons.h"
38 #include "dir.h"
39 #include "file.h"
40 #include "icons.h"
41 #include "splash.h"
42 #include "buffer.h"
44 /* max filetypes (plugins & icons stored here) */
45 #if CONFIG_CODEC == SWCODEC
46 #define MAX_FILETYPES 72
47 #else
48 #define MAX_FILETYPES 48
49 #endif
51 /* a table for the know file types */
52 const struct filetype inbuilt_filetypes[] = {
53 { "mp3", FILE_ATTR_AUDIO, Icon_Audio, VOICE_EXT_MPA },
54 { "mp2", FILE_ATTR_AUDIO, Icon_Audio, VOICE_EXT_MPA },
55 { "mpa", FILE_ATTR_AUDIO, Icon_Audio, VOICE_EXT_MPA },
56 #if CONFIG_CODEC == SWCODEC
57 /* Temporary hack to allow playlist creation */
58 { "mp1", FILE_ATTR_AUDIO, Icon_Audio, VOICE_EXT_MPA },
59 { "ogg", FILE_ATTR_AUDIO, Icon_Audio, VOICE_EXT_MPA },
60 { "wma", FILE_ATTR_AUDIO, Icon_Audio, VOICE_EXT_MPA },
61 { "wav", FILE_ATTR_AUDIO, Icon_Audio, VOICE_EXT_MPA },
62 { "flac",FILE_ATTR_AUDIO, Icon_Audio, VOICE_EXT_MPA },
63 { "ac3", FILE_ATTR_AUDIO, Icon_Audio, VOICE_EXT_MPA },
64 { "a52", FILE_ATTR_AUDIO, Icon_Audio, VOICE_EXT_MPA },
65 { "mpc", FILE_ATTR_AUDIO, Icon_Audio, VOICE_EXT_MPA },
66 { "wv", FILE_ATTR_AUDIO, Icon_Audio, VOICE_EXT_MPA },
67 { "m4a", FILE_ATTR_AUDIO, Icon_Audio, VOICE_EXT_MPA },
68 { "m4b", FILE_ATTR_AUDIO, Icon_Audio, VOICE_EXT_MPA },
69 { "mp4", FILE_ATTR_AUDIO, Icon_Audio, VOICE_EXT_MPA },
70 { "shn", FILE_ATTR_AUDIO, Icon_Audio, VOICE_EXT_MPA },
71 { "aif", FILE_ATTR_AUDIO, Icon_Audio, VOICE_EXT_MPA },
72 { "aiff",FILE_ATTR_AUDIO, Icon_Audio, VOICE_EXT_MPA },
73 { "spx" ,FILE_ATTR_AUDIO, Icon_Audio, VOICE_EXT_MPA },
74 { "sid", FILE_ATTR_AUDIO, Icon_Audio, VOICE_EXT_MPA },
75 { "adx", FILE_ATTR_AUDIO, Icon_Audio, VOICE_EXT_MPA },
76 { "nsf", FILE_ATTR_AUDIO, Icon_Audio, VOICE_EXT_MPA },
77 { "nsfe",FILE_ATTR_AUDIO, Icon_Audio, VOICE_EXT_MPA },
78 { "spc", FILE_ATTR_AUDIO, Icon_Audio, VOICE_EXT_MPA },
79 #endif
80 { "m3u", FILE_ATTR_M3U, Icon_Playlist, LANG_PLAYLIST },
81 { "m3u8",FILE_ATTR_M3U, Icon_Playlist, LANG_PLAYLIST },
82 { "cfg", FILE_ATTR_CFG, Icon_Config, VOICE_EXT_CFG },
83 { "wps", FILE_ATTR_WPS, Icon_Wps, VOICE_EXT_WPS },
84 #ifdef HAVE_REMOTE_LCD
85 { "rwps",FILE_ATTR_RWPS, Icon_Wps, VOICE_EXT_RWPS },
86 #endif
87 #if LCD_DEPTH > 1
88 { "bmp", FILE_ATTR_BMP, Icon_Wps, VOICE_EXT_WPS },
89 #endif
90 #if CONFIG_TUNER
91 { "fmr", FILE_ATTR_FMR, Icon_Preset, LANG_FMR },
92 #endif
93 { "lng", FILE_ATTR_LNG, Icon_Language, LANG_LANGUAGE },
94 { "rock",FILE_ATTR_ROCK,Icon_Plugin, VOICE_EXT_ROCK },
95 #ifdef HAVE_LCD_BITMAP
96 { "fnt", FILE_ATTR_FONT,Icon_Font, VOICE_EXT_FONT },
97 { "kbd", FILE_ATTR_KBD, Icon_Keyboard, VOICE_EXT_KBD },
98 #endif
99 { "bmark",FILE_ATTR_BMARK, Icon_Bookmark, VOICE_EXT_BMARK },
100 { "cue", FILE_ATTR_CUE, Icon_Bookmark, LANG_CUESHEET },
101 #ifdef BOOTFILE_EXT
102 { BOOTFILE_EXT, FILE_ATTR_MOD, Icon_Firmware, VOICE_EXT_AJZ },
103 #endif /* #ifndef SIMULATOR */
106 void tree_get_filetypes(const struct filetype** types, int* count)
108 *types = inbuilt_filetypes;
109 *count = sizeof(inbuilt_filetypes) / sizeof(*inbuilt_filetypes);
112 /* mask for dynamic filetype info in attribute */
113 #define FILETYPES_MASK 0xFF00
114 #define ROCK_EXTENSION "rock"
116 struct file_type {
117 int icon; /* the icon which shall be used for it, NOICON if unknown */
118 bool viewer; /* true if the rock is in viewers, false if in rocks */
119 unsigned char attr; /* FILETYPES_MASK >> 8 */
120 char* plugin; /* Which plugin to use, NULL if unknown, or builtin */
121 char* extension; /* NULL for none */
123 static struct file_type filetypes[MAX_FILETYPES];
124 static int custom_filetype_icons[MAX_FILETYPES];
125 static bool custom_icons_loaded = false;
126 static int filetype_count = 0;
127 static unsigned char heighest_attr = 0;
129 static char *filetypes_strdup(char* string)
131 char *buffer = (char*)buffer_alloc(strlen(string)+1);
132 strcpy(buffer, string);
133 return buffer;
135 static void read_builtin_types(void);
136 static void read_config(char* config_file);
137 #ifdef HAVE_LCD_BITMAP
138 void read_viewer_theme_file(void)
140 char buffer[MAX_PATH];
141 int fd;
142 char *ext, *icon;
143 int i;
144 global_status.viewer_icon_count = 0;
145 custom_icons_loaded = false;
146 custom_filetype_icons[0] = Icon_Folder;
147 for (i=1; i<filetype_count; i++)
149 custom_filetype_icons[i] = filetypes[i].icon;
152 snprintf(buffer, MAX_PATH, "%s/%s.icons", ICON_DIR,
153 global_settings.viewers_icon_file);
154 fd = open(buffer, O_RDONLY);
155 if (fd < 0)
156 return;
157 while (read_line(fd, buffer, MAX_PATH) > 0)
159 if (!settings_parseline(buffer, &ext, &icon))
160 continue;
161 for (i=0; i<filetype_count; i++)
163 if (filetypes[i].extension && !strcasecmp(ext, filetypes[i].extension))
165 if (*icon == '*')
166 custom_filetype_icons[i] = atoi(icon+1);
167 else if (*icon == '-')
168 custom_filetype_icons[i] = Icon_NOICON;
169 else if (*icon >= '0' && *icon <= '9')
171 int number = atoi(icon);
172 if (number > global_status.viewer_icon_count)
173 global_status.viewer_icon_count++;
174 custom_filetype_icons[i] = Icon_Last_Themeable + number;
176 break;
180 close(fd);
181 custom_icons_loaded = true;
183 #endif
185 void filetype_init(void)
187 /* set the directory item first */
188 filetypes[0].extension = NULL;
189 filetypes[0].plugin = NULL;
190 filetypes[0].attr = 0;
191 filetypes[0].icon = Icon_Folder;
193 filetype_count = 1;
194 read_builtin_types();
195 read_config(VIEWERS_CONFIG);
196 #ifdef HAVE_LCD_BITMAP
197 read_viewer_theme_file();
198 #endif
201 /* remove all white spaces from string */
202 static void rm_whitespaces(char* str)
204 char *s = str;
205 while (*str)
207 if (!isspace(*str))
209 *s = *str;
210 s++;
212 str++;
214 *s = '\0';
217 static void read_builtin_types(void)
219 int count = sizeof(inbuilt_filetypes)/sizeof(*inbuilt_filetypes), i;
220 for(i=0; i<count && (filetype_count < MAX_FILETYPES); i++)
222 filetypes[filetype_count].extension = inbuilt_filetypes[i].extension;
223 filetypes[filetype_count].plugin = NULL;
224 filetypes[filetype_count].attr = inbuilt_filetypes[i].tree_attr>>8;
225 if (filetypes[filetype_count].attr > heighest_attr)
226 heighest_attr = filetypes[filetype_count].attr;
227 filetypes[filetype_count].icon = inbuilt_filetypes[i].icon;
228 filetype_count++;
232 static void read_config(char* config_file)
234 char line[64], *s, *e;
235 char extension[8], plugin[32];
236 bool viewer;
237 int fd = open(config_file, O_RDONLY);
238 if (fd < 0)
239 return;
240 /* config file is in the for
241 <extension>,<plugin>,<icon code>
242 ignore line if either of the first two are missing */
243 while (read_line(fd, line, 64) > 0)
245 if (filetype_count >= MAX_FILETYPES)
247 gui_syncsplash(HZ, str(LANG_FILETYPES_FULL));
248 break;
250 rm_whitespaces(line);
251 /* get the extention */
252 s = line;
253 e = strchr(s, ',');
254 if (!e)
255 continue;
256 *e = '\0';
257 strcpy(extension, s);
259 /* get the plugin */
260 s = e+1;
261 e = strchr(s, '/');
262 if (!e)
263 continue;
264 *e = '\0';
265 if (!strcasecmp("viewers", s))
266 viewer = true;
267 else
268 viewer = false;
269 s = e+1;
270 e = strchr(s, ',');
271 if (!e)
272 continue;
273 *e = '\0';
274 strcpy(plugin, s);
275 /* ok, store this plugin/extension, check icon after */
276 filetypes[filetype_count].extension = filetypes_strdup(extension);
277 filetypes[filetype_count].plugin = filetypes_strdup(plugin);
278 filetypes[filetype_count].viewer = viewer;
279 filetypes[filetype_count].attr = heighest_attr +1;
280 filetypes[filetype_count].icon = Icon_Questionmark;
281 heighest_attr++;
282 /* get the icon */
283 #ifdef HAVE_LCD_BITMAP
284 s = e+1;
285 if (*s == '*')
286 filetypes[filetype_count].icon = atoi(s+1);
287 else if (*s == '-')
288 filetypes[filetype_count].icon = Icon_NOICON;
289 else if (*s >= '0' && *s <= '9')
290 filetypes[filetype_count].icon = Icon_Last_Themeable + atoi(s);
291 #else
292 filetypes[filetype_count].icon = Icon_NOICON;
293 #endif
294 filetype_count++;
298 int filetype_get_attr(const char* file)
300 char *extension = strrchr(file, '.');
301 int i;
302 if (!extension)
303 return 0;
304 extension++;
305 for (i=0; i<filetype_count; i++)
307 if (filetypes[i].extension &&
308 !strcasecmp(extension, filetypes[i].extension))
309 return (filetypes[i].attr<<8)&FILE_ATTR_MASK;
311 return 0;
314 static int find_attr(int attr)
316 int i;
317 /* skip the directory item */
318 if ((attr & ATTR_DIRECTORY)==ATTR_DIRECTORY)
319 return 0;
320 for (i=1; i<filetype_count; i++)
322 if ((attr>>8) == filetypes[i].attr)
323 return i;
325 return -1;
328 int filetype_get_icon(int attr)
330 int index = find_attr(attr);
331 if (index < 0)
332 return Icon_NOICON;
333 if (custom_icons_loaded)
334 return custom_filetype_icons[index];
335 return filetypes[index].icon;
338 char* filetype_get_plugin(const struct entry* file)
340 static char plugin_name[MAX_PATH];
341 int index = find_attr(file->attr);
342 if (index < 0)
343 return NULL;
344 if (filetypes[index].plugin == NULL)
345 return NULL;
346 snprintf(plugin_name, MAX_PATH, "%s/%s.%s",
347 filetypes[index].viewer? VIEWERS_DIR: PLUGIN_DIR,
348 filetypes[index].plugin, ROCK_EXTENSION);
349 return plugin_name;
352 bool filetype_supported(int attr)
354 return find_attr(attr) >= 0;
357 int filetype_list_viewers(const char* current_file)
359 int i, count = 0;
360 char *strings[MAX_FILETYPES/2];
361 struct menu_callback_with_desc cb_and_desc =
362 { NULL, ID2P(LANG_ONPLAY_OPEN_WITH), Icon_Plugin };
363 struct menu_item_ex menu;
365 for (i=0; i<filetype_count && count < (MAX_FILETYPES/2); i++)
367 if (filetypes[i].plugin)
369 int j;
370 for (j=0;j<count;j++) /* check if the plugin is in the list yet */
372 if (!strcmp(strings[j], filetypes[i].plugin))
373 break;
375 if (j<count)
376 continue; /* it is so grab the next plugin */
377 strings[count] = filetypes[i].plugin;
378 count++;
381 #ifndef HAVE_LCD_BITMAP
382 if (count == 0)
384 /* FIX: translation! */
385 gui_syncsplash(HZ*2, (unsigned char *)"No viewers found");
386 return PLUGIN_OK;
388 #endif
389 menu.flags = MT_RETURN_ID|MENU_HAS_DESC|MENU_ITEM_COUNT(count);
390 menu.strings = (const char**)strings;
391 menu.callback_and_desc = &cb_and_desc;
392 i = do_menu(&menu, NULL);
393 if (i >= 0)
394 return filetype_load_plugin(strings[i], (void*)current_file);
395 return i;
398 int filetype_load_plugin(const char* plugin, char* file)
400 int fd;
401 char plugin_name[MAX_PATH];
402 snprintf(plugin_name, sizeof(plugin_name), "%s/%s.%s",
403 VIEWERS_DIR, plugin, ROCK_EXTENSION);
404 if ((fd = open(plugin_name,O_RDONLY))>=0)
406 close(fd);
407 return plugin_load(plugin_name,file);
409 else
411 snprintf(plugin_name, sizeof(plugin_name), "%s/%s.%s",
412 PLUGIN_DIR, plugin, ROCK_EXTENSION);
413 if ((fd = open(plugin_name,O_RDONLY))>=0)
415 close(fd);
416 return plugin_load(plugin_name,file);
419 return PLUGIN_ERROR;