2021-03-05 13:18:54 +01:00
|
|
|
/*
|
|
|
|
* Copyright 2020-2021 The Music Player Daemon Project
|
|
|
|
* http://www.musicpd.org
|
|
|
|
*
|
|
|
|
* This program 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 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program 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 this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "HResult.hxx"
|
2021-03-08 22:53:59 +01:00
|
|
|
#include "system/Error.hxx"
|
2021-03-05 13:18:54 +01:00
|
|
|
|
2021-03-05 13:36:59 +01:00
|
|
|
#include <cassert>
|
2021-03-05 13:18:54 +01:00
|
|
|
#include <cstdarg>
|
2021-03-05 13:36:59 +01:00
|
|
|
#include <cstdio>
|
2021-03-05 13:18:54 +01:00
|
|
|
#include <memory>
|
|
|
|
|
2021-05-19 17:43:43 +02:00
|
|
|
#include <audiopolicy.h>
|
|
|
|
|
|
|
|
std::string_view
|
|
|
|
HRESULTToString(HRESULT result) noexcept
|
|
|
|
{
|
|
|
|
using namespace std::literals;
|
|
|
|
switch (result) {
|
|
|
|
#define C(x) \
|
|
|
|
case x: \
|
|
|
|
return #x##sv
|
|
|
|
C(AUDCLNT_E_ALREADY_INITIALIZED);
|
|
|
|
C(AUDCLNT_E_BUFDURATION_PERIOD_NOT_EQUAL);
|
|
|
|
C(AUDCLNT_E_BUFFER_ERROR);
|
|
|
|
C(AUDCLNT_E_BUFFER_OPERATION_PENDING);
|
|
|
|
C(AUDCLNT_E_BUFFER_SIZE_ERROR);
|
|
|
|
C(AUDCLNT_E_BUFFER_SIZE_NOT_ALIGNED);
|
|
|
|
C(AUDCLNT_E_BUFFER_TOO_LARGE);
|
|
|
|
C(AUDCLNT_E_CPUUSAGE_EXCEEDED);
|
|
|
|
C(AUDCLNT_E_DEVICE_INVALIDATED);
|
|
|
|
C(AUDCLNT_E_DEVICE_IN_USE);
|
|
|
|
C(AUDCLNT_E_ENDPOINT_CREATE_FAILED);
|
|
|
|
C(AUDCLNT_E_EXCLUSIVE_MODE_NOT_ALLOWED);
|
|
|
|
C(AUDCLNT_E_INVALID_DEVICE_PERIOD);
|
|
|
|
C(AUDCLNT_E_OUT_OF_ORDER);
|
|
|
|
C(AUDCLNT_E_SERVICE_NOT_RUNNING);
|
|
|
|
C(AUDCLNT_E_UNSUPPORTED_FORMAT);
|
|
|
|
C(AUDCLNT_E_WRONG_ENDPOINT_TYPE);
|
|
|
|
C(AUDCLNT_E_NOT_INITIALIZED);
|
|
|
|
C(AUDCLNT_E_NOT_STOPPED);
|
|
|
|
C(CO_E_NOTINITIALIZED);
|
|
|
|
C(E_INVALIDARG);
|
|
|
|
C(E_OUTOFMEMORY);
|
|
|
|
C(E_POINTER);
|
|
|
|
C(NO_ERROR);
|
|
|
|
#undef C
|
|
|
|
}
|
|
|
|
return std::string_view();
|
|
|
|
}
|
|
|
|
|
2021-03-05 13:36:59 +01:00
|
|
|
std::string
|
|
|
|
HResultCategory::message(int Errcode) const
|
|
|
|
{
|
2021-03-08 22:53:59 +01:00
|
|
|
char buffer[256];
|
|
|
|
|
|
|
|
/* FormatMessage() supports some HRESULT values (depending on
|
|
|
|
the Windows version) */
|
|
|
|
if (FormatMessageA(FORMAT_MESSAGE_FROM_SYSTEM |
|
|
|
|
FORMAT_MESSAGE_IGNORE_INSERTS,
|
|
|
|
nullptr, Errcode, 0,
|
|
|
|
buffer, sizeof(buffer),
|
|
|
|
nullptr))
|
|
|
|
return buffer;
|
|
|
|
|
2021-03-05 13:36:59 +01:00
|
|
|
const auto msg = HRESULTToString(Errcode);
|
|
|
|
if (!msg.empty())
|
|
|
|
return std::string(msg);
|
|
|
|
|
|
|
|
int size = snprintf(buffer, sizeof(buffer), "0x%1x", Errcode);
|
|
|
|
assert(2 <= size && size <= 10);
|
|
|
|
return std::string(buffer, size);
|
|
|
|
}
|
|
|
|
|
2021-03-05 13:18:54 +01:00
|
|
|
std::system_error
|
|
|
|
FormatHResultError(HRESULT result, const char *fmt, ...) noexcept
|
|
|
|
{
|
|
|
|
std::va_list args1, args2;
|
|
|
|
va_start(args1, fmt);
|
|
|
|
va_copy(args2, args1);
|
|
|
|
|
|
|
|
const int size = vsnprintf(nullptr, 0, fmt, args1);
|
|
|
|
va_end(args1);
|
|
|
|
assert(size >= 0);
|
|
|
|
|
|
|
|
auto buffer = std::make_unique<char[]>(size + 1);
|
|
|
|
vsprintf(buffer.get(), fmt, args2);
|
|
|
|
va_end(args2);
|
|
|
|
|
|
|
|
return std::system_error(std::error_code(result, hresult_category()),
|
|
|
|
std::string(buffer.get(), size));
|
|
|
|
}
|