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.

110 lines
2.8KB

  1. /*
  2. * Cross-platform C++ library for Carla, based on Juce v4
  3. * Copyright (C) 2015 ROLI Ltd.
  4. * Copyright (C) 2017-2022 Filipe Coelho <falktx@falktx.com>
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License as
  8. * published by the Free Software Foundation; either version 2 of
  9. * the License, or any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * For a full copy of the GNU General Public License see the doc/GPL.txt file.
  17. */
  18. #include "maths/MathsFunctions.h"
  19. #include "misc/Result.h"
  20. #ifdef CARLA_OS_MAC
  21. # include "text/String.h"
  22. # import <Foundation/NSAutoreleasePool.h>
  23. # import <Foundation/NSString.h>
  24. #endif
  25. #include <cerrno>
  26. #ifndef CARLA_PROPER_CPP11_SUPPORT
  27. namespace std {
  28. using strerror;
  29. }
  30. #endif
  31. //==============================================================================
  32. namespace water
  33. {
  34. #ifdef CARLA_OS_WIN
  35. static inline
  36. Result getResultForLastError()
  37. {
  38. TCHAR messageBuffer [256] = { 0 };
  39. FormatMessage (FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
  40. nullptr, GetLastError(), MAKELANGID (LANG_NEUTRAL, SUBLANG_DEFAULT),
  41. messageBuffer, (DWORD) numElementsInArray (messageBuffer) - 1, nullptr);
  42. return Result::fail (messageBuffer);
  43. }
  44. static inline
  45. int64 water_fileSetPosition (void* handle, int64 pos)
  46. {
  47. LARGE_INTEGER li;
  48. li.QuadPart = pos;
  49. li.LowPart = SetFilePointer ((HANDLE) handle, (LONG) li.LowPart, &li.HighPart, FILE_BEGIN); // (returns -1 if it fails)
  50. return li.QuadPart;
  51. }
  52. HINSTANCE getCurrentModuleInstanceHandle() noexcept;
  53. #else
  54. static inline
  55. Result getResultForErrno()
  56. {
  57. return Result::fail (std::strerror (errno));
  58. }
  59. static inline
  60. int getFD (void* handle) noexcept { return (int) (pointer_sized_int) handle; }
  61. static inline
  62. void* fdToVoidPointer (int fd) noexcept { return (void*) (pointer_sized_int) fd; }
  63. static inline
  64. int64 water_fileSetPosition (void* handle, int64 pos)
  65. {
  66. if (handle != nullptr && lseek (getFD (handle), pos, SEEK_SET) == pos)
  67. return pos;
  68. return -1;
  69. }
  70. #endif
  71. #ifdef CARLA_OS_MAC
  72. static inline
  73. String nsStringToWater (NSString* s)
  74. {
  75. return CharPointer_UTF8 ([s UTF8String]);
  76. }
  77. static inline
  78. NSString* waterStringToNS (const String& s)
  79. {
  80. return [NSString stringWithUTF8String: s.toUTF8()];
  81. }
  82. class AutoNSAutoreleasePool {
  83. public:
  84. AutoNSAutoreleasePool() : pool([NSAutoreleasePool new]) {}
  85. ~AutoNSAutoreleasePool() { [pool drain]; }
  86. private:
  87. NSAutoreleasePool* const pool;
  88. };
  89. #endif
  90. }