Audio plugin host https://kx.studio/carla
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

214 lines
5.2KB

  1. /*
  2. * Carla utils
  3. * Copyright (C) 2011-2013 Filipe Coelho <falktx@falktx.com>
  4. *
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU General Public License as
  7. * published by the Free Software Foundation; either version 2 of
  8. * the License, or any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * For a full copy of the GNU General Public License see the doc/GPL.txt file.
  16. */
  17. #include "CarlaDefines.hpp"
  18. // IDE Helper
  19. #ifndef REAL_BUILD
  20. # include "CarlaUtils.hpp"
  21. # include "CarlaLibUtils.hpp"
  22. #endif
  23. #include <cstdarg> // needed for va_*
  24. #include <cstdio> // needed for *printf
  25. #include <cstdlib> // needed for free
  26. #include <cstring> // needed for str*
  27. #ifdef CARLA_OS_WIN
  28. # include <winsock2.h> // should be included before "windows.h"
  29. # include <windows.h>
  30. #else
  31. # include <dlfcn.h>
  32. # include <unistd.h>
  33. #endif
  34. // -----------------------------------------------------------------------
  35. #ifdef CARLA_UTILS_HPP_INCLUDED
  36. // -----------------------------------------------------------------------
  37. // string print functions
  38. #ifdef DEBUG
  39. void carla_debug(const char* const fmt, ...)
  40. {
  41. va_list args;
  42. va_start(args, fmt);
  43. std::fprintf(stdout, "\x1b[30;1m");
  44. std::vfprintf(stdout, fmt, args);
  45. std::fprintf(stdout, "\x1b[0m\n");
  46. va_end(args);
  47. }
  48. #endif
  49. void carla_stdout(const char* const fmt, ...)
  50. {
  51. va_list args;
  52. va_start(args, fmt);
  53. std::vfprintf(stdout, fmt, args);
  54. std::fprintf(stdout, "\n");
  55. va_end(args);
  56. }
  57. void carla_stderr(const char* const fmt, ...)
  58. {
  59. va_list args;
  60. va_start(args, fmt);
  61. std::vfprintf(stderr, fmt, args);
  62. std::fprintf(stderr, "\n");
  63. va_end(args);
  64. }
  65. void carla_stderr2(const char* const fmt, ...)
  66. {
  67. va_list args;
  68. va_start(args, fmt);
  69. std::fprintf(stderr, "\x1b[31m");
  70. std::vfprintf(stderr, fmt, args);
  71. std::fprintf(stderr, "\x1b[0m\n");
  72. va_end(args);
  73. }
  74. // -----------------------------------------------------------------------
  75. // carla_*sleep
  76. void carla_sleep(const unsigned int secs)
  77. {
  78. CARLA_SAFE_ASSERT_RETURN(secs > 0,);
  79. #ifdef CARLA_OS_WIN
  80. Sleep(secs * 1000);
  81. #else
  82. sleep(secs);
  83. #endif
  84. }
  85. void carla_msleep(const unsigned int msecs)
  86. {
  87. CARLA_SAFE_ASSERT_RETURN(msecs > 0,);
  88. #ifdef CARLA_OS_WIN
  89. Sleep(msecs);
  90. #else
  91. usleep(msecs * 1000);
  92. #endif
  93. }
  94. // -----------------------------------------------------------------------
  95. // carla_setenv
  96. void carla_setenv(const char* const key, const char* const value)
  97. {
  98. CARLA_SAFE_ASSERT_RETURN(key != nullptr,);
  99. CARLA_SAFE_ASSERT_RETURN(value != nullptr,);
  100. #ifdef CARLA_OS_WIN
  101. SetEnvironmentVariableA(key, value);
  102. #else
  103. setenv(key, value, 1);
  104. #endif
  105. }
  106. // -----------------------------------------------------------------------
  107. // carla_strdup
  108. const char* carla_strdup(const char* const strBuf)
  109. {
  110. CARLA_SAFE_ASSERT(strBuf != nullptr);
  111. const size_t bufferLen = (strBuf != nullptr) ? std::strlen(strBuf) : 0;
  112. char* const buffer = new char[bufferLen+1];
  113. if (strBuf != nullptr && bufferLen > 0)
  114. std::strncpy(buffer, strBuf, bufferLen);
  115. buffer[bufferLen] = '\0';
  116. return buffer;
  117. }
  118. const char* carla_strdup_free(char* const strBuf)
  119. {
  120. const char* const buffer(carla_strdup(strBuf));
  121. std::free(strBuf);
  122. return buffer;
  123. }
  124. #endif // CARLA_UTILS_HPP_INCLUDED
  125. // -----------------------------------------------------------------------
  126. #ifdef CARLA_LIB_UTILS_HPP_INCLUDED
  127. // -----------------------------------------------------------------------
  128. // library related calls
  129. void* lib_open(const char* const filename)
  130. {
  131. CARLA_SAFE_ASSERT_RETURN(filename != nullptr, nullptr);
  132. #ifdef CARLA_OS_WIN
  133. return (void*)LoadLibraryA(filename);
  134. #else
  135. return dlopen(filename, RTLD_NOW|RTLD_LOCAL);
  136. #endif
  137. }
  138. bool lib_close(void* const lib)
  139. {
  140. CARLA_SAFE_ASSERT_RETURN(lib != nullptr, false);
  141. #ifdef CARLA_OS_WIN
  142. return FreeLibrary((HMODULE)lib);
  143. #else
  144. return (dlclose(lib) == 0);
  145. #endif
  146. }
  147. void* lib_symbol(void* const lib, const char* const symbol)
  148. {
  149. CARLA_SAFE_ASSERT_RETURN(lib != nullptr, nullptr);
  150. CARLA_SAFE_ASSERT_RETURN(symbol != nullptr, nullptr);
  151. #ifdef CARLA_OS_WIN
  152. return (void*)GetProcAddress((HMODULE)lib, symbol);
  153. #else
  154. return dlsym(lib, symbol);
  155. #endif
  156. }
  157. const char* lib_error(const char* const filename)
  158. {
  159. CARLA_SAFE_ASSERT_RETURN(filename != nullptr, nullptr);
  160. #ifdef CARLA_OS_WIN
  161. static char libError[2048+1];
  162. carla_zeroChar(libError, 2048+1);
  163. LPVOID winErrorString;
  164. DWORD winErrorCode = GetLastError();
  165. FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER|FORMAT_MESSAGE_FROM_SYSTEM|FORMAT_MESSAGE_IGNORE_INSERTS, nullptr, winErrorCode, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPTSTR)&winErrorString, 0, nullptr);
  166. std::snprintf(libError, 2048, "%s: error code %li: %s", filename, winErrorCode, (const char*)winErrorString);
  167. LocalFree(winErrorString);
  168. return libError;
  169. #else
  170. return dlerror();
  171. #endif
  172. }
  173. #endif // CARLA_LIB_UTILS_HPP_INCLUDED
  174. // -----------------------------------------------------------------------