looper/backend.cpp
2024-03-26 18:39:02 -07:00

76 lines
No EOL
2.3 KiB
C++

#include "backend.hpp"
#ifdef GLIB_ENABLED
#include <glib-object.h>
#endif
#include "data.h"
#include "thirdparty/CLI11.hpp"
std::map<std::string, UIBackend*> UIBackend::backends;
std::string UIBackend::get_id() {
return "";
}
int UIBackend::run(std::vector<std::string> realArgs, int argc, char **argv) {
#ifdef GLIB_ENABLED
g_set_application_name("Looper")
#endif
args = realArgs;
playback = new PlaybackInstance();
CLI::App app{DESCRIPTION};
std::string filename = "";
app.allow_extras();
double new_speed = 1.0;
double new_tempo = 1.0;
double new_pitch = 1.0;
auto speed_opt = app.add_option("-s,--speed", new_speed, "Set the initial speed of the playback.")->default_val(1.0);
auto tempo_opt = app.add_option("-t,--tempo", new_tempo, "Set the initial tempo of the playback.")->default_val(1.0);
auto pitch_opt = app.add_option("-p,--pitch", new_pitch, "Set the initial pitch of the playback.")->default_val(1.0);
bool multi_instance = false;
auto multi_instance_opt = app.add_flag<bool>("-m,--multi-instance", multi_instance, "Disables the DBus api to allow multiple instances.");
try {
app.parse(args);
} catch (const CLI::ParseError &e) {
throw app.exit(e);
}
playback->SetSpeed(new_speed);
playback->SetTempo(new_tempo);
playback->SetPitch(new_pitch);
args = app.remaining();
if (!multi_instance) {
DBusAPISender sender;
if (!sender.isOnlyInstance()) {
if (args.size() > 0) {
sender.playFile(args[0]);
}
if (!speed_opt->empty()) {
sender.setSpeed(new_speed);
}
if (!tempo_opt->empty()) {
sender.setTempo(new_tempo);
}
if (!pitch_opt->empty()) {
sender.setPitch(new_pitch);
}
throw 0;
} else {
dbus_api = new DBusAPI(playback);
}
} else {
dbus_api = nullptr;
}
if (args.size() > 0) {
playback->Start(args[0]);
}
return 0;
}
void UIBackend::deinit_backends() {
for (auto &kv : backends) {
delete kv.second;
}
backends.clear();
}
UIBackend::~UIBackend() {
if (dbus_api != nullptr) delete dbus_api;
delete playback;
}
std::string UIBackend::get_name() {
return "Unknown frontend";
}