Fix red, stupid typo
[kugel-rb.git] / apps / filetypes.c
blob28a2da65bbecb0b25b13be23bd2f627c12ad1daa
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
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 { "tta", FILE_ATTR_AUDIO, Icon_Audio, VOICE_EXT_MPA },
109 #endif
110 { "m3u", FILE_ATTR_M3U, Icon_Playlist, LANG_PLAYLIST },
111 { "m3u8",FILE_ATTR_M3U, Icon_Playlist, LANG_PLAYLIST },
112 { "cfg", FILE_ATTR_CFG, Icon_Config, VOICE_EXT_CFG },
113 { "wps", FILE_ATTR_WPS, Icon_Wps, VOICE_EXT_WPS },
114 #ifdef HAVE_REMOTE_LCD
115 { "rwps",FILE_ATTR_RWPS, Icon_Wps, VOICE_EXT_RWPS },
116 #endif
117 #if CONFIG_TUNER
118 { "fmr", FILE_ATTR_FMR, Icon_Preset, LANG_FMR },
119 { "fms", FILE_ATTR_FMS, Icon_Wps, VOICE_EXT_FMS },
120 #endif
121 { "lng", FILE_ATTR_LNG, Icon_Language, LANG_LANGUAGE },
122 { "rock",FILE_ATTR_ROCK,Icon_Plugin, VOICE_EXT_ROCK },
123 { "lua", FILE_ATTR_LUA, Icon_Plugin, VOICE_EXT_ROCK },
124 #ifdef HAVE_LCD_BITMAP
125 { "fnt", FILE_ATTR_FONT,Icon_Font, VOICE_EXT_FONT },
126 { "kbd", FILE_ATTR_KBD, Icon_Keyboard, VOICE_EXT_KBD },
127 #endif
128 { "bmark",FILE_ATTR_BMARK, Icon_Bookmark, VOICE_EXT_BMARK },
129 { "cue", FILE_ATTR_CUE, Icon_Bookmark, VOICE_EXT_CUESHEET },
130 #ifdef HAVE_LCD_BITMAP
131 { "sbs", FILE_ATTR_SBS, Icon_Wps, VOICE_EXT_SBS },
132 #endif
133 #ifdef HAVE_REMOTE_LCD
134 { "rsbs", FILE_ATTR_RSBS, Icon_Wps, VOICE_EXT_RSBS },
135 #if CONFIG_TUNER
136 { "rfms", FILE_ATTR_RFMS, Icon_Wps, VOICE_EXT_RFMS },
137 #endif
138 #endif
139 #ifdef BOOTFILE_EXT
140 { BOOTFILE_EXT, FILE_ATTR_MOD, Icon_Firmware, VOICE_EXT_AJZ },
141 #endif
142 #ifdef BOOTFILE_EXT2
143 { BOOTFILE_EXT2, FILE_ATTR_MOD, Icon_Firmware, VOICE_EXT_AJZ },
144 #endif
147 void tree_get_filetypes(const struct filetype** types, int* count)
149 *types = inbuilt_filetypes;
150 *count = sizeof(inbuilt_filetypes) / sizeof(*inbuilt_filetypes);
153 /* mask for dynamic filetype info in attribute */
154 #define ROCK_EXTENSION "rock"
156 struct file_type {
157 int icon; /* the icon which shall be used for it, NOICON if unknown */
158 unsigned char attr; /* FILE_ATTR_MASK >> 8 */
159 char* plugin; /* Which plugin to use, NULL if unknown, or builtin */
160 char* extension; /* NULL for none */
162 static struct file_type filetypes[MAX_FILETYPES];
163 static int custom_filetype_icons[MAX_FILETYPES];
164 static bool custom_icons_loaded = false;
165 #ifdef HAVE_LCD_COLOR
166 static int custom_colors[MAX_FILETYPES+1];
167 #endif
168 static int filetype_count = 0;
169 static unsigned char highest_attr = 0;
171 static char *filetypes_strdup(char* string)
173 char *buffer = (char*)buffer_alloc(strlen(string)+1);
174 strcpy(buffer, string);
175 return buffer;
177 static void read_builtin_types(void);
178 static void read_config(const char* config_file);
179 #ifdef HAVE_LCD_COLOR
180 /* Colors file format is similar to icons:
181 * ext:hex_color
182 * load a colors file from a theme with:
183 * filetype colours: filename.colours */
184 void read_color_theme_file(void) {
185 char buffer[MAX_PATH], dir[MAX_PATH];
186 int fd;
187 char *ext, *color;
188 int i;
189 for (i = 0; i < MAX_FILETYPES+1; i++) {
190 custom_colors[i] = -1;
192 snprintf(buffer, MAX_PATH, "%s/%s.colours",
193 get_user_file_path(THEME_DIR, 0, dir, sizeof(dir)),
194 global_settings.colors_file);
195 fd = open(buffer, O_RDONLY);
196 if (fd < 0)
197 return;
198 while (read_line(fd, buffer, MAX_PATH) > 0)
200 if (!settings_parseline(buffer, &ext, &color))
201 continue;
202 if (!strcasecmp(ext, "folder"))
204 hex_to_rgb(color, &custom_colors[0]);
205 continue;
207 if (!strcasecmp(ext, "???"))
209 hex_to_rgb(color, &custom_colors[MAX_FILETYPES]);
210 continue;
212 for (i=1; i<filetype_count; i++)
214 if (filetypes[i].extension &&
215 !strcasecmp(ext, filetypes[i].extension))
217 hex_to_rgb(color, &custom_colors[i]);
218 break;
222 close(fd);
224 #endif
225 #ifdef HAVE_LCD_BITMAP
226 void read_viewer_theme_file(void)
228 char buffer[MAX_PATH];
229 int fd;
230 char *ext, *icon;
231 int i;
232 global_status.viewer_icon_count = 0;
233 custom_icons_loaded = false;
234 custom_filetype_icons[0] = Icon_Folder;
235 for (i=1; i<filetype_count; i++)
237 custom_filetype_icons[i] = filetypes[i].icon;
240 snprintf(buffer, MAX_PATH, "%s/%s.icons", ICON_DIR,
241 global_settings.viewers_icon_file);
242 fd = open(buffer, O_RDONLY);
243 if (fd < 0)
244 return;
245 while (read_line(fd, buffer, MAX_PATH) > 0)
247 if (!settings_parseline(buffer, &ext, &icon))
248 continue;
249 for (i=0; i<filetype_count; i++)
251 if (filetypes[i].extension &&
252 !strcasecmp(ext, filetypes[i].extension))
254 if (*icon == '*')
255 custom_filetype_icons[i] = atoi(icon+1);
256 else if (*icon == '-')
257 custom_filetype_icons[i] = Icon_NOICON;
258 else if (*icon >= '0' && *icon <= '9')
260 int number = atoi(icon);
261 if (number > global_status.viewer_icon_count)
262 global_status.viewer_icon_count++;
263 custom_filetype_icons[i] = Icon_Last_Themeable + number;
265 break;
269 close(fd);
270 custom_icons_loaded = true;
272 #endif
274 void filetype_init(void)
276 char path[MAX_PATH];
277 /* set the directory item first */
278 filetypes[0].extension = NULL;
279 filetypes[0].plugin = NULL;
280 filetypes[0].attr = 0;
281 filetypes[0].icon = Icon_Folder;
283 filetype_count = 1;
284 read_builtin_types();
285 read_config(get_user_file_path(VIEWERS_CONFIG, IS_FILE, path, sizeof(path)));
286 #ifdef HAVE_LCD_BITMAP
287 read_viewer_theme_file();
288 #endif
289 #ifdef HAVE_LCD_COLOR
290 read_color_theme_file();
291 #endif
294 /* remove all white spaces from string */
295 static void rm_whitespaces(char* str)
297 char *s = str;
298 while (*str)
300 if (!isspace(*str))
302 *s = *str;
303 s++;
305 str++;
307 *s = '\0';
310 static void read_builtin_types(void)
312 int count = sizeof(inbuilt_filetypes)/sizeof(*inbuilt_filetypes), i;
313 for(i=0; i<count && (filetype_count < MAX_FILETYPES); i++)
315 filetypes[filetype_count].extension = inbuilt_filetypes[i].extension;
316 filetypes[filetype_count].plugin = NULL;
317 filetypes[filetype_count].attr = inbuilt_filetypes[i].tree_attr>>8;
318 if (filetypes[filetype_count].attr > highest_attr)
319 highest_attr = filetypes[filetype_count].attr;
320 filetypes[filetype_count].icon = inbuilt_filetypes[i].icon;
321 filetype_count++;
325 static void read_config(const char* config_file)
327 char line[64], *s, *e;
328 char extension[8], plugin[32];
329 int fd = open(config_file, O_RDONLY);
330 if (fd < 0)
331 return;
332 /* config file is in the for
333 <extension>,<plugin>,<icon code>
334 ignore line if either of the first two are missing */
335 while (read_line(fd, line, 64) > 0)
337 if (filetype_count >= MAX_FILETYPES)
339 splash(HZ, ID2P(LANG_FILETYPES_FULL));
340 break;
342 rm_whitespaces(line);
343 /* get the extention */
344 s = line;
345 e = strchr(s, ',');
346 if (!e)
347 continue;
348 *e = '\0';
349 strcpy(extension, s);
351 /* get the plugin */
352 s = e+1;
353 e = strchr(s, ',');
354 if (!e)
355 continue;
356 *e = '\0';
358 strcpy(plugin, s);
359 /* ok, store this plugin/extension, check icon after */
360 filetypes[filetype_count].extension = filetypes_strdup(extension);
361 filetypes[filetype_count].plugin = filetypes_strdup(plugin);
362 filetypes[filetype_count].attr = highest_attr +1;
363 filetypes[filetype_count].icon = Icon_Questionmark;
364 highest_attr++;
365 /* get the icon */
366 s = e+1;
367 if (*s == '*')
368 filetypes[filetype_count].icon = atoi(s+1);
369 else if (*s == '-')
370 filetypes[filetype_count].icon = Icon_NOICON;
371 else if (*s >= '0' && *s <= '9')
372 filetypes[filetype_count].icon = Icon_Last_Themeable + atoi(s);
373 filetype_count++;
375 close(fd);
378 int filetype_get_attr(const char* file)
380 char *extension = strrchr(file, '.');
381 int i;
382 if (!extension)
383 return 0;
384 extension++;
385 for (i=0; i<filetype_count; i++)
387 if (filetypes[i].extension &&
388 !strcasecmp(extension, filetypes[i].extension))
389 return (filetypes[i].attr<<8)&FILE_ATTR_MASK;
391 return 0;
394 static int find_attr(int attr)
396 int i;
397 /* skip the directory item */
398 if ((attr & ATTR_DIRECTORY)==ATTR_DIRECTORY)
399 return 0;
400 for (i=1; i<filetype_count; i++)
402 if ((attr>>8) == filetypes[i].attr)
403 return i;
405 return -1;
408 #ifdef HAVE_LCD_COLOR
409 int filetype_get_color(const char * name, int attr)
411 char *extension;
412 int i;
413 if ((attr & ATTR_DIRECTORY)==ATTR_DIRECTORY)
414 return custom_colors[0];
415 extension = strrchr(name, '.');
416 if (!extension)
417 return custom_colors[MAX_FILETYPES];
418 extension++;
420 for (i=1; i<filetype_count; i++)
422 if (filetypes[i].extension &&
423 !strcasecmp(extension, filetypes[i].extension))
424 return custom_colors[i];
426 return custom_colors[MAX_FILETYPES];
428 #endif
430 int filetype_get_icon(int attr)
432 int index = find_attr(attr);
433 if (index < 0)
434 return Icon_NOICON;
435 if (custom_icons_loaded)
436 return custom_filetype_icons[index];
437 return filetypes[index].icon;
440 char* filetype_get_plugin(const struct entry* file)
442 static char plugin_name[MAX_PATH];
443 int index = find_attr(file->attr);
444 if (index < 0)
445 return NULL;
446 if (filetypes[index].plugin == NULL)
447 return NULL;
448 snprintf(plugin_name, MAX_PATH, "%s/%s.%s",
449 PLUGIN_DIR, filetypes[index].plugin, ROCK_EXTENSION);
450 return plugin_name;
453 bool filetype_supported(int attr)
455 return find_attr(attr) >= 0;
458 /**** Open With Screen ****/
459 struct cb_data {
460 int *items;
461 const char *current_file;
464 static enum themable_icons openwith_get_icon(int selected_item, void * data)
466 struct cb_data *info = (struct cb_data *)data;
467 int *items = info->items;
468 return filetypes[items[selected_item]].icon;
471 static const char* openwith_get_name(int selected_item, void * data,
472 char * buffer, size_t buffer_len)
474 (void)buffer; (void)buffer_len;
475 struct cb_data *info = (struct cb_data *)data;
476 int *items = info->items;
477 const char *s = strrchr(filetypes[items[selected_item]].plugin, '/');
478 if (s)
479 return s+1;
480 else return filetypes[items[selected_item]].plugin;
483 static int openwith_action_callback(int action, struct gui_synclist *lists)
485 struct cb_data *info = (struct cb_data *)lists->data;
486 int *items = info->items;
487 int i;
488 if (action == ACTION_STD_OK)
490 char plugin[MAX_PATH];
491 i = items[gui_synclist_get_sel_pos(lists)];
492 snprintf(plugin, MAX_PATH, "%s/%s.%s",
493 PLUGIN_DIR, filetypes[i].plugin, ROCK_EXTENSION);
494 plugin_load(plugin, info->current_file);
495 return ACTION_STD_CANCEL;
497 return action;
500 int filetype_list_viewers(const char* current_file)
502 int i, count = 0;
503 int items[MAX_FILETYPES];
504 struct simplelist_info info;
505 struct cb_data data = { items, current_file };
506 for (i=0; i<filetype_count && count < MAX_FILETYPES; i++)
508 if (filetypes[i].plugin)
510 int j;
511 for (j=0;j<count;j++) /* check if the plugin is in the list yet */
513 if (!strcmp(filetypes[i].plugin,filetypes[items[j]].plugin))
514 break;
516 if (j<count)
517 continue; /* it is so grab the next plugin */
518 items[count++] = i;
521 #ifndef HAVE_LCD_BITMAP
522 if (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), 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);