typo fixes (; instead of , at the end of two assignments) (same as r17398 for usb_ser...
[kugel-rb.git] / apps / filetypes.c
blob722456b8f432667bad6a0b41b28b2c7293818e6e
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 <ctype.h>
27 #include "sprintf.h"
28 #include "settings.h"
29 #include "debug.h"
30 #include "lang.h"
31 #include "language.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 48
48 #endif
50 /* a table for the know file types */
51 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 { "wma", FILE_ATTR_AUDIO, Icon_Audio, VOICE_EXT_MPA },
60 { "wmv", FILE_ATTR_AUDIO, Icon_Audio, VOICE_EXT_MPA },
61 { "asf", FILE_ATTR_AUDIO, Icon_Audio, VOICE_EXT_MPA },
62 { "wav", FILE_ATTR_AUDIO, Icon_Audio, VOICE_EXT_MPA },
63 { "flac",FILE_ATTR_AUDIO, Icon_Audio, VOICE_EXT_MPA },
64 { "ac3", FILE_ATTR_AUDIO, Icon_Audio, VOICE_EXT_MPA },
65 { "a52", FILE_ATTR_AUDIO, Icon_Audio, VOICE_EXT_MPA },
66 { "mpc", FILE_ATTR_AUDIO, Icon_Audio, VOICE_EXT_MPA },
67 { "wv", FILE_ATTR_AUDIO, Icon_Audio, VOICE_EXT_MPA },
68 { "m4a", FILE_ATTR_AUDIO, Icon_Audio, VOICE_EXT_MPA },
69 { "m4b", FILE_ATTR_AUDIO, Icon_Audio, VOICE_EXT_MPA },
70 { "mp4", FILE_ATTR_AUDIO, Icon_Audio, VOICE_EXT_MPA },
71 { "shn", FILE_ATTR_AUDIO, Icon_Audio, VOICE_EXT_MPA },
72 { "aif", FILE_ATTR_AUDIO, Icon_Audio, VOICE_EXT_MPA },
73 { "aiff",FILE_ATTR_AUDIO, Icon_Audio, VOICE_EXT_MPA },
74 { "spx" ,FILE_ATTR_AUDIO, Icon_Audio, VOICE_EXT_MPA },
75 { "sid", FILE_ATTR_AUDIO, Icon_Audio, VOICE_EXT_MPA },
76 { "adx", FILE_ATTR_AUDIO, Icon_Audio, VOICE_EXT_MPA },
77 { "nsf", FILE_ATTR_AUDIO, Icon_Audio, VOICE_EXT_MPA },
78 { "nsfe",FILE_ATTR_AUDIO, Icon_Audio, VOICE_EXT_MPA },
79 { "spc", FILE_ATTR_AUDIO, Icon_Audio, VOICE_EXT_MPA },
80 { "ape", FILE_ATTR_AUDIO, Icon_Audio, VOICE_EXT_MPA },
81 { "mac", FILE_ATTR_AUDIO, Icon_Audio, VOICE_EXT_MPA },
82 #endif
83 { "m3u", FILE_ATTR_M3U, Icon_Playlist, LANG_PLAYLIST },
84 { "m3u8",FILE_ATTR_M3U, Icon_Playlist, LANG_PLAYLIST },
85 { "cfg", FILE_ATTR_CFG, Icon_Config, VOICE_EXT_CFG },
86 { "wps", FILE_ATTR_WPS, Icon_Wps, VOICE_EXT_WPS },
87 #ifdef HAVE_REMOTE_LCD
88 { "rwps",FILE_ATTR_RWPS, Icon_Wps, VOICE_EXT_RWPS },
89 #endif
90 #if LCD_DEPTH > 1
91 { "bmp", FILE_ATTR_BMP, Icon_Wps, VOICE_EXT_WPS },
92 #endif
93 #if CONFIG_TUNER
94 { "fmr", FILE_ATTR_FMR, Icon_Preset, LANG_FMR },
95 #endif
96 { "lng", FILE_ATTR_LNG, Icon_Language, LANG_LANGUAGE },
97 { "rock",FILE_ATTR_ROCK,Icon_Plugin, VOICE_EXT_ROCK },
98 #ifdef HAVE_LCD_BITMAP
99 { "fnt", FILE_ATTR_FONT,Icon_Font, VOICE_EXT_FONT },
100 { "kbd", FILE_ATTR_KBD, Icon_Keyboard, VOICE_EXT_KBD },
101 #endif
102 { "bmark",FILE_ATTR_BMARK, Icon_Bookmark, VOICE_EXT_BMARK },
103 { "cue", FILE_ATTR_CUE, Icon_Bookmark, VOICE_EXT_CUESHEET },
104 #ifdef BOOTFILE_EXT
105 { BOOTFILE_EXT, FILE_ATTR_MOD, Icon_Firmware, VOICE_EXT_AJZ },
106 #endif /* #ifndef SIMULATOR */
109 void tree_get_filetypes(const struct filetype** types, int* count)
111 *types = inbuilt_filetypes;
112 *count = sizeof(inbuilt_filetypes) / sizeof(*inbuilt_filetypes);
115 /* mask for dynamic filetype info in attribute */
116 #define FILETYPES_MASK 0xFF00
117 #define ROCK_EXTENSION "rock"
119 struct file_type {
120 int icon; /* the icon which shall be used for it, NOICON if unknown */
121 unsigned char attr; /* FILETYPES_MASK >> 8 */
122 char* plugin; /* Which plugin to use, NULL if unknown, or builtin */
123 char* extension; /* NULL for none */
125 static struct file_type filetypes[MAX_FILETYPES];
126 static int custom_filetype_icons[MAX_FILETYPES];
127 static bool custom_icons_loaded = false;
128 #ifdef HAVE_LCD_COLOR
129 static int custom_colors[MAX_FILETYPES+1];
130 #endif
131 static int filetype_count = 0;
132 static unsigned char heighest_attr = 0;
134 static char *filetypes_strdup(char* string)
136 char *buffer = (char*)buffer_alloc(strlen(string)+1);
137 strcpy(buffer, string);
138 return buffer;
140 static void read_builtin_types(void);
141 static void read_config(char* config_file);
142 #ifdef HAVE_LCD_COLOR
143 /* Colors file format is similar to icons:
144 * ext:hex_color
145 * load a colors file from a theme with:
146 * filetype colours: filename.colours */
147 void read_color_theme_file(void) {
148 char buffer[MAX_PATH];
149 int fd;
150 char *ext, *color;
151 int i;
152 for (i = 0; i < MAX_FILETYPES+1; i++) {
153 custom_colors[i] = -1;
155 snprintf(buffer, MAX_PATH, "%s/%s.colours", THEME_DIR,
156 global_settings.colors_file);
157 fd = open(buffer, O_RDONLY);
158 if (fd < 0)
159 return;
160 while (read_line(fd, buffer, MAX_PATH) > 0)
162 if (!settings_parseline(buffer, &ext, &color))
163 continue;
164 if (!strcasecmp(ext, "folder"))
166 hex_to_rgb(color, &custom_colors[0]);
167 continue;
169 if (!strcasecmp(ext, "???"))
171 hex_to_rgb(color, &custom_colors[MAX_FILETYPES]);
172 continue;
174 for (i=1; i<filetype_count; i++)
176 if (filetypes[i].extension &&
177 !strcasecmp(ext, filetypes[i].extension))
179 hex_to_rgb(color, &custom_colors[i]);
180 break;
184 close(fd);
186 #endif
187 #ifdef HAVE_LCD_BITMAP
188 void read_viewer_theme_file(void)
190 char buffer[MAX_PATH];
191 int fd;
192 char *ext, *icon;
193 int i;
194 global_status.viewer_icon_count = 0;
195 custom_icons_loaded = false;
196 custom_filetype_icons[0] = Icon_Folder;
197 for (i=1; i<filetype_count; i++)
199 custom_filetype_icons[i] = filetypes[i].icon;
202 snprintf(buffer, MAX_PATH, "%s/%s.icons", ICON_DIR,
203 global_settings.viewers_icon_file);
204 fd = open(buffer, O_RDONLY);
205 if (fd < 0)
206 return;
207 while (read_line(fd, buffer, MAX_PATH) > 0)
209 if (!settings_parseline(buffer, &ext, &icon))
210 continue;
211 for (i=0; i<filetype_count; i++)
213 if (filetypes[i].extension &&
214 !strcasecmp(ext, filetypes[i].extension))
216 if (*icon == '*')
217 custom_filetype_icons[i] = atoi(icon+1);
218 else if (*icon == '-')
219 custom_filetype_icons[i] = Icon_NOICON;
220 else if (*icon >= '0' && *icon <= '9')
222 int number = atoi(icon);
223 if (number > global_status.viewer_icon_count)
224 global_status.viewer_icon_count++;
225 custom_filetype_icons[i] = Icon_Last_Themeable + number;
227 break;
231 close(fd);
232 custom_icons_loaded = true;
234 #endif
236 void filetype_init(void)
238 /* set the directory item first */
239 filetypes[0].extension = NULL;
240 filetypes[0].plugin = NULL;
241 filetypes[0].attr = 0;
242 filetypes[0].icon = Icon_Folder;
244 filetype_count = 1;
245 read_builtin_types();
246 read_config(VIEWERS_CONFIG);
247 #ifdef HAVE_LCD_BITMAP
248 read_viewer_theme_file();
249 #endif
250 #ifdef HAVE_LCD_COLOR
251 read_color_theme_file();
252 #endif
255 /* remove all white spaces from string */
256 static void rm_whitespaces(char* str)
258 char *s = str;
259 while (*str)
261 if (!isspace(*str))
263 *s = *str;
264 s++;
266 str++;
268 *s = '\0';
271 static void read_builtin_types(void)
273 int count = sizeof(inbuilt_filetypes)/sizeof(*inbuilt_filetypes), i;
274 for(i=0; i<count && (filetype_count < MAX_FILETYPES); i++)
276 filetypes[filetype_count].extension = inbuilt_filetypes[i].extension;
277 filetypes[filetype_count].plugin = NULL;
278 filetypes[filetype_count].attr = inbuilt_filetypes[i].tree_attr>>8;
279 if (filetypes[filetype_count].attr > heighest_attr)
280 heighest_attr = filetypes[filetype_count].attr;
281 filetypes[filetype_count].icon = inbuilt_filetypes[i].icon;
282 filetype_count++;
286 static void read_config(char* config_file)
288 char line[64], *s, *e;
289 char extension[8], plugin[32];
290 int fd = open(config_file, O_RDONLY);
291 if (fd < 0)
292 return;
293 /* config file is in the for
294 <extension>,<plugin>,<icon code>
295 ignore line if either of the first two are missing */
296 while (read_line(fd, line, 64) > 0)
298 if (filetype_count >= MAX_FILETYPES)
300 gui_syncsplash(HZ, ID2P(LANG_FILETYPES_FULL));
301 break;
303 rm_whitespaces(line);
304 /* get the extention */
305 s = line;
306 e = strchr(s, ',');
307 if (!e)
308 continue;
309 *e = '\0';
310 strcpy(extension, s);
312 /* get the plugin */
313 s = e+1;
314 e = strchr(s, ',');
315 if (!e)
316 continue;
317 *e = '\0';
319 strcpy(plugin, s);
320 /* ok, store this plugin/extension, check icon after */
321 filetypes[filetype_count].extension = filetypes_strdup(extension);
322 filetypes[filetype_count].plugin = filetypes_strdup(plugin);
323 filetypes[filetype_count].attr = heighest_attr +1;
324 filetypes[filetype_count].icon = Icon_Questionmark;
325 heighest_attr++;
326 /* get the icon */
327 s = e+1;
328 if (*s == '*')
329 filetypes[filetype_count].icon = atoi(s+1);
330 else if (*s == '-')
331 filetypes[filetype_count].icon = Icon_NOICON;
332 else if (*s >= '0' && *s <= '9')
333 filetypes[filetype_count].icon = Icon_Last_Themeable + atoi(s);
334 filetype_count++;
338 int filetype_get_attr(const char* file)
340 char *extension = strrchr(file, '.');
341 int i;
342 if (!extension)
343 return 0;
344 extension++;
345 for (i=0; i<filetype_count; i++)
347 if (filetypes[i].extension &&
348 !strcasecmp(extension, filetypes[i].extension))
349 return (filetypes[i].attr<<8)&FILE_ATTR_MASK;
351 return 0;
354 static int find_attr(int attr)
356 int i;
357 /* skip the directory item */
358 if ((attr & ATTR_DIRECTORY)==ATTR_DIRECTORY)
359 return 0;
360 for (i=1; i<filetype_count; i++)
362 if ((attr>>8) == filetypes[i].attr)
363 return i;
365 return -1;
368 #ifdef HAVE_LCD_COLOR
369 int filetype_get_color(const char * name, int attr)
371 char *extension;
372 int i;
373 if ((attr & ATTR_DIRECTORY)==ATTR_DIRECTORY)
374 return custom_colors[0];
375 extension = strrchr(name, '.');
376 if (!extension)
377 return custom_colors[MAX_FILETYPES];
378 extension++;
380 for (i=1; i<filetype_count; i++)
382 if (filetypes[i].extension &&
383 !strcasecmp(extension, filetypes[i].extension))
384 return custom_colors[i];
386 return custom_colors[MAX_FILETYPES];
388 #endif
390 int filetype_get_icon(int attr)
392 int index = find_attr(attr);
393 if (index < 0)
394 return Icon_NOICON;
395 if (custom_icons_loaded)
396 return custom_filetype_icons[index];
397 return filetypes[index].icon;
400 char* filetype_get_plugin(const struct entry* file)
402 static char plugin_name[MAX_PATH];
403 int index = find_attr(file->attr);
404 if (index < 0)
405 return NULL;
406 if (filetypes[index].plugin == NULL)
407 return NULL;
408 snprintf(plugin_name, MAX_PATH, "%s/%s.%s",
409 PLUGIN_DIR, filetypes[index].plugin, ROCK_EXTENSION);
410 return plugin_name;
413 bool filetype_supported(int attr)
415 return find_attr(attr) >= 0;
418 /**** Open With Screen ****/
419 struct cb_data {
420 int *items;
421 char *current_file;
424 static enum themable_icons openwith_get_icon(int selected_item, void * data)
426 struct cb_data *info = (struct cb_data *)data;
427 int *items = info->items;
428 return filetypes[items[selected_item]].icon;
431 static char * openwith_get_name(int selected_item, void * data,
432 char * buffer, size_t buffer_len)
434 (void)buffer; (void)buffer_len;
435 struct cb_data *info = (struct cb_data *)data;
436 int *items = info->items;
437 char *s = strrchr(filetypes[items[selected_item]].plugin, '/');
438 if (s)
439 return s+1;
440 else return filetypes[items[selected_item]].plugin;
443 static int openwith_action_callback(int action, struct gui_synclist *lists)
445 struct cb_data *info = (struct cb_data *)lists->data;
446 int *items = info->items;
447 int i;
448 if (action == ACTION_STD_OK)
450 char plugin[MAX_PATH];
451 i = items[gui_synclist_get_sel_pos(lists)];
452 snprintf(plugin, MAX_PATH, "%s/%s.%s",
453 PLUGIN_DIR, filetypes[i].plugin, ROCK_EXTENSION);
454 plugin_load(plugin, info->current_file);
455 return ACTION_STD_CANCEL;
457 return action;
460 int filetype_list_viewers(const char* current_file)
462 int i, count = 0;
463 int items[MAX_FILETYPES];
464 struct simplelist_info info;
465 struct cb_data data = { items, (char*)current_file };
466 for (i=0; i<filetype_count && count < MAX_FILETYPES; i++)
468 if (filetypes[i].plugin)
470 int j;
471 for (j=0;j<count;j++) /* check if the plugin is in the list yet */
473 if (filetypes[i].plugin &&
474 !strcmp(filetypes[i].plugin,filetypes[items[j]].plugin))
475 break;
477 if (j<count)
478 continue; /* it is so grab the next plugin */
479 items[count++] = i;
482 #ifndef HAVE_LCD_BITMAP
483 if (count == 0)
485 /* FIX: translation! */
486 gui_syncsplash(HZ*2, (unsigned char *)"No viewers found");
487 return PLUGIN_OK;
489 #endif
490 simplelist_info_init(&info, str(LANG_ONPLAY_OPEN_WITH), count, &data);
491 info.action_callback = openwith_action_callback;
492 info.get_name = openwith_get_name;
493 info.get_icon = openwith_get_icon;
494 return simplelist_show_list(&info);
497 int filetype_load_plugin(const char* plugin, char* file)
499 int i;
500 char plugin_name[MAX_PATH];
501 char *s;
503 for (i=0;i<filetype_count;i++)
505 if (filetypes[i].plugin)
507 s = strrchr(filetypes[i].plugin, '/');
508 if (s)
510 if (!strcmp(s+1, plugin))
511 break;
513 else if (!strcmp(filetypes[i].plugin, plugin))
514 break;
517 if (i >= filetype_count)
518 return PLUGIN_ERROR;
519 snprintf(plugin_name, MAX_PATH, "%s/%s.%s",
520 PLUGIN_DIR, filetypes[i].plugin, ROCK_EXTENSION);
521 return plugin_load(plugin_name,file);