migrating to the latest JUCE version

This commit is contained in:
2022-11-04 23:11:33 +01:00
committed by Nikolai Rodionov
parent 4257a0f8ba
commit faf8f18333
2796 changed files with 888518 additions and 784244 deletions

View File

@ -1,54 +1,63 @@
/*
==============================================================================
This file is part of the JUCE library.
Copyright (c) 2020 - Raw Material Software Limited
JUCE is an open source library subject to commercial or open-source
licensing.
The code included in this file is provided under the terms of the ISC license
http://www.isc.org/downloads/software-support-policy/isc-license. Permission
To use, copy, modify, and/or distribute this software for any purpose with or
without fee is hereby granted provided that the above copyright notice and
this permission notice appear in all copies.
JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
DISCLAIMED.
==============================================================================
*/
namespace juce
{
#if JUCE_MAC
class ScopedLowPowerModeDisabler::Pimpl
{
public:
Pimpl() = default;
~Pimpl() { [[NSProcessInfo processInfo] endActivity: activity]; }
private:
id activity { [[NSProcessInfo processInfo] beginActivityWithOptions: NSActivityUserInitiatedAllowingIdleSystemSleep
reason: @"App must remain in high-power mode"] };
JUCE_DECLARE_NON_COPYABLE (Pimpl)
JUCE_DECLARE_NON_MOVEABLE (Pimpl)
};
#else
class ScopedLowPowerModeDisabler::Pimpl {};
#endif
//==============================================================================
ScopedLowPowerModeDisabler::ScopedLowPowerModeDisabler()
: pimpl (std::make_unique<Pimpl>()) {}
ScopedLowPowerModeDisabler::~ScopedLowPowerModeDisabler() = default;
} // namespace juce
/*
==============================================================================
This file is part of the JUCE library.
Copyright (c) 2022 - Raw Material Software Limited
JUCE is an open source library subject to commercial or open-source
licensing.
The code included in this file is provided under the terms of the ISC license
http://www.isc.org/downloads/software-support-policy/isc-license. Permission
To use, copy, modify, and/or distribute this software for any purpose with or
without fee is hereby granted provided that the above copyright notice and
this permission notice appear in all copies.
JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
DISCLAIMED.
==============================================================================
*/
namespace juce
{
#if JUCE_MAC
class ScopedLowPowerModeDisabler::Pimpl
{
public:
Pimpl()
{
if (@available (macOS 10.9, *))
activity = [[NSProcessInfo processInfo] beginActivityWithOptions: NSActivityUserInitiatedAllowingIdleSystemSleep
reason: @"App must remain in high-power mode"];
}
~Pimpl()
{
if (@available (macOS 10.9, *))
[[NSProcessInfo processInfo] endActivity: activity];
}
private:
id activity;
JUCE_DECLARE_NON_COPYABLE (Pimpl)
JUCE_DECLARE_NON_MOVEABLE (Pimpl)
};
#else
class ScopedLowPowerModeDisabler::Pimpl {};
#endif
//==============================================================================
ScopedLowPowerModeDisabler::ScopedLowPowerModeDisabler()
: pimpl (std::make_unique<Pimpl>()) {}
ScopedLowPowerModeDisabler::~ScopedLowPowerModeDisabler() = default;
} // namespace juce

View File

@ -1,49 +1,49 @@
/*
==============================================================================
This file is part of the JUCE library.
Copyright (c) 2020 - Raw Material Software Limited
JUCE is an open source library subject to commercial or open-source
licensing.
The code included in this file is provided under the terms of the ISC license
http://www.isc.org/downloads/software-support-policy/isc-license. Permission
To use, copy, modify, and/or distribute this software for any purpose with or
without fee is hereby granted provided that the above copyright notice and
this permission notice appear in all copies.
JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
DISCLAIMED.
==============================================================================
*/
namespace juce
{
//==============================================================================
/**
Disables low-power-mode for the duration of an instance's lifetime.
Currently this is only implemented on macOS, where it will disable the
"App Nap" power-saving feature.
@tags{Events}
*/
class ScopedLowPowerModeDisabler
{
public:
ScopedLowPowerModeDisabler();
~ScopedLowPowerModeDisabler();
private:
class Pimpl;
std::unique_ptr<Pimpl> pimpl;
JUCE_DECLARE_NON_COPYABLE (ScopedLowPowerModeDisabler)
JUCE_DECLARE_NON_MOVEABLE (ScopedLowPowerModeDisabler)
};
} // namespace juce
/*
==============================================================================
This file is part of the JUCE library.
Copyright (c) 2022 - Raw Material Software Limited
JUCE is an open source library subject to commercial or open-source
licensing.
The code included in this file is provided under the terms of the ISC license
http://www.isc.org/downloads/software-support-policy/isc-license. Permission
To use, copy, modify, and/or distribute this software for any purpose with or
without fee is hereby granted provided that the above copyright notice and
this permission notice appear in all copies.
JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
DISCLAIMED.
==============================================================================
*/
namespace juce
{
//==============================================================================
/**
Disables low-power-mode for the duration of an instance's lifetime.
Currently this is only implemented on macOS, where it will disable the
"App Nap" power-saving feature.
@tags{Events}
*/
class ScopedLowPowerModeDisabler
{
public:
ScopedLowPowerModeDisabler();
~ScopedLowPowerModeDisabler();
private:
class Pimpl;
std::unique_ptr<Pimpl> pimpl;
JUCE_DECLARE_NON_COPYABLE (ScopedLowPowerModeDisabler)
JUCE_DECLARE_NON_MOVEABLE (ScopedLowPowerModeDisabler)
};
} // namespace juce

View File

