| 1 | |
| 2 | /** |
| 3 | * |
| 4 | * @file switch.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 "switch.h" |
| 17 | |
| 18 | #ifdef __SWITCH__ |
| 19 | |
| 20 | #include <switch.h> |
| 21 | #include <malloc.h> |
| 22 | #include "util.h" |
| 23 | #include "io/file.h" |
| 24 | #include "io/log.h" |
| 25 | #include "version.h" |
| 26 | |
| 27 | namespace { |
| 28 | int sock = -1; |
| 29 | |
| 30 | SwkbdTextCheckResult InputString_Filter(char *text, size_t textlen) { |
| 31 | for (size_t i = 0; i < textlen; i++) { |
| 32 | // we do not care about the unused buffer end |
| 33 | if (text[i] == '\0' || i > STRING_LENGTH) { |
| 34 | return SwkbdTextCheckResult_OK; |
| 35 | } |
| 36 | if ((text[i] != ' ') && (text[i] != '.') && |
| 37 | ((text[i] < '0') || (text[i] > '9')) && |
| 38 | ((text[i] < 'a') || (text[i] > 'z'))) { |
| 39 | |
| 40 | strncpy(text, "Invalid character(s) found.\nOnly a-z, 0-9, Dot and Space are allowed.", textlen - 1); |
| 41 | text[textlen] = '\0'; |
| 42 | return SwkbdTextCheckResult_Bad; |
| 43 | } |
| 44 | } |
| 45 | return SwkbdTextCheckResult_OK; |
| 46 | } |
| 47 | |
| 48 | } |
| 49 | |
| 50 | SwitchPlatform::SwitchPlatform() { |
| 51 | // file system |
| 52 | romfsInit(); |
| 53 | |
| 54 | #ifndef NDEBUG |
| 55 | // network console |
| 56 | NetInit(); |
| 57 | sock = nxlinkStdio(); |
| 58 | #endif |
| 59 | } |
| 60 | |
| 61 | SwitchPlatform::~SwitchPlatform() { |
| 62 | romfsExit(); |
| 63 | |
| 64 | #ifndef NDEBUG |
| 65 | NetExit(); |
| 66 | #endif |
| 67 | } |
| 68 | |
| 69 | void SwitchPlatform::NetInit() { |
| 70 | int ret = socketInitializeDefault(); |
| 71 | if (ret != 0) { |
| 72 | LOG_WARN("Failed to initialize sockets 0x%08X\n", (unsigned int)ret); |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | void SwitchPlatform::NetExit() { |
| 77 | if (sock >= 0) { |
| 78 | close(sock); |
| 79 | sock = -1; |
| 80 | } |
| 81 | |
| 82 | socketExit(); |
| 83 | } |
| 84 | |
| 85 | bool SwitchPlatform::NetHasConsole() { |
| 86 | return sock >= 0; |
| 87 | } |
| 88 | |
| 89 | void SwitchPlatform::AddGamePaths() { |
| 90 | gamePaths.add(createString("sdmc:/switch/OpenJazz/"), PATH_TYPE_GAME|PATH_TYPE_CONFIG); |
| 91 | gamePaths.add(createString("romfs:/"), PATH_TYPE_SYSTEM|PATH_TYPE_GAME); |
| 92 | } |
| 93 | |
| 94 | void SwitchPlatform::ErrorNoDatafiles() { |
| 95 | // never generate an error report without CFW |
| 96 | if (!hosversionIsAtmosphere()) return; |
| 97 | |
| 98 | // only applications should use the error applet |
| 99 | AppletType at = appletGetAppletType(); |
| 100 | if (at != AppletType_Application && at != AppletType_SystemApplication) return; |
| 101 | |
| 102 | // make error |
| 103 | const char *errorMsg = "Unable to find game data files.\n\n" |
| 104 | "Game files can be shipped inside RomFS and are searched under sdmc:/switch/OpenJazz/"; |
| 105 | size_t msgLen = strlen(errorMsg) + 60; // enough room for version string |
| 106 | char *error = new char[msgLen]; |
| 107 | snprintf(error, msgLen, "OpenJazz %s:\n\n%s", oj_version, errorMsg); |
| 108 | ErrorApplicationConfig eac; |
| 109 | if (R_SUCCEEDED(errorApplicationCreate(&eac, error, nullptr))) { |
| 110 | errorApplicationShow(&eac); |
| 111 | } |
| 112 | delete[] error; |
| 113 | } |
| 114 | |
| 115 | bool SwitchPlatform::InputIP(char*& current_ip, char*& new_ip) { |
| 116 | SwkbdConfig kbd; |
| 117 | Result rc = swkbdCreate(&kbd, 0); |
| 118 | if (R_SUCCEEDED(rc)) { |
| 119 | swkbdConfigSetType(&kbd, SwkbdType_NumPad); |
| 120 | swkbdConfigSetRightOptionalSymbolKey(&kbd, "."); |
| 121 | swkbdConfigSetHeaderText(&kbd, "OpenJazz"); |
| 122 | swkbdConfigSetSubText(&kbd, "Please enter an IP address"); |
| 123 | swkbdConfigSetInitialText(&kbd, current_ip); |
| 124 | char ip_buf[16] = {0}; |
| 125 | swkbdConfigSetInitialCursorPos(&kbd, 1); // at end |
| 126 | swkbdConfigSetStringLenMax(&kbd, sizeof(ip_buf) - 1); |
| 127 | swkbdConfigSetStringLenMin(&kbd, 7); |
| 128 | rc = swkbdShow(&kbd, ip_buf, sizeof(ip_buf)); |
| 129 | swkbdClose(&kbd); |
| 130 | if (R_SUCCEEDED(rc)) { |
| 131 | new_ip = createString(ip_buf); |
| 132 | return true; |
| 133 | } |
| 134 | } |
| 135 | return false; |
| 136 | } |
| 137 | |
| 138 | bool SwitchPlatform::InputString(const char* hint, char*& current_string, char*& new_string) { |
| 139 | SwkbdConfig kbd; |
| 140 | Result rc = swkbdCreate(&kbd, 0); |
| 141 | if (R_SUCCEEDED(rc)) { |
| 142 | swkbdConfigSetType(&kbd, SwkbdType_QWERTY); |
| 143 | swkbdConfigSetHeaderText(&kbd, "OpenJazz"); |
| 144 | swkbdConfigSetSubText(&kbd, hint); |
| 145 | swkbdConfigSetInitialText(&kbd, current_string); |
| 146 | char string_buf[80] = {0}; // needs to be big enough for the StringFilter message |
| 147 | swkbdConfigSetInitialCursorPos(&kbd, 1); // at end |
| 148 | swkbdConfigSetStringLenMax(&kbd, STRING_LENGTH); |
| 149 | swkbdConfigSetStringLenMin(&kbd, 1); |
| 150 | swkbdConfigSetTextCheckCallback(&kbd, InputString_Filter); |
| 151 | swkbdConfigSetKeySetDisableBitmask(&kbd, SwkbdKeyDisableBitmask_UserName); |
| 152 | rc = swkbdShow(&kbd, string_buf, sizeof(string_buf)); |
| 153 | swkbdClose(&kbd); |
| 154 | if (R_SUCCEEDED(rc)) { |
| 155 | new_string = createString(string_buf); |
| 156 | return true; |
| 157 | } |
| 158 | } |
| 159 | return false; |
| 160 | } |
| 161 | |
| 162 | #endif |