From f84ddaef6cfc44ad640e2692595e020c6a1331cc Mon Sep 17 00:00:00 2001 From: Hane Date: Wed, 9 Aug 2023 15:55:25 +0200 Subject: [PATCH] failed attempt at redrawing --- src/back/backlasses.cpp | 58 ++++++++++++++++++++++++++++++++++++++++ src/back/backlasses.h | 20 ++++++++++++++ src/cont/contclasses.cpp | 43 ++++++++++++++++++++++++----- src/cont/contclasses.h | 21 ++++++++------- src/global.h | 1 - src/qt/qtclasses.cpp | 2 ++ src/qt/qtclasses.h | 12 ++++++--- src/qtestmain.cpp | 10 ++++--- 8 files changed, 142 insertions(+), 25 deletions(-) diff --git a/src/back/backlasses.cpp b/src/back/backlasses.cpp index acd4f3a..9232afc 100644 --- a/src/back/backlasses.cpp +++ b/src/back/backlasses.cpp @@ -1,5 +1,50 @@ #include +ULONG EndpointCallback::AddRef(){ + return InterlockedIncrement(&ref); +} + +ULONG EndpointCallback::Release(){ + ULONG tempRef = InterlockedDecrement(&ref); + if (tempRef == 0) { + delete this; + } + return tempRef; +} + +HRESULT EndpointCallback::QueryInterface(REFIID riid, VOID **ppvInterface) { + if (IID_IUnknown == riid) + { + AddRef(); + *ppvInterface = (IUnknown*)this; + } + else if (__uuidof(IAudioEndpointVolumeCallback) == riid) + { + AddRef(); + *ppvInterface = (IAudioEndpointVolumeCallback*)this; + } + else + { + *ppvInterface = NULL; + return E_NOINTERFACE; + } + return S_OK; +} + +HRESULT EndpointCallback::OnNotify(PAUDIO_VOLUME_NOTIFICATION_DATA pNotify) { + if (pNotify == NULL) return E_INVALIDARG; + + LPGUID guid = osh->getOverseer()->getGuid(); + if (pNotify->guidEventContext != *guid) { + osh->parseExternalEndpointCallback(this, pNotify); + } + return S_OK; +} + +/* EndpointCallback::~EndpointCallback(){ + * PAUDIO_VOLUME_NOTIFICATION_DATA->Release(); + * } */ + Endpoint::Endpoint(IMMDevice* ep){ this->endpoint = ep; if(FAILED(endpoint->Activate(IID_IAudioEndpointVolume, CLSCTX_ALL, NULL, (void**)&endpointVolume))) { log_debugcpp("si"); }; @@ -66,6 +111,13 @@ void Endpoint::setMute() { if(FAILED(endpointVolume->SetMute((mut == false ? 1 : 0), NULL))) { log_debugcpp("si"); }; } +void Endpoint::setCallback(EndpointCallback *epc){ + endpointVolume->RegisterControlChangeNotify((IAudioEndpointVolumeCallback*)*epc); +} + +void Endpoint::removeCallback(EndpointCallback *epc){ + endpointVolume->UnregisterControlChangeNotify((IAudioEndpointVolumeCallback*)*epc); +} Endpoint::~Endpoint(){ log_debugcpp("cum"); @@ -87,6 +139,8 @@ void Overseer::initCOMLibrary(){ (void**)&deviceEnumerator)) ) { log_debugcpp("si"); }; + if(FAILED(CoCreateGuid(guid))) { log_debugcpp("guyyyyyy"); }; + } void Overseer::reloadEndpoints() { @@ -129,6 +183,10 @@ Overseer::Overseer(){ //int Overseer::getDefaultCaptureEndpoint(Endpoint** defaultEndpoint); +LPGUID Overseer::getGuid() { + return guid; +} + std::vector Overseer::getPlaybackEndpoints() { return playbackDevices; } diff --git a/src/back/backlasses.h b/src/back/backlasses.h index ebfe0e5..7c28408 100644 --- a/src/back/backlasses.h +++ b/src/back/backlasses.h @@ -19,6 +19,22 @@ //#include #include +class EndpointCallback : public IAudioEndpointVolumeCallback { + + public: + EndpointCallback(); + + ULONG AddRef(); + ULONG Release(); + HRESULT QueryInterface(REFIID riid, VOID **ppvInterface); + HRESULT OnNotify(PAUDIO_VOLUME_NOTIFICATION_DATA update); + ~EndpointCallback(); + + private: + ULONG ref; + PAUDIO_VOLUME_NOTIFICATION_DATA update; +}; + class Endpoint { public: @@ -30,6 +46,8 @@ class Endpoint { void setMute(); bool getMute(); LPWSTR getName(); + void setCallback(EndpointCallback *epc); + void removeCallback(EndpointCallback *epc); ~Endpoint(); private: @@ -46,6 +64,7 @@ class Overseer { Overseer(); std::vector getPlaybackEndpoints(); void reloadEndpoints(); + LPGUID getGuid(); //~Overseer(); //int getDefaultPlaybackEndpoint(Endpoint** defaultEndpoint); //int getDefaultCaptureEndpoint(Endpoint** defaultEndpoint); @@ -54,6 +73,7 @@ class Overseer { ~Overseer(); private: + LPGUID guid; unsigned int numPlaybackEndpoints; IMMDeviceEnumerator *deviceEnumerator; std::vector playbackDevices; diff --git a/src/cont/contclasses.cpp b/src/cont/contclasses.cpp index bd05f88..e93de35 100644 --- a/src/cont/contclasses.cpp +++ b/src/cont/contclasses.cpp @@ -2,10 +2,13 @@ Overseer OverseerHandler::os; -EndpointHandler::EndpointHandler(Endpoint *ept, QObject *parent) : QObject(parent) { - this->ept = ept; - eptName = QString::fromStdWString(ept->getName()); +EndpointHandler::EndpointHandler(Endpoint *ep, EndpointCallback *epc, QObject *parent) : QObject(parent) { + this->ep = ep; + this->epc = epc; + epName = QString::fromStdWString(ept->getName()); + ep->setCallback(*epc); } + /* * -1 for master volume */ @@ -33,6 +36,13 @@ bool EndpointHandler::getMute(){ return ept->getMute(); } +EndpointHandler::~EndpointHandler() { + ep->removeCallback(*epc); + delete epc; + delete ep; +} + + Overseer* OverseerHandler::getOverseer(){ return &os; } @@ -41,10 +51,29 @@ OverseerHandler::OverseerHandler(QObject *parent) : QObject(parent) { } -std::vector* OverseerHandler::getEndpointHandlers(){ - return endpointHandlers; +std::vector* OverseerHandler::getEndpointWidgets(){ + return &endpointWidgets; } -void OverseerHandler::setEndpointHandlers(std::vector *ephs){ - this->endpointHandlers = ephs; +void OverseerHandler::parseExternalEndpointCallback(EndpointCallback *fEpc, PAUDIO_VOLUME_NOTIFICATION_DATA pNotify){ + log_debugcpp("parsing in da ovasiar"); + for (uint64_t i = 0; i < endpointWidgets.size(); i++){ + if(endpointWidgets.at(i)->eph->epc == fEpc) { + endpointWidgets.at(i)->muteButton->setText(endpointWidgets.at(i)->eph->getMute() ? STRING_UNMUTE : STRING_MUTE); + break; + } + } + + /* + * connect(mainSlider, &QSlider::valueChanged, [this](int newValue){this->eph->setValue(ENDPOINT_MASTER_VOLUME, newValue); }); + * connect(leftChannelSlider, &QSlider::valueChanged, [this](int newValue){ this->eph->setValue(ENDPOINT_LEFT_CHANNEL_VOLUME, newValue); this->leftChannelLabel->setText(QString::number(newValue)); }); + * connect(rightChannelSlider, &QSlider::valueChanged, [this](int newValue){ this->eph->setValue(ENDPOINT_RIGHT_CHANNEL_VOLUME, newValue); this->rightChannelLabel->setText(QString::number(newValue)); }); + * connect(muteButton, &QPushButton::clicked, [this](bool clicked){ log_debugcpp("cliqui" << clicked << "cloqui"); this->eph->setMute(); this->muteButton->setText(this->eph->getMute() ? STRING_UNMUTE : STRING_MUTE); }); + * log_debugcpp("ENDPOINT_WIDGETED"); + */ } + +void OverseerHandler::setEndpointWidgets(std::vector ews){ + this->endpointWidgets = ews; +} + diff --git a/src/cont/contclasses.h b/src/cont/contclasses.h index c562b73..f11f312 100644 --- a/src/cont/contclasses.h +++ b/src/cont/contclasses.h @@ -1,4 +1,5 @@ #pragma once + #include #include "backlasses.h" @@ -7,15 +8,18 @@ class EndpointHandler : public QObject { Q_OBJECT public: - EndpointHandler(Endpoint *ept, QObject *parent = nullptr); + EndpointHandler(Endpoint *ept, EndpointCallback *epc, QObject *parent = nullptr); + //TODO: get(); + Endpoint *ep; + EndpointCallback *epc; + QString epName; + QString getName(); float getVolume(int channel); bool getMute(); private: - Endpoint *ept; - QString eptName; - //QSlider *slidy; + //QSlider *slidy; public slots: void setValue(int channel, int value); @@ -31,18 +35,17 @@ class OverseerHandler : public QObject { public: OverseerHandler(QObject *parent = nullptr); - void setEndpointHandlers(std::vector *ephs); - std::vector* getEndpointHandlers(); + void setEndpointWidgets(std::vector ews); + std::vector* getEndpointWidgets(); + void parseExternalEndpointCallback(EndpointCallback *epc, PAUDIO_VOLUME_NOTIFICATION_DATA pNotify); static Overseer* getOverseer(); private: static Overseer os; - std::vector *endpointHandlers; + std::vector endpointWidgets; //QSlider *slidy; //public slots: //void setValue(int value); - - }; diff --git a/src/global.h b/src/global.h index 96d177b..977e0b8 100644 --- a/src/global.h +++ b/src/global.h @@ -9,7 +9,6 @@ #define STRING_UNMUTE "Unmute" //INIT BACK - class OverseerHandler; extern OverseerHandler *osh; diff --git a/src/qt/qtclasses.cpp b/src/qt/qtclasses.cpp index 0cac13d..70334c3 100644 --- a/src/qt/qtclasses.cpp +++ b/src/qt/qtclasses.cpp @@ -85,6 +85,7 @@ MainWindow::MainWindow(std::vector *ephs, QWidget *parent) : Q ews.push_back(epw); layout->addWidget(epw, i, 0); } + osh->setEndpointWidgets(ews); layout->addItem(new QSpacerItem(1, 1, QSizePolicy::Minimum, QSizePolicy::MinimumExpanding), i, 0); } @@ -112,3 +113,4 @@ MainWindow::MainWindow(std::vector *ephs, QWidget *parent) : Q * ... */ + diff --git a/src/qt/qtclasses.h b/src/qt/qtclasses.h index a02bff8..e7e9d91 100644 --- a/src/qt/qtclasses.h +++ b/src/qt/qtclasses.h @@ -19,11 +19,9 @@ class EndpointWidget : public QWidget { public: EndpointWidget(EndpointHandler* eph, QWidget *parent = nullptr); - //void populateEndpointWidget(EndpointHandler *eph); - //void setEndpointHandlers(std::vector *ephs); - -private: + //TODO: get(); EndpointHandler* eph; + QPushButton *muteButton = nullptr; QLabel *mainLabel = nullptr, *leftChannelLabel = nullptr, *rightChannelLabel = nullptr; QSlider *mainSlider = nullptr; @@ -31,6 +29,11 @@ private: QSlider *rightChannelSlider = nullptr; QGridLayout *layout = nullptr; QGridLayout *mainMuteLayout = nullptr; + //void populateEndpointWidget(EndpointHandler *eph); + //void setEndpointHandlers(std::vector *ephs); + +private: + //std::vector *ephs; //std::vector *sliders; @@ -67,3 +70,4 @@ private: }; #endif + diff --git a/src/qtestmain.cpp b/src/qtestmain.cpp index 883d7e7..d619db1 100644 --- a/src/qtestmain.cpp +++ b/src/qtestmain.cpp @@ -9,6 +9,7 @@ #include #include "qtclasses.h" +#include "global.h" OverseerHandler *osh = new OverseerHandler(); @@ -21,14 +22,14 @@ QApplication* createApplication(int &argc, char *argv[]) int main (int argc, char* argv[]) { //QApplication::setStyle("windowsvista"); //INIT CONT - std::vector epts = OverseerHandler::getOverseer()->getPlaybackEndpoints(); + std::vector eps = OverseerHandler::getOverseer()->getPlaybackEndpoints(); std::vector* ephs = new std::vector; - for(unsigned int i = 0; i < epts.size(); i++){ - EndpointHandler *eph = new EndpointHandler(epts.at(i)); + for(unsigned int i = 0; i < eps.size(); i++){ + EndpointCallback* epc = new EndpointCallback(); + EndpointHandler* eph = new EndpointHandler(eps.at(i), epc); ephs->push_back(eph); } - osh->setEndpointHandlers(ephs); //INIT FRONT QScopedPointer app(createApplication(argc, argv)); MainWindow window = MainWindow(ephs); @@ -37,3 +38,4 @@ int main (int argc, char* argv[]) { window.show(); return app->exec(); } +