Add support for the next/prev side buttons to mpegplayer on the Gigabeat S to control...
[kugel-rb.git] / apps / filetypes.c
blob3c8f4b9a39324dcc4b65a70f5adeae6a56fc599f
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 128
46 #else
47 #define MAX_FILETYPES 64
48 #endif
50 /* a table for the know file types */
51 static const struct filetype inbuilt_filetypes[] = {
52 { "mp3", FILE_ATTR_AUDIO, Icon_Audio, VOICE_EXT_MPA },
53 { "mp2", FILE_ATTR_AUDIO, Icon_Audio, VOICE_EXT_MPA },
54 { "mpa", FILE_ATTR_AUDIO, Icon_Audio, VOICE_EXT_MPA },
55 #if CONFIG_CODEC == SWCODEC
56 /* Temporary hack to allow playlist creation */
57 { "mp1", FILE_ATTR_AUDIO, Icon_Audio, VOICE_EXT_MPA },
58 { "ogg", FILE_ATTR_AUDIO, Icon_Audio, VOICE_EXT_MPA },
59 { "oga", 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 { "mod", FILE_ATTR_AUDIO, Icon_Audio, VOICE_EXT_MPA },
73 { "shn", FILE_ATTR_AUDIO, Icon_Audio, VOICE_EXT_MPA },
74 { "aif", FILE_ATTR_AUDIO, Icon_Audio, VOICE_EXT_MPA },
75 { "aiff",FILE_ATTR_AUDIO, Icon_Audio, VOICE_EXT_MPA },
76 { "spx" ,FILE_ATTR_AUDIO, Icon_Audio, VOICE_EXT_MPA },
77 { "sid", FILE_ATTR_AUDIO, Icon_Audio, VOICE_EXT_MPA },
78 { "adx", FILE_ATTR_AUDIO, Icon_Audio, VOICE_EXT_MPA },
79 { "nsf", FILE_ATTR_AUDIO, Icon_Audio, VOICE_EXT_MPA },
80 { "nsfe",FILE_ATTR_AUDIO, Icon_Audio, VOICE_EXT_MPA },
81 { "spc", FILE_ATTR_AUDIO, Icon_Audio, VOICE_EXT_MPA },
82 { "ape", FILE_ATTR_AUDIO, Icon_Audio, VOICE_EXT_MPA },
83 { "mac", FILE_ATTR_AUDIO, Icon_Audio, VOICE_EXT_MPA },
84 { "sap" ,FILE_ATTR_AUDIO, Icon_Audio, VOICE_EXT_MPA },
85 { "rm", FILE_ATTR_AUDIO, Icon_Audio, VOICE_EXT_MPA },
86 { "ra", FILE_ATTR_AUDIO, Icon_Audio, VOICE_EXT_MPA },
87 { "rmvb",FILE_ATTR_AUDIO, Icon_Audio, VOICE_EXT_MPA },
88 { "cmc", FILE_ATTR_AUDIO, Icon_Audio, VOICE_EXT_MPA },
89 { "cm3", FILE_ATTR_AUDIO, Icon_Audio, VOICE_EXT_MPA },
90 { "cmr", FILE_ATTR_AUDIO, Icon_Audio, VOICE_EXT_MPA },
91 { "cms", FILE_ATTR_AUDIO, Icon_Audio, VOICE_EXT_MPA },
92 { "dmc", FILE_ATTR_AUDIO, Icon_Audio, VOICE_EXT_MPA },
93 { "dlt", FILE_ATTR_AUDIO, Icon_Audio, VOICE_EXT_MPA },
94 { "mpt", FILE_ATTR_AUDIO, Icon_Audio, VOICE_EXT_MPA },
95 { "mpd", FILE_ATTR_AUDIO, Icon_Audio, VOICE_EXT_MPA },
96 { "rmt", FILE_ATTR_AUDIO, Icon_Audio, VOICE_EXT_MPA },
97 { "tmc", FILE_ATTR_AUDIO, Icon_Audio, VOICE_EXT_MPA },
98 { "tm8", FILE_ATTR_AUDIO, Icon_Audio, VOICE_EXT_MPA },
99 { "tm2", FILE_ATTR_AUDIO, Icon_Audio, VOICE_EXT_MPA },
100 { "oma", FILE_ATTR_AUDIO, Icon_Audio, VOICE_EXT_MPA },
101 { "aa3", FILE_ATTR_AUDIO, Icon_Audio, VOICE_EXT_MPA },
102 { "at3", FILE_ATTR_AUDIO, Icon_Audio, VOICE_EXT_MPA },
103 { "mmf", FILE_ATTR_AUDIO, Icon_Audio, VOICE_EXT_MPA },
104 { "au", FILE_ATTR_AUDIO, Icon_Audio, VOICE_EXT_MPA },
105 { "snd", FILE_ATTR_AUDIO, Icon_Audio, VOICE_EXT_MPA },
106 { "vox", FILE_ATTR_AUDIO, Icon_Audio, VOICE_EXT_MPA },
107 { "w64", FILE_ATTR_AUDIO, Icon_Audio, VOICE_EXT_MPA },
108 #endif
109 { "m3u", FILE_ATTR_M3U, Icon_Playlist, LANG_PLAYLIST },
110 { "m3u8",FILE_ATTR_M3U, Icon_Playlist, LANG_PLAYLIST },
111 { "cfg", FILE_ATTR_CFG, Icon_Config, VOICE_EXT_CFG },
112 { "wps", FILE_ATTR_WPS, Icon_Wps, VOICE_EXT_WPS },
113 #ifdef HAVE_REMOTE_LCD
114 { "rwps",FILE_ATTR_RWPS, Icon_Wps, VOICE_EXT_RWPS },
115 #endif
116 #if CONFIG_TUNER
117 { "fmr", FILE_ATTR_FMR, Icon_Preset, LANG_FMR },
118 #endif
119 { "lng", FILE_ATTR_LNG, Icon_Language, LANG_LANGUAGE },
120 { "rock",FILE_ATTR_ROCK,Icon_Plugin, VOICE_EXT_ROCK },
121 { "lua", FILE_ATTR_LUA, Icon_Plugin, VOICE_EXT_ROCK },
122 #ifdef HAVE_LCD_BITMAP
123 { "fnt", FILE_ATTR_FONT,Icon_Font, VOICE_EXT_FONT },
124 { "kbd", FILE_ATTR_KBD, Icon_Keyboard, VOICE_EXT_KBD },
125 #endif
126 { "bmark",FILE_ATTR_BMARK, Icon_Bookmark, VOICE_EXT_BMARK },
127 { "cue", FILE_ATTR_CUE, Icon_Bookmark, VOICE_EXT_CUESHEET },
128 #ifdef HAVE_LCD_BITMAP
129 { "sbs", FILE_ATTR_SBS, Icon_Wps, VOICE_EXT_SBS },
130 #endif
131 #ifdef HAVE_REMOTE_LCD
132 { "rsbs", FILE_ATTR_RSBS, Icon_Wps, VOICE_EXT_RSBS },
133 #endif
134 #ifdef BOOTFILE_EXT
135 { BOOTFILE_EXT, FILE_ATTR_MOD, Icon_Firmware, VOICE_EXT_AJZ },
136 #endif
137 #ifdef BOOTFILE_EXT2
138 { BOOTFILE_EXT2, FILE_ATTR_MOD, Icon_Firmware, VOICE_EXT_AJZ },
139 #endif
142 void tree_get_filetypes(const struct filetype** types, int* count)
144 *types = inbuilt_filetypes;
145 *count = sizeof(inbuilt_filetypes) / sizeof(*inbuilt_filetypes);
148 /* mask for dynamic filetype info in attribute */
149 #define ROCK_EXTENSION "rock"
151 struct file_type {
152 int icon; /* the icon which shall be used for it, NOICON if unknown */
153 unsigned char attr; /* FILE_ATTR_MASK >> 8 */
154 char* plugin; /* Which plugin to use, NULL if unknown, or builtin */
155 char* extension; /* NULL for none */
157 static struct file_type filetypes[MAX_FILETYPES];
158 static int custom_filetype_icons[MAX_FILETYPES];
159 static bool custom_icons_loaded = false;
160 #ifdef HAVE_LCD_COLOR
161 static int custom_colors[MAX_FILETYPES+1];
162 #endif
163 static int filetype_count = 0;
164 static unsigned char highest_attr = 0;
166 static char *filetypes_strdup(char* string)
168 char *buffer = (char*)buffer_alloc(strlen(string)+1);
169 strcpy(buffer, string);
170 return buffer;
172 static void read_builtin_types(void);
173 static void read_config(char* config_file);
174 #ifdef HAVE_LCD_COLOR
175 /* Colors file format is similar to icons:
176 * ext:hex_color
177 * load a colors file from a theme with:
178 * filetype colours: filename.colours */
179 void read_color_theme_file(void) {
180 char buffer[MAX_PATH];
181 int fd;
182 char *ext, *color;
183 int i;
184 for (i = 0; i < MAX_FILETYPES+1; i++) {
185 custom_colors[i] = -1;
187 snprintf(buffer, MAX_PATH, "%s/%s.colours", THEME_DIR,
188 global_settings.colors_file);
189 fd = open(buffer, O_RDONLY);
190 if (fd < 0)
191 return;
192 while (read_line(fd, buffer, MAX_PATH) > 0)
194 if (!settings_parseline(buffer, &ext, &color))
195 continue;
196 if (!strcasecmp(ext, "folder"))
198 hex_to_rgb(color, &custom_colors[0]);
199 continue;
201 if (!strcasecmp(ext, "???"))
203 hex_to_rgb(color, &custom_colors[MAX_FILETYPES]);
204 continue;
206 for (i=1; i<filetype_count; i++)
208 if (filetypes[i].extension &&
209 !strcasecmp(ext, filetypes[i].extension))
211 hex_to_rgb(color, &custom_colors[i]);
212 break;
216 close(fd);
218 #endif
219 #ifdef HAVE_LCD_BITMAP
220 void read_viewer_theme_file(void)
222 char buffer[MAX_PATH];
223 int fd;
224 char *ext, *icon;
225 int i;
226 global_status.viewer_icon_count = 0;
227 custom_icons_loaded = false;
228 custom_filetype_icons[0] = Icon_Folder;
229 for (i=1; i<filetype_count; i++)
231 custom_filetype_icons[i] = filetypes[i].icon;
234 snprintf(buffer, MAX_PATH, "%s/%s.icons", ICON_DIR,
235 global_settings.viewers_icon_file);
236 fd = open(buffer, O_RDONLY);
237 if (fd < 0)
238 return;
239 while (read_line(fd, buffer, MAX_PATH) > 0)
241 if (!settings_parseline(buffer, &ext, &icon))
242 continue;
243 for (i=0; i<filetype_count; i++)
245 if (filetypes[i].extension &&
246 !strcasecmp(ext, filetypes[i].extension))
248 if (*icon == '*')
249 custom_filetype_icons[i] = atoi(icon+1);
250 else if (*icon == '-')
251 custom_filetype_icons[i] = Icon_NOICON;
252 else if (*icon >= '0' && *icon <= '9')
254 int number = atoi(icon);
255 if (number > global_status.viewer_icon_count)
256 global_status.viewer_icon_count++;
257 custom_filetype_icons[i] = Icon_Last_Themeable + number;
259 break;
263 close(fd);
264 custom_icons_loaded = true;
266 #endif
268 void filetype_init(void)
270 /* set the directory item first */
271 filetypes[0].extension = NULL;
272 filetypes[0].plugin = NULL;
273 filetypes[0].attr = 0;
274 filetypes[0].icon = Icon_Folder;
276 filetype_count = 1;
277 read_builtin_types();
278 read_config(VIEWERS_CONFIG);
279 #ifdef HAVE_LCD_BITMAP
280 read_viewer_theme_file();
281 #endif
282 #ifdef HAVE_LCD_COLOR
283 read_color_theme_file();
284 #endif
287 /* remove all white spaces from string */
288 static void rm_whitespaces(char* str)
290 char *s = str;
291 while (*str)
293 if (!isspace(*str))
295 *s = *str;
296 s++;
298 str++;
300 *s = '\0';
303 static void read_builtin_types(void)
305 int count = sizeof(inbuilt_filetypes)/sizeof(*inbuilt_filetypes), i;
306 for(i=0; i<count && (filetype_count < MAX_FILETYPES); i++)
308 filetypes[filetype_count].extension = inbuilt_filetypes[i].extension;
309 filetypes[filetype_count].plugin = NULL;
310 filetypes[filetype_count].attr = inbuilt_filetypes[i].tree_attr>>8;
311 if (filetypes[filetype_count].attr > highest_attr)
312 highest_attr = filetypes[filetype_count].attr;
313 filetypes[filetype_count].icon = inbuilt_filetypes[i].icon;
314 filetype_count++;
318 static void read_config(char* config_file)
320 char line[64], *s, *e;
321 char extension[8], plugin[32];
322 int fd = open(config_file, O_RDONLY);
323 if (fd < 0)
324 return;
325 /* config file is in the for
326 <extension>,<plugin>,<icon code>
327 ignore line if either of the first two are missing */
328 while (read_line(fd, line, 64) > 0)
330 if (filetype_count >= MAX_FILETYPES)
332 splash(HZ, ID2P(LANG_FILETYPES_FULL));
333 break;
335 rm_whitespaces(line);
336 /* get the extention */
337 s = line;
338 e = strchr(s, ',');
339 if (!e)
340 continue;
341 *e = '\0';
342 strcpy(extension, s);
344 /* get the plugin */
345 s = e+1;
346 e = strchr(s, ',');
347 if (!e)
348 continue;
349 *e = '\0';
351 strcpy(plugin, s);
352 /* ok, store this plugin/extension, check icon after */
353 filetypes[filetype_count].extension = filetypes_strdup(extension);
354 filetypes[filetype_count].plugin = filetypes_strdup(plugin);
355 filetypes[filetype_count].attr = highest_attr +1;
356 filetypes[filetype_count].icon = Icon_Questionmark;
357 highest_attr++;
358 /* get the icon */
359 s = e+1;
360 if (*s == '*')
361 filetypes[filetype_count].icon = atoi(s+1);
362 else if (*s == '-')
363 filetypes[filetype_count].icon = Icon_NOICON;
364 else if (*s >= '0' && *s <= '9')
365 filetypes[filetype_count].icon = Icon_Last_Themeable + atoi(s);
366 filetype_count++;
370 int filetype_get_attr(const char* file)
372 char *extension = strrchr(file, '.');
373 int i;
374 if (!extension)
375 return 0;
376 extension++;
377 for (i=0; i<filetype_count; i++)
379 if (filetypes[i].extension &&
380 !strcasecmp(extension, filetypes[i].extension))
381 return (filetypes[i].attr<<8)&FILE_ATTR_MASK;
383 return 0;
386 static int find_attr(int attr)
388 int i;
389 /* skip the directory item */
390 if ((attr & ATTR_DIRECTORY)==ATTR_DIRECTORY)
391 return 0;
392 for (i=1; i<filetype_count; i++)
394 if ((attr>>8) == filetypes[i].attr)
395 return i;
397 return -1;
400 #ifdef HAVE_LCD_COLOR
401 int filetype_get_color(const char * name, int attr)
403 char *extension;
404 int i;
405 if ((attr & ATTR_DIRECTORY)==ATTR_DIRECTORY)
406 return custom_colors[0];
407 extension = strrchr(name, '.');
408 if (!extension)
409 return custom_colors[MAX_FILETYPES];
410 extension++;
412 for (i=1; i<filetype_count; i++)
414 if (filetypes[i].extension &&
415 !strcasecmp(extension, filetypes[i].extension))
416 return custom_colors[i];
418 return custom_colors[MAX_FILETYPES];
420 #endif
422 int filetype_get_icon(int attr)
424 int index = find_attr(attr);
425 if (index < 0)
426 return Icon_NOICON;
427 if (custom_icons_loaded)
428 return custom_filetype_icons[index];
429 return filetypes[index].icon;
432 char* filetype_get_plugin(const struct entry* file)
434 static char plugin_name[MAX_PATH];
435 int index = find_attr(file->attr);
436 if (index < 0)
437 return NULL;
438 if (filetypes[index].plugin == NULL)
439 return NULL;
440 snprintf(plugin_name, MAX_PATH, "%s/%s.%s",
441 PLUGIN_DIR, filetypes[index].plugin, ROCK_EXTENSION);
442 return plugin_name;
445 bool filetype_supported(int attr)
447 return find_attr(attr) >= 0;
450 /**** Open With Screen ****/
451 struct cb_data {
452 int *items;
453 const char *current_file;
456 static enum themable_icons openwith_get_icon(int selected_item, void * data)
458 struct cb_data *info = (struct cb_data *)data;
459 int *items = info->items;
460 return filetypes[items[selected_item]].icon;
463 static const char* openwith_get_name(int selected_item, void * data,
464 char * buffer, size_t buffer_len)
466 (void)buffer; (void)buffer_len;
467 struct cb_data *info = (struct cb_data *)data;
468 int *items = info->items;
469 const char *s = strrchr(filetypes[items[selected_item]].plugin, '/');
470 if (s)
471 return s+1;
472 else return filetypes[items[selected_item]].plugin;
475 static int openwith_action_callback(int action, struct gui_synclist *lists)
477 struct cb_data *info = (struct cb_data *)lists->data;
478 int *items = info->items;
479 int i;
480 if (action == ACTION_STD_OK)
482 char plugin[MAX_PATH];
483 i = items[gui_synclist_get_sel_pos(lists)];
484 snprintf(plugin, MAX_PATH, "%s/%s.%s",
485 PLUGIN_DIR, filetypes[i].plugin, ROCK_EXTENSION);
486 plugin_load(plugin, info->current_file);
487 return ACTION_STD_CANCEL;
489 return action;
492 int filetype_list_viewers(const char* current_file)
494 int i, count = 0;
495 int items[MAX_FILETYPES];
496 struct simplelist_info info;
497 struct cb_data data = { items, current_file };
498 for (i=0; i<filetype_count && count < MAX_FILETYPES; i++)
500 if (filetypes[i].plugin)
502 int j;
503 for (j=0;j<count;j++) /* check if the plugin is in the list yet */
505 if (!strcmp(filetypes[i].plugin,filetypes[items[j]].plugin))
506 break;
508 if (j<count)
509 continue; /* it is so grab the next plugin */
510 items[count++] = i;
513 #ifndef HAVE_LCD_BITMAP
514 if (count == 0)
516 /* FIX: translation! */
517 splash(HZ*2, "No viewers found");
518 return PLUGIN_OK;
520 #endif
521 simplelist_info_init(&info, str(LANG_ONPLAY_OPEN_WITH), count, &data);
522 info.action_callback = openwith_action_callback;
523 info.get_name = openwith_get_name;
524 info.get_icon = global_settings.show_icons?openwith_get_icon:NULL;
525 return simplelist_show_list(&info);
528 int filetype_load_plugin(const char* plugin, char* file)
530 int i;
531 char plugin_name[MAX_PATH];
532 char *s;
534 for (i=0;i<filetype_count;i++)
536 if (filetypes[i].plugin)
538 s = strrchr(filetypes[i].plugin, '/');
539 if (s)
541 if (!strcmp(s+1, plugin))
542 break;
544 else if (!strcmp(filetypes[i].plugin, plugin))
545 break;
548 if (i >= filetype_count)
549 return PLUGIN_ERROR;
550 snprintf(plugin_name, MAX_PATH, "%s/%s.%s",
551 PLUGIN_DIR, filetypes[i].plugin, ROCK_EXTENSION);
552 return plugin_load(plugin_name, file);