2 * OpenAL cross platform audio library
3 * Copyright (C) 1999-2000 by authors.
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
14 * You should have received a copy of the GNU Library General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 * Or go to http://www.gnu.org/copyleft/lgpl.html
24 #define WIN32_LEAN_AND_MEAN
42 #include "alc/alconfig.h"
43 #include "alc/context.h"
46 #include "core/except.h"
47 #include "core/logging.h"
48 #include "direct_defs.h"
49 #include "opthelpers.h"
53 bool TrapALError
{false};
55 void ALCcontext::setError(ALenum errorCode
, const char *msg
, ...)
57 auto message
= std::vector
<char>(256);
59 /* NOLINTBEGIN(*-array-to-pointer-decay) */
63 int msglen
{std::vsnprintf(message
.data(), message
.size(), msg
, args
)};
64 if(msglen
>= 0 && static_cast<size_t>(msglen
) >= message
.size())
66 message
.resize(static_cast<size_t>(msglen
) + 1u);
67 msglen
= std::vsnprintf(message
.data(), message
.size(), msg
, args2
);
71 /* NOLINTEND(*-array-to-pointer-decay) */
77 msg
= "<internal error constructing message>";
78 msglen
= static_cast<int>(strlen(msg
));
81 WARN("Error generated on context %p, code 0x%04x, \"%s\"\n",
82 decltype(std::declval
<void*>()){this}, errorCode
, msg
);
86 /* DebugBreak will cause an exception if there is no debugger */
87 if(IsDebuggerPresent())
89 #elif defined(SIGTRAP)
94 ALenum curerr
{AL_NO_ERROR
};
95 mLastError
.compare_exchange_strong(curerr
, errorCode
);
97 debugMessage(DebugSource::API
, DebugType::Error
, 0, DebugSeverity::High
,
98 {msg
, static_cast<uint
>(msglen
)});
101 /* Special-case alGetError since it (potentially) raises a debug signal and
102 * returns a non-default value for a null context.
104 AL_API
auto AL_APIENTRY
alGetError() noexcept
-> ALenum
106 if(auto context
= GetContextRef()) LIKELY
107 return alGetErrorDirect(context
.get());
109 auto get_value
= [](const char *envname
, const char *optname
) -> ALenum
111 auto optstr
= al::getenv(envname
);
113 optstr
= ConfigValueStr({}, "game_compat", optname
);
117 auto value
= std::strtoul(optstr
->c_str(), &end
, 0);
118 if(end
&& *end
== '\0' && value
<= std::numeric_limits
<ALenum
>::max())
119 return static_cast<ALenum
>(value
);
120 ERR("Invalid default error value: \"%s\"", optstr
->c_str());
122 return AL_INVALID_OPERATION
;
124 static const ALenum deferror
{get_value("__ALSOFT_DEFAULT_ERROR", "default-error")};
126 WARN("Querying error state on null context (implicitly 0x%04x)\n", deferror
);
130 if(IsDebuggerPresent())
132 #elif defined(SIGTRAP)
139 FORCE_ALIGN ALenum AL_APIENTRY
alGetErrorDirect(ALCcontext
*context
) noexcept
141 return context
->mLastError
.exchange(AL_NO_ERROR
);