looper/backend.cpp

76 lines
2.3 KiB
C++
Raw Normal View History

#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;
2024-03-26 18:39:02 -07:00
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);
2024-03-26 18:39:02 -07:00
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);
}
2024-03-26 18:39:02 -07:00
playback->SetSpeed(new_speed);
playback->SetTempo(new_tempo);
playback->SetPitch(new_pitch);
args = app.remaining();
2024-03-26 18:39:02 -07:00
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 {
2024-03-26 18:39:02 -07:00
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();
}
2024-03-26 18:39:02 -07:00
UIBackend::~UIBackend() {
if (dbus_api != nullptr) delete dbus_api;
delete playback;
}
std::string UIBackend::get_name() {
return "Unknown frontend";
}