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.

342 lines
9.9KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library - "Jules' Utility Class Extensions"
  4. Copyright 2004-7 by Raw Material Software ltd.
  5. ------------------------------------------------------------------------------
  6. JUCE can be redistributed and/or modified under the terms of the
  7. GNU General Public License, as published by the Free Software Foundation;
  8. either version 2 of the License, or (at your option) any later version.
  9. JUCE is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with JUCE; if not, visit www.gnu.org/licenses or write to the
  15. Free Software Foundation, Inc., 59 Temple Place, Suite 330,
  16. Boston, MA 02111-1307 USA
  17. ------------------------------------------------------------------------------
  18. If you'd like to release a closed-source product which uses JUCE, commercial
  19. licenses are also available: visit www.rawmaterialsoftware.com/juce for
  20. more information.
  21. ==============================================================================
  22. */
  23. // (This file gets included by juce_mac_NativeCode.mm, rather than being
  24. // compiled on its own).
  25. #ifdef JUCE_INCLUDED_FILE
  26. struct CallbackMessagePayload
  27. {
  28. MessageCallbackFunction* function;
  29. void* parameter;
  30. void* volatile result;
  31. bool volatile hasBeenExecuted;
  32. };
  33. /* When you use multiple DLLs which share similarly-named obj-c classes - like
  34. for example having more than one juce plugin loaded into a host, then when a
  35. method is called, the actual code that runs might actually be in a different module
  36. than the one you expect... So any calls to library functions or statics that are
  37. made inside obj-c methods will probably end up getting executed in a different DLL's
  38. memory space. Not a great thing to happen - this obviously leads to bizarre crashes.
  39. To work around this insanity, I'm only allowing obj-c methods to make calls to
  40. virtual methods of an object that's known to live inside the right module's space.
  41. */
  42. class AppDelegateRedirector
  43. {
  44. public:
  45. AppDelegateRedirector() {}
  46. virtual ~AppDelegateRedirector() {}
  47. virtual NSApplicationTerminateReply shouldTerminate()
  48. {
  49. if (JUCEApplication::getInstance() != 0)
  50. {
  51. JUCEApplication::getInstance()->systemRequestedQuit();
  52. if (! MessageManager::getInstance()->hasStopMessageBeenSent())
  53. return NSTerminateCancel;
  54. }
  55. return NSTerminateNow;
  56. }
  57. virtual BOOL openFile (const NSString* filename)
  58. {
  59. if (JUCEApplication::getInstance() != 0)
  60. {
  61. JUCEApplication::getInstance()->anotherInstanceStarted (nsStringToJuce (filename));
  62. return YES;
  63. }
  64. return NO;
  65. }
  66. virtual void openFiles (NSArray* filenames)
  67. {
  68. StringArray files;
  69. for (int i = 0; i < [filenames count]; ++i)
  70. files.add (nsStringToJuce ((NSString*) [filenames objectAtIndex: i]));
  71. if (files.size() > 0 && JUCEApplication::getInstance() != 0)
  72. JUCEApplication::getInstance()->anotherInstanceStarted (files.joinIntoString (T(" ")));
  73. }
  74. virtual void focusChanged()
  75. {
  76. juce_HandleProcessFocusChange();
  77. }
  78. virtual void deliverMessage (void* message)
  79. {
  80. MessageManager::getInstance()->deliverMessage (message);
  81. }
  82. virtual void deleteSelf()
  83. {
  84. delete this;
  85. }
  86. };
  87. END_JUCE_NAMESPACE
  88. using namespace JUCE_NAMESPACE;
  89. typedef void (*juce_HandleProcessFocusChangeFunction)();
  90. #define JuceAppDelegate MakeObjCClassName(JuceAppDelegate)
  91. @interface JuceAppDelegate : NSObject
  92. {
  93. @private
  94. id oldDelegate;
  95. AppDelegateRedirector* redirector;
  96. }
  97. - (JuceAppDelegate*) init;
  98. - (void) dealloc;
  99. - (BOOL) application: (NSApplication*) theApplication openFile: (NSString*) filename;
  100. - (void) application: (NSApplication*) sender openFiles: (NSArray*) filenames;
  101. - (NSApplicationTerminateReply) applicationShouldTerminate: (NSApplication*) app;
  102. - (void) applicationDidBecomeActive: (NSNotification*) aNotification;
  103. - (void) applicationDidResignActive: (NSNotification*) aNotification;
  104. - (void) applicationWillUnhide: (NSNotification*) aNotification;
  105. - (void) customEvent: (id) data;
  106. - (void) performCallback: (id) info;
  107. - (void) dummyMethod;
  108. @end
  109. @implementation JuceAppDelegate
  110. - (JuceAppDelegate*) init
  111. {
  112. [super init];
  113. redirector = new AppDelegateRedirector();
  114. NSNotificationCenter* center = [NSNotificationCenter defaultCenter];
  115. if (JUCEApplication::getInstance() != 0)
  116. {
  117. oldDelegate = [NSApp delegate];
  118. [NSApp setDelegate: self];
  119. }
  120. else
  121. {
  122. oldDelegate = 0;
  123. [center addObserver: self selector: @selector (applicationDidResignActive:)
  124. name: NSApplicationDidResignActiveNotification object: NSApp];
  125. [center addObserver: self selector: @selector (applicationDidBecomeActive:)
  126. name: NSApplicationDidBecomeActiveNotification object: NSApp];
  127. [center addObserver: self selector: @selector (applicationWillUnhide:)
  128. name: NSApplicationWillUnhideNotification object: NSApp];
  129. }
  130. return self;
  131. }
  132. - (void) dealloc
  133. {
  134. if (oldDelegate != 0)
  135. [NSApp setDelegate: oldDelegate];
  136. redirector->deleteSelf();
  137. [super dealloc];
  138. }
  139. - (NSApplicationTerminateReply) applicationShouldTerminate: (NSApplication*) app
  140. {
  141. return redirector->shouldTerminate();
  142. }
  143. - (BOOL) application: (NSApplication*) app openFile: (NSString*) filename
  144. {
  145. return redirector->openFile (filename);
  146. }
  147. - (void) application: (NSApplication*) sender openFiles: (NSArray*) filenames
  148. {
  149. return redirector->openFiles (filenames);
  150. }
  151. - (void) applicationDidBecomeActive: (NSNotification*) aNotification
  152. {
  153. redirector->focusChanged();
  154. }
  155. - (void) applicationDidResignActive: (NSNotification*) aNotification
  156. {
  157. redirector->focusChanged();
  158. }
  159. - (void) applicationWillUnhide: (NSNotification*) aNotification
  160. {
  161. redirector->focusChanged();
  162. }
  163. - (void) customEvent: (id) n
  164. {
  165. NSData* data = (NSData*) n;
  166. void* message = 0;
  167. [data getBytes: &message length: sizeof (message)];
  168. if (message != 0)
  169. redirector->deliverMessage (message);
  170. [data release];
  171. }
  172. - (void) performCallback: (id) info
  173. {
  174. CallbackMessagePayload* pl = (CallbackMessagePayload*) info;
  175. if (pl != 0)
  176. {
  177. pl->result = (*pl->function) (pl->parameter);
  178. pl->hasBeenExecuted = true;
  179. }
  180. }
  181. - (void) dummyMethod {} // (used as a way of running a dummy thread)
  182. @end
  183. BEGIN_JUCE_NAMESPACE
  184. static JuceAppDelegate* juceAppDelegate = 0;
  185. void MessageManager::runDispatchLoop()
  186. {
  187. const ScopedAutoReleasePool pool;
  188. MessageManagerLock mml;
  189. // must only be called by the message thread!
  190. jassert (isThisTheMessageThread());
  191. [NSApp run];
  192. }
  193. void MessageManager::stopDispatchLoop()
  194. {
  195. quitMessagePosted = true;
  196. [NSApp stop: nil];
  197. }
  198. bool MessageManager::runDispatchLoopUntil (int millisecondsToRunFor)
  199. {
  200. const ScopedAutoReleasePool pool;
  201. jassert (isThisTheMessageThread()); // must only be called by the message thread
  202. uint32 endTime = Time::getMillisecondCounter() + millisecondsToRunFor;
  203. NSDate* endDate = [NSDate dateWithTimeIntervalSinceNow: millisecondsToRunFor * 0.001];
  204. while (Time::getMillisecondCounter() < endTime)
  205. {
  206. const ScopedAutoReleasePool pool;
  207. [[NSRunLoop currentRunLoop] runMode: NSDefaultRunLoopMode
  208. beforeDate: endDate];
  209. NSEvent* e = [NSApp nextEventMatchingMask: NSAnyEventMask
  210. untilDate: endDate
  211. inMode: NSDefaultRunLoopMode
  212. dequeue: YES];
  213. [NSApp sendEvent: e];
  214. }
  215. return ! quitMessagePosted;
  216. }
  217. //==============================================================================
  218. void MessageManager::doPlatformSpecificInitialisation()
  219. {
  220. if (juceAppDelegate == 0)
  221. juceAppDelegate = [[JuceAppDelegate alloc] init];
  222. // This launches a dummy thread, which forces Cocoa to initialise NSThreads
  223. // correctly (needed prior to 10.5)
  224. if (! [NSThread isMultiThreaded])
  225. [NSThread detachNewThreadSelector: @selector (dummyMethod)
  226. toTarget: juceAppDelegate
  227. withObject: nil];
  228. initialiseMainMenu();
  229. }
  230. void MessageManager::doPlatformSpecificShutdown()
  231. {
  232. [[NSRunLoop currentRunLoop] cancelPerformSelectorsWithTarget: juceAppDelegate];
  233. [[NSNotificationCenter defaultCenter] removeObserver: juceAppDelegate];
  234. [juceAppDelegate release];
  235. juceAppDelegate = 0;
  236. }
  237. bool juce_postMessageToSystemQueue (void* message)
  238. {
  239. [juceAppDelegate performSelectorOnMainThread: @selector (customEvent:)
  240. withObject: (id) [[NSData alloc] initWithBytes: &message
  241. length: (int) sizeof (message)]
  242. waitUntilDone: NO];
  243. return true;
  244. }
  245. void MessageManager::broadcastMessage (const String& value) throw()
  246. {
  247. }
  248. void* MessageManager::callFunctionOnMessageThread (MessageCallbackFunction* callback,
  249. void* data)
  250. {
  251. if (isThisTheMessageThread())
  252. {
  253. return (*callback) (data);
  254. }
  255. else
  256. {
  257. CallbackMessagePayload cmp;
  258. cmp.function = callback;
  259. cmp.parameter = data;
  260. cmp.result = 0;
  261. cmp.hasBeenExecuted = false;
  262. [juceAppDelegate performSelectorOnMainThread: @selector (performCallback:)
  263. withObject: (id) &cmp
  264. waitUntilDone: YES];
  265. return cmp.result;
  266. }
  267. }
  268. #endif