strawberry-audio-player-win.../src/engine/alsapcmdevicefinder.cpp

82 lines
2.4 KiB
C++
Raw Normal View History

/*
* Strawberry Music Player
* Copyright 2021, Jonas Kvinge <jonas@jkvinge.net>
*
* Strawberry is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Strawberry is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Strawberry. If not, see <http://www.gnu.org/licenses/>.
*
*/
#include "config.h"
#include <alsa/asoundlib.h>
#include <QString>
#include <core/logging.h>
#include "alsapcmdevicefinder.h"
2023-06-01 19:31:19 +02:00
#include "enginedevice.h"
AlsaPCMDeviceFinder::AlsaPCMDeviceFinder() : DeviceFinder(QStringLiteral("alsa"), { QStringLiteral("alsa"), QStringLiteral("alsasink") }) {}
2023-06-01 19:31:19 +02:00
EngineDeviceList AlsaPCMDeviceFinder::ListDevices() {
2023-06-01 19:31:19 +02:00
EngineDeviceList ret;
void **hints = nullptr;
if (snd_device_name_hint(-1, "pcm", &hints) < 0) {
return ret;
}
2021-08-23 21:21:08 +02:00
for (void **n = hints; *n; ++n) {
char *hint_io = snd_device_name_get_hint(*n, "IOID");
char *hint_name = snd_device_name_get_hint(*n, "NAME");
char *hint_desc = snd_device_name_get_hint(*n, "DESC");
if (hint_io && hint_name && hint_desc && strcmp(hint_io, "Output") == 0) {
const QString name = QString::fromUtf8(hint_name);
char *desc_last = hint_desc;
QString description;
for (char *desc_i = hint_desc; desc_i && *desc_i != '\0'; ++desc_i) {
if (*desc_i == '\n') {
*desc_i = '\0';
if (!description.isEmpty()) description.append(u' ');
description.append(QString::fromUtf8(desc_last));
desc_last = desc_i + 1;
}
}
if (desc_last) {
if (!description.isEmpty()) description.append(u' ');
description.append(QString::fromUtf8(desc_last));
}
2023-06-01 19:31:19 +02:00
EngineDevice device;
device.value = name;
device.description = description;
2023-06-01 19:31:19 +02:00
device.iconname = device.GuessIconName();
2021-06-20 19:04:08 +02:00
ret << device; // clazy:exclude=reserve-candidates
}
if (hint_io) free(hint_io);
if (hint_name) free(hint_name);
if (hint_desc) free(hint_desc);
}
snd_device_name_free_hint(hints);
return ret;
}