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.

353 lines
13KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library - "Jules' Utility Class Extensions"
  4. Copyright 2004-10 by Raw Material Software Ltd.
  5. ------------------------------------------------------------------------------
  6. JUCE can be redistributed and/or modified under the terms of the GNU General
  7. Public License (Version 2), as published by the Free Software Foundation.
  8. A copy of the license is included in the JUCE distribution, or can be found
  9. online at www.gnu.org/licenses.
  10. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  11. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  12. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  13. ------------------------------------------------------------------------------
  14. To release a closed-source product which uses JUCE, commercial licenses are
  15. available: visit www.rawmaterialsoftware.com/juce for more information.
  16. ==============================================================================
  17. */
  18. #ifndef __JUCE_PLATFORMUTILITIES_JUCEHEADER__
  19. #define __JUCE_PLATFORMUTILITIES_JUCEHEADER__
  20. #include "../text/juce_StringArray.h"
  21. #include "../io/files/juce_File.h"
  22. //==============================================================================
  23. /**
  24. A collection of miscellaneous platform-specific utilities.
  25. */
  26. class JUCE_API PlatformUtilities
  27. {
  28. public:
  29. //==============================================================================
  30. /** Plays the operating system's default alert 'beep' sound. */
  31. static void beep();
  32. /** Tries to launch the system's default reader for a given file or URL. */
  33. static bool openDocument (const String& documentURL, const String& parameters);
  34. /** Tries to launch the system's default email app to let the user create an email.
  35. */
  36. static bool launchEmailWithAttachments (const String& targetEmailAddress,
  37. const String& emailSubject,
  38. const String& bodyText,
  39. const StringArray& filesToAttach);
  40. #if JUCE_MAC || JUCE_IOS || DOXYGEN
  41. //==============================================================================
  42. /** MAC ONLY - Turns a Core CF String into a juce one. */
  43. static const String cfStringToJuceString (CFStringRef cfString);
  44. /** MAC ONLY - Turns a juce string into a Core CF one. */
  45. static CFStringRef juceStringToCFString (const String& s);
  46. /** MAC ONLY - Turns a file path into an FSRef, returning true if it succeeds. */
  47. static bool makeFSRefFromPath (FSRef* destFSRef, const String& path);
  48. /** MAC ONLY - Turns an FSRef into a juce string path. */
  49. static const String makePathFromFSRef (FSRef* file);
  50. /** MAC ONLY - Converts any decomposed unicode characters in a string into
  51. their precomposed equivalents.
  52. */
  53. static const String convertToPrecomposedUnicode (const String& s);
  54. /** MAC ONLY - Gets the type of a file from the file's resources. */
  55. static OSType getTypeOfFile (const String& filename);
  56. /** MAC ONLY - Returns true if this file is actually a bundle. */
  57. static bool isBundle (const String& filename);
  58. /** MAC ONLY - Adds an item to the dock */
  59. static void addItemToDock (const File& file);
  60. /** MAC ONLY - Returns the current OS version number.
  61. E.g. if it's running on 10.4, this will be 4, 10.5 will return 5, etc.
  62. */
  63. static int getOSXMinorVersionNumber();
  64. #endif
  65. #if JUCE_WINDOWS || DOXYGEN
  66. //==============================================================================
  67. // Some registry helper functions:
  68. /** WIN32 ONLY - Returns a string from the registry.
  69. The path is a string for the entire path of a value in the registry,
  70. e.g. "HKEY_CURRENT_USER\Software\foo\bar"
  71. */
  72. static const String getRegistryValue (const String& regValuePath,
  73. const String& defaultValue = String::empty);
  74. /** WIN32 ONLY - Sets a registry value as a string.
  75. This will take care of creating any groups needed to get to the given
  76. registry value.
  77. */
  78. static void setRegistryValue (const String& regValuePath,
  79. const String& value);
  80. /** WIN32 ONLY - Returns true if the given value exists in the registry. */
  81. static bool registryValueExists (const String& regValuePath);
  82. /** WIN32 ONLY - Deletes a registry value. */
  83. static void deleteRegistryValue (const String& regValuePath);
  84. /** WIN32 ONLY - Deletes a registry key (which is registry-talk for 'folder'). */
  85. static void deleteRegistryKey (const String& regKeyPath);
  86. /** WIN32 ONLY - Creates a file association in the registry.
  87. This lets you set the exe that should be launched by a given file extension.
  88. @param fileExtension the file extension to associate, including the
  89. initial dot, e.g. ".txt"
  90. @param symbolicDescription a space-free short token to identify the file type
  91. @param fullDescription a human-readable description of the file type
  92. @param targetExecutable the executable that should be launched
  93. @param iconResourceNumber the icon that gets displayed for the file type will be
  94. found by looking up this resource number in the
  95. executable. Pass 0 here to not use an icon
  96. */
  97. static void registerFileAssociation (const String& fileExtension,
  98. const String& symbolicDescription,
  99. const String& fullDescription,
  100. const File& targetExecutable,
  101. int iconResourceNumber);
  102. /** WIN32 ONLY - This returns the HINSTANCE of the current module.
  103. In a normal Juce application this will be set to the module handle
  104. of the application executable.
  105. If you're writing a DLL using Juce and plan to use any Juce messaging or
  106. windows, you'll need to make sure you use the setCurrentModuleInstanceHandle()
  107. to set the correct module handle in your DllMain() function, because
  108. the win32 system relies on the correct instance handle when opening windows.
  109. */
  110. static void* JUCE_CALLTYPE getCurrentModuleInstanceHandle() throw();
  111. /** WIN32 ONLY - Sets a new module handle to be used by the library.
  112. @see getCurrentModuleInstanceHandle()
  113. */
  114. static void JUCE_CALLTYPE setCurrentModuleInstanceHandle (void* newHandle) throw();
  115. /** WIN32 ONLY - Gets the command-line params as a string.
  116. This is needed to avoid unicode problems with the argc type params.
  117. */
  118. static const String JUCE_CALLTYPE getCurrentCommandLineParams();
  119. #endif
  120. /** Clears the floating point unit's flags.
  121. Only has an effect under win32, currently.
  122. */
  123. static void fpuReset();
  124. #if JUCE_LINUX || JUCE_WINDOWS
  125. //==============================================================================
  126. /** Loads a dynamically-linked library into the process's address space.
  127. @param pathOrFilename the platform-dependent name and search path
  128. @returns a handle which can be used by getProcedureEntryPoint(), or
  129. zero if it fails.
  130. @see freeDynamicLibrary, getProcedureEntryPoint
  131. */
  132. static void* loadDynamicLibrary (const String& pathOrFilename);
  133. /** Frees a dynamically-linked library.
  134. @param libraryHandle a handle created by loadDynamicLibrary
  135. @see loadDynamicLibrary, getProcedureEntryPoint
  136. */
  137. static void freeDynamicLibrary (void* libraryHandle);
  138. /** Finds a procedure call in a dynamically-linked library.
  139. @param libraryHandle a library handle returned by loadDynamicLibrary
  140. @param procedureName the name of the procedure call to try to load
  141. @returns a pointer to the function if found, or 0 if it fails
  142. @see loadDynamicLibrary
  143. */
  144. static void* getProcedureEntryPoint (void* libraryHandle,
  145. const String& procedureName);
  146. #endif
  147. #if JUCE_LINUX || DOXYGEN
  148. //==============================================================================
  149. #endif
  150. private:
  151. PlatformUtilities();
  152. PlatformUtilities (const PlatformUtilities&);
  153. PlatformUtilities& operator= (const PlatformUtilities&);
  154. };
  155. //==============================================================================
  156. #if JUCE_MAC || JUCE_IOS
  157. /** A handy C++ wrapper that creates and deletes an NSAutoreleasePool object
  158. using RAII.
  159. */
  160. class ScopedAutoReleasePool
  161. {
  162. public:
  163. ScopedAutoReleasePool();
  164. ~ScopedAutoReleasePool();
  165. private:
  166. void* pool;
  167. ScopedAutoReleasePool (const ScopedAutoReleasePool&);
  168. ScopedAutoReleasePool& operator= (const ScopedAutoReleasePool&);
  169. };
  170. #define JUCE_AUTORELEASEPOOL const JUCE_NAMESPACE::ScopedAutoReleasePool pool;
  171. #else
  172. #define JUCE_AUTORELEASEPOOL
  173. #endif
  174. //==============================================================================
  175. #if JUCE_LINUX
  176. /** A handy class that uses XLockDisplay and XUnlockDisplay to lock the X server
  177. using an RAII approach.
  178. */
  179. class ScopedXLock
  180. {
  181. public:
  182. /** Creating a ScopedXLock object locks the X display.
  183. This uses XLockDisplay() to grab the display that Juce is using.
  184. */
  185. ScopedXLock();
  186. /** Deleting a ScopedXLock object unlocks the X display.
  187. This calls XUnlockDisplay() to release the lock.
  188. */
  189. ~ScopedXLock();
  190. };
  191. #endif
  192. //==============================================================================
  193. #if JUCE_MAC
  194. /**
  195. A wrapper class for picking up events from an Apple IR remote control device.
  196. To use it, just create a subclass of this class, implementing the buttonPressed()
  197. callback, then call start() and stop() to start or stop receiving events.
  198. */
  199. class JUCE_API AppleRemoteDevice
  200. {
  201. public:
  202. //==============================================================================
  203. AppleRemoteDevice();
  204. virtual ~AppleRemoteDevice();
  205. //==============================================================================
  206. /** The set of buttons that may be pressed.
  207. @see buttonPressed
  208. */
  209. enum ButtonType
  210. {
  211. menuButton = 0, /**< The menu button (if it's held for a short time). */
  212. playButton, /**< The play button. */
  213. plusButton, /**< The plus or volume-up button. */
  214. minusButton, /**< The minus or volume-down button. */
  215. rightButton, /**< The right button (if it's held for a short time). */
  216. leftButton, /**< The left button (if it's held for a short time). */
  217. rightButton_Long, /**< The right button (if it's held for a long time). */
  218. leftButton_Long, /**< The menu button (if it's held for a long time). */
  219. menuButton_Long, /**< The menu button (if it's held for a long time). */
  220. playButtonSleepMode,
  221. switched
  222. };
  223. //==============================================================================
  224. /** Override this method to receive the callback about a button press.
  225. The callback will happen on the application's message thread.
  226. Some buttons trigger matching up and down events, in which the isDown parameter
  227. will be true and then false. Others only send a single event when the
  228. button is pressed.
  229. */
  230. virtual void buttonPressed (const ButtonType buttonId, const bool isDown) = 0;
  231. //==============================================================================
  232. /** Starts the device running and responding to events.
  233. Returns true if it managed to open the device.
  234. @param inExclusiveMode if true, the remote will be grabbed exclusively for this app,
  235. and will not be available to any other part of the system. If
  236. false, it will be shared with other apps.
  237. @see stop
  238. */
  239. bool start (const bool inExclusiveMode);
  240. /** Stops the device running.
  241. @see start
  242. */
  243. void stop();
  244. /** Returns true if the device has been started successfully.
  245. */
  246. bool isActive() const;
  247. /** Returns the ID number of the remote, if it has sent one.
  248. */
  249. int getRemoteId() const { return remoteId; }
  250. //==============================================================================
  251. juce_UseDebuggingNewOperator
  252. /** @internal */
  253. void handleCallbackInternal();
  254. private:
  255. void* device;
  256. void* queue;
  257. int remoteId;
  258. bool open (const bool openInExclusiveMode);
  259. AppleRemoteDevice (const AppleRemoteDevice&);
  260. AppleRemoteDevice& operator= (const AppleRemoteDevice&);
  261. };
  262. #endif
  263. #endif // __JUCE_PLATFORMUTILITIES_JUCEHEADER__