* reverted some changes from rev500, finally crazyc was correct (when he skipped...
[open-ps2-loader.git] / src / appsupport.c
blobc215817c153c5ed1b83dbfec6e68c481ba36bc5c
1 #include "include/usbld.h"
2 #include "include/lang.h"
3 #include "include/gui.h"
4 #include "include/appsupport.h"
5 #include "include/themes.h"
6 #include "include/system.h"
7 #include "include/ioman.h"
9 #include "include/usbsupport.h"
10 #include "include/ethsupport.h"
11 #include "include/hddsupport.h"
13 static int appForceUpdate = 1;
14 static int appItemCount = 0;
16 static config_set_t *configApps;
18 // forward declaration
19 static item_list_t appItemList;
21 static struct config_value_t* appGetConfigValue(int id) {
22 struct config_value_t* cur = configApps->head;
24 while (id--) {
25 cur = cur->next;
28 return cur;
31 static char* appGetELFName(char* name) {
32 // Looking for the ELF name
33 char* pos = strrchr(name, '/');
34 if (!pos)
35 pos = strrchr(name, ':');
36 if (pos) {
37 return pos + 1;
40 return name;
43 void appInit(void) {
44 LOG("appInit()\n");
45 appForceUpdate = 1;
47 configApps = configGetByType(CONFIG_APPS);
49 appItemList.enabled = 1;
52 item_list_t* appGetObject(int initOnly) {
53 if (initOnly && !appItemList.enabled)
54 return NULL;
55 return &appItemList;
58 static int appNeedsUpdate(void) {
59 if (appForceUpdate) {
60 appForceUpdate = 0;
61 return 1;
64 return 0;
67 static int appUpdateItemList(void) {
68 appItemCount = 0;
69 configClear(configApps);
70 configRead(configApps);
72 if (configApps->head) {
73 struct config_value_t* cur = configApps->head;
74 while (cur) {
75 cur = cur->next;
76 appItemCount++;
79 return appItemCount;
82 static int appGetItemCount(void) {
83 return appItemCount;
86 static char* appGetItemName(int id) {
87 struct config_value_t* cur = appGetConfigValue(id);
88 return cur->key;
91 static int appGetItemNameLength(int id) {
92 return 32;
95 static char* appGetItemStartup(int id) {
96 struct config_value_t* cur = appGetConfigValue(id);
97 return appGetELFName(cur->val);
100 #ifndef __CHILDPROOF
101 static void appDeleteItem(int id) {
102 struct config_value_t* cur = appGetConfigValue(id);
103 fileXioRemove(cur->val);
104 cur->key[0] = '\0';
105 configApps->modified = 1;
106 configWrite(configApps);
108 appForceUpdate = 1;
111 static void appRenameItem(int id, char* newName) {
112 struct config_value_t* cur = appGetConfigValue(id);
114 char value[255];
115 strncpy(value, cur->val, 255);
116 configRemoveKey(configApps, cur->key);
117 configSetStr(configApps, newName, value);
118 configWrite(configApps);
120 appForceUpdate = 1;
122 #endif
124 static void appLaunchItem(int id, config_set_t* configSet) {
125 struct config_value_t* cur = appGetConfigValue(id);
126 int fd = fioOpen(cur->val, O_RDONLY);
127 if (fd >= 0) {
128 fioClose(fd);
130 int exception = NO_EXCEPTION;
131 if (strncmp(cur->val, "pfs0:", 5) == 0)
132 exception = UNMOUNT_EXCEPTION;
134 char filename[255];
135 sprintf(filename,"%s",cur->val);
136 shutdown(exception); // CAREFUL: shutdown will call appCleanUp, so configApps/cur will be freed
137 sysExecElf(filename, 0, NULL);
139 else
140 guiMsgBox(_l(_STR_ERR_FILE_INVALID), 0, NULL);
143 static config_set_t* appGetConfig(int id) {
144 config_set_t* config = configAlloc(0, NULL, NULL);
145 struct config_value_t* cur = appGetConfigValue(id);
146 configSetStr(config, CONFIG_ITEM_NAME, appGetELFName(cur->val));
147 configSetStr(config, CONFIG_ITEM_LONGNAME, cur->key);
148 configSetStr(config, CONFIG_ITEM_STARTUP, cur->val);
149 return config;
152 static int appGetImage(char* folder, int isRelative, char* value, char* suffix, GSTEXTURE* resultTex, short psm) {
153 value = appGetELFName(value);
154 // We search on ever devices from fatest to slowest (HDD > ETH > USB)
155 static item_list_t *listSupport = NULL;
156 if ( (listSupport = hddGetObject(1)) ) {
157 if (listSupport->itemGetImage(folder, isRelative, value, suffix, resultTex, psm) >= 0)
158 return 0;
161 if ( (listSupport = ethGetObject(1)) ) {
162 if (listSupport->itemGetImage(folder, isRelative, value, suffix, resultTex, psm) >= 0)
163 return 0;
166 if ( (listSupport = usbGetObject(1)) )
167 return listSupport->itemGetImage(folder, isRelative, value, suffix, resultTex, psm);
169 return -1;
172 static void appCleanUp(int exception) {
173 if (appItemList.enabled) {
174 LOG("appCleanUp()\n");
178 static item_list_t appItemList = {
179 APP_MODE, 0, NO_COMPAT, 0, MENU_MIN_INACTIVE_FRAMES, "Applications", _STR_APPS, &appInit, &appNeedsUpdate, &appUpdateItemList,
180 #ifdef __CHILDPROOF
181 &appGetItemCount, NULL, &appGetItemName, &appGetItemNameLength, &appGetItemStartup, NULL, NULL, &appLaunchItem,
182 #else
183 &appGetItemCount, NULL, &appGetItemName, &appGetItemNameLength, &appGetItemStartup, &appDeleteItem, &appRenameItem, &appLaunchItem,
184 #endif
185 #ifdef VMC
186 &appGetConfig, &appGetImage, &appCleanUp, NULL, APP_ICON
187 #else
188 &appGetConfig, &appGetImage, &appCleanUp, APP_ICON
189 #endif