forked from belek666/Open-PS2-Loader
-
Notifications
You must be signed in to change notification settings - Fork 4
/
appsupport.c
434 lines (358 loc) · 11 KB
/
appsupport.c
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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
#include "include/opl.h"
#include "include/lang.h"
#include "include/gui.h"
#include "include/appsupport.h"
#include "include/themes.h"
#include "include/system.h"
#include "include/ioman.h"
#include "include/util.h"
#include "include/usbsupport.h"
#include "include/ethsupport.h"
#include "include/hddsupport.h"
static int appForceUpdate = 1;
static int appItemCount = 0;
static config_set_t *configApps;
static app_info_t *appsList;
struct app_info_linked {
struct app_info_linked *next;
app_info_t app;
};
// forward declaration
static item_list_t appItemList;
static void appFreeList(void);
static struct config_value_t *appGetConfigValue(int id)
{
struct config_value_t *cur = configApps->head;
while (id--) {
cur = cur->next;
}
return cur;
}
static char *appGetELFName(char *name)
{
// Looking for the ELF name
char *pos = strrchr(name, '/');
if (!pos)
pos = strrchr(name, ':');
if (pos) {
return pos + 1;
}
return name;
}
static char *appGetBoot(char *device, int max, char *path)
{
char *pos, *filenamesep;
int len;
// Looking for the boot device & filename from the path
pos = strrchr(path, ':');
if (pos != NULL) {
len = (int)(pos + 1 - path);
if (len + 1 > max)
len = max - 1;
strncpy(device, path, len);
device[len] = '\0';
}
filenamesep = strchr(path, '/');
if (filenamesep != NULL)
return filenamesep + 1;
if (pos) {
return pos + 1;
}
return path;
}
void appInit(void)
{
LOG("APPSUPPORT Init\n");
appForceUpdate = 1;
configGetInt(configGetByType(CONFIG_OPL), "app_frames_delay", &appItemList.delay);
configApps = configGetByType(CONFIG_APPS);
appsList = NULL;
appItemList.enabled = 1;
}
item_list_t *appGetObject(int initOnly)
{
if (initOnly && !appItemList.enabled)
return NULL;
return &appItemList;
}
static int appNeedsUpdate(void)
{
int update;
update = 0;
if (appForceUpdate)
{
appForceUpdate = 0;
update = 1;
}
if (oplShouldAppsUpdate())
update = 1;
return update;
}
static int addAppsLegacyList(struct app_info_linked **appsLinkedList)
{
struct config_value_t *cur;
struct app_info_linked *app;
int count;
configClear(configApps);
configRead(configApps);
count = 0;
cur = configApps->head;
while (cur != NULL)
{
if (*appsLinkedList == NULL)
{
*appsLinkedList = malloc(sizeof(struct app_info_linked));
app = *appsLinkedList;
app->next = NULL;
}
else
{
app = malloc(sizeof(struct app_info_linked));
if (app != NULL) {
app->next = *appsLinkedList;
*appsLinkedList = app;
}
}
if (app == NULL)
{
LOG("APPSUPPORT unable to allocate memory.\n");
break;
}
strncpy(app->app.title, cur->key, APP_TITLE_MAX + 1);
app->app.title[APP_TITLE_MAX] = '\0';
//Split the boot filename from the path.
const char *elfname = appGetELFName(cur->val);
if (elfname != cur->val) {
strncpy(app->app.boot, elfname, APP_BOOT_MAX + 1);
app->app.boot[APP_BOOT_MAX] = '\0';
int pathlen = (int)(elfname - cur->val) - 1;
if (cur->val[pathlen] == ':') //Discard only '/'.
pathlen++;
if (pathlen > APP_PATH_MAX)
pathlen = APP_PATH_MAX;
strncpy(app->app.path, cur->val, pathlen);
app->app.path[pathlen] = '\0';
} else {
//Cannot split boot filename from the path, somehow.
strncpy(app->app.boot, cur->val, APP_BOOT_MAX + 1);
app->app.boot[APP_BOOT_MAX] = '\0';
strncpy(app->app.path, cur->val, APP_PATH_MAX + 1);
app->app.path[APP_BOOT_MAX] = '\0';
}
app->app.legacy = 1;
count++;
cur = cur->next;
}
return count;
}
static int appScanCallback(const char *path, config_set_t *appConfig, void *arg)
{
struct app_info_linked **appsLinkedList = (struct app_info_linked **)arg;
struct app_info_linked *app;
const char *title, *boot;
if (configGetStr(appConfig, APP_CONFIG_TITLE, &title) != 0
&& configGetStr(appConfig, APP_CONFIG_BOOT, &boot) != 0)
{
if (*appsLinkedList == NULL)
{
*appsLinkedList = malloc(sizeof(struct app_info_linked));
app = *appsLinkedList;
app->next = NULL;
}
else
{
app = malloc(sizeof(struct app_info_linked));
if (app != NULL)
{
app->next = *appsLinkedList;
*appsLinkedList = app;
}
}
if (app == NULL)
{
LOG("APPSUPPORT unable to allocate memory.\n");
return -1;
}
strncpy(app->app.title, title, APP_TITLE_MAX+1);
app->app.title[APP_TITLE_MAX] = '\0';
strncpy(app->app.boot, boot, APP_BOOT_MAX+1);
app->app.boot[APP_BOOT_MAX] = '\0';
strncpy(app->app.path, path, APP_PATH_MAX+1);
app->app.path[APP_PATH_MAX] = '\0';
app->app.legacy = 0;
return 0;
} else {
LOG("APPSUPPORT item has no boot/title.\n");
return 1;
}
return -1;
}
static int appUpdateItemList(void)
{
struct app_info_linked *appsLinkedList, *appNext;
int i;
appFreeList();
appsLinkedList = NULL;
//Get legacy apps list first, so it is possible to use appGetConfigValue(id).
appItemCount += addAppsLegacyList(&appsLinkedList);
//Scan devices for apps.
appItemCount += oplScanApps(&appScanCallback, &appsLinkedList);
// Generate apps list
if (appItemCount > 0)
{
appsList = malloc(appItemCount * sizeof(app_info_t));
if (appsList != NULL)
{
for (i = 0; appsLinkedList != NULL; i++)
{ //appsLinkedList contains items in reverse order.
memcpy(&appsList[appItemCount - i - 1], &appsLinkedList->app, sizeof(app_info_t));
appNext = appsLinkedList->next;
free(appsLinkedList);
appsLinkedList = appNext;
}
}
else
{
LOG("APPSUPPORT unable to allocate memory.\n");
appItemCount = 0;
}
}
LOG("APPSUPPORT %d apps loaded\n", appItemCount);
return appItemCount;
}
static void appFreeList(void)
{
if (appsList != NULL)
{
appsList = NULL;
appItemCount = 0;
}
}
static int appGetItemCount(void)
{
return appItemCount;
}
static char *appGetItemName(int id)
{
return appsList[id].title;
}
static int appGetItemNameLength(int id)
{
return CONFIG_KEY_NAME_LEN;
}
/* appGetItemStartup() is called to get the startup path for display & for the art assets.
The path is used immediately, before a subsequent call to appGetItemStartup(). */
static char *appGetItemStartup(int id)
{
if (appsList[id].legacy)
{
struct config_value_t *cur = appGetConfigValue(id);
return cur->val;
} else {
return appsList[id].boot;
}
}
static void appDeleteItem(int id)
{
if (appsList[id].legacy)
{
struct config_value_t *cur = appGetConfigValue(id);
unlink(cur->val);
cur->key[0] = '\0';
configApps->modified = 1;
configWrite(configApps);
} else {
sysDeleteFolder(appsList[id].path);
}
appForceUpdate = 1;
}
static void appRenameItem(int id, char *newName)
{
char value[256];
if (appsList[id].legacy)
{
struct config_value_t *cur = appGetConfigValue(id);
strncpy(value, cur->val, sizeof(value));
configRemoveKey(configApps, cur->key);
configSetStr(configApps, newName, value);
configWrite(configApps);
} else {
config_set_t *appConfig;
snprintf(value, sizeof(value), "%s/%s", appsList[id].path, APP_TITLE_CONFIG_FILE);
appConfig = configAlloc(0, NULL, value);
if (appConfig != NULL)
{
configRead(appConfig);
configSetStr(appConfig, APP_CONFIG_TITLE, newName);
configWrite(appConfig);
configFree(appConfig);
}
}
appForceUpdate = 1;
}
static void appLaunchItem(int id, config_set_t *configSet)
{
int mode, fd;
const char *filename;
//Retrieve configuration set by appGetConfig()
configGetStr(configSet, CONFIG_ITEM_STARTUP, &filename);
fd = open(filename, O_RDONLY);
if (fd >= 0) {
close(fd);
//To keep the necessary device accessible, we will assume the mode that owns the device which contains the file to boot.
mode = oplPath2Mode(filename);
if (mode < 0)
mode = APP_MODE; //Legacy apps mode on memory card (mc?:/*)
deinit(UNMOUNT_EXCEPTION, mode); // CAREFUL: deinit will call appCleanUp, so configApps/cur will be freed
sysExecElf(filename);
} else
guiMsgBox(_l(_STR_ERR_FILE_INVALID), 0, NULL);
}
static config_set_t *appGetConfig(int id)
{
config_set_t *config;
if (appsList[id].legacy) {
config = configAlloc(0, NULL, NULL);
struct config_value_t *cur = appGetConfigValue(id);
configSetStr(config, CONFIG_ITEM_NAME, appGetELFName(cur->val));
configSetStr(config, CONFIG_ITEM_LONGNAME, cur->key);
configSetStr(config, CONFIG_ITEM_STARTUP, cur->val);
} else {
char path[256];
snprintf(path, sizeof(path), "%s/%s", appsList[id].path, APP_TITLE_CONFIG_FILE);
config = configAlloc(0, NULL, path);
configRead(config); //Does not matter if the config file could be loaded or not.
configSetStr(config, CONFIG_ITEM_NAME, appsList[id].boot);
configSetStr(config, CONFIG_ITEM_LONGNAME, appsList[id].title);
snprintf(path, sizeof(path), "%s/%s", appsList[id].path, appsList[id].boot);
configSetStr(config, CONFIG_ITEM_STARTUP, path);
}
return config;
}
static int appGetImage(char *folder, int isRelative, char *value, char *suffix, GSTEXTURE *resultTex, short psm)
{
char device[8], *startup;
startup = appGetBoot(device, sizeof(device), value);
return oplGetAppImage(device, folder, isRelative, startup, suffix, resultTex, psm);
}
//This may be called, even if appInit() was not.
static void appCleanUp(int exception)
{
if (appItemList.enabled) {
LOG("APPSUPPORT CleanUp\n");
appFreeList();
}
}
//This may be called, even if appInit() was not.
static void appShutdown(void)
{
if (appItemList.enabled) {
LOG("APPSUPPORT Shutdown\n");
appFreeList();
}
}
static item_list_t appItemList = {
APP_MODE, -1, 0, MODE_FLAG_NO_COMPAT | MODE_FLAG_NO_UPDATE, MENU_MIN_INACTIVE_FRAMES, APP_MODE_UPDATE_DELAY, "Applications", _STR_APPS, NULL, &appInit, &appNeedsUpdate, &appUpdateItemList,
&appGetItemCount, NULL, &appGetItemName, &appGetItemNameLength, &appGetItemStartup, &appDeleteItem, &appRenameItem, &appLaunchItem,
&appGetConfig, &appGetImage, &appCleanUp, &appShutdown, NULL, APP_ICON
};