* fix for theme font handling (when switching back to the default theme, the language...
[open-ps2-loader/simon.git] / src / appsupport.c
blobdbf7f55b717bd047035394db09f18a5b3ac5b033
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("APPSUPPORT Init\n");
45 appForceUpdate = 1;
46 configGetInt(configGetByType(CONFIG_OPL), "app_frames_delay", &appItemList.delay);
47 configApps = configGetByType(CONFIG_APPS);
48 appItemList.enabled = 1;
51 item_list_t* appGetObject(int initOnly) {
52 if (initOnly && !appItemList.enabled)
53 return NULL;
54 return &appItemList;
57 static int appNeedsUpdate(void) {
58 if (appForceUpdate) {
59 appForceUpdate = 0;
60 return 1;
63 return 0;
66 static int appUpdateItemList(void) {
67 appItemCount = 0;
68 configClear(configApps);
69 configRead(configApps);
71 if (configApps->head) {
72 struct config_value_t* cur = configApps->head;
73 while (cur) {
74 cur = cur->next;
75 appItemCount++;
78 return appItemCount;
81 static int appGetItemCount(void) {
82 return appItemCount;
85 static char* appGetItemName(int id) {
86 struct config_value_t* cur = appGetConfigValue(id);
87 return cur->key;
90 static int appGetItemNameLength(int id) {
91 return 32;
94 static char* appGetItemStartup(int id) {
95 struct config_value_t* cur = appGetConfigValue(id);
96 return appGetELFName(cur->val);
99 #ifndef __CHILDPROOF
100 static void appDeleteItem(int id) {
101 struct config_value_t* cur = appGetConfigValue(id);
102 fileXioRemove(cur->val);
103 cur->key[0] = '\0';
104 configApps->modified = 1;
105 configWrite(configApps);
107 appForceUpdate = 1;
110 static void appRenameItem(int id, char* newName) {
111 struct config_value_t* cur = appGetConfigValue(id);
113 char value[255];
114 strncpy(value, cur->val, 255);
115 configRemoveKey(configApps, cur->key);
116 configSetStr(configApps, newName, value);
117 configWrite(configApps);
119 appForceUpdate = 1;
121 #endif
123 static void appLaunchItem(int id, config_set_t* configSet) {
124 struct config_value_t* cur = appGetConfigValue(id);
125 int fd = fioOpen(cur->val, O_RDONLY);
126 if (fd >= 0) {
127 fioClose(fd);
129 int exception = NO_EXCEPTION;
130 if (strncmp(cur->val, "pfs0:", 5) == 0)
131 exception = UNMOUNT_EXCEPTION;
133 char filename[255];
134 sprintf(filename,"%s",cur->val);
135 shutdown(exception); // CAREFUL: shutdown will call appCleanUp, so configApps/cur will be freed
136 sysExecElf(filename, 0, NULL);
138 else
139 guiMsgBox(_l(_STR_ERR_FILE_INVALID), 0, NULL);
142 static config_set_t* appGetConfig(int id) {
143 config_set_t* config = configAlloc(0, NULL, NULL);
144 struct config_value_t* cur = appGetConfigValue(id);
145 configSetStr(config, CONFIG_ITEM_NAME, appGetELFName(cur->val));
146 configSetStr(config, CONFIG_ITEM_LONGNAME, cur->key);
147 configSetStr(config, CONFIG_ITEM_STARTUP, cur->val);
148 return config;
151 static int appGetImage(char* folder, int isRelative, char* value, char* suffix, GSTEXTURE* resultTex, short psm) {
152 value = appGetELFName(value);
153 // We search on ever devices from fatest to slowest (HDD > ETH > USB)
154 static item_list_t *listSupport = NULL;
155 if ( (listSupport = hddGetObject(1)) ) {
156 if (listSupport->itemGetImage(folder, isRelative, value, suffix, resultTex, psm) >= 0)
157 return 0;
160 if ( (listSupport = ethGetObject(1)) ) {
161 if (listSupport->itemGetImage(folder, isRelative, value, suffix, resultTex, psm) >= 0)
162 return 0;
165 if ( (listSupport = usbGetObject(1)) )
166 return listSupport->itemGetImage(folder, isRelative, value, suffix, resultTex, psm);
168 return -1;
171 static void appCleanUp(int exception) {
172 if (appItemList.enabled) {
173 LOG("APPSUPPORT CleanUp\n");
177 static item_list_t appItemList = {
178 APP_MODE, 0, NO_COMPAT, 0, MENU_MIN_INACTIVE_FRAMES, "Applications", _STR_APPS, &appInit, &appNeedsUpdate, &appUpdateItemList,
179 #ifdef __CHILDPROOF
180 &appGetItemCount, NULL, &appGetItemName, &appGetItemNameLength, &appGetItemStartup, NULL, NULL, &appLaunchItem,
181 #else
182 &appGetItemCount, NULL, &appGetItemName, &appGetItemNameLength, &appGetItemStartup, &appDeleteItem, &appRenameItem, &appLaunchItem,
183 #endif
184 #ifdef VMC
185 &appGetConfig, &appGetImage, &appCleanUp, NULL, APP_ICON
186 #else
187 &appGetConfig, &appGetImage, &appCleanUp, APP_ICON
188 #endif