clarify the gui
[open-ps2-loader.git] / src / opl.c
blob104a4a82e7bb6cf173e9f0e1ea64c8d096d4d2e3
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;
415 config_set_t *configOPL = configGetByType(CONFIG_OPL);
417 // check USB
418 usbLoadModules();
419 if (usbFindPartition(path, "conf_opl.cfg")) {
420 configEnd();
421 configInit(path);
422 value = configReadMulti(types);
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 configSetInt(configOPL, "hdd_mode", 2);
437 return value;
440 if (sysCheckMC() < 0) { // We don't want to get users into alternate mode for their very first of OPL (i.e no config file at all, but still want to save on MC)
441 // set config path to either mass or hdd, to prepare the saving of a new config
442 value = fioDopen("mass0:");
443 if (value >= 0) {
444 fioDclose(value);
445 configEnd();
446 configInit("mass0:");
448 else {
449 configEnd();
450 configInit("pfs0:");
454 return 0;
457 static void _loadConfig() {
458 int result = configReadMulti(lscstatus);
460 if (lscstatus & CONFIG_OPL) {
461 int themeID = -1, langID = -1;
463 if (!(result & CONFIG_OPL)) {
464 result = tryAlternateDevice(lscstatus);
467 if (result & CONFIG_OPL) {
468 config_set_t *configOPL = configGetByType(CONFIG_OPL);
469 char *temp;
471 configGetInt(configOPL, "scrolling", &gScrollSpeed);
472 configGetColor(configOPL, "bg_color", gDefaultBgColor);
473 configGetColor(configOPL, "text_color", gDefaultTextColor);
474 configGetColor(configOPL, "ui_text_color", gDefaultUITextColor);
475 configGetColor(configOPL, "sel_text_color", gDefaultSelTextColor);
476 configGetInt(configOPL, "use_info_screen", &gUseInfoScreen);
477 configGetInt(configOPL, "enable_coverart", &gEnableArt);
478 configGetInt(configOPL, "wide_screen", &gWideScreen);
480 if (configGetStr(configOPL, "theme", &temp))
481 themeID = thmFindGuiID(temp);
483 if (configGetStr(configOPL, "language_text", &temp))
484 langID = lngFindGuiID(temp);
486 if (configGetStr(configOPL, "pc_ip", &temp))
487 sscanf(temp, "%d.%d.%d.%d", &pc_ip[0], &pc_ip[1], &pc_ip[2], &pc_ip[3]);
489 configGetInt(configOPL, "pc_port", &gPCPort);
491 if (configGetStr(configOPL, "pc_share", &temp))
492 strncpy(gPCShareName, temp, 32);
493 if (configGetStr(configOPL, "pc_user", &temp))
494 strncpy(gPCUserName, temp, 32);
495 if (configGetStr(configOPL, "pc_pass", &temp))
496 strncpy(gPCPassword, temp, 32);
497 if (configGetStr(configOPL, "exit_path", &temp))
498 strncpy(gExitPath, temp, 32);
500 configGetInt(configOPL, "autosort", &gAutosort);
501 configGetInt(configOPL, "autorefresh", &gAutoRefresh);
502 configGetInt(configOPL, "default_device", &gDefaultDevice);
503 configGetInt(configOPL, "disable_debug", &gDisableDebug);
504 configGetInt(configOPL, "enable_delete_rename", &gEnableDandR);
505 configGetInt(configOPL, "hdd_spindown", &gHDDSpindown);
506 configGetInt(configOPL, "check_usb_frag", &gCheckUSBFragmentation);
507 configGetInt(configOPL, "usb_delay", &gUSBDelay);
508 if (configGetStr(configOPL, "usb_prefix", &temp))
509 strncpy(gUSBPrefix, temp, 32);
510 configGetInt(configOPL, "remember_last", &gRememberLastPlayed);
511 configGetInt(configOPL, "usb_mode", &gUSBStartMode);
512 configGetInt(configOPL, "hdd_mode", &gHDDStartMode);
513 configGetInt(configOPL, "eth_mode", &gETHStartMode);
514 configGetInt(configOPL, "app_mode", &gAPPStartMode);
517 applyConfig(themeID, langID, gVMode, gVSync);
520 lscret = result;
521 lscstatus = 0;
524 static void _saveConfig() {
525 if (lscstatus & CONFIG_OPL) {
526 config_set_t *configOPL = configGetByType(CONFIG_OPL);
527 configSetInt(configOPL, "scrolling", gScrollSpeed);
528 configSetStr(configOPL, "theme", thmGetValue());
529 configSetStr(configOPL, "language_text", lngGetValue());
530 configSetColor(configOPL, "bg_color", gDefaultBgColor);
531 configSetColor(configOPL, "text_color", gDefaultTextColor);
532 configSetColor(configOPL, "ui_text_color", gDefaultUITextColor);
533 configSetColor(configOPL, "sel_text_color", gDefaultSelTextColor);
534 configSetInt(configOPL, "use_info_screen", gUseInfoScreen);
535 configSetInt(configOPL, "enable_coverart", gEnableArt);
536 configSetInt(configOPL, "wide_screen", gWideScreen);
538 char temp[255];
539 sprintf(temp, "%d.%d.%d.%d", pc_ip[0], pc_ip[1], pc_ip[2], pc_ip[3]);
540 configSetStr(configOPL, "pc_ip", temp);
541 configSetInt(configOPL, "pc_port", gPCPort);
542 configSetStr(configOPL, "pc_share", gPCShareName);
543 configSetStr(configOPL, "pc_user", gPCUserName);
544 configSetStr(configOPL, "pc_pass", gPCPassword);
545 configSetStr(configOPL, "exit_path", gExitPath);
546 configSetInt(configOPL, "autosort", gAutosort);
547 configSetInt(configOPL, "autorefresh", gAutoRefresh);
548 configSetInt(configOPL, "default_device", gDefaultDevice);
549 configSetInt(configOPL, "disable_debug", gDisableDebug);
550 configSetInt(configOPL, "enable_delete_rename", gEnableDandR);
551 configSetInt(configOPL, "hdd_spindown", gHDDSpindown);
552 configSetInt(configOPL, "check_usb_frag", gCheckUSBFragmentation);
553 configSetInt(configOPL, "usb_delay", gUSBDelay);
554 configSetStr(configOPL, "usb_prefix", gUSBPrefix);
555 configSetInt(configOPL, "remember_last", gRememberLastPlayed);
556 configSetInt(configOPL, "usb_mode", gUSBStartMode);
557 configSetInt(configOPL, "hdd_mode", gHDDStartMode);
558 configSetInt(configOPL, "eth_mode", gETHStartMode);
559 configSetInt(configOPL, "app_mode", gAPPStartMode);
562 lscret = configWriteMulti(lscstatus);
563 lscstatus = 0;
566 void applyConfig(int themeID, int langID, int newVMode, int newVSync) {
567 infotxt = _l(_STR_WELCOME);
569 if (gDefaultDevice < 0 || gDefaultDevice > APP_MODE)
570 gDefaultDevice = APP_MODE;
572 guiUpdateScrollSpeed();
573 guiUpdateScreenScale();
575 // we don't want to set the vmode without a reason...
576 int changed = (gVMode != newVMode || gVSync != newVSync);
577 if (changed) {
578 // reinit the graphics...
579 gVMode = newVMode;
580 gVSync = newVSync;
581 rmSetMode(gVSync, gVMode);
583 thmReloadScreenExtents();
584 guiReloadScreenExtents();
586 // also propagate to vmode cfg
587 config_set_t* configVMode = configGetByType(CONFIG_VMODE);
588 if (configVMode) {
589 configSetInt(configVMode, "vmode", newVMode);
590 configSetInt(configVMode, "vsync", newVSync);
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);
606 if (gAutoRefresh)
607 guiSetFrameHook(&menuUpdateHook);
608 else
609 guiSetFrameHook(NULL);
612 int loadConfig(int types) {
613 lscstatus = types;
614 lscret = 0;
616 guiHandleDeferedIO(&lscstatus, _l(_STR_LOADING_SETTINGS), IO_CUSTOM_SIMPLEACTION, &_loadConfig);
618 return lscret;
621 int saveConfig(int types, int showUI) {
622 lscstatus = types;
623 lscret = 0;
625 guiHandleDeferedIO(&lscstatus, _l(_STR_SAVING_SETTINGS), IO_CUSTOM_SIMPLEACTION, &_saveConfig);
627 if (showUI) {
628 if (lscret)
629 guiMsgBox(_l(_STR_SETTINGS_SAVED), 0, NULL);
630 else
631 guiMsgBox(_l(_STR_ERROR_SAVING_SETTINGS), 0, NULL);
634 return lscret;
637 // ----------------------------------------------------------
638 // -------------------- HD SRV Support ----------------------
639 // ----------------------------------------------------------
640 extern void *ps2dev9_irx;
641 extern int size_ps2dev9_irx;
643 extern void *smsutils_irx;
644 extern int size_smsutils_irx;
646 extern void *smstcpip_irx;
647 extern int size_smstcpip_irx;
649 extern void *smsmap_irx;
650 extern int size_smsmap_irx;
652 extern void *ps2atad_irx;
653 extern int size_ps2atad_irx;
655 extern void *ps2hdd_irx;
656 extern int size_ps2hdd_irx;
658 extern void *hdldsvr_irx;
659 extern int size_hdldsvr_irx;
661 void loadHdldSvr(void) {
662 int ret;
663 static char hddarg[] = "-o" "\0" "4" "\0" "-n" "\0" "20";
664 char ipconfig[IPCONFIG_MAX_LEN] __attribute__((aligned(64)));
666 // block all io ops, wait for the ones still running to finish
667 ioBlockOps(1);
669 deinitAllSupport(NO_EXCEPTION);
671 unloadPads();
673 sysReset(SYS_LOAD_PAD_MODULES);
675 int iplen = sysSetIPConfig(ipconfig);
677 ret = sysLoadModuleBuffer(&ps2dev9_irx, size_ps2dev9_irx, 0, NULL);
678 if (ret < 0)
679 return;
681 ret = sysLoadModuleBuffer(&smsutils_irx, size_smsutils_irx, 0, NULL);
682 if (ret < 0)
683 return;
685 ret = sysLoadModuleBuffer(&smstcpip_irx, size_smstcpip_irx, 0, NULL);
686 if (ret < 0)
687 return;
689 ret = sysLoadModuleBuffer(&smsmap_irx, size_smsmap_irx, iplen, ipconfig);
690 if (ret < 0)
691 return;
693 ret = sysLoadModuleBuffer(&ps2atad_irx, size_ps2atad_irx, 0, NULL);
694 if (ret < 0)
695 return;
697 ret = sysLoadModuleBuffer(&ps2hdd_irx, size_ps2hdd_irx, sizeof(hddarg), hddarg);
698 if (ret < 0)
699 return;
701 ret = sysLoadModuleBuffer(&hdldsvr_irx, size_hdldsvr_irx, 0, NULL);
702 if (ret < 0)
703 return;
705 padInit(0);
707 // init all pads
708 ret = 0;
709 while (!ret)
710 ret = startPads();
712 // now ready to display some status
715 void unloadHdldSvr(void) {
716 unloadPads();
718 sysReset(SYS_LOAD_MC_MODULES | SYS_LOAD_PAD_MODULES);
720 LOG_INIT();
721 LOG_ENABLE();
723 mcInit(MC_TYPE_MC);
725 // reinit the input pads
726 padInit(0);
728 int ret = 0;
729 while (!ret)
730 ret = startPads();
732 // now start io again
733 ioBlockOps(0);
735 // init all supports again
736 initAllSupport(1);
739 void handleHdlSrv() {
740 guiRenderTextScreen(_l(_STR_STARTINGHDL));
742 // prepare for hdl, display screen with info
743 loadHdldSvr();
745 int terminate = 0;
747 while (1) {
748 if (terminate != 0)
749 guiRenderTextScreen(_l(_STR_STOPHDL));
750 else
751 guiRenderTextScreen(_l(_STR_RUNNINGHDL));
753 sleep(2);
755 readPads();
757 if(getKeyOn(KEY_CIRCLE) && terminate == 0) {
758 terminate++;
759 } else if(getKeyOn(KEY_CROSS) && terminate == 1) {
760 terminate++;
761 } else if (terminate > 0)
762 terminate--;
764 if (terminate >= 2)
765 break;
768 guiRenderTextScreen(_l(_STR_UNLOADHDL));
770 // restore normal functionality again
771 unloadHdldSvr();
774 // ----------------------------------------------------------
775 // --------------------- Init/Deinit ------------------------
776 // ----------------------------------------------------------
777 static void reset(void) {
778 sysReset(SYS_LOAD_MC_MODULES | SYS_LOAD_PAD_MODULES);
780 SifInitRpc(0);
782 mcInit(MC_TYPE_MC);
785 static void moduleCleanup(opl_io_module_t* mod, int exception) {
786 if (!mod->support)
787 return;
789 if (mod->support->itemCleanUp)
790 mod->support->itemCleanUp(exception);
793 void shutdown(int exception) {
794 unloadPads();
795 ioEnd();
796 guiEnd();
797 menuEnd();
798 lngEnd();
799 thmEnd();
800 rmEnd();
801 configEnd();
803 deinitAllSupport(exception);
807 static void setDefaults(void) {
808 usbd_irx = NULL;
809 size_usbd_irx = 0;
811 clearIOModuleT(&list_support[USB_MODE]);
812 clearIOModuleT(&list_support[ETH_MODE]);
813 clearIOModuleT(&list_support[HDD_MODE]);
814 clearIOModuleT(&list_support[APP_MODE]);
816 gBaseMCDir = "mc?:OPL";
818 ps2_ip[0] = 192; ps2_ip[1] = 168; ps2_ip[2] = 0; ps2_ip[3] = 10;
819 ps2_netmask[0] = 255; ps2_netmask[1] = 255; ps2_netmask[2] = 255; ps2_netmask[3] = 0;
820 ps2_gateway[0] = 192; ps2_gateway[1] = 168; ps2_gateway[2] = 0; ps2_gateway[3] = 1;
821 pc_ip[0] = 192;pc_ip[1] = 168; pc_ip[2] = 0; pc_ip[3] = 2;
822 gPCPort = 445;
823 strncpy(gPCShareName, "PS2SMB", 32);
824 strncpy(gPCUserName, "GUEST", 32);
825 strncpy(gPCPassword, "", 32);
826 gNetworkStartup = ERROR_ETH_NOT_STARTED;
827 gHddStartup = 6;
828 gHDDSpindown = 20;
829 gIPConfigChanged = 0;
830 gScrollSpeed = 1;
831 strncpy(gExitPath, "", 32);
832 gDefaultDevice = APP_MODE;
833 gAutosort = 1;
834 gAutoRefresh = 1;
835 gDisableDebug = 0;
836 gEnableDandR = 0;
837 gRememberLastPlayed = 0;
838 gCheckUSBFragmentation = 1;
839 gUSBDelay = 3;
840 strncpy(gUSBPrefix, "", 32);
841 gUseInfoScreen = 0;
842 gEnableArt = 0;
843 gWideScreen = 0;
845 gUSBStartMode = 0;
846 gHDDStartMode = 0;
847 gETHStartMode = 0;
848 gAPPStartMode = 0;
850 gDefaultBgColor[0] = 0x028;
851 gDefaultBgColor[1] = 0x0c5;
852 gDefaultBgColor[2] = 0x0f9;
854 gDefaultTextColor[0] = 0x0ff;
855 gDefaultTextColor[1] = 0x0ff;
856 gDefaultTextColor[2] = 0x0ff;
858 gDefaultSelTextColor[0] = 0x0ff;
859 gDefaultSelTextColor[1] = 0x080;
860 gDefaultSelTextColor[2] = 0x000;
862 gDefaultUITextColor[0] = 0x040;
863 gDefaultUITextColor[1] = 0x080;
864 gDefaultUITextColor[2] = 0x040;
866 frameCounter = UPDATE_FRAME_COUNT;
868 gVMode = RM_VMODE_AUTO;
869 gVSync = 1;
872 void vmodeInit() {
873 config_set_t* configVMode = configGetByType(CONFIG_VMODE);
875 if (configVMode) {
876 if (configRead(configVMode) == CONFIG_VMODE) {
877 if (!configGetInt(configVMode, "vsync", &gVSync))
878 gVSync = 1;
880 if (!configGetInt(configVMode, "vmode", &gVMode))
881 gVMode = RM_VMODE_AUTO;
885 rmInit(gVSync, gVMode);
888 static void init(void) {
889 // default variable values
890 setDefaults();
892 padInit(0);
893 configInit(NULL);
895 // Loads the vmode config from MCs, then sets the vmode
896 vmodeInit();
898 lngInit();
899 thmInit();
900 guiInit();
901 ioInit();
902 menuInit();
904 startPads();
906 // handler for deffered menu updates
907 ioRegisterHandler(IO_MENU_UPDATE_DEFFERED, &menuDeferredUpdate);
909 // try to restore config
910 _loadConfig();
912 cacheInit();
915 static void deferredInit(void) {
917 // inform GUI main init part is over
918 struct gui_update_t *id = guiOpCreate(GUI_INIT_DONE);
919 guiDeferUpdate(id);
921 if (list_support[gDefaultDevice].support) {
922 id = guiOpCreate(GUI_OP_SELECT_MENU);
923 id->menu.menu = &list_support[gDefaultDevice].menuItem;
924 guiDeferUpdate(id);
928 // --------------------- Main --------------------
929 int main(int argc, char* argv[])
931 LOG_INIT();
932 PREINIT_LOG("OPL GUI start!\n");
934 #ifdef __DEBUG
935 int use_early_debug = 0, exception_test = 0;
936 int i;
937 for (i=1; i<argc; i++) {
938 if (!(strcmp(argv[i], "-use-early-debug"))) {
939 use_early_debug = 1;
940 PREINIT_LOG("Using early debug.\n");
942 if (!(strcmp(argv[i], "-test-exception"))) {
943 exception_test = 1;
944 PREINIT_LOG("Exception test requested.\n");
947 #endif
949 // apply kernel patches
950 sysApplyKernelPatches();
952 // reset, load modules
953 reset();
955 #ifdef __DEBUG
956 if (use_early_debug) {
957 configReadIP();
958 debugSetActive();
961 if (exception_test) {
962 // EXCEPTION test !
963 u32 *p = (u32 *)0xDEADC0DE;
964 *p = 0xDEFACED;
966 #endif
968 init();
970 // until this point in the code is reached, only PREINIT_LOG macro should be used
971 LOG_ENABLE();
973 // queue deffered init which shuts down the intro screen later
974 ioPutRequest(IO_CUSTOM_SIMPLEACTION, &deferredInit);
976 guiIntroLoop();
977 guiMainLoop();
979 return 0;