diff --git a/applications/lockscreen/controller.h b/applications/lockscreen/controller.h index 17c6ff94d..ec9ec6494 100644 --- a/applications/lockscreen/controller.h +++ b/applications/lockscreen/controller.h @@ -4,6 +4,7 @@ #include #include #include +#include using namespace codes::eeems::oxide1; using namespace Oxide::Sentry; @@ -26,19 +27,10 @@ class Controller : public QObject { Controller(QObject* parent) : QObject(parent), confirmPin() { clockTimer = new QTimer(root); - auto bus = QDBusConnection::systemBus(); - qDebug() << "Waiting for tarnish to start up..."; - while(!bus.interface()->registeredServiceNames().value().contains(OXIDE_SERVICE)){ - struct timespec args{ - .tv_sec = 1, - .tv_nsec = 0, - }, res; - nanosleep(&args, &res); - } - api = new General(OXIDE_SERVICE, OXIDE_SERVICE_PATH, bus, this); - + Oxide::Tarnish::connect(); + auto bus = Oxide::Tarnish::getApi()->connection(); qDebug() << "Requesting system API..."; - QDBusObjectPath path = api->requestAPI("system"); + QDBusObjectPath path = Oxide::Tarnish::requestAPI("system"); if(path.path() == "/"){ qDebug() << "Unable to get system API"; throw ""; @@ -50,7 +42,7 @@ class Controller : public QObject { connect(systemApi, &System::deviceSuspending, this, &Controller::deviceSuspending); qDebug() << "Requesting power API..."; - path = api->requestAPI("power"); + path = Oxide::Tarnish::requestAPI("power"); if(path.path() == "/"){ qDebug() << "Unable to get power API"; throw ""; @@ -66,7 +58,7 @@ class Controller : public QObject { connect(powerApi, &Power::chargerWarning, this, &Controller::chargerWarning); qDebug() << "Requesting wifi API..."; - path = api->requestAPI("wifi"); + path = Oxide::Tarnish::requestAPI("wifi"); if(path.path() == "/"){ qDebug() << "Unable to get wifi API"; throw ""; @@ -79,7 +71,7 @@ class Controller : public QObject { connect(wifiApi, &Wifi::rssiChanged, this, &Controller::wifiRssiChanged); qDebug() << "Requesting apps API..."; - path = api->requestAPI("apps"); + path = Oxide::Tarnish::requestAPI("apps"); if(path.path() == "/"){ qDebug() << "Unable to get apps API"; throw ""; @@ -488,7 +480,6 @@ private slots: private: QString confirmPin; - General* api; System* systemApi; codes::eeems::oxide1::Power* powerApi; Wifi* wifiApi; @@ -501,7 +492,7 @@ private slots: QObject* stateControllerUI = nullptr; QObject* pinEntryUI = nullptr; - int tarnishPid() { return api->tarnishPid(); } + int tarnishPid() { return Oxide::Tarnish::tarnishPid(); } QObject* getBatteryUI() { batteryUI = root->findChild("batteryLevel"); return batteryUI; diff --git a/applications/lockscreen/main.cpp b/applications/lockscreen/main.cpp index d187c2aad..2cb243885 100644 --- a/applications/lockscreen/main.cpp +++ b/applications/lockscreen/main.cpp @@ -6,6 +6,7 @@ #include #include #include +#include #include "controller.h" @@ -15,6 +16,7 @@ using namespace Oxide::Sentry; int main(int argc, char *argv[]){ deviceSettings.setupQtEnvironment(); + Tarnish::connect(); QGuiApplication app(argc, argv); sentry_init("decay", argv); auto filter = new EventFilter(&app); diff --git a/applications/screenshot-tool/main.cpp b/applications/screenshot-tool/main.cpp index afc9b9fdb..676097a5d 100644 --- a/applications/screenshot-tool/main.cpp +++ b/applications/screenshot-tool/main.cpp @@ -5,6 +5,7 @@ #include #include #include +#include using namespace codes::eeems::oxide1; using namespace Oxide::Sentry; @@ -47,38 +48,30 @@ int main(int argc, char *argv[]){ app.setOrganizationDomain(OXIDE_SERVICE); app.setApplicationName("fret"); app.setApplicationVersion(APP_VERSION); - auto bus = QDBusConnection::systemBus(); - qDebug() << "Waiting for tarnish to start up..."; - while(!bus.interface()->registeredServiceNames().value().contains(OXIDE_SERVICE)){ - struct timespec args{ - .tv_sec = 1, - .tv_nsec = 0, - }, res; - nanosleep(&args, &res); - } - General api(OXIDE_SERVICE, OXIDE_SERVICE_PATH, bus, &app); + Oxide::Tarnish::connect(); + auto bus = Oxide::Tarnish::getApi()->connection(); qDebug() << "Requesting system API..."; - QDBusObjectPath path = api.requestAPI("system"); + QDBusObjectPath path = Oxide::Tarnish::requestAPI("system"); if(path.path() == "/"){ qDebug() << "Unable to get system API"; return EXIT_FAILURE; } System system(OXIDE_SERVICE, path.path(), bus, &app); qDebug() << "Requesting screen API..."; - path = api.requestAPI("screen"); + path = Oxide::Tarnish::requestAPI("screen"); if(path.path() == "/"){ qDebug() << "Unable to get screen API"; return EXIT_FAILURE; } Screen screen(OXIDE_SERVICE, path.path(), bus, &app); qDebug() << "Requesting notification API..."; - path = api.requestAPI("notification"); + path = Oxide::Tarnish::requestAPI("notification"); if(path.path() == "/"){ qDebug() << "Unable to get notification API"; return EXIT_FAILURE; } Notifications notifications(OXIDE_SERVICE, path.path(), bus, &app); - qDebug() << "Connecting signal listener..."; + qDebug() << "Connecting signal listener..."; QObject::connect(&system, &System::rightAction, [&screen, ¬ifications, bus, &app]{ qDebug() << "Taking screenshot"; auto reply = screen.screenshot(); diff --git a/applications/system-service/dbusservice.h b/applications/system-service/dbusservice.h index 4ae7a5254..6073e13bb 100644 --- a/applications/system-service/dbusservice.h +++ b/applications/system-service/dbusservice.h @@ -214,6 +214,25 @@ class DBusService : public APIBase { int tarnishPid(){ return qApp->applicationPid(); } + Q_INVOKABLE void registerChild(qint64 childPid, QString name, QDBusUnixFileDescriptor stdout, QDBusUnixFileDescriptor stderr){ + Q_UNUSED(childPid) + if(!QDBusUnixFileDescriptor::isSupported()){ + qCritical("QDBusUnixFileDescriptor is not supported"); + ::kill(childPid, SIGTERM); + } + if(!stdout.isValid()){ + O_WARNING("stdout passed in by" << childPid << "is invalid"); + ::kill(childPid, SIGTERM); + } + if(!stderr.isValid()){ + O_WARNING("stderr passed in by" << childPid << "is invalid"); + ::kill(childPid, SIGTERM); + } + //stdout.fileDescriptor(); + //stderr.fileDescriptor(); + qDebug() << "registerChild" << childPid << name; + } + public slots: QDBusObjectPath requestAPI(QString name, QDBusMessage message) { #ifdef SENTRY diff --git a/applications/task-switcher/controller.h b/applications/task-switcher/controller.h index 41c995a4f..4531c98af 100644 --- a/applications/task-switcher/controller.h +++ b/applications/task-switcher/controller.h @@ -10,6 +10,7 @@ #include #include #include +#include #include "screenprovider.h" #include "appitem.h" @@ -33,23 +34,14 @@ class Controller : public QObject { : QObject(parent),applications() { blankImage = new QImage(qApp->primaryScreen()->geometry().size(), QImage::Format_Mono); this->screenProvider = screenProvider; - auto bus = QDBusConnection::systemBus(); - qDebug() << "Waiting for tarnish to start up..."; - while(!bus.interface()->registeredServiceNames().value().contains(OXIDE_SERVICE)){ - struct timespec args{ - .tv_sec = 1, - .tv_nsec = 0, - }, res; - nanosleep(&args, &res); - } - api = new General(OXIDE_SERVICE, OXIDE_SERVICE_PATH, bus, this); - + Oxide::Tarnish::connect(); + auto bus = Oxide::Tarnish::getApi()->connection(); SignalHandler::setup_unix_signal_handlers(); connect(signalHandler, &SignalHandler::sigUsr1, this, &Controller::sigUsr1); connect(signalHandler, &SignalHandler::sigUsr2, this, &Controller::sigUsr2); qDebug() << "Requesting screen API..."; - QDBusObjectPath path = api->requestAPI("screen"); + QDBusObjectPath path = Oxide::Tarnish::requestAPI("screen"); if(path.path() == "/"){ qDebug() << "Unable to get screen API"; throw ""; @@ -57,7 +49,7 @@ class Controller : public QObject { screenApi = new Screen(OXIDE_SERVICE, path.path(), bus, this); qDebug() << "Requesting apps API..."; - path = api->requestAPI("apps"); + path = Oxide::Tarnish::requestAPI("apps"); if(path.path() == "/"){ qDebug() << "Unable to get apps API"; throw ""; @@ -268,7 +260,6 @@ private slots: } private: - General* api; Screen* screenApi; Apps* appsApi; QObject* root = nullptr; @@ -277,7 +268,7 @@ private slots: QList applications; QImage* blankImage; - int tarnishPid() { return api->tarnishPid(); } + int tarnishPid() { return Oxide::Tarnish::tarnishPid(); } QObject* getStateControllerUI(){ stateControllerUI = root->findChild("stateController"); return stateControllerUI; diff --git a/interfaces/dbusservice.xml b/interfaces/dbusservice.xml index e50ba6570..609fa7588 100644 --- a/interfaces/dbusservice.xml +++ b/interfaces/dbusservice.xml @@ -24,5 +24,11 @@ + + + + + + diff --git a/shared/liboxide/liboxide.pro b/shared/liboxide/liboxide.pro index f5c9f6413..fae119c17 100644 --- a/shared/liboxide/liboxide.pro +++ b/shared/liboxide/liboxide.pro @@ -23,7 +23,8 @@ SOURCES += \ settingsfile.cpp \ slothandler.cpp \ sysobject.cpp \ - signalhandler.cpp + signalhandler.cpp \ + tarnish.cpp HEADERS += \ ../epaper/epframebuffer.h \ @@ -41,7 +42,8 @@ HEADERS += \ settingsfile.h \ slothandler.h \ sysobject.h \ - signalhandler.h + signalhandler.h \ + tarnish.h PRECOMPILED_HEADER = \ liboxide_stable.h diff --git a/shared/liboxide/tarnish.cpp b/shared/liboxide/tarnish.cpp new file mode 100644 index 000000000..4a4f95b14 --- /dev/null +++ b/shared/liboxide/tarnish.cpp @@ -0,0 +1,48 @@ +#include "tarnish.h" +#include "meta.h" +#include "debug.h" + +#include + +General* api = nullptr; + +bool verifyConnection(){ + if(api == nullptr){ + return false; + } + if(!api->isValid()){ + delete api; + api = nullptr; + return false; + } + return true; +} + +namespace Oxide::Tarnish { + General* getApi(){ return api; } + QDBusObjectPath requestAPI(std::string name){ + connect(); + return api->requestAPI(QString::fromStdString(name)); + } + void connect(){ connect(qApp->applicationName().toStdString()); } + void connect(std::string name){ + if(verifyConnection()){ + return; + } + auto bus = QDBusConnection::systemBus(); + O_DEBUG("Waiting for tarnish to start up..."); + while(!bus.interface()->registeredServiceNames().value().contains(OXIDE_SERVICE)){ + timespec args{ + .tv_sec = 1, + .tv_nsec = 0, + }, res; + nanosleep(&args, &res); + } + api = new General(OXIDE_SERVICE, OXIDE_SERVICE_PATH, bus, qApp); + api->registerChild(getpid(), QString::fromStdString(name), QDBusUnixFileDescriptor(fileno(stdin)), QDBusUnixFileDescriptor(fileno(stdout))); + } + int tarnishPid(){ + connect(); + return api->tarnishPid(); + } +} diff --git a/shared/liboxide/tarnish.h b/shared/liboxide/tarnish.h new file mode 100644 index 000000000..a9d8ed75c --- /dev/null +++ b/shared/liboxide/tarnish.h @@ -0,0 +1,34 @@ +/*! + * \addtogroup Oxide::Tarnish + * \brief The Tarnish module + * @{ + * \file + */ +#pragma once +#include "liboxide_global.h" +#include "dbus.h" + +using namespace codes::eeems::oxide1; + +namespace Oxide::Tarnish { + /*! + * \brief Get the current General API instance + * \return The current General API instance + */ + General* getApi(); + QDBusObjectPath requestAPI(std::string name); + /*! + * \brief Connect to the current tarnish instance + */ + void connect(); + /*! + * \brief Connect to the current tarnish instance + * \param Name of current application + */ + void connect(std::string name); + /*! + * \brief Get the tarnish PID + * \return The tarnish PID + */ + int tarnishPid(); +}