alError.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. /**
  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.
  8. *
  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.
  13. *
  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
  19. */
  20. #include "config.h"
  21. #include <signal.h>
  22. #include <stdarg.h>
  23. #ifdef HAVE_WINDOWS_H
  24. #define WIN32_LEAN_AND_MEAN
  25. #include <windows.h>
  26. #endif
  27. #include "alMain.h"
  28. #include "AL/alc.h"
  29. #include "alError.h"
  30. ALboolean TrapALError = AL_FALSE;
  31. void alSetError(ALCcontext *context, ALenum errorCode, const char *msg, ...)
  32. {
  33. ALenum curerr = AL_NO_ERROR;
  34. char message[1024] = { 0 };
  35. va_list args;
  36. int msglen;
  37. va_start(args, msg);
  38. msglen = vsnprintf(message, sizeof(message), msg, args);
  39. va_end(args);
  40. if(msglen < 0 || (size_t)msglen >= sizeof(message))
  41. {
  42. message[sizeof(message)-1] = 0;
  43. msglen = (int)strlen(message);
  44. }
  45. if(msglen > 0)
  46. msg = message;
  47. else
  48. {
  49. msg = "<internal error constructing message>";
  50. msglen = (int)strlen(msg);
  51. }
  52. WARN("Error generated on context %p, code 0x%04x, \"%s\"\n",
  53. context, errorCode, message);
  54. if(TrapALError)
  55. {
  56. #ifdef _WIN32
  57. /* DebugBreak will cause an exception if there is no debugger */
  58. if(IsDebuggerPresent())
  59. DebugBreak();
  60. #elif defined(SIGTRAP)
  61. raise(SIGTRAP);
  62. #endif
  63. }
  64. ATOMIC_COMPARE_EXCHANGE_STRONG_SEQ(&context->LastError, &curerr, errorCode);
  65. if((ATOMIC_LOAD(&context->EnabledEvts, almemory_order_relaxed)&EventType_Error))
  66. {
  67. ALbitfieldSOFT enabledevts;
  68. almtx_lock(&context->EventCbLock);
  69. enabledevts = ATOMIC_LOAD(&context->EnabledEvts, almemory_order_relaxed);
  70. if((enabledevts&EventType_Error) && context->EventCb)
  71. (*context->EventCb)(AL_EVENT_TYPE_ERROR_SOFT, 0, errorCode, msglen, msg,
  72. context->EventParam);
  73. almtx_unlock(&context->EventCbLock);
  74. }
  75. }
  76. AL_API ALenum AL_APIENTRY alGetError(void)
  77. {
  78. ALCcontext *context;
  79. ALenum errorCode;
  80. context = GetContextRef();
  81. if(!context)
  82. {
  83. const ALenum deferror = AL_INVALID_OPERATION;
  84. WARN("Querying error state on null context (implicitly 0x%04x)\n", deferror);
  85. if(TrapALError)
  86. {
  87. #ifdef _WIN32
  88. if(IsDebuggerPresent())
  89. DebugBreak();
  90. #elif defined(SIGTRAP)
  91. raise(SIGTRAP);
  92. #endif
  93. }
  94. return deferror;
  95. }
  96. errorCode = ATOMIC_EXCHANGE_SEQ(&context->LastError, AL_NO_ERROR);
  97. ALCcontext_DecRef(context);
  98. return errorCode;
  99. }