The JUCE cross-platform C++ framework, with DISTRHO/KXStudio specific changes
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

166 lines
4.8KB

  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, JavaMethod.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_DECLARE_SINGLETON (Handler, false)
  48. Handler() : nativeHandler (getEnv()->NewObject (JNIHandler, JNIHandler.constructor)) {}
  49. bool post (jobject runnable)
  50. {
  51. return (getEnv()->CallBooleanMethod (nativeHandler.get(), JNIHandler.post, runnable) != 0);
  52. }
  53. GlobalRef nativeHandler;
  54. };
  55. JUCE_IMPLEMENT_SINGLETON (Handler)
  56. }
  57. //==============================================================================
  58. struct AndroidMessageQueue : private Android::Runnable
  59. {
  60. JUCE_DECLARE_SINGLETON_SINGLETHREADED (AndroidMessageQueue, true)
  61. AndroidMessageQueue()
  62. : self (CreateJavaInterface (this, "java/lang/Runnable").get())
  63. {
  64. }
  65. ~AndroidMessageQueue()
  66. {
  67. jassert (MessageManager::getInstance()->isThisTheMessageThread());
  68. }
  69. bool post (MessageManager::MessageBase::Ptr&& message)
  70. {
  71. queue.add (static_cast<MessageManager::MessageBase::Ptr&& > (message));
  72. // this will call us on the message thread
  73. return handler.post (self.get());
  74. }
  75. private:
  76. void run() override
  77. {
  78. while (true)
  79. {
  80. MessageManager::MessageBase::Ptr message (queue.removeAndReturn (0));
  81. if (message == nullptr)
  82. break;
  83. message->messageCallback();
  84. }
  85. }
  86. // the this pointer to this class in Java land
  87. GlobalRef self;
  88. ReferenceCountedArray<MessageManager::MessageBase, CriticalSection> queue;
  89. Android::Handler handler;
  90. };
  91. JUCE_IMPLEMENT_SINGLETON (AndroidMessageQueue)
  92. //==============================================================================
  93. void MessageManager::doPlatformSpecificInitialisation() { AndroidMessageQueue::getInstance(); }
  94. void MessageManager::doPlatformSpecificShutdown() { AndroidMessageQueue::deleteInstance(); }
  95. //==============================================================================
  96. bool MessageManager::dispatchNextMessageOnSystemQueue (const bool)
  97. {
  98. Logger::outputDebugString ("*** Modal loops are not possible in Android!! Exiting...");
  99. exit (1);
  100. return true;
  101. }
  102. bool MessageManager::postMessageToSystemQueue (MessageManager::MessageBase* const message)
  103. {
  104. return AndroidMessageQueue::getInstance()->post (message);
  105. }
  106. //==============================================================================
  107. void MessageManager::broadcastMessage (const String&)
  108. {
  109. }
  110. void MessageManager::runDispatchLoop()
  111. {
  112. }
  113. void MessageManager::stopDispatchLoop()
  114. {
  115. struct QuitCallback : public CallbackMessage
  116. {
  117. QuitCallback() {}
  118. void messageCallback() override
  119. {
  120. android.activity.callVoidMethod (JuceAppActivity.finish);
  121. }
  122. };
  123. (new QuitCallback())->post();
  124. quitMessagePosted = true;
  125. }
  126. } // namespace juce