| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
#include "CliServer.hh" |
| 7 |
#include "CommandLineParser.hh" |
| 8 |
#include "Display.hh" |
| 9 |
#include "EnumSetting.hh" |
| 10 |
#include "EventDistributor.hh" |
| 11 |
#include "MSXException.hh" |
| 12 |
#include "Reactor.hh" |
| 13 |
#include "RenderSettings.hh" |
| 14 |
#include "Thread.hh" |
| 15 |
|
| 16 |
#include "one_of.hh" |
| 17 |
#include "random.hh" |
| 18 |
#include "setenv.hh" |
| 19 |
|
| 20 |
#include <SDL.h> |
| 21 |
|
| 22 |
#include <cstdio> |
| 23 |
#include <cstdlib> |
| 24 |
#include <ctime> |
| 25 |
#include <exception> |
| 26 |
#include <iostream> |
| 27 |
#ifdef _WIN32 |
| 28 |
#include "win32-arggen.hh" |
| 29 |
#endif |
| 30 |
|
| 31 |
namespace openmsx { |
| 32 |
|
| 33 |
#ifdef _WIN32 |
| 34 |
|
| 35 |
static void EnableConsoleOutput() |
| 36 |
{ |
| 37 |
if (AttachConsole(ATTACH_PARENT_PROCESS)) { |
| 38 |
freopen("CONOUT$", "w", stdout); |
| 39 |
freopen("CONOUT$", "w", stderr); |
| 40 |
} |
| 41 |
} |
| 42 |
#endif |
| 43 |
|
| 44 |
static void initializeSDL() |
| 45 |
{ |
| 46 |
int flags = 0; |
| 47 |
flags |= SDL_INIT_JOYSTICK; |
| 48 |
#ifndef NDEBUG |
| 49 |
flags |= SDL_INIT_NOPARACHUTE; |
| 50 |
#endif |
| 51 |
if (SDL_Init(flags) < 0) { |
| 52 |
throw FatalError("Couldn't init SDL: ", SDL_GetError()); |
| 53 |
} |
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
setenv("FREETYPE_PROPERTIES", "truetype:interpreter-version=35", 0); |
| 61 |
} |
| 62 |
|
| 63 |
static int main(int argc, char **argv) |
| 64 |
{ |
| 65 |
#ifdef _WIN32 |
| 66 |
EnableConsoleOutput(); |
| 67 |
#endif |
| 68 |
|
| 69 |
try { |
| 70 |
randomize(); |
| 71 |
initializeSDL(); |
| 72 |
|
| 73 |
Thread::setMainThread(); |
| 74 |
Reactor reactor; |
| 75 |
#ifdef _WIN32 |
| 76 |
(void)argc; (void)argv; |
| 77 |
ArgumentGenerator argGen; |
| 78 |
auto args = argGen.getArgs(); |
| 79 |
#else |
| 80 |
std::span<char*> args{argv, size_t(argc)}; |
| 81 |
#endif |
| 82 |
CommandLineParser parser(reactor); |
| 83 |
parser.parse(args); |
| 84 |
auto parseStatus = parser.getParseStatus(); |
| 85 |
|
| 86 |
if (parseStatus != one_of(CommandLineParser::Status::EXIT, CommandLineParser::Status::TEST)) { |
| 87 |
reactor.runStartupScripts(parser); |
| 88 |
|
| 89 |
auto& display = reactor.getDisplay(); |
| 90 |
auto& render = display.getRenderSettings().getRendererSetting(); |
| 91 |
if ((render.getEnum() == RenderSettings::RendererID::UNINITIALIZED) && |
| 92 |
(parseStatus != CommandLineParser::Status::CONTROL)) { |
| 93 |
render.setValue(render.getDefaultValue()); |
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
reactor.getEventDistributor().deliverEvents(); |
| 101 |
} |
| 102 |
|
| 103 |
CliServer cliServer(reactor.getCommandController(), |
| 104 |
reactor.getEventDistributor(), |
| 105 |
reactor.getGlobalCliComm()); |
| 106 |
|
| 107 |
if (parser.getParseStatus() == CommandLineParser::Status::RUN) { |
| 108 |
reactor.powerOn(); |
| 109 |
} |
| 110 |
display.repaint(); |
| 111 |
reactor.run(); |
| 112 |
} |
| 113 |
} catch (FatalError& e) { |
| 114 |
std::cerr << "Fatal error: " << e.getMessage() << '\n'; |
| 115 |
exitCode = 1; |
| 116 |
} catch (MSXException& e) { |
| 117 |
std::cerr << "Uncaught exception: " << e.getMessage() << '\n'; |
| 118 |
exitCode = 1; |
| 119 |
} catch (std::exception& e) { |
| 120 |
std::cerr << "Uncaught std::exception: " << e.what() << '\n'; |
| 121 |
exitCode = 1; |
| 122 |
} catch (...) { |
| 123 |
std::cerr << "Uncaught exception of unexpected type." << '\n'; |
| 124 |
exitCode = 1; |
| 125 |
} |
| 126 |
|
| 127 |
if (SDL_WasInit(SDL_INIT_EVERYTHING)) { |
| 128 |
SDL_Quit(); |
| 129 |
} |
| 130 |
|
| 131 |
return exitCode; |
| 132 |
} |
| 133 |
|
| 134 |
} |
| 135 |
|
| 136 |
|
| 137 |
int main(int argc, char **argv) |
| 138 |
{ |
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
return openmsx::main(argc, argv); |
| 144 |
} |