summaryrefslogtreecommitdiffstats
path: root/apps/open_plugin.c
blob: c64661f2e620783cbe5faafe83c6cc0cb1cc7f3b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
/***************************************************************************
 *             __________               __   ___.
 *   Open      \______   \ ____   ____ |  | _\_ |__   _______  ___
 *   Source     |       _//  _ \_/ ___\|  |/ /| __ \ /  _ \  \/  /
 *   Jukebox    |    |   (  <_> )  \___|    < | \_\ (  <_> > <  <
 *   Firmware   |____|_  /\____/ \___  >__|_ \|___  /\____/__/\_ \
 *                     \/            \/     \/    \/            \/
 * $Id$
 *
 * Copyright (C) 2020 by William Wilgus
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
 * KIND, either express or implied.
 *
 ****************************************************************************/

#ifndef __PCTOOL__

#include "plugin.h"
#include "open_plugin.h"
#include "pathfuncs.h"
#include "splash.h"
#include "lang.h"

#define ROCK_EXT "rock"
#define ROCK_LEN 5
#define OP_EXT "opx"
#define OP_LEN 4

struct open_plugin_entry_t open_plugin_entry = {0};

static const int op_entry_sz = sizeof(struct open_plugin_entry_t);

static int open_plugin_hash_get_entry(uint32_t hash,
                  struct open_plugin_entry_t *entry,
                               const char* dat_file);

static inline void op_clear_entry(struct open_plugin_entry_t *entry)
{
    if (entry)
    {
        memset(entry, 0, op_entry_sz);
        entry->lang_id = -1;
    }
}

static int op_update_dat(struct open_plugin_entry_t *entry)
{
    int fd, fd1;
    uint32_t hash;

    if (!entry || entry->hash == 0)
        return -1;

    hash = entry->hash;

    fd = open(OPEN_PLUGIN_DAT ".tmp", O_WRONLY | O_CREAT | O_TRUNC, 0666);
    if (!fd)
        return -1;
    write(fd, entry, op_entry_sz);

    fd1 = open(OPEN_PLUGIN_DAT, O_RDONLY);
    if (fd1)
    {
        while (read(fd1, &open_plugin_entry, op_entry_sz) == op_entry_sz)
        {
            if (open_plugin_entry.hash != hash)
                write(fd, &open_plugin_entry, op_entry_sz);
        }
        close(fd1);
        remove(OPEN_PLUGIN_DAT);
    }
    close(fd);

    rename(OPEN_PLUGIN_DAT ".tmp", OPEN_PLUGIN_DAT);

    op_clear_entry(&open_plugin_entry);
    return 0;
}

uint32_t open_plugin_add_path(const char *key, const char *plugin, const char *parameter)
{
    int len;
    bool is_valid = false;
    uint32_t hash;
    int32_t lang_id;
    char *pos = "\0";

    if(!key)
    {
        op_clear_entry(&open_plugin_entry);
        return 0;
    }

    lang_id = P2ID((unsigned char*)key);
    key = P2STR((unsigned char *)key);

    open_plugin_get_hash(key, &hash);


    if(open_plugin_entry.hash != hash)
    {
        /* the entry in ram needs saved */
        op_update_dat(&open_plugin_entry);
    }

    if (plugin)
    {
        /* name */
        if (path_basename(plugin, (const char **)&pos) == 0)
            pos = "\0";

        len = strlcpy(open_plugin_entry.name, pos, OPEN_PLUGIN_NAMESZ);
        if (len > ROCK_LEN && strcasecmp(&(pos[len-ROCK_LEN]), "." ROCK_EXT) == 0)
        {
            is_valid = true;

            /* path */
            strlcpy(open_plugin_entry.path, plugin, OPEN_PLUGIN_BUFSZ);

            if(parameter)
                strlcpy(open_plugin_entry.param, parameter, OPEN_PLUGIN_BUFSZ);
            else
                open_plugin_entry.param[0] = '\0';
        }
        else if (len > OP_LEN && strcasecmp(&(pos[len-OP_LEN]), "." OP_EXT) == 0)
        {
            is_valid = true;
            open_plugin_hash_get_entry(0, &open_plugin_entry, plugin);
        }
    }

    if (!is_valid)
    {
        if (lang_id != LANG_SHORTCUTS) /* from shortcuts menu */
            splashf(HZ / 2, str(LANG_OPEN_PLUGIN_NOT_A_PLUGIN), pos);
        op_clear_entry(&open_plugin_entry);
        hash = 0;
    }
    else
    {
        open_plugin_entry.hash = hash;
        open_plugin_entry.lang_id = lang_id;
    }

    return hash;
}

void open_plugin_browse(const char *key)
{
    struct browse_context browse;
    char tmp_buf[OPEN_PLUGIN_BUFSZ+1];
    open_plugin_get_entry(key, &open_plugin_entry);

    if (open_plugin_entry.path[0] == '\0')
        strcpy(open_plugin_entry.path, PLUGIN_DIR"/");

    browse_context_init(&browse, SHOW_ALL, BROWSE_SELECTONLY, "",
                         Icon_Plugin, open_plugin_entry.path, NULL);

    browse.buf = tmp_buf;
    browse.bufsize = OPEN_PLUGIN_BUFSZ;

    if (rockbox_browse(&browse) == GO_TO_PREVIOUS)
        open_plugin_add_path(key, tmp_buf, NULL);
}

static int open_plugin_hash_get_entry(uint32_t hash,
                  struct open_plugin_entry_t *entry,
                               const char* dat_file)
{
    int ret = -1, record = -1;

    if (entry)
    {

        if (hash != 0)
        {
            if(entry->hash == hash) /* hasn't been flushed yet? */
                return 0;
            else
                op_update_dat(&open_plugin_entry);
        }

        int fd = open(dat_file, O_RDONLY);

        if (fd)
        {
            while (read(fd, entry, op_entry_sz) == op_entry_sz)
            {
                record++;
                if (hash == 0 || entry->hash == hash)
                {
                    ret = record;
                    break;
                }
            }
            close(fd);
        }
        if (ret < 0)
        {
            memset(entry, 0, op_entry_sz);
            entry->lang_id = -1;
        }
    }

    return ret;
}

int open_plugin_get_entry(const char *key, struct open_plugin_entry_t *entry)
{
    uint32_t hash;
    key = P2STR((unsigned char *)key);

    open_plugin_get_hash(key, &hash); /* in open_plugin.h */
    return open_plugin_hash_get_entry(hash, entry, OPEN_PLUGIN_DAT);
}

int open_plugin_run(const char *key)
{
    int ret = 0;
    const char *path;
    const char *param;

    open_plugin_get_entry(key, &open_plugin_entry);

    path = open_plugin_entry.path;
    param = open_plugin_entry.param;
    if (param[0] == '\0')
        param = NULL;

    if (path)
        ret = plugin_load(path, param);

    if (ret != GO_TO_PLUGIN)
        op_clear_entry(&open_plugin_entry);

    return ret;
}

void open_plugin_cache_flush(void)
{
    op_update_dat(&open_plugin_entry);
}

#endif /* ndef __PCTOOL__ */