* don't display a share connection error when listing share
[open-ps2-loader.git] / src / opl.c
blob35dcbd1ca3f7c8a4a9a150147d6bd6657cf239e7
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, &mode);
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 return;
360 // see if we have to update
361 if (mod->support->itemNeedsUpdate()) {
362 mod->support->uip = 1;
363 updateMenuFromGameList(mod);
364 mod->support->uip = 0;
368 static void menuUpdateHook() {
369 // if timer exceeds some threshold, schedule updates of the available input sources
370 frameCounter++;
372 if (frameCounter > UPDATE_FRAME_COUNT) {
373 frameCounter = 0;
375 // schedule updates of all the list handlers
376 if (list_support[USB_MODE].support && list_support[USB_MODE].support->enabled)
377 ioPutRequest(IO_MENU_UPDATE_DEFFERED, &list_support[USB_MODE].support->mode);
378 if (list_support[ETH_MODE].support && list_support[ETH_MODE].support->enabled)
379 ioPutRequest(IO_MENU_UPDATE_DEFFERED, &list_support[ETH_MODE].support->mode);
380 if (list_support[HDD_MODE].support && list_support[HDD_MODE].support->enabled)
381 ioPutRequest(IO_MENU_UPDATE_DEFFERED, &list_support[HDD_MODE].support->mode);
382 if (list_support[APP_MODE].support && list_support[APP_MODE].support->enabled)
383 ioPutRequest(IO_MENU_UPDATE_DEFFERED, &list_support[APP_MODE].support->mode);
387 static void errorMessageHook() {
388 guiMsgBox(errorMessage, 0, NULL);
390 // reset the original frame hook
391 frameCounter = 0;
392 if (gAutoRefresh)
393 guiSetFrameHook(&menuUpdateHook);
394 else
395 guiSetFrameHook(NULL);
398 void setErrorMessage(int strId, int error) {
399 snprintf(errorMessage, 255, _l(strId), error);
400 guiSetFrameHook(&errorMessageHook);
403 // ----------------------------------------------------------
404 // ------------------ Configuration handling ----------------
405 // ----------------------------------------------------------
407 static int lscstatus = CONFIG_ALL;
408 static int lscret = 0;
410 static int tryAlternateDevice(int types) {
411 char path[64];
412 int value;
414 // check USB
415 usbLoadModules();
416 if (usbFindPartition(path, "conf_opl.cfg")) {
417 configEnd();
418 configInit(path);
419 value = configReadMulti(types);
420 config_set_t *configOPL = configGetByType(CONFIG_OPL);
421 configSetInt(configOPL, "usb_mode", 2);
422 return value;
425 // check HDD
426 hddLoadModules();
427 sprintf(path, "pfs0:conf_opl.cfg");
428 value = fioOpen(path, O_RDONLY);
429 if(value >= 0) {
430 fioClose(value);
431 configEnd();
432 configInit("pfs0:");
433 value = configReadMulti(types);
434 config_set_t *configOPL = configGetByType(CONFIG_OPL);
435 configSetInt(configOPL, "hdd_mode", 2);
436 return value;
439 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)
440 // set config path to either mass or hdd, to prepare the saving of a new config
441 value = fioDopen("mass0:");
442 if (value >= 0) {
443 fioDclose(value);
444 configEnd();
445 configInit("mass0:");
447 else {
448 configEnd();
449 configInit("pfs0:");
453 return 0;
456 static void _loadConfig() {
457 int result = configReadMulti(lscstatus);
459 if (lscstatus & CONFIG_OPL) {
460 int themeID = -1, langID = -1;
462 if (!(result & CONFIG_OPL)) {
463 result = tryAlternateDevice(lscstatus);
466 if (result & CONFIG_OPL) {
467 config_set_t *configOPL = configGetByType(CONFIG_OPL);
468 char *temp;
470 configGetInt(configOPL, "scrolling", &gScrollSpeed);
471 configGetColor(configOPL, "bg_color", gDefaultBgColor);
472 configGetColor(configOPL, "text_color", gDefaultTextColor);
473 configGetColor(configOPL, "ui_text_color", gDefaultUITextColor);
474 configGetColor(configOPL, "sel_text_color", gDefaultSelTextColor);
475 configGetInt(configOPL, "use_info_screen", &gUseInfoScreen);
476 configGetInt(configOPL, "enable_coverart", &gEnableArt);
477 configGetInt(configOPL, "wide_screen", &gWideScreen);
478 configGetInt(configOPL, "vsync", &gVSync);
479 configGetInt(configOPL, "vmode", &gVMode);
482 if (configGetStr(configOPL, "theme", &temp))
483 themeID = thmFindGuiID(temp);
485 if (configGetStr(configOPL, "language_text", &temp))
486 langID = lngFindGuiID(temp);
488 if (configGetStr(configOPL, "pc_ip", &temp))
489 sscanf(temp, "%d.%d.%d.%d", &pc_ip[0], &pc_ip[1], &pc_ip[2], &pc_ip[3]);
491 configGetInt(configOPL, "pc_port", &gPCPort);
493 if (configGetStr(configOPL, "pc_share", &temp))
494 strncpy(gPCShareName, temp, 32);
495 if (configGetStr(configOPL, "pc_user", &temp))
496 strncpy(gPCUserName, temp, 32);
497 if (configGetStr(configOPL, "pc_pass", &temp))
498 strncpy(gPCPassword, temp, 32);
499 if (configGetStr(configOPL, "exit_path", &temp))
500 strncpy(gExitPath, temp, 32);
502 configGetInt(configOPL, "autosort", &gAutosort);
503 configGetInt(configOPL, "autorefresh", &gAutoRefresh);
504 configGetInt(configOPL, "default_device", &gDefaultDevice);
505 configGetInt(configOPL, "disable_debug", &gDisableDebug);
506 configGetInt(configOPL, "enable_delete_rename", &gEnableDandR);
507 configGetInt(configOPL, "hdd_spindown", &gHDDSpindown);
508 configGetInt(configOPL, "check_usb_frag", &gCheckUSBFragmentation);
509 configGetInt(configOPL, "usb_delay", &gUSBDelay);
510 if (configGetStr(configOPL, "usb_prefix", &temp))
511 strncpy(gUSBPrefix, temp, 32);
512 if (configGetStr(configOPL, "eth_prefix", &temp))
513 strncpy(gETHPrefix, 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 configSetStr(configOPL, "eth_prefix", gETHPrefix);
562 configSetInt(configOPL, "remember_last", gRememberLastPlayed);
563 configSetInt(configOPL, "usb_mode", gUSBStartMode);
564 configSetInt(configOPL, "hdd_mode", gHDDStartMode);
565 configSetInt(configOPL, "eth_mode", gETHStartMode);
566 configSetInt(configOPL, "app_mode", gAPPStartMode);
569 lscret = configWriteMulti(lscstatus);
570 lscstatus = 0;
573 void applyConfig(int themeID, int langID) {
574 infotxt = _l(_STR_WELCOME);
576 if (gDefaultDevice < 0 || gDefaultDevice > APP_MODE)
577 gDefaultDevice = APP_MODE;
579 guiUpdateScrollSpeed();
580 guiUpdateScreenScale();
582 if (gAutoRefresh)
583 guiSetFrameHook(&menuUpdateHook);
584 else
585 guiSetFrameHook(NULL);
587 int changed = rmSetMode(0);
588 if (changed) {
589 // reinit the graphics...
590 thmReloadScreenExtents();
591 guiReloadScreenExtents();
594 // theme must be set after color, and lng after theme
595 changed = thmSetGuiValue(themeID, changed);
596 if (langID != -1)
597 lngSetGuiValue(langID);
599 initAllSupport(0);
601 moduleUpdateMenu(USB_MODE, changed);
602 moduleUpdateMenu(ETH_MODE, changed);
603 moduleUpdateMenu(HDD_MODE, changed);
604 moduleUpdateMenu(APP_MODE, changed);
607 int loadConfig(int types) {
608 lscstatus = types;
609 lscret = 0;
611 guiHandleDeferedIO(&lscstatus, _l(_STR_LOADING_SETTINGS), IO_CUSTOM_SIMPLEACTION, &_loadConfig);
613 return lscret;
616 int saveConfig(int types, int showUI) {
617 lscstatus = types;
618 lscret = 0;
620 guiHandleDeferedIO(&lscstatus, _l(_STR_SAVING_SETTINGS), IO_CUSTOM_SIMPLEACTION, &_saveConfig);
622 if (showUI) {
623 if (lscret)
624 guiMsgBox(_l(_STR_SETTINGS_SAVED), 0, NULL);
625 else
626 guiMsgBox(_l(_STR_ERROR_SAVING_SETTINGS), 0, NULL);
629 return lscret;
632 // ----------------------------------------------------------
633 // -------------------- HD SRV Support ----------------------
634 // ----------------------------------------------------------
635 extern void *ps2dev9_irx;
636 extern int size_ps2dev9_irx;
638 extern void *smsutils_irx;
639 extern int size_smsutils_irx;
641 extern void *smstcpip_irx;
642 extern int size_smstcpip_irx;
644 extern void *smsmap_irx;
645 extern int size_smsmap_irx;
647 extern void *ps2atad_irx;
648 extern int size_ps2atad_irx;
650 extern void *ps2hdd_irx;
651 extern int size_ps2hdd_irx;
653 extern void *hdldsvr_irx;
654 extern int size_hdldsvr_irx;
656 void loadHdldSvr(void) {
657 int ret;
658 static char hddarg[] = "-o" "\0" "4" "\0" "-n" "\0" "20";
659 char ipconfig[IPCONFIG_MAX_LEN] __attribute__((aligned(64)));
661 // block all io ops, wait for the ones still running to finish
662 ioBlockOps(1);
664 deinitAllSupport(NO_EXCEPTION);
666 unloadPads();
668 sysReset(SYS_LOAD_PAD_MODULES);
670 int iplen = sysSetIPConfig(ipconfig);
672 ret = sysLoadModuleBuffer(&ps2dev9_irx, size_ps2dev9_irx, 0, NULL);
673 if (ret < 0)
674 return;
676 ret = sysLoadModuleBuffer(&smsutils_irx, size_smsutils_irx, 0, NULL);
677 if (ret < 0)
678 return;
680 ret = sysLoadModuleBuffer(&smstcpip_irx, size_smstcpip_irx, 0, NULL);
681 if (ret < 0)
682 return;
684 ret = sysLoadModuleBuffer(&smsmap_irx, size_smsmap_irx, iplen, ipconfig);
685 if (ret < 0)
686 return;
688 ret = sysLoadModuleBuffer(&ps2atad_irx, size_ps2atad_irx, 0, NULL);
689 if (ret < 0)
690 return;
692 ret = sysLoadModuleBuffer(&ps2hdd_irx, size_ps2hdd_irx, sizeof(hddarg), hddarg);
693 if (ret < 0)
694 return;
696 ret = sysLoadModuleBuffer(&hdldsvr_irx, size_hdldsvr_irx, 0, NULL);
697 if (ret < 0)
698 return;
700 padInit(0);
702 // init all pads
703 ret = 0;
704 while (!ret)
705 ret = startPads();
707 // now ready to display some status
710 void unloadHdldSvr(void) {
711 unloadPads();
713 sysReset(SYS_LOAD_MC_MODULES | SYS_LOAD_PAD_MODULES);
715 LOG_INIT();
716 LOG_ENABLE();
718 mcInit(MC_TYPE_MC);
720 // reinit the input pads
721 padInit(0);
723 int ret = 0;
724 while (!ret)
725 ret = startPads();
727 // now start io again
728 ioBlockOps(0);
730 // init all supports again
731 initAllSupport(1);
734 void handleHdlSrv() {
735 guiRenderTextScreen(_l(_STR_STARTINGHDL));
737 // prepare for hdl, display screen with info
738 loadHdldSvr();
740 int terminate = 0;
742 while (1) {
743 if (terminate != 0)
744 guiRenderTextScreen(_l(_STR_STOPHDL));
745 else
746 guiRenderTextScreen(_l(_STR_RUNNINGHDL));
748 sleep(2);
750 readPads();
752 if(getKeyOn(KEY_CIRCLE) && terminate == 0) {
753 terminate++;
754 } else if(getKeyOn(KEY_CROSS) && terminate == 1) {
755 terminate++;
756 } else if (terminate > 0)
757 terminate--;
759 if (terminate >= 2)
760 break;
763 guiRenderTextScreen(_l(_STR_UNLOADHDL));
765 // restore normal functionality again
766 unloadHdldSvr();
769 // ----------------------------------------------------------
770 // --------------------- Init/Deinit ------------------------
771 // ----------------------------------------------------------
772 static void reset(void) {
773 sysReset(SYS_LOAD_MC_MODULES | SYS_LOAD_PAD_MODULES);
775 SifInitRpc(0);
777 mcInit(MC_TYPE_MC);
780 static void moduleCleanup(opl_io_module_t* mod, int exception) {
781 if (!mod->support)
782 return;
784 if (mod->support->itemCleanUp)
785 mod->support->itemCleanUp(exception);
788 void shutdown(int exception) {
789 unloadPads();
790 ioEnd();
791 guiEnd();
792 menuEnd();
793 lngEnd();
794 thmEnd();
795 rmEnd();
796 configEnd();
798 deinitAllSupport(exception);
802 static void setDefaults(void) {
803 usbd_irx = NULL;
804 size_usbd_irx = 0;
806 clearIOModuleT(&list_support[USB_MODE]);
807 clearIOModuleT(&list_support[ETH_MODE]);
808 clearIOModuleT(&list_support[HDD_MODE]);
809 clearIOModuleT(&list_support[APP_MODE]);
811 gBaseMCDir = "mc?:OPL";
813 ps2_ip[0] = 192; ps2_ip[1] = 168; ps2_ip[2] = 0; ps2_ip[3] = 10;
814 ps2_netmask[0] = 255; ps2_netmask[1] = 255; ps2_netmask[2] = 255; ps2_netmask[3] = 0;
815 ps2_gateway[0] = 192; ps2_gateway[1] = 168; ps2_gateway[2] = 0; ps2_gateway[3] = 1;
816 pc_ip[0] = 192;pc_ip[1] = 168; pc_ip[2] = 0; pc_ip[3] = 2;
817 gPCPort = 445;
818 strncpy(gPCShareName, "", 32);
819 strncpy(gPCUserName, "GUEST", 32);
820 strncpy(gPCPassword, "", 32);
821 gNetworkStartup = ERROR_ETH_NOT_STARTED;
822 gHddStartup = 6;
823 gHDDSpindown = 20;
824 gIPConfigChanged = 0;
825 gScrollSpeed = 1;
826 strncpy(gExitPath, "", 32);
827 gDefaultDevice = APP_MODE;
828 gAutosort = 1;
829 gAutoRefresh = 1;
830 gDisableDebug = 0;
831 gEnableDandR = 0;
832 gRememberLastPlayed = 0;
833 gCheckUSBFragmentation = 1;
834 gUSBDelay = 3;
835 strncpy(gUSBPrefix, "", 32);
836 strncpy(gETHPrefix, "", 32);
837 gUseInfoScreen = 0;
838 gEnableArt = 0;
839 gWideScreen = 0;
841 gUSBStartMode = 0;
842 gHDDStartMode = 0;
843 gETHStartMode = 0;
844 gAPPStartMode = 0;
846 gDefaultBgColor[0] = 0x028;
847 gDefaultBgColor[1] = 0x0c5;
848 gDefaultBgColor[2] = 0x0f9;
850 gDefaultTextColor[0] = 0x0ff;
851 gDefaultTextColor[1] = 0x0ff;
852 gDefaultTextColor[2] = 0x0ff;
854 gDefaultSelTextColor[0] = 0x0ff;
855 gDefaultSelTextColor[1] = 0x080;
856 gDefaultSelTextColor[2] = 0x000;
858 gDefaultUITextColor[0] = 0x040;
859 gDefaultUITextColor[1] = 0x080;
860 gDefaultUITextColor[2] = 0x040;
862 frameCounter = UPDATE_FRAME_COUNT;
864 gVMode = RM_VMODE_AUTO;
865 gVSync = 1;
868 static void init(void) {
869 // default variable values
870 setDefaults();
872 padInit(0);
873 configInit(NULL);
875 rmInit();
876 lngInit();
877 thmInit();
878 guiInit();
879 ioInit();
880 menuInit();
882 startPads();
884 // handler for deffered menu updates
885 ioRegisterHandler(IO_MENU_UPDATE_DEFFERED, &menuDeferredUpdate);
887 // try to restore config
888 _loadConfig();
890 cacheInit();
893 static void deferredInit(void) {
895 // inform GUI main init part is over
896 struct gui_update_t *id = guiOpCreate(GUI_INIT_DONE);
897 guiDeferUpdate(id);
899 if (list_support[gDefaultDevice].support) {
900 id = guiOpCreate(GUI_OP_SELECT_MENU);
901 id->menu.menu = &list_support[gDefaultDevice].menuItem;
902 guiDeferUpdate(id);
906 // --------------------- Main --------------------
907 int main(int argc, char* argv[])
909 LOG_INIT();
910 PREINIT_LOG("OPL GUI start!\n");
912 #ifdef __DEBUG
913 int use_early_debug = 0, exception_test = 0;
914 int i;
915 for (i=1; i<argc; i++) {
916 if (!(strcmp(argv[i], "-use-early-debug"))) {
917 use_early_debug = 1;
918 PREINIT_LOG("Using early debug.\n");
920 if (!(strcmp(argv[i], "-test-exception"))) {
921 exception_test = 1;
922 PREINIT_LOG("Exception test requested.\n");
925 #endif
927 // apply kernel patches
928 sysApplyKernelPatches();
930 // reset, load modules
931 reset();
933 #ifdef __DEBUG
934 if (use_early_debug) {
935 configReadIP();
936 debugSetActive();
939 if (exception_test) {
940 // EXCEPTION test !
941 u32 *p = (u32 *)0xDEADC0DE;
942 *p = 0xDEFACED;
944 #endif
946 init();
948 // until this point in the code is reached, only PREINIT_LOG macro should be used
949 LOG_ENABLE();
951 // queue deffered init which shuts down the intro screen later
952 ioPutRequest(IO_CUSTOM_SIMPLEACTION, &deferredInit);
954 guiIntroLoop();
955 guiMainLoop();
957 return 0;