blob: 16a00a423f45ba3b6dedf5f3b6e668c50c557ffd [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 },
Solomon Peachy928557b2018-12-22 20:01:42 -0500123 { "aac", FILE_ATTR_AUDIO, Icon_Audio, VOICE_EXT_MPA },
Jonathan Gordon36a2e302007-04-18 13:03:01 +0000124#endif
125 { "m3u", FILE_ATTR_M3U, Icon_Playlist, LANG_PLAYLIST },
126 { "m3u8",FILE_ATTR_M3U, Icon_Playlist, LANG_PLAYLIST },
127 { "cfg", FILE_ATTR_CFG, Icon_Config, VOICE_EXT_CFG },
128 { "wps", FILE_ATTR_WPS, Icon_Wps, VOICE_EXT_WPS },
129#ifdef HAVE_REMOTE_LCD
130 { "rwps",FILE_ATTR_RWPS, Icon_Wps, VOICE_EXT_RWPS },
131#endif
Jonathan Gordon36a2e302007-04-18 13:03:01 +0000132#if CONFIG_TUNER
133 { "fmr", FILE_ATTR_FMR, Icon_Preset, LANG_FMR },
Jonathan Gordon1bd072c2010-05-12 10:38:00 +0000134 { "fms", FILE_ATTR_FMS, Icon_Wps, VOICE_EXT_FMS },
Jonathan Gordon36a2e302007-04-18 13:03:01 +0000135#endif
136 { "lng", FILE_ATTR_LNG, Icon_Language, LANG_LANGUAGE },
137 { "rock",FILE_ATTR_ROCK,Icon_Plugin, VOICE_EXT_ROCK },
Maurus Cuelenaered789e412009-10-28 23:31:42 +0000138 { "lua", FILE_ATTR_LUA, Icon_Plugin, VOICE_EXT_ROCK },
Jonathan Gordon36a2e302007-04-18 13:03:01 +0000139#ifdef HAVE_LCD_BITMAP
140 { "fnt", FILE_ATTR_FONT,Icon_Font, VOICE_EXT_FONT },
141 { "kbd", FILE_ATTR_KBD, Icon_Keyboard, VOICE_EXT_KBD },
142#endif
143 { "bmark",FILE_ATTR_BMARK, Icon_Bookmark, VOICE_EXT_BMARK },
Nils Wallméniusb3113672007-08-05 19:19:39 +0000144 { "cue", FILE_ATTR_CUE, Icon_Bookmark, VOICE_EXT_CUESHEET },
Thomas Martitz1016ee42009-10-19 15:28:15 +0000145#ifdef HAVE_LCD_BITMAP
146 { "sbs", FILE_ATTR_SBS, Icon_Wps, VOICE_EXT_SBS },
147#endif
148#ifdef HAVE_REMOTE_LCD
149 { "rsbs", FILE_ATTR_RSBS, Icon_Wps, VOICE_EXT_RSBS },
Jonathan Gordon1bd072c2010-05-12 10:38:00 +0000150#if CONFIG_TUNER
151 { "rfms", FILE_ATTR_RFMS, Icon_Wps, VOICE_EXT_RFMS },
Teruaki Kawashima7d3f8c02010-11-09 14:35:19 +0000152#endif
Thomas Martitz1016ee42009-10-19 15:28:15 +0000153#endif
Jonathan Gordon36a2e302007-04-18 13:03:01 +0000154#ifdef BOOTFILE_EXT
155 { BOOTFILE_EXT, FILE_ATTR_MOD, Icon_Firmware, VOICE_EXT_AJZ },
Nils Wallménius01729e72008-08-15 08:27:39 +0000156#endif
Dave Chapman4fe2ee02009-10-10 11:46:23 +0000157#ifdef BOOTFILE_EXT2
158 { BOOTFILE_EXT2, FILE_ATTR_MOD, Icon_Firmware, VOICE_EXT_AJZ },
159#endif
Jonathan Gordon36a2e302007-04-18 13:03:01 +0000160};
161
162void tree_get_filetypes(const struct filetype** types, int* count)
163{
164 *types = inbuilt_filetypes;
165 *count = sizeof(inbuilt_filetypes) / sizeof(*inbuilt_filetypes);
166}
Björn Stenbergfb00c212004-05-21 20:08:24 +0000167
Jonathan Gordon21165a32007-03-29 06:16:00 +0000168#define ROCK_EXTENSION "rock"
Björn Stenbergfb00c212004-05-21 20:08:24 +0000169
Jonathan Gordon21165a32007-03-29 06:16:00 +0000170struct file_type {
Thomas Martitz7b374b42011-06-20 19:32:48 +0000171 enum themable_icons icon; /* the icon which shall be used for it, NOICON if unknown */
Maurus Cuelenaeredb8d79d2009-10-28 23:31:45 +0000172 unsigned char attr; /* FILE_ATTR_MASK >> 8 */
Jonathan Gordon21165a32007-03-29 06:16:00 +0000173 char* plugin; /* Which plugin to use, NULL if unknown, or builtin */
174 char* extension; /* NULL for none */
175};
Jonathan Gordon07a7d472007-08-12 09:53:41 +0000176static struct file_type filetypes[MAX_FILETYPES];
177static int custom_filetype_icons[MAX_FILETYPES];
Jonathan Gordona9c1df42007-04-16 14:33:29 +0000178static bool custom_icons_loaded = false;
Brandon Low74cbb0a2007-06-17 21:16:34 +0000179#ifdef HAVE_LCD_COLOR
Jonathan Gordon70595a22013-01-20 22:29:43 +1100180static int custom_colors[MAX_FILETYPES];
Brandon Low74cbb0a2007-06-17 21:16:34 +0000181#endif
Jonathan Gordon70595a22013-01-20 22:29:43 +1100182struct filetype_unknown {
Jonathan Gordon3b6b5132013-01-21 09:07:04 +1100183 int icon;
Jonathan Gordon70595a22013-01-20 22:29:43 +1100184#ifdef HAVE_LCD_COLOR
185 int color;
186#endif
187};
188static struct filetype_unknown unknown_file = {
189 .icon = Icon_NOICON,
190#ifdef HAVE_LCD_COLOR
191 .color = -1,
192#endif
193};
194
Teruaki Kawashima7d3f8c02010-11-09 14:35:19 +0000195/* index array to filetypes used in open with list. */
196static int viewers[MAX_VIEWERS];
Jonathan Gordon07a7d472007-08-12 09:53:41 +0000197static int filetype_count = 0;
Nils Wallménius11a667b2008-05-24 20:56:19 +0000198static unsigned char highest_attr = 0;
Teruaki Kawashima7d3f8c02010-11-09 14:35:19 +0000199static int viewer_count = 0;
Björn Stenbergfb00c212004-05-21 20:08:24 +0000200
Thomas Martitzd0b72e22011-08-30 14:01:33 +0000201static int strdup_handle, strdup_bufsize, strdup_cur_idx;
Thomas Martitzbaa070c2011-08-30 14:01:45 +0000202static int move_callback(int handle, void* current, void* new)
203{
204 /*could compare to strdup_handle, but ops is only used once */
205 (void)handle;
206 size_t diff = new - current;
207#define FIX_PTR(x) \
Jonathan Gordonccaf55a2011-09-21 13:32:37 +0000208 { if ((void*)x >= current && (void*)x < (current+strdup_bufsize)) x+= diff; }
Thomas Martitzbaa070c2011-08-30 14:01:45 +0000209 for(int i = 0; i < filetype_count; i++)
210 {
211 FIX_PTR(filetypes[i].extension);
212 FIX_PTR(filetypes[i].plugin);
213 }
214 return BUFLIB_CB_OK;
215}
216
217static struct buflib_callbacks ops = {
218 .move_callback = move_callback,
219 .shrink_callback = NULL,
220};
221
Jonathan Gordon21165a32007-03-29 06:16:00 +0000222static char *filetypes_strdup(char* string)
Björn Stenbergfb00c212004-05-21 20:08:24 +0000223{
Thomas Martitzd0b72e22011-08-30 14:01:33 +0000224 char *buffer = core_get_data(strdup_handle) + strdup_cur_idx;
225 strdup_cur_idx += strlcpy(buffer, string, strdup_bufsize-strdup_cur_idx)+1;
Jonathan Gordon21165a32007-03-29 06:16:00 +0000226 return buffer;
227}
Thomas Martitzd0b72e22011-08-30 14:01:33 +0000228
Teruaki Kawashima7d3f8c02010-11-09 14:35:19 +0000229static char *filetypes_store_plugin(char *plugin, int n)
230{
231 int i;
232 /* if the plugin is in the list already, use it. */
233 for (i=0; i<viewer_count; i++)
234 {
235 if (!strcmp(filetypes[viewers[i]].plugin, plugin))
236 return filetypes[viewers[i]].plugin;
237 }
238 /* otherwise, allocate buffer */
239 if (viewer_count < MAX_VIEWERS)
240 viewers[viewer_count++] = n;
241 return filetypes_strdup(plugin);
242}
Teruaki Kawashima194bc862010-11-27 12:57:19 +0000243
244static int find_extension(const char* extension)
245{
246 int i;
247 if (!extension)
248 return -1;
249 for (i=1; i<filetype_count; i++)
250 {
251 if (filetypes[i].extension &&
252 !strcasecmp(extension, filetypes[i].extension))
253 return i;
254 }
255 return -1;
256}
257
Jonathan Gordon21165a32007-03-29 06:16:00 +0000258static void read_builtin_types(void);
Thomas Martitzd0b72e22011-08-30 14:01:33 +0000259static void read_config(int fd);
Brandon Low74cbb0a2007-06-17 21:16:34 +0000260#ifdef HAVE_LCD_COLOR
261/* Colors file format is similar to icons:
262 * ext:hex_color
263 * load a colors file from a theme with:
Paul Loudenf8b59132007-06-19 04:21:06 +0000264 * filetype colours: filename.colours */
Brandon Low74cbb0a2007-06-17 21:16:34 +0000265void read_color_theme_file(void) {
Thomas Martitz2c241602010-12-06 22:26:31 +0000266 char buffer[MAX_PATH];
Brandon Low74cbb0a2007-06-17 21:16:34 +0000267 int fd;
268 char *ext, *color;
269 int i;
Jonathan Gordon70595a22013-01-20 22:29:43 +1100270 for (i = 0; i < MAX_FILETYPES; i++) {
Brandon Low74cbb0a2007-06-17 21:16:34 +0000271 custom_colors[i] = -1;
272 }
Thomas Martitz2c241602010-12-06 22:26:31 +0000273 snprintf(buffer, MAX_PATH, THEME_DIR "/%s.colours",
Brandon Low74cbb0a2007-06-17 21:16:34 +0000274 global_settings.colors_file);
275 fd = open(buffer, O_RDONLY);
276 if (fd < 0)
277 return;
278 while (read_line(fd, buffer, MAX_PATH) > 0)
279 {
280 if (!settings_parseline(buffer, &ext, &color))
281 continue;
Brandon Lowf8682a02007-06-17 23:10:01 +0000282 if (!strcasecmp(ext, "folder"))
283 {
Dave Chapmane92d2c52008-03-21 13:41:35 +0000284 hex_to_rgb(color, &custom_colors[0]);
Brandon Lowf8682a02007-06-17 23:10:01 +0000285 continue;
286 }
Paul Louden5b25a6e2007-06-18 22:41:39 +0000287 if (!strcasecmp(ext, "???"))
288 {
Jonathan Gordon70595a22013-01-20 22:29:43 +1100289 hex_to_rgb(color, &unknown_file.color);
Paul Louden5b25a6e2007-06-18 22:41:39 +0000290 continue;
291 }
Teruaki Kawashima194bc862010-11-27 12:57:19 +0000292 i = find_extension(ext);
293 if (i >= 0)
294 hex_to_rgb(color, &custom_colors[i]);
Brandon Low74cbb0a2007-06-17 21:16:34 +0000295 }
296 close(fd);
297}
298#endif
Jonathan Gordon6a5cc0b2007-04-16 09:14:36 +0000299#ifdef HAVE_LCD_BITMAP
300void read_viewer_theme_file(void)
301{
302 char buffer[MAX_PATH];
303 int fd;
304 char *ext, *icon;
305 int i;
Jonathan Gordon70595a22013-01-20 22:29:43 +1100306 int *icon_dest;
Jonathan Gordon5b5d1012007-04-26 05:30:46 +0000307 global_status.viewer_icon_count = 0;
Jonathan Gordona9c1df42007-04-16 14:33:29 +0000308 custom_icons_loaded = false;
Jonathan Gordonfd951ab2007-04-16 14:52:47 +0000309 custom_filetype_icons[0] = Icon_Folder;
310 for (i=1; i<filetype_count; i++)
Jonathan Gordon27643c62007-04-17 09:17:23 +0000311 {
Jonathan Gordon9d3694c2007-04-22 08:50:24 +0000312 custom_filetype_icons[i] = filetypes[i].icon;
Jonathan Gordon27643c62007-04-17 09:17:23 +0000313 }
Jonathan Gordona9c1df42007-04-16 14:33:29 +0000314
Jonathan Gordon6a5cc0b2007-04-16 09:14:36 +0000315 snprintf(buffer, MAX_PATH, "%s/%s.icons", ICON_DIR,
316 global_settings.viewers_icon_file);
317 fd = open(buffer, O_RDONLY);
318 if (fd < 0)
319 return;
320 while (read_line(fd, buffer, MAX_PATH) > 0)
321 {
322 if (!settings_parseline(buffer, &ext, &icon))
323 continue;
Teruaki Kawashima194bc862010-11-27 12:57:19 +0000324 i = find_extension(ext);
325 if (i >= 0)
Jonathan Gordon70595a22013-01-20 22:29:43 +1100326 icon_dest = &custom_filetype_icons[i];
327 else if (!strcmp(ext, "???"))
328 icon_dest = &unknown_file.icon;
329 else
330 icon_dest = NULL;
331
332 if (icon_dest)
Jonathan Gordon6a5cc0b2007-04-16 09:14:36 +0000333 {
Teruaki Kawashima194bc862010-11-27 12:57:19 +0000334 if (*icon == '*')
Jonathan Gordon70595a22013-01-20 22:29:43 +1100335 *icon_dest = atoi(icon+1);
Teruaki Kawashima194bc862010-11-27 12:57:19 +0000336 else if (*icon == '-')
Jonathan Gordon70595a22013-01-20 22:29:43 +1100337 *icon_dest = Icon_NOICON;
Teruaki Kawashima194bc862010-11-27 12:57:19 +0000338 else if (*icon >= '0' && *icon <= '9')
Jonathan Gordon6a5cc0b2007-04-16 09:14:36 +0000339 {
Teruaki Kawashima194bc862010-11-27 12:57:19 +0000340 int number = atoi(icon);
341 if (number > global_status.viewer_icon_count)
342 global_status.viewer_icon_count++;
Jonathan Gordon70595a22013-01-20 22:29:43 +1100343 *icon_dest = Icon_Last_Themeable + number;
Jonathan Gordon6a5cc0b2007-04-16 09:14:36 +0000344 }
345 }
346 }
347 close(fd);
Jonathan Gordona9c1df42007-04-16 14:33:29 +0000348 custom_icons_loaded = true;
Jonathan Gordon6a5cc0b2007-04-16 09:14:36 +0000349}
350#endif
Björn Stenbergfb00c212004-05-21 20:08:24 +0000351
Jonathan Gordon21165a32007-03-29 06:16:00 +0000352void filetype_init(void)
353{
354 /* set the directory item first */
355 filetypes[0].extension = NULL;
356 filetypes[0].plugin = NULL;
357 filetypes[0].attr = 0;
Jonathan Gordon6a5cc0b2007-04-16 09:14:36 +0000358 filetypes[0].icon = Icon_Folder;
Teruaki Kawashima7d3f8c02010-11-09 14:35:19 +0000359
Thomas Martitzd0b72e22011-08-30 14:01:33 +0000360 /* estimate bufsize with the filesize, will not be larger */
Teruaki Kawashima7d3f8c02010-11-09 14:35:19 +0000361 viewer_count = 0;
Jonathan Gordon21165a32007-03-29 06:16:00 +0000362 filetype_count = 1;
Thomas Martitzd0b72e22011-08-30 14:01:33 +0000363
364 int fd = open(VIEWERS_CONFIG, O_RDONLY);
365 if (fd < 0)
366 return;
367
368 strdup_bufsize = filesize(fd);
Thomas Martitzbaa070c2011-08-30 14:01:45 +0000369 strdup_handle = core_alloc_ex("filetypes", strdup_bufsize, &ops);
Bertrik Sikkend023bf02012-06-10 17:15:47 +0200370 if (strdup_handle <= 0)
Thomas Jarosch96afbb32011-10-02 20:04:44 +0000371 {
372 close(fd);
Thomas Martitzd0b72e22011-08-30 14:01:33 +0000373 return;
Thomas Jarosch96afbb32011-10-02 20:04:44 +0000374 }
Jonathan Gordon21165a32007-03-29 06:16:00 +0000375 read_builtin_types();
Thomas Martitzd0b72e22011-08-30 14:01:33 +0000376 read_config(fd);
Thomas Jarosch96afbb32011-10-02 20:04:44 +0000377 close(fd);
Jonathan Gordon6a5cc0b2007-04-16 09:14:36 +0000378#ifdef HAVE_LCD_BITMAP
379 read_viewer_theme_file();
380#endif
Brandon Low74cbb0a2007-06-17 21:16:34 +0000381#ifdef HAVE_LCD_COLOR
382 read_color_theme_file();
383#endif
Thomas Martitzd0b72e22011-08-30 14:01:33 +0000384 core_shrink(strdup_handle, core_get_data(strdup_handle), strdup_cur_idx);
Björn Stenbergfb00c212004-05-21 20:08:24 +0000385}
386
Jonathan Gordon21165a32007-03-29 06:16:00 +0000387/* remove all white spaces from string */
388static void rm_whitespaces(char* str)
Björn Stenbergfb00c212004-05-21 20:08:24 +0000389{
Jonathan Gordon21165a32007-03-29 06:16:00 +0000390 char *s = str;
391 while (*str)
Björn Stenbergfb00c212004-05-21 20:08:24 +0000392 {
Jonathan Gordon21165a32007-03-29 06:16:00 +0000393 if (!isspace(*str))
Björn Stenbergfb00c212004-05-21 20:08:24 +0000394 {
Jonathan Gordon21165a32007-03-29 06:16:00 +0000395 *s = *str;
396 s++;
397 }
398 str++;
399 }
400 *s = '\0';
401}
402
403static void read_builtin_types(void)
404{
Jonathan Gordon36a2e302007-04-18 13:03:01 +0000405 int count = sizeof(inbuilt_filetypes)/sizeof(*inbuilt_filetypes), i;
Jonathan Gordon07a7d472007-08-12 09:53:41 +0000406 for(i=0; i<count && (filetype_count < MAX_FILETYPES); i++)
Jonathan Gordon21165a32007-03-29 06:16:00 +0000407 {
Jonathan Gordon36a2e302007-04-18 13:03:01 +0000408 filetypes[filetype_count].extension = inbuilt_filetypes[i].extension;
Jonathan Gordon21165a32007-03-29 06:16:00 +0000409 filetypes[filetype_count].plugin = NULL;
Jonathan Gordon07a7d472007-08-12 09:53:41 +0000410 filetypes[filetype_count].attr = inbuilt_filetypes[i].tree_attr>>8;
Nils Wallménius11a667b2008-05-24 20:56:19 +0000411 if (filetypes[filetype_count].attr > highest_attr)
412 highest_attr = filetypes[filetype_count].attr;
Jonathan Gordon07a7d472007-08-12 09:53:41 +0000413 filetypes[filetype_count].icon = inbuilt_filetypes[i].icon;
Jonathan Gordon21165a32007-03-29 06:16:00 +0000414 filetype_count++;
415 }
416}
417
Thomas Martitzd0b72e22011-08-30 14:01:33 +0000418static void read_config(int fd)
Jonathan Gordon21165a32007-03-29 06:16:00 +0000419{
420 char line[64], *s, *e;
Teruaki Kawashima7d3f8c02010-11-09 14:35:19 +0000421 char *extension, *plugin;
Teruaki Kawashima7d3f8c02010-11-09 14:35:19 +0000422 /* config file is in the format
Jonathan Gordon21165a32007-03-29 06:16:00 +0000423 <extension>,<plugin>,<icon code>
424 ignore line if either of the first two are missing */
Teruaki Kawashima7d3f8c02010-11-09 14:35:19 +0000425 while (read_line(fd, line, sizeof line) > 0)
Jonathan Gordon21165a32007-03-29 06:16:00 +0000426 {
Jonathan Gordon07a7d472007-08-12 09:53:41 +0000427 if (filetype_count >= MAX_FILETYPES)
Jonathan Gordon21165a32007-03-29 06:16:00 +0000428 {
Nils Wallménius01729e72008-08-15 08:27:39 +0000429 splash(HZ, ID2P(LANG_FILETYPES_FULL));
Jonathan Gordon21165a32007-03-29 06:16:00 +0000430 break;
431 }
432 rm_whitespaces(line);
Teruaki Kawashima194bc862010-11-27 12:57:19 +0000433 /* get the extension */
Jonathan Gordon21165a32007-03-29 06:16:00 +0000434 s = line;
435 e = strchr(s, ',');
436 if (!e)
437 continue;
438 *e = '\0';
Teruaki Kawashima7d3f8c02010-11-09 14:35:19 +0000439 extension = s;
440
Jonathan Gordon21165a32007-03-29 06:16:00 +0000441 /* get the plugin */
442 s = e+1;
Jonathan Gordon21165a32007-03-29 06:16:00 +0000443 e = strchr(s, ',');
444 if (!e)
445 continue;
446 *e = '\0';
Teruaki Kawashima7d3f8c02010-11-09 14:35:19 +0000447 plugin = s;
448
Jonathan Gordon70595a22013-01-20 22:29:43 +1100449 if (!strcmp("???", extension))
450 {
451 /* get the icon */
452 s = e+1;
453 if (*s == '*')
454 unknown_file.icon = atoi(s+1);
455 else if (*s == '-')
456 unknown_file.icon = Icon_NOICON;
457 else if (*s >= '0' && *s <= '9')
458 unknown_file.icon = Icon_Last_Themeable + atoi(s);
459 continue;
460 }
461
Jonathan Gordon21165a32007-03-29 06:16:00 +0000462 /* ok, store this plugin/extension, check icon after */
Teruaki Kawashima7d3f8c02010-11-09 14:35:19 +0000463 struct file_type *file_type = &filetypes[filetype_count];
464 file_type->extension = filetypes_strdup(extension);
465 file_type->plugin = filetypes_store_plugin(plugin, filetype_count);
466 file_type->attr = highest_attr +1;
467 file_type->icon = Icon_Questionmark;
Nils Wallménius11a667b2008-05-24 20:56:19 +0000468 highest_attr++;
Jonathan Gordon21165a32007-03-29 06:16:00 +0000469 /* get the icon */
Jonathan Gordon21165a32007-03-29 06:16:00 +0000470 s = e+1;
Jonathan Gordon6a5cc0b2007-04-16 09:14:36 +0000471 if (*s == '*')
Teruaki Kawashima7d3f8c02010-11-09 14:35:19 +0000472 file_type->icon = atoi(s+1);
Jonathan Gordon6a5cc0b2007-04-16 09:14:36 +0000473 else if (*s == '-')
Teruaki Kawashima7d3f8c02010-11-09 14:35:19 +0000474 file_type->icon = Icon_NOICON;
Jonathan Gordon4a167392007-04-16 12:26:49 +0000475 else if (*s >= '0' && *s <= '9')
Teruaki Kawashima7d3f8c02010-11-09 14:35:19 +0000476 file_type->icon = Icon_Last_Themeable + atoi(s);
Jonathan Gordon21165a32007-03-29 06:16:00 +0000477 filetype_count++;
Björn Stenbergfb00c212004-05-21 20:08:24 +0000478 }
Jonathan Gordon21165a32007-03-29 06:16:00 +0000479}
Björn Stenbergfb00c212004-05-21 20:08:24 +0000480
Jonathan Gordon21165a32007-03-29 06:16:00 +0000481int filetype_get_attr(const char* file)
482{
483 char *extension = strrchr(file, '.');
484 int i;
485 if (!extension)
486 return 0;
487 extension++;
Teruaki Kawashima194bc862010-11-27 12:57:19 +0000488
489 i = find_extension(extension);
490 if (i >= 0)
491 return (filetypes[i].attr<<8)&FILE_ATTR_MASK;
Björn Stenbergfb00c212004-05-21 20:08:24 +0000492 return 0;
493}
494
Jonathan Gordon21165a32007-03-29 06:16:00 +0000495static int find_attr(int attr)
Björn Stenbergfb00c212004-05-21 20:08:24 +0000496{
497 int i;
Jonathan Gordon21165a32007-03-29 06:16:00 +0000498 /* skip the directory item */
499 if ((attr & ATTR_DIRECTORY)==ATTR_DIRECTORY)
500 return 0;
501 for (i=1; i<filetype_count; i++)
502 {
503 if ((attr>>8) == filetypes[i].attr)
504 return i;
505 }
506 return -1;
507}
Björn Stenbergfb00c212004-05-21 20:08:24 +0000508
Brandon Low74cbb0a2007-06-17 21:16:34 +0000509#ifdef HAVE_LCD_COLOR
Brandon Lowf8682a02007-06-17 23:10:01 +0000510int filetype_get_color(const char * name, int attr)
Brandon Low74cbb0a2007-06-17 21:16:34 +0000511{
Brandon Lowf8682a02007-06-17 23:10:01 +0000512 char *extension;
513 int i;
514 if ((attr & ATTR_DIRECTORY)==ATTR_DIRECTORY)
515 return custom_colors[0];
516 extension = strrchr(name, '.');
517 if (!extension)
Boris Gjenero15fa7f82013-06-05 18:35:09 -0400518 return unknown_file.color;
Brandon Lowf8682a02007-06-17 23:10:01 +0000519 extension++;
Teruaki Kawashima194bc862010-11-27 12:57:19 +0000520
521 i = find_extension(extension);
522 if (i >= 0)
523 return custom_colors[i];
Jonathan Gordon70595a22013-01-20 22:29:43 +1100524 return unknown_file.color;
Brandon Low74cbb0a2007-06-17 21:16:34 +0000525}
526#endif
527
Jonathan Gordon6a5cc0b2007-04-16 09:14:36 +0000528int filetype_get_icon(int attr)
Jonathan Gordon21165a32007-03-29 06:16:00 +0000529{
530 int index = find_attr(attr);
531 if (index < 0)
Jonathan Gordon70595a22013-01-20 22:29:43 +1100532 return unknown_file.icon;
Jonathan Gordona9c1df42007-04-16 14:33:29 +0000533 if (custom_icons_loaded)
534 return custom_filetype_icons[index];
Jonathan Gordon6a5cc0b2007-04-16 09:14:36 +0000535 return filetypes[index].icon;
Jonathan Gordon21165a32007-03-29 06:16:00 +0000536}
537
538char* filetype_get_plugin(const struct entry* file)
539{
540 static char plugin_name[MAX_PATH];
541 int index = find_attr(file->attr);
542 if (index < 0)
543 return NULL;
Jonathan Gordon31241342007-04-16 11:10:41 +0000544 if (filetypes[index].plugin == NULL)
Jonathan Gordonccaf24a2007-04-16 11:01:41 +0000545 return NULL;
Jonathan Gordon21165a32007-03-29 06:16:00 +0000546 snprintf(plugin_name, MAX_PATH, "%s/%s.%s",
Jonathan Gordonfda7d722007-08-06 13:42:52 +0000547 PLUGIN_DIR, filetypes[index].plugin, ROCK_EXTENSION);
Jonathan Gordon21165a32007-03-29 06:16:00 +0000548 return plugin_name;
549}
550
Teruaki Kawashima7d3f8c02010-11-09 14:35:19 +0000551bool filetype_supported(int attr)
Jonathan Gordon21165a32007-03-29 06:16:00 +0000552{
553 return find_attr(attr) >= 0;
554}
555
Jonathan Gordon73160322007-08-07 07:55:18 +0000556/**** Open With Screen ****/
Jonathan Gordon5ca8abf2007-10-21 00:11:47 +0000557struct cb_data {
Nils Wallménius11a667b2008-05-24 20:56:19 +0000558 const char *current_file;
Jonathan Gordon5ca8abf2007-10-21 00:11:47 +0000559};
Nils Wallménius68489612008-04-09 15:25:17 +0000560
561static enum themable_icons openwith_get_icon(int selected_item, void * data)
Jonathan Gordon73160322007-08-07 07:55:18 +0000562{
Teruaki Kawashima7d3f8c02010-11-09 14:35:19 +0000563 (void)data;
564 return filetypes[viewers[selected_item]].icon;
Jonathan Gordon73160322007-08-07 07:55:18 +0000565}
Nils Wallménius68489612008-04-09 15:25:17 +0000566
Nils Wallménius3200d042009-08-20 16:47:44 +0000567static const char* openwith_get_name(int selected_item, void * data,
568 char * buffer, size_t buffer_len)
Jonathan Gordon73160322007-08-07 07:55:18 +0000569{
Teruaki Kawashima7d3f8c02010-11-09 14:35:19 +0000570 (void)data; (void)buffer; (void)buffer_len;
571 const char *s = strrchr(filetypes[viewers[selected_item]].plugin, '/');
Jonathan Gordon73160322007-08-07 07:55:18 +0000572 if (s)
573 return s+1;
Teruaki Kawashima7d3f8c02010-11-09 14:35:19 +0000574 else return filetypes[viewers[selected_item]].plugin;
Jonathan Gordon73160322007-08-07 07:55:18 +0000575}
Nils Wallménius68489612008-04-09 15:25:17 +0000576
Solomon Peachy4adad0b2018-10-30 09:45:26 -0400577static int openwith_get_talk(int selected_item, void * data)
578{
579 (void)data;
580 char viewer_filename[MAX_FILENAME];
581 snprintf(viewer_filename, MAX_FILENAME, "%s.%s",
582 filetypes[viewers[selected_item]].plugin, ROCK_EXTENSION);
583 talk_file_or_spell(PLUGIN_DIR, viewer_filename,
584 NULL, false);
585 return 0;
586}
587
Nils Wallménius68489612008-04-09 15:25:17 +0000588static int openwith_action_callback(int action, struct gui_synclist *lists)
Jonathan Gordon5ca8abf2007-10-21 00:11:47 +0000589{
Jonathan Gordonf5ec3e42007-12-17 02:37:21 +0000590 struct cb_data *info = (struct cb_data *)lists->data;
Jonathan Gordon5ca8abf2007-10-21 00:11:47 +0000591 int i;
592 if (action == ACTION_STD_OK)
593 {
594 char plugin[MAX_PATH];
Teruaki Kawashima7d3f8c02010-11-09 14:35:19 +0000595 i = viewers[gui_synclist_get_sel_pos(lists)];
Jonathan Gordon5ca8abf2007-10-21 00:11:47 +0000596 snprintf(plugin, MAX_PATH, "%s/%s.%s",
597 PLUGIN_DIR, filetypes[i].plugin, ROCK_EXTENSION);
598 plugin_load(plugin, info->current_file);
599 return ACTION_STD_CANCEL;
600 }
601 return action;
602}
Nils Wallménius68489612008-04-09 15:25:17 +0000603
Jonathan Gordon21165a32007-03-29 06:16:00 +0000604int filetype_list_viewers(const char* current_file)
605{
Jonathan Gordon5ca8abf2007-10-21 00:11:47 +0000606 struct simplelist_info info;
Teruaki Kawashima7d3f8c02010-11-09 14:35:19 +0000607 struct cb_data data = { current_file };
Jonathan Gordon21165a32007-03-29 06:16:00 +0000608#ifndef HAVE_LCD_BITMAP
Teruaki Kawashima7d3f8c02010-11-09 14:35:19 +0000609 if (viewer_count == 0)
Jonathan Gordon21165a32007-03-29 06:16:00 +0000610 {
Solomon Peachy4adad0b2018-10-30 09:45:26 -0400611 splash(HZ*2, ID2P(LANG_NO_VIEWERS));
Jonathan Gordon21165a32007-03-29 06:16:00 +0000612 return PLUGIN_OK;
613 }
614#endif
Teruaki Kawashima7d3f8c02010-11-09 14:35:19 +0000615 simplelist_info_init(&info, str(LANG_ONPLAY_OPEN_WITH), viewer_count, &data);
Jonathan Gordon5ca8abf2007-10-21 00:11:47 +0000616 info.action_callback = openwith_action_callback;
617 info.get_name = openwith_get_name;
Jonathan Gordone4253712009-12-16 08:36:46 +0000618 info.get_icon = global_settings.show_icons?openwith_get_icon:NULL;
Solomon Peachy4adad0b2018-10-30 09:45:26 -0400619 info.get_talk = openwith_get_talk;
Jonathan Gordon5ca8abf2007-10-21 00:11:47 +0000620 return simplelist_show_list(&info);
Björn Stenbergfb00c212004-05-21 20:08:24 +0000621}
622
Nils Wallméniusa0377bd2011-12-05 19:23:46 +0000623int filetype_load_plugin(const char* plugin, const char* file)
Björn Stenbergfb00c212004-05-21 20:08:24 +0000624{
Jonathan Gordon73160322007-08-07 07:55:18 +0000625 int i;
Jonathan Gordon21165a32007-03-29 06:16:00 +0000626 char plugin_name[MAX_PATH];
Jonathan Gordon73160322007-08-07 07:55:18 +0000627 char *s;
Teruaki Kawashima7d3f8c02010-11-09 14:35:19 +0000628
Jonathan Gordon73160322007-08-07 07:55:18 +0000629 for (i=0;i<filetype_count;i++)
Linus Nielsen Feltzing25efd332006-03-13 07:33:30 +0000630 {
Jonathan Gordon73160322007-08-07 07:55:18 +0000631 if (filetypes[i].plugin)
Linus Nielsen Feltzing25efd332006-03-13 07:33:30 +0000632 {
Jonathan Gordon73160322007-08-07 07:55:18 +0000633 s = strrchr(filetypes[i].plugin, '/');
634 if (s)
635 {
636 if (!strcmp(s+1, plugin))
637 break;
638 }
639 else if (!strcmp(filetypes[i].plugin, plugin))
640 break;
Linus Nielsen Feltzing25efd332006-03-13 07:33:30 +0000641 }
642 }
Jonathan Gordon73160322007-08-07 07:55:18 +0000643 if (i >= filetype_count)
644 return PLUGIN_ERROR;
645 snprintf(plugin_name, MAX_PATH, "%s/%s.%s",
646 PLUGIN_DIR, filetypes[i].plugin, ROCK_EXTENSION);
Steve Bavinad95df22008-05-12 17:52:50 +0000647 return plugin_load(plugin_name, file);
Björn Stenbergfb00c212004-05-21 20:08:24 +0000648}