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.

540 lines
21KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2022 - Raw Material Software Limited
  5. JUCE is an open source library subject to commercial or open-source
  6. licensing.
  7. The code included in this file is provided under the terms of the ISC license
  8. http://www.isc.org/downloads/software-support-policy/isc-license. Permission
  9. To use, copy, modify, and/or distribute this software for any purpose with or
  10. without fee is hereby granted provided that the above copyright notice and
  11. this permission notice appear in all copies.
  12. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  13. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  14. DISCLAIMED.
  15. ==============================================================================
  16. */
  17. namespace juce
  18. {
  19. using AppFocusChangeCallback = void (*)();
  20. AppFocusChangeCallback appFocusChangeCallback = nullptr;
  21. using CheckEventBlockedByModalComps = bool (*)(NSEvent*);
  22. CheckEventBlockedByModalComps isEventBlockedByModalComps = nullptr;
  23. using MenuTrackingChangedCallback = void (*)(bool);
  24. MenuTrackingChangedCallback menuTrackingChangedCallback = nullptr;
  25. //==============================================================================
  26. struct AppDelegateClass : public ObjCClass<NSObject>
  27. {
  28. AppDelegateClass() : ObjCClass<NSObject> ("JUCEAppDelegate_")
  29. {
  30. addMethod (@selector (applicationWillFinishLaunching:), applicationWillFinishLaunching);
  31. addMethod (@selector (applicationShouldTerminate:), applicationShouldTerminate);
  32. addMethod (@selector (applicationWillTerminate:), applicationWillTerminate);
  33. addMethod (@selector (application:openFile:), application_openFile);
  34. addMethod (@selector (application:openFiles:), application_openFiles);
  35. addMethod (@selector (applicationDidBecomeActive:), applicationDidBecomeActive);
  36. addMethod (@selector (applicationDidResignActive:), applicationDidResignActive);
  37. addMethod (@selector (applicationWillUnhide:), applicationWillUnhide);
  38. JUCE_BEGIN_IGNORE_WARNINGS_GCC_LIKE ("-Wundeclared-selector")
  39. addMethod (@selector (getUrl:withReplyEvent:), getUrl_withReplyEvent);
  40. addMethod (@selector (broadcastMessageCallback:), broadcastMessageCallback);
  41. addMethod (@selector (mainMenuTrackingBegan:), mainMenuTrackingBegan);
  42. addMethod (@selector (mainMenuTrackingEnded:), mainMenuTrackingEnded);
  43. addMethod (@selector (dummyMethod), dummyMethod);
  44. JUCE_END_IGNORE_WARNINGS_GCC_LIKE
  45. #if JUCE_PUSH_NOTIFICATIONS
  46. //==============================================================================
  47. addIvar<NSObject<NSApplicationDelegate, NSUserNotificationCenterDelegate>*> ("pushNotificationsDelegate");
  48. addMethod (@selector (applicationDidFinishLaunching:), applicationDidFinishLaunching);
  49. JUCE_BEGIN_IGNORE_WARNINGS_GCC_LIKE ("-Wundeclared-selector")
  50. addMethod (@selector (setPushNotificationsDelegate:), setPushNotificationsDelegate);
  51. JUCE_END_IGNORE_WARNINGS_GCC_LIKE
  52. addMethod (@selector (application:didRegisterForRemoteNotificationsWithDeviceToken:), registeredForRemoteNotifications);
  53. addMethod (@selector (application:didFailToRegisterForRemoteNotificationsWithError:), failedToRegisterForRemoteNotifications);
  54. addMethod (@selector (application:didReceiveRemoteNotification:), didReceiveRemoteNotification);
  55. #endif
  56. registerClass();
  57. }
  58. private:
  59. static void applicationWillFinishLaunching (id self, SEL, NSNotification*)
  60. {
  61. JUCE_BEGIN_IGNORE_WARNINGS_GCC_LIKE ("-Wundeclared-selector")
  62. [[NSAppleEventManager sharedAppleEventManager] setEventHandler: self
  63. andSelector: @selector (getUrl:withReplyEvent:)
  64. forEventClass: kInternetEventClass
  65. andEventID: kAEGetURL];
  66. JUCE_END_IGNORE_WARNINGS_GCC_LIKE
  67. }
  68. #if JUCE_PUSH_NOTIFICATIONS
  69. static void applicationDidFinishLaunching (id self, SEL, NSNotification* notification)
  70. {
  71. if (notification.userInfo != nil)
  72. {
  73. JUCE_BEGIN_IGNORE_WARNINGS_GCC_LIKE ("-Wdeprecated-declarations")
  74. // NSUserNotification is deprecated from macOS 11, but there doesn't seem to be a
  75. // replacement for NSApplicationLaunchUserNotificationKey returning a non-deprecated type
  76. NSUserNotification* userNotification = notification.userInfo[NSApplicationLaunchUserNotificationKey];
  77. JUCE_END_IGNORE_WARNINGS_GCC_LIKE
  78. if (userNotification != nil && userNotification.userInfo != nil)
  79. didReceiveRemoteNotification (self, nil, [NSApplication sharedApplication], userNotification.userInfo);
  80. }
  81. }
  82. #endif
  83. static NSApplicationTerminateReply applicationShouldTerminate (id /*self*/, SEL, NSApplication*)
  84. {
  85. if (auto* app = JUCEApplicationBase::getInstance())
  86. {
  87. app->systemRequestedQuit();
  88. if (! MessageManager::getInstance()->hasStopMessageBeenSent())
  89. return NSTerminateCancel;
  90. }
  91. return NSTerminateNow;
  92. }
  93. static void applicationWillTerminate (id /*self*/, SEL, NSNotification*)
  94. {
  95. JUCEApplicationBase::appWillTerminateByForce();
  96. }
  97. static BOOL application_openFile (id /*self*/, SEL, NSApplication*, NSString* filename)
  98. {
  99. if (auto* app = JUCEApplicationBase::getInstance())
  100. {
  101. app->anotherInstanceStarted (quotedIfContainsSpaces (filename));
  102. return YES;
  103. }
  104. return NO;
  105. }
  106. static void application_openFiles (id /*self*/, SEL, NSApplication*, NSArray* filenames)
  107. {
  108. if (auto* app = JUCEApplicationBase::getInstance())
  109. {
  110. StringArray files;
  111. for (NSString* f in filenames)
  112. files.add (quotedIfContainsSpaces (f));
  113. if (files.size() > 0)
  114. app->anotherInstanceStarted (files.joinIntoString (" "));
  115. }
  116. }
  117. static void applicationDidBecomeActive (id /*self*/, SEL, NSNotification*) { focusChanged(); }
  118. static void applicationDidResignActive (id /*self*/, SEL, NSNotification*) { focusChanged(); }
  119. static void applicationWillUnhide (id /*self*/, SEL, NSNotification*) { focusChanged(); }
  120. static void broadcastMessageCallback (id /*self*/, SEL, NSNotification* n)
  121. {
  122. NSDictionary* dict = (NSDictionary*) [n userInfo];
  123. auto messageString = nsStringToJuce ((NSString*) [dict valueForKey: nsStringLiteral ("message")]);
  124. MessageManager::getInstance()->deliverBroadcastMessage (messageString);
  125. }
  126. static void mainMenuTrackingBegan (id /*self*/, SEL, NSNotification*)
  127. {
  128. if (menuTrackingChangedCallback != nullptr)
  129. menuTrackingChangedCallback (true);
  130. }
  131. static void mainMenuTrackingEnded (id /*self*/, SEL, NSNotification*)
  132. {
  133. if (menuTrackingChangedCallback != nullptr)
  134. menuTrackingChangedCallback (false);
  135. }
  136. static void dummyMethod (id /*self*/, SEL) {} // (used as a way of running a dummy thread)
  137. static void focusChanged()
  138. {
  139. if (appFocusChangeCallback != nullptr)
  140. (*appFocusChangeCallback)();
  141. }
  142. static void getUrl_withReplyEvent (id /*self*/, SEL, NSAppleEventDescriptor* event, NSAppleEventDescriptor*)
  143. {
  144. if (auto* app = JUCEApplicationBase::getInstance())
  145. app->anotherInstanceStarted (quotedIfContainsSpaces ([[event paramDescriptorForKeyword: keyDirectObject] stringValue]));
  146. }
  147. static String quotedIfContainsSpaces (NSString* file)
  148. {
  149. String s (nsStringToJuce (file));
  150. s = s.unquoted().replace ("\"", "\\\"");
  151. if (s.containsChar (' '))
  152. s = s.quoted();
  153. return s;
  154. }
  155. #if JUCE_PUSH_NOTIFICATIONS
  156. //==============================================================================
  157. static void setPushNotificationsDelegate (id self, SEL, NSObject<NSApplicationDelegate, NSUserNotificationCenterDelegate>* delegate)
  158. {
  159. object_setInstanceVariable (self, "pushNotificationsDelegate", delegate);
  160. }
  161. static NSObject<NSApplicationDelegate, NSUserNotificationCenterDelegate>* getPushNotificationsDelegate (id self)
  162. {
  163. return getIvar<NSObject<NSApplicationDelegate, NSUserNotificationCenterDelegate>*> (self, "pushNotificationsDelegate");
  164. }
  165. static void registeredForRemoteNotifications (id self, SEL, NSApplication* application, NSData* deviceToken)
  166. {
  167. auto* delegate = getPushNotificationsDelegate (self);
  168. SEL selector = @selector (application:didRegisterForRemoteNotificationsWithDeviceToken:);
  169. if (delegate != nil && [delegate respondsToSelector: selector])
  170. {
  171. NSInvocation* invocation = [NSInvocation invocationWithMethodSignature: [delegate methodSignatureForSelector: selector]];
  172. [invocation setSelector: selector];
  173. [invocation setTarget: delegate];
  174. [invocation setArgument: &application atIndex:2];
  175. [invocation setArgument: &deviceToken atIndex:3];
  176. [invocation invoke];
  177. }
  178. }
  179. static void failedToRegisterForRemoteNotifications (id self, SEL, NSApplication* application, NSError* error)
  180. {
  181. auto* delegate = getPushNotificationsDelegate (self);
  182. SEL selector = @selector (application:didFailToRegisterForRemoteNotificationsWithError:);
  183. if (delegate != nil && [delegate respondsToSelector: selector])
  184. {
  185. NSInvocation* invocation = [NSInvocation invocationWithMethodSignature: [delegate methodSignatureForSelector: selector]];
  186. [invocation setSelector: selector];
  187. [invocation setTarget: delegate];
  188. [invocation setArgument: &application atIndex:2];
  189. [invocation setArgument: &error atIndex:3];
  190. [invocation invoke];
  191. }
  192. }
  193. static void didReceiveRemoteNotification (id self, SEL, NSApplication* application, NSDictionary* userInfo)
  194. {
  195. auto* delegate = getPushNotificationsDelegate (self);
  196. SEL selector = @selector (application:didReceiveRemoteNotification:);
  197. if (delegate != nil && [delegate respondsToSelector: selector])
  198. {
  199. NSInvocation* invocation = [NSInvocation invocationWithMethodSignature: [delegate methodSignatureForSelector: selector]];
  200. [invocation setSelector: selector];
  201. [invocation setTarget: delegate];
  202. [invocation setArgument: &application atIndex:2];
  203. [invocation setArgument: &userInfo atIndex:3];
  204. [invocation invoke];
  205. }
  206. }
  207. #endif
  208. };
  209. // This is declared at file scope, so that it's guaranteed to be
  210. // constructed before and destructed after `appDelegate` (below)
  211. static AppDelegateClass appDelegateClass;
  212. //==============================================================================
  213. struct AppDelegate
  214. {
  215. public:
  216. AppDelegate()
  217. {
  218. delegate = [appDelegateClass.createInstance() init];
  219. NSNotificationCenter* center = [NSNotificationCenter defaultCenter];
  220. JUCE_BEGIN_IGNORE_WARNINGS_GCC_LIKE ("-Wundeclared-selector")
  221. [center addObserver: delegate selector: @selector (mainMenuTrackingBegan:)
  222. name: NSMenuDidBeginTrackingNotification object: nil];
  223. [center addObserver: delegate selector: @selector (mainMenuTrackingEnded:)
  224. name: NSMenuDidEndTrackingNotification object: nil];
  225. JUCE_END_IGNORE_WARNINGS_GCC_LIKE
  226. if (JUCEApplicationBase::isStandaloneApp())
  227. {
  228. [NSApp setDelegate: delegate];
  229. JUCE_BEGIN_IGNORE_WARNINGS_GCC_LIKE ("-Wundeclared-selector")
  230. [[NSDistributedNotificationCenter defaultCenter] addObserver: delegate
  231. selector: @selector (broadcastMessageCallback:)
  232. name: getBroadcastEventName()
  233. object: nil
  234. suspensionBehavior: NSNotificationSuspensionBehaviorDeliverImmediately];
  235. JUCE_END_IGNORE_WARNINGS_GCC_LIKE
  236. }
  237. else
  238. {
  239. [center addObserver: delegate selector: @selector (applicationDidResignActive:)
  240. name: NSApplicationDidResignActiveNotification object: NSApp];
  241. [center addObserver: delegate selector: @selector (applicationDidBecomeActive:)
  242. name: NSApplicationDidBecomeActiveNotification object: NSApp];
  243. [center addObserver: delegate selector: @selector (applicationWillUnhide:)
  244. name: NSApplicationWillUnhideNotification object: NSApp];
  245. }
  246. }
  247. ~AppDelegate()
  248. {
  249. [[NSRunLoop currentRunLoop] cancelPerformSelectorsWithTarget: delegate];
  250. [[NSNotificationCenter defaultCenter] removeObserver: delegate];
  251. if (JUCEApplicationBase::isStandaloneApp())
  252. {
  253. [NSApp setDelegate: nil];
  254. [[NSDistributedNotificationCenter defaultCenter] removeObserver: delegate
  255. name: getBroadcastEventName()
  256. object: nil];
  257. }
  258. [delegate release];
  259. }
  260. static NSString* getBroadcastEventName()
  261. {
  262. return juceStringToNS ("juce_" + String::toHexString (File::getSpecialLocation (File::currentExecutableFile).hashCode64()));
  263. }
  264. MessageQueue messageQueue;
  265. id delegate;
  266. };
  267. //==============================================================================
  268. void MessageManager::runDispatchLoop()
  269. {
  270. if (quitMessagePosted.get() == 0) // check that the quit message wasn't already posted..
  271. {
  272. JUCE_AUTORELEASEPOOL
  273. {
  274. // must only be called by the message thread!
  275. jassert (isThisTheMessageThread());
  276. #if JUCE_CATCH_UNHANDLED_EXCEPTIONS
  277. @try
  278. {
  279. [NSApp run];
  280. }
  281. @catch (NSException* e)
  282. {
  283. // An AppKit exception will kill the app, but at least this provides a chance to log it.,
  284. std::runtime_error ex (std::string ("NSException: ") + [[e name] UTF8String] + ", Reason:" + [[e reason] UTF8String]);
  285. JUCEApplicationBase::sendUnhandledException (&ex, __FILE__, __LINE__);
  286. }
  287. @finally
  288. {
  289. }
  290. #else
  291. [NSApp run];
  292. #endif
  293. }
  294. }
  295. }
  296. static void shutdownNSApp()
  297. {
  298. [NSApp stop: nil];
  299. [NSEvent startPeriodicEventsAfterDelay: 0 withPeriod: 0.1];
  300. }
  301. void MessageManager::stopDispatchLoop()
  302. {
  303. if (isThisTheMessageThread())
  304. {
  305. quitMessagePosted = true;
  306. shutdownNSApp();
  307. }
  308. else
  309. {
  310. struct QuitCallback : public CallbackMessage
  311. {
  312. QuitCallback() {}
  313. void messageCallback() override { MessageManager::getInstance()->stopDispatchLoop(); }
  314. };
  315. (new QuitCallback())->post();
  316. }
  317. }
  318. #if JUCE_MODAL_LOOPS_PERMITTED
  319. bool MessageManager::runDispatchLoopUntil (int millisecondsToRunFor)
  320. {
  321. jassert (millisecondsToRunFor >= 0);
  322. jassert (isThisTheMessageThread()); // must only be called by the message thread
  323. auto endTime = Time::currentTimeMillis() + millisecondsToRunFor;
  324. while (quitMessagePosted.get() == 0)
  325. {
  326. JUCE_AUTORELEASEPOOL
  327. {
  328. auto msRemaining = endTime - Time::currentTimeMillis();
  329. if (msRemaining <= 0)
  330. break;
  331. CFRunLoopRunInMode (kCFRunLoopDefaultMode, jmin (1.0, msRemaining * 0.001), true);
  332. if (NSEvent* e = [NSApp nextEventMatchingMask: NSEventMaskAny
  333. untilDate: [NSDate dateWithTimeIntervalSinceNow: 0.001]
  334. inMode: NSDefaultRunLoopMode
  335. dequeue: YES])
  336. if (isEventBlockedByModalComps == nullptr || ! (*isEventBlockedByModalComps) (e))
  337. [NSApp sendEvent: e];
  338. }
  339. }
  340. return quitMessagePosted.get() == 0;
  341. }
  342. #endif
  343. //==============================================================================
  344. void initialiseNSApplication();
  345. void initialiseNSApplication()
  346. {
  347. JUCE_AUTORELEASEPOOL
  348. {
  349. [NSApplication sharedApplication];
  350. }
  351. }
  352. static std::unique_ptr<AppDelegate> appDelegate;
  353. void MessageManager::doPlatformSpecificInitialisation()
  354. {
  355. if (appDelegate == nil)
  356. appDelegate.reset (new AppDelegate());
  357. }
  358. void MessageManager::doPlatformSpecificShutdown()
  359. {
  360. appDelegate = nullptr;
  361. }
  362. bool MessageManager::postMessageToSystemQueue (MessageBase* message)
  363. {
  364. jassert (appDelegate != nil);
  365. appDelegate->messageQueue.post (message);
  366. return true;
  367. }
  368. void MessageManager::broadcastMessage (const String& message)
  369. {
  370. NSDictionary* info = [NSDictionary dictionaryWithObject: juceStringToNS (message)
  371. forKey: nsStringLiteral ("message")];
  372. [[NSDistributedNotificationCenter defaultCenter] postNotificationName: AppDelegate::getBroadcastEventName()
  373. object: nil
  374. userInfo: info];
  375. }
  376. // Special function used by some plugin classes to re-post carbon events
  377. void repostCurrentNSEvent();
  378. void repostCurrentNSEvent()
  379. {
  380. struct EventReposter : public CallbackMessage
  381. {
  382. EventReposter() : e ([[NSApp currentEvent] retain]) {}
  383. ~EventReposter() override { [e release]; }
  384. void messageCallback() override
  385. {
  386. [NSApp postEvent: e atStart: YES];
  387. }
  388. NSEvent* e;
  389. };
  390. (new EventReposter())->post();
  391. }
  392. //==============================================================================
  393. #if JUCE_MAC
  394. struct MountedVolumeListChangeDetector::Pimpl
  395. {
  396. Pimpl (MountedVolumeListChangeDetector& d) : owner (d)
  397. {
  398. static ObserverClass cls;
  399. delegate = [cls.createInstance() init];
  400. ObserverClass::setOwner (delegate, this);
  401. NSNotificationCenter* nc = [[NSWorkspace sharedWorkspace] notificationCenter];
  402. JUCE_BEGIN_IGNORE_WARNINGS_GCC_LIKE ("-Wundeclared-selector")
  403. [nc addObserver: delegate selector: @selector (changed:) name: NSWorkspaceDidMountNotification object: nil];
  404. [nc addObserver: delegate selector: @selector (changed:) name: NSWorkspaceDidUnmountNotification object: nil];
  405. JUCE_END_IGNORE_WARNINGS_GCC_LIKE
  406. }
  407. ~Pimpl()
  408. {
  409. [[[NSWorkspace sharedWorkspace] notificationCenter] removeObserver: delegate];
  410. [delegate release];
  411. }
  412. private:
  413. MountedVolumeListChangeDetector& owner;
  414. id delegate;
  415. struct ObserverClass : public ObjCClass<NSObject>
  416. {
  417. ObserverClass() : ObjCClass<NSObject> ("JUCEDriveObserver_")
  418. {
  419. addIvar<Pimpl*> ("owner");
  420. JUCE_BEGIN_IGNORE_WARNINGS_GCC_LIKE ("-Wundeclared-selector")
  421. addMethod (@selector (changed:), changed);
  422. JUCE_END_IGNORE_WARNINGS_GCC_LIKE
  423. addProtocol (@protocol (NSTextInput));
  424. registerClass();
  425. }
  426. static Pimpl* getOwner (id self) { return getIvar<Pimpl*> (self, "owner"); }
  427. static void setOwner (id self, Pimpl* owner) { object_setInstanceVariable (self, "owner", owner); }
  428. static void changed (id self, SEL, NSNotification*)
  429. {
  430. getOwner (self)->owner.mountedVolumeListChanged();
  431. }
  432. };
  433. };
  434. MountedVolumeListChangeDetector::MountedVolumeListChangeDetector() { pimpl.reset (new Pimpl (*this)); }
  435. MountedVolumeListChangeDetector::~MountedVolumeListChangeDetector() {}
  436. #endif
  437. } // namespace juce