Refactored program structure

This commit is contained in:
Hane 2023-08-11 16:39:10 +02:00
commit 80685f1a21
10 changed files with 140 additions and 96 deletions

View file

@ -1,6 +1,7 @@
QMAKE_CXXFLAGS += --target=x86_64-w64-mingw32 QMAKE_CXXFLAGS += --target=x86_64-w64-mingw32
QMAKE_LINKER += clang++ QMAKE_LINKER += clang++
QMAKE_LFLAGS += -v QMAKE_LFLAGS += -v
DEFINES += DEBUG
CONFIG += debug console CONFIG += debug console
QT += widgets QT += widgets
INCLUDEPATH += "$$PWD\src" "$$PWD\src\qt" "$$PWD\src\back" "$$PWD\src\cont" INCLUDEPATH += "$$PWD\src" "$$PWD\src\qt" "$$PWD\src\back" "$$PWD\src\cont"

View file

@ -1,7 +1,7 @@
#include <backlasses.h> #include <backlasses.h>
EndpointCallback::EndpointCallback(Endpoint* ep){ EndpointCallback::EndpointCallback(Endpoint* ep){
this.ep = ep; this->ep = ep;
} }
ULONG EndpointCallback::AddRef(){ ULONG EndpointCallback::AddRef(){
@ -36,13 +36,15 @@ HRESULT EndpointCallback::QueryInterface(REFIID riid, VOID **ppvInterface) {
} }
HRESULT EndpointCallback::OnNotify(PAUDIO_VOLUME_NOTIFICATION_DATA pNotify) { HRESULT EndpointCallback::OnNotify(PAUDIO_VOLUME_NOTIFICATION_DATA pNotify) {
if (pNotify == NULL) return E_INVALIDARG; if (pNotify == NULL) return E_INVALIDARG;
AUDIO_VOLUME_NOTIFICATION_DATA eventData = *pNotify; /*
LPGUID guid = osh->getOverseer()->getGuid(); * AUDIO_VOLUME_NOTIFICATION_DATA eventData = *pNotify;
* LPGUID guid = osh->getOverseer()->getGuid();
if (eventData.guidEventContext != *guid) { *
osh->parseExternalEndpointCallback(this, eventData); * if (eventData.guidEventContext != *guid) {
} * osh->parseExternalEndpointCallback(this, eventData);
* }
*/
return S_OK; return S_OK;
} }
@ -58,11 +60,11 @@ Endpoint::Endpoint(IMMDevice* ep){
endpoint->OpenPropertyStore(STGM_READ, &properties); endpoint->OpenPropertyStore(STGM_READ, &properties);
PROPVARIANT pv; PROPVARIANT pv;
properties->GetValue(PKEY_Device_FriendlyName , &pv); properties->GetValue(PKEY_Device_FriendlyName , &pv);
friendlyName = pv.pwszVal; friendlyName = std::wstring(pv.pwszVal);
} }
void Endpoint::setIndex(uint64_t idx){ void Endpoint::setIndex(uint64_t idx){
this.idx = idx; this->idx = idx;
} }
uint64_t Endpoint::getIndex(){ uint64_t Endpoint::getIndex(){
@ -70,7 +72,7 @@ uint64_t Endpoint::getIndex(){
} }
LPWSTR Endpoint::getName(){ std::wstring Endpoint::getName(){
return friendlyName; return friendlyName;
} }
@ -118,10 +120,10 @@ void Endpoint::setVolume(int channel, float volume) {
} }
void Endpoint::setMute() { void Endpoint::setMute() {
log_debugcpp("bool mute arrives as " << mut);
BOOL mut; BOOL mut;
//log_debugcpp("bool mute arrives as " << mut);
if(FAILED(endpointVolume->GetMute(&mut))) { log_debugcpp("si"); } if(FAILED(endpointVolume->GetMute(&mut))) { log_debugcpp("si"); }
log_debugcpp("translate to BOOL as " << mute); log_debugcpp("translate to BOOL as " << mut);
if(FAILED(endpointVolume->SetMute((mut == false ? 1 : 0), NULL))) { log_debugcpp("si"); }; if(FAILED(endpointVolume->SetMute((mut == false ? 1 : 0), NULL))) { log_debugcpp("si"); };
} }
@ -135,7 +137,6 @@ void Endpoint::removeCallback(EndpointCallback *epc){
Endpoint::~Endpoint(){ Endpoint::~Endpoint(){
log_debugcpp("cum"); log_debugcpp("cum");
free(friendlyName);
properties->Release(); properties->Release();
endpointVolume->Release(); endpointVolume->Release();
endpoint->Release(); endpoint->Release();
@ -153,8 +154,18 @@ void Overseer::initCOMLibrary(){
(void**)&deviceEnumerator)) ) (void**)&deviceEnumerator)) )
{ log_debugcpp("si"); }; { log_debugcpp("si"); };
if(FAILED(CoCreateGuid(guid))) { log_debugcpp("guyyyyyy"); }; /*
* LPGUID tempGuid = nullptr;
* if(FAILED(CoCreateGuid(tempGuid))) { log_debugcpp("Failed to obtain GUID"); };
*
* this->guid.data1 = tempGuid->Data1;
* this->guid.data2 = tempGuid->Data2;
* this->guid.data3 = tempGuid->Data3;
* for (int i = 0; i < 8; i++){
* this->guid.data4[i] = tempGuid->Data4[i];
* }
*/
//TODO: Release lpguid?
} }
void Overseer::reloadEndpoints() { void Overseer::reloadEndpoints() {
@ -184,6 +195,7 @@ void Overseer::reloadEndpoints() {
Overseer::Overseer(){ Overseer::Overseer(){
//Initializing COM library //Initializing COM library
log_debugcpp("Initializing Overseer");
initCOMLibrary(); initCOMLibrary();
//Obtaining playback endpoint collection on this point in time //Obtaining playback endpoint collection on this point in time
@ -198,7 +210,8 @@ Overseer::Overseer(){
//int Overseer::getDefaultCaptureEndpoint(Endpoint** defaultEndpoint); //int Overseer::getDefaultCaptureEndpoint(Endpoint** defaultEndpoint);
LPGUID Overseer::getGuid() { //TODO guid
NGuid Overseer::getGuid() {
return guid; return guid;
} }

View file

@ -1,11 +1,13 @@
#pragma once #pragma once
#define WIN32_LEAN_AND_MEAN #define WIN32_LEAN_AND_MEAN
//done by qt by def #define UNICODE
#include "debug.h" //#include "debug.h"
#include "global.h" #include "global.h"
#include <vector> #include "contclasses.h"
#include <iostream> /* #include <vector> */
#include <wstring> /* #include <iostream> */
/* #include <wstring> */
#include <Windows.h> #include <Windows.h>
#include <mmdeviceapi.h> #include <mmdeviceapi.h>
@ -20,24 +22,9 @@
//#include <comip.h> //#include <comip.h>
#include <Winerror.h> #include <Winerror.h>
//class EndpointWidget; #include "contclasses.h"
class EndpointCallback : public IAudioEndpointVolumeCallback { class EndpointCallback;
public:
EndpointCallback(Endpoint* ep);
ULONG AddRef();
ULONG Release();
HRESULT QueryInterface(REFIID riid, VOID **ppvInterface);
HRESULT OnNotify(PAUDIO_VOLUME_NOTIFICATION_DATA update);
~EndpointCallback();
private:
ULONG ref = 1;
Endpoint* ep;
PAUDIO_VOLUME_NOTIFICATION_DATA update;
};
class Endpoint { class Endpoint {
@ -51,7 +38,7 @@ class Endpoint {
float getVolume(int channel); float getVolume(int channel);
void setMute(); void setMute();
bool getMute(); bool getMute();
LPWSTR getName(); std::wstring getName();
void setCallback(EndpointCallback *epc); void setCallback(EndpointCallback *epc);
void removeCallback(EndpointCallback *epc); void removeCallback(EndpointCallback *epc);
~Endpoint(); ~Endpoint();
@ -65,13 +52,30 @@ class Endpoint {
// LPWSTR endpointID = NULL; // LPWSTR endpointID = NULL;
}; };
class EndpointCallback : public IAudioEndpointVolumeCallback {
public:
EndpointCallback(Endpoint* ep);
ULONG AddRef();
ULONG Release();
HRESULT QueryInterface(REFIID riid, VOID **ppvInterface);
HRESULT OnNotify(PAUDIO_VOLUME_NOTIFICATION_DATA update);
//~EndpointCallback();
private:
ULONG ref = 1;
Endpoint* ep;
//PAUDIO_VOLUME_NOTIFICATION_DATA update;
};
class Overseer { class Overseer {
//TODO singleton? //TODO singleton?
public: public:
Overseer(); Overseer();
std::vector<Endpoint*> getPlaybackEndpoints(); std::vector<Endpoint*> getPlaybackEndpoints();
void reloadEndpoints(); void reloadEndpoints();
LPGUID getGuid(); NGuid getGuid();
//~Overseer(); //~Overseer();
//int getDefaultPlaybackEndpoint(Endpoint** defaultEndpoint); //int getDefaultPlaybackEndpoint(Endpoint** defaultEndpoint);
//int getDefaultCaptureEndpoint(Endpoint** defaultEndpoint); //int getDefaultCaptureEndpoint(Endpoint** defaultEndpoint);
@ -80,7 +84,7 @@ class Overseer {
~Overseer(); ~Overseer();
private: private:
LPGUID guid; NGuid guid;
unsigned int numPlaybackEndpoints; unsigned int numPlaybackEndpoints;
IMMDeviceEnumerator *deviceEnumerator; IMMDeviceEnumerator *deviceEnumerator;
std::vector<Endpoint*> playbackDevices; std::vector<Endpoint*> playbackDevices;

View file

@ -1,4 +1,4 @@
#incluse "backlasses.h" #include "backlasses.h"
#include "contclasses.h" #include "contclasses.h"
Overseer OverseerHandler::os; Overseer OverseerHandler::os;
@ -7,12 +7,12 @@ EndpointHandler::EndpointHandler(uint64_t idx) {
std::vector<Endpoint*> endpoints = osh->getPlaybackEndpoints(); std::vector<Endpoint*> endpoints = osh->getPlaybackEndpoints();
this->ep = endpoints.at(idx); this->ep = endpoints.at(idx);
epc = new EndpointCallback(ep); epc = new EndpointCallback(ep);
epName = QString::fromStdWString(ept->getName()); //epName = ep->getName();
ep->setCallback(*epc); //ep->setCallback(*epc);
} }
void EndpointHandler::setIndex(uint64_t idx){ void EndpointHandler::setIndex(uint64_t idx){
this.idx = idx; this->idx = idx;
} }
uint64_t EndpointHandler::getIndex(){ uint64_t EndpointHandler::getIndex(){
@ -25,36 +25,36 @@ uint64_t EndpointHandler::getIndex(){
*/ */
void EndpointHandler::setValue(int channel, int value){ void EndpointHandler::setValue(int channel, int value){
if (channel == ENDPOINT_MASTER_VOLUME) if (channel == ENDPOINT_MASTER_VOLUME)
ept->setVolume(channel, (float)value / 100); ep->setVolume(channel, (float)value / 100);
else ept->setVolume(channel, (float)value / 100); else ep->setVolume(channel, (float)value / 100);
} }
void EndpointHandler::setMute(){ void EndpointHandler::setMute(){
//Qt momento, de ahi el param? //Qt momento, de ahi el param?
log_debugcpp("kinda handling the muting tbh"); log_debugcpp("kinda handling the muting tbh");
ept->setMute(); ep->setMute();
} }
QString EndpointHandler::getName(){ std::wstring EndpointHandler::getName(){
return eptName; return ep->getName();
} }
float EndpointHandler::getVolume(int channel){ float EndpointHandler::getVolume(int channel){
return ept->getVolume(channel); return ep->getVolume(channel);
} }
bool EndpointHandler::getMute(){ bool EndpointHandler::getMute(){
return ept->getMute(); return ep->getMute();
} }
EndpointHandler::~EndpointHandler() { EndpointHandler::~EndpointHandler() {
ep->removeCallback(*epc); ep->removeCallback(epc);
delete epc; delete epc;
delete ep; delete ep;
} }
std::vector<Endpoint*> OverseerHandler::getPlaybackEndpoints() { std::vector<Endpoint*> OverseerHandler::getPlaybackEndpoints() {
return os->getPlaybackEndpoints(); return os.getPlaybackEndpoints();
} }
/* /*
@ -64,38 +64,46 @@ std::vector<Endpoint*> OverseerHandler::getPlaybackEndpoints() {
*/ */
std::vector<EndpointHandler*>* OverseerHandler::getEndpointHandlers(){ std::vector<EndpointHandler*> OverseerHandler::getEndpointHandlers(){
return &endpointHandlers; return endpointHandlers;
} }
uint64_t OverseerHandler::getPlaybackEndpointsCount(){ uint64_t OverseerHandler::getPlaybackEndpointsCount(){
return os->getplaybackEndpoints().size(); return os.getPlaybackEndpoints().size();
} }
void OverseerHandler::reloadEndpointHandlers(){ void OverseerHandler::reloadEndpointHandlers(){
//std::vector<EndpointHandler*>* ephs = new std::vector<EndpointHandler*>; //std::vector<EndpointHandler*>* ephs = new std::vector<EndpointHandler*>;
log_debugcpp(" VSize: " << this->getPlaybackEndpointsCount());
for(uint64_t i = 0; i < this->getPlaybackEndpointsCount(); i++){
log_debugcpp("Creating handler " << i);
if(i < (this->endpointHandlers.size()) &&
this->endpointHandlers.at(i) != nullptr)
delete endpointHandlers.at(i);
for(uint64_t i = 0; i < osh->getPlaybackEndpointsCount(); i++){
if(i < osh->getPlaybackEndpointsCount().size() &&
osh->getPlaybackEndpointsCount().at(i) != nullptr)
delete ephs.at(i);
EndpointHandler* eph = new EndpointHandler(i); EndpointHandler* eph = new EndpointHandler(i);
log_debugcpp("Created handler " << i << ", adding to vector. " << " VSize: " << this->getPlaybackEndpointsCount());
if (i >= osh->getPlaybackEndpointsCount().size())
ephs.push_back(eph); if (i >= this->endpointHandlers.size())
else epsh.at(i) = eph; endpointHandlers.push_back(eph);
else endpointHandlers.at(i) = eph;
} }
//setEndpointHandlers(ephs); //setEndpointHandlers(ephs);
} }
void OverseerHandler::parseExternalEndpointCallback(EndpointCallback *fEpc, PAUDIO_VOLUME_NOTIFICATION_DATA pNotify){ /*
log_debugcpp("parsing in da ovasiar"); * void OverseerHandler::parseExternalEndpointCallback(EndpointCallback *fEpc, PAUDIO_VOLUME_NOTIFICATION_DATA pNotify){
for (uint64_t i = 0; i < endpointHandlers.size(); i++){ * log_debugcpp("parsing in da ovasiar");
if(endpointHandlers.at(i)->eph->epc == fEpc) { * for (uint64_t i = 0; i < endpointHandlers.size(); i++){
endpointHandlers.at(i)->muteButton->setText(endpointHandlers.at(i)->eph->getMute() ? STRING_UNMUTE : STRING_MUTE); * if(endpointHandlers.at(i)->eph->epc == fEpc) {
break; * endpointHandlers.at(i)->muteButton->setText(endpointHandlers.at(i)->eph->getMute() ? STRING_UNMUTE : STRING_MUTE);
} * break;
} * }
* }
*/
/* /*
* connect(mainSlider, &QSlider::valueChanged, [this](int newValue){this->eph->setValue(ENDPOINT_MASTER_VOLUME, newValue); }); * connect(mainSlider, &QSlider::valueChanged, [this](int newValue){this->eph->setValue(ENDPOINT_MASTER_VOLUME, newValue); });
@ -104,7 +112,7 @@ void OverseerHandler::parseExternalEndpointCallback(EndpointCallback *fEpc, PAUD
* 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); }); * 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"); * log_debugcpp("ENDPOINT_WIDGETED");
*/ */
}
void OverseerHandler::setEndpointHandlers(std::vector<EndpointHandler*> ews){ void OverseerHandler::setEndpointHandlers(std::vector<EndpointHandler*> ews){
this->endpointHandlers = ews; this->endpointHandlers = ews;

View file

@ -1,27 +1,36 @@
#pragma once #pragma once
#include "backlasses.h" class Endpoint;
class EndpointCallback;
class Overseer;
struct NGuid {
uint32_t data1;
uint16_t data2;
uint16_t data3;
unsigned char data4[8];
};
class EndpointHandler { class EndpointHandler {
public: public:
EndpointHandler(Endpoint *ept); EndpointHandler(uint64_t idx);
//TODO: get(); //TODO: get();
Endpoint *ep; Endpoint *ep;
EndpointCallback *epc; EndpointCallback *epc;
QString epName; //std::wstring epName;
void setIndex(uint64_t idx); void setIndex(uint64_t idx);
uint64_t getIndex();
void setVolume(int channel, float volume); void setVolume(int channel, float volume);
//todo qstrin????? idiota std::wstring getName();
QString getName();
float getVolume(int channel); float getVolume(int channel);
bool getMute(); bool getMute();
void setValue(int channel, int value); void setValue(int channel, int value);
void setMute(); void setMute();
~EndpointHandler();
private: private:
uint64_t idx; uint64_t idx;
//QSlider *slidy; //QSlider *slidy;
@ -37,18 +46,16 @@ class OverseerHandler {
public: public:
//OverseerHandler(); //OverseerHandler();
void setEndpointHandlers(std::vector<EndpointHandler*> ews); void setEndpointHandlers(std::vector<EndpointHandler*> ews);
std::vector<EndpointHandler*>* getEndpointHandlers(); std::vector<EndpointHandler*> getEndpointHandlers();
std::vector<Endpoint*> getPlaybackEndpoints(); std::vector<Endpoint*> getPlaybackEndpoints();
uint64_t getPlaybackEndpointsCount(); uint64_t getPlaybackEndpointsCount();
void parseExternalEndpointCallback(EndpointCallback *epc, PAUDIO_VOLUME_NOTIFICATION_DATA pNotify); void reloadEndpointHandlers();
//void parseExternalEndpointCallback(EndpointCallback *epc, PAUDIO_VOLUME_NOTIFICATION_DATA pNotify);
//static Overseer* getOverseer(); //static Overseer* getOverseer();
private: private:
static Overseer os; static Overseer os;
std::vector<EndpointHandler*> endpointHandlers; std::vector<EndpointHandler*> endpointHandlers;
//QSlider *slidy;
//public slots:
//void setValue(int value);
}; };

View file

@ -8,3 +8,4 @@
#else #else
#define log_debugcpp(str) #define log_debugcpp(str)
#endif #endif

View file

@ -1,5 +1,11 @@
#pragma once #pragma once
#include <vector>
#include <iostream>
#include <string>
#include "debug.h"
//TODO enum capullo //TODO enum capullo
#define ENDPOINT_MASTER_VOLUME -1 #define ENDPOINT_MASTER_VOLUME -1
#define ENDPOINT_LEFT_CHANNEL_VOLUME 0 #define ENDPOINT_LEFT_CHANNEL_VOLUME 0

View file

@ -9,7 +9,7 @@ EndpointWidget::EndpointWidget(EndpointHandler* eph, QWidget *parent) : QWidget(
if (parent == nullptr) { log_debugcpp("owo?"); } if (parent == nullptr) { log_debugcpp("owo?"); }
muteButton = new QPushButton(); muteButton = new QPushButton();
mainLabel = new QLabel(eph->getName()); mainLabel = new QLabel(QString::fromStdWString(eph->getName()));
leftChannelLabel = new QLabel("88"); leftChannelLabel = new QLabel("88");
rightChannelLabel = new QLabel("77"); rightChannelLabel = new QLabel("77");
mainSlider = new QSlider(Qt::Horizontal); mainSlider = new QSlider(Qt::Horizontal);
@ -64,7 +64,7 @@ EndpointWidget::EndpointWidget(EndpointHandler* eph, QWidget *parent) : QWidget(
void EndpointWidget::setIndex(uint64_t idx){ void EndpointWidget::setIndex(uint64_t idx){
this.idx = idx; this->idx = idx;
} }
uint64_t EndpointWidget::getIndex(){ uint64_t EndpointWidget::getIndex(){
@ -89,7 +89,7 @@ MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) {
* setEndpointHandlers(ephs); * setEndpointHandlers(ephs);
*/ */
unsigned int i = 0; unsigned int i = 0;
for (; i < ephs->size(); i++) { for (; i < (osh->getEndpointHandlers().size()); i++) {
log_debugcpp("EPWidget creation"); log_debugcpp("EPWidget creation");
EndpointWidget *epw = new EndpointWidget(osh->getEndpointHandlers().at(i), widget); EndpointWidget *epw = new EndpointWidget(osh->getEndpointHandlers().at(i), widget);
ews.push_back(epw); ews.push_back(epw);

View file

@ -2,13 +2,16 @@
#ifndef MAINWINDOW_H #ifndef MAINWINDOW_H
#define MAINWINDOW_H #define MAINWINDOW_H
#include <vector> //#include <vector>
#include <QMainWindow> #include <QMainWindow>
#include <QApplication> #include <QApplication>
#include <QLabel> #include <QLabel>
#include <QSlider> #include <QSlider>
#include <QGridLayout> #include <QGridLayout>
#include <QPushButton> #include <QPushButton>
//#include "debug.h"
#include "global.h"
#include "contclasses.h" #include "contclasses.h"
//#include <Q> //#include <Q>
//#include <QWidgets> //#include <QWidgets>
@ -22,6 +25,8 @@ public:
//TODO: get(); //TODO: get();
EndpointHandler* eph; EndpointHandler* eph;
void setIndex(uint64_t idx); void setIndex(uint64_t idx);
uint64_t getIndex();
void setVolume(int channel, float volume); void setVolume(int channel, float volume);
QPushButton *muteButton = nullptr; QPushButton *muteButton = nullptr;
@ -53,7 +58,7 @@ class MainWindow : public QMainWindow {
//QWidget *centralWidget; //QWidget *centralWidget;
public: public:
MainWindow(std::vector<EndpointHandler*> *ephs, QWidget *parent = nullptr); MainWindow(QWidget *parent = nullptr);
//void setEndpointHandlers(std::vector<EndpointHandler*> *ephs); //void setEndpointHandlers(std::vector<EndpointHandler*> *ephs);
private: private:

View file

@ -1,6 +1,3 @@
#include <iostream>
#include <vector>
//#include <stdio.h> //#include <stdio.h>
//#include <stdlib.h> //#include <stdlib.h>
@ -22,7 +19,9 @@ QApplication* createApplication(int &argc, char *argv[])
int main (int argc, char* argv[]) { int main (int argc, char* argv[]) {
//QApplication::setStyle("windowsvista"); //QApplication::setStyle("windowsvista");
//INIT CONT //INIT CONT
log_debugcpp("main init");
osh->reloadEndpointHandlers(); osh->reloadEndpointHandlers();
log_debugcpp("Reloaded endpoint handlers");
//INIT FRONT //INIT FRONT
QScopedPointer<QApplication> app(createApplication(argc, argv)); QScopedPointer<QApplication> app(createApplication(argc, argv));