Add new tarball generation script.
[maemo-rb.git] / apps / filetypes.c
blob49196d40eda7a6f9aef2bc87408c09063f42447d
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 "core_alloc.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 { "ay", FILE_ATTR_AUDIO, Icon_Audio, VOICE_EXT_MPA },
116 { "gbs", FILE_ATTR_AUDIO, Icon_Audio, VOICE_EXT_MPA },
117 { "hes", FILE_ATTR_AUDIO, Icon_Audio, VOICE_EXT_MPA },
118 { "sgc", FILE_ATTR_AUDIO, Icon_Audio, VOICE_EXT_MPA },
119 { "vgm", FILE_ATTR_AUDIO, Icon_Audio, VOICE_EXT_MPA },
120 { "vgz", FILE_ATTR_AUDIO, Icon_Audio, VOICE_EXT_MPA },
121 { "kss", FILE_ATTR_AUDIO, Icon_Audio, VOICE_EXT_MPA },
122 #endif
123 { "m3u", FILE_ATTR_M3U, Icon_Playlist, LANG_PLAYLIST },
124 { "m3u8",FILE_ATTR_M3U, Icon_Playlist, LANG_PLAYLIST },
125 { "cfg", FILE_ATTR_CFG, Icon_Config, VOICE_EXT_CFG },
126 { "wps", FILE_ATTR_WPS, Icon_Wps, VOICE_EXT_WPS },
127 #ifdef HAVE_REMOTE_LCD
128 { "rwps",FILE_ATTR_RWPS, Icon_Wps, VOICE_EXT_RWPS },
129 #endif
130 #if CONFIG_TUNER
131 { "fmr", FILE_ATTR_FMR, Icon_Preset, LANG_FMR },
132 { "fms", FILE_ATTR_FMS, Icon_Wps, VOICE_EXT_FMS },
133 #endif
134 { "lng", FILE_ATTR_LNG, Icon_Language, LANG_LANGUAGE },
135 { "rock",FILE_ATTR_ROCK,Icon_Plugin, VOICE_EXT_ROCK },
136 { "lua", FILE_ATTR_LUA, Icon_Plugin, VOICE_EXT_ROCK },
137 #ifdef HAVE_LCD_BITMAP
138 { "fnt", FILE_ATTR_FONT,Icon_Font, VOICE_EXT_FONT },
139 { "kbd", FILE_ATTR_KBD, Icon_Keyboard, VOICE_EXT_KBD },
140 #endif
141 { "bmark",FILE_ATTR_BMARK, Icon_Bookmark, VOICE_EXT_BMARK },
142 { "cue", FILE_ATTR_CUE, Icon_Bookmark, VOICE_EXT_CUESHEET },
143 #ifdef HAVE_LCD_BITMAP
144 { "sbs", FILE_ATTR_SBS, Icon_Wps, VOICE_EXT_SBS },
145 #endif
146 #ifdef HAVE_REMOTE_LCD
147 { "rsbs", FILE_ATTR_RSBS, Icon_Wps, VOICE_EXT_RSBS },
148 #if CONFIG_TUNER
149 { "rfms", FILE_ATTR_RFMS, Icon_Wps, VOICE_EXT_RFMS },
150 #endif
151 #endif
152 #ifdef BOOTFILE_EXT
153 { BOOTFILE_EXT, FILE_ATTR_MOD, Icon_Firmware, VOICE_EXT_AJZ },
154 #endif
155 #ifdef BOOTFILE_EXT2
156 { BOOTFILE_EXT2, FILE_ATTR_MOD, Icon_Firmware, VOICE_EXT_AJZ },
157 #endif
160 void tree_get_filetypes(const struct filetype** types, int* count)
162 *types = inbuilt_filetypes;
163 *count = sizeof(inbuilt_filetypes) / sizeof(*inbuilt_filetypes);
166 #define ROCK_EXTENSION "rock"
168 struct file_type {
169 enum themable_icons icon; /* the icon which shall be used for it, NOICON if unknown */
170 unsigned char attr; /* FILE_ATTR_MASK >> 8 */
171 char* plugin; /* Which plugin to use, NULL if unknown, or builtin */
172 char* extension; /* NULL for none */
174 static struct file_type filetypes[MAX_FILETYPES];
175 static int custom_filetype_icons[MAX_FILETYPES];
176 static bool custom_icons_loaded = false;
177 #ifdef HAVE_LCD_COLOR
178 static int custom_colors[MAX_FILETYPES+1];
179 #endif
180 /* index array to filetypes used in open with list. */
181 static int viewers[MAX_VIEWERS];
182 static int filetype_count = 0;
183 static unsigned char highest_attr = 0;
184 static int viewer_count = 0;
186 static int strdup_handle, strdup_bufsize, strdup_cur_idx;
187 static int move_callback(int handle, void* current, void* new)
189 /*could compare to strdup_handle, but ops is only used once */
190 (void)handle;
191 size_t diff = new - current;
192 #define FIX_PTR(x) \
193 { if ((void*)x >= current && (void*)x < (current+strdup_bufsize)) x+= diff; }
194 for(int i = 0; i < filetype_count; i++)
196 FIX_PTR(filetypes[i].extension);
197 FIX_PTR(filetypes[i].plugin);
199 return BUFLIB_CB_OK;
202 static struct buflib_callbacks ops = {
203 .move_callback = move_callback,
204 .shrink_callback = NULL,
207 static char *filetypes_strdup(char* string)
209 char *buffer = core_get_data(strdup_handle) + strdup_cur_idx;
210 strdup_cur_idx += strlcpy(buffer, string, strdup_bufsize-strdup_cur_idx)+1;
211 return buffer;
214 static char *filetypes_store_plugin(char *plugin, int n)
216 int i;
217 /* if the plugin is in the list already, use it. */
218 for (i=0; i<viewer_count; i++)
220 if (!strcmp(filetypes[viewers[i]].plugin, plugin))
221 return filetypes[viewers[i]].plugin;
223 /* otherwise, allocate buffer */
224 if (viewer_count < MAX_VIEWERS)
225 viewers[viewer_count++] = n;
226 return filetypes_strdup(plugin);
229 static int find_extension(const char* extension)
231 int i;
232 if (!extension)
233 return -1;
234 for (i=1; i<filetype_count; i++)
236 if (filetypes[i].extension &&
237 !strcasecmp(extension, filetypes[i].extension))
238 return i;
240 return -1;
243 static void read_builtin_types(void);
244 static void read_config(int fd);
245 #ifdef HAVE_LCD_COLOR
246 /* Colors file format is similar to icons:
247 * ext:hex_color
248 * load a colors file from a theme with:
249 * filetype colours: filename.colours */
250 void read_color_theme_file(void) {
251 char buffer[MAX_PATH];
252 int fd;
253 char *ext, *color;
254 int i;
255 for (i = 0; i < MAX_FILETYPES+1; i++) {
256 custom_colors[i] = -1;
258 snprintf(buffer, MAX_PATH, THEME_DIR "/%s.colours",
259 global_settings.colors_file);
260 fd = open(buffer, O_RDONLY);
261 if (fd < 0)
262 return;
263 while (read_line(fd, buffer, MAX_PATH) > 0)
265 if (!settings_parseline(buffer, &ext, &color))
266 continue;
267 if (!strcasecmp(ext, "folder"))
269 hex_to_rgb(color, &custom_colors[0]);
270 continue;
272 if (!strcasecmp(ext, "???"))
274 hex_to_rgb(color, &custom_colors[MAX_FILETYPES]);
275 continue;
277 i = find_extension(ext);
278 if (i >= 0)
279 hex_to_rgb(color, &custom_colors[i]);
281 close(fd);
283 #endif
284 #ifdef HAVE_LCD_BITMAP
285 void read_viewer_theme_file(void)
287 char buffer[MAX_PATH];
288 int fd;
289 char *ext, *icon;
290 int i;
291 global_status.viewer_icon_count = 0;
292 custom_icons_loaded = false;
293 custom_filetype_icons[0] = Icon_Folder;
294 for (i=1; i<filetype_count; i++)
296 custom_filetype_icons[i] = filetypes[i].icon;
299 snprintf(buffer, MAX_PATH, "%s/%s.icons", ICON_DIR,
300 global_settings.viewers_icon_file);
301 fd = open(buffer, O_RDONLY);
302 if (fd < 0)
303 return;
304 while (read_line(fd, buffer, MAX_PATH) > 0)
306 if (!settings_parseline(buffer, &ext, &icon))
307 continue;
308 i = find_extension(ext);
309 if (i >= 0)
311 if (*icon == '*')
312 custom_filetype_icons[i] = atoi(icon+1);
313 else if (*icon == '-')
314 custom_filetype_icons[i] = Icon_NOICON;
315 else if (*icon >= '0' && *icon <= '9')
317 int number = atoi(icon);
318 if (number > global_status.viewer_icon_count)
319 global_status.viewer_icon_count++;
320 custom_filetype_icons[i] = Icon_Last_Themeable + number;
324 close(fd);
325 custom_icons_loaded = true;
327 #endif
329 void filetype_init(void)
331 /* set the directory item first */
332 filetypes[0].extension = NULL;
333 filetypes[0].plugin = NULL;
334 filetypes[0].attr = 0;
335 filetypes[0].icon = Icon_Folder;
337 /* estimate bufsize with the filesize, will not be larger */
338 viewer_count = 0;
339 filetype_count = 1;
341 int fd = open(VIEWERS_CONFIG, O_RDONLY);
342 if (fd < 0)
343 return;
345 strdup_bufsize = filesize(fd);
346 strdup_handle = core_alloc_ex("filetypes", strdup_bufsize, &ops);
347 if (strdup_handle < 0)
349 close(fd);
350 return;
352 read_builtin_types();
353 read_config(fd);
354 close(fd);
355 #ifdef HAVE_LCD_BITMAP
356 read_viewer_theme_file();
357 #endif
358 #ifdef HAVE_LCD_COLOR
359 read_color_theme_file();
360 #endif
361 core_shrink(strdup_handle, core_get_data(strdup_handle), strdup_cur_idx);
364 /* remove all white spaces from string */
365 static void rm_whitespaces(char* str)
367 char *s = str;
368 while (*str)
370 if (!isspace(*str))
372 *s = *str;
373 s++;
375 str++;
377 *s = '\0';
380 static void read_builtin_types(void)
382 int count = sizeof(inbuilt_filetypes)/sizeof(*inbuilt_filetypes), i;
383 for(i=0; i<count && (filetype_count < MAX_FILETYPES); i++)
385 filetypes[filetype_count].extension = inbuilt_filetypes[i].extension;
386 filetypes[filetype_count].plugin = NULL;
387 filetypes[filetype_count].attr = inbuilt_filetypes[i].tree_attr>>8;
388 if (filetypes[filetype_count].attr > highest_attr)
389 highest_attr = filetypes[filetype_count].attr;
390 filetypes[filetype_count].icon = inbuilt_filetypes[i].icon;
391 filetype_count++;
395 static void read_config(int fd)
397 char line[64], *s, *e;
398 char *extension, *plugin;
399 /* config file is in the format
400 <extension>,<plugin>,<icon code>
401 ignore line if either of the first two are missing */
402 while (read_line(fd, line, sizeof line) > 0)
404 if (filetype_count >= MAX_FILETYPES)
406 splash(HZ, ID2P(LANG_FILETYPES_FULL));
407 break;
409 rm_whitespaces(line);
410 /* get the extension */
411 s = line;
412 e = strchr(s, ',');
413 if (!e)
414 continue;
415 *e = '\0';
416 extension = s;
418 /* get the plugin */
419 s = e+1;
420 e = strchr(s, ',');
421 if (!e)
422 continue;
423 *e = '\0';
424 plugin = s;
426 /* ok, store this plugin/extension, check icon after */
427 struct file_type *file_type = &filetypes[filetype_count];
428 file_type->extension = filetypes_strdup(extension);
429 file_type->plugin = filetypes_store_plugin(plugin, filetype_count);
430 file_type->attr = highest_attr +1;
431 file_type->icon = Icon_Questionmark;
432 highest_attr++;
433 /* get the icon */
434 s = e+1;
435 if (*s == '*')
436 file_type->icon = atoi(s+1);
437 else if (*s == '-')
438 file_type->icon = Icon_NOICON;
439 else if (*s >= '0' && *s <= '9')
440 file_type->icon = Icon_Last_Themeable + atoi(s);
441 filetype_count++;
445 int filetype_get_attr(const char* file)
447 char *extension = strrchr(file, '.');
448 int i;
449 if (!extension)
450 return 0;
451 extension++;
453 i = find_extension(extension);
454 if (i >= 0)
455 return (filetypes[i].attr<<8)&FILE_ATTR_MASK;
456 return 0;
459 static int find_attr(int attr)
461 int i;
462 /* skip the directory item */
463 if ((attr & ATTR_DIRECTORY)==ATTR_DIRECTORY)
464 return 0;
465 for (i=1; i<filetype_count; i++)
467 if ((attr>>8) == filetypes[i].attr)
468 return i;
470 return -1;
473 #ifdef HAVE_LCD_COLOR
474 int filetype_get_color(const char * name, int attr)
476 char *extension;
477 int i;
478 if ((attr & ATTR_DIRECTORY)==ATTR_DIRECTORY)
479 return custom_colors[0];
480 extension = strrchr(name, '.');
481 if (!extension)
482 return custom_colors[MAX_FILETYPES];
483 extension++;
485 i = find_extension(extension);
486 if (i >= 0)
487 return custom_colors[i];
488 return custom_colors[MAX_FILETYPES];
490 #endif
492 int filetype_get_icon(int attr)
494 int index = find_attr(attr);
495 if (index < 0)
496 return Icon_NOICON;
497 if (custom_icons_loaded)
498 return custom_filetype_icons[index];
499 return filetypes[index].icon;
502 char* filetype_get_plugin(const struct entry* file)
504 static char plugin_name[MAX_PATH];
505 int index = find_attr(file->attr);
506 if (index < 0)
507 return NULL;
508 if (filetypes[index].plugin == NULL)
509 return NULL;
510 snprintf(plugin_name, MAX_PATH, "%s/%s.%s",
511 PLUGIN_DIR, filetypes[index].plugin, ROCK_EXTENSION);
512 return plugin_name;
515 bool filetype_supported(int attr)
517 return find_attr(attr) >= 0;
520 /**** Open With Screen ****/
521 struct cb_data {
522 const char *current_file;
525 static enum themable_icons openwith_get_icon(int selected_item, void * data)
527 (void)data;
528 return filetypes[viewers[selected_item]].icon;
531 static const char* openwith_get_name(int selected_item, void * data,
532 char * buffer, size_t buffer_len)
534 (void)data; (void)buffer; (void)buffer_len;
535 const char *s = strrchr(filetypes[viewers[selected_item]].plugin, '/');
536 if (s)
537 return s+1;
538 else return filetypes[viewers[selected_item]].plugin;
541 static int openwith_action_callback(int action, struct gui_synclist *lists)
543 struct cb_data *info = (struct cb_data *)lists->data;
544 int i;
545 if (action == ACTION_STD_OK)
547 char plugin[MAX_PATH];
548 i = viewers[gui_synclist_get_sel_pos(lists)];
549 snprintf(plugin, MAX_PATH, "%s/%s.%s",
550 PLUGIN_DIR, filetypes[i].plugin, ROCK_EXTENSION);
551 plugin_load(plugin, info->current_file);
552 return ACTION_STD_CANCEL;
554 return action;
557 int filetype_list_viewers(const char* current_file)
559 struct simplelist_info info;
560 struct cb_data data = { current_file };
561 #ifndef HAVE_LCD_BITMAP
562 if (viewer_count == 0)
564 /* FIX: translation! */
565 splash(HZ*2, "No viewers found");
566 return PLUGIN_OK;
568 #endif
569 simplelist_info_init(&info, str(LANG_ONPLAY_OPEN_WITH), viewer_count, &data);
570 info.action_callback = openwith_action_callback;
571 info.get_name = openwith_get_name;
572 info.get_icon = global_settings.show_icons?openwith_get_icon:NULL;
573 return simplelist_show_list(&info);
576 int filetype_load_plugin(const char* plugin, const char* file)
578 int i;
579 char plugin_name[MAX_PATH];
580 char *s;
582 for (i=0;i<filetype_count;i++)
584 if (filetypes[i].plugin)
586 s = strrchr(filetypes[i].plugin, '/');
587 if (s)
589 if (!strcmp(s+1, plugin))
590 break;
592 else if (!strcmp(filetypes[i].plugin, plugin))
593 break;
596 if (i >= filetype_count)
597 return PLUGIN_ERROR;
598 snprintf(plugin_name, MAX_PATH, "%s/%s.%s",
599 PLUGIN_DIR, filetypes[i].plugin, ROCK_EXTENSION);
600 return plugin_load(plugin_name, file);