More robust implementation of MPEG frame header search. The parser will not only...
[kugel-rb.git] / apps / filetypes.c
blob481c0df6de7c1e0ffed5003d483d6a77c376393a
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
8 * $Id$
10 * Copyright (C) 2007 Jonathan Gordon
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.
17 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
18 * KIND, either express or implied.
20 ****************************************************************************/
22 #include <stdio.h>
23 #include <string.h>
24 #include <stdlib.h>
25 #include <stdbool.h>
26 #include "string.h"
27 #include <ctype.h>
29 #include "settings.h"
30 #include "debug.h"
31 #include "lang.h"
32 #include "kernel.h"
33 #include "plugin.h"
34 #include "filetypes.h"
35 #include "screens.h"
36 #include "dir.h"
37 #include "file.h"
38 #include "splash.h"
39 #include "buffer.h"
40 #include "icons.h"
41 #include "logf.h"
43 /* max filetypes (plugins & icons stored here) */
44 #if CONFIG_CODEC == SWCODEC
45 #define MAX_FILETYPES 192
46 #else
47 #define MAX_FILETYPES 128
48 #endif
49 /* max viewer plugins */
50 #ifdef HAVE_LCD_BITMAP
51 #define MAX_VIEWERS 56
52 #else
53 #define MAX_VIEWERS 24
54 #endif
56 /* a table for the known file types */
57 static const struct filetype inbuilt_filetypes[] = {
58 { "mp3", FILE_ATTR_AUDIO, Icon_Audio, VOICE_EXT_MPA },
59 { "mp2", FILE_ATTR_AUDIO, Icon_Audio, VOICE_EXT_MPA },
60 { "mpa", FILE_ATTR_AUDIO, Icon_Audio, VOICE_EXT_MPA },
61 #if CONFIG_CODEC == SWCODEC
62 /* Temporary hack to allow playlist creation */
63 { "mp1", FILE_ATTR_AUDIO, Icon_Audio, VOICE_EXT_MPA },
64 { "ogg", FILE_ATTR_AUDIO, Icon_Audio, VOICE_EXT_MPA },
65 { "oga", FILE_ATTR_AUDIO, Icon_Audio, VOICE_EXT_MPA },
66 { "wma", FILE_ATTR_AUDIO, Icon_Audio, VOICE_EXT_MPA },
67 { "wmv", FILE_ATTR_AUDIO, Icon_Audio, VOICE_EXT_MPA },
68 { "asf", FILE_ATTR_AUDIO, Icon_Audio, VOICE_EXT_MPA },
69 { "wav", FILE_ATTR_AUDIO, Icon_Audio, VOICE_EXT_MPA },
70 { "flac",FILE_ATTR_AUDIO, Icon_Audio, VOICE_EXT_MPA },
71 { "ac3", FILE_ATTR_AUDIO, Icon_Audio, VOICE_EXT_MPA },
72 { "a52", FILE_ATTR_AUDIO, Icon_Audio, VOICE_EXT_MPA },
73 { "mpc", FILE_ATTR_AUDIO, Icon_Audio, VOICE_EXT_MPA },
74 { "wv", FILE_ATTR_AUDIO, Icon_Audio, VOICE_EXT_MPA },
75 { "m4a", FILE_ATTR_AUDIO, Icon_Audio, VOICE_EXT_MPA },
76 { "m4b", FILE_ATTR_AUDIO, Icon_Audio, VOICE_EXT_MPA },
77 { "mp4", FILE_ATTR_AUDIO, Icon_Audio, VOICE_EXT_MPA },
78 { "mod", FILE_ATTR_AUDIO, Icon_Audio, VOICE_EXT_MPA },
79 { "shn", FILE_ATTR_AUDIO, Icon_Audio, VOICE_EXT_MPA },
80 { "aif", FILE_ATTR_AUDIO, Icon_Audio, VOICE_EXT_MPA },
81 { "aiff",FILE_ATTR_AUDIO, Icon_Audio, VOICE_EXT_MPA },
82 { "spx" ,FILE_ATTR_AUDIO, Icon_Audio, VOICE_EXT_MPA },
83 { "sid", FILE_ATTR_AUDIO, Icon_Audio, VOICE_EXT_MPA },
84 { "adx", FILE_ATTR_AUDIO, Icon_Audio, VOICE_EXT_MPA },
85 { "nsf", FILE_ATTR_AUDIO, Icon_Audio, VOICE_EXT_MPA },
86 { "nsfe",FILE_ATTR_AUDIO, Icon_Audio, VOICE_EXT_MPA },
87 { "spc", FILE_ATTR_AUDIO, Icon_Audio, VOICE_EXT_MPA },
88 { "ape", FILE_ATTR_AUDIO, Icon_Audio, VOICE_EXT_MPA },
89 { "mac", FILE_ATTR_AUDIO, Icon_Audio, VOICE_EXT_MPA },
90 { "sap" ,FILE_ATTR_AUDIO, Icon_Audio, VOICE_EXT_MPA },
91 { "rm", FILE_ATTR_AUDIO, Icon_Audio, VOICE_EXT_MPA },
92 { "ra", FILE_ATTR_AUDIO, Icon_Audio, VOICE_EXT_MPA },
93 { "rmvb",FILE_ATTR_AUDIO, Icon_Audio, VOICE_EXT_MPA },
94 { "cmc", FILE_ATTR_AUDIO, Icon_Audio, VOICE_EXT_MPA },
95 { "cm3", FILE_ATTR_AUDIO, Icon_Audio, VOICE_EXT_MPA },
96 { "cmr", FILE_ATTR_AUDIO, Icon_Audio, VOICE_EXT_MPA },
97 { "cms", FILE_ATTR_AUDIO, Icon_Audio, VOICE_EXT_MPA },
98 { "dmc", FILE_ATTR_AUDIO, Icon_Audio, VOICE_EXT_MPA },
99 { "dlt", FILE_ATTR_AUDIO, Icon_Audio, VOICE_EXT_MPA },
100 { "mpt", FILE_ATTR_AUDIO, Icon_Audio, VOICE_EXT_MPA },
101 { "mpd", FILE_ATTR_AUDIO, Icon_Audio, VOICE_EXT_MPA },
102 { "rmt", FILE_ATTR_AUDIO, Icon_Audio, VOICE_EXT_MPA },
103 { "tmc", FILE_ATTR_AUDIO, Icon_Audio, VOICE_EXT_MPA },
104 { "tm8", FILE_ATTR_AUDIO, Icon_Audio, VOICE_EXT_MPA },
105 { "tm2", FILE_ATTR_AUDIO, Icon_Audio, VOICE_EXT_MPA },
106 { "oma", FILE_ATTR_AUDIO, Icon_Audio, VOICE_EXT_MPA },
107 { "aa3", FILE_ATTR_AUDIO, Icon_Audio, VOICE_EXT_MPA },
108 { "at3", FILE_ATTR_AUDIO, Icon_Audio, VOICE_EXT_MPA },
109 { "mmf", FILE_ATTR_AUDIO, Icon_Audio, VOICE_EXT_MPA },
110 { "au", FILE_ATTR_AUDIO, Icon_Audio, VOICE_EXT_MPA },
111 { "snd", FILE_ATTR_AUDIO, Icon_Audio, VOICE_EXT_MPA },
112 { "vox", FILE_ATTR_AUDIO, Icon_Audio, VOICE_EXT_MPA },
113 { "w64", FILE_ATTR_AUDIO, Icon_Audio, VOICE_EXT_MPA },
114 { "tta", FILE_ATTR_AUDIO, Icon_Audio, VOICE_EXT_MPA },
115 #endif
116 { "m3u", FILE_ATTR_M3U, Icon_Playlist, LANG_PLAYLIST },
117 { "m3u8",FILE_ATTR_M3U, Icon_Playlist, LANG_PLAYLIST },
118 { "cfg", FILE_ATTR_CFG, Icon_Config, VOICE_EXT_CFG },
119 { "wps", FILE_ATTR_WPS, Icon_Wps, VOICE_EXT_WPS },
120 #ifdef HAVE_REMOTE_LCD
121 { "rwps",FILE_ATTR_RWPS, Icon_Wps, VOICE_EXT_RWPS },
122 #endif
123 #if CONFIG_TUNER
124 { "fmr", FILE_ATTR_FMR, Icon_Preset, LANG_FMR },
125 { "fms", FILE_ATTR_FMS, Icon_Wps, VOICE_EXT_FMS },
126 #endif
127 { "lng", FILE_ATTR_LNG, Icon_Language, LANG_LANGUAGE },
128 { "rock",FILE_ATTR_ROCK,Icon_Plugin, VOICE_EXT_ROCK },
129 { "lua", FILE_ATTR_LUA, Icon_Plugin, VOICE_EXT_ROCK },
130 #ifdef HAVE_LCD_BITMAP
131 { "fnt", FILE_ATTR_FONT,Icon_Font, VOICE_EXT_FONT },
132 { "kbd", FILE_ATTR_KBD, Icon_Keyboard, VOICE_EXT_KBD },
133 #endif
134 { "bmark",FILE_ATTR_BMARK, Icon_Bookmark, VOICE_EXT_BMARK },
135 { "cue", FILE_ATTR_CUE, Icon_Bookmark, VOICE_EXT_CUESHEET },
136 #ifdef HAVE_LCD_BITMAP
137 { "sbs", FILE_ATTR_SBS, Icon_Wps, VOICE_EXT_SBS },
138 #endif
139 #ifdef HAVE_REMOTE_LCD
140 { "rsbs", FILE_ATTR_RSBS, Icon_Wps, VOICE_EXT_RSBS },
141 #if CONFIG_TUNER
142 { "rfms", FILE_ATTR_RFMS, Icon_Wps, VOICE_EXT_RFMS },
143 #endif
144 #endif
145 #ifdef BOOTFILE_EXT
146 { BOOTFILE_EXT, FILE_ATTR_MOD, Icon_Firmware, VOICE_EXT_AJZ },
147 #endif
148 #ifdef BOOTFILE_EXT2
149 { BOOTFILE_EXT2, FILE_ATTR_MOD, Icon_Firmware, VOICE_EXT_AJZ },
150 #endif
153 void tree_get_filetypes(const struct filetype** types, int* count)
155 *types = inbuilt_filetypes;
156 *count = sizeof(inbuilt_filetypes) / sizeof(*inbuilt_filetypes);
159 #define ROCK_EXTENSION "rock"
161 struct file_type {
162 int icon; /* the icon which shall be used for it, NOICON if unknown */
163 unsigned char attr; /* FILE_ATTR_MASK >> 8 */
164 char* plugin; /* Which plugin to use, NULL if unknown, or builtin */
165 char* extension; /* NULL for none */
167 static struct file_type filetypes[MAX_FILETYPES];
168 static int custom_filetype_icons[MAX_FILETYPES];
169 static bool custom_icons_loaded = false;
170 #ifdef HAVE_LCD_COLOR
171 static int custom_colors[MAX_FILETYPES+1];
172 #endif
173 /* index array to filetypes used in open with list. */
174 static int viewers[MAX_VIEWERS];
175 static int filetype_count = 0;
176 static unsigned char highest_attr = 0;
177 static int viewer_count = 0;
179 static char *filetypes_strdup(char* string)
181 char *buffer = (char*)buffer_alloc(strlen(string)+1);
182 strcpy(buffer, string);
183 return buffer;
185 static char *filetypes_store_plugin(char *plugin, int n)
187 int i;
188 /* if the plugin is in the list already, use it. */
189 for (i=0; i<viewer_count; i++)
191 if (!strcmp(filetypes[viewers[i]].plugin, plugin))
192 return filetypes[viewers[i]].plugin;
194 /* otherwise, allocate buffer */
195 if (viewer_count < MAX_VIEWERS)
196 viewers[viewer_count++] = n;
197 return filetypes_strdup(plugin);
200 static int find_extension(const char* extension)
202 int i;
203 if (!extension)
204 return -1;
205 for (i=1; i<filetype_count; i++)
207 if (filetypes[i].extension &&
208 !strcasecmp(extension, filetypes[i].extension))
209 return i;
211 return -1;
214 static void read_builtin_types(void);
215 static void read_config(const char* config_file);
216 #ifdef HAVE_LCD_COLOR
217 /* Colors file format is similar to icons:
218 * ext:hex_color
219 * load a colors file from a theme with:
220 * filetype colours: filename.colours */
221 void read_color_theme_file(void) {
222 char buffer[MAX_PATH];
223 int fd;
224 char *ext, *color;
225 int i;
226 for (i = 0; i < MAX_FILETYPES+1; i++) {
227 custom_colors[i] = -1;
229 snprintf(buffer, MAX_PATH, THEME_DIR "/%s.colours",
230 global_settings.colors_file);
231 fd = open(buffer, O_RDONLY);
232 if (fd < 0)
233 return;
234 while (read_line(fd, buffer, MAX_PATH) > 0)
236 if (!settings_parseline(buffer, &ext, &color))
237 continue;
238 if (!strcasecmp(ext, "folder"))
240 hex_to_rgb(color, &custom_colors[0]);
241 continue;
243 if (!strcasecmp(ext, "???"))
245 hex_to_rgb(color, &custom_colors[MAX_FILETYPES]);
246 continue;
248 i = find_extension(ext);
249 if (i >= 0)
250 hex_to_rgb(color, &custom_colors[i]);
252 close(fd);
254 #endif
255 #ifdef HAVE_LCD_BITMAP
256 void read_viewer_theme_file(void)
258 char buffer[MAX_PATH];
259 int fd;
260 char *ext, *icon;
261 int i;
262 global_status.viewer_icon_count = 0;
263 custom_icons_loaded = false;
264 custom_filetype_icons[0] = Icon_Folder;
265 for (i=1; i<filetype_count; i++)
267 custom_filetype_icons[i] = filetypes[i].icon;
270 snprintf(buffer, MAX_PATH, "%s/%s.icons", ICON_DIR,
271 global_settings.viewers_icon_file);
272 fd = open(buffer, O_RDONLY);
273 if (fd < 0)
274 return;
275 while (read_line(fd, buffer, MAX_PATH) > 0)
277 if (!settings_parseline(buffer, &ext, &icon))
278 continue;
279 i = find_extension(ext);
280 if (i >= 0)
282 if (*icon == '*')
283 custom_filetype_icons[i] = atoi(icon+1);
284 else if (*icon == '-')
285 custom_filetype_icons[i] = Icon_NOICON;
286 else if (*icon >= '0' && *icon <= '9')
288 int number = atoi(icon);
289 if (number > global_status.viewer_icon_count)
290 global_status.viewer_icon_count++;
291 custom_filetype_icons[i] = Icon_Last_Themeable + number;
295 close(fd);
296 custom_icons_loaded = true;
298 #endif
300 void filetype_init(void)
302 /* set the directory item first */
303 filetypes[0].extension = NULL;
304 filetypes[0].plugin = NULL;
305 filetypes[0].attr = 0;
306 filetypes[0].icon = Icon_Folder;
308 viewer_count = 0;
309 filetype_count = 1;
310 read_builtin_types();
311 read_config(VIEWERS_CONFIG);
312 #ifdef HAVE_LCD_BITMAP
313 read_viewer_theme_file();
314 #endif
315 #ifdef HAVE_LCD_COLOR
316 read_color_theme_file();
317 #endif
320 /* remove all white spaces from string */
321 static void rm_whitespaces(char* str)
323 char *s = str;
324 while (*str)
326 if (!isspace(*str))
328 *s = *str;
329 s++;
331 str++;
333 *s = '\0';
336 static void read_builtin_types(void)
338 int count = sizeof(inbuilt_filetypes)/sizeof(*inbuilt_filetypes), i;
339 for(i=0; i<count && (filetype_count < MAX_FILETYPES); i++)
341 filetypes[filetype_count].extension = inbuilt_filetypes[i].extension;
342 filetypes[filetype_count].plugin = NULL;
343 filetypes[filetype_count].attr = inbuilt_filetypes[i].tree_attr>>8;
344 if (filetypes[filetype_count].attr > highest_attr)
345 highest_attr = filetypes[filetype_count].attr;
346 filetypes[filetype_count].icon = inbuilt_filetypes[i].icon;
347 filetype_count++;
351 static void read_config(const char* config_file)
353 char line[64], *s, *e;
354 char *extension, *plugin;
355 int fd = open(config_file, O_RDONLY);
356 if (fd < 0)
357 return;
358 /* config file is in the format
359 <extension>,<plugin>,<icon code>
360 ignore line if either of the first two are missing */
361 while (read_line(fd, line, sizeof line) > 0)
363 if (filetype_count >= MAX_FILETYPES)
365 splash(HZ, ID2P(LANG_FILETYPES_FULL));
366 break;
368 rm_whitespaces(line);
369 /* get the extension */
370 s = line;
371 e = strchr(s, ',');
372 if (!e)
373 continue;
374 *e = '\0';
375 extension = s;
377 /* get the plugin */
378 s = e+1;
379 e = strchr(s, ',');
380 if (!e)
381 continue;
382 *e = '\0';
383 plugin = s;
385 /* ok, store this plugin/extension, check icon after */
386 struct file_type *file_type = &filetypes[filetype_count];
387 file_type->extension = filetypes_strdup(extension);
388 file_type->plugin = filetypes_store_plugin(plugin, filetype_count);
389 file_type->attr = highest_attr +1;
390 file_type->icon = Icon_Questionmark;
391 highest_attr++;
392 /* get the icon */
393 s = e+1;
394 if (*s == '*')
395 file_type->icon = atoi(s+1);
396 else if (*s == '-')
397 file_type->icon = Icon_NOICON;
398 else if (*s >= '0' && *s <= '9')
399 file_type->icon = Icon_Last_Themeable + atoi(s);
400 filetype_count++;
402 close(fd);
405 int filetype_get_attr(const char* file)
407 char *extension = strrchr(file, '.');
408 int i;
409 if (!extension)
410 return 0;
411 extension++;
413 i = find_extension(extension);
414 if (i >= 0)
415 return (filetypes[i].attr<<8)&FILE_ATTR_MASK;
416 return 0;
419 static int find_attr(int attr)
421 int i;
422 /* skip the directory item */
423 if ((attr & ATTR_DIRECTORY)==ATTR_DIRECTORY)
424 return 0;
425 for (i=1; i<filetype_count; i++)
427 if ((attr>>8) == filetypes[i].attr)
428 return i;
430 return -1;
433 #ifdef HAVE_LCD_COLOR
434 int filetype_get_color(const char * name, int attr)
436 char *extension;
437 int i;
438 if ((attr & ATTR_DIRECTORY)==ATTR_DIRECTORY)
439 return custom_colors[0];
440 extension = strrchr(name, '.');
441 if (!extension)
442 return custom_colors[MAX_FILETYPES];
443 extension++;
445 i = find_extension(extension);
446 if (i >= 0)
447 return custom_colors[i];
448 return custom_colors[MAX_FILETYPES];
450 #endif
452 int filetype_get_icon(int attr)
454 int index = find_attr(attr);
455 if (index < 0)
456 return Icon_NOICON;
457 if (custom_icons_loaded)
458 return custom_filetype_icons[index];
459 return filetypes[index].icon;
462 char* filetype_get_plugin(const struct entry* file)
464 static char plugin_name[MAX_PATH];
465 int index = find_attr(file->attr);
466 if (index < 0)
467 return NULL;
468 if (filetypes[index].plugin == NULL)
469 return NULL;
470 snprintf(plugin_name, MAX_PATH, "%s/%s.%s",
471 PLUGIN_DIR, filetypes[index].plugin, ROCK_EXTENSION);
472 return plugin_name;
475 bool filetype_supported(int attr)
477 return find_attr(attr) >= 0;
480 /**** Open With Screen ****/
481 struct cb_data {
482 const char *current_file;
485 static enum themable_icons openwith_get_icon(int selected_item, void * data)
487 (void)data;
488 return filetypes[viewers[selected_item]].icon;
491 static const char* openwith_get_name(int selected_item, void * data,
492 char * buffer, size_t buffer_len)
494 (void)data; (void)buffer; (void)buffer_len;
495 const char *s = strrchr(filetypes[viewers[selected_item]].plugin, '/');
496 if (s)
497 return s+1;
498 else return filetypes[viewers[selected_item]].plugin;
501 static int openwith_action_callback(int action, struct gui_synclist *lists)
503 struct cb_data *info = (struct cb_data *)lists->data;
504 int i;
505 if (action == ACTION_STD_OK)
507 char plugin[MAX_PATH];
508 i = viewers[gui_synclist_get_sel_pos(lists)];
509 snprintf(plugin, MAX_PATH, "%s/%s.%s",
510 PLUGIN_DIR, filetypes[i].plugin, ROCK_EXTENSION);
511 plugin_load(plugin, info->current_file);
512 return ACTION_STD_CANCEL;
514 return action;
517 int filetype_list_viewers(const char* current_file)
519 struct simplelist_info info;
520 struct cb_data data = { current_file };
521 #ifndef HAVE_LCD_BITMAP
522 if (viewer_count == 0)
524 /* FIX: translation! */
525 splash(HZ*2, "No viewers found");
526 return PLUGIN_OK;
528 #endif
529 simplelist_info_init(&info, str(LANG_ONPLAY_OPEN_WITH), viewer_count, &data);
530 info.action_callback = openwith_action_callback;
531 info.get_name = openwith_get_name;
532 info.get_icon = global_settings.show_icons?openwith_get_icon:NULL;
533 return simplelist_show_list(&info);
536 int filetype_load_plugin(const char* plugin, char* file)
538 int i;
539 char plugin_name[MAX_PATH];
540 char *s;
542 for (i=0;i<filetype_count;i++)
544 if (filetypes[i].plugin)
546 s = strrchr(filetypes[i].plugin, '/');
547 if (s)
549 if (!strcmp(s+1, plugin))
550 break;
552 else if (!strcmp(filetypes[i].plugin, plugin))
553 break;
556 if (i >= filetype_count)
557 return PLUGIN_ERROR;
558 snprintf(plugin_name, MAX_PATH, "%s/%s.%s",
559 PLUGIN_DIR, filetypes[i].plugin, ROCK_EXTENSION);
560 return plugin_load(plugin_name, file);