#include "dbus.hpp" #include "log.hpp" const char *DBusAPI::serviceName = "com.experimentalcraft.looper"; const char *DBusAPI::objectPath = "/com/experimentalcraft/looper"; const char *DBusAPI::interfaceName = "com.experimentalcraft.Looper"; DBusAPI::DBusAPI(Playback *playback) { this->playback = playback; connection = sdbus::createSessionBusConnection(serviceName); api = sdbus::createObject(*connection, objectPath); api->registerMethod(interfaceName, "open", "s", "", [this](sdbus::MethodCall call) { std::string filePath; call >> filePath; this->playback->Start(filePath); }); api->registerMethod(interfaceName, "setSpeed", "d", "", [this](sdbus::MethodCall call) { double newValue; call >> newValue; this->playback->speed = newValue; this->playback->Update(); }); api->registerMethod(interfaceName, "setTempo", "d", "", [this](sdbus::MethodCall call) { double newValue; call >> newValue; this->playback->tempo = newValue; this->playback->Update(); }); api->registerMethod(interfaceName, "setPitch", "d", "", [this](sdbus::MethodCall call) { double newValue; call >> newValue; this->playback->pitch = newValue; this->playback->Update(); }); api->registerSignal(interfaceName, "fileStarted", "s"); api->finishRegistration(); connection->enterEventLoopAsync(); } bool DBusAPISender::isOnlyInstance() { return !connected; } void DBusAPISender::playFile(std::string file) { if (connected) { auto method = proxy->createMethodCall(DBusAPI::interfaceName, "open"); method.dontExpectReply(); method << file; proxy->callMethod(method); } } void DBusAPISender::setPitch(double value) { if (connected) { auto method = proxy->createMethodCall(DBusAPI::interfaceName, "setPitch"); method.dontExpectReply(); method << value; proxy->callMethod(method); } } void DBusAPISender::setSpeed(double value) { if (connected) { auto method = proxy->createMethodCall(DBusAPI::interfaceName, "setSpeed"); method.dontExpectReply(); method << value; proxy->callMethod(method); } } void DBusAPISender::setTempo(double value) { if (connected) { auto method = proxy->createMethodCall(DBusAPI::interfaceName, "setTempo"); method.dontExpectReply(); method << value; proxy->callMethod(method); } } DBusAPISender::DBusAPISender() { DEBUG.writeln("Checking and connecting to main instance..."); try { connection = sdbus::createSessionBusConnection(); proxy = sdbus::createProxy(*connection, DBusAPI::serviceName, DBusAPI::objectPath); proxy->finishRegistration(); { auto method = proxy->createMethodCall("org.freedesktop.DBus.Peer", "Ping"); proxy->callMethod(method); } DEBUG.writeln("Main instance found."); } catch (sdbus::Error) { connected = false; DEBUG.writeln("Main instance not found."); } }