Disabling auto-refresh of game list by default, as it is causing bugs sometimes
[open-ps2-loader.git] / src / opl.c
blobdde7d78e020a46cd2d7b746f43e4f0076c1e985b
1 /*
2 Copyright 2009, Volca
3 Licenced under Academic Free License version 3.0
4 Review OpenUsbLd README & LICENSE files for further details.
5 */
7 #include "include/usbld.h"
8 #include "include/ioman.h"
9 #include "include/gui.h"
10 #include "include/renderman.h"
11 #include "include/lang.h"
12 #include "include/themes.h"
13 #include "include/textures.h"
14 #include "include/pad.h"
15 #include "include/texcache.h"
16 #include "include/dia.h"
17 #include "include/dialogs.h"
18 #include "include/menusys.h"
19 #include "include/system.h"
20 #include "include/debug.h"
21 #include "include/config.h"
23 #include "include/usbsupport.h"
24 #include "include/ethsupport.h"
25 #include "include/hddsupport.h"
26 #include "include/appsupport.h"
28 // for sleep()
29 #include <unistd.h>
31 #ifdef __EESIO_DEBUG
32 #include <sio.h>
33 #define LOG_INIT() sio_init(38400, 0, 0, 0, 0)
34 #define LOG_ENABLE() do { } while(0)
35 #else
36 #ifdef __DEBUG
37 #define LOG_INIT() do { } while(0)
38 #define LOG_ENABLE() ioPutRequest(IO_CUSTOM_SIMPLEACTION, &debugSetActive)
39 #else
40 #define LOG_INIT() do { } while(0)
41 #define LOG_ENABLE() do { } while(0)
42 #endif
43 #endif
45 #define UPDATE_FRAME_COUNT 250
47 extern void *usbd_irx;
48 extern int size_usbd_irx;
50 typedef struct {
51 item_list_t *support;
53 /// menu item used with this list support
54 menu_item_t menuItem;
56 /// submenu list
57 submenu_list_t *subMenu;
58 } opl_io_module_t;
60 static void clearIOModuleT(opl_io_module_t *mod) {
61 mod->subMenu = NULL;
62 mod->support = NULL;
63 mod->menuItem.execCross = NULL;
64 mod->menuItem.execCircle = NULL;
65 mod->menuItem.execSquare = NULL;
66 mod->menuItem.execTriangle = NULL;
67 mod->menuItem.hints = NULL;
68 mod->menuItem.icon_id = -1;
69 mod->menuItem.current = NULL;
70 mod->menuItem.submenu = NULL;
71 mod->menuItem.pagestart = NULL;
72 mod->menuItem.remindLast = 0;
73 mod->menuItem.refresh = NULL;
74 mod->menuItem.text = NULL;
75 mod->menuItem.text_id = -1;
76 mod->menuItem.userdata = NULL;
79 // forward decl
80 static void moduleCleanup(opl_io_module_t* mod, int exception);
82 // frame counter
83 static int frameCounter;
85 static char errorMessage[255];
87 static opl_io_module_t list_support[4];
89 void moduleUpdateMenu(int mode, int themeChanged) {
90 if (mode == -1)
91 return;
93 opl_io_module_t* mod = &list_support[mode];
95 if (!mod->support)
96 return;
98 // refresh Hints
99 menuRemoveHints(&mod->menuItem);
101 menuAddHint(&mod->menuItem, _STR_SETTINGS, START_ICON);
102 if (!mod->support->enabled)
103 menuAddHint(&mod->menuItem, _STR_START_DEVICE, CROSS_ICON);
104 else {
105 if (gUseInfoScreen && gTheme->infoElems.first)
106 menuAddHint(&mod->menuItem, _STR_INFO, CROSS_ICON);
107 else
108 menuAddHint(&mod->menuItem, _STR_RUN, CROSS_ICON);
109 if (mod->support->haveCompatibilityMode)
110 menuAddHint(&mod->menuItem, _STR_COMPAT_SETTINGS, TRIANGLE_ICON);
111 if (gEnableDandR) {
112 if (mod->support->itemRename)
113 menuAddHint(&mod->menuItem, _STR_RENAME, CIRCLE_ICON);
114 if (mod->support->itemDelete)
115 menuAddHint(&mod->menuItem, _STR_DELETE, SQUARE_ICON);
119 // refresh Cache
120 if (themeChanged)
121 submenuRebuildCache(mod->subMenu);
124 static void itemExecCross(struct menu_item *curMenu) {
125 item_list_t *support = curMenu->userdata;
127 if (support) {
128 if (support->enabled) {
129 if (curMenu->current) {
130 config_set_t* configSet = menuLoadConfig();
131 support->itemLaunch(curMenu->current->item.id, configSet);
134 else {
135 support->itemInit();
136 moduleUpdateMenu(support->mode, 0);
137 if (!gAutoRefresh)
138 ioPutRequest(IO_MENU_UPDATE_DEFFERED, &support->mode);
141 else
142 guiMsgBox("NULL Support object. Please report", 0, NULL);
145 static void itemExecTriangle(struct menu_item *curMenu) {
146 if (!curMenu->current)
147 return;
149 item_list_t *support = curMenu->userdata;
151 if (support) {
152 if (support->haveCompatibilityMode) {
153 config_set_t* configSet = menuLoadConfig();
154 if (guiShowCompatConfig(curMenu->current->item.id, support, configSet) == COMPAT_TEST)
155 support->itemLaunch(curMenu->current->item.id, configSet);
158 else
159 guiMsgBox("NULL Support object. Please report", 0, NULL);
162 static void itemExecSquare(struct menu_item *curMenu) {
163 if (!curMenu->current)
164 return;
166 if (!gEnableDandR)
167 return;
169 item_list_t *support = curMenu->userdata;
171 if (support) {
172 if (support->itemDelete) {
173 if (guiMsgBox(_l(_STR_DELETE_WARNING), 1, NULL)) {
174 support->itemDelete(curMenu->current->item.id);
175 if (gAutoRefresh)
176 frameCounter = UPDATE_FRAME_COUNT;
177 else
178 ioPutRequest(IO_MENU_UPDATE_DEFFERED, &support->mode);
182 else
183 guiMsgBox("NULL Support object. Please report", 0, NULL);
186 static void itemExecCircle(struct menu_item *curMenu) {
187 if (!curMenu->current)
188 return;
190 if (!gEnableDandR)
191 return;
193 item_list_t *support = curMenu->userdata;
195 if (support) {
196 if (support->itemRename) {
197 int nameLength = support->itemGetNameLength(curMenu->current->item.id);
198 char newName[nameLength];
199 strncpy(newName, curMenu->current->item.text, nameLength);
200 if (guiShowKeyboard(newName, nameLength)) {
201 support->itemRename(curMenu->current->item.id, newName);
202 if (gAutoRefresh)
203 frameCounter = UPDATE_FRAME_COUNT;
204 else
205 ioPutRequest(IO_MENU_UPDATE_DEFFERED, &support->mode);
209 else
210 guiMsgBox("NULL Support object. Please report", 0, NULL);
213 static void itemExecRefresh(struct menu_item *curMenu) {
214 item_list_t *support = curMenu->userdata;
216 if (support && support->enabled)
217 ioPutRequest(IO_MENU_UPDATE_DEFFERED, &support->mode);
220 static void initMenuForListSupport(int mode) {
221 opl_io_module_t* mod = &list_support[mode];
222 mod->menuItem.icon_id = mod->support->iconId;
223 mod->menuItem.text = NULL;
224 mod->menuItem.text_id = mod->support->textId;
226 mod->menuItem.userdata = mod->support;
228 mod->subMenu = NULL;
230 mod->menuItem.submenu = NULL;
231 mod->menuItem.current = NULL;
232 mod->menuItem.pagestart = NULL;
233 mod->menuItem.remindLast = 0;
235 mod->menuItem.refresh = &itemExecRefresh;
236 mod->menuItem.execCross = &itemExecCross;
237 mod->menuItem.execTriangle = &itemExecTriangle;
238 mod->menuItem.execSquare = &itemExecSquare;
239 mod->menuItem.execCircle = &itemExecCircle;
241 mod->menuItem.hints = NULL;
243 moduleUpdateMenu(mode, 0);
245 struct gui_update_t *mc = guiOpCreate(GUI_OP_ADD_MENU);
246 mc->menu.menu = &mod->menuItem;
247 mc->menu.subMenu = &mod->subMenu;
248 guiDeferUpdate(mc);
251 static void initSupport(item_list_t* itemList, int startMode, int mode, int force_reinit) {
252 opl_io_module_t* mod = &list_support[mode];
253 if (!mod->support) {
254 itemList->uip = 1; // stop updates until we're done with init
255 mod->support = itemList;
256 initMenuForListSupport(mode);
257 itemList->uip = 0;
260 if (((force_reinit) && (startMode && mod->support->enabled)) \
261 || (startMode == 2 && !mod->support->enabled)) {
262 // stop updates until we're done with init of the device
263 mod->support->uip = 1;
264 mod->support->itemInit();
265 moduleUpdateMenu(mode, 0);
266 mod->support->uip = 0;
268 if (gAutoRefresh)
269 frameCounter = UPDATE_FRAME_COUNT;
270 else
271 ioPutRequest(IO_MENU_UPDATE_DEFFERED, &mod->support->mode); // can't use mode as the variable will die at end of execution
275 static void initAllSupport(int force_reinit) {
276 if (gUSBStartMode)
277 initSupport(usbGetObject(0), gUSBStartMode, USB_MODE, force_reinit);
279 if (gETHStartMode)
280 initSupport(ethGetObject(0), gETHStartMode, ETH_MODE, force_reinit||(gNetworkStartup >= ERROR_ETH_SMB_LOGON));
282 if (gHDDStartMode)
283 initSupport(hddGetObject(0), gHDDStartMode, HDD_MODE, force_reinit);
285 if (gAPPStartMode)
286 initSupport(appGetObject(0), gAPPStartMode, APP_MODE, force_reinit);
289 static void deinitAllSupport(int exception) {
290 moduleCleanup(&list_support[USB_MODE], exception);
291 moduleCleanup(&list_support[ETH_MODE], exception);
292 moduleCleanup(&list_support[HDD_MODE], exception);
293 moduleCleanup(&list_support[APP_MODE], exception);
296 // ----------------------------------------------------------
297 // ----------------------- Updaters -------------------------
298 // ----------------------------------------------------------
299 static void updateMenuFromGameList(opl_io_module_t* mdl) {
300 // lock - gui has to be unused here
301 guiLock();
303 submenuDestroy(&mdl->subMenu);
304 mdl->menuItem.submenu = NULL;
305 mdl->menuItem.current = NULL;
306 mdl->menuItem.pagestart = NULL;
307 mdl->menuItem.remindLast = 0;
309 // unlock, the rest is deferred
310 guiUnlock();
312 char* temp = NULL;
313 if (gRememberLastPlayed)
314 configGetStr(configGetByType(CONFIG_LAST), "last_played", &temp);
316 // read the new game list
317 struct gui_update_t *gup = NULL;
318 int count = mdl->support->itemUpdate();
319 if (count) {
320 int i;
322 for (i = 0; i < count; ++i) {
324 gup = guiOpCreate(GUI_OP_APPEND_MENU);
326 gup->menu.menu = &mdl->menuItem;
327 gup->menu.subMenu = &mdl->subMenu;
329 gup->submenu.icon_id = -1;
330 gup->submenu.id = i;
331 gup->submenu.text = mdl->support->itemGetName(i);
332 gup->submenu.text_id = -1;
333 gup->submenu.selected = 0;
335 if (gRememberLastPlayed && temp && strcmp(temp, mdl->support->itemGetStartup(i)) == 0)
336 gup->submenu.selected = 1;
338 guiDeferUpdate(gup);
342 if (gAutosort) {
343 gup = guiOpCreate(GUI_OP_SORT);
344 gup->menu.menu = &mdl->menuItem;
345 gup->menu.subMenu = &mdl->subMenu;
346 guiDeferUpdate(gup);
350 void menuDeferredUpdate(void* data) {
351 int* mode = data;
353 opl_io_module_t* mod = &list_support[*mode];
354 if (!mod->support)
355 return;
357 if (mod->support->uip) {
358 ioPutRequest(IO_MENU_UPDATE_DEFFERED, mode); // we are busy, so re-post refresh for later
359 return;
362 // see if we have to update
363 if (mod->support->itemNeedsUpdate()) {
364 mod->support->uip = 1;
365 updateMenuFromGameList(mod);
366 mod->support->uip = 0;
370 static void menuUpdateHook() {
371 // if timer exceeds some threshold, schedule updates of the available input sources
372 frameCounter++;
374 if (frameCounter > UPDATE_FRAME_COUNT) {
375 frameCounter = 0;
377 // schedule updates of all the list handlers
378 if (list_support[USB_MODE].support && list_support[USB_MODE].support->enabled)
379 ioPutRequest(IO_MENU_UPDATE_DEFFERED, &list_support[USB_MODE].support->mode);
380 if (list_support[ETH_MODE].support && list_support[ETH_MODE].support->enabled)
381 ioPutRequest(IO_MENU_UPDATE_DEFFERED, &list_support[ETH_MODE].support->mode);
382 if (list_support[HDD_MODE].support && list_support[HDD_MODE].support->enabled)
383 ioPutRequest(IO_MENU_UPDATE_DEFFERED, &list_support[HDD_MODE].support->mode);
384 if (list_support[APP_MODE].support && list_support[APP_MODE].support->enabled)
385 ioPutRequest(IO_MENU_UPDATE_DEFFERED, &list_support[APP_MODE].support->mode);
389 static void errorMessageHook() {
390 guiMsgBox(errorMessage, 0, NULL);
392 // reset the original frame hook
393 frameCounter = 0;
394 if (gAutoRefresh)
395 guiSetFrameHook(&menuUpdateHook);
396 else
397 guiSetFrameHook(NULL);
400 void setErrorMessage(int strId, int error) {
401 snprintf(errorMessage, 255, _l(strId), error);
402 guiSetFrameHook(&errorMessageHook);
405 // ----------------------------------------------------------
406 // ------------------ Configuration handling ----------------
407 // ----------------------------------------------------------
409 static int lscstatus = CONFIG_ALL;
410 static int lscret = 0;
412 static int tryAlternateDevice(int types) {
413 char path[64];
414 int value;
416 // check USB
417 usbLoadModules();
418 if (usbFindPartition(path, "conf_opl.cfg")) {
419 configEnd();
420 configInit(path);
421 value = configReadMulti(types);
422 config_set_t *configOPL = configGetByType(CONFIG_OPL);
423 configSetInt(configOPL, "usb_mode", 2);
424 return value;
427 // check HDD
428 hddLoadModules();
429 sprintf(path, "pfs0:conf_opl.cfg");
430 value = fioOpen(path, O_RDONLY);
431 if(value >= 0) {
432 fioClose(value);
433 configEnd();
434 configInit("pfs0:");
435 value = configReadMulti(types);
436 config_set_t *configOPL = configGetByType(CONFIG_OPL);
437 configSetInt(configOPL, "hdd_mode", 2);
438 return value;
441 if (sysCheckMC() < 0) { // We don't want to get users into alternate mode for their very first launch of OPL (i.e no config file at all, but still want to save on MC)
442 // set config path to either mass or hdd, to prepare the saving of a new config
443 value = fioDopen("mass0:");
444 if (value >= 0) {
445 fioDclose(value);
446 configEnd();
447 configInit("mass0:");
449 else {
450 configEnd();
451 configInit("pfs0:");
455 return 0;
458 static void _loadConfig() {
459 int result = configReadMulti(lscstatus);
461 if (lscstatus & CONFIG_OPL) {
462 int themeID = -1, langID = -1;
464 if (!(result & CONFIG_OPL)) {
465 result = tryAlternateDevice(lscstatus);
468 if (result & CONFIG_OPL) {
469 config_set_t *configOPL = configGetByType(CONFIG_OPL);
470 char *temp;
472 configGetInt(configOPL, "scrolling", &gScrollSpeed);
473 configGetColor(configOPL, "bg_color", gDefaultBgColor);
474 configGetColor(configOPL, "text_color", gDefaultTextColor);
475 configGetColor(configOPL, "ui_text_color", gDefaultUITextColor);
476 configGetColor(configOPL, "sel_text_color", gDefaultSelTextColor);
477 configGetInt(configOPL, "use_info_screen", &gUseInfoScreen);
478 configGetInt(configOPL, "enable_coverart", &gEnableArt);
479 configGetInt(configOPL, "wide_screen", &gWideScreen);
480 configGetInt(configOPL, "vsync", &gVSync);
481 configGetInt(configOPL, "vmode", &gVMode);
484 if (configGetStr(configOPL, "theme", &temp))
485 themeID = thmFindGuiID(temp);
487 if (configGetStr(configOPL, "language_text", &temp))
488 langID = lngFindGuiID(temp);
490 if (configGetStr(configOPL, "pc_ip", &temp))
491 sscanf(temp, "%d.%d.%d.%d", &pc_ip[0], &pc_ip[1], &pc_ip[2], &pc_ip[3]);
493 configGetInt(configOPL, "pc_port", &gPCPort);
495 if (configGetStr(configOPL, "pc_share", &temp))
496 strncpy(gPCShareName, temp, 32);
497 if (configGetStr(configOPL, "pc_user", &temp))
498 strncpy(gPCUserName, temp, 32);
499 if (configGetStr(configOPL, "pc_pass", &temp))
500 strncpy(gPCPassword, temp, 32);
501 if (configGetStr(configOPL, "exit_path", &temp))
502 strncpy(gExitPath, temp, 32);
504 configGetInt(configOPL, "autosort", &gAutosort);
505 configGetInt(configOPL, "autorefresh", &gAutoRefresh);
506 configGetInt(configOPL, "default_device", &gDefaultDevice);
507 configGetInt(configOPL, "disable_debug", &gDisableDebug);
508 configGetInt(configOPL, "enable_delete_rename", &gEnableDandR);
509 configGetInt(configOPL, "hdd_spindown", &gHDDSpindown);
510 configGetInt(configOPL, "check_usb_frag", &gCheckUSBFragmentation);
511 configGetInt(configOPL, "usb_delay", &gUSBDelay);
512 if (configGetStr(configOPL, "usb_prefix", &temp))
513 strncpy(gUSBPrefix, temp, 32);
514 if (configGetStr(configOPL, "eth_prefix", &temp))
515 strncpy(gETHPrefix, temp, 32);
516 configGetInt(configOPL, "remember_last", &gRememberLastPlayed);
517 configGetInt(configOPL, "usb_mode", &gUSBStartMode);
518 configGetInt(configOPL, "hdd_mode", &gHDDStartMode);
519 configGetInt(configOPL, "eth_mode", &gETHStartMode);
520 configGetInt(configOPL, "app_mode", &gAPPStartMode);
523 applyConfig(themeID, langID);
526 lscret = result;
527 lscstatus = 0;
530 static void _saveConfig() {
531 if (lscstatus & CONFIG_OPL) {
532 config_set_t *configOPL = configGetByType(CONFIG_OPL);
533 configSetInt(configOPL, "scrolling", gScrollSpeed);
534 configSetStr(configOPL, "theme", thmGetValue());
535 configSetStr(configOPL, "language_text", lngGetValue());
536 configSetColor(configOPL, "bg_color", gDefaultBgColor);
537 configSetColor(configOPL, "text_color", gDefaultTextColor);
538 configSetColor(configOPL, "ui_text_color", gDefaultUITextColor);
539 configSetColor(configOPL, "sel_text_color", gDefaultSelTextColor);
540 configSetInt(configOPL, "use_info_screen", gUseInfoScreen);
541 configSetInt(configOPL, "enable_coverart", gEnableArt);
542 configSetInt(configOPL, "wide_screen", gWideScreen);
543 configSetInt(configOPL, "vmode", gVMode);
544 configSetInt(configOPL, "vsync", gVSync);
546 char temp[255];
547 sprintf(temp, "%d.%d.%d.%d", pc_ip[0], pc_ip[1], pc_ip[2], pc_ip[3]);
548 configSetStr(configOPL, "pc_ip", temp);
549 configSetInt(configOPL, "pc_port", gPCPort);
550 configSetStr(configOPL, "pc_share", gPCShareName);
551 configSetStr(configOPL, "pc_user", gPCUserName);
552 configSetStr(configOPL, "pc_pass", gPCPassword);
553 configSetStr(configOPL, "exit_path", gExitPath);
554 configSetInt(configOPL, "autosort", gAutosort);
555 configSetInt(configOPL, "autorefresh", gAutoRefresh);
556 configSetInt(configOPL, "default_device", gDefaultDevice);
557 configSetInt(configOPL, "disable_debug", gDisableDebug);
558 configSetInt(configOPL, "enable_delete_rename", gEnableDandR);
559 configSetInt(configOPL, "hdd_spindown", gHDDSpindown);
560 configSetInt(configOPL, "check_usb_frag", gCheckUSBFragmentation);
561 configSetInt(configOPL, "usb_delay", gUSBDelay);
562 configSetStr(configOPL, "usb_prefix", gUSBPrefix);
563 configSetStr(configOPL, "eth_prefix", gETHPrefix);
564 configSetInt(configOPL, "remember_last", gRememberLastPlayed);
565 configSetInt(configOPL, "usb_mode", gUSBStartMode);
566 configSetInt(configOPL, "hdd_mode", gHDDStartMode);
567 configSetInt(configOPL, "eth_mode", gETHStartMode);
568 configSetInt(configOPL, "app_mode", gAPPStartMode);
571 lscret = configWriteMulti(lscstatus);
572 lscstatus = 0;
575 void applyConfig(int themeID, int langID) {
576 infotxt = _l(_STR_WELCOME);
578 if (gDefaultDevice < 0 || gDefaultDevice > APP_MODE)
579 gDefaultDevice = APP_MODE;
581 guiUpdateScrollSpeed();
582 guiUpdateScreenScale();
584 if (gAutoRefresh)
585 guiSetFrameHook(&menuUpdateHook);
586 else
587 guiSetFrameHook(NULL);
589 int changed = rmSetMode(0);
590 if (changed) {
591 // reinit the graphics...
592 thmReloadScreenExtents();
593 guiReloadScreenExtents();
596 // theme must be set after color, and lng after theme
597 changed = thmSetGuiValue(themeID, changed);
598 if (langID != -1)
599 lngSetGuiValue(langID);
601 initAllSupport(0);
603 moduleUpdateMenu(USB_MODE, changed);
604 moduleUpdateMenu(ETH_MODE, changed);
605 moduleUpdateMenu(HDD_MODE, changed);
606 moduleUpdateMenu(APP_MODE, changed);
609 int loadConfig(int types) {
610 lscstatus = types;
611 lscret = 0;
613 guiHandleDeferedIO(&lscstatus, _l(_STR_LOADING_SETTINGS), IO_CUSTOM_SIMPLEACTION, &_loadConfig);
615 return lscret;
618 int saveConfig(int types, int showUI) {
619 lscstatus = types;
620 lscret = 0;
622 guiHandleDeferedIO(&lscstatus, _l(_STR_SAVING_SETTINGS), IO_CUSTOM_SIMPLEACTION, &_saveConfig);
624 if (showUI) {
625 if (lscret)
626 guiMsgBox(_l(_STR_SETTINGS_SAVED), 0, NULL);
627 else
628 guiMsgBox(_l(_STR_ERROR_SAVING_SETTINGS), 0, NULL);
631 return lscret;
634 // ----------------------------------------------------------
635 // -------------------- HD SRV Support ----------------------
636 // ----------------------------------------------------------
637 extern void *ps2dev9_irx;
638 extern int size_ps2dev9_irx;
640 extern void *smsutils_irx;
641 extern int size_smsutils_irx;
643 extern void *smstcpip_irx;
644 extern int size_smstcpip_irx;
646 extern void *smsmap_irx;
647 extern int size_smsmap_irx;
649 extern void *ps2atad_irx;
650 extern int size_ps2atad_irx;
652 extern void *ps2hdd_irx;
653 extern int size_ps2hdd_irx;
655 extern void *hdldsvr_irx;
656 extern int size_hdldsvr_irx;
658 void loadHdldSvr(void) {
659 int ret;
660 static char hddarg[] = "-o" "\0" "4" "\0" "-n" "\0" "20";
661 char ipconfig[IPCONFIG_MAX_LEN] __attribute__((aligned(64)));
663 // block all io ops, wait for the ones still running to finish
664 ioBlockOps(1);
666 deinitAllSupport(NO_EXCEPTION);
668 unloadPads();
670 sysReset(SYS_LOAD_PAD_MODULES);
672 int iplen = sysSetIPConfig(ipconfig);
674 ret = sysLoadModuleBuffer(&ps2dev9_irx, size_ps2dev9_irx, 0, NULL);
675 if (ret < 0)
676 return;
678 ret = sysLoadModuleBuffer(&smsutils_irx, size_smsutils_irx, 0, NULL);
679 if (ret < 0)
680 return;
682 ret = sysLoadModuleBuffer(&smstcpip_irx, size_smstcpip_irx, 0, NULL);
683 if (ret < 0)
684 return;
686 ret = sysLoadModuleBuffer(&smsmap_irx, size_smsmap_irx, iplen, ipconfig);
687 if (ret < 0)
688 return;
690 ret = sysLoadModuleBuffer(&ps2atad_irx, size_ps2atad_irx, 0, NULL);
691 if (ret < 0)
692 return;
694 ret = sysLoadModuleBuffer(&ps2hdd_irx, size_ps2hdd_irx, sizeof(hddarg), hddarg);
695 if (ret < 0)
696 return;
698 ret = sysLoadModuleBuffer(&hdldsvr_irx, size_hdldsvr_irx, 0, NULL);
699 if (ret < 0)
700 return;
702 padInit(0);
704 // init all pads
705 ret = 0;
706 while (!ret)
707 ret = startPads();
709 // now ready to display some status
712 void unloadHdldSvr(void) {
713 unloadPads();
715 sysReset(SYS_LOAD_MC_MODULES | SYS_LOAD_PAD_MODULES);
717 LOG_INIT();
718 LOG_ENABLE();
720 mcInit(MC_TYPE_MC);
722 // reinit the input pads
723 padInit(0);
725 int ret = 0;
726 while (!ret)
727 ret = startPads();
729 // now start io again
730 ioBlockOps(0);
732 // init all supports again
733 initAllSupport(1);
736 void handleHdlSrv() {
737 guiRenderTextScreen(_l(_STR_STARTINGHDL));
739 // prepare for hdl, display screen with info
740 loadHdldSvr();
742 int terminate = 0;
744 while (1) {
745 if (terminate != 0)
746 guiRenderTextScreen(_l(_STR_STOPHDL));
747 else
748 guiRenderTextScreen(_l(_STR_RUNNINGHDL));
750 sleep(2);
752 readPads();
754 if(getKeyOn(KEY_CIRCLE) && terminate == 0) {
755 terminate++;
756 } else if(getKeyOn(KEY_CROSS) && terminate == 1) {
757 terminate++;
758 } else if (terminate > 0)
759 terminate--;
761 if (terminate >= 2)
762 break;
765 guiRenderTextScreen(_l(_STR_UNLOADHDL));
767 // restore normal functionality again
768 unloadHdldSvr();
771 // ----------------------------------------------------------
772 // --------------------- Init/Deinit ------------------------
773 // ----------------------------------------------------------
774 static void reset(void) {
775 sysReset(SYS_LOAD_MC_MODULES | SYS_LOAD_PAD_MODULES);
777 SifInitRpc(0);
779 mcInit(MC_TYPE_MC);
782 static void moduleCleanup(opl_io_module_t* mod, int exception) {
783 if (!mod->support)
784 return;
786 if (mod->support->itemCleanUp)
787 mod->support->itemCleanUp(exception);
790 void shutdown(int exception) {
791 unloadPads();
792 ioEnd();
793 guiEnd();
794 menuEnd();
795 lngEnd();
796 thmEnd();
797 rmEnd();
798 configEnd();
800 deinitAllSupport(exception);
804 static void setDefaults(void) {
805 usbd_irx = NULL;
806 size_usbd_irx = 0;
808 clearIOModuleT(&list_support[USB_MODE]);
809 clearIOModuleT(&list_support[ETH_MODE]);
810 clearIOModuleT(&list_support[HDD_MODE]);
811 clearIOModuleT(&list_support[APP_MODE]);
813 gBaseMCDir = "mc?:OPL";
815 ps2_ip[0] = 192; ps2_ip[1] = 168; ps2_ip[2] = 0; ps2_ip[3] = 10;
816 ps2_netmask[0] = 255; ps2_netmask[1] = 255; ps2_netmask[2] = 255; ps2_netmask[3] = 0;
817 ps2_gateway[0] = 192; ps2_gateway[1] = 168; ps2_gateway[2] = 0; ps2_gateway[3] = 1;
818 pc_ip[0] = 192;pc_ip[1] = 168; pc_ip[2] = 0; pc_ip[3] = 2;
819 gPCPort = 445;
820 strncpy(gPCShareName, "", 32);
821 strncpy(gPCUserName, "GUEST", 32);
822 strncpy(gPCPassword, "", 32);
823 gNetworkStartup = ERROR_ETH_NOT_STARTED;
824 gHddStartup = 6;
825 gHDDSpindown = 20;
826 gIPConfigChanged = 0;
827 gScrollSpeed = 1;
828 strncpy(gExitPath, "", 32);
829 gDefaultDevice = APP_MODE;
830 gAutosort = 1;
831 gAutoRefresh = 0;
832 gDisableDebug = 0;
833 gEnableDandR = 0;
834 gRememberLastPlayed = 0;
835 gCheckUSBFragmentation = 1;
836 gUSBDelay = 3;
837 strncpy(gUSBPrefix, "", 32);
838 strncpy(gETHPrefix, "", 32);
839 gUseInfoScreen = 0;
840 gEnableArt = 0;
841 gWideScreen = 0;
843 gUSBStartMode = 0;
844 gHDDStartMode = 0;
845 gETHStartMode = 0;
846 gAPPStartMode = 0;
848 gDefaultBgColor[0] = 0x028;
849 gDefaultBgColor[1] = 0x0c5;
850 gDefaultBgColor[2] = 0x0f9;
852 gDefaultTextColor[0] = 0x0ff;
853 gDefaultTextColor[1] = 0x0ff;
854 gDefaultTextColor[2] = 0x0ff;
856 gDefaultSelTextColor[0] = 0x0ff;
857 gDefaultSelTextColor[1] = 0x080;
858 gDefaultSelTextColor[2] = 0x000;
860 gDefaultUITextColor[0] = 0x040;
861 gDefaultUITextColor[1] = 0x080;
862 gDefaultUITextColor[2] = 0x040;
864 frameCounter = UPDATE_FRAME_COUNT;
866 gVMode = RM_VMODE_AUTO;
867 gVSync = 1;
870 static void init(void) {
871 // default variable values
872 setDefaults();
874 padInit(0);
875 configInit(NULL);
877 rmInit();
878 lngInit();
879 thmInit();
880 guiInit();
881 ioInit();
882 menuInit();
884 startPads();
886 // handler for deffered menu updates
887 ioRegisterHandler(IO_MENU_UPDATE_DEFFERED, &menuDeferredUpdate);
889 // try to restore config
890 _loadConfig();
892 cacheInit();
895 static void deferredInit(void) {
897 // inform GUI main init part is over
898 struct gui_update_t *id = guiOpCreate(GUI_INIT_DONE);
899 guiDeferUpdate(id);
901 if (list_support[gDefaultDevice].support) {
902 id = guiOpCreate(GUI_OP_SELECT_MENU);
903 id->menu.menu = &list_support[gDefaultDevice].menuItem;
904 guiDeferUpdate(id);
908 // --------------------- Main --------------------
909 int main(int argc, char* argv[])
911 LOG_INIT();
912 PREINIT_LOG("OPL GUI start!\n");
914 #ifdef __DEBUG
915 int use_early_debug = 0, exception_test = 0;
916 int i;
917 for (i=1; i<argc; i++) {
918 if (!(strcmp(argv[i], "-use-early-debug"))) {
919 use_early_debug = 1;
920 PREINIT_LOG("OPL Using early debug.\n");
922 if (!(strcmp(argv[i], "-test-exception"))) {
923 exception_test = 1;
924 PREINIT_LOG("OPL Exception test requested.\n");
927 #endif
929 // apply kernel patches
930 sysApplyKernelPatches();
932 // reset, load modules
933 reset();
935 #ifdef __DEBUG
936 if (use_early_debug) {
937 configReadIP();
938 debugSetActive();
941 if (exception_test) {
942 // EXCEPTION test !
943 u32 *p = (u32 *)0xDEADC0DE;
944 *p = 0xDEFACED;
946 #endif
948 init();
950 // until this point in the code is reached, only PREINIT_LOG macro should be used
951 LOG_ENABLE();
953 // queue deffered init which shuts down the intro screen later
954 ioPutRequest(IO_CUSTOM_SIMPLEACTION, &deferredInit);
956 guiIntroLoop();
957 guiMainLoop();
959 return 0;