@ -1,301 +1,302 @@
/*
==============================================================================
This file is part of the JUCE library.
Copyright (c) 2020 - Raw Material Software Limited
JUCE is an open source library subject to commercial or open-source
licensing.
The code included in this file is provided under the terms of the ISC license
http://www.isc.org/downloads/software-support-policy/isc-license. Permission
To use, copy, modify, and/or distribute this software for any purpose with or
without fee is hereby granted provided that the above copyright notice and
this permission notice appear in all copies.
JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
DISCLAIMED.
==============================================================================
*/
namespace juce
{
//==============================================================================
namespace Android
{
class Runnable : public juce::AndroidInterfaceImplementer
{
public:
virtual void run() = 0;
private:
jobject invoke (jobject proxy, jobject method, jobjectArray args) override
{
auto* env = getEnv();
auto methodName = juce::juceString ((jstring) env->CallObjectMethod (method, JavaMethod.getName));
if (methodName == "run")
{
run();
return nullptr;
}
// invoke base class
return AndroidInterfaceImplementer::invoke (proxy, method, args);
}
};
struct Handler
{
Handler() : nativeHandler (LocalRef<jobject> (getEnv()->NewObject (AndroidHandler, AndroidHandler.constructor))) {}
~Handler() { clearSingletonInstance(); }
JUCE_DECLARE_SINGLETON (Handler, false)
bool post (jobject runnable)
{
return (getEnv()->CallBooleanMethod (nativeHandler.get(), AndroidHandler.post, runnable) != 0);
}
GlobalRef nativeHandler;
};
JUCE_IMPLEMENT_SINGLETON (Handler)
}
//==============================================================================
struct AndroidMessageQueue : private Android::Runnable
{
JUCE_DECLARE_SINGLETON_SINGLETHREADED (AndroidMessageQueue, true)
AndroidMessageQueue()
: self (CreateJavaInterface (this, "java/lang/Runnable"))
{
}
~AndroidMessageQueue() override
{
JUCE_ASSERT_MESSAGE_THREAD
clearSingletonInstance();
}
bool post (MessageManager::MessageBase::Ptr&& message)
{
queue.add (std::move (message));
// this will call us on the message thread
return handler.post (self.get());
}
private:
void run() override
{
for (;;)
{
MessageManager::MessageBase::Ptr message (queue.removeAndReturn (0));
if (message == nullptr)
break;
message->messageCallback();
}
}
// the this pointer to this class in Java land
GlobalRef self;
ReferenceCountedArray<MessageManager::MessageBase, CriticalSection> queue;
Android::Handler handler;
};
JUCE_IMPLEMENT_SINGLETON (AndroidMessageQueue)
//==============================================================================
void MessageManager::doPlatformSpecificInitialisation() { AndroidMessageQueue::getInstance(); }
void MessageManager::doPlatformSpecificShutdown() { AndroidMessageQueue::deleteInstance(); }
bool MessageManager::postMessageToSystemQueue (MessageManager::MessageBase* const message)
{
return AndroidMessageQueue::getInstance()->post (message);
}
//==============================================================================
void MessageManager::broadcastMessage (const String&)
{
}
void MessageManager::runDispatchLoop()
{
}
void MessageManager::stopDispatchLoop()
{
struct QuitCallback : public CallbackMessage
{
QuitCallback() {}
void messageCallback() override
{
auto* env = getEnv();
LocalRef<jobject> activity (getCurrentActivity());
if (activity != nullptr)
{
jmethodID quitMethod = env->GetMethodID (AndroidActivity, "finishAndRemoveTask", "()V");
if (quitMethod != nullptr)
{
env->CallVoidMethod (activity.get(), quitMethod);
return;
}
quitMethod = env->GetMethodID (AndroidActivity, "finish", "()V");
jassert (quitMethod != nullptr);
env->CallVoidMethod (activity.get(), quitMethod);
}
else
{
jassertfalse;
}
}
};
(new QuitCallback())->post();
quitMessagePosted = true;
}
//==============================================================================
class JuceAppLifecycle : public ActivityLifecycleCallbacks
{
public:
JuceAppLifecycle (juce::JUCEApplicationBase* (*initSymbolAddr)())
: createApplicationSymbol (initSymbolAddr)
{
LocalRef<jobject> appContext (getAppContext());
if (appContext != nullptr)
{
auto* env = getEnv();
myself = GlobalRef (CreateJavaInterface (this, "android/app/Application$ActivityLifecycleCallbacks"));
env->CallVoidMethod (appContext.get(), AndroidApplication.registerActivityLifecycleCallbacks, myself.get());
}
}
~JuceAppLifecycle() override
{
LocalRef<jobject> appContext (getAppContext());
if (appContext != nullptr && myself != nullptr)
{
auto* env = getEnv();
clear();
env->CallVoidMethod (appContext.get(), AndroidApplication.unregisterActivityLifecycleCallbacks, myself.get());
myself.clear();
}
}
void onActivityCreated (jobject, jobject) override
{
checkCreated();
}
void onActivityDestroyed (jobject activity) override
{
auto* env = getEnv();
// if the main activity is being destroyed, only then tear-down JUCE
if (env->IsSameObject (getMainActivity().get(), activity) != 0)
{
JUCEApplicationBase::appWillTerminateByForce();
JNIClassBase::releaseAllClasses (env);
jclass systemClass = (jclass) env->FindClass ("java/lang/System");
jmethodID exitMethod = env->GetStaticMethodID (systemClass, "exit", "(I)V");
env->CallStaticVoidMethod (systemClass, exitMethod, 0);
}
}
void onActivityStarted (jobject) override
{
checkCreated();
}
void onActivityPaused (jobject) override
{
if (auto* app = JUCEApplicationBase::getInstance())
app->suspended();
}
void onActivityResumed (jobject) override
{
checkInitialised();
if (auto* app = JUCEApplicationBase::getInstance())
app->resumed();
}
static JuceAppLifecycle& getInstance (juce::JUCEApplicationBase* (*initSymbolAddr)())
{
static JuceAppLifecycle juceAppLifecycle (initSymbolAddr);
return juceAppLifecycle;
}
private:
void checkCreated()
{
if (JUCEApplicationBase::getInstance() == nullptr)
{
DBG (SystemStats::getJUCEVersion());
JUCEApplicationBase::createInstance = createApplicationSymbol;
initialiseJuce_GUI();
if (! JUCEApplicationBase::createInstance())
jassertfalse; // you must supply an application object for an android app!
jassert (MessageManager::getInstance()->isThisTheMessageThread());
}
}
void checkInitialised()
{
checkCreated();
if (! hasBeenInitialised)
{
if (auto* app = JUCEApplicationBase::getInstance())
{
hasBeenInitialised = app->initialiseApp();
if (! hasBeenInitialised)
exit (app->shutdownApp());
}
}
}
GlobalRef myself;
juce::JUCEApplicationBase* (*createApplicationSymbol)();
bool hasBeenInitialised = false;
};
//==============================================================================
File juce_getExecutableFile();
void juce_juceEventsAndroidStartApp()
{
auto dllPath = juce_getExecutableFile().getFullPathName();
auto addr = reinterpret_cast<juce::JUCEApplicationBase*(*)()> (DynamicLibrary (dllPath)
.getFunction ("juce_CreateApplication"));
if (addr != nullptr)
JuceAppLifecycle::getInstance (addr);
}
} // namespace juce
/*
==============================================================================
This file is part of the JUCE library.
Copyright (c) 2022 - Raw Material Software Limited
JUCE is an open source library subject to commercial or open-source
licensing.
The code included in this file is provided under the terms of the ISC license
http://www.isc.org/downloads/software-support-policy/isc-license. Permission
To use, copy, modify, and/or distribute this software for any purpose with or
without fee is hereby granted provided that the above copyright notice and
this permission notice appear in all copies.
JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
DISCLAIMED.
==============================================================================
*/
namespace juce
{
//==============================================================================
namespace Android
{
class Runnable : public juce::AndroidInterfaceImplementer
{
public:
virtual void run() = 0;
private:
jobject invoke (jobject proxy, jobject method, jobjectArray args) override
{
auto* env = getEnv();
auto methodName = juce::juceString ((jstring) env->CallObjectMethod (method, JavaMethod.getName));
if (methodName == "run")
{
run();
return nullptr;
}
// invoke base class
return AndroidInterfaceImplementer::invoke (proxy, method, args);
}
};
struct Handler
{
Handler() : nativeHandler (LocalRef<jobject> (getEnv()->NewObject (AndroidHandler, AndroidHandler.constructor))) {}
~Handler() { clearSingletonInstance(); }
JUCE_DECLARE_SINGLETON (Handler, false)
bool post (jobject runnable)
{
return (getEnv()->CallBooleanMethod (nativeHandler.get(), AndroidHandler.post, runnable) != 0);
}
GlobalRef nativeHandler;
};
JUCE_IMPLEMENT_SINGLETON (Handler)
}
//==============================================================================
struct AndroidMessageQueue : private Android::Runnable
{
JUCE_DECLARE_SINGLETON_SINGLETHREADED (AndroidMessageQueue, true)
AndroidMessageQueue()
: self (CreateJavaInterface (this, "java/lang/Runnable"))
{
}
~AndroidMessageQueue() override
{
JUCE_ASSERT_MESSAGE_THREAD
clearSingletonInstance();
}
bool post (MessageManager::MessageBase::Ptr&& message)
{
queue.add (std::move (message));
// this will call us on the message thread
return handler.post (self.get());
}
private:
void run() override
{
for (;;)
{
MessageManager::MessageBase::Ptr message (queue.removeAndReturn (0));
if (message == nullptr)
break;
message->messageCallback();
}
}
// the this pointer to this class in Java land
GlobalRef self;
ReferenceCountedArray<MessageManager::MessageBase, CriticalSection> queue;
Android::Handler handler;
};
JUCE_IMPLEMENT_SINGLETON (AndroidMessageQueue)
//==============================================================================
void MessageManager::doPlatformSpecificInitialisation() { AndroidMessageQueue::getInstance(); }
void MessageManager::doPlatformSpecificShutdown() { AndroidMessageQueue::deleteInstance(); }
bool MessageManager::postMessageToSystemQueue (MessageManager::MessageBase* const message)
{
return AndroidMessageQueue::getInstance()->post (message);
}
//==============================================================================
void MessageManager::broadcastMessage (const String&)
{
}
void MessageManager::runDispatchLoop()
{
}
void MessageManager::stopDispatchLoop()
{
struct QuitCallback : public CallbackMessage
{
QuitCallback() {}
void messageCallback() override
{
auto* env = getEnv();
LocalRef<jobject> activity (getCurrentActivity());
if (activity != nullptr)
{
jmethodID quitMethod = env->GetMethodID (AndroidActivity, "finishAndRemoveTask", "()V");
if (quitMethod != nullptr)
{
env->CallVoidMethod (activity.get(), quitMethod);
return;
}
quitMethod = env->GetMethodID (AndroidActivity, "finish", "()V");
jassert (quitMethod != nullptr);
env->CallVoidMethod (activity.get(), quitMethod);
}
else
{
jassertfalse;
}
}
};
(new QuitCallback())->post();
quitMessagePosted = true;
}
//==============================================================================
class JuceAppLifecycle : public ActivityLifecycleCallbacks
{
public:
JuceAppLifecycle (juce::JUCEApplicationBase* (*initSymbolAddr)())
: createApplicationSymbol (initSymbolAddr)
{
LocalRef<jobject> appContext (getAppContext());
if (appContext != nullptr)
{
auto* env = getEnv();
myself = GlobalRef (CreateJavaInterface (this, "android/app/Application$ActivityLifecycleCallbacks"));
env->CallVoidMethod (appContext.get(), AndroidApplication.registerActivityLifecycleCallbacks, myself.get());
}
}
~JuceAppLifecycle() override
{
LocalRef<jobject> appContext (getAppContext());
if (appContext != nullptr && myself != nullptr)
{
auto* env = getEnv();
clear();
env->CallVoidMethod (appContext.get(), AndroidApplication.unregisterActivityLifecycleCallbacks, myself.get());
myself.clear();
}
}
void onActivityCreated (jobject, jobject) override
{
checkCreated();
}
void onActivityDestroyed (jobject activity) override
{
auto* env = getEnv();
// if the main activity is being destroyed, only then tear-down JUCE
if (env->IsSameObject (getMainActivity().get(), activity) != 0)
{
JUCEApplicationBase::appWillTerminateByForce();
JNIClassBase::releaseAllClasses (env);
jclass systemClass = (jclass) env->FindClass ("java/lang/System");
jmethodID exitMethod = env->GetStaticMethodID (systemClass, "exit", "(I)V");
env->CallStaticVoidMethod (systemClass, exitMethod, 0);
}
}
void onActivityStarted (jobject) override
{
checkCreated();
}
void onActivityPaused (jobject) override
{
if (auto* app = JUCEApplicationBase::getInstance())
app->suspended();
}
void onActivityResumed (jobject) override
{
checkInitialised();
if (auto* app = JUCEApplicationBase::getInstance())
app->resumed();
}
static JuceAppLifecycle& getInstance (juce::JUCEApplicationBase* (*initSymbolAddr)())
{
static JuceAppLifecycle juceAppLifecycle (initSymbolAddr);
return juceAppLifecycle;
}
private:
void checkCreated()
{
if (JUCEApplicationBase::getInstance() == nullptr)
{
DBG (SystemStats::getJUCEVersion());
JUCEApplicationBase::createInstance = createApplicationSymbol;
initialiseJuce_GUI();
if (! JUCEApplicationBase::createInstance())
jassertfalse; // you must supply an application object for an android app!
jassert (MessageManager::getInstance()->isThisTheMessageThread());
}
}
void checkInitialised()
{
checkCreated();
if (! hasBeenInitialised)
{
if (auto* app = JUCEApplicationBase::getInstance())
{
hasBeenInitialised = app->initialiseApp();
if (! hasBeenInitialised)
exit (app->shutdownApp());
}
}
}
GlobalRef myself;
juce::JUCEApplicationBase* (*createApplicationSymbol)();
bool hasBeenInitialised = false;
};
//==============================================================================
File juce_getExecutableFile();
void juce_juceEventsAndroidStartApp();
void juce_juceEventsAndroidStartApp()
{
auto dllPath = juce_getExecutableFile().getFullPathName();
auto addr = reinterpret_cast<juce::JUCEApplicationBase*(*)()> (DynamicLibrary (dllPath)
.getFunction ("juce_CreateApplication"));
if (addr != nullptr)
JuceAppLifecycle::getInstance (addr);
}
} // namespace juce

View File

@ -2,7 +2,7 @@
==============================================================================
This file is part of the JUCE library.
Copyright (c) 2020 - Raw Material Software Limited
Copyright (c) 2022 - Raw Material Software Limited
JUCE is an open source library subject to commercial or open-source
licensing.

View File

@ -1,50 +1,50 @@
/*
==============================================================================
This file is part of the JUCE library.
Copyright (c) 2020 - Raw Material Software Limited
JUCE is an open source library subject to commercial or open-source
licensing.
The code included in this file is provided under the terms of the ISC license
http://www.isc.org/downloads/software-support-policy/isc-license. Permission
To use, copy, modify, and/or distribute this software for any purpose with or
without fee is hereby granted provided that the above copyright notice and
this permission notice appear in all copies.
JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
DISCLAIMED.
==============================================================================
*/
namespace juce
{
namespace LinuxEventLoop
{
/** Registers a callback that will be called when a file descriptor is ready for I/O.
This will add the given file descriptor to the internal set of file descriptors
that will be passed to the poll() call. When this file descriptor has data to read
the readCallback will be called.
@param fd the file descriptor to be monitored
@param readCallback a callback that will be called when the file descriptor has
data to read. The file descriptor will be passed as an argument
@param eventMask a bit mask specifying the events you are interested in for the
file descriptor. The possible values for this are defined in
<poll.h>
*/
void registerFdCallback (int fd, std::function<void (int)> readCallback, short eventMask = 1 /*POLLIN*/);
/** Unregisters a previously registered file descriptor.
@see registerFdCallback
*/
void unregisterFdCallback (int fd);
}
} // namespace juce
/*
==============================================================================
This file is part of the JUCE library.
Copyright (c) 2022 - Raw Material Software Limited
JUCE is an open source library subject to commercial or open-source
licensing.
The code included in this file is provided under the terms of the ISC license
http://www.isc.org/downloads/software-support-policy/isc-license. Permission
To use, copy, modify, and/or distribute this software for any purpose with or
without fee is hereby granted provided that the above copyright notice and
this permission notice appear in all copies.
JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
DISCLAIMED.
==============================================================================
*/
namespace juce
{
namespace LinuxEventLoop
{
/** Registers a callback that will be called when a file descriptor is ready for I/O.
This will add the given file descriptor to the internal set of file descriptors
that will be passed to the poll() call. When this file descriptor has data to read
the readCallback will be called.
@param fd the file descriptor to be monitored
@param readCallback a callback that will be called when the file descriptor has
data to read. The file descriptor will be passed as an argument
@param eventMask a bit mask specifying the events you are interested in for the
file descriptor. The possible values for this are defined in
<poll.h>
*/
void registerFdCallback (int fd, std::function<void (int)> readCallback, short eventMask = 1 /*POLLIN*/);
/** Unregisters a previously registered file descriptor.
@see registerFdCallback
*/
void unregisterFdCallback (int fd);
}
} // namespace juce

