Compare commits

...
Sign in to create a new pull request.

1 commit

Author SHA1 Message Date
Andrea Odetti
4d72e03138 boost -> getopt 2022-02-17 12:09:40 +00:00
13 changed files with 371 additions and 232 deletions

View file

@ -1,6 +1,7 @@
include(GNUInstallDirs)
set(SOURCE_FILES
argparser.cpp
commonframe.cpp
gnuframe.cpp
fileregistry.cpp
@ -12,6 +13,7 @@ set(SOURCE_FILES
)
set(HEADER_FILES
argparser.h
commonframe.h
gnuframe.h
fileregistry.h
@ -28,7 +30,6 @@ add_library(common2 STATIC
)
find_package(Boost REQUIRED
COMPONENTS program_options
)
target_include_directories(common2 PRIVATE
@ -37,7 +38,6 @@ target_include_directories(common2 PRIVATE
)
target_link_libraries(common2 PRIVATE
Boost::program_options
appleii
windows
)

View file

@ -0,0 +1,319 @@
#include "StdAfx.h"
#include "frontends/common2/programoptions.h"
#include "linux/version.h"
#include "Memory.h"
#include <getopt.h>
#include <regex>
#include <iostream>
#include <iomanip>
namespace
{
struct OptionData_t
{
const char * name;
int has_arg;
int val;
const char * description;
};
bool isShort(const int val)
{
return val >= 0 && val <= 0xFF;
}
void printHelp(const std::vector<std::pair<std::string, std::vector<OptionData_t>>> & data)
{
for (const auto & categories : data)
{
std::cout << categories.first << ":" << std::endl;
for (const auto & option : categories.second)
{
std::cout << " ";
std::ostringstream value;
if (isShort(option.val))
{
value << "[ -" << char(option.val) << " ] ";
}
value << "--" << option.name;
if (option.has_arg == required_argument)
{
value << " arg";
}
std::cout << std::left << std::setw(25) << value.str() << "\t" << option.description << std::endl;
}
std::cout << std::endl;
}
}
void parseGeometry(const std::string & s, common2::Geometry & geometry)
{
std::smatch m;
if (std::regex_match(s, m, std::regex("^(\\d+)x(\\d+)(\\+(\\d+)\\+(\\d+))?$")))
{
const size_t groups = m.size();
if (groups == 6)
{
geometry.width = std::stoi(m.str(1));
geometry.height = std::stoi(m.str(2));
if (!m.str(3).empty())
{
geometry.x = std::stoi(m.str(4));
geometry.y = std::stoi(m.str(5));
}
return;
}
}
throw std::runtime_error("Invalid geometry: " + s);
}
void extractOptions(
const std::vector<std::pair<std::string, std::vector<OptionData_t>>> & data,
std::vector<option> & longOptions,
std::string & shortOptions
)
{
longOptions.clear();
std::ostringstream shorts;
for (const auto & categories : data)
{
for (const auto & option : categories.second)
{
longOptions.push_back({option.name, option.has_arg, nullptr, option.val});
const int val = option.val;
if (isShort(val))
{
shorts << char(val);
if (option.has_arg == required_argument)
{
shorts << ":";
}
}
}
}
longOptions.push_back({nullptr, 0, nullptr, 0});
shortOptions = shorts.str();
}
}
namespace common2
{
bool getEmulatorOptions(int argc, char * argv [], const std::string & edition, EmulatorOptions & options)
{
const int FIXED_SPEED = 1000;
const int SDL_DRIVER = 1001;
const int GL_SWAP = 1002;
const int NO_IMGUI = 1003;
const int MEM_CLEAR = 1004;
const int GEOMETRY = 1005;
const int NO_SQUARING = 1006;
const int DEVICE_NAME = 1007;
const int HEADLESS = 1008;
const int ROM = 1009;
const int F8ROM = 1010;
const std::string name = "Apple Emulator for " + edition + " (based on AppleWin " + getVersion() + ")";
const std::vector<std::pair<std::string, std::vector<OptionData_t>>> allOptions =
{
{
name.c_str(),
{
{"help", no_argument, 'h', "Print this help message"},
}
},
{
"General",
{
{"log", no_argument, 'l', "Log to AppleWin.log"},
{"conf", required_argument, 'c', "Select configuration file"},
{"qt-ini", no_argument, 'q', "Use Qt ini file (read only)"},
{"registry", required_argument, 'r', "Registry options section.path=value"},
}
},
{
"Emulator",
{
{"mem-clear", required_argument, MEM_CLEAR, "Memory initialization pattern [0..7]"},
{"fixed-speed", no_argument, FIXED_SPEED, "Fixed (non-adaptive) speed"},
{"no-squaring", no_argument, NO_SQUARING, "Gamepad range is (already) a square"},
{"device-name", required_argument, DEVICE_NAME, "Gamepad device name"},
{"headless", no_argument, HEADLESS, "Headless: disable video (freewheel)"},
{"ntsc", no_argument, 'n', "NTSC: execute NTSC code"},
{"benchmark", no_argument, 'b', "Benchmark emulator"},
{"rom", required_argument, ROM, "Custom 12k/16k ROM"},
{"f8rom", required_argument, F8ROM, "Custom 2k ROM"},
}
},
{
"Disk",
{
{"d1", required_argument, '1', "Disk in S6D1 drive"},
{"d2", required_argument, '2', "Disk in S6D2 drive"},
}
},
{
"Snapshot",
{
{"state-filename", required_argument, 'f', "Set snapshot filename"},
{"load-state", required_argument, 's', "Load snapshot from file"},
}
},
{
"SDL",
{
{"sdl-driver", required_argument, SDL_DRIVER, "SDL driver"},
{"gl-swap", required_argument, GL_SWAP, "SDL_GL_SwapInterval"},
{"no-imgui", no_argument, NO_IMGUI, "Plain SDL2 renderer"},
{"geometry", required_argument, GEOMETRY, "WxH[+X+Y]"},
}
}
};
std::vector<option> longOptions;
std::string shortOptions;
extractOptions(allOptions, longOptions, shortOptions);
options.imgui = true;
options.paddleSquaring = true;
while (1)
{
int optionIndex = 0;
const int c = getopt_long(argc, argv, shortOptions.c_str(), longOptions.data(), &optionIndex);
if (c == -1)
{
return true;
}
switch (c)
{
case '?':
case 'h':
{
printHelp(allOptions);
return false;
}
case 'c':
{
options.configurationFile = optarg;
break;
}
case 'l':
{
options.log = true;
break;
}
case '1':
{
options.disk1 = optarg;
break;
}
case '2':
{
options.disk2 = optarg;
break;
}
case 'f':
{
options.snapshotFilename = optarg;
options.loadSnapshot = false;
break;
}
case 's':
{
options.snapshotFilename = optarg;
options.loadSnapshot = true;
break;
}
case 'q':
{
options.useQtIni = true;
break;
}
case 'r':
{
options.registryOptions.emplace_back(optarg);
break;
}
case 'n':
{
options.ntsc = true;
break;
}
case 'b':
{
options.benchmark = true;
break;
}
case FIXED_SPEED:
{
options.fixedSpeed = true;
break;
}
case SDL_DRIVER:
{
options.sdlDriver = std::stoi(optarg);
break;
}
case GL_SWAP:
{
options.glSwapInterval = std::stoi(optarg);
break;
}
case NO_IMGUI:
{
options.imgui = false;
break;
}
case MEM_CLEAR:
{
const int memclear = std::stoi(optarg);
if (memclear >=0 && memclear < NUM_MIP)
{
options.memclear = memclear;
}
break;
}
case GEOMETRY:
{
options.geometry.empty = false;
parseGeometry(optarg, options.geometry);
break;
}
case NO_SQUARING:
{
options.paddleSquaring = 0;
break;
}
case DEVICE_NAME:
{
options.paddleDeviceName = optarg;
break;
}
case HEADLESS:
{
options.headless = true;
break;
}
case ROM:
{
options.customRom = optarg;
break;
}
case F8ROM:
{
options.customRomF8 = optarg;
break;
}
}
}
return true;
}
}

View file

@ -0,0 +1,8 @@
#include <string>
namespace common2
{
struct EmulatorOptions;
bool getEmulatorOptions(int argc, char * argv [], const std::string & edition, EmulatorOptions & options);
}

View file

@ -3,6 +3,7 @@
#include "frontends/common2/fileregistry.h"
#include "frontends/common2/ptreeregistry.h"
#include "frontends/common2/programoptions.h"
#include "frontends/common2/utils.h"
#include "Log.h"
#include "windows.h"
@ -10,8 +11,6 @@
#include <boost/property_tree/ini_parser.hpp>
#include <sys/stat.h>
namespace
{
@ -83,32 +82,6 @@ namespace
namespace common2
{
std::string GetHomeDir()
{
const char* homeDir = getenv("HOME");
if (!homeDir)
{
throw std::runtime_error("${HOME} not set, cannot locate configuration file");
}
return std::string(homeDir);
}
std::string GetConfigFile(const std::string & filename)
{
const std::string dir = GetHomeDir() + "/.applewin";
const int status = mkdir(dir.c_str(), S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH);
if (!status || (errno == EEXIST))
{
return dir + "/" + filename;
}
else
{
const char * s = strerror(errno);
LogFileOutput("No registry. Cannot create %s in %s: %s\n", filename.c_str(), dir.c_str(), s);
return std::string();
}
}
std::shared_ptr<Registry> CreateFileRegistry(const EmulatorOptions & options)
{

View file

@ -10,8 +10,6 @@ namespace common2
struct EmulatorOptions;
std::string GetConfigFile(const std::string & filename);
std::shared_ptr<Registry> CreateFileRegistry(const EmulatorOptions & options);
std::string GetHomeDir();
}

View file

@ -1,6 +1,6 @@
#include "StdAfx.h"
#include "frontends/common2/gnuframe.h"
#include "frontends/common2/fileregistry.h"
#include "frontends/common2/utils.h"
#include <sys/stat.h>
#include <unistd.h>

View file

@ -1,48 +1,11 @@
#include "frontends/common2/programoptions.h"
#include "frontends/common2/fileregistry.h"
#include "linux/version.h"
#include "frontends/common2/utils.h"
#include "linux/paddle.h"
#include <boost/program_options.hpp>
#include "StdAfx.h"
#include "Memory.h"
#include "Log.h"
#include "Disk.h"
#include "Utilities.h"
#include "Core.h"
#include <iostream>
#include <regex>
namespace po = boost::program_options;
namespace
{
void parseGeometry(const std::string & s, common2::Geometry & geometry)
{
std::smatch m;
if (std::regex_match(s, m, std::regex("^(\\d+)x(\\d+)(\\+(\\d+)\\+(\\d+))?$")))
{
const size_t groups = m.size();
if (groups == 6)
{
geometry.width = std::stoi(m.str(1));
geometry.height = std::stoi(m.str(2));
if (!m.str(3).empty())
{
geometry.x = std::stoi(m.str(4));
geometry.y = std::stoi(m.str(5));
}
return;
}
}
throw std::runtime_error("Invalid sizes: " + s);
}
}
namespace common2
{
@ -52,160 +15,6 @@ namespace common2
configurationFile = GetConfigFile("applewin.conf");
}
bool getEmulatorOptions(int argc, const char * argv [], const std::string & edition, EmulatorOptions & options)
{
const std::string name = "Apple Emulator for " + edition + " (based on AppleWin " + getVersion() + ")";
po::options_description desc(name);
desc.add_options()
("help,h", "Print this help message")
;
po::options_description configDesc("configuration");
configDesc.add_options()
("conf", po::value<std::string>()->default_value(options.configurationFile), "Select configuration file")
("registry,r", po::value<std::vector<std::string>>(), "Registry options section.path=value")
("qt-ini,q", "Use Qt ini file (read only)")
;
desc.add(configDesc);
po::options_description diskDesc("Disk");
diskDesc.add_options()
("d1,1", po::value<std::string>(), "Disk in 1st drive")
("d2,2", po::value<std::string>(), "Disk in 2nd drive")
;
desc.add(diskDesc);
po::options_description snapshotDesc("Snapshot");
snapshotDesc.add_options()
("state-filename,f", po::value<std::string>(), "Set snapshot filename")
("load-state,s", po::value<std::string>(), "Load snapshot from file")
;
desc.add(snapshotDesc);
po::options_description memoryDesc("Memory");
memoryDesc.add_options()
("memclear", po::value<int>()->default_value(options.memclear), "Memory initialization pattern [0..7]")
;
desc.add(memoryDesc);
po::options_description emulatorDesc("Emulator");
emulatorDesc.add_options()
("log", "Log to AppleWin.log")
("headless", "Headless: disable video (freewheel)")
("fixed-speed", "Fixed (non-adaptive) speed")
("ntsc,nt", "NTSC: execute NTSC code")
("benchmark,b", "Benchmark emulator")
("rom", po::value<std::string>(), "Custom 12k/16k ROM")
("f8rom", po::value<std::string>(), "Custom 2k ROM")
;
desc.add(emulatorDesc);
po::options_description sdlDesc("SDL");
sdlDesc.add_options()
("sdl-driver", po::value<int>()->default_value(options.sdlDriver), "SDL driver")
("gl-swap", po::value<int>()->default_value(options.glSwapInterval), "SDL_GL_SwapInterval")
("no-imgui", "Plain SDL2 renderer")
("geometry", po::value<std::string>(), "WxH[+X+Y]")
;
desc.add(sdlDesc);
po::options_description paddleDesc("Paddle");
paddleDesc.add_options()
("no-squaring", "Gamepad range is (already) a square")
("device-name", po::value<std::string>(), "Gamepad device name")
;
desc.add(paddleDesc);
po::variables_map vm;
try
{
po::store(po::parse_command_line(argc, argv, desc), vm);
if (vm.count("help"))
{
std::cout << desc << std::endl;
return false;
}
options.configurationFile = vm["conf"].as<std::string>();
options.useQtIni = vm.count("qt-ini");
options.sdlDriver = vm["sdl-driver"].as<int>();
options.glSwapInterval = vm["gl-swap"].as<int>();
options.imgui = vm.count("no-imgui") == 0;
if (vm.count("registry"))
{
options.registryOptions = vm["registry"].as<std::vector<std::string> >();
}
if (vm.count("d1"))
{
options.disk1 = vm["d1"].as<std::string>();
}
if (vm.count("d2"))
{
options.disk2 = vm["d2"].as<std::string>();
}
if (vm.count("load-state"))
{
options.snapshotFilename = vm["load-state"].as<std::string>();
options.loadSnapshot = true;
}
if (vm.count("state-filename"))
{
options.snapshotFilename = vm["state-filename"].as<std::string>();
options.loadSnapshot = false;
}
if (vm.count("rom"))
{
options.customRom = vm["rom"].as<std::string>();
}
if (vm.count("f8rom"))
{
options.customRomF8 = vm["f8rom"].as<std::string>();
}
const int memclear = vm["memclear"].as<int>();
if (memclear >=0 && memclear < NUM_MIP)
options.memclear = memclear;
options.benchmark = vm.count("benchmark") > 0;
options.headless = vm.count("headless") > 0;
options.log = vm.count("log") > 0;
options.ntsc = vm.count("ntsc") > 0;
options.fixedSpeed = vm.count("fixed-speed") > 0;
options.paddleSquaring = vm.count("no-squaring") == 0;
if (vm.count("device-name"))
{
options.paddleDeviceName = vm["device-name"].as<std::string>();
}
if (vm.count("geometry"))
{
options.geometry.empty = false;
parseGeometry(vm["geometry"].as<std::string>(), options.geometry);
}
return true;
}
catch (const po::error& e)
{
std::cerr << "ERROR: " << e.what() << std::endl << desc << std::endl;
return false;
}
catch (const std::exception & e)
{
std::cerr << "ERROR: " << e.what() << std::endl;
return false;
}
}
void applyOptions(const EmulatorOptions & options)
{
g_nMemoryClearType = options.memclear;

View file

@ -57,8 +57,6 @@ namespace common2
std::vector<std::string> registryOptions;
};
bool getEmulatorOptions(int argc, const char * argv [], const std::string & edition, EmulatorOptions & options);
void applyOptions(const EmulatorOptions & options);
}

View file

@ -4,13 +4,42 @@
#include "SaveState.h"
#include "Registry.h"
#include "Log.h"
#include <libgen.h>
#include <unistd.h>
#include <sys/stat.h>
namespace common2
{
std::string GetHomeDir()
{
const char* homeDir = getenv("HOME");
if (!homeDir)
{
throw std::runtime_error("${HOME} not set, cannot locate configuration file");
}
return std::string(homeDir);
}
std::string GetConfigFile(const std::string & filename)
{
const std::string dir = GetHomeDir() + "/.applewin";
const int status = mkdir(dir.c_str(), S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH);
if (!status || (errno == EEXIST))
{
return dir + "/" + filename;
}
else
{
const char * s = strerror(errno);
LogFileOutput("No registry. Cannot create %s in %s: %s\n", filename.c_str(), dir.c_str(), s);
return std::string();
}
}
void setSnapshotFilename(const std::string & filename)
{
if (!filename.empty())

View file

@ -6,6 +6,9 @@ namespace common2
{
struct Geometry;
std::string GetConfigFile(const std::string & filename);
std::string GetHomeDir();
void setSnapshotFilename(const std::string & filename);
void loadGeometryFromRegistry(const std::string &section, Geometry & geometry);

View file

@ -20,6 +20,7 @@
#include "linux/context.h"
#include "frontends/common2/fileregistry.h"
#include "frontends/common2/programoptions.h"
#include "frontends/common2/argparser.h"
#include "frontends/common2/utils.h"
#include "frontends/ncurses/world.h"
#include "frontends/ncurses/nframe.h"
@ -137,7 +138,7 @@ namespace
}
}
int run_ncurses(int argc, const char * argv [])
int run_ncurses(int argc, char * argv [])
{
common2::EmulatorOptions options;
const bool run = getEmulatorOptions(argc, argv, "ncurses", options);
@ -178,7 +179,7 @@ namespace
}
int main(int argc, const char * argv [])
int main(int argc, char * argv [])
{
try
{

View file

@ -2,7 +2,7 @@
#include "frontends/sdl/imgui/sdlimguiframe.h"
#include "frontends/sdl/utils.h"
#include "frontends/common2/fileregistry.h"
#include "frontends/common2/utils.h"
#include "frontends/common2/programoptions.h"
#include "frontends/sdl/imgui/image.h"

View file

@ -11,6 +11,7 @@
#include "frontends/common2/fileregistry.h"
#include "frontends/common2/utils.h"
#include "frontends/common2/programoptions.h"
#include "frontends/common2/argparser.h"
#include "frontends/common2/timer.h"
#include "frontends/sdl/gamepad.h"
#include "frontends/sdl/sdirectsound.h"
@ -56,7 +57,7 @@ namespace
}
void run_sdl(int argc, const char * argv [])
void run_sdl(int argc, char * argv [])
{
std::cerr << std::fixed << std::setprecision(2);
@ -183,7 +184,7 @@ void run_sdl(int argc, const char * argv [])
#endif
}
int main(int argc, const char * argv [])
int main(int argc, char * argv [])
{
//First we need to start up SDL, and make sure it went ok
const Uint32 flags = SDL_INIT_VIDEO | SDL_INIT_GAMECONTROLLER | SDL_INIT_AUDIO | SDL_INIT_TIMER;