Skip to content

Commit

Permalink
added auto desktop audio output detection
Browse files Browse the repository at this point in the history
  • Loading branch information
Odizinne committed Nov 7, 2024
1 parent 80ac98b commit 4f95841
Show file tree
Hide file tree
Showing 7 changed files with 152 additions and 82 deletions.
25 changes: 20 additions & 5 deletions AudioManager/AudioManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,15 +49,16 @@ std::vector<Device> parseDevices(const std::string &output)
if (deviceStarted) {
devices.push_back(currentDevice);
}
currentDevice = Device{-1, "", ""};
currentDevice = Device{-1, "", "", false};
deviceStarted = true;
currentDevice.index = std::stoi(line.substr(line.find(":") + 2));
} else if (line.find("Name") != std::string::npos) {
std::string_view lineView(line);
currentDevice.name = std::string(lineView.substr(lineView.find(":") + 2));
currentDevice.name = line.substr(line.find(":") + 2);
} else if (line.find("Type") != std::string::npos) {
std::string_view lineView(line);
currentDevice.type = std::string(lineView.substr(lineView.find(":") + 2));
currentDevice.type = line.substr(line.find(":") + 2);
} else if (line.find("Default") != std::string::npos) {
std::string defaultValue = line.substr(line.find(":") + 2);
currentDevice.isDefault = (defaultValue == "True");
}
}

Expand Down Expand Up @@ -90,6 +91,20 @@ bool checkDevice(const std::string &deviceName)
return false;
}

std::string AudioManager::getDefaultOutputDevice()
{
std::string output = executeCommand("Get-AudioDevice -l");
std::vector<Device> devices = parseDevices(output);

for (const auto &device : devices) {
if (device.type == "Playback" && device.isDefault) {
return device.name;
}
}

return "";
}

void AudioManager::setAudioDevice(const std::string &deviceName)
{
bool deviceFound = false;
Expand Down
2 changes: 2 additions & 0 deletions AudioManager/AudioManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,14 @@ struct Device
int index;
std::string name;
std::string type;
bool isDefault = false;
};

namespace AudioManager
{
void setAudioDevice(const std::string &deviceName);
void detectNewOutputs();
std::string getDefaultOutputDevice();
};

#endif // AUDIOMANAGER_H
8 changes: 8 additions & 0 deletions BigPictureTV/BigPictureTV.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,9 @@ void BigPictureTV::checkWindowTitle()
}

if (isRunning && !gamemodeActive) {
if (autodetect_desktop) {
currentAudioOutputName = AudioManager::getDefaultOutputDevice();
}
gamemodeActive = true;
handleActions(false);
handleMonitorChanges(false, disable_monitor_switch);
Expand Down Expand Up @@ -124,6 +127,10 @@ void BigPictureTV::handleAudioChanges(bool isDesktopMode, bool disableAudio)
return;
}

if (autodetect_desktop && isDesktopMode) {
AudioManager::setAudioDevice(currentAudioOutputName);
}

QString audioDevice = isDesktopMode ? desktop_audio_device
: gamemode_audio_device;

Expand Down Expand Up @@ -207,6 +214,7 @@ void BigPictureTV::loadSettings()
target_window_mode = settings.value("target_window_mode").toInt();
custom_window_title = settings.value("custom_window_title").toString();
autodetect_HDMI = settings.value("autodetect_hdmi").toBool();
autodetect_desktop = settings.value("autodetect_desktop").toBool();