View File

@ -0,0 +1,55 @@
/*
==============================================================================
This file is part of the JUCE library.
Copyright (c) 2022 - Raw Material Software Limited
JUCE is an open source library subject to commercial or open-source
licensing.
The code included in this file is provided under the terms of the ISC license
http://www.isc.org/downloads/software-support-policy/isc-license. Permission
To use, copy, modify, and/or distribute this software for any purpose with or
without fee is hereby granted provided that the above copyright notice and
this permission notice appear in all copies.
JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
DISCLAIMED.
==============================================================================
*/
namespace juce
{
struct LinuxEventLoopInternal
{
/**
@internal
Receives notifications when a fd callback is added or removed.
This is useful for VST3 plugins that host other VST3 plugins. In this scenario, the 'inner'
plugin may want to register additional file descriptors on top of those registered by the
'outer' plugin. In order for this to work, the outer plugin must in turn pass this request
on to the host, to indicate that the outer plugin wants to receive FD callbacks for both the
inner and outer plugin.
*/
struct Listener
{
virtual ~Listener() = default;
virtual void fdCallbacksChanged() = 0;
};
/** @internal */
static void registerLinuxEventLoopListener (Listener&);
/** @internal */
static void deregisterLinuxEventLoopListener (Listener&);
/** @internal */
static void invokeEventLoopCallbackForFd (int);
/** @internal */
static std::vector<int> getRegisteredFds();
};
} // namespace juce

View File

@ -1,336 +1,395 @@
/*
==============================================================================
This file is part of the JUCE library.
Copyright (c) 2020 - Raw Material Software Limited
JUCE is an open source library subject to commercial or open-source
licensing.
The code included in this file is provided under the terms of the ISC license
http://www.isc.org/downloads/software-support-policy/isc-license. Permission
To use, copy, modify, and/or distribute this software for any purpose with or
without fee is hereby granted provided that the above copyright notice and
this permission notice appear in all copies.
JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
DISCLAIMED.
==============================================================================
*/
namespace juce
{
//==============================================================================
class InternalMessageQueue
{
public:
InternalMessageQueue()
{
auto err = ::socketpair (AF_LOCAL, SOCK_STREAM, 0, msgpipe);
jassertquiet (err == 0);
LinuxEventLoop::registerFdCallback (getReadHandle(),
[this] (int fd)
{
while (auto msg = popNextMessage (fd))
{
JUCE_TRY
{
msg->messageCallback();
}
JUCE_CATCH_EXCEPTION
}
});
}
~InternalMessageQueue()
{
LinuxEventLoop::unregisterFdCallback (getReadHandle());
close (getReadHandle());
close (getWriteHandle());
clearSingletonInstance();
}
//==============================================================================
void postMessage (MessageManager::MessageBase* const msg) noexcept
{
ScopedLock sl (lock);
queue.add (msg);
if (bytesInSocket < maxBytesInSocketQueue)
{
bytesInSocket++;
ScopedUnlock ul (lock);
unsigned char x = 0xff;
auto numBytes = write (getWriteHandle(), &x, 1);
ignoreUnused (numBytes);
}
}
//==============================================================================
JUCE_DECLARE_SINGLETON (InternalMessageQueue, false)
private:
CriticalSection lock;
ReferenceCountedArray <MessageManager::MessageBase> queue;
int msgpipe[2];
int bytesInSocket = 0;
static constexpr int maxBytesInSocketQueue = 128;
int getWriteHandle() const noexcept { return msgpipe[0]; }
int getReadHandle() const noexcept { return msgpipe[1]; }
MessageManager::MessageBase::Ptr popNextMessage (int fd) noexcept
{
const ScopedLock sl (lock);
if (bytesInSocket > 0)
{
--bytesInSocket;
ScopedUnlock ul (lock);
unsigned char x;
auto numBytes = read (fd, &x, 1);
ignoreUnused (numBytes);
}
return queue.removeAndReturn (0);
}
};
JUCE_IMPLEMENT_SINGLETON (InternalMessageQueue)
//==============================================================================
struct InternalRunLoop
{
public:
InternalRunLoop()
{
fdReadCallbacks.reserve (16);
}
void registerFdCallback (int fd, std::function<void (int)>&& cb, short eventMask)
{
const ScopedLock sl (lock);
if (shouldDeferModifyingReadCallbacks)
{
deferredReadCallbackModifications.emplace_back ([this, fd, cb, eventMask]() mutable
{
registerFdCallback (fd, std::move (cb), eventMask);
});
return;
}
fdReadCallbacks.push_back ({ fd, std::move (cb) });
pfds.push_back ({ fd, eventMask, 0 });
}
void unregisterFdCallback (int fd)
{
const ScopedLock sl (lock);
if (shouldDeferModifyingReadCallbacks)
{
deferredReadCallbackModifications.emplace_back ([this, fd] { unregisterFdCallback (fd); });
return;
}
{
auto removePredicate = [=] (const std::pair<int, std::function<void (int)>>& cb) { return cb.first == fd; };
fdReadCallbacks.erase (std::remove_if (std::begin (fdReadCallbacks), std::end (fdReadCallbacks), removePredicate),
std::end (fdReadCallbacks));
}
{
auto removePredicate = [=] (const pollfd& pfd) { return pfd.fd == fd; };
pfds.erase (std::remove_if (std::begin (pfds), std::end (pfds), removePredicate),
std::end (pfds));
}
}
bool dispatchPendingEvents()
{
const ScopedLock sl (lock);
if (poll (&pfds.front(), static_cast<nfds_t> (pfds.size()), 0) == 0)
return false;
bool eventWasSent = false;
for (auto& pfd : pfds)
{
if (pfd.revents == 0)
continue;
pfd.revents = 0;
auto fd = pfd.fd;
for (auto& fdAndCallback : fdReadCallbacks)
{
if (fdAndCallback.first == fd)
{
{
ScopedValueSetter<bool> insideFdReadCallback (shouldDeferModifyingReadCallbacks, true);
fdAndCallback.second (fd);
}
if (! deferredReadCallbackModifications.empty())
{
for (auto& deferredRegisterEvent : deferredReadCallbackModifications)
deferredRegisterEvent();
deferredReadCallbackModifications.clear();
// elements may have been removed from the fdReadCallbacks/pfds array so we really need
// to call poll again
return true;
}
eventWasSent = true;
}
}
}
return eventWasSent;
}
void sleepUntilNextEvent (int timeoutMs)
{
poll (&pfds.front(), static_cast<nfds_t> (pfds.size()), timeoutMs);
}
std::vector<std::pair<int, std::function<void (int)>>> getFdReadCallbacks()
{
const ScopedLock sl (lock);
return fdReadCallbacks;
}
//==============================================================================
JUCE_DECLARE_SINGLETON (InternalRunLoop, false)
private:
CriticalSection lock;
std::vector<std::pair<int, std::function<void (int)>>> fdReadCallbacks;
std::vector<pollfd> pfds;
bool shouldDeferModifyingReadCallbacks = false;
std::vector<std::function<void()>> deferredReadCallbackModifications;
};
JUCE_IMPLEMENT_SINGLETON (InternalRunLoop)
//==============================================================================
namespace LinuxErrorHandling
{
static bool keyboardBreakOccurred = false;
void keyboardBreakSignalHandler (int sig)
{
if (sig == SIGINT)
keyboardBreakOccurred = true;
}
void installKeyboardBreakHandler()
{
struct sigaction saction;
sigset_t maskSet;
sigemptyset (&maskSet);
saction.sa_handler = keyboardBreakSignalHandler;
saction.sa_mask = maskSet;
saction.sa_flags = 0;
sigaction (SIGINT, &saction, nullptr);
}
}
//==============================================================================
void MessageManager::doPlatformSpecificInitialisation()
{
if (JUCEApplicationBase::isStandaloneApp())
LinuxErrorHandling::installKeyboardBreakHandler();
InternalRunLoop::getInstance();
InternalMessageQueue::getInstance();
}
void MessageManager::doPlatformSpecificShutdown()
{
InternalMessageQueue::deleteInstance();
InternalRunLoop::deleteInstance();
}
bool MessageManager::postMessageToSystemQueue (MessageManager::MessageBase* const message)
{
if (auto* queue = InternalMessageQueue::getInstanceWithoutCreating())
{
queue->postMessage (message);
return true;
}
return false;
}
void MessageManager::broadcastMessage (const String&)
{
// TODO
}
// this function expects that it will NEVER be called simultaneously for two concurrent threads
bool dispatchNextMessageOnSystemQueue (bool returnIfNoPendingMessages)
{
for (;;)
{
if (LinuxErrorHandling::keyboardBreakOccurred)
JUCEApplicationBase::quit();
if (auto* runLoop = InternalRunLoop::getInstanceWithoutCreating())
{
if (runLoop->dispatchPendingEvents())
break;
if (returnIfNoPendingMessages)
return false;
runLoop->sleepUntilNextEvent (2000);
}
}
return true;
}
//==============================================================================
void LinuxEventLoop::registerFdCallback (int fd, std::function<void (int)> readCallback, short eventMask)
{
if (auto* runLoop = InternalRunLoop::getInstanceWithoutCreating())
runLoop->registerFdCallback (fd, std::move (readCallback), eventMask);
}
void LinuxEventLoop::unregisterFdCallback (int fd)
{
if (auto* runLoop = InternalRunLoop::getInstanceWithoutCreating())
runLoop->unregisterFdCallback (fd);
}
} // namespace juce
JUCE_API std::vector<std::pair<int, std::function<void (int)>>> getFdReadCallbacks()
{
using namespace juce;
if (auto* runLoop = InternalRunLoop::getInstanceWithoutCreating())
return runLoop->getFdReadCallbacks();
jassertfalse;
return {};
}
/*
==============================================================================
This file is part of the JUCE library.
Copyright (c) 2022 - Raw Material Software Limited
JUCE is an open source library subject to commercial or open-source
licensing.
The code included in this file is provided under the terms of the ISC license
http://www.isc.org/downloads/software-support-policy/isc-license. Permission
To use, copy, modify, and/or distribute this software for any purpose with or
without fee is hereby granted provided that the above copyright notice and
this permission notice appear in all copies.
JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
DISCLAIMED.
==============================================================================
*/
namespace juce
{
//==============================================================================
class InternalMessageQueue
{
public:
InternalMessageQueue()
{
auto err = ::socketpair (AF_LOCAL, SOCK_STREAM, 0, msgpipe);
jassertquiet (err == 0);
LinuxEventLoop::registerFdCallback (getReadHandle(),
[this] (int fd)
{
while (auto msg = popNextMessage (fd))
{
JUCE_TRY
{
msg->messageCallback();
}
JUCE_CATCH_EXCEPTION
}
});
}
~InternalMessageQueue()
{
LinuxEventLoop::unregisterFdCallback (getReadHandle());
close (getReadHandle());
close (getWriteHandle());
clearSingletonInstance();
}
//==============================================================================
void postMessage (MessageManager::MessageBase* const msg) noexcept
{
ScopedLock sl (lock);
queue.add (msg);
if (bytesInSocket < maxBytesInSocketQueue)
{
bytesInSocket++;
ScopedUnlock ul (lock);
unsigned char x = 0xff;
auto numBytes = write (getWriteHandle(), &x, 1);
ignoreUnused (numBytes);
}
}
//==============================================================================
JUCE_DECLARE_SINGLETON (InternalMessageQueue, false)
private:
CriticalSection lock;
ReferenceCountedArray <MessageManager::MessageBase> queue;
int msgpipe[2];
int bytesInSocket = 0;
static constexpr int maxBytesInSocketQueue = 128;
int getWriteHandle() const noexcept { return msgpipe[0]; }
int getReadHandle() const noexcept { return msgpipe[1]; }
MessageManager::MessageBase::Ptr popNextMessage (int fd) noexcept
{
const ScopedLock sl (lock);
if (bytesInSocket > 0)
{
--bytesInSocket;
ScopedUnlock ul (lock);
unsigned char x;
auto numBytes = read (fd, &x, 1);
ignoreUnused (numBytes);
}
return queue.removeAndReturn (0);
}
};
JUCE_IMPLEMENT_SINGLETON (InternalMessageQueue)
//==============================================================================
/*
Stores callbacks associated with file descriptors (FD).
The callback for a particular FD should be called whenever that file has data to read.
For standalone apps, the main thread will call poll to wait for new data on any FD, and then
call the associated callbacks for any FDs that changed.
For plugins, the host (generally) provides some kind of run loop mechanism instead.
- In VST2 plugins, the host should call effEditIdle at regular intervals, and plugins can
dispatch all pending events inside this callback. The host doesn't know about any of the
plugin's FDs, so it's possible there will be a bit of latency between an FD becoming ready,
and its associated callback being called.
- In VST3 plugins, it's possible to register each FD individually with the host. In this case,
the facilities in LinuxEventLoopInternal can be used to observe added/removed FD callbacks,
and the host can be notified whenever the set of FDs changes. The host will call onFDIsSet
whenever a particular FD has data ready. This call should be forwarded through to
InternalRunLoop::dispatchEvent.
*/
struct InternalRunLoop
{
public:
InternalRunLoop() = default;
void registerFdCallback (int fd, std::function<void()>&& cb, short eventMask)
{
{
const ScopedLock sl (lock);
callbacks.emplace (fd, std::make_shared<std::function<void()>> (std::move (cb)));
const auto iter = getPollfd (fd);
if (iter == pfds.end() || iter->fd != fd)
pfds.insert (iter, { fd, eventMask, 0 });
else
jassertfalse;
jassert (pfdsAreSorted());
}
listeners.call ([] (auto& l) { l.fdCallbacksChanged(); });
}
void unregisterFdCallback (int fd)
{
{
const ScopedLock sl (lock);
callbacks.erase (fd);
const auto iter = getPollfd (fd);
if (iter != pfds.end() && iter->fd == fd)
pfds.erase (iter);
else
jassertfalse;
jassert (pfdsAreSorted());
}
listeners.call ([] (auto& l) { l.fdCallbacksChanged(); });
}
bool dispatchPendingEvents()
{
callbackStorage.clear();
getFunctionsToCallThisTime (callbackStorage);
// CriticalSection should be available during the callback
for (auto& fn : callbackStorage)
(*fn)();
return ! callbackStorage.empty();
}
void dispatchEvent (int fd) const
{
const auto fn = [&]
{
const ScopedLock sl (lock);
const auto iter = callbacks.find (fd);
return iter != callbacks.end() ? iter->second : nullptr;
}();
// CriticalSection should be available during the callback
if (auto* callback = fn.get())
(*callback)();
}
bool sleepUntilNextEvent (int timeoutMs)
{
const ScopedLock sl (lock);
return poll (pfds.data(), static_cast<nfds_t> (pfds.size()), timeoutMs) != 0;
}
std::vector<int> getRegisteredFds()
{
const ScopedLock sl (lock);
std::vector<int> result;
result.reserve (callbacks.size());
std::transform (callbacks.begin(),
callbacks.end(),
std::back_inserter (result),
[] (const auto& pair) { return pair.first; });
return result;
}
void addListener (LinuxEventLoopInternal::Listener& listener) { listeners.add (&listener); }
void removeListener (LinuxEventLoopInternal::Listener& listener) { listeners.remove (&listener); }
//==============================================================================
JUCE_DECLARE_SINGLETON (InternalRunLoop, false)
private:
using SharedCallback = std::shared_ptr<std::function<void()>>;
/* Appends any functions that need to be called to the passed-in vector.
We take a copy of each shared function so that the functions can be called without
locking or racing in the event that the function attempts to register/deregister a
new FD callback.
*/
void getFunctionsToCallThisTime (std::vector<SharedCallback>& functions)
{
const ScopedLock sl (lock);
if (! sleepUntilNextEvent (0))
return;
for (auto& pfd : pfds)
{
if (std::exchange (pfd.revents, 0) != 0)
{
const auto iter = callbacks.find (pfd.fd);
if (iter != callbacks.end())
functions.emplace_back (iter->second);
}
}
}
std::vector<pollfd>::iterator getPollfd (int fd)
{
return std::lower_bound (pfds.begin(), pfds.end(), fd, [] (auto descriptor, auto toFind)
{
return descriptor.fd < toFind;
});
}
bool pfdsAreSorted() const
{
return std::is_sorted (pfds.begin(), pfds.end(), [] (auto a, auto b) { return a.fd < b.fd; });
}
CriticalSection lock;
std::map<int, SharedCallback> callbacks;
std::vector<SharedCallback> callbackStorage;
std::vector<pollfd> pfds;
ListenerList<LinuxEventLoopInternal::Listener> listeners;
};
JUCE_IMPLEMENT_SINGLETON (InternalRunLoop)
//==============================================================================
namespace LinuxErrorHandling
{
static bool keyboardBreakOccurred = false;
static void keyboardBreakSignalHandler (int sig)
{
if (sig == SIGINT)
keyboardBreakOccurred = true;
}
static void installKeyboardBreakHandler()
{
struct sigaction saction;
sigset_t maskSet;
sigemptyset (&maskSet);
saction.sa_handler = keyboardBreakSignalHandler;
saction.sa_mask = maskSet;
saction.sa_flags = 0;
sigaction (SIGINT, &saction, nullptr);
}
}
//==============================================================================
void MessageManager::doPlatformSpecificInitialisation()
{
if (JUCEApplicationBase::isStandaloneApp())
LinuxErrorHandling::installKeyboardBreakHandler();
InternalRunLoop::getInstance();
InternalMessageQueue::getInstance();
}
void MessageManager::doPlatformSpecificShutdown()
{
InternalMessageQueue::deleteInstance();
InternalRunLoop::deleteInstance();
}
bool MessageManager::postMessageToSystemQueue (MessageManager::MessageBase* const message)
{
if (auto* queue = InternalMessageQueue::getInstanceWithoutCreating())
{
queue->postMessage (message);
return true;
}
return false;
}
void MessageManager::broadcastMessage (const String&)
{
// TODO
}
// this function expects that it will NEVER be called simultaneously for two concurrent threads
bool dispatchNextMessageOnSystemQueue (bool returnIfNoPendingMessages)
{
for (;;)
{
if (LinuxErrorHandling::keyboardBreakOccurred)
JUCEApplicationBase::quit();
if (auto* runLoop = InternalRunLoop::getInstanceWithoutCreating())
{
if (runLoop->dispatchPendingEvents())
break;
if (returnIfNoPendingMessages)
return false;
runLoop->sleepUntilNextEvent (2000);
}
}
return true;
}
//==============================================================================
void LinuxEventLoop::registerFdCallback (int fd, std::function<void (int)> readCallback, short eventMask)
{
if (auto* runLoop = InternalRunLoop::getInstanceWithoutCreating())
runLoop->registerFdCallback (fd, [cb = std::move (readCallback), fd] { cb (fd); }, eventMask);
}
void LinuxEventLoop::unregisterFdCallback (int fd)
{
if (auto* runLoop = InternalRunLoop::getInstanceWithoutCreating())
runLoop->unregisterFdCallback (fd);
}
//==============================================================================
void LinuxEventLoopInternal::registerLinuxEventLoopListener (LinuxEventLoopInternal::Listener& listener)
{
if (auto* runLoop = InternalRunLoop::getInstanceWithoutCreating())
runLoop->addListener (listener);
}
void LinuxEventLoopInternal::deregisterLinuxEventLoopListener (LinuxEventLoopInternal::Listener& listener)
{
if (auto* runLoop = InternalRunLoop::getInstanceWithoutCreating())
runLoop->removeListener (listener);
}
void LinuxEventLoopInternal::invokeEventLoopCallbackForFd (int fd)
{
if (auto* runLoop = InternalRunLoop::getInstanceWithoutCreating())
runLoop->dispatchEvent (fd);
}
std::vector<int> LinuxEventLoopInternal::getRegisteredFds()
{
if (auto* runLoop = InternalRunLoop::getInstanceWithoutCreating())
return runLoop->getRegisteredFds();
return {};
}
} // namespace juce

