summaryrefslogtreecommitdiff
path: root/apps/open_plugin.c
diff options
context:
space:
mode:
authorWilliam Wilgus <wilgus.william@gmail.com>2020-07-31 22:45:10 -0400
committerWilliam Wilgus <wilgus.william@gmail.com>2020-08-17 10:15:14 -0400
commit35502834423049b319fde41ff305b48de67d2d51 (patch)
tree00d638d9348cd38f15cf6d341e700bc36153b3f8 /apps/open_plugin.c
parentd553bb1149800daf16dcb92bc0608fe6248e1dab (diff)
downloadrockbox-35502834423049b319fde41ff305b48de67d2d51.tar.gz
rockbox-35502834423049b319fde41ff305b48de67d2d51.zip
Add open_plugin to core
open_plugin allows arbitrary plugins to be called in hotkey and start screen replaces PictureFlow Integration shortcuts menu plays plugins now too rather than store paths and parameters in the settings that reside in memory instead entries in a file are searched by hash. after all, the plugin has to be loaded from disk anyways ---------------------------------------------------------------------------- shortcut_viewer.rock-- can now call plugins rather than taking you to them in the browser ----------------------------------------------------------------------------- Added a new option to menus: F_CB_ON_SELECT_ONLY instead of option callback every time a item is accessed F_CB_ON_SELECT_ONLY fires callback only when item is selected ----------------------------------------------------------------------------- Added manual entries ----------------------------------------------------------------------------- Change-Id: I078b57b1d2b4dd633c89212c1082fcbc1b516e6a
Diffstat (limited to 'apps/open_plugin.c')
-rw-r--r--apps/open_plugin.c204
1 files changed, 204 insertions, 0 deletions
diff --git a/apps/open_plugin.c b/apps/open_plugin.c
new file mode 100644
index 0000000000..0fb20403bc
--- /dev/null
+++ b/apps/open_plugin.c
@@ -0,0 +1,204 @@
1/***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
9 *
10 * Copyright (C) 2020 by William Wilgus
11 *
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.
16 *
17 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
18 * KIND, either express or implied.
19 *
20 ****************************************************************************/
21
22#ifndef __PCTOOL__
23
24#include "plugin.h"
25#include "open_plugin.h"
26#include "pathfuncs.h"
27#include "keyboard.h"
28#include "splash.h"
29#include "lang.h"
30
31struct open_plugin_entry_t open_plugin_entry;
32
33static const int op_entry_sz = sizeof(struct open_plugin_entry_t);
34
35static void get_param(void)
36{
37 char tmp_buf[OPEN_PLUGIN_BUFSZ+1];
38 strlcpy(tmp_buf, open_plugin_entry.param, OPEN_PLUGIN_BUFSZ);
39 if (kbd_input(tmp_buf, OPEN_PLUGIN_BUFSZ, NULL))
40 strlcpy(open_plugin_entry.param, tmp_buf, OPEN_PLUGIN_BUFSZ);
41}
42
43uint32_t open_plugin_add_path(const char *key, const char *plugin, const char *parameter)
44{
45 int len;
46 uint32_t hash;
47 char *pos;
48 int fd = 0;
49
50 /*strlcpy(plug_entry.key, key, sizeof(plug_entry.key));*/
51 open_plugin_entry.lang_id = P2ID((unsigned char*)key);
52 key = P2STR((unsigned char *)key);
53
54 open_plugin_get_hash(key, &hash);
55 open_plugin_entry.hash = hash;
56
57 if (plugin)
58 {
59 /* name */
60 if (path_basename(plugin, (const char **)&pos) == 0)
61 pos = "\0";
62
63 len = strlcpy(open_plugin_entry.name, pos, OPEN_PLUGIN_NAMESZ);
64
65 if(len > 5 && strcasecmp(&(pos[len-5]), ".rock") == 0)
66 {
67 fd = open(OPEN_PLUGIN_DAT ".tmp", O_WRONLY | O_CREAT | O_TRUNC, 0666);
68 if (!fd)
69 return 0;
70
71 /* path */
72 strlcpy(open_plugin_entry.path, plugin, OPEN_PLUGIN_BUFSZ);
73
74 if(parameter)
75 {
76 if (parameter[0] == '\0' &&
77 yesno_pop(ID2P(LANG_PARAMETER)))
78 {
79 get_param();
80 }
81 else
82 strlcpy(open_plugin_entry.param, parameter, OPEN_PLUGIN_BUFSZ);
83 }
84
85 write(fd, &open_plugin_entry, op_entry_sz);
86 }
87 else
88 {
89 if (open_plugin_entry.lang_id != LANG_SHORTCUTS)
90 splashf(HZ / 2, str(LANG_OPEN_PLUGIN_NOT_A_PLUGIN), pos);
91 return 0;
92 }
93 }
94
95 int fd1 = open(OPEN_PLUGIN_DAT, O_RDONLY);
96 if (fd1)
97 {
98 while (read(fd1, &open_plugin_entry, op_entry_sz) == op_entry_sz)
99 {
100 if (open_plugin_entry.hash != hash)
101 write(fd, &open_plugin_entry, op_entry_sz);
102 }
103 close(fd1);
104 }
105 close(fd);
106
107 if(fd1)
108 {
109 remove(OPEN_PLUGIN_DAT);
110 rename(OPEN_PLUGIN_DAT ".tmp", OPEN_PLUGIN_DAT);
111 }
112 else
113 hash = 0;
114
115 return hash;
116}
117
118void open_plugin_browse(const char *key)
119{
120 struct browse_context browse;
121 char tmp_buf[OPEN_PLUGIN_BUFSZ+1];
122 open_plugin_get_entry(key, &open_plugin_entry);
123
124 if (open_plugin_entry.path[0] == '\0')
125 strcpy(open_plugin_entry.path, PLUGIN_DIR"/");
126
127 browse_context_init(&browse, SHOW_ALL, BROWSE_SELECTONLY, "",
128 Icon_Plugin, open_plugin_entry.path, NULL);
129
130 browse.buf = tmp_buf;
131 browse.bufsize = OPEN_PLUGIN_BUFSZ;
132
133 if (rockbox_browse(&browse) == GO_TO_PREVIOUS)
134 {
135 open_plugin_add_path(key, tmp_buf, NULL);
136 }
137}
138
139void open_plugin_remove(const char *key)
140{
141 (void)key;
142 open_plugin_add_path(key, NULL, NULL);
143}
144
145static int open_plugin_hash_get_entry(uint32_t hash, struct open_plugin_entry_t *entry)
146{
147 int ret = -1, record = -1;
148
149 if (entry)
150 {
151 int fd = open(OPEN_PLUGIN_DAT, O_RDONLY);
152
153 if (fd)
154 {
155 while (read(fd, entry, op_entry_sz) == op_entry_sz)
156 {
157 record++;
158 if (entry->hash == hash)
159 {
160 ret = record;
161 break;
162 }
163 }
164 close(fd);
165 }
166 if (ret < 0)
167 {
168 memset(entry, 0, op_entry_sz);
169 entry->lang_id = -1;
170 }
171 }
172
173 return ret;
174}
175
176int open_plugin_get_entry(const char *key, struct open_plugin_entry_t *entry)
177{
178 uint32_t hash;
179 key = P2STR((unsigned char *)key);
180
181 open_plugin_get_hash(key, &hash);
182 return open_plugin_hash_get_entry(hash, entry);
183}
184
185int open_plugin_run(const char *key)
186{
187 int ret = 0;
188 const char *path;
189 const char *param;
190
191 open_plugin_get_entry(key, &open_plugin_entry);
192
193 path = open_plugin_entry.path;
194 param = open_plugin_entry.param;
195 if (param[0] == '\0')
196 param = NULL;
197
198 if (path)
199 ret = plugin_load(path, param);
200
201 return ret;
202}
203
204#endif /* ndef __PCTOOL__ */