blob: 38324d0f3a03d5dc24096456f14429ca09f216d1 [file] [log] [blame]
Björn Stenbergfb00c212004-05-21 20:08:24 +00001/***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 *
8 * $Id$
9 *
Jonathan Gordon21165a32007-03-29 06:16:00 +000010 * Copyright (C) 2007 Jonathan Gordon
Björn Stenbergfb00c212004-05-21 20:08:24 +000011 *
Daniel Stenberg2acc0ac2008-06-28 18:10:04 +000012 * 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.
Björn Stenbergfb00c212004-05-21 20:08:24 +000016 *
17 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
18 * KIND, either express or implied.
19 *
20 ****************************************************************************/
21
22#include <stdio.h>
23#include <string.h>
24#include <stdlib.h>
25#include <stdbool.h>
Jonathan Gordon21165a32007-03-29 06:16:00 +000026#include "string.h"
27#include <ctype.h>
Björn Stenbergfb00c212004-05-21 20:08:24 +000028
Björn Stenbergfb00c212004-05-21 20:08:24 +000029#include "settings.h"
30#include "debug.h"
31#include "lang.h"
Björn Stenbergfb00c212004-05-21 20:08:24 +000032#include "kernel.h"
33#include "plugin.h"
34#include "filetypes.h"
35#include "screens.h"
Björn Stenbergfb00c212004-05-21 20:08:24 +000036#include "dir.h"
37#include "file.h"
Kevin Ferraree991bee2005-11-16 15:12:15 +000038#include "splash.h"
Thomas Martitzd0b72e22011-08-30 14:01:33 +000039#include "core_alloc.h"
Brandon Low74cbb0a2007-06-17 21:16:34 +000040#include "icons.h"
Brandon Lowf8682a02007-06-17 23:10:01 +000041#include "logf.h"
Björn Stenbergfb00c212004-05-21 20:08:24 +000042
Jonathan Gordon07a7d472007-08-12 09:53:41 +000043/* max filetypes (plugins & icons stored here) */
44#if CONFIG_CODEC == SWCODEC
Yoshihisa Uchidaf2df42e2010-05-12 12:41:31 +000045#define MAX_FILETYPES 192
Jonathan Gordon07a7d472007-08-12 09:53:41 +000046#else
Yoshihisa Uchidaf2df42e2010-05-12 12:41:31 +000047#define MAX_FILETYPES 128
Jonathan Gordon07a7d472007-08-12 09:53:41 +000048#endif
Teruaki Kawashima7d3f8c02010-11-09 14:35:19 +000049/* max viewer plugins */
50#ifdef HAVE_LCD_BITMAP
51#define MAX_VIEWERS 56
52#else
53#define MAX_VIEWERS 24
54#endif
Jonathan Gordon07a7d472007-08-12 09:53:41 +000055
Teruaki Kawashima7d3f8c02010-11-09 14:35:19 +000056/* a table for the known file types */
Nils Wallménius11a667b2008-05-24 20:56:19 +000057static const struct filetype inbuilt_filetypes[] = {
Jonathan Gordon36a2e302007-04-18 13:03:01 +000058 { "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
Jonathan Gordon07a7d472007-08-12 09:53:41 +000062 /* Temporary hack to allow playlist creation */
Jonathan Gordon36a2e302007-04-18 13:03:01 +000063 { "mp1", FILE_ATTR_AUDIO, Icon_Audio, VOICE_EXT_MPA },
64 { "ogg", FILE_ATTR_AUDIO, Icon_Audio, VOICE_EXT_MPA },
Dave Chapman25d4f4b2008-10-30 01:17:26 +000065 { "oga", FILE_ATTR_AUDIO, Icon_Audio, VOICE_EXT_MPA },
Jonathan Gordon36a2e302007-04-18 13:03:01 +000066 { "wma", FILE_ATTR_AUDIO, Icon_Audio, VOICE_EXT_MPA },
Dave Chapman48304ce2007-07-03 13:16:49 +000067 { "wmv", FILE_ATTR_AUDIO, Icon_Audio, VOICE_EXT_MPA },
68 { "asf", FILE_ATTR_AUDIO, Icon_Audio, VOICE_EXT_MPA },
Jonathan Gordon36a2e302007-04-18 13:03:01 +000069 { "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 },
Thom Johansenc0f7eb92008-05-21 11:19:58 +000078 { "mod", FILE_ATTR_AUDIO, Icon_Audio, VOICE_EXT_MPA },
Jonathan Gordon36a2e302007-04-18 13:03:01 +000079 { "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 },
Frederik M J Vestre1b8e3802012-07-26 14:38:32 +020083 { "opus",FILE_ATTR_AUDIO, Icon_Audio, VOICE_EXT_MPA },
Jonathan Gordon36a2e302007-04-18 13:03:01 +000084 { "sid", FILE_ATTR_AUDIO, Icon_Audio, VOICE_EXT_MPA },
85 { "adx", FILE_ATTR_AUDIO, Icon_Audio, VOICE_EXT_MPA },
86 { "nsf", FILE_ATTR_AUDIO, Icon_Audio, VOICE_EXT_MPA },
87 { "nsfe",FILE_ATTR_AUDIO, Icon_Audio, VOICE_EXT_MPA },
88 { "spc", FILE_ATTR_AUDIO, Icon_Audio, VOICE_EXT_MPA },
Dave Chapman52027422007-06-05 16:58:29 +000089 { "ape", FILE_ATTR_AUDIO, Icon_Audio, VOICE_EXT_MPA },
90 { "mac", FILE_ATTR_AUDIO, Icon_Audio, VOICE_EXT_MPA },
Dominik Wenger25bb37b2008-07-26 15:16:10 +000091 { "sap" ,FILE_ATTR_AUDIO, Icon_Audio, VOICE_EXT_MPA },
Mohamed Tareke184ef12009-07-06 22:40:45 +000092 { "rm", FILE_ATTR_AUDIO, Icon_Audio, VOICE_EXT_MPA },
93 { "ra", FILE_ATTR_AUDIO, Icon_Audio, VOICE_EXT_MPA },
Mohamed Tarek320bd1a2009-07-27 11:36:26 +000094 { "rmvb",FILE_ATTR_AUDIO, Icon_Audio, VOICE_EXT_MPA },
Dominik Wengere96d1392010-02-06 12:49:03 +000095 { "cmc", FILE_ATTR_AUDIO, Icon_Audio, VOICE_EXT_MPA },
96 { "cm3", FILE_ATTR_AUDIO, Icon_Audio, VOICE_EXT_MPA },
97 { "cmr", FILE_ATTR_AUDIO, Icon_Audio, VOICE_EXT_MPA },
98 { "cms", FILE_ATTR_AUDIO, Icon_Audio, VOICE_EXT_MPA },
99 { "dmc", FILE_ATTR_AUDIO, Icon_Audio, VOICE_EXT_MPA },
100 { "dlt", FILE_ATTR_AUDIO, Icon_Audio, VOICE_EXT_MPA },
101 { "mpt", FILE_ATTR_AUDIO, Icon_Audio, VOICE_EXT_MPA },
102 { "mpd", FILE_ATTR_AUDIO, Icon_Audio, VOICE_EXT_MPA },
103 { "rmt", FILE_ATTR_AUDIO, Icon_Audio, VOICE_EXT_MPA },
104 { "tmc", FILE_ATTR_AUDIO, Icon_Audio, VOICE_EXT_MPA },
105 { "tm8", FILE_ATTR_AUDIO, Icon_Audio, VOICE_EXT_MPA },
106 { "tm2", FILE_ATTR_AUDIO, Icon_Audio, VOICE_EXT_MPA },
Mohamed Tarek23ac6ff2010-02-12 03:21:37 +0000107 { "oma", FILE_ATTR_AUDIO, Icon_Audio, VOICE_EXT_MPA },
108 { "aa3", FILE_ATTR_AUDIO, Icon_Audio, VOICE_EXT_MPA },
Mohamed Tarek82f05892010-02-16 03:34:39 +0000109 { "at3", FILE_ATTR_AUDIO, Icon_Audio, VOICE_EXT_MPA },
Yoshihisa Uchida45e009a2010-02-24 11:46:29 +0000110 { "mmf", FILE_ATTR_AUDIO, Icon_Audio, VOICE_EXT_MPA },
Yoshihisa Uchida4e3c8072010-02-28 07:22:20 +0000111 { "au", FILE_ATTR_AUDIO, Icon_Audio, VOICE_EXT_MPA },
112 { "snd", FILE_ATTR_AUDIO, Icon_Audio, VOICE_EXT_MPA },
Yoshihisa Uchida8c5eaa32010-02-28 08:13:30 +0000113 { "vox", FILE_ATTR_AUDIO, Icon_Audio, VOICE_EXT_MPA },
Yoshihisa Uchida561cb2c2010-02-28 08:48:07 +0000114 { "w64", FILE_ATTR_AUDIO, Icon_Audio, VOICE_EXT_MPA },
Yoshihisa Uchida0f5c6d42010-05-13 12:40:09 +0000115 { "tta", FILE_ATTR_AUDIO, Icon_Audio, VOICE_EXT_MPA },
Andree Buschmannacb09172011-08-07 20:01:04 +0000116 { "ay", FILE_ATTR_AUDIO, Icon_Audio, VOICE_EXT_MPA },
117 { "gbs", FILE_ATTR_AUDIO, Icon_Audio, VOICE_EXT_MPA },
118 { "hes", FILE_ATTR_AUDIO, Icon_Audio, VOICE_EXT_MPA },
119 { "sgc", FILE_ATTR_AUDIO, Icon_Audio, VOICE_EXT_MPA },
120 { "vgm", FILE_ATTR_AUDIO, Icon_Audio, VOICE_EXT_MPA },
121 { "vgz", FILE_ATTR_AUDIO, Icon_Audio, VOICE_EXT_MPA },
122 { "kss", FILE_ATTR_AUDIO, Icon_Audio, VOICE_EXT_MPA },
Jonathan Gordon36a2e302007-04-18 13:03:01 +0000123#endif
124 { "m3u", FILE_ATTR_M3U, Icon_Playlist, LANG_PLAYLIST },
125 { "m3u8",FILE_ATTR_M3U, Icon_Playlist, LANG_PLAYLIST },
126 { "cfg", FILE_ATTR_CFG, Icon_Config, VOICE_EXT_CFG },
127 { "wps", FILE_ATTR_WPS, Icon_Wps, VOICE_EXT_WPS },
128#ifdef HAVE_REMOTE_LCD
129 { "rwps",FILE_ATTR_RWPS, Icon_Wps, VOICE_EXT_RWPS },
130#endif
Jonathan Gordon36a2e302007-04-18 13:03:01 +0000131#if CONFIG_TUNER
132 { "fmr", FILE_ATTR_FMR, Icon_Preset, LANG_FMR },
Jonathan Gordon1bd072c2010-05-12 10:38:00 +0000133 { "fms", FILE_ATTR_FMS, Icon_Wps, VOICE_EXT_FMS },
Jonathan Gordon36a2e302007-04-18 13:03:01 +0000134#endif
135 { "lng", FILE_ATTR_LNG, Icon_Language, LANG_LANGUAGE },
136 { "rock",FILE_ATTR_ROCK,Icon_Plugin, VOICE_EXT_ROCK },
Maurus Cuelenaered789e412009-10-28 23:31:42 +0000137 { "lua", FILE_ATTR_LUA, Icon_Plugin, VOICE_EXT_ROCK },
Jonathan Gordon36a2e302007-04-18 13:03:01 +0000138#ifdef HAVE_LCD_BITMAP
139 { "fnt", FILE_ATTR_FONT,Icon_Font, VOICE_EXT_FONT },
140 { "kbd", FILE_ATTR_KBD, Icon_Keyboard, VOICE_EXT_KBD },
141#endif
142 { "bmark",FILE_ATTR_BMARK, Icon_Bookmark, VOICE_EXT_BMARK },
Nils Wallméniusb3113672007-08-05 19:19:39 +0000143 { "cue", FILE_ATTR_CUE, Icon_Bookmark, VOICE_EXT_CUESHEET },
Thomas Martitz1016ee42009-10-19 15:28:15 +0000144#ifdef HAVE_LCD_BITMAP
145 { "sbs", FILE_ATTR_SBS, Icon_Wps, VOICE_EXT_SBS },
146#endif
147#ifdef HAVE_REMOTE_LCD
148 { "rsbs", FILE_ATTR_RSBS, Icon_Wps, VOICE_EXT_RSBS },
Jonathan Gordon1bd072c2010-05-12 10:38:00 +0000149#if CONFIG_TUNER
150 { "rfms", FILE_ATTR_RFMS, Icon_Wps, VOICE_EXT_RFMS },
Teruaki Kawashima7d3f8c02010-11-09 14:35:19 +0000151#endif
Thomas Martitz1016ee42009-10-19 15:28:15 +0000152#endif
Jonathan Gordon36a2e302007-04-18 13:03:01 +0000153#ifdef BOOTFILE_EXT
154 { BOOTFILE_EXT, FILE_ATTR_MOD, Icon_Firmware, VOICE_EXT_AJZ },
Nils Wallménius01729e72008-08-15 08:27:39 +0000155#endif
Dave Chapman4fe2ee02009-10-10 11:46:23 +0000156#ifdef BOOTFILE_EXT2
157 { BOOTFILE_EXT2, FILE_ATTR_MOD, Icon_Firmware, VOICE_EXT_AJZ },
158#endif
Jonathan Gordon36a2e302007-04-18 13:03:01 +0000159};
160
161void tree_get_filetypes(const struct filetype** types, int* count)
162{
163 *types = inbuilt_filetypes;
164 *count = sizeof(inbuilt_filetypes) / sizeof(*inbuilt_filetypes);
165}
Björn Stenbergfb00c212004-05-21 20:08:24 +0000166
Jonathan Gordon21165a32007-03-29 06:16:00 +0000167#define ROCK_EXTENSION "rock"
Björn Stenbergfb00c212004-05-21 20:08:24 +0000168
Jonathan Gordon21165a32007-03-29 06:16:00 +0000169struct file_type {
Thomas Martitz7b374b42011-06-20 19:32:48 +0000170 enum themable_icons icon; /* the icon which shall be used for it, NOICON if unknown */
Maurus Cuelenaeredb8d79d2009-10-28 23:31:45 +0000171 unsigned char attr; /* FILE_ATTR_MASK >> 8 */
Jonathan Gordon21165a32007-03-29 06:16:00 +0000172 char* plugin; /* Which plugin to use, NULL if unknown, or builtin */
173 char* extension; /* NULL for none */
174};
Jonathan Gordon07a7d472007-08-12 09:53:41 +0000175static struct file_type filetypes[MAX_FILETYPES];
176static int custom_filetype_icons[MAX_FILETYPES];
Jonathan Gordona9c1df42007-04-16 14:33:29 +0000177static bool custom_icons_loaded = false;
Brandon Low74cbb0a2007-06-17 21:16:34 +0000178#ifdef HAVE_LCD_COLOR
Jonathan Gordon70595a22013-01-20 22:29:43 +1100179static int custom_colors[MAX_FILETYPES];
Brandon Low74cbb0a2007-06-17 21:16:34 +0000180#endif
Jonathan Gordon70595a22013-01-20 22:29:43 +1100181struct filetype_unknown {
Jonathan Gordon3b6b5132013-01-21 09:07:04 +1100182 int icon;
Jonathan Gordon70595a22013-01-20 22:29:43 +1100183#ifdef HAVE_LCD_COLOR
184 int color;
185#endif
186};
187static struct filetype_unknown unknown_file = {
188 .icon = Icon_NOICON,
189#ifdef HAVE_LCD_COLOR
190 .color = -1,
191#endif
192};
193
Teruaki Kawashima7d3f8c02010-11-09 14:35:19 +0000194/* index array to filetypes used in open with list. */
195static int viewers[MAX_VIEWERS];
Jonathan Gordon07a7d472007-08-12 09:53:41 +0000196static int filetype_count = 0;
Nils Wallménius11a667b2008-05-24 20:56:19 +0000197static unsigned char highest_attr = 0;
Teruaki Kawashima7d3f8c02010-11-09 14:35:19 +0000198static int viewer_count = 0;
Björn Stenbergfb00c212004-05-21 20:08:24 +0000199
Thomas Martitzd0b72e22011-08-30 14:01:33 +0000200static int strdup_handle, strdup_bufsize, strdup_cur_idx;
Thomas Martitzbaa070c2011-08-30 14:01:45 +0000201static int move_callback(int handle, void* current, void* new)
202{
203 /*could compare to strdup_handle, but ops is only used once */
204 (void)handle;
205 size_t diff = new - current;
206#define FIX_PTR(x) \
Jonathan Gordonccaf55a2011-09-21 13:32:37 +0000207 { if ((void*)x >= current && (void*)x < (current+strdup_bufsize)) x+= diff; }
Thomas Martitzbaa070c2011-08-30 14:01:45 +0000208 for(int i = 0; i < filetype_count; i++)
209 {
210 FIX_PTR(filetypes[i].extension);
211 FIX_PTR(filetypes[i].plugin);
212 }
213 return BUFLIB_CB_OK;
214}
215
216static struct buflib_callbacks ops = {
217 .move_callback = move_callback,
218 .shrink_callback = NULL,
219};
220
Jonathan Gordon21165a32007-03-29 06:16:00 +0000221static char *filetypes_strdup(char* string)
Björn Stenbergfb00c212004-05-21 20:08:24 +0000222{
Thomas Martitzd0b72e22011-08-30 14:01:33 +0000223 char *buffer = core_get_data(strdup_handle) + strdup_cur_idx;
224 strdup_cur_idx += strlcpy(buffer, string, strdup_bufsize-strdup_cur_idx)+1;
Jonathan Gordon21165a32007-03-29 06:16:00 +0000225 return buffer;
226}
Thomas Martitzd0b72e22011-08-30 14:01:33 +0000227
Teruaki Kawashima7d3f8c02010-11-09 14:35:19 +0000228static char *filetypes_store_plugin(char *plugin, int n)
229{
230 int i;
231 /* if the plugin is in the list already, use it. */
232 for (i=0; i<viewer_count; i++)
233 {
234 if (!strcmp(filetypes[viewers[i]].plugin, plugin))
235 return filetypes[viewers[i]].plugin;
236 }
237 /* otherwise, allocate buffer */
238 if (viewer_count < MAX_VIEWERS)
239 viewers[viewer_count++] = n;
240 return filetypes_strdup(plugin);
241}
Teruaki Kawashima194bc862010-11-27 12:57:19 +0000242
243static int find_extension(const char* extension)
244{
245 int i;
246 if (!extension)
247 return -1;
248 for (i=1; i<filetype_count; i++)
249 {
250 if (filetypes[i].extension &&
251 !strcasecmp(extension, filetypes[i].extension))
252 return i;
253 }
254 return -1;
255}
256
Jonathan Gordon21165a32007-03-29 06:16:00 +0000257static void read_builtin_types(void);
Thomas Martitzd0b72e22011-08-30 14:01:33 +0000258static void read_config(int fd);
Brandon Low74cbb0a2007-06-17 21:16:34 +0000259#ifdef HAVE_LCD_COLOR
260/* Colors file format is similar to icons:
261 * ext:hex_color
262 * load a colors file from a theme with:
Paul Loudenf8b59132007-06-19 04:21:06 +0000263 * filetype colours: filename.colours */
Brandon Low74cbb0a2007-06-17 21:16:34 +0000264void read_color_theme_file(void) {
Thomas Martitz2c241602010-12-06 22:26:31 +0000265 char buffer[MAX_PATH];
Brandon Low74cbb0a2007-06-17 21:16:34 +0000266 int fd;
267 char *ext, *color;
268 int i;
Jonathan Gordon70595a22013-01-20 22:29:43 +1100269 for (i = 0; i < MAX_FILETYPES; i++) {
Brandon Low74cbb0a2007-06-17 21:16:34 +0000270 custom_colors[i] = -1;
271 }
Thomas Martitz2c241602010-12-06 22:26:31 +0000272 snprintf(buffer, MAX_PATH, THEME_DIR "/%s.colours",
Brandon Low74cbb0a2007-06-17 21:16:34 +0000273 global_settings.colors_file);
274 fd = open(buffer, O_RDONLY);
275 if (fd < 0)
276 return;
277 while (read_line(fd, buffer, MAX_PATH) > 0)
278 {
279 if (!settings_parseline(buffer, &ext, &color))
280 continue;
Brandon Lowf8682a02007-06-17 23:10:01 +0000281 if (!strcasecmp(ext, "folder"))
282 {
Dave Chapmane92d2c52008-03-21 13:41:35 +0000283 hex_to_rgb(color, &custom_colors[0]);
Brandon Lowf8682a02007-06-17 23:10:01 +0000284 continue;
285 }
Paul Louden5b25a6e2007-06-18 22:41:39 +0000286 if (!strcasecmp(ext, "???"))
287 {
Jonathan Gordon70595a22013-01-20 22:29:43 +1100288 hex_to_rgb(color, &unknown_file.color);
Paul Louden5b25a6e2007-06-18 22:41:39 +0000289 continue;
290 }
Teruaki Kawashima194bc862010-11-27 12:57:19 +0000291 i = find_extension(ext);
292 if (i >= 0)
293 hex_to_rgb(color, &custom_colors[i]);
Brandon Low74cbb0a2007-06-17 21:16:34 +0000294 }
295 close(fd);
296}
297#endif
Jonathan Gordon6a5cc0b2007-04-16 09:14:36 +0000298#ifdef HAVE_LCD_BITMAP
299void read_viewer_theme_file(void)
300{
301 char buffer[MAX_PATH];
302 int fd;
303 char *ext, *icon;
304 int i;
Jonathan Gordon70595a22013-01-20 22:29:43 +1100305 int *icon_dest;
Jonathan Gordon5b5d1012007-04-26 05:30:46 +0000306 global_status.viewer_icon_count = 0;
Jonathan Gordona9c1df42007-04-16 14:33:29 +0000307 custom_icons_loaded = false;
Jonathan Gordonfd951ab2007-04-16 14:52:47 +0000308 custom_filetype_icons[0] = Icon_Folder;
309 for (i=1; i<filetype_count; i++)
Jonathan Gordon27643c62007-04-17 09:17:23 +0000310 {
Jonathan Gordon9d3694c2007-04-22 08:50:24 +0000311 custom_filetype_icons[i] = filetypes[i].icon;
Jonathan Gordon27643c62007-04-17 09:17:23 +0000312 }
Jonathan Gordona9c1df42007-04-16 14:33:29 +0000313
Jonathan Gordon6a5cc0b2007-04-16 09:14:36 +0000314 snprintf(buffer, MAX_PATH, "%s/%s.icons", ICON_DIR,
315 global_settings.viewers_icon_file);
316 fd = open(buffer, O_RDONLY);
317 if (fd < 0)
318 return;
319 while (read_line(fd, buffer, MAX_PATH) > 0)
320 {
321 if (!settings_parseline(buffer, &ext, &icon))
322 continue;
Teruaki Kawashima194bc862010-11-27 12:57:19 +0000323 i = find_extension(ext);
324 if (i >= 0)
Jonathan Gordon70595a22013-01-20 22:29:43 +1100325 icon_dest = &custom_filetype_icons[i];
326 else if (!strcmp(ext, "???"))
327 icon_dest = &unknown_file.icon;
328 else
329 icon_dest = NULL;
330
331 if (icon_dest)
Jonathan Gordon6a5cc0b2007-04-16 09:14:36 +0000332 {
Teruaki Kawashima194bc862010-11-27 12:57:19 +0000333 if (*icon == '*')
Jonathan Gordon70595a22013-01-20 22:29:43 +1100334 *icon_dest = atoi(icon+1);
Teruaki Kawashima194bc862010-11-27 12:57:19 +0000335 else if (*icon == '-')
Jonathan Gordon70595a22013-01-20 22:29:43 +1100336 *icon_dest = Icon_NOICON;
Teruaki Kawashima194bc862010-11-27 12:57:19 +0000337 else if (*icon >= '0' && *icon <= '9')
Jonathan Gordon6a5cc0b2007-04-16 09:14:36 +0000338 {
Teruaki Kawashima194bc862010-11-27 12:57:19 +0000339 int number = atoi(icon);
340 if (number > global_status.viewer_icon_count)
341 global_status.viewer_icon_count++;
Jonathan Gordon70595a22013-01-20 22:29:43 +1100342 *icon_dest = Icon_Last_Themeable + number;
Jonathan Gordon6a5cc0b2007-04-16 09:14:36 +0000343 }
344 }
345 }
346 close(fd);
Jonathan Gordona9c1df42007-04-16 14:33:29 +0000347 custom_icons_loaded = true;
Jonathan Gordon6a5cc0b2007-04-16 09:14:36 +0000348}
349#endif
Björn Stenbergfb00c212004-05-21 20:08:24 +0000350
Jonathan Gordon21165a32007-03-29 06:16:00 +0000351void filetype_init(void)
352{
353 /* set the directory item first */
354 filetypes[0].extension = NULL;
355 filetypes[0].plugin = NULL;
356 filetypes[0].attr = 0;
Jonathan Gordon6a5cc0b2007-04-16 09:14:36 +0000357 filetypes[0].icon = Icon_Folder;
Teruaki Kawashima7d3f8c02010-11-09 14:35:19 +0000358
Thomas Martitzd0b72e22011-08-30 14:01:33 +0000359 /* estimate bufsize with the filesize, will not be larger */
Teruaki Kawashima7d3f8c02010-11-09 14:35:19 +0000360 viewer_count = 0;
Jonathan Gordon21165a32007-03-29 06:16:00 +0000361 filetype_count = 1;
Thomas Martitzd0b72e22011-08-30 14:01:33 +0000362
363 int fd = open(VIEWERS_CONFIG, O_RDONLY);
364 if (fd < 0)
365 return;
366
367 strdup_bufsize = filesize(fd);
Thomas Martitzbaa070c2011-08-30 14:01:45 +0000368 strdup_handle = core_alloc_ex("filetypes", strdup_bufsize, &ops);
Bertrik Sikkend023bf02012-06-10 17:15:47 +0200369 if (strdup_handle <= 0)
Thomas Jarosch96afbb32011-10-02 20:04:44 +0000370 {
371 close(fd);
Thomas Martitzd0b72e22011-08-30 14:01:33 +0000372 return;
Thomas Jarosch96afbb32011-10-02 20:04:44 +0000373 }
Jonathan Gordon21165a32007-03-29 06:16:00 +0000374 read_builtin_types();
Thomas Martitzd0b72e22011-08-30 14:01:33 +0000375 read_config(fd);
Thomas Jarosch96afbb32011-10-02 20:04:44 +0000376 close(fd);
Jonathan Gordon6a5cc0b2007-04-16 09:14:36 +0000377#ifdef HAVE_LCD_BITMAP
378 read_viewer_theme_file();
379#endif
Brandon Low74cbb0a2007-06-17 21:16:34 +0000380#ifdef HAVE_LCD_COLOR
381 read_color_theme_file();
382#endif
Thomas Martitzd0b72e22011-08-30 14:01:33 +0000383 core_shrink(strdup_handle, core_get_data(strdup_handle), strdup_cur_idx);
Björn Stenbergfb00c212004-05-21 20:08:24 +0000384}
385
Jonathan Gordon21165a32007-03-29 06:16:00 +0000386/* remove all white spaces from string */
387static void rm_whitespaces(char* str)
Björn Stenbergfb00c212004-05-21 20:08:24 +0000388{
Jonathan Gordon21165a32007-03-29 06:16:00 +0000389 char *s = str;
390 while (*str)
Björn Stenbergfb00c212004-05-21 20:08:24 +0000391 {
Jonathan Gordon21165a32007-03-29 06:16:00 +0000392 if (!isspace(*str))
Björn Stenbergfb00c212004-05-21 20:08:24 +0000393 {
Jonathan Gordon21165a32007-03-29 06:16:00 +0000394 *s = *str;
395 s++;
396 }
397 str++;
398 }
399 *s = '\0';
400}
401
402static void read_builtin_types(void)
403{
Jonathan Gordon36a2e302007-04-18 13:03:01 +0000404 int count = sizeof(inbuilt_filetypes)/sizeof(*inbuilt_filetypes), i;
Jonathan Gordon07a7d472007-08-12 09:53:41 +0000405 for(i=0; i<count && (filetype_count < MAX_FILETYPES); i++)
Jonathan Gordon21165a32007-03-29 06:16:00 +0000406 {
Jonathan Gordon36a2e302007-04-18 13:03:01 +0000407 filetypes[filetype_count].extension = inbuilt_filetypes[i].extension;
Jonathan Gordon21165a32007-03-29 06:16:00 +0000408 filetypes[filetype_count].plugin = NULL;
Jonathan Gordon07a7d472007-08-12 09:53:41 +0000409 filetypes[filetype_count].attr = inbuilt_filetypes[i].tree_attr>>8;
Nils Wallménius11a667b2008-05-24 20:56:19 +0000410 if (filetypes[filetype_count].attr > highest_attr)
411 highest_attr = filetypes[filetype_count].attr;
Jonathan Gordon07a7d472007-08-12 09:53:41 +0000412 filetypes[filetype_count].icon = inbuilt_filetypes[i].icon;
Jonathan Gordon21165a32007-03-29 06:16:00 +0000413 filetype_count++;
414 }
415}
416
Thomas Martitzd0b72e22011-08-30 14:01:33 +0000417static void read_config(int fd)
Jonathan Gordon21165a32007-03-29 06:16:00 +0000418{
419 char line[64], *s, *e;
Teruaki Kawashima7d3f8c02010-11-09 14:35:19 +0000420 char *extension, *plugin;
Teruaki Kawashima7d3f8c02010-11-09 14:35:19 +0000421 /* config file is in the format
Jonathan Gordon21165a32007-03-29 06:16:00 +0000422 <extension>,<plugin>,<icon code>
423 ignore line if either of the first two are missing */
Teruaki Kawashima7d3f8c02010-11-09 14:35:19 +0000424 while (read_line(fd, line, sizeof line) > 0)
Jonathan Gordon21165a32007-03-29 06:16:00 +0000425 {
Jonathan Gordon07a7d472007-08-12 09:53:41 +0000426 if (filetype_count >= MAX_FILETYPES)
Jonathan Gordon21165a32007-03-29 06:16:00 +0000427 {
Nils Wallménius01729e72008-08-15 08:27:39 +0000428 splash(HZ, ID2P(LANG_FILETYPES_FULL));
Jonathan Gordon21165a32007-03-29 06:16:00 +0000429 break;
430 }
431 rm_whitespaces(line);
Teruaki Kawashima194bc862010-11-27 12:57:19 +0000432 /* get the extension */
Jonathan Gordon21165a32007-03-29 06:16:00 +0000433 s = line;
434 e = strchr(s, ',');
435 if (!e)
436 continue;
437 *e = '\0';
Teruaki Kawashima7d3f8c02010-11-09 14:35:19 +0000438 extension = s;
439
Jonathan Gordon21165a32007-03-29 06:16:00 +0000440 /* get the plugin */
441 s = e+1;
Jonathan Gordon21165a32007-03-29 06:16:00 +0000442 e = strchr(s, ',');
443 if (!e)
444 continue;
445 *e = '\0';
Teruaki Kawashima7d3f8c02010-11-09 14:35:19 +0000446 plugin = s;
447
Jonathan Gordon70595a22013-01-20 22:29:43 +1100448 if (!strcmp("???", extension))
449 {
450 /* get the icon */
451 s = e+1;
452 if (*s == '*')
453 unknown_file.icon = atoi(s+1);
454 else if (*s == '-')
455 unknown_file.icon = Icon_NOICON;
456 else if (*s >= '0' && *s <= '9')
457 unknown_file.icon = Icon_Last_Themeable + atoi(s);
458 continue;
459 }
460
Jonathan Gordon21165a32007-03-29 06:16:00 +0000461 /* ok, store this plugin/extension, check icon after */
Teruaki Kawashima7d3f8c02010-11-09 14:35:19 +0000462 struct file_type *file_type = &filetypes[filetype_count];
463 file_type->extension = filetypes_strdup(extension);
464 file_type->plugin = filetypes_store_plugin(plugin, filetype_count);
465 file_type->attr = highest_attr +1;
466 file_type->icon = Icon_Questionmark;
Nils Wallménius11a667b2008-05-24 20:56:19 +0000467 highest_attr++;
Jonathan Gordon21165a32007-03-29 06:16:00 +0000468 /* get the icon */
Jonathan Gordon21165a32007-03-29 06:16:00 +0000469 s = e+1;
Jonathan Gordon6a5cc0b2007-04-16 09:14:36 +0000470 if (*s == '*')
Teruaki Kawashima7d3f8c02010-11-09 14:35:19 +0000471 file_type->icon = atoi(s+1);
Jonathan Gordon6a5cc0b2007-04-16 09:14:36 +0000472 else if (*s == '-')
Teruaki Kawashima7d3f8c02010-11-09 14:35:19 +0000473 file_type->icon = Icon_NOICON;
Jonathan Gordon4a167392007-04-16 12:26:49 +0000474 else if (*s >= '0' && *s <= '9')
Teruaki Kawashima7d3f8c02010-11-09 14:35:19 +0000475 file_type->icon = Icon_Last_Themeable + atoi(s);
Jonathan Gordon21165a32007-03-29 06:16:00 +0000476 filetype_count++;
Björn Stenbergfb00c212004-05-21 20:08:24 +0000477 }
Jonathan Gordon21165a32007-03-29 06:16:00 +0000478}
Björn Stenbergfb00c212004-05-21 20:08:24 +0000479
Jonathan Gordon21165a32007-03-29 06:16:00 +0000480int filetype_get_attr(const char* file)
481{
482 char *extension = strrchr(file, '.');
483 int i;
484 if (!extension)
485 return 0;
486 extension++;
Teruaki Kawashima194bc862010-11-27 12:57:19 +0000487
488 i = find_extension(extension);
489 if (i >= 0)
490 return (filetypes[i].attr<<8)&FILE_ATTR_MASK;
Björn Stenbergfb00c212004-05-21 20:08:24 +0000491 return 0;
492}
493
Jonathan Gordon21165a32007-03-29 06:16:00 +0000494static int find_attr(int attr)
Björn Stenbergfb00c212004-05-21 20:08:24 +0000495{
496 int i;
Jonathan Gordon21165a32007-03-29 06:16:00 +0000497 /* skip the directory item */
498 if ((attr & ATTR_DIRECTORY)==ATTR_DIRECTORY)
499 return 0;
500 for (i=1; i<filetype_count; i++)
501 {
502 if ((attr>>8) == filetypes[i].attr)
503 return i;
504 }
505 return -1;
506}
Björn Stenbergfb00c212004-05-21 20:08:24 +0000507
Brandon Low74cbb0a2007-06-17 21:16:34 +0000508#ifdef HAVE_LCD_COLOR
Brandon Lowf8682a02007-06-17 23:10:01 +0000509int filetype_get_color(const char * name, int attr)
Brandon Low74cbb0a2007-06-17 21:16:34 +0000510{
Brandon Lowf8682a02007-06-17 23:10:01 +0000511 char *extension;
512 int i;
513 if ((attr & ATTR_DIRECTORY)==ATTR_DIRECTORY)
514 return custom_colors[0];
515 extension = strrchr(name, '.');
516 if (!extension)
Boris Gjenero15fa7f82013-06-05 18:35:09 -0400517 return unknown_file.color;
Brandon Lowf8682a02007-06-17 23:10:01 +0000518 extension++;
Teruaki Kawashima194bc862010-11-27 12:57:19 +0000519
520 i = find_extension(extension);
521 if (i >= 0)
522 return custom_colors[i];
Jonathan Gordon70595a22013-01-20 22:29:43 +1100523 return unknown_file.color;
Brandon Low74cbb0a2007-06-17 21:16:34 +0000524}
525#endif
526
Jonathan Gordon6a5cc0b2007-04-16 09:14:36 +0000527int filetype_get_icon(int attr)
Jonathan Gordon21165a32007-03-29 06:16:00 +0000528{
529 int index = find_attr(attr);
530 if (index < 0)
Jonathan Gordon70595a22013-01-20 22:29:43 +1100531 return unknown_file.icon;
Jonathan Gordona9c1df42007-04-16 14:33:29 +0000532 if (custom_icons_loaded)
533 return custom_filetype_icons[index];
Jonathan Gordon6a5cc0b2007-04-16 09:14:36 +0000534 return filetypes[index].icon;
Jonathan Gordon21165a32007-03-29 06:16:00 +0000535}
536
537char* filetype_get_plugin(const struct entry* file)
538{
539 static char plugin_name[MAX_PATH];
540 int index = find_attr(file->attr);
541 if (index < 0)
542 return NULL;
Jonathan Gordon31241342007-04-16 11:10:41 +0000543 if (filetypes[index].plugin == NULL)
Jonathan Gordonccaf24a2007-04-16 11:01:41 +0000544 return NULL;
Jonathan Gordon21165a32007-03-29 06:16:00 +0000545 snprintf(plugin_name, MAX_PATH, "%s/%s.%s",
Jonathan Gordonfda7d722007-08-06 13:42:52 +0000546 PLUGIN_DIR, filetypes[index].plugin, ROCK_EXTENSION);
Jonathan Gordon21165a32007-03-29 06:16:00 +0000547 return plugin_name;
548}
549
Teruaki Kawashima7d3f8c02010-11-09 14:35:19 +0000550bool filetype_supported(int attr)
Jonathan Gordon21165a32007-03-29 06:16:00 +0000551{
552 return find_attr(attr) >= 0;
553}
554
Jonathan Gordon73160322007-08-07 07:55:18 +0000555/**** Open With Screen ****/
Jonathan Gordon5ca8abf2007-10-21 00:11:47 +0000556struct cb_data {
Nils Wallménius11a667b2008-05-24 20:56:19 +0000557 const char *current_file;
Jonathan Gordon5ca8abf2007-10-21 00:11:47 +0000558};
Nils Wallménius68489612008-04-09 15:25:17 +0000559
560static enum themable_icons openwith_get_icon(int selected_item, void * data)
Jonathan Gordon73160322007-08-07 07:55:18 +0000561{
Teruaki Kawashima7d3f8c02010-11-09 14:35:19 +0000562 (void)data;
563 return filetypes[viewers[selected_item]].icon;
Jonathan Gordon73160322007-08-07 07:55:18 +0000564}
Nils Wallménius68489612008-04-09 15:25:17 +0000565
Nils Wallménius3200d042009-08-20 16:47:44 +0000566static const char* openwith_get_name(int selected_item, void * data,
567 char * buffer, size_t buffer_len)
Jonathan Gordon73160322007-08-07 07:55:18 +0000568{
Teruaki Kawashima7d3f8c02010-11-09 14:35:19 +0000569 (void)data; (void)buffer; (void)buffer_len;
570 const char *s = strrchr(filetypes[viewers[selected_item]].plugin, '/');
Jonathan Gordon73160322007-08-07 07:55:18 +0000571 if (s)
572 return s+1;
Teruaki Kawashima7d3f8c02010-11-09 14:35:19 +0000573 else return filetypes[viewers[selected_item]].plugin;
Jonathan Gordon73160322007-08-07 07:55:18 +0000574}
Nils Wallménius68489612008-04-09 15:25:17 +0000575
576static int openwith_action_callback(int action, struct gui_synclist *lists)
Jonathan Gordon5ca8abf2007-10-21 00:11:47 +0000577{
Jonathan Gordonf5ec3e42007-12-17 02:37:21 +0000578 struct cb_data *info = (struct cb_data *)lists->data;
Jonathan Gordon5ca8abf2007-10-21 00:11:47 +0000579 int i;
580 if (action == ACTION_STD_OK)
581 {
582 char plugin[MAX_PATH];
Teruaki Kawashima7d3f8c02010-11-09 14:35:19 +0000583 i = viewers[gui_synclist_get_sel_pos(lists)];
Jonathan Gordon5ca8abf2007-10-21 00:11:47 +0000584 snprintf(plugin, MAX_PATH, "%s/%s.%s",
585 PLUGIN_DIR, filetypes[i].plugin, ROCK_EXTENSION);
586 plugin_load(plugin, info->current_file);
587 return ACTION_STD_CANCEL;
588 }
589 return action;
590}
Nils Wallménius68489612008-04-09 15:25:17 +0000591
Jonathan Gordon21165a32007-03-29 06:16:00 +0000592int filetype_list_viewers(const char* current_file)
593{
Jonathan Gordon5ca8abf2007-10-21 00:11:47 +0000594 struct simplelist_info info;
Teruaki Kawashima7d3f8c02010-11-09 14:35:19 +0000595 struct cb_data data = { current_file };
Jonathan Gordon21165a32007-03-29 06:16:00 +0000596#ifndef HAVE_LCD_BITMAP
Teruaki Kawashima7d3f8c02010-11-09 14:35:19 +0000597 if (viewer_count == 0)
Jonathan Gordon21165a32007-03-29 06:16:00 +0000598 {
599 /* FIX: translation! */
Nils Wallménius01729e72008-08-15 08:27:39 +0000600 splash(HZ*2, "No viewers found");
Jonathan Gordon21165a32007-03-29 06:16:00 +0000601 return PLUGIN_OK;
602 }
603#endif
Teruaki Kawashima7d3f8c02010-11-09 14:35:19 +0000604 simplelist_info_init(&info, str(LANG_ONPLAY_OPEN_WITH), viewer_count, &data);
Jonathan Gordon5ca8abf2007-10-21 00:11:47 +0000605 info.action_callback = openwith_action_callback;
606 info.get_name = openwith_get_name;
Jonathan Gordone4253712009-12-16 08:36:46 +0000607 info.get_icon = global_settings.show_icons?openwith_get_icon:NULL;
Jonathan Gordon5ca8abf2007-10-21 00:11:47 +0000608 return simplelist_show_list(&info);
Björn Stenbergfb00c212004-05-21 20:08:24 +0000609}
610
Nils Wallméniusa0377bd2011-12-05 19:23:46 +0000611int filetype_load_plugin(const char* plugin, const char* file)
Björn Stenbergfb00c212004-05-21 20:08:24 +0000612{
Jonathan Gordon73160322007-08-07 07:55:18 +0000613 int i;
Jonathan Gordon21165a32007-03-29 06:16:00 +0000614 char plugin_name[MAX_PATH];
Jonathan Gordon73160322007-08-07 07:55:18 +0000615 char *s;
Teruaki Kawashima7d3f8c02010-11-09 14:35:19 +0000616
Jonathan Gordon73160322007-08-07 07:55:18 +0000617 for (i=0;i<filetype_count;i++)
Linus Nielsen Feltzing25efd332006-03-13 07:33:30 +0000618 {
Jonathan Gordon73160322007-08-07 07:55:18 +0000619 if (filetypes[i].plugin)
Linus Nielsen Feltzing25efd332006-03-13 07:33:30 +0000620 {
Jonathan Gordon73160322007-08-07 07:55:18 +0000621 s = strrchr(filetypes[i].plugin, '/');
622 if (s)
623 {
624 if (!strcmp(s+1, plugin))
625 break;
626 }
627 else if (!strcmp(filetypes[i].plugin, plugin))
628 break;
Linus Nielsen Feltzing25efd332006-03-13 07:33:30 +0000629 }
630 }
Jonathan Gordon73160322007-08-07 07:55:18 +0000631 if (i >= filetype_count)
632 return PLUGIN_ERROR;
633 snprintf(plugin_name, MAX_PATH, "%s/%s.%s",
634 PLUGIN_DIR, filetypes[i].plugin, ROCK_EXTENSION);
Steve Bavinad95df22008-05-12 17:52:50 +0000635 return plugin_load(plugin_name, file);
Björn Stenbergfb00c212004-05-21 20:08:24 +0000636}