Switched to new cleaner argument system

This commit is contained in:
AlexandreRouma
2022-02-24 20:49:53 +01:00
parent 5c138aa4a5
commit 2779516378
50 changed files with 777 additions and 205 deletions

View File

@ -16,7 +16,6 @@
#include <gui/style.h>
#include <gui/menus/theme.h>
#include <filesystem>
#include <options.h>
// Credit to the ImGui android OpenGL3 example for a lot of this code!
@ -36,9 +35,10 @@ namespace backend {
int PollUnicodeChars();
void doPartialInit() {
std::string root = core::args["root"];
backend::init();
style::loadFonts(options::opts.root + "/res"); // TODO: Don't hardcode, use config
icons::load(options::opts.root + "/res");
style::loadFonts(root + "/res"); // TODO: Don't hardcode, use config
icons::load(root + "/res");
thememenu::applyTheme();
ImGui::GetStyle().ScaleAllSizes(style::uiScale);
gui::mainWindow.setFirstMenuRender();

View File

@ -86,6 +86,31 @@ public:
return sval;
}
bool b() {
if (type != CLI_ARG_TYPE_BOOL && type != CLI_ARG_TYPE_VOID) { throw std::runtime_error("Not a bool"); }
return bval;
}
int i() {
if (type != CLI_ARG_TYPE_INT) { throw std::runtime_error("Not an int"); }
return ival;
}
float f() {
if (type != CLI_ARG_TYPE_FLOAT) { throw std::runtime_error("Not a float"); }
return (float)fval;
}
double d() {
if (type != CLI_ARG_TYPE_FLOAT) { throw std::runtime_error("Not a float"); }
return fval;
}
const std::string& s() {
if (type != CLI_ARG_TYPE_STRING) { throw std::runtime_error("Not a string"); }
return sval;
}
friend CommandArgsParser;
CLIArgType type;

View File

@ -11,7 +11,6 @@
#include <stb_image.h>
#include <config.h>
#include <core.h>
#include <options.h>
#include <filesystem>
#include <gui/menus/theme.h>
#include <backend.h>
@ -41,7 +40,7 @@ namespace core {
void setInputSampleRate(double samplerate) {
// Forward this to the server
if (options::opts.serverMode) { server::setInputSampleRate(samplerate); return; }
if (args["server"].b()) { server::setInputSampleRate(samplerate); return; }
sigpath::signalPath.sourceSampleRate = samplerate;
double effectiveSr = samplerate / ((double)(1 << sigpath::signalPath.decimation));
@ -75,25 +74,25 @@ int sdrpp_main(int argc, char* argv[]) {
return 0;
}
// Load default options and parse command line
options::loadDefaults();
if (!options::parse(argc, argv)) { return -1; }
bool serverMode = core::args["server"];
#ifdef _WIN32
if (!options::opts.showConsole && !options::opts.serverMode) { FreeConsole(); }
if (!core::args["con"].b() && !serverMode) { FreeConsole(); }
#endif
// Check root directory
if (!std::filesystem::exists(options::opts.root)) {
spdlog::warn("Root directory {0} does not exist, creating it", options::opts.root);
if (!std::filesystem::create_directories(options::opts.root)) {
spdlog::error("Could not create root directory {0}", options::opts.root);
std::string root = core::args["root"];
if (!std::filesystem::exists(root)) {
spdlog::warn("Root directory {0} does not exist, creating it", root);
if (!std::filesystem::create_directories(root)) {
spdlog::error("Could not create root directory {0}", root);
return -1;
}
}
if (!std::filesystem::is_directory(options::opts.root)) {
spdlog::error("{0} is not a directory", options::opts.root);
// Check that the path actually is a directory
if (!std::filesystem::is_directory(root)) {
spdlog::error("{0} is not a directory", root);
return -1;
}
@ -225,8 +224,8 @@ int sdrpp_main(int argc, char* argv[]) {
defConfig["modulesDirectory"] = "../Plugins";
defConfig["resourcesDirectory"] = "../Resources";
#elif defined(__ANDROID__)
defConfig["modulesDirectory"] = options::opts.root + "/modules";
defConfig["resourcesDirectory"] = options::opts.root + "/res";
defConfig["modulesDirectory"] = root + "/modules";
defConfig["resourcesDirectory"] = root + "/res";
#else
defConfig["modulesDirectory"] = INSTALL_PREFIX "/lib/sdrpp/plugins";
defConfig["resourcesDirectory"] = INSTALL_PREFIX "/share/sdrpp";
@ -234,7 +233,7 @@ int sdrpp_main(int argc, char* argv[]) {
// Load config
spdlog::info("Loading config");
core::configManager.setPath(options::opts.root + "/config.json");
core::configManager.setPath(root + "/config.json");
core::configManager.load(defConfig);
core::configManager.enableAutoSave();
core::configManager.acquire();
@ -296,7 +295,7 @@ int sdrpp_main(int argc, char* argv[]) {
core::configManager.release(true);
if (options::opts.serverMode) { return server::main(); }
if (serverMode) { return server::main(); }
core::configManager.acquire();
std::string resDir = core::configManager.conf["resourcesDirectory"];
@ -314,6 +313,9 @@ int sdrpp_main(int argc, char* argv[]) {
int biRes = backend::init(resDir);
if (biRes < 0) { return biRes; }
// Intialize SmGui in normal mode
SmGui::init(false);
if (!style::loadFonts(resDir)) { return -1; }
thememenu::init(resDir);
LoadingScreen::init();

View File

@ -1,7 +1,6 @@
#include <gui/icons.h>
#include <stdint.h>
#include <config.h>
#include <options.h>
#define STB_IMAGE_IMPLEMENTATION
#include <imgui/stb_image.h>

View File

@ -24,7 +24,6 @@
#include <filesystem>
#include <signal_path/source.h>
#include <gui/dialogs/loading_screen.h>
#include <options.h>
#include <gui/colormaps.h>
#include <gui/widgets/snr_meter.h>
#include <gui/tuner.h>

View File

@ -1,6 +1,5 @@
#include <gui/menus/theme.h>
#include <gui/gui.h>
#include <options.h>
#include <core.h>
#include <gui/style.h>

View File

@ -1,6 +1,5 @@
#include "smgui.h"
#include "style.h"
#include <options.h>
#include <gui/widgets/stepped_slider.h>
#include <gui/gui.h>
@ -25,6 +24,7 @@ namespace SmGui {
std::string diffId = "";
DrawListElem diffValue;
bool nextItemFillWidth = false;
bool serverMode = false;
std::string ImStrToString(const char* imstr) {
int len = 0;
@ -33,6 +33,10 @@ namespace SmGui {
return std::string(imstr, end);
}
void init(bool server) {
serverMode = server;
}
// Rec/Play functions
void setDiff(std::string id, SmGui::DrawListElem value) {
diffId = id;
@ -457,7 +461,7 @@ namespace SmGui {
// Format functions
void FillWidth() {
if (!options::opts.serverMode) {
if (!serverMode) {
nextItemFillWidth = true;
ImGui::SetNextItemWidth(ImGui::GetContentRegionAvail().x);
return;
@ -466,17 +470,17 @@ namespace SmGui {
}
void SameLine() {
if (!options::opts.serverMode) { ImGui::SameLine(); return; }
if (!serverMode) { ImGui::SameLine(); return; }
if (rdl) { rdl->pushStep(DRAW_STEP_SAME_LINE, false); }
}
void BeginDisabled() {
if (!options::opts.serverMode) { style::beginDisabled(); return; }
if (!serverMode) { style::beginDisabled(); return; }
if (rdl) { rdl->pushStep(DRAW_STEP_BEGIN_DISABLED, false); }
}
void EndDisabled() {
if (!options::opts.serverMode) { style::endDisabled(); return; }
if (!serverMode) { style::endDisabled(); return; }
if (rdl) { rdl->pushStep(DRAW_STEP_END_DISABLED, false); }
}
@ -484,7 +488,7 @@ namespace SmGui {
// Widget functions
bool Combo(const char *label, int *current_item, const char *items_separated_by_zeros, int popup_max_height_in_items) {
nextItemFillWidth = false;
if (!options::opts.serverMode) { return ImGui::Combo(label, current_item, items_separated_by_zeros, popup_max_height_in_items); }
if (!serverMode) { return ImGui::Combo(label, current_item, items_separated_by_zeros, popup_max_height_in_items); }
if (rdl) {
rdl->pushStep(DRAW_STEP_COMBO, forceSyncForNext);
rdl->pushString(label);
@ -501,7 +505,7 @@ namespace SmGui {
}
bool Button(const char *label, ImVec2 size) {
if (!options::opts.serverMode) {
if (!serverMode) {
if (nextItemFillWidth) {
nextItemFillWidth = false;
size.x = ImGui::GetContentRegionAvail().x;
@ -519,7 +523,7 @@ namespace SmGui {
}
void Columns(int count, const char *id, bool border) {
if (!options::opts.serverMode) { ImGui::Columns(count, id, border); return; }
if (!serverMode) { ImGui::Columns(count, id, border); return; }
if (rdl) {
rdl->pushStep(DRAW_STEP_COLUMNS, forceSyncForNext);
rdl->pushInt(count);
@ -530,12 +534,12 @@ namespace SmGui {
}
void NextColumn() {
if (!options::opts.serverMode) { ImGui::NextColumn(); return; }
if (!serverMode) { ImGui::NextColumn(); return; }
if (rdl) { rdl->pushStep(DRAW_STEP_NEXT_COLUMN, false); }
}
bool RadioButton(const char *label, bool active) {
if (!options::opts.serverMode) { return ImGui::RadioButton(label, active); }
if (!serverMode) { return ImGui::RadioButton(label, active); }
if (rdl) {
rdl->pushStep(DRAW_STEP_RADIO_BUTTON, forceSyncForNext);
rdl->pushString(label);
@ -546,17 +550,17 @@ namespace SmGui {
}
void BeginGroup() {
if (!options::opts.serverMode) { ImGui::BeginGroup(); return; }
if (!serverMode) { ImGui::BeginGroup(); return; }
if (rdl) { rdl->pushStep(DRAW_STEP_BEGIN_GROUP, false); }
}
void EndGroup() {
if (!options::opts.serverMode) { ImGui::EndGroup(); return; }
if (!serverMode) { ImGui::EndGroup(); return; }
if (rdl) { rdl->pushStep(DRAW_STEP_END_GROUP, false); }
}
void LeftLabel(const char *text) {
if (!options::opts.serverMode) { ImGui::LeftLabel(text); return; }
if (!serverMode) { ImGui::LeftLabel(text); return; }
if (rdl) {
rdl->pushStep(DRAW_STEP_LEFT_LABEL, forceSyncForNext);
rdl->pushString(text);
@ -566,7 +570,7 @@ namespace SmGui {
bool SliderInt(const char *label, int *v, int v_min, int v_max, FormatString format, ImGuiSliderFlags flags) {
nextItemFillWidth = false;
if (!options::opts.serverMode) { return ImGui::SliderInt(label, v, v_min, v_max, fmtStr[format], flags); }
if (!serverMode) { return ImGui::SliderInt(label, v, v_min, v_max, fmtStr[format], flags); }
if (rdl) {
rdl->pushStep(DRAW_STEP_SLIDER_INT, forceSyncForNext);
rdl->pushString(label);
@ -586,7 +590,7 @@ namespace SmGui {
bool SliderFloatWithSteps(const char *label, float *v, float v_min, float v_max, float v_step, FormatString display_format) {
nextItemFillWidth = false;
if (!options::opts.serverMode) { return ImGui::SliderFloatWithSteps(label, v, v_min, v_max, v_step, fmtStr[display_format]); }
if (!serverMode) { return ImGui::SliderFloatWithSteps(label, v, v_min, v_max, v_step, fmtStr[display_format]); }
if (rdl) {
rdl->pushStep(DRAW_STEP_SLIDER_FLOAT_WITH_STEPS, forceSyncForNext);
rdl->pushString(label);
@ -606,7 +610,7 @@ namespace SmGui {
bool InputInt(const char *label, int *v, int step, int step_fast, ImGuiInputTextFlags flags) {
nextItemFillWidth = false;
if (!options::opts.serverMode) { return ImGui::InputInt(label, v, step, step_fast, flags); }
if (!serverMode) { return ImGui::InputInt(label, v, step, step_fast, flags); }
if (rdl) {
rdl->pushStep(DRAW_STEP_INPUT_INT, forceSyncForNext);
rdl->pushString(label);
@ -624,7 +628,7 @@ namespace SmGui {
}
bool Checkbox(const char *label, bool *v) {
if (!options::opts.serverMode) { return ImGui::Checkbox(label, v); }
if (!serverMode) { return ImGui::Checkbox(label, v); }
if (rdl) {
rdl->pushStep(DRAW_STEP_CHECKBOX, forceSyncForNext);
rdl->pushString(label);
@ -640,7 +644,7 @@ namespace SmGui {
bool SliderFloat(const char *label, float *v, float v_min, float v_max, FormatString format, ImGuiSliderFlags flags) {
nextItemFillWidth = false;
if (!options::opts.serverMode) { return ImGui::SliderFloat(label, v, v_min, v_max, fmtStr[format], flags); }
if (!serverMode) { return ImGui::SliderFloat(label, v, v_min, v_max, fmtStr[format], flags); }
if (rdl) {
rdl->pushStep(DRAW_STEP_SLIDER_FLOAT, forceSyncForNext);
rdl->pushString(label);
@ -660,7 +664,7 @@ namespace SmGui {
bool InputText(const char *label, char *buf, size_t buf_size, ImGuiInputTextFlags flags) {
nextItemFillWidth = false;
if (!options::opts.serverMode) { return ImGui::InputText(label, buf, buf_size, flags); }
if (!serverMode) { return ImGui::InputText(label, buf, buf_size, flags); }
if (rdl) {
rdl->pushStep(DRAW_STEP_INPUT_TEXT, forceSyncForNext);
rdl->pushString(label);
@ -677,7 +681,7 @@ namespace SmGui {
}
void Text(const char* str) {
if (!options::opts.serverMode) { ImGui::TextUnformatted(str); return; }
if (!serverMode) { ImGui::TextUnformatted(str); return; }
if (rdl) {
rdl->pushStep(DRAW_STEP_TEXT, false);
rdl->pushString(str);
@ -685,7 +689,7 @@ namespace SmGui {
}
void TextColored(const ImVec4 &col, const char *str) {
if (!options::opts.serverMode) { ImGui::TextColored(col, "%s", str); return; }
if (!serverMode) { ImGui::TextColored(col, "%s", str); return; }
if (rdl) {
rdl->pushStep(DRAW_STEP_TEXT_COLORED, false);
rdl->pushFloat(col.x);
@ -697,7 +701,7 @@ namespace SmGui {
}
void OpenPopup(const char *str_id, ImGuiPopupFlags popup_flags) {
if (!options::opts.serverMode) { ImGui::OpenPopup(str_id, popup_flags); return; }
if (!serverMode) { ImGui::OpenPopup(str_id, popup_flags); return; }
if (rdl) {
rdl->pushStep(DRAW_STEP_OPEN_POPUP, false);
rdl->pushString(str_id);
@ -706,7 +710,7 @@ namespace SmGui {
}
bool BeginPopup(const char *str_id, ImGuiWindowFlags flags) {
if (!options::opts.serverMode) { return ImGui::BeginPopup(str_id, flags); }
if (!serverMode) { return ImGui::BeginPopup(str_id, flags); }
if (rdl) {
rdl->pushStep(DRAW_STEP_BEGIN_POPUP, false);
rdl->pushString(str_id);
@ -716,14 +720,14 @@ namespace SmGui {
}
void EndPopup() {
if (!options::opts.serverMode) { ImGui::EndPopup(); return; }
if (!serverMode) { ImGui::EndPopup(); return; }
if (rdl) {
rdl->pushStep(DRAW_STEP_END_POPUP, false);
}
}
bool BeginTable(const char *str_id, int column, ImGuiTableFlags flags, const ImVec2 &outer_size, float inner_width) {
if (!options::opts.serverMode) { return ImGui::BeginTable(str_id, column, flags, outer_size, inner_width); }
if (!serverMode) { return ImGui::BeginTable(str_id, column, flags, outer_size, inner_width); }
if (rdl) {
rdl->pushStep(DRAW_STEP_BEGIN_TABLE, false);
rdl->pushString(str_id);
@ -737,14 +741,14 @@ namespace SmGui {
}
void EndTable() {
if (!options::opts.serverMode) { ImGui::EndTable(); return; }
if (!serverMode) { ImGui::EndTable(); return; }
if (rdl) {
rdl->pushStep(DRAW_STEP_END_TABLE, false);
}
}
void TableNextRow(ImGuiTableRowFlags row_flags, float min_row_height) {
if (!options::opts.serverMode) { ImGui::TableNextRow(row_flags, min_row_height); return; }
if (!serverMode) { ImGui::TableNextRow(row_flags, min_row_height); return; }
if (rdl) {
rdl->pushStep(DRAW_STEP_TABLE_NEXT_ROW, false);
rdl->pushInt(row_flags);
@ -753,7 +757,7 @@ namespace SmGui {
}
void TableSetColumnIndex(int column_n) {
if (!options::opts.serverMode) { ImGui::TableSetColumnIndex(column_n); return; }
if (!serverMode) { ImGui::TableSetColumnIndex(column_n); return; }
if (rdl) {
rdl->pushStep(DRAW_STEP_TABLE_SET_COLUMN_INDEX, false);
rdl->pushInt(column_n);
@ -761,7 +765,7 @@ namespace SmGui {
}
void SetNextItemWidth(float item_width) {
if (!options::opts.serverMode) { ImGui::SetNextItemWidth(item_width); return; }
if (!serverMode) { ImGui::SetNextItemWidth(item_width); return; }
if (rdl) {
rdl->pushStep(DRAW_STEP_SET_NEXT_ITEM_WIDTH, false);
rdl->pushFloat(item_width);

View File

@ -100,6 +100,7 @@ namespace SmGui {
// Rec/Play functions
// TODO: Maybe move verification to the load function instead of checking in drawFrame
void init(bool server);
void setDiff(std::string id, SmGui::DrawListElem value);
void startRecord(DrawList* dl);
void stopRecord();

View File

@ -2,7 +2,6 @@
#include <imgui.h>
#include <imgui_internal.h>
#include <config.h>
#include <options.h>
#include <spdlog/spdlog.h>
#include <filesystem>

View File

@ -1,11 +1,12 @@
#include <gui/widgets/file_select.h>
#include <regex>
#include <options.h>
#include <filesystem>
#include <gui/file_dialogs.h>
#include <core.h>
FileSelect::FileSelect(std::string defaultPath, std::vector<std::string> filter) {
_filter = filter;
root = core::args["root"];
setPath(defaultPath);
}
@ -54,7 +55,7 @@ void FileSelect::setPath(std::string path, bool markChanged) {
}
std::string FileSelect::expandString(std::string input) {
input = std::regex_replace(input, std::regex("%ROOT%"), options::opts.root);
input = std::regex_replace(input, std::regex("%ROOT%"), root);
return std::regex_replace(input, std::regex("//"), "/");
}

View File

@ -22,6 +22,7 @@ private:
void worker();
std::thread workerThread;
std::vector<std::string> _filter;
std::string root = "";
bool pathValid = false;
bool dialogOpen = false;

View File

@ -1,10 +1,11 @@
#include <gui/widgets/folder_select.h>
#include <regex>
#include <options.h>
#include <filesystem>
#include <gui/file_dialogs.h>
#include <core.h>
FolderSelect::FolderSelect(std::string defaultPath) {
root = core::args["root"];
setPath(defaultPath);
}
@ -53,7 +54,7 @@ void FolderSelect::setPath(std::string path, bool markChanged) {
}
std::string FolderSelect::expandString(std::string input) {
input = std::regex_replace(input, std::regex("%ROOT%"), options::opts.root);
input = std::regex_replace(input, std::regex("%ROOT%"), root);
return std::regex_replace(input, std::regex("//"), "/");
}

View File

@ -20,6 +20,7 @@ public:
private:
void worker();
std::thread workerThread;
std::string root = "";
bool pathValid = false;
bool dialogOpen = false;

View File

@ -1,57 +0,0 @@
#include <options.h>
#include <spdlog/spdlog.h>
#include <stdlib.h>
#include <filesystem>
namespace options {
CMDLineOptions opts;
void loadDefaults() {
#if defined(_WIN32)
opts.root = ".";
opts.showConsole = false;
#elif defined(IS_MACOS_BUNDLE)
std::string homedir = getenv("HOME");
opts.root = homedir + "/Library/Application Support/sdrpp";
#elif defined(__ANDROID__)
opts.root = "/storage/self/primary/sdrpp";
#else
std::string homedir = getenv("HOME");
opts.root = homedir + "/.config/sdrpp";
#endif
opts.root = std::filesystem::absolute(opts.root).string();
opts.serverHost = "0.0.0.0";
opts.serverPort = 5259;
}
bool parse(int argc, char* argv[]) {
for (int i = 1; i < argc; i++) {
char* arg = argv[i];
if (!strcmp(arg, "-r") || !strcmp(arg, "--root")) {
if (i == argc - 1) { return false; }
opts.root = std::filesystem::absolute(argv[++i]).string();
}
else if (!strcmp(arg, "-s") || !strcmp(arg, "--show-console")) {
opts.showConsole = true;
}
else if (!strcmp(arg, "--server")) {
opts.serverMode = true;
}
else if (!strcmp(arg, "-a") || !strcmp(arg, "--addr")) {
if (i == argc - 1) { return false; }
opts.serverHost = argv[++i];
opts.showConsole = true;
}
else if (!strcmp(arg, "-p") || !strcmp(arg, "--port")) {
if (i == argc - 1) { return false; }
sscanf(argv[++i], "%d", &opts.serverPort);
opts.showConsole = true;
}
else {
spdlog::error("Invalid command line option: {0}", arg);
return false;
}
}
return true;
}
}

View File

@ -1,18 +0,0 @@
#pragma once
#include <string>
#include <module.h>
namespace options {
struct CMDLineOptions {
std::string root;
bool showConsole;
bool serverMode;
std::string serverHost;
int serverPort;
};
SDRPP_EXPORT CMDLineOptions opts;
void loadDefaults();
bool parse(int argc, char* argv[]);
}

View File

@ -3,7 +3,6 @@
#include <spdlog/spdlog.h>
#include <version.h>
#include <config.h>
#include <options.h>
#include <filesystem>
#include <dsp/types.h>
#include <signal_path/signal_path.h>
@ -75,6 +74,7 @@ namespace server {
// Initialize compressor
cctx = ZSTD_createCCtx();
// Load config
core::configManager.acquire();
std::string modulesDir = core::configManager.conf["modulesDirectory"];
std::vector<std::string> modules = core::configManager.conf["modules"];
@ -83,8 +83,10 @@ namespace server {
core::configManager.release();
modulesDir = std::filesystem::absolute(modulesDir).string();
spdlog::info("Loading modules");
// Intialize SmGui in server mode
SmGui::init(true);
spdlog::info("Loading modules");
// Load modules and check type to only load sources ( TODO: Have a proper type parameter int the info )
// TODO LATER: Add whitelist/blacklist stuff
if (std::filesystem::is_directory(modulesDir)) {
@ -146,10 +148,12 @@ namespace server {
sigpath::sourceManager.selectSource(sourceList[sourceId]);
// TODO: Use command line option
listener = net::listen(options::opts.serverHost, options::opts.serverPort);
std::string host = core::args["addr"];
int port = core::args["port"];
listener = net::listen(host, port);
listener->acceptAsync(_clientHandler, NULL);
spdlog::info("Ready, listening on {0}:{1}", options::opts.serverHost, options::opts.serverPort);
spdlog::info("Ready, listening on {0}:{1}", host, port);
while(1) { std::this_thread::sleep_for(std::chrono::milliseconds(100)); }
return 0;

View File

@ -2,7 +2,7 @@
#include <signal_path/source.h>
#include <spdlog/spdlog.h>
#include <signal_path/signal_path.h>
#include <options.h>
#include <core.h>
SourceManager::SourceManager() {
}
@ -50,7 +50,7 @@ void SourceManager::selectSource(std::string name) {
selectedHandler = sources[name];
selectedHandler->selectHandler(selectedHandler->ctx);
selectedName = name;
if (options::opts.serverMode) {
if (core::args["server"].b()) {
server::setInput(selectedHandler->stream);
}
else {