qDebug() << "Audio configuration:";
qDebug() << "";
Expand Down
2 changes: 2 additions & 0 deletions BigPictureTV/BigPictureTV.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ private slots:
bool disable_nightlight_action;
bool target_window_mode;
bool autodetect_HDMI;
bool autodetect_desktop;
std::string currentAudioOutputName;
QSettings settings;
bool gamemodeActive;
};
Expand Down
17 changes: 16 additions & 1 deletion Configurator/Configurator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ Configurator::Configurator(QWidget *parent)
ui->actionsFrame->setVisible(false);
ui->advancedFrame->setVisible(false);
Utils::setFrameColorBasedOnWindow(this, ui->frame);
this->setFixedSize(356, 224);
this->setFixedSize(382, 224);
setupConnections();
getAudioCapabilities();
}
Expand All @@ -50,6 +50,7 @@ void Configurator::setupConnections()
connect(ui->actionsButton, &QPushButton::clicked, this, &Configurator::setActionsTab);
connect(ui->advancedButton, &QPushButton::clicked, this, &Configurator::setAdvancedTab);
connect(ui->autodetectCheckBox, &QCheckBox::checkStateChanged, this, &Configurator::onAutodetectCheckBoxStateChanged);
connect(ui->autodetectDesktopCheckBox, &QCheckBox::checkStateChanged, this, &Configurator::onAutodetectDesktopCheckBoxStateChanged);

ui->startupCheckBox->setChecked(ShortcutManager::isShortcutPresent("BigPictureTV.lnk"));
initDiscordAction();
Expand Down Expand Up @@ -82,6 +83,9 @@ void Configurator::getAudioCapabilities()
if (ui->autodetectCheckBox->isChecked()) {
ui->gamemodeAudioLineEdit->setEnabled(false);
}
if (ui->autodetectDesktopCheckBox->isChecked()) {
ui->desktopAudioLineEdit->setEnabled(false);
}
}
}

Expand Down Expand Up @@ -121,6 +125,14 @@ void Configurator::onAutodetectCheckBoxStateChanged(Qt::CheckState state)
}
}

void Configurator::onAutodetectDesktopCheckBoxStateChanged(Qt::CheckState state)
{
bool isChecked = (state == Qt::Checked);
if (!ui->disableAudioCheckBox->isChecked()) {
ui->desktopAudioLineEdit->setDisabled(isChecked);
}
}

void Configurator::onTargetWindowComboBoxIndexChanged(int index)
{
if (index == 1) {
Expand Down Expand Up @@ -210,11 +222,13 @@ void Configurator::loadSettings()
ui->customWindowLineEdit->setText(settings.value("custom_window_title", "").toString());
ui->targetWindowComboBox->setCurrentIndex(settings.value("target_window_mode", 0).toInt());
ui->autodetectCheckBox->setChecked(settings.value("autodetect_hdmi", false).toBool());
ui->autodetectDesktopCheckBox->setChecked(settings.value("autodetect_desktop", false).toBool());

toggleAudioSettings(!ui->disableAudioCheckBox->isChecked());
toggleMonitorSettings(!ui->disableMonitorCheckBox->isChecked());
toggleCustomWindowTitle(ui->targetWindowComboBox->currentIndex() == 1);
ui->gamemodeAudioLineEdit->setDisabled(ui->autodetectCheckBox->isChecked());
ui->desktopAudioLineEdit->setDisabled(ui->autodetectDesktopCheckBox->isChecked());
}

void Configurator::saveSettings()
Expand All @@ -233,6 +247,7 @@ void Configurator::saveSettings()
settings.setValue("target_window_mode", ui->targetWindowComboBox->currentIndex());
settings.setValue("custom_window_title", ui->customWindowLineEdit->text());
settings.setValue("autodetect_hdmi", ui->autodetectCheckBox->isChecked());
settings.setValue("autodetect_desktop", ui->autodetectDesktopCheckBox->isChecked());
}

void Configurator::toggleAudioSettings(bool state)
Expand Down
1 change: 1 addition & 0 deletions Configurator/Configurator.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ private slots:
void onDisableAudioCheckboxStateChanged(Qt::CheckState state);
void onDisableMonitorCheckboxStateChanged(Qt::CheckState state);
void onAutodetectCheckBoxStateChanged(Qt::CheckState state);
void onAutodetectDesktopCheckBoxStateChanged(Qt::CheckState state);
void onTargetWindowComboBoxIndexChanged(int index);
void onAudioButtonClicked();
void setGeneralTab();
Expand Down
Loading

0 comments on commit 4f95841

Please sign in to comment.