wip: heap corruption (3rd item main volume)

This commit is contained in:
Hane 2023-09-10 21:58:01 +02:00
commit 14fae226bc
6 changed files with 107 additions and 52 deletions

View file

@ -184,11 +184,9 @@ Endpoint::Endpoint(IMMDevice* ep, uint64_t idx){
//todo: preguntitas owindows dword no es uint32_t even tho mingw mingas //todo: preguntitas owindows dword no es uint32_t even tho mingw mingas
if(FAILED(endpoint->GetState(&this->endpointState))) {exit(-1);}; if(FAILED(endpoint->GetState(&this->endpointState))) {exit(-1);};
if (this->endpointState == DEVICE_STATE_ACTIVE) { activateEndpointVolume();
if(FAILED(endpoint->Activate(IID_IAudioEndpointVolume, CLSCTX_ALL, NULL, (void**)&endpointVolume))) { /* log_debugcpp("si"); */ };
if (FAILED(endpointVolume->GetChannelCount(&channelCount))) {};/* log_debugcpp("get channel count fail"); */ reloadEndpointChannels();
}
//todo:: atexit into exit Gather ID //todo:: atexit into exit Gather ID
LPWSTR tempString = nullptr; LPWSTR tempString = nullptr;
@ -206,6 +204,17 @@ Endpoint::Endpoint(IMMDevice* ep, uint64_t idx){
friendlyName = std::wstring(pv.pwszVal); friendlyName = std::wstring(pv.pwszVal);
} }
void Endpoint::activateEndpointVolume() {
if (this->endpointVolume == nullptr)
if(FAILED(endpoint->Activate(IID_IAudioEndpointVolume, CLSCTX_ALL, NULL, (void**)&this->endpointVolume))) { log_debugcpp(std::string("no endpointVolume (IAudioEndpointVolume)")); };
}
void Endpoint::reloadEndpointChannels() {
if (this->endpointState == DEVICE_STATE_ACTIVE) {
if (FAILED(endpointVolume->GetChannelCount(&channelCount))) {};/* log_debugcpp("get channel count fail"); */
}
}
void Endpoint::setIndex(uint64_t idx){ void Endpoint::setIndex(uint64_t idx){
this->idx = idx; this->idx = idx;
} }
@ -246,6 +255,8 @@ bool Endpoint::getMute(){
void Endpoint::setState(uint8_t state){ void Endpoint::setState(uint8_t state){
this->endpointState = state; this->endpointState = state;
if(state == EndpointState::ENDPOINT_ACTIVE)
this->reloadEndpointChannels();
} }
size_t Endpoint::getState(){ size_t Endpoint::getState(){
@ -264,10 +275,13 @@ void Endpoint::setVolume(NGuid guid, int channel, float volume) {
void Endpoint::setMute(NGuid guid, bool muted) { void Endpoint::setMute(NGuid guid, bool muted) {
GUID tempMsGuid = NGuidToGUID(guid); GUID tempMsGuid = NGuidToGUID(guid);
if(FAILED(endpointVolume->SetMute(muted, &tempMsGuid))) { /* TIP: Above */ }; if(FAILED(endpointVolume->SetMute(muted, &tempMsGuid))) { log_wdebugcpp(std::wstring(L"EndpointVolume null?")); };
} }
void Endpoint::setVolumeCallback(EndpointVolumeCallback *epc){ void Endpoint::setVolumeCallback(EndpointVolumeCallback *epc){
if(endpointVolume == nullptr) {
this->activateEndpointVolume();
}
endpointVolume->RegisterControlChangeNotify((IAudioEndpointVolumeCallback*)epc); endpointVolume->RegisterControlChangeNotify((IAudioEndpointVolumeCallback*)epc);
} }

View file

@ -33,6 +33,7 @@ class Endpoint {
public: public:
Endpoint(IMMDevice* endpoint, uint64_t idx); Endpoint(IMMDevice* endpoint, uint64_t idx);
void reloadEndpointChannels();
uint64_t getIndex(); uint64_t getIndex();
void setIndex(uint64_t idx); void setIndex(uint64_t idx);
void setVolume(NGuid guid, int channel, float volume); void setVolume(NGuid guid, int channel, float volume);
@ -53,9 +54,11 @@ class Endpoint {
~Endpoint(); ~Endpoint();
private: private:
void inline activateEndpointVolume();
uint32_t channelCount = 0; uint32_t channelCount = 0;
IMMDevice* endpoint; IMMDevice* endpoint;
IAudioEndpointVolume *endpointVolume ; IAudioEndpointVolume *endpointVolume = nullptr;
IPropertyStore *properties; IPropertyStore *properties;
std::wstring friendlyName; std::wstring friendlyName;
std::wstring endpointId; std::wstring endpointId;

View file

@ -4,19 +4,13 @@
EndpointHandler::EndpointHandler(uint64_t idx) { EndpointHandler::EndpointHandler(uint64_t idx) {
//std::vector<Endpoint*> endpoints = osh->getPlaybackEndpoints().at(idx); //std::vector<Endpoint*> endpoints = osh->getPlaybackEndpoints().at(idx);
this->idx = idx;
this->ep = osh->getPlaybackEndpoints().at(idx); this->ep = osh->getPlaybackEndpoints().at(idx);
epc = new EndpointVolumeCallback(ep); epc = new EndpointVolumeCallback(ep);
this->callbackInfo.caller = osh->getGuid();
//epName = ep->getName(); //epName = ep->getName();
if (this->ep->getState() == EndpointState::ENDPOINT_ACTIVE) { this->setBackEndpointVolumeCallbackInfoContent(this->getState());
callbackInfo.muted = this->getMute();
callbackInfo.mainVolume = this->getVolume(AudioChannel::CHANNEL_MAIN);
callbackInfo.channels = this->getChannelCount();
ep->setVolumeCallback(epc);
callbackInfo.channelVolumes.resize(this->callbackInfo.channels);
for(uint32_t i = 0; i < this->getChannelCount(); i++){
callbackInfo.channelVolumes[i] = this->getVolume(i);
}
}
} }
void EndpointHandler::setFrontVisibilityInfo(EndpointState state, uint64_t frontIdx){ void EndpointHandler::setFrontVisibilityInfo(EndpointState state, uint64_t frontIdx){
@ -91,8 +85,28 @@ size_t EndpointHandler::getState(){
return ep->getState(); return ep->getState();
} }
void EndpointHandler::setBackEndpointVolumeCallbackInfoContent(uint8_t state) {
if(state == EndpointState::ENDPOINT_ACTIVE) {
callbackInfo.muted = this->getMute();
callbackInfo.mainVolume = this->getVolume(AudioChannel::CHANNEL_MAIN);
callbackInfo.channels = this->getChannelCount();
ep->setVolumeCallback(epc);
callbackInfo.channelVolumes.resize(this->callbackInfo.channels);
for(uint32_t i = 0; i < this->getChannelCount(); i++){
callbackInfo.channelVolumes.at(i) = this->getVolume(i);
}
}
}
void EndpointHandler::setState(uint8_t state){ void EndpointHandler::setState(uint8_t state){
ep->setState(state); ep->setState(state);
this->setBackEndpointVolumeCallbackInfoContent(state);
}
void EndpointHandler::setState(uint8_t state, uint64_t index){
ep->setState(state);
this->setFrontVisibilityInfo((EndpointState)state, index);
this->setBackEndpointVolumeCallbackInfoContent(state);
} }
uint8_t EndpointHandler::getRoles(){ uint8_t EndpointHandler::getRoles(){
@ -168,25 +182,27 @@ void OverseerHandler::changeFrontDefaultsCallback(Roles role, std::wstring endpo
} }
void OverseerHandler::reviseEndpointShowing(std::wstring endpointId, EndpointState state) { void OverseerHandler::reviseEndpointShowing(std::wstring endpointId, EndpointState state) {
EndpointHandler* affected = nullptr; EndpointHandler* eph = nullptr;
for (auto eph : this->endpointHandlers) { for (auto loopEph : this->endpointHandlers) {
if (eph->getId() == endpointId) { if (loopEph->getId() == endpointId) {
affected = eph; eph = loopEph;
break; break;
} }
} }
//todo: //todo:
if(EndpointState::ENDPOINT_ACTIVE & state) { if(EndpointState::ENDPOINT_ACTIVE & state) {
//todo: this->addEndpointWidget(eph);
return; } else if (eph->getFrontVisibilityState() == EndpointState::ENDPOINT_ACTIVE){
} else if (affected->getFrontVisibilityState() == EndpointState::ENDPOINT_ACTIVE){ this->removeEndpointWidget(eph->getFrontVisibilityIndex());
this->removeEndpointWidget(affected->getFrontVisibilityIndex());
affected->setFrontVisibilityInfo(EndpointState::ENDPOINT_ALL, INT_MAX);
} }
return; return;
//this->reviseEndpointShowing(endpointId, role); //this->reviseEndpointShowing(endpointId, role);
} }
void OverseerHandler::setAddEndpointWidgetFunction(std::function<void(EndpointHandler*)> addEndpointWidget){
this->addEndpointWidget = addEndpointWidget;
}
void OverseerHandler::setRemoveEndpointWidgetFunction(std::function<void(uint64_t)> removeEndpointWidget){ void OverseerHandler::setRemoveEndpointWidgetFunction(std::function<void(uint64_t)> removeEndpointWidget){
this->removeEndpointWidget = removeEndpointWidget; this->removeEndpointWidget = removeEndpointWidget;
} }

View file

@ -62,6 +62,8 @@ class EndpointHandler {
public: public:
EndpointHandler(uint64_t idx); EndpointHandler(uint64_t idx);
void setBackEndpointVolumeCallbackInfoContent(uint8_t state);
//these two, currently unused. If I use them, I should feel bad. //these two, currently unused. If I use them, I should feel bad.
//EndpointVolumeCallback* getEndpointVolumeCallback(); //EndpointVolumeCallback* getEndpointVolumeCallback();
//Endpoint* getEndpoint(); //Endpoint* getEndpoint();
@ -92,12 +94,14 @@ public:
void setVolume(NGuid guid, int channel, int value); void setVolume(NGuid guid, int channel, int value);
void setMute(NGuid guid, bool muted); void setMute(NGuid guid, bool muted);
void setState(uint8_t state); void setState(uint8_t state);
void setState(uint8_t state, uint64_t idx);
~EndpointHandler(); ~EndpointHandler();
private: private:
uint64_t idx; uint64_t idx;
Endpoint *ep = nullptr; Endpoint *ep = nullptr;
EndpointVolumeCallback *epc = nullptr; EndpointVolumeCallback *epc = nullptr;
BackEndpointVolumeCallbackInfo callbackInfo; BackEndpointVolumeCallbackInfo callbackInfo;
struct EndpointHandlerFrontVisibility { struct EndpointHandlerFrontVisibility {
EndpointState visibility = EndpointState::ENDPOINT_ALL; EndpointState visibility = EndpointState::ENDPOINT_ALL;
@ -107,7 +111,6 @@ private:
//QSlider *slidy; //QSlider *slidy;
}; };
class OverseerHandler { class OverseerHandler {
public: public:
@ -118,6 +121,8 @@ public:
//void setReviseEndpointShowingFunction(std::function<void(std::wstring, EndpointState)> reviseEndpointShowing); //void setReviseEndpointShowingFunction(std::function<void(std::wstring, EndpointState)> reviseEndpointShowing);
void reviseEndpointShowing(std::wstring endpointId, EndpointState state); void reviseEndpointShowing(std::wstring endpointId, EndpointState state);
void setRemoveEndpointWidgetFunction(std::function<void(uint64_t)> removeEndpointWidget); void setRemoveEndpointWidgetFunction(std::function<void(uint64_t)> removeEndpointWidget);
void setAddEndpointWidgetFunction(std::function<void(EndpointHandler*)> addEndpointWidget);
void setEndpointHandlers(std::vector<EndpointHandler*> ephs); void setEndpointHandlers(std::vector<EndpointHandler*> ephs);
std::vector<EndpointHandler*> getEndpointHandlers(); std::vector<EndpointHandler*> getEndpointHandlers();
std::vector<Endpoint*> getPlaybackEndpoints(); std::vector<Endpoint*> getPlaybackEndpoints();
@ -130,6 +135,7 @@ private:
std::vector<EndpointHandler*> endpointHandlers; std::vector<EndpointHandler*> endpointHandlers;
std::function<void(Roles, std::wstring /* endpointid */)> changeFrontDefaults; std::function<void(Roles, std::wstring /* endpointid */)> changeFrontDefaults;
std::function<void(uint64_t /* epw id */)> removeEndpointWidget; std::function<void(uint64_t /* epw id */)> removeEndpointWidget;
std::function<void(EndpointHandler*)> addEndpointWidget;
//std::function<void(uint64_t /* device */, uint32_t /* channel */, float /* value */)> updateFrontVolumeCallback; //std::function<void(uint64_t /* device */, uint32_t /* channel */, float /* value */)> updateFrontVolumeCallback;
//std::function<void(uint64_t /* device */, bool /* mute */)> updateFrontMuteCallback; //std::function<void(uint64_t /* device */, bool /* mute */)> updateFrontMuteCallback;

View file

@ -1,7 +1,8 @@
#include "qtclasses.h" #include "qtclasses.h"
EndpointWidgetEvent::EndpointWidgetEvent(QEvent::Type type, int idx) : QEvent(type){ template <typename T>
this->idx = idx; EndpointWidgetEvent<T>::EndpointWidgetEvent(QEvent::Type type, T payload) : QEvent(type){
this->payload = payload;
} }
void ExtendedCheckBox::customEvent(QEvent* ev) { void ExtendedCheckBox::customEvent(QEvent* ev) {
@ -28,11 +29,13 @@ EndpointWidget::EndpointWidget(uint64_t idx, EndpointHandler* eph, QWidget *pare
//todo: based on qgridlayout, name+mute should be its own widget, same with channels //todo: based on qgridlayout, name+mute should be its own widget, same with channels
this->idx = idx; this->idx = idx;
this->eph = eph; this->eph = eph;
this->eph->setFrontVisibilityInfo(EndpointState::ENDPOINT_ACTIVE, idx); //todo: sussy
this->eph->setState(EndpointState::ENDPOINT_ACTIVE, idx);
layout = new QGridLayout(this); layout = new QGridLayout(this);
//this->setLayout(layout); //this->setLayout(layout);
log_debugcpp("epw main layout parent: " + std::to_string((intptr_t)(layout->parent()))); log_debugcpp("epw main layout parent: " + std::to_string((intptr_t)(layout->parent())));
if (parent == nullptr) { log_debugcpp("owo?"); } if (parent == nullptr) { log_debugcpp("ayooooo?"); }
defaultRolesCheckBoxes = { defaultRolesCheckBoxes = {
{Roles::ROLE_ALL, new ExtendedCheckBox(this)}, {Roles::ROLE_ALL, new ExtendedCheckBox(this)},
@ -183,31 +186,44 @@ EndpointWidget::EndpointWidget(uint64_t idx, EndpointHandler* eph, QWidget *pare
log_debugcpp("ENDPOINT_WIDGETED"); log_debugcpp("ENDPOINT_WIDGETED");
} }
EndpointWidget::~EndpointWidget() {
this->eph->setFrontVisibilityInfo(EndpointState::ENDPOINT_ALL, INT_MAX);
}
void MainWindow::customEvent(QEvent* ev) { void MainWindow::customEvent(QEvent* ev) {
if (ev->type() == CustomQEvent::EndpointWidgetObsolete) { if (ev->type() == CustomQEvent::EndpointWidgetObsolete) {
ev->setAccepted(true); ev->setAccepted(true);
this->removeEndpointWidget((EndpointWidgetEvent*)ev); this->removeEndpointWidget((EndpointWidgetEvent<uint64_t>*)ev);
return; return;
} else if (ev->type() == (QEvent::Type)CustomQEvent::EndpointWidgetCreated) {
ev->setAccepted(true);
this->addEndpointWidget((EndpointWidgetEvent<EndpointHandler*>*)ev);
} }
// Make sure the rest of events are handled // Make sure the rest of events are handled
QMainWindow::customEvent(ev); QMainWindow::customEvent(ev);
} }
void MainWindow::removeEndpointWidget(EndpointWidgetEvent* ev){ void MainWindow::removeEndpointWidget(EndpointWidgetEvent<uint64_t>* ev){
uint64_t i = ev->idx; uint64_t i = ev->payload;
this->ews.at(i)->setParent(nullptr); this->ews.at(i)->setParent(nullptr);
this->layout->removeWidget(ews.at(i)); this->layout->removeWidget(ews.at(i));
uint64_t saisu = ews.size(); uint64_t saisu = ews.size();
//delete ews.at(index); //delete ews.at(index);
while ((i + 1) < ews.size()) { while ((i + 1) < ews.size()) {
ews.at(i) = ews.at(i + 1); ews.at(i) = ews.at(i + 1);
ews.at(i)->updateEndpointHandlerFrontInfo(i); ews.at(i)->updateEndpointHandlerFrontIndex(i);
i++; i++;
} }
ews.pop_back(); ews.pop_back();
return; return;
} }
void MainWindow::addEndpointWidget(EndpointWidgetEvent<EndpointHandler*>* ev){
EndpointWidget* epw = new EndpointWidget(this->ews.size(), ev->payload, widget);
this->layout->addWidget(epw);
ews.push_back(epw);
return;
}
void EndpointWidget::updateMute(int checked){ void EndpointWidget::updateMute(int checked){
bool muted = (checked == 2 ? true : false); bool muted = (checked == 2 ? true : false);
@ -254,7 +270,7 @@ EndpointHandler* EndpointWidget::getEndpointHandler(){
return this->eph; return this->eph;
} }
void EndpointWidget::updateEndpointHandlerFrontInfo(uint64_t index){ void EndpointWidget::updateEndpointHandlerFrontIndex(uint64_t index){
this->eph->setFrontVisibilityInfo(EndpointState::ENDPOINT_ACTIVE, index); this->eph->setFrontVisibilityInfo(EndpointState::ENDPOINT_ACTIVE, index);
} }
@ -275,6 +291,7 @@ MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) {
* Registering needed custom events * Registering needed custom events
*/ */
QEvent::registerEventType(CustomQEvent::EndpointWidgetObsolete); QEvent::registerEventType(CustomQEvent::EndpointWidgetObsolete);
QEvent::registerEventType(CustomQEvent::EndpointWidgetCreated);
QEvent::registerEventType(CustomQEvent::EndpointDefaultChange); QEvent::registerEventType(CustomQEvent::EndpointDefaultChange);
widget = new QWidget(); widget = new QWidget();
@ -348,20 +365,14 @@ MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) {
}); });
osh->setRemoveEndpointWidgetFunction([this](uint64_t index) { osh->setRemoveEndpointWidgetFunction([this](uint64_t index) {
EndpointWidgetEvent* removeObsoleteEndpointWidget = new EndpointWidgetEvent((QEvent::Type)CustomQEvent::EndpointWidgetObsolete, index); QCoreApplication::instance()->postEvent(this, new EndpointWidgetEvent<uint64_t>((QEvent::Type)CustomQEvent::EndpointWidgetObsolete, index));
QCoreApplication::instance()->postEvent(this, removeObsoleteEndpointWidget);
}); });
/* osh->setAddEndpointWidgetFunction([this](EndpointHandler* eph) {
* osh->setReviseEndpointShowingFunction([this](std::wstring endpointId, Roles role){ QCoreApplication::instance()->postEvent(this, new EndpointWidgetEvent<EndpointHandler*>((QEvent::Type)CustomQEvent::EndpointWidgetCreated, eph));
* });
*
* });
*/
} }
void MainWindow::closeEvent(QCloseEvent *event) { void MainWindow::closeEvent(QCloseEvent *event) {
if (!event->spontaneous() || !isVisible()) return; if (!event->spontaneous() || !isVisible()) return;
@ -389,7 +400,7 @@ void MainWindow::reloadEndpointWidgets() {
for (size_t epwIndex = 0; i < (osh->getEndpointHandlers().size()); i++) { for (size_t epwIndex = 0; i < (osh->getEndpointHandlers().size()); i++) {
if (osh->getEndpointHandlers().at(i)->getState() == EndpointState::ENDPOINT_ACTIVE){ if (osh->getEndpointHandlers().at(i)->getState() == EndpointState::ENDPOINT_ACTIVE){
log_debugcpp("EPWidget creation"); log_debugcpp("EPWidget creation");
osh->getEndpointHandlers().at(i)->getCallbackInfo()->caller = osh->getGuid(); //osh->getEndpointHandlers().at(i)->getCallbackInfo()->caller = osh->getGuid();
EndpointWidget *epw = new EndpointWidget(epwIndex, osh->getEndpointHandlers().at(i), widget); EndpointWidget *epw = new EndpointWidget(epwIndex, osh->getEndpointHandlers().at(i), widget);
epwIndex++; epwIndex++;
//alfinal estoes solopara inicializarlmao //alfinal estoes solopara inicializarlmao

View file

@ -58,14 +58,17 @@
*/ */
enum CustomQEvent { enum CustomQEvent {
EndpointWidgetObsolete = 1001, EndpointWidgetObsolete = 1001,
EndpointDefaultChange = 1002, EndpointWidgetCreated = 1002,
EndpointDefaultChange = 1003,
}; };
template <typename T>
class EndpointWidgetEvent : public QEvent { class EndpointWidgetEvent : public QEvent {
public: public:
EndpointWidgetEvent(QEvent::Type type, int idx); EndpointWidgetEvent(QEvent::Type type, T payload);
uint64_t idx; T payload;
}; };
//Q_DECLARE_METATYPE(EndpointWidgetEvent) //Q_DECLARE_METATYPE(EndpointWidgetEvent)
@ -89,7 +92,7 @@ public:
EndpointWidget(uint64_t idx, EndpointHandler* eph, QWidget *parent = nullptr); EndpointWidget(uint64_t idx, EndpointHandler* eph, QWidget *parent = nullptr);
EndpointHandler* getEndpointHandler(); EndpointHandler* getEndpointHandler();
void updateEndpointHandlerFrontInfo(uint64_t index); void updateEndpointHandlerFrontIndex(uint64_t index);
void setIndex(uint64_t idx); void setIndex(uint64_t idx);
uint64_t getIndex(); uint64_t getIndex();
@ -104,6 +107,7 @@ public:
QGridLayout *layout = nullptr; QGridLayout *layout = nullptr;
QGridLayout *mainMuteLayout = nullptr; QGridLayout *mainMuteLayout = nullptr;
std::map<Roles, ExtendedCheckBox*> defaultRolesCheckBoxes; std::map<Roles, ExtendedCheckBox*> defaultRolesCheckBoxes;
~EndpointWidget();
//void updateMainVolume(float newValue); //void updateMainVolume(float newValue);
//void updateVolume(uint32_t channel, float newValue); //void updateVolume(uint32_t channel, float newValue);
@ -143,7 +147,8 @@ protected:
private slots: private slots:
void trayIconActivated(QSystemTrayIcon::ActivationReason reason); void trayIconActivated(QSystemTrayIcon::ActivationReason reason);
void removeEndpointWidget(EndpointWidgetEvent* ev); void removeEndpointWidget(EndpointWidgetEvent<uint64_t>* ev);
void addEndpointWidget(EndpointWidgetEvent<EndpointHandler*>* ev);
//TODO: destroy/empty existing EndpointWidgets //TODO: destroy/empty existing EndpointWidgets
//void setEndpointHandlers(std::vector<EndpointHandler*> *ephs); //void setEndpointHandlers(std::vector<EndpointHandler*> *ephs);