The JUCE cross-platform C++ framework, with DISTRHO/KXStudio specific changes
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.

186 lines
5.2KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2015 - ROLI Ltd.
  5. Permission is granted to use this software under the terms of either:
  6. a) the GPL v2 (or any later version)
  7. b) the Affero GPL v3
  8. Details of these licenses can be found at: www.gnu.org/licenses
  9. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  10. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  11. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  12. ------------------------------------------------------------------------------
  13. To release a closed-source product which uses JUCE, commercial licenses are
  14. available: visit www.juce.com for more information.
  15. ==============================================================================
  16. */
  17. #include "projucer_LiveCodeBuilderDLL.h"
  18. struct CompileEngineDLL
  19. {
  20. CompileEngineDLL()
  21. {
  22. tryLoadDll();
  23. }
  24. ~CompileEngineDLL()
  25. {
  26. shutdown();
  27. }
  28. void tryLoadDll()
  29. {
  30. // never load the dynamic lib multiple times
  31. if (! isLoaded())
  32. {
  33. File f = findDLLFile();
  34. if (f != File() && dll.open (f.getLinkedTarget().getFullPathName()))
  35. {
  36. #define INIT_LIVE_DLL_FN(name, returnType, params) name = (name##_type) dll.getFunction (#name);
  37. LIVE_DLL_FUNCTIONS (INIT_LIVE_DLL_FN);
  38. #undef INIT_LIVE_DLL_FN
  39. }
  40. }
  41. }
  42. void initialise (CrashCallbackFunction crashFn, QuitCallbackFunction quitFn, bool setupSignals)
  43. {
  44. if (isLoaded())
  45. projucer_initialise (crashFn, quitFn, setPropertyCallback, getPropertyCallback, setupSignals);
  46. }
  47. void shutdown()
  48. {
  49. if (isLoaded())
  50. projucer_shutdown();
  51. }
  52. bool isLoaded() const
  53. {
  54. #define CHECK_LIVE_DLL_FN(name, returnType, params) if (name == nullptr) return false;
  55. LIVE_DLL_FUNCTIONS (CHECK_LIVE_DLL_FN);
  56. #undef CHECK_LIVE_DLL_FN
  57. return projucer_getVersion() == requiredVersion;
  58. }
  59. #define DECLARE_LIVE_DLL_FN(name, returnType, params) \
  60. typedef returnType (*name##_type) params; \
  61. name##_type name = nullptr;
  62. LIVE_DLL_FUNCTIONS (DECLARE_LIVE_DLL_FN);
  63. #undef DECLARE_LIVE_DLL_FN
  64. static String getDLLName()
  65. {
  66. #if JUCE_MAC
  67. return "JUCECompileEngine.dylib";
  68. #elif JUCE_LINUX
  69. return "JUCECompileEngine.so";
  70. #elif JUCE_WINDOWS
  71. return "JUCECompileEngine.dll";
  72. #else
  73. #error
  74. return "JUCECompileEngine.so";
  75. #endif
  76. }
  77. static File getVersionedUserAppSupportFolder()
  78. {
  79. File userAppData (File::getSpecialLocation (File::userApplicationDataDirectory));
  80. #if JUCE_MAC
  81. userAppData = userAppData.getChildFile ("Application Support");
  82. #endif
  83. return userAppData.getChildFile (String ("Projucer-") + ProjectInfo::versionString);
  84. }
  85. private:
  86. DynamicLibrary dll;
  87. enum { requiredVersion = 1 };
  88. static File findDLLFile()
  89. {
  90. File dllFile;
  91. if (tryFindDLLFileInAppFolder(dllFile))
  92. return dllFile;
  93. #if JUCE_MAC
  94. if (tryFindDLLFileInAppBundle(dllFile))
  95. return dllFile;
  96. #endif
  97. if (tryFindDLLFileInAppConfigFolder(dllFile))
  98. return dllFile;
  99. return File();
  100. }
  101. #if JUCE_MAC
  102. static bool tryFindDLLFileInAppBundle(File &outFile)
  103. {
  104. File currentAppFile (File::getSpecialLocation (File::currentApplicationFile));
  105. return tryFindDLLFileInFolder (currentAppFile.getChildFile ("Contents"), outFile);
  106. }
  107. #endif
  108. static bool tryFindDLLFileInAppFolder(File &outFile)
  109. {
  110. File currentAppFile (File::getSpecialLocation (File::currentApplicationFile));
  111. return tryFindDLLFileInFolder (currentAppFile.getParentDirectory(), outFile);
  112. }
  113. static bool tryFindDLLFileInAppConfigFolder(File &outFile)
  114. {
  115. File userAppDataFolder (getVersionedUserAppSupportFolder());
  116. return tryFindDLLFileInFolder (userAppDataFolder, outFile);
  117. }
  118. static bool tryFindDLLFileInFolder(File folder, File& outFile)
  119. {
  120. File file = folder.getChildFile (getDLLName());
  121. if (isDLLFile (file))
  122. {
  123. outFile = file;
  124. return true;
  125. }
  126. return false;
  127. }
  128. static bool isDLLFile (const File& f)
  129. {
  130. return f.getFileName().equalsIgnoreCase (getDLLName()) && f.exists();
  131. }
  132. static void setPropertyCallback (const char* key, const char* value)
  133. {
  134. if (String (key).isNotEmpty())
  135. getGlobalProperties().setValue (key, value);
  136. else
  137. jassertfalse;
  138. }
  139. static void getPropertyCallback (const char* key, char* value, size_t size)
  140. {
  141. jassert (getGlobalProperties().getValue (key).getNumBytesAsUTF8() < size);
  142. value[0] = 0;
  143. getGlobalProperties().getValue (key).copyToUTF8 (value, size);
  144. }
  145. };