* reverted some changes from rev500, finally crazyc was correct (when he skipped...
[open-ps2-loader.git] / src / opl.c
blob3b76bb13d4ed296d19b12522ce45c08af2d7250c
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 #define IO_MENU_UPDATE_DEFFERED 2
49 extern void *usbd_irx;
50 extern int size_usbd_irx;
52 typedef struct {
53 item_list_t *support;
55 /// menu item used with this list support
56 menu_item_t menuItem;
58 /// submenu list
59 submenu_list_t *subMenu;
60 } opl_io_module_t;
62 static void clearIOModuleT(opl_io_module_t *mod) {
63 mod->subMenu = NULL;
64 mod->support = NULL;
65 mod->menuItem.execCross = NULL;
66 mod->menuItem.execCircle = NULL;
67 mod->menuItem.execSquare = NULL;
68 mod->menuItem.execTriangle = NULL;
69 mod->menuItem.hints = NULL;
70 mod->menuItem.icon_id = -1;
71 mod->menuItem.current = NULL;
72 mod->menuItem.submenu = NULL;
73 mod->menuItem.pagestart = NULL;
74 mod->menuItem.remindLast = 0;
75 mod->menuItem.refresh = NULL;
76 mod->menuItem.text = NULL;
77 mod->menuItem.text_id = -1;
78 mod->menuItem.userdata = NULL;
81 // forward decl
82 static void moduleCleanup(opl_io_module_t* mod, int exception);
84 // frame counter
85 static int frameCounter;
87 static char errorMessage[255];
89 static opl_io_module_t list_support[4];
91 void moduleUpdateMenu(int mode, int themeChanged) {
92 if (mode == -1)
93 return;
95 opl_io_module_t* mod = &list_support[mode];
97 if (!mod->support)
98 return;
100 // refresh Hints
101 menuRemoveHints(&mod->menuItem);
103 menuAddHint(&mod->menuItem, _STR_SETTINGS, START_ICON);
104 if (!mod->support->enabled)
105 menuAddHint(&mod->menuItem, _STR_START_DEVICE, CROSS_ICON);
106 else {
107 if (gUseInfoScreen && gTheme->infoElems.first)
108 menuAddHint(&mod->menuItem, _STR_INFO, CROSS_ICON);
109 else
110 menuAddHint(&mod->menuItem, _STR_RUN, CROSS_ICON);
111 if (mod->support->haveCompatibilityMode)
112 menuAddHint(&mod->menuItem, _STR_COMPAT_SETTINGS, TRIANGLE_ICON);
113 if (gEnableDandR) {
114 if (mod->support->itemRename)
115 menuAddHint(&mod->menuItem, _STR_RENAME, CIRCLE_ICON);
116 if (mod->support->itemDelete)
117 menuAddHint(&mod->menuItem, _STR_DELETE, SQUARE_ICON);
121 // refresh Cache
122 if (themeChanged)
123 submenuRebuildCache(mod->subMenu);
126 static void itemExecCross(struct menu_item *curMenu) {
127 item_list_t *support = curMenu->userdata;
129 if (support) {
130 if (support->enabled) {
131 if (curMenu->current) {
132 config_set_t* configSet = menuLoadConfig();
133 support->itemLaunch(curMenu->current->item.id, configSet);
136 else {
137 support->itemInit();
138 moduleUpdateMenu(support->mode, 0);
139 if (!gAutoRefresh)
140 ioPutRequest(IO_MENU_UPDATE_DEFFERED, &list_support[support->mode]);
143 else
144 guiMsgBox("NULL Support object. Please report", 0, NULL);
147 static void itemExecTriangle(struct menu_item *curMenu) {
148 if (!curMenu->current)
149 return;
151 item_list_t *support = curMenu->userdata;
153 if (support) {
154 if (support->haveCompatibilityMode) {
155 config_set_t* configSet = menuLoadConfig();
156 if (guiShowCompatConfig(curMenu->current->item.id, support, configSet) == COMPAT_TEST)
157 support->itemLaunch(curMenu->current->item.id, configSet);
160 else
161 guiMsgBox("NULL Support object. Please report", 0, NULL);
164 static void itemExecSquare(struct menu_item *curMenu) {
165 if (!curMenu->current)
166 return;
168 if (!gEnableDandR)
169 return;
171 item_list_t *support = curMenu->userdata;
173 if (support) {
174 if (support->itemDelete) {
175 if (guiMsgBox(_l(_STR_DELETE_WARNING), 1, NULL)) {
176 support->itemDelete(curMenu->current->item.id);
177 if (gAutoRefresh)
178 frameCounter = UPDATE_FRAME_COUNT;
179 else
180 ioPutRequest(IO_MENU_UPDATE_DEFFERED, &list_support[support->mode]);
184 else
185 guiMsgBox("NULL Support object. Please report", 0, NULL);
188 static void itemExecCircle(struct menu_item *curMenu) {
189 if (!curMenu->current)
190 return;
192 if (!gEnableDandR)
193 return;
195 item_list_t *support = curMenu->userdata;
197 if (support) {
198 if (support->itemRename) {
199 int nameLength = support->itemGetNameLength(curMenu->current->item.id);
200 char newName[nameLength];
201 strncpy(newName, curMenu->current->item.text, nameLength);
202 if (guiShowKeyboard(newName, nameLength)) {
203 support->itemRename(curMenu->current->item.id, newName);
204 if (gAutoRefresh)
205 frameCounter = UPDATE_FRAME_COUNT;
206 else
207 ioPutRequest(IO_MENU_UPDATE_DEFFERED, &list_support[support->mode]);
211 else
212 guiMsgBox("NULL Support object. Please report", 0, NULL);
215 static void itemExecRefresh(struct menu_item *curMenu) {
216 item_list_t *support = curMenu->userdata;
218 if (support && support->enabled)
219 ioPutRequest(IO_MENU_UPDATE_DEFFERED, &list_support[support->mode]);
222 static void initMenuForListSupport(int mode) {
223 opl_io_module_t* item = &list_support[mode];
224 item->menuItem.icon_id = item->support->iconId;
225 item->menuItem.text = NULL;
226 item->menuItem.text_id = item->support->textId;
228 item->menuItem.userdata = item->support;
230 item->subMenu = NULL;
232 item->menuItem.submenu = NULL;
233 item->menuItem.current = NULL;
234 item->menuItem.pagestart = NULL;
235 item->menuItem.remindLast = 0;
237 item->menuItem.refresh = &itemExecRefresh;
238 item->menuItem.execCross = &itemExecCross;
239 item->menuItem.execTriangle = &itemExecTriangle;
240 item->menuItem.execSquare = &itemExecSquare;
241 item->menuItem.execCircle = &itemExecCircle;
243 item->menuItem.hints = NULL;
245 moduleUpdateMenu(mode, 0);
247 struct gui_update_t *mc = guiOpCreate(GUI_OP_ADD_MENU);
248 mc->menu.menu = &item->menuItem;
249 mc->menu.subMenu = &item->subMenu;
250 guiDeferUpdate(mc);
253 static void initSupport(item_list_t* itemList, int startMode, int mode, int force_reinit) {
254 if (!list_support[mode].support) {
255 itemList->uip = 1; // stop updates until we're done with init
256 list_support[mode].support = itemList;
257 initMenuForListSupport(mode);
258 itemList->uip = 0;
261 if (((force_reinit) && (startMode && list_support[mode].support->enabled)) \
262 || (startMode == 2 && !list_support[mode].support->enabled)) {
263 // stop updates until we're done with init of the device
264 list_support[mode].support->uip = 1;
265 list_support[mode].support->itemInit();
266 moduleUpdateMenu(mode, 0);
267 list_support[mode].support->uip = 0;
269 if (gAutoRefresh)
270 frameCounter = UPDATE_FRAME_COUNT;
271 else
272 ioPutRequest(IO_MENU_UPDATE_DEFFERED, &list_support[mode]);
276 static void initAllSupport(int force_reinit) {
277 if (gUSBStartMode)
278 initSupport(usbGetObject(0), gUSBStartMode, USB_MODE, force_reinit);
280 if (gETHStartMode)
281 initSupport(ethGetObject(0), gETHStartMode, ETH_MODE, force_reinit||(gNetworkStartup >= ERROR_ETH_SMB_LOGON));
283 if (gHDDStartMode)
284 initSupport(hddGetObject(0), gHDDStartMode, HDD_MODE, force_reinit);
286 if (gAPPStartMode)
287 initSupport(appGetObject(0), gAPPStartMode, APP_MODE, force_reinit);
290 static void deinitAllSupport(int exception) {
291 moduleCleanup(&list_support[USB_MODE], exception);
292 moduleCleanup(&list_support[ETH_MODE], exception);
293 moduleCleanup(&list_support[HDD_MODE], exception);
294 moduleCleanup(&list_support[APP_MODE], exception);
297 // ----------------------------------------------------------
298 // ----------------------- Updaters -------------------------
299 // ----------------------------------------------------------
300 static void updateMenuFromGameList(opl_io_module_t* mdl) {
301 // lock - gui has to be unused here
302 guiLock();
304 submenuDestroy(&mdl->subMenu);
305 mdl->menuItem.submenu = NULL;
306 mdl->menuItem.current = NULL;
307 mdl->menuItem.pagestart = NULL;
308 mdl->menuItem.remindLast = 0;
310 // unlock, the rest is deferred
311 guiUnlock();
313 char* temp = NULL;
314 if (gRememberLastPlayed)
315 configGetStr(configGetByType(CONFIG_LAST), "last_played", &temp);
317 // read the new game list
318 struct gui_update_t *gup = NULL;
319 int count = mdl->support->itemUpdate();
320 if (count) {
321 int i;
323 for (i = 0; i < count; ++i) {
325 gup = guiOpCreate(GUI_OP_APPEND_MENU);
327 gup->menu.menu = &mdl->menuItem;
328 gup->menu.subMenu = &mdl->subMenu;
330 gup->submenu.icon_id = -1;
331 gup->submenu.id = i;
332 gup->submenu.text = mdl->support->itemGetName(i);
333 gup->submenu.text_id = -1;
334 gup->submenu.selected = 0;
336 if (gRememberLastPlayed && temp && strcmp(temp, mdl->support->itemGetStartup(i)) == 0)
337 gup->submenu.selected = 1;
339 guiDeferUpdate(gup);
343 if (gAutosort) {
344 gup = guiOpCreate(GUI_OP_SORT);
345 gup->menu.menu = &mdl->menuItem;
346 gup->menu.subMenu = &mdl->subMenu;
347 guiDeferUpdate(gup);
351 static void menuDeferredUpdate(void* data) {
352 opl_io_module_t* mdl = data;
354 if (!mdl->support)
355 return;
357 if (mdl->support->uip)
358 return;
360 // see if we have to update
361 if (mdl->support->itemNeedsUpdate()) {
362 mdl->support->uip = 1;
364 updateMenuFromGameList(mdl);
366 mdl->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]);
380 if (list_support[ETH_MODE].support && list_support[ETH_MODE].support->enabled)
381 ioPutRequest(IO_MENU_UPDATE_DEFFERED, &list_support[ETH_MODE]);
382 if (list_support[HDD_MODE].support && list_support[HDD_MODE].support->enabled)
383 ioPutRequest(IO_MENU_UPDATE_DEFFERED, &list_support[HDD_MODE]);
384 if (list_support[APP_MODE].support && list_support[APP_MODE].support->enabled)
385 ioPutRequest(IO_MENU_UPDATE_DEFFERED, &list_support[APP_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 configGetInt(configOPL, "remember_last", &gRememberLastPlayed);
515 configGetInt(configOPL, "usb_mode", &gUSBStartMode);
516 configGetInt(configOPL, "hdd_mode", &gHDDStartMode);
517 configGetInt(configOPL, "eth_mode", &gETHStartMode);
518 configGetInt(configOPL, "app_mode", &gAPPStartMode);
521 applyConfig(themeID, langID);
524 lscret = result;
525 lscstatus = 0;
528 static void _saveConfig() {
529 if (lscstatus & CONFIG_OPL) {
530 config_set_t *configOPL = configGetByType(CONFIG_OPL);
531 configSetInt(configOPL, "scrolling", gScrollSpeed);
532 configSetStr(configOPL, "theme", thmGetValue());
533 configSetStr(configOPL, "language_text", lngGetValue());
534 configSetColor(configOPL, "bg_color", gDefaultBgColor);
535 configSetColor(configOPL, "text_color", gDefaultTextColor);
536 configSetColor(configOPL, "ui_text_color", gDefaultUITextColor);
537 configSetColor(configOPL, "sel_text_color", gDefaultSelTextColor);
538 configSetInt(configOPL, "use_info_screen", gUseInfoScreen);
539 configSetInt(configOPL, "enable_coverart", gEnableArt);
540 configSetInt(configOPL, "wide_screen", gWideScreen);
541 configSetInt(configOPL, "vmode", gVMode);
542 configSetInt(configOPL, "vsync", gVSync);
544 char temp[255];
545 sprintf(temp, "%d.%d.%d.%d", pc_ip[0], pc_ip[1], pc_ip[2], pc_ip[3]);
546 configSetStr(configOPL, "pc_ip", temp);
547 configSetInt(configOPL, "pc_port", gPCPort);
548 configSetStr(configOPL, "pc_share", gPCShareName);
549 configSetStr(configOPL, "pc_user", gPCUserName);
550 configSetStr(configOPL, "pc_pass", gPCPassword);
551 configSetStr(configOPL, "exit_path", gExitPath);
552 configSetInt(configOPL, "autosort", gAutosort);
553 configSetInt(configOPL, "autorefresh", gAutoRefresh);
554 configSetInt(configOPL, "default_device", gDefaultDevice);
555 configSetInt(configOPL, "disable_debug", gDisableDebug);
556 configSetInt(configOPL, "enable_delete_rename", gEnableDandR);
557 configSetInt(configOPL, "hdd_spindown", gHDDSpindown);
558 configSetInt(configOPL, "check_usb_frag", gCheckUSBFragmentation);
559 configSetInt(configOPL, "usb_delay", gUSBDelay);
560 configSetStr(configOPL, "usb_prefix", gUSBPrefix);
561 configSetInt(configOPL, "remember_last", gRememberLastPlayed);
562 configSetInt(configOPL, "usb_mode", gUSBStartMode);
563 configSetInt(configOPL, "hdd_mode", gHDDStartMode);
564 configSetInt(configOPL, "eth_mode", gETHStartMode);
565 configSetInt(configOPL, "app_mode", gAPPStartMode);
568 lscret = configWriteMulti(lscstatus);
569 lscstatus = 0;
572 void applyConfig(int themeID, int langID) {
573 infotxt = _l(_STR_WELCOME);
575 if (gDefaultDevice < 0 || gDefaultDevice > APP_MODE)
576 gDefaultDevice = APP_MODE;
578 guiUpdateScrollSpeed();
579 guiUpdateScreenScale();
581 int changed = rmSetMode(0);
582 if (changed) {
583 // reinit the graphics...
584 thmReloadScreenExtents();
585 guiReloadScreenExtents();
588 // theme must be set after color, and lng after theme
589 changed = thmSetGuiValue(themeID, changed);
590 if (langID != -1)
591 lngSetGuiValue(langID);
593 initAllSupport(0);
595 moduleUpdateMenu(USB_MODE, changed);
596 moduleUpdateMenu(ETH_MODE, changed);
597 moduleUpdateMenu(HDD_MODE, changed);
598 moduleUpdateMenu(APP_MODE, changed);
600 if (gAutoRefresh)
601 guiSetFrameHook(&menuUpdateHook);
602 else
603 guiSetFrameHook(NULL);
606 int loadConfig(int types) {
607 lscstatus = types;
608 lscret = 0;
610 guiHandleDeferedIO(&lscstatus, _l(_STR_LOADING_SETTINGS), IO_CUSTOM_SIMPLEACTION, &_loadConfig);
612 return lscret;
615 int saveConfig(int types, int showUI) {
616 lscstatus = types;
617 lscret = 0;
619 guiHandleDeferedIO(&lscstatus, _l(_STR_SAVING_SETTINGS), IO_CUSTOM_SIMPLEACTION, &_saveConfig);
621 if (showUI) {
622 if (lscret)
623 guiMsgBox(_l(_STR_SETTINGS_SAVED), 0, NULL);
624 else
625 guiMsgBox(_l(_STR_ERROR_SAVING_SETTINGS), 0, NULL);
628 return lscret;
631 // ----------------------------------------------------------
632 // -------------------- HD SRV Support ----------------------
633 // ----------------------------------------------------------
634 extern void *ps2dev9_irx;
635 extern int size_ps2dev9_irx;
637 extern void *smsutils_irx;
638 extern int size_smsutils_irx;
640 extern void *smstcpip_irx;
641 extern int size_smstcpip_irx;
643 extern void *smsmap_irx;
644 extern int size_smsmap_irx;
646 extern void *ps2atad_irx;
647 extern int size_ps2atad_irx;
649 extern void *ps2hdd_irx;
650 extern int size_ps2hdd_irx;
652 extern void *hdldsvr_irx;
653 extern int size_hdldsvr_irx;
655 void loadHdldSvr(void) {
656 int ret;
657 static char hddarg[] = "-o" "\0" "4" "\0" "-n" "\0" "20";
658 char ipconfig[IPCONFIG_MAX_LEN] __attribute__((aligned(64)));
660 // block all io ops, wait for the ones still running to finish
661 ioBlockOps(1);
663 deinitAllSupport(NO_EXCEPTION);
665 unloadPads();
667 sysReset(SYS_LOAD_PAD_MODULES);
669 int iplen = sysSetIPConfig(ipconfig);
671 ret = sysLoadModuleBuffer(&ps2dev9_irx, size_ps2dev9_irx, 0, NULL);
672 if (ret < 0)
673 return;
675 ret = sysLoadModuleBuffer(&smsutils_irx, size_smsutils_irx, 0, NULL);
676 if (ret < 0)
677 return;
679 ret = sysLoadModuleBuffer(&smstcpip_irx, size_smstcpip_irx, 0, NULL);
680 if (ret < 0)
681 return;
683 ret = sysLoadModuleBuffer(&smsmap_irx, size_smsmap_irx, iplen, ipconfig);
684 if (ret < 0)
685 return;
687 ret = sysLoadModuleBuffer(&ps2atad_irx, size_ps2atad_irx, 0, NULL);
688 if (ret < 0)
689 return;
691 ret = sysLoadModuleBuffer(&ps2hdd_irx, size_ps2hdd_irx, sizeof(hddarg), hddarg);
692 if (ret < 0)
693 return;
695 ret = sysLoadModuleBuffer(&hdldsvr_irx, size_hdldsvr_irx, 0, NULL);
696 if (ret < 0)
697 return;
699 padInit(0);
701 // init all pads
702 ret = 0;
703 while (!ret)
704 ret = startPads();
706 // now ready to display some status
709 void unloadHdldSvr(void) {
710 unloadPads();
712 sysReset(SYS_LOAD_MC_MODULES | SYS_LOAD_PAD_MODULES);
714 LOG_INIT();
715 LOG_ENABLE();
717 mcInit(MC_TYPE_MC);
719 // reinit the input pads
720 padInit(0);
722 int ret = 0;
723 while (!ret)
724 ret = startPads();
726 // now start io again
727 ioBlockOps(0);
729 // init all supports again
730 initAllSupport(1);
733 void handleHdlSrv() {
734 guiRenderTextScreen(_l(_STR_STARTINGHDL));
736 // prepare for hdl, display screen with info
737 loadHdldSvr();
739 int terminate = 0;
741 while (1) {
742 if (terminate != 0)
743 guiRenderTextScreen(_l(_STR_STOPHDL));
744 else
745 guiRenderTextScreen(_l(_STR_RUNNINGHDL));
747 sleep(2);
749 readPads();
751 if(getKeyOn(KEY_CIRCLE) && terminate == 0) {
752 terminate++;
753 } else if(getKeyOn(KEY_CROSS) && terminate == 1) {
754 terminate++;
755 } else if (terminate > 0)
756 terminate--;
758 if (terminate >= 2)
759 break;
762 guiRenderTextScreen(_l(_STR_UNLOADHDL));
764 // restore normal functionality again
765 unloadHdldSvr();
768 // ----------------------------------------------------------
769 // --------------------- Init/Deinit ------------------------
770 // ----------------------------------------------------------
771 static void reset(void) {
772 sysReset(SYS_LOAD_MC_MODULES | SYS_LOAD_PAD_MODULES);
774 SifInitRpc(0);
776 mcInit(MC_TYPE_MC);
779 static void moduleCleanup(opl_io_module_t* mod, int exception) {
780 if (!mod->support)
781 return;
783 if (mod->support->itemCleanUp)
784 mod->support->itemCleanUp(exception);
787 void shutdown(int exception) {
788 unloadPads();
789 ioEnd();
790 guiEnd();
791 menuEnd();
792 lngEnd();
793 thmEnd();
794 rmEnd();
795 configEnd();
797 deinitAllSupport(exception);
801 static void setDefaults(void) {
802 usbd_irx = NULL;
803 size_usbd_irx = 0;
805 clearIOModuleT(&list_support[USB_MODE]);
806 clearIOModuleT(&list_support[ETH_MODE]);
807 clearIOModuleT(&list_support[HDD_MODE]);
808 clearIOModuleT(&list_support[APP_MODE]);
810 gBaseMCDir = "mc?:OPL";
812 ps2_ip[0] = 192; ps2_ip[1] = 168; ps2_ip[2] = 0; ps2_ip[3] = 10;
813 ps2_netmask[0] = 255; ps2_netmask[1] = 255; ps2_netmask[2] = 255; ps2_netmask[3] = 0;
814 ps2_gateway[0] = 192; ps2_gateway[1] = 168; ps2_gateway[2] = 0; ps2_gateway[3] = 1;
815 pc_ip[0] = 192;pc_ip[1] = 168; pc_ip[2] = 0; pc_ip[3] = 2;
816 gPCPort = 445;
817 strncpy(gPCShareName, "PS2SMB", 32);
818 strncpy(gPCUserName, "GUEST", 32);
819 strncpy(gPCPassword, "", 32);
820 gNetworkStartup = ERROR_ETH_NOT_STARTED;
821 gHddStartup = 6;
822 gHDDSpindown = 20;
823 gIPConfigChanged = 0;
824 gScrollSpeed = 1;
825 strncpy(gExitPath, "", 32);
826 gDefaultDevice = APP_MODE;
827 gAutosort = 1;
828 gAutoRefresh = 1;
829 gDisableDebug = 0;
830 gEnableDandR = 0;
831 gRememberLastPlayed = 0;
832 gCheckUSBFragmentation = 1;
833 gUSBDelay = 3;
834 strncpy(gUSBPrefix, "", 32);
835 gUseInfoScreen = 0;
836 gEnableArt = 0;
837 gWideScreen = 0;
839 gUSBStartMode = 0;
840 gHDDStartMode = 0;
841 gETHStartMode = 0;
842 gAPPStartMode = 0;
844 gDefaultBgColor[0] = 0x028;
845 gDefaultBgColor[1] = 0x0c5;
846 gDefaultBgColor[2] = 0x0f9;
848 gDefaultTextColor[0] = 0x0ff;
849 gDefaultTextColor[1] = 0x0ff;
850 gDefaultTextColor[2] = 0x0ff;
852 gDefaultSelTextColor[0] = 0x0ff;
853 gDefaultSelTextColor[1] = 0x080;
854 gDefaultSelTextColor[2] = 0x000;
856 gDefaultUITextColor[0] = 0x040;
857 gDefaultUITextColor[1] = 0x080;
858 gDefaultUITextColor[2] = 0x040;
860 frameCounter = UPDATE_FRAME_COUNT;
862 gVMode = RM_VMODE_AUTO;
863 gVSync = 1;
866 static void init(void) {
867 // default variable values
868 setDefaults();
870 padInit(0);
871 configInit(NULL);
873 rmInit();
874 lngInit();
875 thmInit();
876 guiInit();
877 ioInit();
878 menuInit();
880 startPads();
882 // handler for deffered menu updates
883 ioRegisterHandler(IO_MENU_UPDATE_DEFFERED, &menuDeferredUpdate);
885 // try to restore config
886 _loadConfig();
888 cacheInit();
891 static void deferredInit(void) {
893 // inform GUI main init part is over
894 struct gui_update_t *id = guiOpCreate(GUI_INIT_DONE);
895 guiDeferUpdate(id);
897 if (list_support[gDefaultDevice].support) {
898 id = guiOpCreate(GUI_OP_SELECT_MENU);
899 id->menu.menu = &list_support[gDefaultDevice].menuItem;
900 guiDeferUpdate(id);
904 // --------------------- Main --------------------
905 int main(int argc, char* argv[])
907 LOG_INIT();
908 PREINIT_LOG("OPL GUI start!\n");
910 #ifdef __DEBUG
911 int use_early_debug = 0, exception_test = 0;
912 int i;
913 for (i=1; i<argc; i++) {
914 if (!(strcmp(argv[i], "-use-early-debug"))) {
915 use_early_debug = 1;
916 PREINIT_LOG("Using early debug.\n");
918 if (!(strcmp(argv[i], "-test-exception"))) {
919 exception_test = 1;
920 PREINIT_LOG("Exception test requested.\n");
923 #endif
925 // apply kernel patches
926 sysApplyKernelPatches();
928 // reset, load modules
929 reset();
931 #ifdef __DEBUG
932 if (use_early_debug) {
933 configReadIP();
934 debugSetActive();
937 if (exception_test) {
938 // EXCEPTION test !
939 u32 *p = (u32 *)0xDEADC0DE;
940 *p = 0xDEFACED;
942 #endif
944 init();
946 // until this point in the code is reached, only PREINIT_LOG macro should be used
947 LOG_ENABLE();
949 // queue deffered init which shuts down the intro screen later
950 ioPutRequest(IO_CUSTOM_SIMPLEACTION, &deferredInit);
952 guiIntroLoop();
953 guiMainLoop();
955 return 0;