simple fix for FS#7274 - selected item might not be shown when a list is drawn in...
[Rockbox.git] / apps / filetypes.c
blobe826efc7dcc195d2d4e0c716373298866814dd70
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 { "ape", FILE_ATTR_AUDIO, Icon_Audio, VOICE_EXT_MPA },
80 { "mac", FILE_ATTR_AUDIO, Icon_Audio, VOICE_EXT_MPA },
81 #endif
82 { "m3u", FILE_ATTR_M3U, Icon_Playlist, LANG_PLAYLIST },
83 { "m3u8",FILE_ATTR_M3U, Icon_Playlist, LANG_PLAYLIST },
84 { "cfg", FILE_ATTR_CFG, Icon_Config, VOICE_EXT_CFG },
85 { "wps", FILE_ATTR_WPS, Icon_Wps, VOICE_EXT_WPS },
86 #ifdef HAVE_REMOTE_LCD
87 { "rwps",FILE_ATTR_RWPS, Icon_Wps, VOICE_EXT_RWPS },
88 #endif
89 #if LCD_DEPTH > 1
90 { "bmp", FILE_ATTR_BMP, Icon_Wps, VOICE_EXT_WPS },
91 #endif
92 #if CONFIG_TUNER
93 { "fmr", FILE_ATTR_FMR, Icon_Preset, LANG_FMR },
94 #endif
95 { "lng", FILE_ATTR_LNG, Icon_Language, LANG_LANGUAGE },
96 { "rock",FILE_ATTR_ROCK,Icon_Plugin, VOICE_EXT_ROCK },
97 #ifdef HAVE_LCD_BITMAP
98 { "fnt", FILE_ATTR_FONT,Icon_Font, VOICE_EXT_FONT },
99 { "kbd", FILE_ATTR_KBD, Icon_Keyboard, VOICE_EXT_KBD },
100 #endif
101 { "bmark",FILE_ATTR_BMARK, Icon_Bookmark, VOICE_EXT_BMARK },
102 { "cue", FILE_ATTR_CUE, Icon_Bookmark, LANG_CUESHEET },
103 #ifdef BOOTFILE_EXT
104 { BOOTFILE_EXT, FILE_ATTR_MOD, Icon_Firmware, VOICE_EXT_AJZ },
105 #endif /* #ifndef SIMULATOR */
108 void tree_get_filetypes(const struct filetype** types, int* count)
110 *types = inbuilt_filetypes;
111 *count = sizeof(inbuilt_filetypes) / sizeof(*inbuilt_filetypes);
114 /* mask for dynamic filetype info in attribute */
115 #define FILETYPES_MASK 0xFF00
116 #define ROCK_EXTENSION "rock"
118 struct file_type {
119 int icon; /* the icon which shall be used for it, NOICON if unknown */
120 bool viewer; /* true if the rock is in viewers, false if in rocks */
121 unsigned char attr; /* FILETYPES_MASK >> 8 */
122 char* plugin; /* Which plugin to use, NULL if unknown, or builtin */
123 char* extension; /* NULL for none */
125 static struct file_type filetypes[MAX_FILETYPES];
126 static int custom_filetype_icons[MAX_FILETYPES];
127 static bool custom_icons_loaded = false;
128 static int filetype_count = 0;
129 static unsigned char heighest_attr = 0;
131 static char *filetypes_strdup(char* string)
133 char *buffer = (char*)buffer_alloc(strlen(string)+1);
134 strcpy(buffer, string);
135 return buffer;
137 static void read_builtin_types(void);
138 static void read_config(char* config_file);
139 #ifdef HAVE_LCD_BITMAP
140 void read_viewer_theme_file(void)
142 char buffer[MAX_PATH];
143 int fd;
144 char *ext, *icon;
145 int i;
146 global_status.viewer_icon_count = 0;
147 custom_icons_loaded = false;
148 custom_filetype_icons[0] = Icon_Folder;
149 for (i=1; i<filetype_count; i++)
151 custom_filetype_icons[i] = filetypes[i].icon;
154 snprintf(buffer, MAX_PATH, "%s/%s.icons", ICON_DIR,
155 global_settings.viewers_icon_file);
156 fd = open(buffer, O_RDONLY);
157 if (fd < 0)
158 return;
159 while (read_line(fd, buffer, MAX_PATH) > 0)
161 if (!settings_parseline(buffer, &ext, &icon))
162 continue;
163 for (i=0; i<filetype_count; i++)
165 if (filetypes[i].extension && !strcasecmp(ext, filetypes[i].extension))
167 if (*icon == '*')
168 custom_filetype_icons[i] = atoi(icon+1);
169 else if (*icon == '-')
170 custom_filetype_icons[i] = Icon_NOICON;
171 else if (*icon >= '0' && *icon <= '9')
173 int number = atoi(icon);
174 if (number > global_status.viewer_icon_count)
175 global_status.viewer_icon_count++;
176 custom_filetype_icons[i] = Icon_Last_Themeable + number;
178 break;
182 close(fd);
183 custom_icons_loaded = true;
185 #endif
187 void filetype_init(void)
189 /* set the directory item first */
190 filetypes[0].extension = NULL;
191 filetypes[0].plugin = NULL;
192 filetypes[0].attr = 0;
193 filetypes[0].icon = Icon_Folder;
195 filetype_count = 1;
196 read_builtin_types();
197 read_config(VIEWERS_CONFIG);
198 #ifdef HAVE_LCD_BITMAP
199 read_viewer_theme_file();
200 #endif
203 /* remove all white spaces from string */
204 static void rm_whitespaces(char* str)
206 char *s = str;
207 while (*str)
209 if (!isspace(*str))
211 *s = *str;
212 s++;
214 str++;
216 *s = '\0';
219 static void read_builtin_types(void)
221 int count = sizeof(inbuilt_filetypes)/sizeof(*inbuilt_filetypes), i;
222 for(i=0; i<count && (filetype_count < MAX_FILETYPES); i++)
224 filetypes[filetype_count].extension = inbuilt_filetypes[i].extension;
225 filetypes[filetype_count].plugin = NULL;
226 filetypes[filetype_count].attr = inbuilt_filetypes[i].tree_attr>>8;
227 if (filetypes[filetype_count].attr > heighest_attr)
228 heighest_attr = filetypes[filetype_count].attr;
229 filetypes[filetype_count].icon = inbuilt_filetypes[i].icon;
230 filetype_count++;
234 static void read_config(char* config_file)
236 char line[64], *s, *e;
237 char extension[8], plugin[32];
238 bool viewer;
239 int fd = open(config_file, O_RDONLY);
240 if (fd < 0)
241 return;
242 /* config file is in the for
243 <extension>,<plugin>,<icon code>
244 ignore line if either of the first two are missing */
245 while (read_line(fd, line, 64) > 0)
247 if (filetype_count >= MAX_FILETYPES)
249 gui_syncsplash(HZ, str(LANG_FILETYPES_FULL));
250 break;
252 rm_whitespaces(line);
253 /* get the extention */
254 s = line;
255 e = strchr(s, ',');
256 if (!e)
257 continue;
258 *e = '\0';
259 strcpy(extension, s);
261 /* get the plugin */
262 s = e+1;
263 e = strchr(s, '/');
264 if (!e)
265 continue;
266 *e = '\0';
267 if (!strcasecmp("viewers", s))
268 viewer = true;
269 else
270 viewer = false;
271 s = e+1;
272 e = strchr(s, ',');
273 if (!e)
274 continue;
275 *e = '\0';
276 strcpy(plugin, s);
277 /* ok, store this plugin/extension, check icon after */
278 filetypes[filetype_count].extension = filetypes_strdup(extension);
279 filetypes[filetype_count].plugin = filetypes_strdup(plugin);
280 filetypes[filetype_count].viewer = viewer;
281 filetypes[filetype_count].attr = heighest_attr +1;
282 filetypes[filetype_count].icon = Icon_Questionmark;
283 heighest_attr++;
284 /* get the icon */
285 #ifdef HAVE_LCD_BITMAP
286 s = e+1;
287 if (*s == '*')
288 filetypes[filetype_count].icon = atoi(s+1);
289 else if (*s == '-')
290 filetypes[filetype_count].icon = Icon_NOICON;
291 else if (*s >= '0' && *s <= '9')
292 filetypes[filetype_count].icon = Icon_Last_Themeable + atoi(s);
293 #else
294 filetypes[filetype_count].icon = Icon_NOICON;
295 #endif
296 filetype_count++;
300 int filetype_get_attr(const char* file)
302 char *extension = strrchr(file, '.');
303 int i;
304 if (!extension)
305 return 0;
306 extension++;
307 for (i=0; i<filetype_count; i++)
309 if (filetypes[i].extension &&
310 !strcasecmp(extension, filetypes[i].extension))
311 return (filetypes[i].attr<<8)&FILE_ATTR_MASK;
313 return 0;
316 static int find_attr(int attr)
318 int i;
319 /* skip the directory item */
320 if ((attr & ATTR_DIRECTORY)==ATTR_DIRECTORY)
321 return 0;
322 for (i=1; i<filetype_count; i++)
324 if ((attr>>8) == filetypes[i].attr)
325 return i;
327 return -1;
330 int filetype_get_icon(int attr)
332 int index = find_attr(attr);
333 if (index < 0)
334 return Icon_NOICON;
335 if (custom_icons_loaded)
336 return custom_filetype_icons[index];
337 return filetypes[index].icon;
340 char* filetype_get_plugin(const struct entry* file)
342 static char plugin_name[MAX_PATH];
343 int index = find_attr(file->attr);
344 if (index < 0)
345 return NULL;
346 if (filetypes[index].plugin == NULL)
347 return NULL;
348 snprintf(plugin_name, MAX_PATH, "%s/%s.%s",
349 filetypes[index].viewer? VIEWERS_DIR: PLUGIN_DIR,
350 filetypes[index].plugin, ROCK_EXTENSION);
351 return plugin_name;
354 bool filetype_supported(int attr)
356 return find_attr(attr) >= 0;
359 int filetype_list_viewers(const char* current_file)
361 int i, count = 0;
362 char *strings[MAX_FILETYPES/2];
363 struct menu_callback_with_desc cb_and_desc =
364 { NULL, ID2P(LANG_ONPLAY_OPEN_WITH), Icon_Plugin };
365 struct menu_item_ex menu;
367 for (i=0; i<filetype_count && count < (MAX_FILETYPES/2); i++)
369 if (filetypes[i].plugin)
371 int j;
372 for (j=0;j<count;j++) /* check if the plugin is in the list yet */
374 if (!strcmp(strings[j], filetypes[i].plugin))
375 break;
377 if (j<count)
378 continue; /* it is so grab the next plugin */
379 strings[count] = filetypes[i].plugin;
380 count++;
383 #ifndef HAVE_LCD_BITMAP
384 if (count == 0)
386 /* FIX: translation! */
387 gui_syncsplash(HZ*2, (unsigned char *)"No viewers found");
388 return PLUGIN_OK;
390 #endif
391 menu.flags = MT_RETURN_ID|MENU_HAS_DESC|MENU_ITEM_COUNT(count);
392 menu.strings = (const char**)strings;
393 menu.callback_and_desc = &cb_and_desc;
394 i = do_menu(&menu, NULL);
395 if (i >= 0)
396 return filetype_load_plugin(strings[i], (void*)current_file);
397 return i;
400 int filetype_load_plugin(const char* plugin, char* file)
402 int fd;
403 char plugin_name[MAX_PATH];
404 snprintf(plugin_name, sizeof(plugin_name), "%s/%s.%s",
405 VIEWERS_DIR, plugin, ROCK_EXTENSION);
406 if ((fd = open(plugin_name,O_RDONLY))>=0)
408 close(fd);
409 return plugin_load(plugin_name,file);
411 else
413 snprintf(plugin_name, sizeof(plugin_name), "%s/%s.%s",
414 PLUGIN_DIR, plugin, ROCK_EXTENSION);
415 if ((fd = open(plugin_name,O_RDONLY))>=0)
417 close(fd);
418 return plugin_load(plugin_name,file);
421 return PLUGIN_ERROR;