VST3: fetch midi mappings all at once, use it for note/sound-off
[carla.git] / source / utils / CarlaUtils.hpp
blob7346b51175058691e729a7a3ad4114762dd6f65f
1 /*
2 * Carla common utils
3 * Copyright (C) 2011-2023 Filipe Coelho <falktx@falktx.com>
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.
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.
15 * For a full copy of the GNU General Public License see the doc/GPL.txt file.
18 #ifndef CARLA_UTILS_HPP_INCLUDED
19 #define CARLA_UTILS_HPP_INCLUDED
21 #include "CarlaDefines.h"
23 #include <cassert>
24 #include <cstdarg>
25 #include <cstdio>
26 #include <cstdlib>
27 #include <cstring>
29 #ifdef CARLA_PROPER_CPP11_SUPPORT
30 # ifdef __GNUC__
31 # include <cxxabi.h>
32 # endif
33 # include <cstdint>
34 #else
35 # include <stdint.h>
36 #endif
38 #ifdef CARLA_OS_WIN
39 # ifndef NOMINMAX
40 # define NOMINMAX
41 # endif
42 # define WIN32_LEAN_AND_MEAN 1
43 # include <winsock2.h>
44 # include <windows.h>
45 #else
46 # include <unistd.h>
47 #endif
49 // --------------------------------------------------------------------------------------------------------------------
50 // misc functions
53 * Return "true" or "false" according to yesNo.
55 static inline
56 const char* bool2str(const bool yesNo) noexcept
58 return yesNo ? "true" : "false";
62 * Set a string as empty/null.
64 static inline
65 void nullStrBuf(char* const strBuf) noexcept
67 strBuf[0] = '\0';
71 * Dummy function.
73 static inline
74 void pass() noexcept {}
76 // --------------------------------------------------------------------------------------------------------------------
77 // string print functions
80 * Internal noexcept-safe fopen function.
82 static inline
83 FILE* __carla_fopen(const char* const filename, FILE* const fallback) noexcept
85 #ifdef CARLA_OS_LINUX
86 if (std::getenv("CARLA_CAPTURE_CONSOLE_OUTPUT") == nullptr)
87 return fallback;
89 FILE* ret = nullptr;
91 try {
92 ret = std::fopen(filename, "a+");
93 } CARLA_CATCH_UNWIND catch (...) {}
95 if (ret == nullptr)
96 ret = fallback;
98 return ret;
99 #else
100 return fallback;
101 // unused
102 (void)filename;
103 #endif
107 * Print a string to stdout with newline (gray color).
108 * Does nothing if DEBUG is not defined.
110 #ifndef DEBUG
111 # define carla_debug(...)
112 #else
113 static inline
114 void carla_debug(const char* const fmt, ...) noexcept
116 static FILE* const output = __carla_fopen("/tmp/carla.debug.log", stdout);
118 try {
119 va_list args;
120 va_start(args, fmt);
122 if (output == stdout)
124 #ifdef CARLA_OS_MAC
125 std::fprintf(output, "\x1b[37;1m[carla] ");
126 #else
127 std::fprintf(output, "\x1b[30;1m[carla] ");
128 #endif
129 std::vfprintf(output, fmt, args);
130 std::fprintf(output, "\x1b[0m\n");
132 else
134 std::fprintf(output, "[carla] ");
135 std::vfprintf(output, fmt, args);
136 std::fprintf(output, "\n");
139 std::fflush(output);
140 va_end(args);
141 } CARLA_CATCH_UNWIND catch (...) {}
143 #endif
146 * Print a string to stdout with newline.
148 static inline
149 void carla_stdout(const char* const fmt, ...) noexcept
151 static FILE* const output = __carla_fopen("/tmp/carla.stdout.log", stdout);
153 try {
154 va_list args;
155 va_start(args, fmt);
156 std::fprintf(output, "[carla] ");
157 std::vfprintf(output, fmt, args);
158 std::fprintf(output, "\n");
159 #ifndef DEBUG
160 if (output != stdout)
161 #endif
162 std::fflush(output);
163 va_end(args);
164 } CARLA_CATCH_UNWIND catch (...) {}
168 * Print a string to stderr with newline.
170 static inline
171 void carla_stderr(const char* const fmt, ...) noexcept
173 static FILE* const output = __carla_fopen("/tmp/carla.stderr.log", stderr);
175 try {
176 va_list args;
177 va_start(args, fmt);
178 std::fprintf(output, "[carla] ");
179 std::vfprintf(output, fmt, args);
180 std::fprintf(output, "\n");
181 #ifndef DEBUG
182 if (output != stderr)
183 #endif
184 std::fflush(output);
185 va_end(args);
186 } CARLA_CATCH_UNWIND catch (...) {}
190 * Print a string to stderr with newline (red color).
192 static inline
193 void carla_stderr2(const char* const fmt, ...) noexcept
195 static FILE* const output = __carla_fopen("/tmp/carla.stderr2.log", stderr);
197 try {
198 va_list args;
199 va_start(args, fmt);
201 if (output == stderr)
203 std::fprintf(output, "\x1b[31m[carla] ");
204 std::vfprintf(output, fmt, args);
205 std::fprintf(output, "\x1b[0m\n");
207 else
209 std::fprintf(output, "[carla] ");
210 std::vfprintf(output, fmt, args);
211 std::fprintf(output, "\n");
214 std::fflush(output);
215 va_end(args);
216 } CARLA_CATCH_UNWIND catch (...) {}
219 // --------------------------------------------------------------------------------------------------------------------
220 // carla_safe_assert*
223 * Print a safe assertion error message.
225 static inline
226 void carla_safe_assert(const char* const assertion, const char* const file, const int line) noexcept
228 carla_stderr2("Carla assertion failure: \"%s\" in file %s, line %i", assertion, file, line);
232 * Print a safe assertion error message, with 1 extra signed integer value.
234 static inline
235 void carla_safe_assert_int(const char* const assertion, const char* const file,
236 const int line, const int value) noexcept
238 carla_stderr2("Carla assertion failure: \"%s\" in file %s, line %i, value %i", assertion, file, line, value);
242 * Print a safe assertion error message, with 1 extra unsigned integer value.
244 static inline
245 void carla_safe_assert_uint(const char* const assertion, const char* const file,
246 const int line, const uint value) noexcept
248 carla_stderr2("Carla assertion failure: \"%s\" in file %s, line %i, value %u", assertion, file, line, value);
252 * Print a safe assertion error message, with 2 extra signed integer values.
254 static inline
255 void carla_safe_assert_int2(const char* const assertion, const char* const file,
256 const int line, const int v1, const int v2) noexcept
258 carla_stderr2("Carla assertion failure: \"%s\" in file %s, line %i, v1 %i, v2 %i", assertion, file, line, v1, v2);
262 * Print a safe assertion error message, with 2 extra unsigned integer values.
264 static inline
265 void carla_safe_assert_uint2(const char* const assertion, const char* const file,
266 const int line, const uint v1, const uint v2) noexcept
268 carla_stderr2("Carla assertion failure: \"%s\" in file %s, line %i, v1 %u, v2 %u", assertion, file, line, v1, v2);
272 * Print a safe assertion error message, with a custom error message.
274 static inline
275 void carla_custom_safe_assert(const char* const message,
276 const char* const assertion, const char* const file, const int line) noexcept
278 carla_stderr2("Carla assertion failure: %s, condition \"%s\" in file %s, line %i", message, assertion, file, line);
281 // --------------------------------------------------------------------------------------------------------------------
282 // carla_safe_exception*
285 * Print a safe exception error message.
287 static inline
288 void carla_safe_exception(const char* const exception, const char* const file, const int line) noexcept
290 carla_stderr2("Carla exception caught: \"%s\" in file %s, line %i", exception, file, line);
293 // --------------------------------------------------------------------------------------------------------------------
294 // carla_setenv
297 * Set environment variable 'key' to 'value'.
299 static inline
300 void carla_setenv(const char* const key, const char* const value) noexcept
302 CARLA_SAFE_ASSERT_RETURN(key != nullptr && key[0] != '\0',);
303 CARLA_SAFE_ASSERT_RETURN(value != nullptr,);
305 #ifdef CARLA_OS_WIN
306 try {
307 ::SetEnvironmentVariableA(key, value);
308 } CARLA_SAFE_EXCEPTION("carla_setenv");
309 #else
310 ::setenv(key, value, 1);
311 #endif
315 * Unset environment variable 'key'.
317 static inline
318 void carla_unsetenv(const char* const key) noexcept
320 CARLA_SAFE_ASSERT_RETURN(key != nullptr && key[0] != '\0',);
322 #ifdef CARLA_OS_WIN
323 try {
324 ::SetEnvironmentVariableA(key, nullptr);
325 } CARLA_SAFE_EXCEPTION("carla_unsetenv");
326 #else
327 ::unsetenv(key);
328 #endif
331 // --------------------------------------------------------------------------------------------------------------------
332 // carla_strdup
335 * Custom 'strdup' function.
336 * Returned value is always valid, and must be freed with "delete[] var".
337 * May throw.
339 static inline
340 const char* carla_strdup(const char* const strBuf)
342 CARLA_SAFE_ASSERT(strBuf != nullptr);
344 const std::size_t bufferLen = (strBuf != nullptr) ? std::strlen(strBuf) : 0;
345 char* const buffer = new char[bufferLen+1];
347 if (bufferLen > 0)
348 std::memcpy(buffer, strBuf, bufferLen);
350 buffer[bufferLen] = '\0';
352 return buffer;
356 * Custom 'strdup' function.
357 * Calls "std::free(strBuf)".
358 * Returned value is always valid, and must be freed with "delete[] var".
359 * May throw.
361 static inline
362 const char* carla_strdup_free(char* const strBuf)
364 const char* const buffer(carla_strdup(strBuf));
365 std::free(strBuf);
366 return buffer;
370 * Custom 'strdup' function, safe version.
371 * Returned value may be null. It must be freed with "delete[] var".
373 static inline
374 const char* carla_strdup_safe(const char* const strBuf) noexcept
376 CARLA_SAFE_ASSERT_RETURN(strBuf != nullptr, nullptr);
378 const std::size_t bufferLen = std::strlen(strBuf);
379 char* buffer;
381 try {
382 buffer = new char[bufferLen+1];
383 } CARLA_SAFE_EXCEPTION_RETURN("carla_strdup_safe", nullptr);
385 if (bufferLen > 0)
386 std::memcpy(buffer, strBuf, bufferLen);
388 buffer[bufferLen] = '\0';
390 return buffer;
393 // --------------------------------------------------------------------------------------------------------------------
394 // memory functions
397 * Add array values to another array.
399 template<typename T>
400 static inline
401 void carla_add(T dest[], const T src[], const std::size_t count) noexcept
403 CARLA_SAFE_ASSERT_RETURN(dest != nullptr,);
404 CARLA_SAFE_ASSERT_RETURN(src != nullptr,);
405 CARLA_SAFE_ASSERT_RETURN(dest != src,);
406 CARLA_SAFE_ASSERT_RETURN(count > 0,);
408 for (std::size_t i=0; i<count; ++i)
409 dest[i] += src[i];
413 * Add array values to another array, with a multiplication factor.
415 template<typename T>
416 static inline
417 void carla_addWithMultiply(T dest[], const T src[], const T& multiplier, const std::size_t count) noexcept
419 CARLA_SAFE_ASSERT_RETURN(dest != nullptr,);
420 CARLA_SAFE_ASSERT_RETURN(src != nullptr,);
421 CARLA_SAFE_ASSERT_RETURN(dest != src,);
422 CARLA_SAFE_ASSERT_RETURN(count > 0,);
424 for (std::size_t i=0; i<count; ++i)
425 dest[i] += src[i] * multiplier;
429 * Copy array values to another array.
431 template<typename T>
432 static inline
433 void carla_copy(T dest[], const T src[], const std::size_t count) noexcept
435 CARLA_SAFE_ASSERT_RETURN(dest != nullptr,);
436 CARLA_SAFE_ASSERT_RETURN(src != nullptr,);
437 CARLA_SAFE_ASSERT_RETURN(dest != src,);
438 CARLA_SAFE_ASSERT_RETURN(count > 0,);
440 std::memcpy(dest, src, count*sizeof(T));
444 * Copy array values to another array, with a multiplication factor.
446 template<typename T>
447 static inline
448 void carla_copyWithMultiply(T dest[], const T src[], const T& multiplier, const std::size_t count) noexcept
450 CARLA_SAFE_ASSERT_RETURN(dest != nullptr,);
451 CARLA_SAFE_ASSERT_RETURN(src != nullptr,);
452 CARLA_SAFE_ASSERT_RETURN(dest != src,);
453 CARLA_SAFE_ASSERT_RETURN(count > 0,);
455 for (std::size_t i=0; i<count; ++i)
456 dest[i] = src[i] * multiplier;
460 * Fill an array with a fixed value.
462 template<typename T>
463 static inline
464 void carla_fill(T data[], const T& value, const std::size_t count) noexcept
466 CARLA_SAFE_ASSERT_RETURN(data != nullptr,);
467 CARLA_SAFE_ASSERT_RETURN(count > 0,);
469 if (value == 0)
471 std::memset(data, 0, count*sizeof(T));
473 else
475 for (std::size_t i=0; i<count; ++i)
476 data[i] = value;
481 * Multiply an array with a fixed value.
483 template<typename T>
484 static inline
485 void carla_multiply(T data[], const T& multiplier, const std::size_t count) noexcept
487 CARLA_SAFE_ASSERT_RETURN(data != nullptr,);
488 CARLA_SAFE_ASSERT_RETURN(count > 0,);
490 if (multiplier == 0)
492 std::memset(data, 0, count*sizeof(T));
494 else
496 for (std::size_t i=0; i<count; ++i)
497 data[i] *= multiplier;
502 * Clear a byte array.
504 static inline
505 void carla_zeroBytes(uint8_t bytes[], const std::size_t count) noexcept
507 CARLA_SAFE_ASSERT_RETURN(bytes != nullptr,);
508 CARLA_SAFE_ASSERT_RETURN(count > 0,);
510 std::memset(bytes, 0, count*sizeof(uint8_t));
514 * Clear a char array.
516 static inline
517 void carla_zeroChars(char chars[], const std::size_t count) noexcept
519 CARLA_SAFE_ASSERT_RETURN(chars != nullptr,);
520 CARLA_SAFE_ASSERT_RETURN(count > 0,);
522 std::memset(chars, 0, count*sizeof(char));
526 * Clear a pointer array.
528 template<typename T>
529 static inline
530 void carla_zeroPointers(T* ptrs[], const std::size_t count) noexcept
532 CARLA_SAFE_ASSERT_RETURN(ptrs != nullptr,);
533 CARLA_SAFE_ASSERT_RETURN(count > 0,);
535 std::memset(ptrs, 0, count*sizeof(T*));
539 * Clear a single struct.
541 template <typename T>
542 static inline
543 void carla_zeroStruct(T& s) noexcept
545 std::memset(&s, 0, sizeof(T));
549 * Clear a struct array.
551 template <typename T>
552 static inline
553 void carla_zeroStructs(T structs[], const std::size_t count) noexcept
555 CARLA_SAFE_ASSERT_RETURN(structs != nullptr,);
556 CARLA_SAFE_ASSERT_RETURN(count > 0,);
558 std::memset(structs, 0, count*sizeof(T));
562 * Copy a single struct.
564 template <typename T>
565 static inline
566 void carla_copyStruct(T& dest, const T& src) noexcept
568 std::memcpy(&dest, &src, sizeof(T));
572 * Copy a struct array.
574 template <typename T>
575 static inline
576 void carla_copyStructs(T dest[], const T src[], const std::size_t count) noexcept
578 CARLA_SAFE_ASSERT_RETURN(dest != nullptr,);
579 CARLA_SAFE_ASSERT_RETURN(src != nullptr,);
580 CARLA_SAFE_ASSERT_RETURN(dest != src,);
581 CARLA_SAFE_ASSERT_RETURN(count > 0,);
583 std::memcpy(dest, src, count*sizeof(T));
586 // --------------------------------------------------------------------------------------------------------------------
588 #endif // CARLA_UTILS_HPP_INCLUDED