Deactive the playlist viewer wps shortcut on the x5/m5 because of its hardware inabil...
[kugel-rb/myfork.git] / apps / filetypes.c
blob1772cac7ee5db02f5c9f12e083ca6888e5e29bd5
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 "sprintf.h"
30 #include "settings.h"
31 #include "debug.h"
32 #include "lang.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 static 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 { "oga", FILE_ATTR_AUDIO, Icon_Audio, VOICE_EXT_MPA },
61 { "wma", FILE_ATTR_AUDIO, Icon_Audio, VOICE_EXT_MPA },
62 { "wmv", FILE_ATTR_AUDIO, Icon_Audio, VOICE_EXT_MPA },
63 { "asf", FILE_ATTR_AUDIO, Icon_Audio, VOICE_EXT_MPA },
64 { "wav", FILE_ATTR_AUDIO, Icon_Audio, VOICE_EXT_MPA },
65 { "flac",FILE_ATTR_AUDIO, Icon_Audio, VOICE_EXT_MPA },
66 { "ac3", FILE_ATTR_AUDIO, Icon_Audio, VOICE_EXT_MPA },
67 { "a52", FILE_ATTR_AUDIO, Icon_Audio, VOICE_EXT_MPA },
68 { "mpc", FILE_ATTR_AUDIO, Icon_Audio, VOICE_EXT_MPA },
69 { "wv", FILE_ATTR_AUDIO, Icon_Audio, VOICE_EXT_MPA },
70 { "m4a", FILE_ATTR_AUDIO, Icon_Audio, VOICE_EXT_MPA },
71 { "m4b", FILE_ATTR_AUDIO, Icon_Audio, VOICE_EXT_MPA },
72 { "mp4", FILE_ATTR_AUDIO, Icon_Audio, VOICE_EXT_MPA },
73 { "mod", FILE_ATTR_AUDIO, Icon_Audio, VOICE_EXT_MPA },
74 { "shn", FILE_ATTR_AUDIO, Icon_Audio, VOICE_EXT_MPA },
75 { "aif", FILE_ATTR_AUDIO, Icon_Audio, VOICE_EXT_MPA },
76 { "aiff",FILE_ATTR_AUDIO, Icon_Audio, VOICE_EXT_MPA },
77 { "spx" ,FILE_ATTR_AUDIO, Icon_Audio, VOICE_EXT_MPA },
78 { "sid", FILE_ATTR_AUDIO, Icon_Audio, VOICE_EXT_MPA },
79 { "adx", FILE_ATTR_AUDIO, Icon_Audio, VOICE_EXT_MPA },
80 { "nsf", FILE_ATTR_AUDIO, Icon_Audio, VOICE_EXT_MPA },
81 { "nsfe",FILE_ATTR_AUDIO, Icon_Audio, VOICE_EXT_MPA },
82 { "spc", FILE_ATTR_AUDIO, Icon_Audio, VOICE_EXT_MPA },
83 { "ape", FILE_ATTR_AUDIO, Icon_Audio, VOICE_EXT_MPA },
84 { "mac", FILE_ATTR_AUDIO, Icon_Audio, VOICE_EXT_MPA },
85 { "sap" ,FILE_ATTR_AUDIO, Icon_Audio, VOICE_EXT_MPA },
86 { "rm", FILE_ATTR_AUDIO, Icon_Audio, VOICE_EXT_MPA },
87 { "ra", FILE_ATTR_AUDIO, Icon_Audio, VOICE_EXT_MPA },
88 #endif
89 { "m3u", FILE_ATTR_M3U, Icon_Playlist, LANG_PLAYLIST },
90 { "m3u8",FILE_ATTR_M3U, Icon_Playlist, LANG_PLAYLIST },
91 { "cfg", FILE_ATTR_CFG, Icon_Config, VOICE_EXT_CFG },
92 { "wps", FILE_ATTR_WPS, Icon_Wps, VOICE_EXT_WPS },
93 #ifdef HAVE_REMOTE_LCD
94 { "rwps",FILE_ATTR_RWPS, Icon_Wps, VOICE_EXT_RWPS },
95 #endif
96 #if LCD_DEPTH > 1
97 { "bmp", FILE_ATTR_BMP, Icon_Wps, VOICE_EXT_WPS },
98 #endif
99 #if CONFIG_TUNER
100 { "fmr", FILE_ATTR_FMR, Icon_Preset, LANG_FMR },
101 #endif
102 { "lng", FILE_ATTR_LNG, Icon_Language, LANG_LANGUAGE },
103 { "rock",FILE_ATTR_ROCK,Icon_Plugin, VOICE_EXT_ROCK },
104 #ifdef HAVE_LCD_BITMAP
105 { "fnt", FILE_ATTR_FONT,Icon_Font, VOICE_EXT_FONT },
106 { "kbd", FILE_ATTR_KBD, Icon_Keyboard, VOICE_EXT_KBD },
107 #endif
108 { "bmark",FILE_ATTR_BMARK, Icon_Bookmark, VOICE_EXT_BMARK },
109 { "cue", FILE_ATTR_CUE, Icon_Bookmark, VOICE_EXT_CUESHEET },
110 #ifdef BOOTFILE_EXT
111 { BOOTFILE_EXT, FILE_ATTR_MOD, Icon_Firmware, VOICE_EXT_AJZ },
112 #endif
115 void tree_get_filetypes(const struct filetype** types, int* count)
117 *types = inbuilt_filetypes;
118 *count = sizeof(inbuilt_filetypes) / sizeof(*inbuilt_filetypes);
121 /* mask for dynamic filetype info in attribute */
122 #define FILETYPES_MASK 0xFF00
123 #define ROCK_EXTENSION "rock"
125 struct file_type {
126 int icon; /* the icon which shall be used for it, NOICON if unknown */
127 unsigned char attr; /* FILETYPES_MASK >> 8 */
128 char* plugin; /* Which plugin to use, NULL if unknown, or builtin */
129 char* extension; /* NULL for none */
131 static struct file_type filetypes[MAX_FILETYPES];
132 static int custom_filetype_icons[MAX_FILETYPES];
133 static bool custom_icons_loaded = false;
134 #ifdef HAVE_LCD_COLOR
135 static int custom_colors[MAX_FILETYPES+1];
136 #endif
137 static int filetype_count = 0;
138 static unsigned char highest_attr = 0;
140 static char *filetypes_strdup(char* string)
142 char *buffer = (char*)buffer_alloc(strlen(string)+1);
143 strcpy(buffer, string);
144 return buffer;
146 static void read_builtin_types(void);
147 static void read_config(char* config_file);
148 #ifdef HAVE_LCD_COLOR
149 /* Colors file format is similar to icons:
150 * ext:hex_color
151 * load a colors file from a theme with:
152 * filetype colours: filename.colours */
153 void read_color_theme_file(void) {
154 char buffer[MAX_PATH];
155 int fd;
156 char *ext, *color;
157 int i;
158 for (i = 0; i < MAX_FILETYPES+1; i++) {
159 custom_colors[i] = -1;
161 snprintf(buffer, MAX_PATH, "%s/%s.colours", THEME_DIR,
162 global_settings.colors_file);
163 fd = open(buffer, O_RDONLY);
164 if (fd < 0)
165 return;
166 while (read_line(fd, buffer, MAX_PATH) > 0)
168 if (!settings_parseline(buffer, &ext, &color))
169 continue;
170 if (!strcasecmp(ext, "folder"))
172 hex_to_rgb(color, &custom_colors[0]);
173 continue;
175 if (!strcasecmp(ext, "???"))
177 hex_to_rgb(color, &custom_colors[MAX_FILETYPES]);
178 continue;
180 for (i=1; i<filetype_count; i++)
182 if (filetypes[i].extension &&
183 !strcasecmp(ext, filetypes[i].extension))
185 hex_to_rgb(color, &custom_colors[i]);
186 break;
190 close(fd);
192 #endif
193 #ifdef HAVE_LCD_BITMAP
194 void read_viewer_theme_file(void)
196 char buffer[MAX_PATH];
197 int fd;
198 char *ext, *icon;
199 int i;
200 global_status.viewer_icon_count = 0;
201 custom_icons_loaded = false;
202 custom_filetype_icons[0] = Icon_Folder;
203 for (i=1; i<filetype_count; i++)
205 custom_filetype_icons[i] = filetypes[i].icon;
208 snprintf(buffer, MAX_PATH, "%s/%s.icons", ICON_DIR,
209 global_settings.viewers_icon_file);
210 fd = open(buffer, O_RDONLY);
211 if (fd < 0)
212 return;
213 while (read_line(fd, buffer, MAX_PATH) > 0)
215 if (!settings_parseline(buffer, &ext, &icon))
216 continue;
217 for (i=0; i<filetype_count; i++)
219 if (filetypes[i].extension &&
220 !strcasecmp(ext, filetypes[i].extension))
222 if (*icon == '*')
223 custom_filetype_icons[i] = atoi(icon+1);
224 else if (*icon == '-')
225 custom_filetype_icons[i] = Icon_NOICON;
226 else if (*icon >= '0' && *icon <= '9')
228 int number = atoi(icon);
229 if (number > global_status.viewer_icon_count)
230 global_status.viewer_icon_count++;
231 custom_filetype_icons[i] = Icon_Last_Themeable + number;
233 break;
237 close(fd);
238 custom_icons_loaded = true;
240 #endif
242 void filetype_init(void)
244 /* set the directory item first */
245 filetypes[0].extension = NULL;
246 filetypes[0].plugin = NULL;
247 filetypes[0].attr = 0;
248 filetypes[0].icon = Icon_Folder;
250 filetype_count = 1;
251 read_builtin_types();
252 read_config(VIEWERS_CONFIG);
253 #ifdef HAVE_LCD_BITMAP
254 read_viewer_theme_file();
255 #endif
256 #ifdef HAVE_LCD_COLOR
257 read_color_theme_file();
258 #endif
261 /* remove all white spaces from string */
262 static void rm_whitespaces(char* str)
264 char *s = str;
265 while (*str)
267 if (!isspace(*str))
269 *s = *str;
270 s++;
272 str++;
274 *s = '\0';
277 static void read_builtin_types(void)
279 int count = sizeof(inbuilt_filetypes)/sizeof(*inbuilt_filetypes), i;
280 for(i=0; i<count && (filetype_count < MAX_FILETYPES); i++)
282 filetypes[filetype_count].extension = inbuilt_filetypes[i].extension;
283 filetypes[filetype_count].plugin = NULL;
284 filetypes[filetype_count].attr = inbuilt_filetypes[i].tree_attr>>8;
285 if (filetypes[filetype_count].attr > highest_attr)
286 highest_attr = filetypes[filetype_count].attr;
287 filetypes[filetype_count].icon = inbuilt_filetypes[i].icon;
288 filetype_count++;
292 static void read_config(char* config_file)
294 char line[64], *s, *e;
295 char extension[8], plugin[32];
296 int fd = open(config_file, O_RDONLY);
297 if (fd < 0)
298 return;
299 /* config file is in the for
300 <extension>,<plugin>,<icon code>
301 ignore line if either of the first two are missing */
302 while (read_line(fd, line, 64) > 0)
304 if (filetype_count >= MAX_FILETYPES)
306 splash(HZ, ID2P(LANG_FILETYPES_FULL));
307 break;
309 rm_whitespaces(line);
310 /* get the extention */
311 s = line;
312 e = strchr(s, ',');
313 if (!e)
314 continue;
315 *e = '\0';
316 strcpy(extension, s);
318 /* get the plugin */
319 s = e+1;
320 e = strchr(s, ',');
321 if (!e)
322 continue;
323 *e = '\0';
325 strcpy(plugin, s);
326 /* ok, store this plugin/extension, check icon after */
327 filetypes[filetype_count].extension = filetypes_strdup(extension);
328 filetypes[filetype_count].plugin = filetypes_strdup(plugin);
329 filetypes[filetype_count].attr = highest_attr +1;
330 filetypes[filetype_count].icon = Icon_Questionmark;
331 highest_attr++;
332 /* get the icon */
333 s = e+1;
334 if (*s == '*')
335 filetypes[filetype_count].icon = atoi(s+1);
336 else if (*s == '-')
337 filetypes[filetype_count].icon = Icon_NOICON;
338 else if (*s >= '0' && *s <= '9')
339 filetypes[filetype_count].icon = Icon_Last_Themeable + atoi(s);
340 filetype_count++;
344 int filetype_get_attr(const char* file)
346 char *extension = strrchr(file, '.');
347 int i;
348 if (!extension)
349 return 0;
350 extension++;
351 for (i=0; i<filetype_count; i++)
353 if (filetypes[i].extension &&
354 !strcasecmp(extension, filetypes[i].extension))
355 return (filetypes[i].attr<<8)&FILE_ATTR_MASK;
357 return 0;
360 static int find_attr(int attr)
362 int i;
363 /* skip the directory item */
364 if ((attr & ATTR_DIRECTORY)==ATTR_DIRECTORY)
365 return 0;
366 for (i=1; i<filetype_count; i++)
368 if ((attr>>8) == filetypes[i].attr)
369 return i;
371 return -1;
374 #ifdef HAVE_LCD_COLOR
375 int filetype_get_color(const char * name, int attr)
377 char *extension;
378 int i;
379 if ((attr & ATTR_DIRECTORY)==ATTR_DIRECTORY)
380 return custom_colors[0];
381 extension = strrchr(name, '.');
382 if (!extension)
383 return custom_colors[MAX_FILETYPES];
384 extension++;
386 for (i=1; i<filetype_count; i++)
388 if (filetypes[i].extension &&
389 !strcasecmp(extension, filetypes[i].extension))
390 return custom_colors[i];
392 return custom_colors[MAX_FILETYPES];
394 #endif
396 int filetype_get_icon(int attr)
398 int index = find_attr(attr);
399 if (index < 0)
400 return Icon_NOICON;
401 if (custom_icons_loaded)
402 return custom_filetype_icons[index];
403 return filetypes[index].icon;
406 char* filetype_get_plugin(const struct entry* file)
408 static char plugin_name[MAX_PATH];
409 int index = find_attr(file->attr);
410 if (index < 0)
411 return NULL;
412 if (filetypes[index].plugin == NULL)
413 return NULL;
414 snprintf(plugin_name, MAX_PATH, "%s/%s.%s",
415 PLUGIN_DIR, filetypes[index].plugin, ROCK_EXTENSION);
416 return plugin_name;
419 bool filetype_supported(int attr)
421 return find_attr(attr) >= 0;
424 /**** Open With Screen ****/
425 struct cb_data {
426 int *items;
427 const char *current_file;
430 static enum themable_icons openwith_get_icon(int selected_item, void * data)
432 struct cb_data *info = (struct cb_data *)data;
433 int *items = info->items;
434 return filetypes[items[selected_item]].icon;
437 static char * openwith_get_name(int selected_item, void * data,
438 char * buffer, size_t buffer_len)
440 (void)buffer; (void)buffer_len;
441 struct cb_data *info = (struct cb_data *)data;
442 int *items = info->items;
443 char *s = strrchr(filetypes[items[selected_item]].plugin, '/');
444 if (s)
445 return s+1;
446 else return filetypes[items[selected_item]].plugin;
449 static int openwith_action_callback(int action, struct gui_synclist *lists)
451 struct cb_data *info = (struct cb_data *)lists->data;
452 int *items = info->items;
453 int i;
454 if (action == ACTION_STD_OK)
456 char plugin[MAX_PATH];
457 i = items[gui_synclist_get_sel_pos(lists)];
458 snprintf(plugin, MAX_PATH, "%s/%s.%s",
459 PLUGIN_DIR, filetypes[i].plugin, ROCK_EXTENSION);
460 plugin_load(plugin, info->current_file);
461 return ACTION_STD_CANCEL;
463 return action;
466 int filetype_list_viewers(const char* current_file)
468 int i, count = 0;
469 int items[MAX_FILETYPES];
470 struct simplelist_info info;
471 struct cb_data data = { items, current_file };
472 for (i=0; i<filetype_count && count < MAX_FILETYPES; i++)
474 if (filetypes[i].plugin)
476 int j;
477 for (j=0;j<count;j++) /* check if the plugin is in the list yet */
479 if (!strcmp(filetypes[i].plugin,filetypes[items[j]].plugin))
480 break;
482 if (j<count)
483 continue; /* it is so grab the next plugin */
484 items[count++] = i;
487 #ifndef HAVE_LCD_BITMAP
488 if (count == 0)
490 /* FIX: translation! */
491 splash(HZ*2, "No viewers found");
492 return PLUGIN_OK;
494 #endif
495 simplelist_info_init(&info, str(LANG_ONPLAY_OPEN_WITH), count, &data);
496 info.action_callback = openwith_action_callback;
497 info.get_name = openwith_get_name;
498 info.get_icon = openwith_get_icon;
499 return simplelist_show_list(&info);
502 int filetype_load_plugin(const char* plugin, char* file)
504 int i;
505 char plugin_name[MAX_PATH];
506 char *s;
508 for (i=0;i<filetype_count;i++)
510 if (filetypes[i].plugin)
512 s = strrchr(filetypes[i].plugin, '/');
513 if (s)
515 if (!strcmp(s+1, plugin))
516 break;
518 else if (!strcmp(filetypes[i].plugin, plugin))
519 break;
522 if (i >= filetype_count)
523 return PLUGIN_ERROR;
524 snprintf(plugin_name, MAX_PATH, "%s/%s.%s",
525 PLUGIN_DIR, filetypes[i].plugin, ROCK_EXTENSION);
526 return plugin_load(plugin_name, file);