mirror of
https://github.com/overte-org/overte.git
synced 2025-08-09 06:58:56 +02:00
clean up select_audio_device
This commit is contained in:
parent
837f37220a
commit
3d80774317
2 changed files with 49 additions and 50 deletions
|
@ -21,27 +21,21 @@
|
||||||
|
|
||||||
using namespace scripting;
|
using namespace scripting;
|
||||||
|
|
||||||
static const INPUT = "INPUT";
|
static Setting::Handle<QString> desktopInputDeviceSetting { QStringList { Audio::AUDIO, Audio::DESKTOP, "INPUT" }};
|
||||||
static const OUTPUT= "OUTPUT";
|
static Setting::Handle<QString> desktopOutputDeviceSetting { QStringList { Audio::AUDIO, Audio::DESKTOP, "OUTPUT" }};
|
||||||
|
static Setting::Handle<QString> hmdInputDeviceSetting { QStringList { Audio::AUDIO, Audio::HMD, "INPUT" }};
|
||||||
static Setting::Handle<QString> desktopInputDeviceSetting { QStringList { Audio::AUDIO, Audio::DESKTOP, INPUT }};
|
static Setting::Handle<QString> hmdOutputDeviceSetting { QStringList { Audio::AUDIO, Audio::HMD, "OUTPUT" }};
|
||||||
static Setting::Handle<QString> desktopOutputDeviceSetting { QStringList { Audio::AUDIO, Audio::DESKTOP, OUTPUT }};
|
|
||||||
static Setting::Handle<QString> hmdInputDeviceSetting { QStringList { Audio::AUDIO, Audio::HMD, INPUT }};
|
|
||||||
static Setting::Handle<QString> hmdOutputDeviceSetting { QStringList { Audio::AUDIO, Audio::HMD, OUTPUT }};
|
|
||||||
|
|
||||||
const bool hmdSetting = true;
|
const bool hmdSetting = true;
|
||||||
const bool desktopSetting = false;
|
const bool desktopSetting = false;
|
||||||
|
|
||||||
std::map<bool, std::map<QAudio::Mode, QString>> deviceSettings {
|
Setting::Handle<QString>& getSetting(bool contextIsHMD, QAudio::Mode mode) {
|
||||||
{ desktopSetting,
|
if (mode == QAudio::AudioInput) {
|
||||||
{ QAudio::AudioInput, desktopInputDeviceSetting },
|
return contextIsHMD ? hmdInputDeviceSetting : desktopInputDeviceSetting;
|
||||||
{ QAudio::AudioOutput, desktopOutputDeviceSetting }
|
} else { // if (mode == QAudio::AudioOutput)
|
||||||
},
|
return contextIsHMD ? hmdOutputDeviceSetting : desktopOutputDeviceSetting;
|
||||||
{ hmdSetting,
|
}
|
||||||
{ QAudio::AudioInput, hmdInputDeviceSetting },
|
}
|
||||||
{ QAudio::AudioOutput, hmdOutputDeviceSetting }
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
QHash<int, QByteArray> AudioDeviceList::_roles {
|
QHash<int, QByteArray> AudioDeviceList::_roles {
|
||||||
{ Qt::DisplayRole, "display" },
|
{ Qt::DisplayRole, "display" },
|
||||||
|
@ -64,33 +58,38 @@ QVariant AudioDeviceList::data(const QModelIndex& index, int role) const {
|
||||||
}
|
}
|
||||||
|
|
||||||
bool AudioDeviceList::setData(const QModelIndex& index, const QVariant& value, int role) {
|
bool AudioDeviceList::setData(const QModelIndex& index, const QVariant& value, int role) {
|
||||||
if (!index.isValid() || index.row() >= _devices.size()) {
|
if (!index.isValid() || index.row() >= _devices.size() || role != Qt::CheckStateRole) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// only allow switching to a new device, not deactivating an in-use device
|
||||||
|
auto selected = value.toBool();
|
||||||
|
if (!selected) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
return setDevice(index.row(), true);
|
||||||
|
}
|
||||||
|
|
||||||
|
bool AudioDeviceList::setDevice(int row, bool fromUser) {
|
||||||
bool success = false;
|
bool success = false;
|
||||||
|
auto& device = _devices[row];
|
||||||
|
|
||||||
if (role == Qt::CheckStateRole) {
|
// skip if already selected
|
||||||
auto selected = value.toBool();
|
if (!device.selected) {
|
||||||
auto& device = _devices[index.row()];
|
auto client = DependencyManager::get<AudioClient>();
|
||||||
|
QMetaObject::invokeMethod(client.data(), "switchAudioDevice", Qt::BlockingQueuedConnection,
|
||||||
|
Q_RETURN_ARG(bool, success),
|
||||||
|
Q_ARG(QAudio::Mode, _mode),
|
||||||
|
Q_ARG(const QAudioDeviceInfo&, device.info));
|
||||||
|
|
||||||
// only allow switching to a new device, not deactivating an in-use device
|
if (success) {
|
||||||
if (selected
|
device.selected = true;
|
||||||
// skip if already selected
|
if (fromUser) {
|
||||||
&& selected != device.selected) {
|
emit deviceSelected(device.info, _selectedDevice);
|
||||||
|
}
|
||||||
auto client = DependencyManager::get<AudioClient>();
|
emit deviceChanged(device.info);
|
||||||
QMetaObject::invokeMethod(client.data(), "switchAudioDevice", Qt::BlockingQueuedConnection,
|
}
|
||||||
Q_RETURN_ARG(bool, success),
|
|
||||||
Q_ARG(QAudio::Mode, _mode),
|
|
||||||
Q_ARG(const QAudioDeviceInfo&, device.info));
|
|
||||||
|
|
||||||
if (success) {
|
|
||||||
device.selected = true;
|
|
||||||
emit deviceSelected(device.info, _selectedDevice);
|
|
||||||
emit deviceChanged(device.info);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
emit dataChanged(createIndex(0, 0), createIndex(rowCount() - 1, 0));
|
emit dataChanged(createIndex(0, 0), createIndex(rowCount() - 1, 0));
|
||||||
|
@ -109,7 +108,7 @@ void AudioDeviceList::resetDevice(bool contextIsHMD, const QString& device) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (i < rowCount()) {
|
if (i < rowCount()) {
|
||||||
success = setData(createIndex(i, 0), true, Qt::CheckStateRole);
|
success = setDevice(i, false);
|
||||||
}
|
}
|
||||||
|
|
||||||
// the selection failed - reset it
|
// the selection failed - reset it
|
||||||
|
@ -197,20 +196,17 @@ AudioDevices::AudioDevices(bool& contextIsHMD) : _contextIsHMD(contextIsHMD) {
|
||||||
}
|
}
|
||||||
|
|
||||||
void AudioDevices::onContextChanged(const QString& context) {
|
void AudioDevices::onContextChanged(const QString& context) {
|
||||||
auto input = deviceSettings[_contextIsHMD][QAudio::AudioInput].get();
|
auto input = getSetting(_contextIsHMD, QAudio::AudioInput).get();
|
||||||
auto output = deviceSettings[_contextIsHMD][QAudio::AudioOutput].get();
|
auto output = getSetting(_contextIsHMD, QAudio::AudioOutput).get();
|
||||||
|
|
||||||
_inputs.resetDevice(_contextIsHMD, input);
|
_inputs.resetDevice(_contextIsHMD, input);
|
||||||
_outputs.resetDevice(_contextIsHMD, output);
|
_outputs.resetDevice(_contextIsHMD, output);
|
||||||
}
|
}
|
||||||
|
|
||||||
void AudioDevices::onDeviceSelected(QAudio::Mode mode, const QAudioDeviceInfo& device, const QAudioDeviceInfo& previousDevice) {
|
void AudioDevices::onDeviceSelected(QAudio::Mode mode, const QAudioDeviceInfo& device, const QAudioDeviceInfo& previousDevice) {
|
||||||
QString deviceName;
|
QString deviceName = device.isNull() ? QString() : device.deviceName();
|
||||||
if (!device.isNull()) {
|
|
||||||
deviceName = device.deviceName();
|
|
||||||
}
|
|
||||||
|
|
||||||
auto& setting = deviceSettings[_contextIsHMD][mode];
|
auto& setting = getSetting(_contextIsHMD, mode);
|
||||||
|
|
||||||
// check for a previous device
|
// check for a previous device
|
||||||
auto wasDefault = setting.get().isNull();
|
auto wasDefault = setting.get().isNull();
|
||||||
|
@ -223,7 +219,8 @@ void AudioDevices::onDeviceSelected(QAudio::Mode mode, const QAudioDeviceInfo& d
|
||||||
QJsonObject data;
|
QJsonObject data;
|
||||||
|
|
||||||
const QString MODE = "audio_mode";
|
const QString MODE = "audio_mode";
|
||||||
data[MODE] = mode == QAudio::AudioInput ? INPUT : OUTPUT;
|
const QString INPUT = "INPUT";
|
||||||
|
const QString OUTPUT = "OUTPUT"; data[MODE] = mode == QAudio::AudioInput ? INPUT : OUTPUT;
|
||||||
|
|
||||||
const QString CONTEXT = "display_mode";
|
const QString CONTEXT = "display_mode";
|
||||||
data[CONTEXT] = _contextIsHMD ? Audio::HMD : Audio::DESKTOP;
|
data[CONTEXT] = _contextIsHMD ? Audio::HMD : Audio::DESKTOP;
|
||||||
|
@ -238,7 +235,7 @@ void AudioDevices::onDeviceSelected(QAudio::Mode mode, const QAudioDeviceInfo& d
|
||||||
data[PREVIOUS_DEVICE] = previousDevice.deviceName();
|
data[PREVIOUS_DEVICE] = previousDevice.deviceName();
|
||||||
data[WAS_DEFAULT] = wasDefault;
|
data[WAS_DEFAULT] = wasDefault;
|
||||||
|
|
||||||
UserActivityLogger::getInstance()->logAction("selected_audio_device", data);
|
UserActivityLogger::getInstance().logAction("selected_audio_device", data);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -54,6 +54,8 @@ private slots:
|
||||||
private:
|
private:
|
||||||
friend class AudioDevices;
|
friend class AudioDevices;
|
||||||
|
|
||||||
|
bool setDevice(int index, bool fromUser);
|
||||||
|
|
||||||
static QHash<int, QByteArray> _roles;
|
static QHash<int, QByteArray> _roles;
|
||||||
static Qt::ItemFlags _flags;
|
static Qt::ItemFlags _flags;
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue