VST3: fetch midi mappings all at once, use it for note/sound-off
[carla.git] / source / modules / juce_events / native / juce_osx_MessageQueue.h
blob58f39cdf953c6bc0ae369f4a56dc316dd99ffee6
1 /*
2 ==============================================================================
4 This file is part of the JUCE library.
5 Copyright (c) 2022 - Raw Material Software Limited
7 JUCE is an open source library subject to commercial or open-source
8 licensing.
10 The code included in this file is provided under the terms of the ISC license
11 http://www.isc.org/downloads/software-support-policy/isc-license. Permission
12 To use, copy, modify, and/or distribute this software for any purpose with or
13 without fee is hereby granted provided that the above copyright notice and
14 this permission notice appear in all copies.
16 JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
17 EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
18 DISCLAIMED.
20 ==============================================================================
23 namespace juce
26 //==============================================================================
27 /* An internal message pump class used in OSX and iOS. */
28 class MessageQueue
30 public:
31 MessageQueue()
33 #if JUCE_IOS
34 runLoop = CFRunLoopGetCurrent();
35 #else
36 runLoop = CFRunLoopGetMain();
37 #endif
39 CFRunLoopSourceContext sourceContext;
40 zerostruct (sourceContext); // (can't use "= { 0 }" on this object because it's typedef'ed as a C struct)
41 sourceContext.info = this;
42 sourceContext.perform = runLoopSourceCallback;
43 runLoopSource.reset (CFRunLoopSourceCreate (kCFAllocatorDefault, 1, &sourceContext));
44 CFRunLoopAddSource (runLoop, runLoopSource.get(), kCFRunLoopCommonModes);
47 ~MessageQueue() noexcept
49 CFRunLoopRemoveSource (runLoop, runLoopSource.get(), kCFRunLoopCommonModes);
50 CFRunLoopSourceInvalidate (runLoopSource.get());
53 void post (MessageManager::MessageBase* const message)
55 messages.add (message);
56 wakeUp();
59 private:
60 ReferenceCountedArray<MessageManager::MessageBase, CriticalSection> messages;
61 CFRunLoopRef runLoop;
62 CFUniquePtr<CFRunLoopSourceRef> runLoopSource;
64 void wakeUp() noexcept
66 CFRunLoopSourceSignal (runLoopSource.get());
67 CFRunLoopWakeUp (runLoop);
70 bool deliverNextMessage()
72 const MessageManager::MessageBase::Ptr nextMessage (messages.removeAndReturn (0));
74 if (nextMessage == nullptr)
75 return false;
77 JUCE_AUTORELEASEPOOL
79 JUCE_TRY
81 nextMessage->messageCallback();
83 JUCE_CATCH_EXCEPTION
86 return true;
89 void runLoopCallback() noexcept
91 for (int i = 4; --i >= 0;)
92 if (! deliverNextMessage())
93 return;
95 wakeUp();
98 static void runLoopSourceCallback (void* info) noexcept
100 static_cast<MessageQueue*> (info)->runLoopCallback();
104 } // namespace juce