Fixes a problem where the sim would try to start the WPS on HAVE_RTC_ALARM sims ...
[Rockbox.git] / apps / filetypes.c
blob5e72fca5a1eeb6635791024e3da0e4580ed52e29
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 "dir.h"
38 #include "file.h"
39 #include "splash.h"
40 #include "buffer.h"
41 #include "icons.h"
42 #include "logf.h"
44 /* max filetypes (plugins & icons stored here) */
45 #if CONFIG_CODEC == SWCODEC
46 #define MAX_FILETYPES 128
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 { "wmv", FILE_ATTR_AUDIO, Icon_Audio, VOICE_EXT_MPA },
62 { "asf", FILE_ATTR_AUDIO, Icon_Audio, VOICE_EXT_MPA },
63 { "wav", FILE_ATTR_AUDIO, Icon_Audio, VOICE_EXT_MPA },
64 { "flac",FILE_ATTR_AUDIO, Icon_Audio, VOICE_EXT_MPA },
65 { "ac3", FILE_ATTR_AUDIO, Icon_Audio, VOICE_EXT_MPA },
66 { "a52", FILE_ATTR_AUDIO, Icon_Audio, VOICE_EXT_MPA },
67 { "mpc", FILE_ATTR_AUDIO, Icon_Audio, VOICE_EXT_MPA },
68 { "wv", FILE_ATTR_AUDIO, Icon_Audio, VOICE_EXT_MPA },
69 { "m4a", FILE_ATTR_AUDIO, Icon_Audio, VOICE_EXT_MPA },
70 { "m4b", FILE_ATTR_AUDIO, Icon_Audio, VOICE_EXT_MPA },
71 { "mp4", FILE_ATTR_AUDIO, Icon_Audio, VOICE_EXT_MPA },
72 { "shn", FILE_ATTR_AUDIO, Icon_Audio, VOICE_EXT_MPA },
73 { "aif", FILE_ATTR_AUDIO, Icon_Audio, VOICE_EXT_MPA },
74 { "aiff",FILE_ATTR_AUDIO, Icon_Audio, VOICE_EXT_MPA },
75 { "spx" ,FILE_ATTR_AUDIO, Icon_Audio, VOICE_EXT_MPA },
76 { "sid", FILE_ATTR_AUDIO, Icon_Audio, VOICE_EXT_MPA },
77 { "adx", FILE_ATTR_AUDIO, Icon_Audio, VOICE_EXT_MPA },
78 { "nsf", FILE_ATTR_AUDIO, Icon_Audio, VOICE_EXT_MPA },
79 { "nsfe",FILE_ATTR_AUDIO, Icon_Audio, VOICE_EXT_MPA },
80 { "spc", FILE_ATTR_AUDIO, Icon_Audio, VOICE_EXT_MPA },
81 { "ape", FILE_ATTR_AUDIO, Icon_Audio, VOICE_EXT_MPA },
82 { "mac", FILE_ATTR_AUDIO, Icon_Audio, VOICE_EXT_MPA },
83 #endif
84 { "m3u", FILE_ATTR_M3U, Icon_Playlist, LANG_PLAYLIST },
85 { "m3u8",FILE_ATTR_M3U, Icon_Playlist, LANG_PLAYLIST },
86 { "cfg", FILE_ATTR_CFG, Icon_Config, VOICE_EXT_CFG },
87 { "wps", FILE_ATTR_WPS, Icon_Wps, VOICE_EXT_WPS },
88 #ifdef HAVE_REMOTE_LCD
89 { "rwps",FILE_ATTR_RWPS, Icon_Wps, VOICE_EXT_RWPS },
90 #endif
91 #if LCD_DEPTH > 1
92 { "bmp", FILE_ATTR_BMP, Icon_Wps, VOICE_EXT_WPS },
93 #endif
94 #if CONFIG_TUNER
95 { "fmr", FILE_ATTR_FMR, Icon_Preset, LANG_FMR },
96 #endif
97 { "lng", FILE_ATTR_LNG, Icon_Language, LANG_LANGUAGE },
98 { "rock",FILE_ATTR_ROCK,Icon_Plugin, VOICE_EXT_ROCK },
99 #ifdef HAVE_LCD_BITMAP
100 { "fnt", FILE_ATTR_FONT,Icon_Font, VOICE_EXT_FONT },
101 { "kbd", FILE_ATTR_KBD, Icon_Keyboard, VOICE_EXT_KBD },
102 #endif
103 { "bmark",FILE_ATTR_BMARK, Icon_Bookmark, VOICE_EXT_BMARK },
104 { "cue", FILE_ATTR_CUE, Icon_Bookmark, VOICE_EXT_CUESHEET },
105 #ifdef BOOTFILE_EXT
106 { BOOTFILE_EXT, FILE_ATTR_MOD, Icon_Firmware, VOICE_EXT_AJZ },
107 #endif /* #ifndef SIMULATOR */
110 void tree_get_filetypes(const struct filetype** types, int* count)
112 *types = inbuilt_filetypes;
113 *count = sizeof(inbuilt_filetypes) / sizeof(*inbuilt_filetypes);
116 /* mask for dynamic filetype info in attribute */
117 #define FILETYPES_MASK 0xFF00
118 #define ROCK_EXTENSION "rock"
120 struct file_type {
121 int icon; /* the icon which shall be used for it, NOICON if unknown */
122 unsigned char attr; /* FILETYPES_MASK >> 8 */
123 char* plugin; /* Which plugin to use, NULL if unknown, or builtin */
124 char* extension; /* NULL for none */
126 static struct file_type filetypes[MAX_FILETYPES];
127 static int custom_filetype_icons[MAX_FILETYPES];
128 static bool custom_icons_loaded = false;
129 #ifdef HAVE_LCD_COLOR
130 static int custom_colors[MAX_FILETYPES+1];
131 #endif
132 static int filetype_count = 0;
133 static unsigned char heighest_attr = 0;
135 static char *filetypes_strdup(char* string)
137 char *buffer = (char*)buffer_alloc(strlen(string)+1);
138 strcpy(buffer, string);
139 return buffer;
141 static void read_builtin_types(void);
142 static void read_config(char* config_file);
143 #ifdef HAVE_LCD_COLOR
144 /* Colors file format is similar to icons:
145 * ext:hex_color
146 * load a colors file from a theme with:
147 * filetype colours: filename.colours */
148 void read_color_theme_file(void) {
149 char buffer[MAX_PATH];
150 int fd;
151 char *ext, *color;
152 int i;
153 for (i = 0; i < MAX_FILETYPES+1; i++) {
154 custom_colors[i] = -1;
156 snprintf(buffer, MAX_PATH, "%s/%s.colours", THEME_DIR,
157 global_settings.colors_file);
158 fd = open(buffer, O_RDONLY);
159 if (fd < 0)
160 return;
161 while (read_line(fd, buffer, MAX_PATH) > 0)
163 if (!settings_parseline(buffer, &ext, &color))
164 continue;
165 if (!strcasecmp(ext, "folder"))
167 custom_colors[0] = hex_to_rgb(color);
168 continue;
170 if (!strcasecmp(ext, "???"))
172 custom_colors[MAX_FILETYPES] = hex_to_rgb(color);
173 continue;
175 for (i=1; i<filetype_count; i++)
177 if (filetypes[i].extension &&
178 !strcasecmp(ext, filetypes[i].extension))
180 custom_colors[i] = hex_to_rgb(color);
181 break;
185 close(fd);
187 #endif
188 #ifdef HAVE_LCD_BITMAP
189 void read_viewer_theme_file(void)
191 char buffer[MAX_PATH];
192 int fd;
193 char *ext, *icon;
194 int i;
195 global_status.viewer_icon_count = 0;
196 custom_icons_loaded = false;
197 custom_filetype_icons[0] = Icon_Folder;
198 for (i=1; i<filetype_count; i++)
200 custom_filetype_icons[i] = filetypes[i].icon;
203 snprintf(buffer, MAX_PATH, "%s/%s.icons", ICON_DIR,
204 global_settings.viewers_icon_file);
205 fd = open(buffer, O_RDONLY);
206 if (fd < 0)
207 return;
208 while (read_line(fd, buffer, MAX_PATH) > 0)
210 if (!settings_parseline(buffer, &ext, &icon))
211 continue;
212 for (i=0; i<filetype_count; i++)
214 if (filetypes[i].extension &&
215 !strcasecmp(ext, filetypes[i].extension))
217 if (*icon == '*')
218 custom_filetype_icons[i] = atoi(icon+1);
219 else if (*icon == '-')
220 custom_filetype_icons[i] = Icon_NOICON;
221 else if (*icon >= '0' && *icon <= '9')
223 int number = atoi(icon);
224 if (number > global_status.viewer_icon_count)
225 global_status.viewer_icon_count++;
226 custom_filetype_icons[i] = Icon_Last_Themeable + number;
228 break;
232 close(fd);
233 custom_icons_loaded = true;
235 #endif
237 void filetype_init(void)
239 /* set the directory item first */
240 filetypes[0].extension = NULL;
241 filetypes[0].plugin = NULL;
242 filetypes[0].attr = 0;
243 filetypes[0].icon = Icon_Folder;
245 filetype_count = 1;
246 read_builtin_types();
247 read_config(VIEWERS_CONFIG);
248 #ifdef HAVE_LCD_BITMAP
249 read_viewer_theme_file();
250 #endif
251 #ifdef HAVE_LCD_COLOR
252 read_color_theme_file();
253 #endif
256 /* remove all white spaces from string */
257 static void rm_whitespaces(char* str)
259 char *s = str;
260 while (*str)
262 if (!isspace(*str))
264 *s = *str;
265 s++;
267 str++;
269 *s = '\0';
272 static void read_builtin_types(void)
274 int count = sizeof(inbuilt_filetypes)/sizeof(*inbuilt_filetypes), i;
275 for(i=0; i<count && (filetype_count < MAX_FILETYPES); i++)
277 filetypes[filetype_count].extension = inbuilt_filetypes[i].extension;
278 filetypes[filetype_count].plugin = NULL;
279 filetypes[filetype_count].attr = inbuilt_filetypes[i].tree_attr>>8;
280 if (filetypes[filetype_count].attr > heighest_attr)
281 heighest_attr = filetypes[filetype_count].attr;
282 filetypes[filetype_count].icon = inbuilt_filetypes[i].icon;
283 filetype_count++;
287 static void read_config(char* config_file)
289 char line[64], *s, *e;
290 char extension[8], plugin[32];
291 int fd = open(config_file, O_RDONLY);
292 if (fd < 0)
293 return;
294 /* config file is in the for
295 <extension>,<plugin>,<icon code>
296 ignore line if either of the first two are missing */
297 while (read_line(fd, line, 64) > 0)
299 if (filetype_count >= MAX_FILETYPES)
301 gui_syncsplash(HZ, ID2P(LANG_FILETYPES_FULL));
302 break;
304 rm_whitespaces(line);
305 /* get the extention */
306 s = line;
307 e = strchr(s, ',');
308 if (!e)
309 continue;
310 *e = '\0';
311 strcpy(extension, s);
313 /* get the plugin */
314 s = e+1;
315 e = strchr(s, ',');
316 if (!e)
317 continue;
318 *e = '\0';
320 strcpy(plugin, s);
321 /* ok, store this plugin/extension, check icon after */
322 filetypes[filetype_count].extension = filetypes_strdup(extension);
323 filetypes[filetype_count].plugin = filetypes_strdup(plugin);
324 filetypes[filetype_count].attr = heighest_attr +1;
325 filetypes[filetype_count].icon = Icon_Questionmark;
326 heighest_attr++;
327 /* get the icon */
328 #ifdef HAVE_LCD_BITMAP
329 s = e+1;
330 if (*s == '*')
331 filetypes[filetype_count].icon = atoi(s+1);
332 else if (*s == '-')
333 filetypes[filetype_count].icon = Icon_NOICON;
334 else if (*s >= '0' && *s <= '9')
335 filetypes[filetype_count].icon = Icon_Last_Themeable + atoi(s);
336 #else
337 filetypes[filetype_count].icon = Icon_NOICON;
338 #endif
339 filetype_count++;
343 int filetype_get_attr(const char* file)
345 char *extension = strrchr(file, '.');
346 int i;
347 if (!extension)
348 return 0;
349 extension++;
350 for (i=0; i<filetype_count; i++)
352 if (filetypes[i].extension &&
353 !strcasecmp(extension, filetypes[i].extension))
354 return (filetypes[i].attr<<8)&FILE_ATTR_MASK;
356 return 0;
359 static int find_attr(int attr)
361 int i;
362 /* skip the directory item */
363 if ((attr & ATTR_DIRECTORY)==ATTR_DIRECTORY)
364 return 0;
365 for (i=1; i<filetype_count; i++)
367 if ((attr>>8) == filetypes[i].attr)
368 return i;
370 return -1;
373 #ifdef HAVE_LCD_COLOR
374 int filetype_get_color(const char * name, int attr)
376 char *extension;
377 int i;
378 if ((attr & ATTR_DIRECTORY)==ATTR_DIRECTORY)
379 return custom_colors[0];
380 extension = strrchr(name, '.');
381 if (!extension)
382 return custom_colors[MAX_FILETYPES];
383 extension++;
385 for (i=1; i<filetype_count; i++)
387 if (filetypes[i].extension &&
388 !strcasecmp(extension, filetypes[i].extension))
389 return custom_colors[i];
391 return custom_colors[MAX_FILETYPES];
393 #endif
395 int filetype_get_icon(int attr)
397 int index = find_attr(attr);
398 if (index < 0)
399 return Icon_NOICON;
400 if (custom_icons_loaded)
401 return custom_filetype_icons[index];
402 return filetypes[index].icon;
405 char* filetype_get_plugin(const struct entry* file)
407 static char plugin_name[MAX_PATH];
408 int index = find_attr(file->attr);
409 if (index < 0)
410 return NULL;
411 if (filetypes[index].plugin == NULL)
412 return NULL;
413 snprintf(plugin_name, MAX_PATH, "%s/%s.%s",
414 PLUGIN_DIR, filetypes[index].plugin, ROCK_EXTENSION);
415 return plugin_name;
418 bool filetype_supported(int attr)
420 return find_attr(attr) >= 0;
423 /**** Open With Screen ****/
424 enum themable_icons openwith_get_icon(int selected_item, void * data)
426 int *items = (int*)data;
427 return filetypes[items[selected_item]].icon;
429 char * openwith_get_name(int selected_item, void * data, char * buffer)
431 (void)buffer;
432 int *items = (int*)data;
433 char *s = strrchr(filetypes[items[selected_item]].plugin, '/');
434 if (s)
435 return s+1;
436 else return filetypes[items[selected_item]].plugin;
439 int filetype_list_viewers(const char* current_file)
441 int i, count = 0, action;
442 int items[MAX_FILETYPES];
443 struct gui_synclist lists;
444 for (i=0; i<filetype_count && count < MAX_FILETYPES; i++)
446 if (filetypes[i].plugin)
448 int j;
449 for (j=0;j<count;j++) /* check if the plugin is in the list yet */
451 if (filetypes[i].plugin &&
452 !strcmp(filetypes[i].plugin,filetypes[items[j]].plugin))
453 break;
455 if (j<count)
456 continue; /* it is so grab the next plugin */
457 items[count++] = i;
460 #ifndef HAVE_LCD_BITMAP
461 if (count == 0)
463 /* FIX: translation! */
464 gui_syncsplash(HZ*2, (unsigned char *)"No viewers found");
465 return PLUGIN_OK;
467 #endif
468 gui_synclist_init(&lists,openwith_get_name,(void*)items, false, 1);
469 gui_synclist_set_nb_items(&lists, count);
470 gui_synclist_set_icon_callback(&lists, openwith_get_icon);
471 gui_synclist_set_title(&lists, str(LANG_ONPLAY_OPEN_WITH), Icon_Plugin);
472 gui_synclist_select_item(&lists, 0);
473 gui_synclist_draw(&lists);
474 while (1)
476 gui_syncstatusbar_draw(&statusbars, true);
477 action = get_action(CONTEXT_MAINMENU,HZ);
478 if ((action == ACTION_NONE) ||
479 gui_synclist_do_button(&lists, &action, LIST_WRAP_UNLESS_HELD))
480 continue;
481 else if (action == ACTION_STD_OK)
483 char plugin[MAX_PATH];
484 i = items[gui_synclist_get_sel_pos(&lists)];
485 snprintf(plugin, MAX_PATH, "%s/%s.%s",
486 PLUGIN_DIR, filetypes[i].plugin, ROCK_EXTENSION);
487 return plugin_load(plugin, (char*)current_file);
489 else if (action == ACTION_STD_CANCEL)
490 return action;
494 int filetype_load_plugin(const char* plugin, char* file)
496 int i;
497 char plugin_name[MAX_PATH];
498 char *s;
500 for (i=0;i<filetype_count;i++)
502 if (filetypes[i].plugin)
504 s = strrchr(filetypes[i].plugin, '/');
505 if (s)
507 if (!strcmp(s+1, plugin))
508 break;
510 else if (!strcmp(filetypes[i].plugin, plugin))
511 break;
514 if (i >= filetype_count)
515 return PLUGIN_ERROR;
516 snprintf(plugin_name, MAX_PATH, "%s/%s.%s",
517 PLUGIN_DIR, filetypes[i].plugin, ROCK_EXTENSION);
518 return plugin_load(plugin_name,file);