Audio plugin host https://kx.studio/carla
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

148 lines
4.5KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2017 - ROLI Ltd.
  5. JUCE is an open source library subject to commercial or open-source
  6. licensing.
  7. The code included in this file is provided under the terms of the ISC license
  8. http://www.isc.org/downloads/software-support-policy/isc-license. Permission
  9. To use, copy, modify, and/or distribute this software for any purpose with or
  10. without fee is hereby granted provided that the above copyright notice and
  11. this permission notice appear in all copies.
  12. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  13. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  14. DISCLAIMED.
  15. ==============================================================================
  16. */
  17. namespace juce
  18. {
  19. #define JNI_CLASS_MEMBERS(METHOD, STATICMETHOD, FIELD, STATICFIELD) \
  20. METHOD (constructor, "<init>", "()V") \
  21. METHOD (post, "post", "(Ljava/lang/Runnable;)Z") \
  22. DECLARE_JNI_CLASS (JNIHandler, "android/os/Handler");
  23. #undef JNI_CLASS_MEMBERS
  24. //==============================================================================
  25. namespace Android
  26. {
  27. class Runnable : public juce::AndroidInterfaceImplementer
  28. {
  29. public:
  30. virtual void run() = 0;
  31. private:
  32. jobject invoke (jobject proxy, jobject method, jobjectArray args) override
  33. {
  34. auto* env = getEnv();
  35. auto methodName = juce::juceString ((jstring) env->CallObjectMethod (method, Method.getName));
  36. if (methodName == "run")
  37. {
  38. run();
  39. return nullptr;
  40. }
  41. // invoke base class
  42. return AndroidInterfaceImplementer::invoke (proxy, method, args);
  43. }
  44. };
  45. struct Handler
  46. {
  47. juce_DeclareSingleton (Handler, false)
  48. Handler() : nativeHandler (getEnv()->NewObject (JNIHandler, JNIHandler.constructor)) {}
  49. bool post (Runnable* runnable)
  50. {
  51. return (getEnv()->CallBooleanMethod (nativeHandler.get(), JNIHandler.post,
  52. CreateJavaInterface (runnable, "java/lang/Runnable").get()) != 0);
  53. }
  54. GlobalRef nativeHandler;
  55. };
  56. juce_ImplementSingleton (Handler);
  57. }
  58. //==============================================================================
  59. void MessageManager::doPlatformSpecificInitialisation() { Android::Handler::getInstance(); }
  60. void MessageManager::doPlatformSpecificShutdown() {}
  61. //==============================================================================
  62. bool MessageManager::dispatchNextMessageOnSystemQueue (const bool)
  63. {
  64. Logger::outputDebugString ("*** Modal loops are not possible in Android!! Exiting...");
  65. exit (1);
  66. return true;
  67. }
  68. //==============================================================================
  69. struct AndroidMessageCallback : public Android::Runnable
  70. {
  71. AndroidMessageCallback (const MessageManager::MessageBase::Ptr& messageToDeliver)
  72. : message (messageToDeliver)
  73. {}
  74. AndroidMessageCallback (MessageManager::MessageBase::Ptr && messageToDeliver)
  75. : message (static_cast<MessageManager::MessageBase::Ptr&&> (messageToDeliver))
  76. {}
  77. void run() override
  78. {
  79. JUCE_TRY
  80. {
  81. message->messageCallback();
  82. // delete the message already here as Java will only run the
  83. // destructor of this runnable the next time the garbage
  84. // collector kicks in.
  85. message = nullptr;
  86. }
  87. JUCE_CATCH_EXCEPTION
  88. }
  89. MessageManager::MessageBase::Ptr message;
  90. };
  91. bool MessageManager::postMessageToSystemQueue (MessageManager::MessageBase* const message)
  92. {
  93. return Android::Handler::getInstance()->post (new AndroidMessageCallback (message));
  94. }
  95. //==============================================================================
  96. void MessageManager::broadcastMessage (const String&)
  97. {
  98. }
  99. void MessageManager::runDispatchLoop()
  100. {
  101. }
  102. void MessageManager::stopDispatchLoop()
  103. {
  104. struct QuitCallback : public CallbackMessage
  105. {
  106. QuitCallback() {}
  107. void messageCallback() override
  108. {
  109. android.activity.callVoidMethod (JuceAppActivity.finish);
  110. }
  111. };
  112. (new QuitCallback())->post();
  113. quitMessagePosted = true;
  114. }
  115. } // namespace juce