View File

@ -2,7 +2,7 @@
==============================================================================
This file is part of the JUCE library.
Copyright (c) 2020 - Raw Material Software Limited
Copyright (c) 2022 - Raw Material Software Limited
JUCE is an open source library subject to commercial or open-source
licensing.
@ -37,36 +37,36 @@ struct AppDelegateClass : public ObjCClass<NSObject>
{
AppDelegateClass() : ObjCClass<NSObject> ("JUCEAppDelegate_")
{
addMethod (@selector (applicationWillFinishLaunching:), applicationWillFinishLaunching, "v@:@");
addMethod (@selector (applicationShouldTerminate:), applicationShouldTerminate, "I@:@");
addMethod (@selector (applicationWillTerminate:), applicationWillTerminate, "v@:@");
addMethod (@selector (application:openFile:), application_openFile, "c@:@@");
addMethod (@selector (application:openFiles:), application_openFiles, "v@:@@");
addMethod (@selector (applicationDidBecomeActive:), applicationDidBecomeActive, "v@:@");
addMethod (@selector (applicationDidResignActive:), applicationDidResignActive, "v@:@");
addMethod (@selector (applicationWillUnhide:), applicationWillUnhide, "v@:@");
addMethod (@selector (applicationWillFinishLaunching:), applicationWillFinishLaunching);
addMethod (@selector (applicationShouldTerminate:), applicationShouldTerminate);
addMethod (@selector (applicationWillTerminate:), applicationWillTerminate);
addMethod (@selector (application:openFile:), application_openFile);
addMethod (@selector (application:openFiles:), application_openFiles);
addMethod (@selector (applicationDidBecomeActive:), applicationDidBecomeActive);
addMethod (@selector (applicationDidResignActive:), applicationDidResignActive);
addMethod (@selector (applicationWillUnhide:), applicationWillUnhide);
JUCE_BEGIN_IGNORE_WARNINGS_GCC_LIKE ("-Wundeclared-selector")
addMethod (@selector (getUrl:withReplyEvent:), getUrl_withReplyEvent, "v@:@@");
addMethod (@selector (broadcastMessageCallback:), broadcastMessageCallback, "v@:@");
addMethod (@selector (mainMenuTrackingBegan:), mainMenuTrackingBegan, "v@:@");
addMethod (@selector (mainMenuTrackingEnded:), mainMenuTrackingEnded, "v@:@");
addMethod (@selector (dummyMethod), dummyMethod, "v@:");
addMethod (@selector (getUrl:withReplyEvent:), getUrl_withReplyEvent);
addMethod (@selector (broadcastMessageCallback:), broadcastMessageCallback);
addMethod (@selector (mainMenuTrackingBegan:), mainMenuTrackingBegan);
addMethod (@selector (mainMenuTrackingEnded:), mainMenuTrackingEnded);
addMethod (@selector (dummyMethod), dummyMethod);
JUCE_END_IGNORE_WARNINGS_GCC_LIKE
#if JUCE_PUSH_NOTIFICATIONS
//==============================================================================
addIvar<NSObject<NSApplicationDelegate, NSUserNotificationCenterDelegate>*> ("pushNotificationsDelegate");
addMethod (@selector (applicationDidFinishLaunching:), applicationDidFinishLaunching, "v@:@");
addMethod (@selector (applicationDidFinishLaunching:), applicationDidFinishLaunching);
JUCE_BEGIN_IGNORE_WARNINGS_GCC_LIKE ("-Wundeclared-selector")
addMethod (@selector (setPushNotificationsDelegate:), setPushNotificationsDelegate, "v@:@");
addMethod (@selector (setPushNotificationsDelegate:), setPushNotificationsDelegate);
JUCE_END_IGNORE_WARNINGS_GCC_LIKE
addMethod (@selector (application:didRegisterForRemoteNotificationsWithDeviceToken:), registeredForRemoteNotifications, "v@:@@");
addMethod (@selector (application:didFailToRegisterForRemoteNotificationsWithError:), failedToRegisterForRemoteNotifications, "v@:@@");
addMethod (@selector (application:didReceiveRemoteNotification:), didReceiveRemoteNotification, "v@:@@");
addMethod (@selector (application:didRegisterForRemoteNotificationsWithDeviceToken:), registeredForRemoteNotifications);
addMethod (@selector (application:didFailToRegisterForRemoteNotificationsWithError:), failedToRegisterForRemoteNotifications);
addMethod (@selector (application:didReceiveRemoteNotification:), didReceiveRemoteNotification);
#endif
registerClass();
@ -460,8 +460,8 @@ void MessageManager::broadcastMessage (const String& message)
}
// Special function used by some plugin classes to re-post carbon events
void __attribute__ ((visibility("default"))) repostCurrentNSEvent();
void __attribute__ ((visibility("default"))) repostCurrentNSEvent()
void repostCurrentNSEvent();
void repostCurrentNSEvent()
{
struct EventReposter : public CallbackMessage
{
@ -515,7 +515,7 @@ private:
addIvar<Pimpl*> ("owner");
JUCE_BEGIN_IGNORE_WARNINGS_GCC_LIKE ("-Wundeclared-selector")
addMethod (@selector (changed:), changed, "v@:@");
addMethod (@selector (changed:), changed);
JUCE_END_IGNORE_WARNINGS_GCC_LIKE
addProtocol (@protocol (NSTextInput));

View File

@ -1,104 +1,104 @@
/*
==============================================================================
This file is part of the JUCE library.
Copyright (c) 2020 - Raw Material Software Limited
JUCE is an open source library subject to commercial or open-source
licensing.
The code included in this file is provided under the terms of the ISC license
http://www.isc.org/downloads/software-support-policy/isc-license. Permission
To use, copy, modify, and/or distribute this software for any purpose with or
without fee is hereby granted provided that the above copyright notice and
this permission notice appear in all copies.
JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
DISCLAIMED.
==============================================================================
*/
namespace juce
{
//==============================================================================
/* An internal message pump class used in OSX and iOS. */
class MessageQueue
{
public:
MessageQueue()
{
#if JUCE_IOS
runLoop = CFRunLoopGetCurrent();
#else
runLoop = CFRunLoopGetMain();
#endif
CFRunLoopSourceContext sourceContext;
zerostruct (sourceContext); // (can't use "= { 0 }" on this object because it's typedef'ed as a C struct)
sourceContext.info = this;
sourceContext.perform = runLoopSourceCallback;
runLoopSource.reset (CFRunLoopSourceCreate (kCFAllocatorDefault, 1, &sourceContext));
CFRunLoopAddSource (runLoop, runLoopSource.get(), kCFRunLoopCommonModes);
}
~MessageQueue() noexcept
{
CFRunLoopRemoveSource (runLoop, runLoopSource.get(), kCFRunLoopCommonModes);
CFRunLoopSourceInvalidate (runLoopSource.get());
}
void post (MessageManager::MessageBase* const message)
{
messages.add (message);
wakeUp();
}
private:
ReferenceCountedArray<MessageManager::MessageBase, CriticalSection> messages;
CFRunLoopRef runLoop;
CFUniquePtr<CFRunLoopSourceRef> runLoopSource;
void wakeUp() noexcept
{
CFRunLoopSourceSignal (runLoopSource.get());
CFRunLoopWakeUp (runLoop);
}
bool deliverNextMessage()
{
const MessageManager::MessageBase::Ptr nextMessage (messages.removeAndReturn (0));
if (nextMessage == nullptr)
return false;
JUCE_AUTORELEASEPOOL
{
JUCE_TRY
{
nextMessage->messageCallback();
}
JUCE_CATCH_EXCEPTION
}
return true;
}
void runLoopCallback() noexcept
{
for (int i = 4; --i >= 0;)
if (! deliverNextMessage())
return;
wakeUp();
}
static void runLoopSourceCallback (void* info) noexcept
{
static_cast<MessageQueue*> (info)->runLoopCallback();
}
};
} // namespace juce
/*
==============================================================================
This file is part of the JUCE library.
Copyright (c) 2022 - Raw Material Software Limited
JUCE is an open source library subject to commercial or open-source
licensing.
The code included in this file is provided under the terms of the ISC license
http://www.isc.org/downloads/software-support-policy/isc-license. Permission
To use, copy, modify, and/or distribute this software for any purpose with or
without fee is hereby granted provided that the above copyright notice and
this permission notice appear in all copies.
JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
DISCLAIMED.
==============================================================================
*/
namespace juce
{
//==============================================================================
/* An internal message pump class used in OSX and iOS. */
class MessageQueue
{
public:
MessageQueue()
{
#if JUCE_IOS
runLoop = CFRunLoopGetCurrent();
#else
runLoop = CFRunLoopGetMain();
#endif
CFRunLoopSourceContext sourceContext;
zerostruct (sourceContext); // (can't use "= { 0 }" on this object because it's typedef'ed as a C struct)
sourceContext.info = this;
sourceContext.perform = runLoopSourceCallback;
runLoopSource.reset (CFRunLoopSourceCreate (kCFAllocatorDefault, 1, &sourceContext));
CFRunLoopAddSource (runLoop, runLoopSource.get(), kCFRunLoopCommonModes);
}
~MessageQueue() noexcept
{
CFRunLoopRemoveSource (runLoop, runLoopSource.get(), kCFRunLoopCommonModes);
CFRunLoopSourceInvalidate (runLoopSource.get());
}
void post (MessageManager::MessageBase* const message)
{
messages.add (message);
wakeUp();
}
private:
ReferenceCountedArray<MessageManager::MessageBase, CriticalSection> messages;
CFRunLoopRef runLoop;
CFUniquePtr<CFRunLoopSourceRef> runLoopSource;
void wakeUp() noexcept
{
CFRunLoopSourceSignal (runLoopSource.get());
CFRunLoopWakeUp (runLoop);
}
bool deliverNextMessage()
{
const MessageManager::MessageBase::Ptr nextMessage (messages.removeAndReturn (0));
if (nextMessage == nullptr)
return false;
JUCE_AUTORELEASEPOOL
{
JUCE_TRY
{
nextMessage->messageCallback();
}
JUCE_CATCH_EXCEPTION
}
return true;
}
void runLoopCallback() noexcept
{
for (int i = 4; --i >= 0;)
if (! deliverNextMessage())
return;
wakeUp();
}
static void runLoopSourceCallback (void* info) noexcept
{
static_cast<MessageQueue*> (info)->runLoopCallback();
}
};
} // namespace juce

View File

@ -1,134 +1,134 @@
/*
==============================================================================
This file is part of the JUCE library.
Copyright (c) 2020 - Raw Material Software Limited
JUCE is an open source library subject to commercial or open-source
licensing.
The code included in this file is provided under the terms of the ISC license
http://www.isc.org/downloads/software-support-policy/isc-license. Permission
To use, copy, modify, and/or distribute this software for any purpose with or
without fee is hereby granted provided that the above copyright notice and
this permission notice appear in all copies.
JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
DISCLAIMED.
==============================================================================
*/
namespace juce
{
//==============================================================================
class HiddenMessageWindow
{
public:
HiddenMessageWindow (const TCHAR* const messageWindowName, WNDPROC wndProc)
{
String className ("JUCE_");
className << String::toHexString (Time::getHighResolutionTicks());
HMODULE moduleHandle = (HMODULE) Process::getCurrentModuleInstanceHandle();
WNDCLASSEX wc = {};
wc.cbSize = sizeof (wc);
wc.lpfnWndProc = wndProc;
wc.cbWndExtra = 4;
wc.hInstance = moduleHandle;
wc.lpszClassName = className.toWideCharPointer();
atom = RegisterClassEx (&wc);
jassert (atom != 0);
hwnd = CreateWindow (getClassNameFromAtom(), messageWindowName,
0, 0, 0, 0, 0,
nullptr, nullptr, moduleHandle, nullptr);
jassert (hwnd != nullptr);
}
~HiddenMessageWindow()
{
DestroyWindow (hwnd);
UnregisterClass (getClassNameFromAtom(), nullptr);
}
inline HWND getHWND() const noexcept { return hwnd; }
private:
ATOM atom;
HWND hwnd;
LPCTSTR getClassNameFromAtom() noexcept { return (LPCTSTR) (pointer_sized_uint) atom; }
};
//==============================================================================
class JuceWindowIdentifier
{
public:
static bool isJUCEWindow (HWND hwnd) noexcept
{
return GetWindowLongPtr (hwnd, GWLP_USERDATA) == getImprobableWindowNumber();
}
static void setAsJUCEWindow (HWND hwnd, bool isJuceWindow) noexcept
{
SetWindowLongPtr (hwnd, GWLP_USERDATA, isJuceWindow ? getImprobableWindowNumber() : 0);
}
private:
static LONG_PTR getImprobableWindowNumber() noexcept
{
static auto number = (LONG_PTR) Random().nextInt64();
return number;
}
};
//==============================================================================
class DeviceChangeDetector : private Timer
{
public:
DeviceChangeDetector (const wchar_t* const name)
: messageWindow (name, (WNDPROC) deviceChangeEventCallback)
{
SetWindowLongPtr (messageWindow.getHWND(), GWLP_USERDATA, (LONG_PTR) this);
}
virtual void systemDeviceChanged() = 0;
void triggerAsyncDeviceChangeCallback()
{
// We'll pause before sending a message, because on device removal, the OS hasn't always updated
// its device lists correctly at this point. This also helps avoid repeated callbacks.
startTimer (500);
}
private:
HiddenMessageWindow messageWindow;
static LRESULT CALLBACK deviceChangeEventCallback (HWND h, const UINT message,
const WPARAM wParam, const LPARAM lParam)
{
if (message == WM_DEVICECHANGE
&& (wParam == 0x8000 /*DBT_DEVICEARRIVAL*/
|| wParam == 0x8004 /*DBT_DEVICEREMOVECOMPLETE*/
|| wParam == 0x0007 /*DBT_DEVNODES_CHANGED*/))
{
((DeviceChangeDetector*) GetWindowLongPtr (h, GWLP_USERDATA))
->triggerAsyncDeviceChangeCallback();
}
return DefWindowProc (h, message, wParam, lParam);
}
void timerCallback() override
{
stopTimer();
systemDeviceChanged();
}
};
} // namespace juce
/*
==============================================================================
This file is part of the JUCE library.
Copyright (c) 2022 - Raw Material Software Limited
JUCE is an open source library subject to commercial or open-source
licensing.
The code included in this file is provided under the terms of the ISC license
http://www.isc.org/downloads/software-support-policy/isc-license. Permission
To use, copy, modify, and/or distribute this software for any purpose with or
without fee is hereby granted provided that the above copyright notice and
this permission notice appear in all copies.
JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
DISCLAIMED.
==============================================================================
*/
namespace juce
{
//==============================================================================
class HiddenMessageWindow
{
public:
HiddenMessageWindow (const TCHAR* const messageWindowName, WNDPROC wndProc)
{
String className ("JUCE_");
className << String::toHexString (Time::getHighResolutionTicks());
HMODULE moduleHandle = (HMODULE) Process::getCurrentModuleInstanceHandle();
WNDCLASSEX wc = {};
wc.cbSize = sizeof (wc);
wc.lpfnWndProc = wndProc;
wc.cbWndExtra = 4;
wc.hInstance = moduleHandle;
wc.lpszClassName = className.toWideCharPointer();
atom = RegisterClassEx (&wc);
jassert (atom != 0);
hwnd = CreateWindow (getClassNameFromAtom(), messageWindowName,
0, 0, 0, 0, 0,
nullptr, nullptr, moduleHandle, nullptr);
jassert (hwnd != nullptr);
}
~HiddenMessageWindow()
{
DestroyWindow (hwnd);
UnregisterClass (getClassNameFromAtom(), nullptr);
}
inline HWND getHWND() const noexcept { return hwnd; }
private:
ATOM atom;
HWND hwnd;
LPCTSTR getClassNameFromAtom() noexcept { return (LPCTSTR) (pointer_sized_uint) atom; }
};
//==============================================================================
class JuceWindowIdentifier
{
public:
static bool isJUCEWindow (HWND hwnd) noexcept
{
return GetWindowLongPtr (hwnd, GWLP_USERDATA) == getImprobableWindowNumber();
}
static void setAsJUCEWindow (HWND hwnd, bool isJuceWindow) noexcept
{
SetWindowLongPtr (hwnd, GWLP_USERDATA, isJuceWindow ? getImprobableWindowNumber() : 0);
}
private:
static LONG_PTR getImprobableWindowNumber() noexcept
{
static auto number = (LONG_PTR) Random().nextInt64();
return number;
}
};
//==============================================================================
class DeviceChangeDetector : private Timer
{
public:
DeviceChangeDetector (const wchar_t* const name)
: messageWindow (name, (WNDPROC) deviceChangeEventCallback)
{
SetWindowLongPtr (messageWindow.getHWND(), GWLP_USERDATA, (LONG_PTR) this);
}
virtual void systemDeviceChanged() = 0;
void triggerAsyncDeviceChangeCallback()
{
// We'll pause before sending a message, because on device removal, the OS hasn't always updated
// its device lists correctly at this point. This also helps avoid repeated callbacks.
startTimer (500);
}
private:
HiddenMessageWindow messageWindow;
static LRESULT CALLBACK deviceChangeEventCallback (HWND h, const UINT message,
const WPARAM wParam, const LPARAM lParam)
{
if (message == WM_DEVICECHANGE
&& (wParam == 0x8000 /*DBT_DEVICEARRIVAL*/
|| wParam == 0x8004 /*DBT_DEVICEREMOVECOMPLETE*/
|| wParam == 0x0007 /*DBT_DEVNODES_CHANGED*/))
{
((DeviceChangeDetector*) GetWindowLongPtr (h, GWLP_USERDATA))
->triggerAsyncDeviceChangeCallback();
}
return DefWindowProc (h, message, wParam, lParam);
}
void timerCallback() override
{
stopTimer();
systemDeviceChanged();
}
};
} // namespace juce

View File

@ -1,328 +1,328 @@
/*
==============================================================================
This file is part of the JUCE library.
Copyright (c) 2020 - Raw Material Software Limited
JUCE is an open source library subject to commercial or open-source
licensing.
The code included in this file is provided under the terms of the ISC license
http://www.isc.org/downloads/software-support-policy/isc-license. Permission
To use, copy, modify, and/or distribute this software for any purpose with or
without fee is hereby granted provided that the above copyright notice and
this permission notice appear in all copies.
JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
DISCLAIMED.
==============================================================================
*/
namespace juce
{
extern HWND juce_messageWindowHandle;
#if JUCE_MODULE_AVAILABLE_juce_audio_plugin_client && JucePlugin_Build_Unity
bool juce_isRunningInUnity();
#endif
#if JUCE_MODULE_AVAILABLE_juce_gui_extra
LRESULT juce_offerEventToActiveXControl (::MSG&);
#endif
using CheckEventBlockedByModalComps = bool (*)(const MSG&);
CheckEventBlockedByModalComps isEventBlockedByModalComps = nullptr;
using SettingChangeCallbackFunc = void (*)(void);
SettingChangeCallbackFunc settingChangeCallback = nullptr;
//==============================================================================
class InternalMessageQueue
{
public:
InternalMessageQueue()
{
messageWindow = std::make_unique<HiddenMessageWindow> (messageWindowName, (WNDPROC) messageWndProc);
juce_messageWindowHandle = messageWindow->getHWND();
}
~InternalMessageQueue()
{
juce_messageWindowHandle = nullptr;
clearSingletonInstance();
}
JUCE_DECLARE_SINGLETON (InternalMessageQueue, false)
//==============================================================================
void broadcastMessage (const String& message)
{
auto localCopy = message;
Array<HWND> windows;
EnumWindows (&broadcastEnumWindowProc, (LPARAM) &windows);
for (int i = windows.size(); --i >= 0;)
{
COPYDATASTRUCT data;
data.dwData = broadcastMessageMagicNumber;
data.cbData = (DWORD) (((size_t) localCopy.length() + 1) * sizeof (CharPointer_UTF32::CharType));
data.lpData = (void*) localCopy.toUTF32().getAddress();
DWORD_PTR result;
SendMessageTimeout (windows.getUnchecked (i), WM_COPYDATA,
(WPARAM) juce_messageWindowHandle,
(LPARAM) &data,
SMTO_BLOCK | SMTO_ABORTIFHUNG, 8000, &result);
}
}
void postMessage (MessageManager::MessageBase* message)
{
bool shouldTriggerMessageQueueDispatch = false;
{
const ScopedLock sl (lock);
shouldTriggerMessageQueueDispatch = messageQueue.isEmpty();
messageQueue.add (message);
}
if (! shouldTriggerMessageQueueDispatch)
return;
#if JUCE_MODULE_AVAILABLE_juce_audio_plugin_client && JucePlugin_Build_Unity
if (juce_isRunningInUnity())
{
SendNotifyMessage (juce_messageWindowHandle, customMessageID, 0, 0);
return;
}
#endif
PostMessage (juce_messageWindowHandle, customMessageID, 0, 0);
}
bool dispatchNextMessage (bool returnIfNoPendingMessages)
{
MSG m;
if (returnIfNoPendingMessages && ! PeekMessage (&m, nullptr, 0, 0, PM_NOREMOVE))
return false;
if (GetMessage (&m, nullptr, 0, 0) >= 0)
{
#if JUCE_MODULE_AVAILABLE_juce_gui_extra
if (juce_offerEventToActiveXControl (m) != S_FALSE)
return true;
#endif
if (m.message == customMessageID && m.hwnd == juce_messageWindowHandle)
{
dispatchMessages();
}
else if (m.message == WM_QUIT)
{
if (auto* app = JUCEApplicationBase::getInstance())
app->systemRequestedQuit();
}
else if (isEventBlockedByModalComps == nullptr || ! isEventBlockedByModalComps (m))
{
if ((m.message == WM_LBUTTONDOWN || m.message == WM_RBUTTONDOWN)
&& ! JuceWindowIdentifier::isJUCEWindow (m.hwnd))
{
// if it's someone else's window being clicked on, and the focus is
// currently on a juce window, pass the kb focus over..
auto currentFocus = GetFocus();
if (currentFocus == nullptr || JuceWindowIdentifier::isJUCEWindow (currentFocus))
SetFocus (m.hwnd);
}
TranslateMessage (&m);
DispatchMessage (&m);
}
}
return true;
}
private:
//==============================================================================
static LRESULT CALLBACK messageWndProc (HWND h, UINT message, WPARAM wParam, LPARAM lParam) noexcept
{
if (h == juce_messageWindowHandle)
{
if (message == customMessageID)
{
if (auto* queue = InternalMessageQueue::getInstanceWithoutCreating())
queue->dispatchMessages();
return 0;
}
if (message == WM_COPYDATA)
{
handleBroadcastMessage (reinterpret_cast<const COPYDATASTRUCT*> (lParam));
return 0;
}
if (message == WM_SETTINGCHANGE)
if (settingChangeCallback != nullptr)
settingChangeCallback();
}
return DefWindowProc (h, message, wParam, lParam);
}
static BOOL CALLBACK broadcastEnumWindowProc (HWND hwnd, LPARAM lParam)
{
if (hwnd != juce_messageWindowHandle)
{
TCHAR windowName[64] = { 0 }; // no need to read longer strings than this
GetWindowText (hwnd, windowName, 63);
if (String (windowName) == messageWindowName)
reinterpret_cast<Array<HWND>*> (lParam)->add (hwnd);
}
return TRUE;
}
static void dispatchMessage (MessageManager::MessageBase* message)
{
JUCE_TRY
{
message->messageCallback();
}
JUCE_CATCH_EXCEPTION
message->decReferenceCount();
}
static void handleBroadcastMessage (const COPYDATASTRUCT* data)
{
if (data != nullptr && data->dwData == broadcastMessageMagicNumber)
{
struct BroadcastMessage : public CallbackMessage
{
BroadcastMessage (CharPointer_UTF32 text, size_t length) : message (text, length) {}
void messageCallback() override { MessageManager::getInstance()->deliverBroadcastMessage (message); }
String message;
};
(new BroadcastMessage (CharPointer_UTF32 ((const CharPointer_UTF32::CharType*) data->lpData),
data->cbData / sizeof (CharPointer_UTF32::CharType)))
->post();
}
}
void dispatchMessages()
{
ReferenceCountedArray<MessageManager::MessageBase> messagesToDispatch;
{
const ScopedLock sl (lock);
if (messageQueue.isEmpty())
return;
messagesToDispatch.swapWith (messageQueue);
}
for (int i = 0; i < messagesToDispatch.size(); ++i)
{
auto message = messagesToDispatch.getUnchecked (i);
message->incReferenceCount();
dispatchMessage (message.get());
}
}
//==============================================================================
static constexpr unsigned int customMessageID = WM_USER + 123;
static constexpr unsigned int broadcastMessageMagicNumber = 0xc403;
static const TCHAR messageWindowName[];
std::unique_ptr<HiddenMessageWindow> messageWindow;
CriticalSection lock;
ReferenceCountedArray<MessageManager::MessageBase> messageQueue;
//==============================================================================
JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (InternalMessageQueue)
};
JUCE_IMPLEMENT_SINGLETON (InternalMessageQueue)
const TCHAR InternalMessageQueue::messageWindowName[] = _T("JUCEWindow");
//==============================================================================
bool dispatchNextMessageOnSystemQueue (bool returnIfNoPendingMessages)
{
if (auto* queue = InternalMessageQueue::getInstanceWithoutCreating())
return queue->dispatchNextMessage (returnIfNoPendingMessages);
return false;
}
bool MessageManager::postMessageToSystemQueue (MessageManager::MessageBase* const message)
{
if (auto* queue = InternalMessageQueue::getInstanceWithoutCreating())
{
queue->postMessage (message);
return true;
}
return false;
}
void MessageManager::broadcastMessage (const String& value)
{
if (auto* queue = InternalMessageQueue::getInstanceWithoutCreating())
queue->broadcastMessage (value);
}
//==============================================================================
void MessageManager::doPlatformSpecificInitialisation()
{
ignoreUnused (OleInitialize (nullptr));
InternalMessageQueue::getInstance();
}
void MessageManager::doPlatformSpecificShutdown()
{
InternalMessageQueue::deleteInstance();
OleUninitialize();
}
//==============================================================================
struct MountedVolumeListChangeDetector::Pimpl : private DeviceChangeDetector
{
Pimpl (MountedVolumeListChangeDetector& d) : DeviceChangeDetector (L"MountedVolumeList"), owner (d)
{
File::findFileSystemRoots (lastVolumeList);
}
void systemDeviceChanged() override
{
Array<File> newList;
File::findFileSystemRoots (newList);
if (lastVolumeList != newList)
{
lastVolumeList = newList;
owner.mountedVolumeListChanged();
}
}
MountedVolumeListChangeDetector& owner;
Array<File> lastVolumeList;
};
MountedVolumeListChangeDetector::MountedVolumeListChangeDetector() { pimpl.reset (new Pimpl (*this)); }
MountedVolumeListChangeDetector::~MountedVolumeListChangeDetector() {}
} // namespace juce
/*
==============================================================================
This file is part of the JUCE library.
Copyright (c) 2022 - Raw Material Software Limited
JUCE is an open source library subject to commercial or open-source
licensing.
The code included in this file is provided under the terms of the ISC license
http://www.isc.org/downloads/software-support-policy/isc-license. Permission
To use, copy, modify, and/or distribute this software for any purpose with or
without fee is hereby granted provided that the above copyright notice and
this permission notice appear in all copies.
JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
DISCLAIMED.
==============================================================================
*/
namespace juce
{
extern HWND juce_messageWindowHandle;
#if JUCE_MODULE_AVAILABLE_juce_audio_plugin_client && JucePlugin_Build_Unity
bool juce_isRunningInUnity();
#endif
#if JUCE_MODULE_AVAILABLE_juce_gui_extra
LRESULT juce_offerEventToActiveXControl (::MSG&);
#endif
using CheckEventBlockedByModalComps = bool (*)(const MSG&);
CheckEventBlockedByModalComps isEventBlockedByModalComps = nullptr;
using SettingChangeCallbackFunc = void (*)(void);
SettingChangeCallbackFunc settingChangeCallback = nullptr;
//==============================================================================
class InternalMessageQueue
{
public:
InternalMessageQueue()
{
messageWindow = std::make_unique<HiddenMessageWindow> (messageWindowName, (WNDPROC) messageWndProc);
juce_messageWindowHandle = messageWindow->getHWND();
}
~InternalMessageQueue()
{
juce_messageWindowHandle = nullptr;
clearSingletonInstance();
}
JUCE_DECLARE_SINGLETON (InternalMessageQueue, false)
//==============================================================================
void broadcastMessage (const String& message)
{
auto localCopy = message;
Array<HWND> windows;
EnumWindows (&broadcastEnumWindowProc, (LPARAM) &windows);
for (int i = windows.size(); --i >= 0;)
{
COPYDATASTRUCT data;
data.dwData = broadcastMessageMagicNumber;
data.cbData = (DWORD) (((size_t) localCopy.length() + 1) * sizeof (CharPointer_UTF32::CharType));
data.lpData = (void*) localCopy.toUTF32().getAddress();
DWORD_PTR result;
SendMessageTimeout (windows.getUnchecked (i), WM_COPYDATA,
(WPARAM) juce_messageWindowHandle,
(LPARAM) &data,
SMTO_BLOCK | SMTO_ABORTIFHUNG, 8000, &result);
}
}
void postMessage (MessageManager::MessageBase* message)
{
bool shouldTriggerMessageQueueDispatch = false;
{
const ScopedLock sl (lock);
shouldTriggerMessageQueueDispatch = messageQueue.isEmpty();
messageQueue.add (message);
}
if (! shouldTriggerMessageQueueDispatch)
return;
#if JUCE_MODULE_AVAILABLE_juce_audio_plugin_client && JucePlugin_Build_Unity
if (juce_isRunningInUnity())
{
SendNotifyMessage (juce_messageWindowHandle, customMessageID, 0, 0);
return;
}
#endif
PostMessage (juce_messageWindowHandle, customMessageID, 0, 0);
}
bool dispatchNextMessage (bool returnIfNoPendingMessages)
{
MSG m;
if (returnIfNoPendingMessages && ! PeekMessage (&m, nullptr, 0, 0, PM_NOREMOVE))
return false;
if (GetMessage (&m, nullptr, 0, 0) >= 0)
{
#if JUCE_MODULE_AVAILABLE_juce_gui_extra
if (juce_offerEventToActiveXControl (m) != S_FALSE)
return true;
#endif
if (m.message == customMessageID && m.hwnd == juce_messageWindowHandle)
{
dispatchMessages();
}
else if (m.message == WM_QUIT)
{
if (auto* app = JUCEApplicationBase::getInstance())
app->systemRequestedQuit();
}
else if (isEventBlockedByModalComps == nullptr || ! isEventBlockedByModalComps (m))
{
if ((m.message == WM_LBUTTONDOWN || m.message == WM_RBUTTONDOWN)
&& ! JuceWindowIdentifier::isJUCEWindow (m.hwnd))
{
// if it's someone else's window being clicked on, and the focus is
// currently on a juce window, pass the kb focus over..
auto currentFocus = GetFocus();
if (currentFocus == nullptr || JuceWindowIdentifier::isJUCEWindow (currentFocus))
SetFocus (m.hwnd);
}
TranslateMessage (&m);
DispatchMessage (&m);
}
}
return true;
}
private:
//==============================================================================
static LRESULT CALLBACK messageWndProc (HWND h, UINT message, WPARAM wParam, LPARAM lParam) noexcept
{
if (h == juce_messageWindowHandle)
{
if (message == customMessageID)
{
if (auto* queue = InternalMessageQueue::getInstanceWithoutCreating())
queue->dispatchMessages();
return 0;
}
if (message == WM_COPYDATA)
{
handleBroadcastMessage (reinterpret_cast<const COPYDATASTRUCT*> (lParam));
return 0;
}
if (message == WM_SETTINGCHANGE)
if (settingChangeCallback != nullptr)
settingChangeCallback();
}
return DefWindowProc (h, message, wParam, lParam);
}
static BOOL CALLBACK broadcastEnumWindowProc (HWND hwnd, LPARAM lParam)
{
if (hwnd != juce_messageWindowHandle)
{
TCHAR windowName[64] = { 0 }; // no need to read longer strings than this
GetWindowText (hwnd, windowName, 63);
if (String (windowName) == messageWindowName)
reinterpret_cast<Array<HWND>*> (lParam)->add (hwnd);
}
return TRUE;
}
static void dispatchMessage (MessageManager::MessageBase* message)
{
JUCE_TRY
{
message->messageCallback();
}
JUCE_CATCH_EXCEPTION
message->decReferenceCount();
}
static void handleBroadcastMessage (const COPYDATASTRUCT* data)
{
if (data != nullptr && data->dwData == broadcastMessageMagicNumber)
{
struct BroadcastMessage : public CallbackMessage
{
BroadcastMessage (CharPointer_UTF32 text, size_t length) : message (text, length) {}
void messageCallback() override { MessageManager::getInstance()->deliverBroadcastMessage (message); }
String message;
};
(new BroadcastMessage (CharPointer_UTF32 ((const CharPointer_UTF32::CharType*) data->lpData),
data->cbData / sizeof (CharPointer_UTF32::CharType)))
->post();
}
}
void dispatchMessages()
{
ReferenceCountedArray<MessageManager::MessageBase> messagesToDispatch;
{
const ScopedLock sl (lock);
if (messageQueue.isEmpty())
return;
messagesToDispatch.swapWith (messageQueue);
}
for (int i = 0; i < messagesToDispatch.size(); ++i)
{
auto message = messagesToDispatch.getUnchecked (i);
message->incReferenceCount();
dispatchMessage (message.get());
}
}
//==============================================================================
static constexpr unsigned int customMessageID = WM_USER + 123;
static constexpr unsigned int broadcastMessageMagicNumber = 0xc403;
static const TCHAR messageWindowName[];
std::unique_ptr<HiddenMessageWindow> messageWindow;
CriticalSection lock;
ReferenceCountedArray<MessageManager::MessageBase> messageQueue;
//==============================================================================
JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (InternalMessageQueue)
};
JUCE_IMPLEMENT_SINGLETON (InternalMessageQueue)
const TCHAR InternalMessageQueue::messageWindowName[] = _T("JUCEWindow");
//==============================================================================
bool dispatchNextMessageOnSystemQueue (bool returnIfNoPendingMessages)
{
if (auto* queue = InternalMessageQueue::getInstanceWithoutCreating())
return queue->dispatchNextMessage (returnIfNoPendingMessages);
return false;
}
bool MessageManager::postMessageToSystemQueue (MessageManager::MessageBase* const message)
{
if (auto* queue = InternalMessageQueue::getInstanceWithoutCreating())
{
queue->postMessage (message);
return true;
}
return false;
}
void MessageManager::broadcastMessage (const String& value)
{
if (auto* queue = InternalMessageQueue::getInstanceWithoutCreating())
queue->broadcastMessage (value);
}
//==============================================================================
void MessageManager::doPlatformSpecificInitialisation()
{
ignoreUnused (OleInitialize (nullptr));
InternalMessageQueue::getInstance();
}
void MessageManager::doPlatformSpecificShutdown()
{
InternalMessageQueue::deleteInstance();
OleUninitialize();
}
//==============================================================================
struct MountedVolumeListChangeDetector::Pimpl : private DeviceChangeDetector
{
Pimpl (MountedVolumeListChangeDetector& d) : DeviceChangeDetector (L"MountedVolumeList"), owner (d)
{
File::findFileSystemRoots (lastVolumeList);
}
void systemDeviceChanged() override
{
Array<File> newList;
File::findFileSystemRoots (newList);
if (lastVolumeList != newList)
{
lastVolumeList = newList;
owner.mountedVolumeListChanged();
}
}
MountedVolumeListChangeDetector& owner;
Array<File> lastVolumeList;
};
MountedVolumeListChangeDetector::MountedVolumeListChangeDetector() { pimpl.reset (new Pimpl (*this)); }
MountedVolumeListChangeDetector::~MountedVolumeListChangeDetector() {}
} // namespace juce

View File

@ -1,82 +1,82 @@
/*
==============================================================================
This file is part of the JUCE library.
Copyright (c) 2020 - Raw Material Software Limited
JUCE is an open source library subject to commercial or open-source
licensing.
The code included in this file is provided under the terms of the ISC license
http://www.isc.org/downloads/software-support-policy/isc-license. Permission
To use, copy, modify, and/or distribute this software for any purpose with or
without fee is hereby granted provided that the above copyright notice and
this permission notice appear in all copies.
JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
DISCLAIMED.
==============================================================================
*/
namespace juce
{
WinRTWrapper::WinRTWrapper()
{
winRTHandle = ::LoadLibraryA ("api-ms-win-core-winrt-l1-1-0");
if (winRTHandle == nullptr)
return;
roInitialize = (RoInitializeFuncPtr) ::GetProcAddress (winRTHandle, "RoInitialize");
createHString = (WindowsCreateStringFuncPtr) ::GetProcAddress (winRTHandle, "WindowsCreateString");
deleteHString = (WindowsDeleteStringFuncPtr) ::GetProcAddress (winRTHandle, "WindowsDeleteString");
getHStringRawBuffer = (WindowsGetStringRawBufferFuncPtr) ::GetProcAddress (winRTHandle, "WindowsGetStringRawBuffer");
roActivateInstance = (RoActivateInstanceFuncPtr) ::GetProcAddress (winRTHandle, "RoActivateInstance");
roGetActivationFactory = (RoGetActivationFactoryFuncPtr) ::GetProcAddress (winRTHandle, "RoGetActivationFactory");
if (roInitialize == nullptr || createHString == nullptr || deleteHString == nullptr
|| getHStringRawBuffer == nullptr || roActivateInstance == nullptr || roGetActivationFactory == nullptr)
return;
HRESULT status = roInitialize (1);
initialised = ! (status != S_OK && status != S_FALSE && status != 0x80010106L);
}
WinRTWrapper::~WinRTWrapper()
{
if (winRTHandle != nullptr)
::FreeLibrary (winRTHandle);
clearSingletonInstance();
}
WinRTWrapper::ScopedHString::ScopedHString (String str)
{
if (WinRTWrapper::getInstance()->isInitialised())
WinRTWrapper::getInstance()->createHString (str.toWideCharPointer(),
static_cast<uint32_t> (str.length()),
&hstr);
}
WinRTWrapper::ScopedHString::~ScopedHString()
{
if (WinRTWrapper::getInstance()->isInitialised() && hstr != nullptr)
WinRTWrapper::getInstance()->deleteHString (hstr);
}
String WinRTWrapper::hStringToString (HSTRING hstr)
{
if (isInitialised())
if (const wchar_t* str = getHStringRawBuffer (hstr, nullptr))
return String (str);
return {};
}
JUCE_IMPLEMENT_SINGLETON (WinRTWrapper)
}
/*
==============================================================================
This file is part of the JUCE library.
Copyright (c) 2022 - Raw Material Software Limited
JUCE is an open source library subject to commercial or open-source
licensing.
The code included in this file is provided under the terms of the ISC license
http://www.isc.org/downloads/software-support-policy/isc-license. Permission
To use, copy, modify, and/or distribute this software for any purpose with or
without fee is hereby granted provided that the above copyright notice and
this permission notice appear in all copies.
JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
DISCLAIMED.
==============================================================================
*/
namespace juce
{
WinRTWrapper::WinRTWrapper()
{
winRTHandle = ::LoadLibraryA ("api-ms-win-core-winrt-l1-1-0");
if (winRTHandle == nullptr)
return;
roInitialize = (RoInitializeFuncPtr) ::GetProcAddress (winRTHandle, "RoInitialize");
createHString = (WindowsCreateStringFuncPtr) ::GetProcAddress (winRTHandle, "WindowsCreateString");
deleteHString = (WindowsDeleteStringFuncPtr) ::GetProcAddress (winRTHandle, "WindowsDeleteString");
getHStringRawBuffer = (WindowsGetStringRawBufferFuncPtr) ::GetProcAddress (winRTHandle, "WindowsGetStringRawBuffer");
roActivateInstance = (RoActivateInstanceFuncPtr) ::GetProcAddress (winRTHandle, "RoActivateInstance");
roGetActivationFactory = (RoGetActivationFactoryFuncPtr) ::GetProcAddress (winRTHandle, "RoGetActivationFactory");
if (roInitialize == nullptr || createHString == nullptr || deleteHString == nullptr
|| getHStringRawBuffer == nullptr || roActivateInstance == nullptr || roGetActivationFactory == nullptr)
return;
HRESULT status = roInitialize (1);
initialised = ! (status != S_OK && status != S_FALSE && status != 0x80010106L);
}
WinRTWrapper::~WinRTWrapper()
{
if (winRTHandle != nullptr)
::FreeLibrary (winRTHandle);
clearSingletonInstance();
}
WinRTWrapper::ScopedHString::ScopedHString (String str)
{
if (WinRTWrapper::getInstance()->isInitialised())
WinRTWrapper::getInstance()->createHString (str.toWideCharPointer(),
static_cast<uint32_t> (str.length()),
&hstr);
}
WinRTWrapper::ScopedHString::~ScopedHString()
{
if (WinRTWrapper::getInstance()->isInitialised() && hstr != nullptr)
WinRTWrapper::getInstance()->deleteHString (hstr);
}
String WinRTWrapper::hStringToString (HSTRING hstr)
{
if (isInitialised())
if (const wchar_t* str = getHStringRawBuffer (hstr, nullptr))
return String (str);
return {};
}
JUCE_IMPLEMENT_SINGLETON (WinRTWrapper)
}

View File

@ -1,112 +1,112 @@
/*
==============================================================================
This file is part of the JUCE library.
Copyright (c) 2020 - Raw Material Software Limited
JUCE is an open source library subject to commercial or open-source
licensing.
The code included in this file is provided under the terms of the ISC license
http://www.isc.org/downloads/software-support-policy/isc-license. Permission
To use, copy, modify, and/or distribute this software for any purpose with or
without fee is hereby granted provided that the above copyright notice and
this permission notice appear in all copies.
JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
DISCLAIMED.
==============================================================================
*/
namespace juce
{
class WinRTWrapper : public DeletedAtShutdown
{
public:
//==============================================================================
~WinRTWrapper();
bool isInitialised() const noexcept { return initialised; }
JUCE_DECLARE_SINGLETON (WinRTWrapper, true)
//==============================================================================
template <class ComClass>
ComSmartPtr<ComClass> activateInstance (const wchar_t* runtimeClassID, REFCLSID classUUID)
{
ComSmartPtr<ComClass> result;
if (isInitialised())
{
ComSmartPtr<IInspectable> inspectable;
ScopedHString runtimeClass (runtimeClassID);
auto hr = roActivateInstance (runtimeClass.get(), inspectable.resetAndGetPointerAddress());
if (SUCCEEDED (hr))
inspectable->QueryInterface (classUUID, (void**) result.resetAndGetPointerAddress());
}
return result;
}
template <class ComClass>
ComSmartPtr<ComClass> getWRLFactory (const wchar_t* runtimeClassID)
{
ComSmartPtr<ComClass> comPtr;
if (isInitialised())
{
ScopedHString classID (runtimeClassID);
if (classID.get() != nullptr)
roGetActivationFactory (classID.get(), __uuidof (ComClass), (void**) comPtr.resetAndGetPointerAddress());
}
return comPtr;
}
//==============================================================================
class ScopedHString
{
public:
ScopedHString (String);
~ScopedHString();
HSTRING get() const noexcept { return hstr; }
private:
HSTRING hstr = nullptr;
JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (ScopedHString)
};
String hStringToString (HSTRING);
private:
WinRTWrapper();
//==============================================================================
HMODULE winRTHandle = nullptr;
bool initialised = false;
typedef HRESULT (WINAPI* RoInitializeFuncPtr) (int);
typedef HRESULT (WINAPI* WindowsCreateStringFuncPtr) (LPCWSTR, UINT32, HSTRING*);
typedef HRESULT (WINAPI* WindowsDeleteStringFuncPtr) (HSTRING);
typedef PCWSTR (WINAPI* WindowsGetStringRawBufferFuncPtr) (HSTRING, UINT32*);
typedef HRESULT (WINAPI* RoActivateInstanceFuncPtr) (HSTRING, IInspectable**);
typedef HRESULT (WINAPI* RoGetActivationFactoryFuncPtr) (HSTRING, REFIID, void**);
RoInitializeFuncPtr roInitialize = nullptr;
WindowsCreateStringFuncPtr createHString = nullptr;
WindowsDeleteStringFuncPtr deleteHString = nullptr;
WindowsGetStringRawBufferFuncPtr getHStringRawBuffer = nullptr;
RoActivateInstanceFuncPtr roActivateInstance = nullptr;
RoGetActivationFactoryFuncPtr roGetActivationFactory = nullptr;
//==============================================================================
JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (WinRTWrapper)
};
} // namespace juce
/*
==============================================================================
This file is part of the JUCE library.
Copyright (c) 2022 - Raw Material Software Limited
JUCE is an open source library subject to commercial or open-source
licensing.
The code included in this file is provided under the terms of the ISC license
http://www.isc.org/downloads/software-support-policy/isc-license. Permission
To use, copy, modify, and/or distribute this software for any purpose with or
without fee is hereby granted provided that the above copyright notice and
this permission notice appear in all copies.
JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
DISCLAIMED.
==============================================================================
*/
namespace juce
{
class WinRTWrapper : public DeletedAtShutdown
{
public:
//==============================================================================
~WinRTWrapper();
bool isInitialised() const noexcept { return initialised; }
JUCE_DECLARE_SINGLETON (WinRTWrapper, true)
//==============================================================================
template <class ComClass>
ComSmartPtr<ComClass> activateInstance (const wchar_t* runtimeClassID, REFCLSID classUUID)
{
ComSmartPtr<ComClass> result;
if (isInitialised())
{
ComSmartPtr<IInspectable> inspectable;
ScopedHString runtimeClass (runtimeClassID);
auto hr = roActivateInstance (runtimeClass.get(), inspectable.resetAndGetPointerAddress());
if (SUCCEEDED (hr))
inspectable->QueryInterface (classUUID, (void**) result.resetAndGetPointerAddress());
}
return result;
}
template <class ComClass>
ComSmartPtr<ComClass> getWRLFactory (const wchar_t* runtimeClassID)
{
ComSmartPtr<ComClass> comPtr;
if (isInitialised())
{
ScopedHString classID (runtimeClassID);
if (classID.get() != nullptr)
roGetActivationFactory (classID.get(), __uuidof (ComClass), (void**) comPtr.resetAndGetPointerAddress());
}
return comPtr;
}
//==============================================================================
class ScopedHString
{
public:
ScopedHString (String);
~ScopedHString();
HSTRING get() const noexcept { return hstr; }
private:
HSTRING hstr = nullptr;
JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (ScopedHString)
};
String hStringToString (HSTRING);
private:
WinRTWrapper();
//==============================================================================
HMODULE winRTHandle = nullptr;
bool initialised = false;
typedef HRESULT (WINAPI* RoInitializeFuncPtr) (int);
typedef HRESULT (WINAPI* WindowsCreateStringFuncPtr) (LPCWSTR, UINT32, HSTRING*);
typedef HRESULT (WINAPI* WindowsDeleteStringFuncPtr) (HSTRING);
typedef PCWSTR (WINAPI* WindowsGetStringRawBufferFuncPtr) (HSTRING, UINT32*);
typedef HRESULT (WINAPI* RoActivateInstanceFuncPtr) (HSTRING, IInspectable**);
typedef HRESULT (WINAPI* RoGetActivationFactoryFuncPtr) (HSTRING, REFIID, void**);
RoInitializeFuncPtr roInitialize = nullptr;
WindowsCreateStringFuncPtr createHString = nullptr;
WindowsDeleteStringFuncPtr deleteHString = nullptr;
WindowsGetStringRawBufferFuncPtr getHStringRawBuffer = nullptr;
RoActivateInstanceFuncPtr roActivateInstance = nullptr;
RoGetActivationFactoryFuncPtr roGetActivationFactory = nullptr;
//==============================================================================
JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (WinRTWrapper)
};
} // namespace juce