| 1 | #include "../ethos.hpp" |
| 2 | |
| 3 | Utility* utility = nullptr; |
| 4 | |
| 5 | void Utility::setInterface(Emulator::Interface* emulator) { |
| 6 | program->active = emulator; |
| 7 | presentation->synchronize(); |
| 8 | } |
| 9 | |
| 10 | //load from command-line, etc |
| 11 | void Utility::loadMedia(string pathname) { |
| 12 | pathname.transform("\\", "/"); |
| 13 | if(pathname.endsWith("/")) pathname.rtrim("/"); |
| 14 | |
| 15 | if(!directory::exists(pathname)) return; |
| 16 | string type = extension(pathname); |
| 17 | |
| 18 | //determine type by comparing extension against all emulation cores |
| 19 | for(auto& emulator : program->emulator) { |
| 20 | for(auto& media : emulator->media) { |
| 21 | if(media.bootable == false) continue; |
| 22 | if(type != media.type) continue; |
| 23 | loadMedia(emulator, media, {pathname, "/"}); |
| 24 | libraryManager->setVisible(false); |
| 25 | return; |
| 26 | } |
| 27 | } |
| 28 | |
| 29 | MessageWindow().setText("Unable to determine media type.").warning(); |
| 30 | } |
| 31 | |
| 32 | //load base cartridge |
| 33 | void Utility::loadMedia(Emulator::Interface* emulator, Emulator::Interface::Media& media, string pathname) { |
| 34 | unload(); |
| 35 | setInterface(emulator); |
| 36 | path(0) = program->path({media.name, ".sys/"}); |
| 37 | path(media.id) = pathname; |
| 38 | this->pathname.append(pathname); |
| 39 | |
| 40 | system().load(media.id); |
| 41 | system().power(); |
| 42 | |
| 43 | presentation->setSystemName(media.name); |
| 44 | presentation->setVisible(); |
| 45 | load(); |
| 46 | } |
| 47 | |
| 48 | //request from emulation core to load non-volatile media folder |
| 49 | void Utility::loadRequest(unsigned id, string name, string type) { |
| 50 | string pathname = libraryManager->load(type); |
| 51 | if(pathname.empty()) return; |
| 52 | path(id) = pathname; |
| 53 | this->pathname.append(pathname); |
| 54 | |
| 55 | system().load(id); |
| 56 | } |
| 57 | |
| 58 | //request from emulation core to load non-volatile media file |
| 59 | void Utility::loadRequest(unsigned id, string path) { |
| 60 | string pathname = {this->path(system().group(id)), path}; |
| 61 | if(file::exists(pathname) == false) return; |
| 62 | mmapstream stream(pathname); |
| 63 | return system().load(id, stream); |
| 64 | } |
| 65 | |
| 66 | //request from emulation core to save non-volatile media file |
| 67 | void Utility::saveRequest(unsigned id, string path) { |
| 68 | string pathname = {this->path(system().group(id)), path}; |
| 69 | filestream stream(pathname, file::mode::write); |
| 70 | return system().save(id, stream); |
| 71 | } |
| 72 | |
| 73 | void Utility::connect(unsigned port, unsigned device) { |
| 74 | if(program->active == nullptr) return; |
| 75 | system().connect(port, device); |
| 76 | } |
| 77 | |
| 78 | void Utility::power() { |
| 79 | if(program->active == nullptr) return; |
| 80 | system().power(); |
| 81 | } |
| 82 | |
| 83 | void Utility::reset() { |
| 84 | if(program->active == nullptr) return; |
| 85 | system().reset(); |
| 86 | } |
| 87 | |
| 88 | void Utility::load() { |
| 89 | presentation->setTitle(system().title()); |
| 90 | |
| 91 | cheatEditor->load({pathname[0], "cheats.bml"}); |
| 92 | stateManager->load({pathname[0], "bsnes/states.bsa"}, 1); |
| 93 | |
| 94 | synchronizeDSP(); |
| 95 | |
| 96 | resize(); |
| 97 | updateShader(); |
| 98 | cheatEditor->synchronize(); |
| 99 | cheatEditor->refresh(); |
| 100 | } |
| 101 | |
| 102 | void Utility::unload() { |
| 103 | if(program->active == nullptr) return; |
| 104 | if(tracerEnable) tracerToggle(); |
| 105 | |
| 106 | cheatEditor->save({pathname[0], "cheats.bml"}); |
| 107 | stateManager->save({pathname[0], "bsnes/states.bsa"}, 1); |
| 108 | |
| 109 | system().unload(); |
| 110 | path.reset(); |
| 111 | pathname.reset(); |
| 112 | cheatEditor->reset(); |
| 113 | stateManager->reset(); |
| 114 | setInterface(nullptr); |
| 115 | |
| 116 | video.clear(); |
| 117 | audio.clear(); |
| 118 | presentation->setTitle({Emulator::Name, " v", Emulator::Version}); |
| 119 | cheatDatabase->setVisible(false); |
| 120 | cheatEditor->setVisible(false); |
| 121 | stateManager->setVisible(false); |
| 122 | } |
| 123 | |
| 124 | void Utility::saveState(unsigned slot) { |
| 125 | if(program->active == nullptr) return; |
| 126 | serializer s = system().serialize(); |
| 127 | if(s.size() == 0) return; |
| 128 | directory::create({pathname[0], "bsnes/"}); |
| 129 | if(file::write({pathname[0], "bsnes/state-", slot, ".bsa"}, s.data(), s.size()) == false); |
| 130 | showMessage({"Saved to slot ", slot}); |
| 131 | } |
| 132 | |
| 133 | void Utility::loadState(unsigned slot) { |
| 134 | if(program->active == nullptr) return; |
| 135 | auto memory = file::read({pathname[0], "bsnes/state-", slot, ".bsa"}); |
| 136 | if(memory.size() == 0) return showMessage({"Unable to locate slot ", slot, " state"}); |
| 137 | serializer s(memory.data(), memory.size()); |
| 138 | if(system().unserialize(s) == false) return showMessage({"Slot ", slot, " state incompatible"}); |
| 139 | showMessage({"Loaded from slot ", slot}); |
| 140 | } |
| 141 | |
| 142 | void Utility::tracerToggle() { |
| 143 | if(program->active == nullptr) return; |
| 144 | tracerEnable = !tracerEnable; |
| 145 | bool result = system().tracerEnable(tracerEnable); |
| 146 | if( tracerEnable && result) return utility->showMessage("Tracer activated"); |
| 147 | if( tracerEnable && !result) return tracerEnable = false, utility->showMessage("Unable to activate tracer"); |
| 148 | if(!tracerEnable && result) return utility->showMessage("Tracer deactivated"); |
| 149 | if(!tracerEnable && !result) return utility->showMessage("Unable to deactivate tracer"); |
| 150 | } |
| 151 | |
| 152 | void Utility::synchronizeDSP() { |
| 153 | if(program->active == nullptr) return; |
| 154 | |
| 155 | if(config->video.synchronize == false) { |
| 156 | return dspaudio.setFrequency(system().audioFrequency()); |
| 157 | } |
| 158 | |
| 159 | double inputRatio = system().audioFrequency() / system().videoFrequency(); |
| 160 | double outputRatio = config->timing.audio / config->timing.video; |
| 161 | double frequency = inputRatio / outputRatio * config->audio.frequency; |
| 162 | |
| 163 | dspaudio.setFrequency(frequency); |
| 164 | } |
| 165 | |
| 166 | void Utility::synchronizeRuby() { |
| 167 | video.set(Video::Synchronize, config->video.synchronize); |
| 168 | audio.set(Audio::Synchronize, config->audio.synchronize); |
| 169 | audio.set(Audio::Frequency, config->audio.frequency); |
| 170 | audio.set(Audio::Latency, config->audio.latency); |
| 171 | |
| 172 | switch(config->audio.resampler) { |
| 173 | case 0: dspaudio.setResampler(DSP::ResampleEngine::Linear); break; |
| 174 | case 1: dspaudio.setResampler(DSP::ResampleEngine::Hermite); break; |
| 175 | case 2: dspaudio.setResampler(DSP::ResampleEngine::Sinc); break; |
| 176 | } |
| 177 | dspaudio.setResamplerFrequency(config->audio.frequency); |
| 178 | dspaudio.setVolume(config->audio.mute ? 0.0 : config->audio.volume * 0.01); |
| 179 | synchronizeDSP(); |
| 180 | } |
| 181 | |
| 182 | void Utility::updatePalette() { |
| 183 | if(program->active == nullptr) return; |
| 184 | |
| 185 | if(config->video.shader == "Display Emulation" && config->video.driver == "OpenGL") { |
| 186 | system().paletteUpdate(Emulator::Interface::PaletteMode::Channel); |
| 187 | } else if(config->video.colorEmulation) { |
| 188 | system().paletteUpdate(Emulator::Interface::PaletteMode::Emulation); |
| 189 | } else { |
| 190 | system().paletteUpdate(Emulator::Interface::PaletteMode::Standard); |
| 191 | } |
| 192 | } |
| 193 | |
| 194 | void Utility::updateShader() { |
| 195 | if(config->video.shader == "None") { |
| 196 | video.set(Video::Shader, (const char*)""); |
| 197 | video.set(Video::Filter, Video::FilterNearest); |
| 198 | } else if(config->video.shader == "Blur") { |
| 199 | video.set(Video::Shader, (const char*)""); |
| 200 | video.set(Video::Filter, Video::FilterLinear); |
| 201 | } else if(config->video.shader == "Display Emulation" && config->video.driver != "OpenGL") { |
| 202 | video.set(Video::Shader, (const char*)""); |
| 203 | video.set(Video::Filter, Video::FilterLinear); |
| 204 | } else if(config->video.shader == "Display Emulation") { |
| 205 | if(program->active) { |
| 206 | string pathname = program->path("Video Shaders/"); |
| 207 | pathname.append("Display Emulation/"); |
| 208 | pathname.append(presentation->systemName, ".shader/"); |
| 209 | if(directory::exists(pathname)) { |
| 210 | video.set(Video::Shader, (const char*)pathname); |
| 211 | } else { |
| 212 | video.set(Video::Shader, (const char*)""); |
| 213 | video.set(Video::Filter, Video::FilterLinear); |
| 214 | } |
| 215 | } else { |
| 216 | video.set(Video::Shader, (const char*)""); |
| 217 | video.set(Video::Filter, Video::FilterLinear); |
| 218 | } |
| 219 | } else { |
| 220 | video.set(Video::Shader, (const char*)config->video.shader); |
| 221 | } |
| 222 | updatePalette(); |
| 223 | } |
| 224 | |
| 225 | void Utility::resize(bool resizeWindow) { |
| 226 | if(program->active == nullptr) { |
| 227 | auto geometry = presentation->geometry(); |
| 228 | presentation->viewport.setGeometry({0, 0, geometry.width, geometry.height}); |
| 229 | return; |
| 230 | } |
| 231 | |
| 232 | Geometry geometry = presentation->geometry(); |
| 233 | unsigned width = system().information.width; |
| 234 | unsigned height = system().information.height; |
| 235 | |
| 236 | unsigned scaledWidth = geometry.width / width; |
| 237 | unsigned scaledHeight = geometry.height / height; |
| 238 | unsigned multiplier = max(1u, min(scaledWidth, scaledHeight)); |
| 239 | |
| 240 | if(config->video.aspectCorrection) { |
| 241 | width *= system().information.aspectRatio; |
| 242 | } |
| 243 | |
| 244 | width *= multiplier; |
| 245 | height *= multiplier; |
| 246 | |
| 247 | unsigned scaleMode = 0; |
| 248 | |
| 249 | if(config->video.scaleMode == 1) { |
| 250 | width = (double)width * ((double)geometry.height / height); |
| 251 | height = geometry.height; |
| 252 | } |
| 253 | |
| 254 | if(config->video.scaleMode == 2) { |
| 255 | width = geometry.width; |
| 256 | height = geometry.height; |
| 257 | } |
| 258 | |
| 259 | if(resizeWindow == false) { |
| 260 | if(geometry.width < width ) width = geometry.width; |
| 261 | if(geometry.height < height) height = geometry.height; |
| 262 | |
| 263 | presentation->viewport.setGeometry({ |
| 264 | (geometry.width - width) / 2, (geometry.height - height) / 2, width, height |
| 265 | }); |
| 266 | } else { |
| 267 | presentation->setGeometry({geometry.x, geometry.y, width, height}); |
| 268 | presentation->viewport.setGeometry({0, 0, width, height}); |
| 269 | } |
| 270 | |
| 271 | presentation->synchronize(); |
| 272 | } |
| 273 | |
| 274 | void Utility::toggleFullScreen() { |
| 275 | static Geometry geometry; |
| 276 | |
| 277 | if(presentation->fullScreen() == false) { |
| 278 | geometry = presentation->geometry(); |
| 279 | presentation->setMenuVisible(false); |
| 280 | presentation->setStatusVisible(false); |
| 281 | presentation->setFullScreen(true); |
| 282 | input.acquire(); |
| 283 | } else { |
| 284 | input.unacquire(); |
| 285 | presentation->setMenuVisible(true); |
| 286 | presentation->setStatusVisible(true); |
| 287 | presentation->setFullScreen(false); |
| 288 | presentation->setGeometry(geometry); |
| 289 | } |
| 290 | |
| 291 | resize(); |
| 292 | } |
| 293 | |
| 294 | void Utility::updateStatus() { |
| 295 | time_t currentTime = time(0); |
| 296 | string text; |
| 297 | if((currentTime - statusTime) <= 2) { |
| 298 | text = statusMessage; |
| 299 | } else if(program->active == nullptr) { |
| 300 | text = "No cartridge loaded"; |
| 301 | } else if(program->pause || program->autopause) { |
| 302 | text = "Paused"; |
| 303 | } else { |
| 304 | text = statusText; |
| 305 | } |
| 306 | if(text != presentation->statusText()) { |
| 307 | presentation->setStatusText(text); |
| 308 | } |
| 309 | } |
| 310 | |
| 311 | void Utility::setStatusText(string text) { |
| 312 | statusText = text; |
| 313 | } |
| 314 | |
| 315 | void Utility::showMessage(string message) { |
| 316 | statusTime = time(0); |
| 317 | statusMessage = message; |
| 318 | } |
| 319 | |
| 320 | string Utility::libraryPath() { |
| 321 | string path = string::read({configpath(), "higan/library.bml"}).strip().ltrim<1>("Path: ").transform("\\", "/"); |
| 322 | if(path.empty()) path = {userpath(), "Emulation/"}; |
| 323 | if(path.endsWith("/") == false) path.append("/"); |
| 324 | return path; |
| 325 | } |
| 326 | |
| 327 | Utility::Utility() { |
| 328 | tracerEnable = false; |
| 329 | statusTime = 0; |
| 330 | } |