| 1 | |
| 2 | /** |
| 3 | * |
| 4 | * @file 3ds.cpp |
| 5 | * |
| 6 | * Part of the OpenJazz project |
| 7 | * |
| 8 | * @par Licence: |
| 9 | * Copyright (c) 2015-2026 Carsten Teibes |
| 10 | * |
| 11 | * OpenJazz is distributed under the terms of |
| 12 | * the GNU General Public License, version 2.0 |
| 13 | * |
| 14 | */ |
| 15 | |
| 16 | #include "3ds.h" |
| 17 | |
| 18 | #ifdef __3DS__ |
| 19 | |
| 20 | #include <3ds.h> |
| 21 | #include <malloc.h> |
| 22 | #include "util.h" |
| 23 | #include "io/file.h" |
| 24 | #include "io/log.h" |
| 25 | |
| 26 | namespace { |
| 27 | constexpr int SOC_BUFFERSIZE = 0x100000; |
| 28 | |
| 29 | u32 *socBuffer = nullptr; |
| 30 | int sock = -1; |
| 31 | |
| 32 | SwkbdCallbackResult InputString_Filter(void*, const char** ppMessage, const char* text, size_t textlen) { |
| 33 | for (size_t i = 0; i < textlen; i++) { |
| 34 | |
| 35 | if ((text[i] != ' ') && (text[i] != '.') && |
| 36 | ((text[i] < '0') || (text[i] > '9')) && |
| 37 | ((text[i] < 'a') || (text[i] > 'z'))) { |
| 38 | |
| 39 | *ppMessage = "Invalid character(s) found.\nOnly a-z, 0-9, Dot and Space\nare allowed."; |
| 40 | return SWKBD_CALLBACK_CONTINUE; |
| 41 | |
| 42 | } |
| 43 | } |
| 44 | |
| 45 | return SWKBD_CALLBACK_OK; |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | N3dsPlatform::N3dsPlatform() { |
| 50 | // file system |
| 51 | romfsInit(); |
| 52 | |
| 53 | #ifndef NDEBUG |
| 54 | // network console |
| 55 | NetInit(); |
| 56 | sock = link3dsStdio(); |
| 57 | #endif |
| 58 | } |
| 59 | |
| 60 | N3dsPlatform::~N3dsPlatform() { |
| 61 | romfsExit(); |
| 62 | |
| 63 | #ifndef NDEBUG |
| 64 | NetExit(); |
| 65 | #endif |
| 66 | } |
| 67 | |
| 68 | void N3dsPlatform::NetInit() { |
| 69 | socBuffer = static_cast<u32*>(memalign(0x1000, SOC_BUFFERSIZE)); |
| 70 | if(!socBuffer) { |
| 71 | LOG_WARN("Failed to allocate soc:u buffer"); |
| 72 | return; |
| 73 | } |
| 74 | |
| 75 | int ret = socInit(socBuffer, SOC_BUFFERSIZE); |
| 76 | if (ret != 0) { |
| 77 | LOG_WARN("Failed to initialize soc:u: 0x%08X\n", (unsigned int)ret); |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | void N3dsPlatform::NetExit() { |
| 82 | if(sock > 0) |
| 83 | close(sock); |
| 84 | |
| 85 | socExit(); |
| 86 | |
| 87 | if(socBuffer) |
| 88 | free(socBuffer); |
| 89 | } |
| 90 | |
| 91 | bool N3dsPlatform::NetHasConsole() { |
| 92 | return sock > 0; |
| 93 | } |
| 94 | |
| 95 | void N3dsPlatform::AddGamePaths() { |
| 96 | gamePaths.add(createString("sdmc:/3ds/OpenJazz/"), PATH_TYPE_GAME|PATH_TYPE_CONFIG); |
| 97 | gamePaths.add(createString("romfs:/"), PATH_TYPE_SYSTEM|PATH_TYPE_GAME); |
| 98 | } |
| 99 | |
| 100 | |
| 101 | void N3dsPlatform::ErrorNoDatafiles() { |
| 102 | errorConf errCnf; |
| 103 | const char *error = "Unable to find game data files.\n\n" |
| 104 | "Game files can be shipped inside RomFS and are searched under\n" |
| 105 | "sdmc:/3ds/OpenJazz/"; |
| 106 | |
| 107 | errorInit(&errCnf, ERROR_TEXT_WORD_WRAP, CFG_LANGUAGE_EN); |
| 108 | errorText(&errCnf, error); |
| 109 | errorDisp(&errCnf); |
| 110 | } |
| 111 | |
| 112 | bool N3dsPlatform::InputIP(char*& current_ip, char*& new_ip) { |
| 113 | SwkbdState swkbd; |
| 114 | char ip_buf[16]; |
| 115 | SwkbdButton button = SWKBD_BUTTON_NONE; |
| 116 | |
| 117 | swkbdInit(&swkbd, SWKBD_TYPE_NUMPAD, 2, 15); |
| 118 | swkbdSetInitialText(&swkbd, current_ip); |
| 119 | swkbdSetHintText(&swkbd, "Please enter an IP address"); |
| 120 | swkbdSetValidation(&swkbd, SWKBD_NOTEMPTY_NOTBLANK, 0, 0); |
| 121 | swkbdSetFeatures(&swkbd, SWKBD_FIXED_WIDTH); |
| 122 | swkbdSetNumpadKeys(&swkbd, '.', 0); |
| 123 | |
| 124 | button = swkbdInputText(&swkbd, ip_buf, sizeof(ip_buf)); |
| 125 | if (button == SWKBD_BUTTON_CONFIRM) { |
| 126 | new_ip = createString(ip_buf); |
| 127 | return true; |
| 128 | } |
| 129 | |
| 130 | return false; |
| 131 | } |
| 132 | |
| 133 | bool N3dsPlatform::InputString(const char* hint, char*& current_string, char*& new_string) { |
| 134 | SwkbdState swkbd; |
| 135 | char string_buf[STRING_LENGTH + 1]; |
| 136 | SwkbdButton button = SWKBD_BUTTON_NONE; |
| 137 | |
| 138 | swkbdInit(&swkbd, SWKBD_TYPE_QWERTY, 2, STRING_LENGTH); |
| 139 | swkbdSetInitialText(&swkbd, current_string); |
| 140 | swkbdSetHintText(&swkbd, hint); |
| 141 | swkbdSetValidation(&swkbd, SWKBD_NOTEMPTY_NOTBLANK, 0, 0); |
| 142 | swkbdSetFilterCallback(&swkbd, InputString_Filter, nullptr); |
| 143 | |
| 144 | button = swkbdInputText(&swkbd, string_buf, sizeof(string_buf)); |
| 145 | if (button == SWKBD_BUTTON_CONFIRM) { |
| 146 | new_string = createString(string_buf); |
| 147 | return true; |
| 148 | } |
| 149 | |
| 150 | return false; |
| 151 | } |
| 152 | |
| 153 | #endif |