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.

276 lines
8.1KB

  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. END_JUCE_NAMESPACE
  34. using namespace JUCE_NAMESPACE;
  35. @interface JuceAppDelegate : NSObject
  36. {
  37. id oldDelegate;
  38. }
  39. - (JuceAppDelegate*) init;
  40. - (void) dealloc;
  41. - (BOOL) application: (NSApplication*) theApplication openFile: (NSString*) filename;
  42. - (void) application: (NSApplication*) sender openFiles: (NSArray*) filenames;
  43. - (NSApplicationTerminateReply) applicationShouldTerminate: (NSApplication*) app;
  44. - (void) applicationDidBecomeActive: (NSNotification*) aNotification;
  45. - (void) applicationDidResignActive: (NSNotification*) aNotification;
  46. - (void) applicationWillUnhide: (NSNotification*) aNotification;
  47. - (void) customEvent: (id) data;
  48. - (void) performCallback: (id) info;
  49. @end
  50. @implementation JuceAppDelegate
  51. - (JuceAppDelegate*) init
  52. {
  53. [super init];
  54. NSNotificationCenter* center = [NSNotificationCenter defaultCenter];
  55. if (JUCEApplication::getInstance() != 0)
  56. {
  57. oldDelegate = [NSApp delegate];
  58. [NSApp setDelegate: self];
  59. }
  60. else
  61. {
  62. oldDelegate = 0;
  63. [center addObserver: self selector: @selector (applicationDidResignActive:)
  64. name: NSApplicationDidResignActiveNotification object: NSApp];
  65. [center addObserver: self selector: @selector (applicationDidBecomeActive:)
  66. name: NSApplicationDidBecomeActiveNotification object: NSApp];
  67. [center addObserver: self selector: @selector (applicationWillUnhide:)
  68. name: NSApplicationWillUnhideNotification object: NSApp];
  69. }
  70. return self;
  71. }
  72. - (void) dealloc
  73. {
  74. if (oldDelegate != 0)
  75. [NSApp setDelegate: oldDelegate];
  76. [super dealloc];
  77. }
  78. - (NSApplicationTerminateReply) applicationShouldTerminate: (NSApplication*) app
  79. {
  80. if (JUCEApplication::getInstance() != 0)
  81. {
  82. JUCEApplication::getInstance()->systemRequestedQuit();
  83. if (! MessageManager::getInstance()->hasStopMessageBeenSent())
  84. return NSTerminateCancel;
  85. }
  86. return NSTerminateNow;
  87. }
  88. - (BOOL) application: (NSApplication*) app openFile: (NSString*) filename
  89. {
  90. if (JUCEApplication::getInstance() != 0)
  91. {
  92. JUCEApplication::getInstance()->anotherInstanceStarted (nsStringToJuce (filename));
  93. return YES;
  94. }
  95. return NO;
  96. }
  97. - (void) application: (NSApplication*) sender openFiles: (NSArray*) filenames
  98. {
  99. StringArray files;
  100. for (int i = 0; i < [filenames count]; ++i)
  101. files.add (nsStringToJuce ((NSString*) [filenames objectAtIndex: i]));
  102. if (files.size() > 0 && JUCEApplication::getInstance() != 0)
  103. JUCEApplication::getInstance()->anotherInstanceStarted (files.joinIntoString (T(" ")));
  104. }
  105. - (void) applicationDidBecomeActive: (NSNotification*) aNotification
  106. {
  107. juce_HandleProcessFocusChange();
  108. }
  109. - (void) applicationDidResignActive: (NSNotification*) aNotification
  110. {
  111. juce_HandleProcessFocusChange();
  112. }
  113. - (void) applicationWillUnhide: (NSNotification*) aNotification
  114. {
  115. juce_HandleProcessFocusChange();
  116. }
  117. - (void) customEvent: (id) n
  118. {
  119. NSData* data = (NSData*) n;
  120. void* message = 0;
  121. [data getBytes: &message length: sizeof (message)];
  122. if (message != 0)
  123. MessageManager::getInstance()->deliverMessage (message);
  124. [data release];
  125. }
  126. - (void) performCallback: (id) info
  127. {
  128. CallbackMessagePayload* pl = (CallbackMessagePayload*) info;
  129. if (pl != 0)
  130. {
  131. pl->result = (*pl->function) (pl->parameter);
  132. pl->hasBeenExecuted = true;
  133. }
  134. }
  135. @end
  136. BEGIN_JUCE_NAMESPACE
  137. static JuceAppDelegate* juceAppDelegate = 0;
  138. void MessageManager::runDispatchLoop()
  139. {
  140. const ScopedAutoReleasePool pool;
  141. MessageManagerLock mml;
  142. // must only be called by the message thread!
  143. jassert (isThisTheMessageThread());
  144. [NSApp run];
  145. }
  146. void MessageManager::stopDispatchLoop()
  147. {
  148. quitMessagePosted = true;
  149. [NSApp stop: nil];
  150. }
  151. bool MessageManager::runDispatchLoopUntil (int millisecondsToRunFor)
  152. {
  153. const ScopedAutoReleasePool pool;
  154. jassert (isThisTheMessageThread()); // must only be called by the message thread
  155. uint32 endTime = Time::getMillisecondCounter() + millisecondsToRunFor;
  156. NSDate* endDate = [NSDate dateWithTimeIntervalSinceNow: millisecondsToRunFor * 0.001];
  157. while (Time::getMillisecondCounter() < endTime)
  158. {
  159. const ScopedAutoReleasePool pool;
  160. [[NSRunLoop currentRunLoop] runMode: NSDefaultRunLoopMode
  161. beforeDate: endDate];
  162. NSEvent* e = [NSApp nextEventMatchingMask: NSAnyEventMask
  163. untilDate: endDate
  164. inMode: NSDefaultRunLoopMode
  165. dequeue: YES];
  166. [NSApp sendEvent: e];
  167. }
  168. return ! quitMessagePosted;
  169. }
  170. //==============================================================================
  171. void MessageManager::doPlatformSpecificInitialisation()
  172. {
  173. if (juceAppDelegate == 0)
  174. juceAppDelegate = [[JuceAppDelegate alloc] init];
  175. initialiseMainMenu();
  176. }
  177. void MessageManager::doPlatformSpecificShutdown()
  178. {
  179. [[NSRunLoop currentRunLoop] cancelPerformSelectorsWithTarget: juceAppDelegate];
  180. [[NSNotificationCenter defaultCenter] removeObserver: juceAppDelegate];
  181. [juceAppDelegate release];
  182. juceAppDelegate = 0;
  183. }
  184. bool juce_postMessageToSystemQueue (void* message)
  185. {
  186. [juceAppDelegate performSelectorOnMainThread: @selector (customEvent:)
  187. withObject: (id) [[NSData alloc] initWithBytes: &message
  188. length: (int) sizeof (message)]
  189. waitUntilDone: NO];
  190. return true;
  191. }
  192. void MessageManager::broadcastMessage (const String& value) throw()
  193. {
  194. }
  195. void* MessageManager::callFunctionOnMessageThread (MessageCallbackFunction* callback,
  196. void* data)
  197. {
  198. if (isThisTheMessageThread())
  199. {
  200. return (*callback) (data);
  201. }
  202. else
  203. {
  204. CallbackMessagePayload cmp;
  205. cmp.function = callback;
  206. cmp.parameter = data;
  207. cmp.result = 0;
  208. cmp.hasBeenExecuted = false;
  209. [juceAppDelegate performSelectorOnMainThread: @selector (performCallback:)
  210. withObject: (id) &cmp
  211. waitUntilDone: YES];
  212. return cmp.result;
  213. }
  214. }
  215. #endif