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.

1067 lines
41KB

  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. By using JUCE, you agree to the terms of both the JUCE 5 End-User License
  8. Agreement and JUCE 5 Privacy Policy (both updated and effective as of the
  9. 27th April 2017).
  10. End User License Agreement: www.juce.com/juce-5-licence
  11. Privacy Policy: www.juce.com/juce-5-privacy-policy
  12. Or: You may also use this code under the terms of the GPL v3 (see
  13. www.gnu.org/licenses).
  14. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  15. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  16. DISCLAIMED.
  17. ==============================================================================
  18. */
  19. } // (juce namespace)
  20. extern juce::JUCEApplicationBase* juce_CreateApplication(); // (from START_JUCE_APPLICATION)
  21. namespace juce
  22. {
  23. //==============================================================================
  24. #if JUCE_IN_APP_PURCHASES && JUCE_MODULE_AVAILABLE_juce_product_unlocking
  25. extern void juce_inAppPurchaseCompleted (void*);
  26. #endif
  27. //==============================================================================
  28. JUCE_JNI_CALLBACK (JUCE_ANDROID_ACTIVITY_CLASSNAME, launchApp, void, (JNIEnv* env, jobject activity,
  29. jstring appFile, jstring appDataDir))
  30. {
  31. setEnv (env);
  32. android.initialise (env, activity, appFile, appDataDir);
  33. DBG (SystemStats::getJUCEVersion());
  34. JUCEApplicationBase::createInstance = &juce_CreateApplication;
  35. initialiseJuce_GUI();
  36. if (JUCEApplicationBase* app = JUCEApplicationBase::createInstance())
  37. {
  38. if (! app->initialiseApp())
  39. exit (app->shutdownApp());
  40. }
  41. else
  42. {
  43. jassertfalse; // you must supply an application object for an android app!
  44. }
  45. jassert (MessageManager::getInstance()->isThisTheMessageThread());
  46. }
  47. JUCE_JNI_CALLBACK (JUCE_ANDROID_ACTIVITY_CLASSNAME, suspendApp, void, (JNIEnv* env, jobject))
  48. {
  49. setEnv (env);
  50. if (JUCEApplicationBase* const app = JUCEApplicationBase::getInstance())
  51. app->suspended();
  52. }
  53. JUCE_JNI_CALLBACK (JUCE_ANDROID_ACTIVITY_CLASSNAME, resumeApp, void, (JNIEnv* env, jobject))
  54. {
  55. setEnv (env);
  56. if (JUCEApplicationBase* const app = JUCEApplicationBase::getInstance())
  57. app->resumed();
  58. }
  59. JUCE_JNI_CALLBACK (JUCE_ANDROID_ACTIVITY_CLASSNAME, quitApp, void, (JNIEnv* env, jobject))
  60. {
  61. setEnv (env);
  62. JUCEApplicationBase::appWillTerminateByForce();
  63. android.shutdown (env);
  64. }
  65. JUCE_JNI_CALLBACK (JUCE_ANDROID_ACTIVITY_CLASSNAME, appActivityResult, void, (JNIEnv* env, jobject, jint requestCode, jint /*resultCode*/, jobject intentData))
  66. {
  67. setEnv (env);
  68. #if JUCE_IN_APP_PURCHASES && JUCE_MODULE_AVAILABLE_juce_product_unlocking
  69. if (requestCode == 1001)
  70. juce_inAppPurchaseCompleted (intentData);
  71. #else
  72. ignoreUnused (intentData, requestCode);
  73. #endif
  74. }
  75. //==============================================================================
  76. #define JNI_CLASS_MEMBERS(METHOD, STATICMETHOD, FIELD, STATICFIELD) \
  77. METHOD (drawBitmap, "drawBitmap", "([IIIFFIIZLandroid/graphics/Paint;)V") \
  78. METHOD (getClipBounds, "getClipBounds", "()Landroid/graphics/Rect;")
  79. DECLARE_JNI_CLASS (CanvasMinimal, "android/graphics/Canvas");
  80. #undef JNI_CLASS_MEMBERS
  81. //==============================================================================
  82. #define JNI_CLASS_MEMBERS(METHOD, STATICMETHOD, FIELD, STATICFIELD) \
  83. METHOD (setViewName, "setViewName", "(Ljava/lang/String;)V") \
  84. METHOD (layout, "layout", "(IIII)V") \
  85. METHOD (getLeft, "getLeft", "()I") \
  86. METHOD (getTop, "getTop", "()I") \
  87. METHOD (getWidth, "getWidth", "()I") \
  88. METHOD (getHeight, "getHeight", "()I") \
  89. METHOD (getLocationOnScreen, "getLocationOnScreen", "([I)V") \
  90. METHOD (bringToFront, "bringToFront", "()V") \
  91. METHOD (requestFocus, "requestFocus", "()Z") \
  92. METHOD (setVisible, "setVisible", "(Z)V") \
  93. METHOD (isVisible, "isVisible", "()Z") \
  94. METHOD (hasFocus, "hasFocus", "()Z") \
  95. METHOD (invalidate, "invalidate", "(IIII)V") \
  96. METHOD (containsPoint, "containsPoint", "(II)Z") \
  97. METHOD (showKeyboard, "showKeyboard", "(Ljava/lang/String;)V") \
  98. METHOD (setSystemUiVisibility, "setSystemUiVisibilityCompat", "(I)V") \
  99. DECLARE_JNI_CLASS (ComponentPeerView, JUCE_ANDROID_ACTIVITY_CLASSPATH "$ComponentPeerView");
  100. #undef JNI_CLASS_MEMBERS
  101. //==============================================================================
  102. class AndroidComponentPeer : public ComponentPeer,
  103. private Timer
  104. {
  105. public:
  106. AndroidComponentPeer (Component& comp, const int windowStyleFlags)
  107. : ComponentPeer (comp, windowStyleFlags),
  108. fullScreen (false),
  109. sizeAllocated (0),
  110. scale ((float) Desktop::getInstance().getDisplays().getMainDisplay().scale)
  111. {
  112. // NB: must not put this in the initialiser list, as it invokes a callback,
  113. // which will fail if the peer is only half-constructed.
  114. view = GlobalRef (android.activity.callObjectMethod (JuceAppActivity.createNewView,
  115. (jboolean) component.isOpaque(),
  116. (jlong) this));
  117. if (isFocused())
  118. handleFocusGain();
  119. }
  120. ~AndroidComponentPeer()
  121. {
  122. if (MessageManager::getInstance()->isThisTheMessageThread())
  123. {
  124. frontWindow = nullptr;
  125. android.activity.callVoidMethod (JuceAppActivity.deleteView, view.get());
  126. }
  127. else
  128. {
  129. struct ViewDeleter : public CallbackMessage
  130. {
  131. ViewDeleter (const GlobalRef& view_) : view (view_) {}
  132. void messageCallback() override
  133. {
  134. android.activity.callVoidMethod (JuceAppActivity.deleteView, view.get());
  135. }
  136. private:
  137. GlobalRef view;
  138. };
  139. (new ViewDeleter (view))->post();
  140. }
  141. view.clear();
  142. }
  143. void* getNativeHandle() const override
  144. {
  145. return (void*) view.get();
  146. }
  147. void setVisible (bool shouldBeVisible) override
  148. {
  149. if (MessageManager::getInstance()->isThisTheMessageThread())
  150. {
  151. view.callVoidMethod (ComponentPeerView.setVisible, shouldBeVisible);
  152. }
  153. else
  154. {
  155. struct VisibilityChanger : public CallbackMessage
  156. {
  157. VisibilityChanger (const GlobalRef& view_, bool shouldBeVisible_)
  158. : view (view_), shouldBeVisible (shouldBeVisible_)
  159. {}
  160. void messageCallback() override
  161. {
  162. view.callVoidMethod (ComponentPeerView.setVisible, shouldBeVisible);
  163. }
  164. GlobalRef view;
  165. bool shouldBeVisible;
  166. };
  167. (new VisibilityChanger (view, shouldBeVisible))->post();
  168. }
  169. }
  170. void setTitle (const String& title) override
  171. {
  172. view.callVoidMethod (ComponentPeerView.setViewName, javaString (title).get());
  173. }
  174. void setBounds (const Rectangle<int>& userRect, bool isNowFullScreen) override
  175. {
  176. Rectangle<int> r = (userRect.toFloat() * scale).toNearestInt();
  177. if (MessageManager::getInstance()->isThisTheMessageThread())
  178. {
  179. fullScreen = isNowFullScreen;
  180. view.callVoidMethod (ComponentPeerView.layout,
  181. r.getX(), r.getY(), r.getRight(), r.getBottom());
  182. }
  183. else
  184. {
  185. class ViewMover : public CallbackMessage
  186. {
  187. public:
  188. ViewMover (const GlobalRef& v, const Rectangle<int>& boundsToUse) : view (v), bounds (boundsToUse) {}
  189. void messageCallback() override
  190. {
  191. view.callVoidMethod (ComponentPeerView.layout,
  192. bounds.getX(), bounds.getY(), bounds.getRight(), bounds.getBottom());
  193. }
  194. private:
  195. GlobalRef view;
  196. Rectangle<int> bounds;
  197. };
  198. (new ViewMover (view, r))->post();
  199. }
  200. }
  201. Rectangle<int> getBounds() const override
  202. {
  203. return (Rectangle<float> (view.callIntMethod (ComponentPeerView.getLeft),
  204. view.callIntMethod (ComponentPeerView.getTop),
  205. view.callIntMethod (ComponentPeerView.getWidth),
  206. view.callIntMethod (ComponentPeerView.getHeight)) / scale).toNearestInt();
  207. }
  208. void handleScreenSizeChange() override
  209. {
  210. ComponentPeer::handleScreenSizeChange();
  211. if (isFullScreen())
  212. setFullScreen (true);
  213. }
  214. Point<float> getScreenPosition() const
  215. {
  216. return Point<float> (view.callIntMethod (ComponentPeerView.getLeft),
  217. view.callIntMethod (ComponentPeerView.getTop)) / scale;
  218. }
  219. Point<float> localToGlobal (Point<float> relativePosition) override
  220. {
  221. return relativePosition + getScreenPosition();
  222. }
  223. Point<float> globalToLocal (Point<float> screenPosition) override
  224. {
  225. return screenPosition - getScreenPosition();
  226. }
  227. void setMinimised (bool /*shouldBeMinimised*/) override
  228. {
  229. // n/a
  230. }
  231. bool isMinimised() const override
  232. {
  233. return false;
  234. }
  235. bool shouldNavBarsBeHidden() const
  236. {
  237. if (fullScreen)
  238. if (Component* kiosk = Desktop::getInstance().getKioskModeComponent())
  239. if (kiosk->getPeer() == this)
  240. return true;
  241. return false;
  242. }
  243. void setNavBarsHidden (bool hidden) const
  244. {
  245. enum
  246. {
  247. SYSTEM_UI_FLAG_VISIBLE = 0,
  248. SYSTEM_UI_FLAG_LOW_PROFILE = 1,
  249. SYSTEM_UI_FLAG_HIDE_NAVIGATION = 2,
  250. SYSTEM_UI_FLAG_FULLSCREEN = 4,
  251. SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION = 512,
  252. SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN = 1024,
  253. SYSTEM_UI_FLAG_IMMERSIVE = 2048,
  254. SYSTEM_UI_FLAG_IMMERSIVE_STICKY = 4096
  255. };
  256. view.callVoidMethod (ComponentPeerView.setSystemUiVisibility,
  257. hidden ? (jint) (SYSTEM_UI_FLAG_HIDE_NAVIGATION | SYSTEM_UI_FLAG_FULLSCREEN | SYSTEM_UI_FLAG_IMMERSIVE_STICKY)
  258. : (jint) (SYSTEM_UI_FLAG_VISIBLE));
  259. }
  260. void setFullScreen (bool shouldBeFullScreen) override
  261. {
  262. // updating the nav bar visibility is a bit odd on Android - need to wait for
  263. if (shouldNavBarsBeHidden())
  264. {
  265. if (! isTimerRunning())
  266. startTimer (500);
  267. }
  268. else
  269. setNavBarsHidden (false);
  270. Rectangle<int> r (shouldBeFullScreen ? Desktop::getInstance().getDisplays().getMainDisplay().userArea
  271. : lastNonFullscreenBounds);
  272. if ((! shouldBeFullScreen) && r.isEmpty())
  273. r = getBounds();
  274. // (can't call the component's setBounds method because that'll reset our fullscreen flag)
  275. if (! r.isEmpty())
  276. setBounds (r, shouldBeFullScreen);
  277. component.repaint();
  278. }
  279. bool isFullScreen() const override
  280. {
  281. return fullScreen;
  282. }
  283. void timerCallback() override
  284. {
  285. setNavBarsHidden (shouldNavBarsBeHidden());
  286. setFullScreen (fullScreen);
  287. stopTimer();
  288. }
  289. void setIcon (const Image& /*newIcon*/) override
  290. {
  291. // n/a
  292. }
  293. bool contains (Point<int> localPos, bool trueIfInAChildWindow) const override
  294. {
  295. return isPositiveAndBelow (localPos.x, component.getWidth())
  296. && isPositiveAndBelow (localPos.y, component.getHeight())
  297. && ((! trueIfInAChildWindow) || view.callBooleanMethod (ComponentPeerView.containsPoint,
  298. localPos.x * scale,
  299. localPos.y * scale));
  300. }
  301. BorderSize<int> getFrameSize() const override
  302. {
  303. // TODO
  304. return BorderSize<int>();
  305. }
  306. bool setAlwaysOnTop (bool /*alwaysOnTop*/) override
  307. {
  308. // TODO
  309. return false;
  310. }
  311. void toFront (bool makeActive) override
  312. {
  313. // Avoid calling bringToFront excessively: it's very slow
  314. if (frontWindow != this)
  315. {
  316. view.callVoidMethod (ComponentPeerView.bringToFront);
  317. frontWindow = this;
  318. }
  319. if (makeActive)
  320. grabFocus();
  321. handleBroughtToFront();
  322. }
  323. void toBehind (ComponentPeer*) override
  324. {
  325. // TODO
  326. }
  327. //==============================================================================
  328. void handleMouseDownCallback (int index, Point<float> sysPos, int64 time)
  329. {
  330. Point<float> pos = sysPos / scale;
  331. lastMousePos = localToGlobal (pos);
  332. // this forces a mouse-enter/up event, in case for some reason we didn't get a mouse-up before.
  333. handleMouseEvent (MouseInputSource::InputSourceType::touch, pos, currentModifiers.withoutMouseButtons(),
  334. MouseInputSource::invalidPressure, MouseInputSource::invalidOrientation, time, {}, index);
  335. if (isValidPeer (this))
  336. handleMouseDragCallback (index, sysPos, time);
  337. }
  338. void handleMouseDragCallback (int index, Point<float> pos, int64 time)
  339. {
  340. pos /= scale;
  341. lastMousePos = localToGlobal (pos);
  342. jassert (index < 64);
  343. touchesDown = (touchesDown | (1 << (index & 63)));
  344. currentModifiers = currentModifiers.withoutMouseButtons().withFlags (ModifierKeys::leftButtonModifier);
  345. handleMouseEvent (MouseInputSource::InputSourceType::touch, pos, currentModifiers.withoutMouseButtons().withFlags (ModifierKeys::leftButtonModifier),
  346. MouseInputSource::invalidPressure, MouseInputSource::invalidOrientation, time, {}, index);
  347. }
  348. void handleMouseUpCallback (int index, Point<float> pos, int64 time)
  349. {
  350. pos /= scale;
  351. lastMousePos = localToGlobal (pos);
  352. jassert (index < 64);
  353. touchesDown = (touchesDown & ~(1 << (index & 63)));
  354. if (touchesDown == 0)
  355. currentModifiers = currentModifiers.withoutMouseButtons();
  356. handleMouseEvent (MouseInputSource::InputSourceType::touch, pos, currentModifiers.withoutMouseButtons(), MouseInputSource::invalidPressure,
  357. MouseInputSource::invalidOrientation, time, {}, index);
  358. }
  359. void handleKeyDownCallback (int k, int kc)
  360. {
  361. handleKeyPress (k, static_cast<juce_wchar> (kc));
  362. }
  363. void handleKeyUpCallback (int /*k*/, int /*kc*/)
  364. {
  365. }
  366. void handleBackButtonCallback()
  367. {
  368. if (JUCEApplicationBase* const app = JUCEApplicationBase::getInstance())
  369. app->backButtonPressed();
  370. }
  371. //==============================================================================
  372. bool isFocused() const override
  373. {
  374. if (view != nullptr)
  375. return view.callBooleanMethod (ComponentPeerView.hasFocus);
  376. return false;
  377. }
  378. void grabFocus() override
  379. {
  380. if (view != nullptr)
  381. view.callBooleanMethod (ComponentPeerView.requestFocus);
  382. }
  383. void handleFocusChangeCallback (bool hasFocus)
  384. {
  385. if (hasFocus)
  386. handleFocusGain();
  387. else
  388. handleFocusLoss();
  389. }
  390. static const char* getVirtualKeyboardType (TextInputTarget::VirtualKeyboardType type) noexcept
  391. {
  392. switch (type)
  393. {
  394. case TextInputTarget::textKeyboard: return "text";
  395. case TextInputTarget::numericKeyboard: return "number";
  396. case TextInputTarget::decimalKeyboard: return "numberDecimal";
  397. case TextInputTarget::urlKeyboard: return "textUri";
  398. case TextInputTarget::emailAddressKeyboard: return "textEmailAddress";
  399. case TextInputTarget::phoneNumberKeyboard: return "phone";
  400. default: jassertfalse; break;
  401. }
  402. return "text";
  403. }
  404. void textInputRequired (Point<int>, TextInputTarget& target) override
  405. {
  406. view.callVoidMethod (ComponentPeerView.showKeyboard,
  407. javaString (getVirtualKeyboardType (target.getKeyboardType())).get());
  408. }
  409. void dismissPendingTextInput() override
  410. {
  411. view.callVoidMethod (ComponentPeerView.showKeyboard, javaString ("").get());
  412. }
  413. //==============================================================================
  414. void handlePaintCallback (JNIEnv* env, jobject canvas, jobject paint)
  415. {
  416. jobject rect = env->CallObjectMethod (canvas, CanvasMinimal.getClipBounds);
  417. const int left = env->GetIntField (rect, RectClass.left);
  418. const int top = env->GetIntField (rect, RectClass.top);
  419. const int right = env->GetIntField (rect, RectClass.right);
  420. const int bottom = env->GetIntField (rect, RectClass.bottom);
  421. env->DeleteLocalRef (rect);
  422. const Rectangle<int> clip (left, top, right - left, bottom - top);
  423. const int sizeNeeded = clip.getWidth() * clip.getHeight();
  424. if (sizeAllocated < sizeNeeded)
  425. {
  426. buffer.clear();
  427. sizeAllocated = sizeNeeded;
  428. buffer = GlobalRef (env->NewIntArray (sizeNeeded));
  429. }
  430. if (jint* dest = env->GetIntArrayElements ((jintArray) buffer.get(), 0))
  431. {
  432. {
  433. Image temp (new PreallocatedImage (clip.getWidth(), clip.getHeight(),
  434. dest, ! component.isOpaque()));
  435. {
  436. LowLevelGraphicsSoftwareRenderer g (temp);
  437. g.setOrigin (-clip.getPosition());
  438. g.addTransform (AffineTransform::scale (scale));
  439. handlePaint (g);
  440. }
  441. }
  442. env->ReleaseIntArrayElements ((jintArray) buffer.get(), dest, 0);
  443. env->CallVoidMethod (canvas, CanvasMinimal.drawBitmap, (jintArray) buffer.get(), 0, clip.getWidth(),
  444. (jfloat) clip.getX(), (jfloat) clip.getY(),
  445. clip.getWidth(), clip.getHeight(), true, paint);
  446. }
  447. }
  448. void repaint (const Rectangle<int>& userArea) override
  449. {
  450. Rectangle<int> area = userArea * scale;
  451. if (MessageManager::getInstance()->isThisTheMessageThread())
  452. {
  453. view.callVoidMethod (ComponentPeerView.invalidate, area.getX(), area.getY(), area.getRight(), area.getBottom());
  454. }
  455. else
  456. {
  457. struct ViewRepainter : public CallbackMessage
  458. {
  459. ViewRepainter (const GlobalRef& view_, const Rectangle<int>& area_)
  460. : view (view_), area (area_) {}
  461. void messageCallback() override
  462. {
  463. view.callVoidMethod (ComponentPeerView.invalidate, area.getX(), area.getY(),
  464. area.getRight(), area.getBottom());
  465. }
  466. private:
  467. GlobalRef view;
  468. const Rectangle<int> area;
  469. };
  470. (new ViewRepainter (view, area))->post();
  471. }
  472. }
  473. void performAnyPendingRepaintsNow() override
  474. {
  475. // TODO
  476. }
  477. void setAlpha (float /*newAlpha*/) override
  478. {
  479. // TODO
  480. }
  481. StringArray getAvailableRenderingEngines() override
  482. {
  483. return StringArray ("Software Renderer");
  484. }
  485. //==============================================================================
  486. static ModifierKeys currentModifiers;
  487. static Point<float> lastMousePos;
  488. static int64 touchesDown;
  489. private:
  490. //==============================================================================
  491. GlobalRef view;
  492. GlobalRef buffer;
  493. bool fullScreen;
  494. int sizeAllocated;
  495. float scale;
  496. static AndroidComponentPeer* frontWindow;
  497. struct PreallocatedImage : public ImagePixelData
  498. {
  499. PreallocatedImage (const int width_, const int height_, jint* data_, bool hasAlpha_)
  500. : ImagePixelData (Image::ARGB, width_, height_), data (data_), hasAlpha (hasAlpha_)
  501. {
  502. if (hasAlpha_)
  503. zeromem (data_, static_cast<size_t> (width * height) * sizeof (jint));
  504. }
  505. ~PreallocatedImage()
  506. {
  507. if (hasAlpha)
  508. {
  509. PixelARGB* pix = (PixelARGB*) data;
  510. for (int i = width * height; --i >= 0;)
  511. {
  512. pix->unpremultiply();
  513. ++pix;
  514. }
  515. }
  516. }
  517. ImageType* createType() const override { return new SoftwareImageType(); }
  518. LowLevelGraphicsContext* createLowLevelContext() override { return new LowLevelGraphicsSoftwareRenderer (Image (this)); }
  519. void initialiseBitmapData (Image::BitmapData& bm, int x, int y, Image::BitmapData::ReadWriteMode /*mode*/) override
  520. {
  521. bm.lineStride = width * static_cast<int> (sizeof (jint));
  522. bm.pixelStride = static_cast<int> (sizeof (jint));
  523. bm.pixelFormat = Image::ARGB;
  524. bm.data = (uint8*) (data + x + y * width);
  525. }
  526. ImagePixelData::Ptr clone() override
  527. {
  528. PreallocatedImage* s = new PreallocatedImage (width, height, 0, hasAlpha);
  529. s->allocatedData.malloc (sizeof (jint) * static_cast<size_t> (width * height));
  530. s->data = s->allocatedData;
  531. memcpy (s->data, data, sizeof (jint) * static_cast<size_t> (width * height));
  532. return s;
  533. }
  534. private:
  535. jint* data;
  536. HeapBlock<jint> allocatedData;
  537. bool hasAlpha;
  538. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (PreallocatedImage)
  539. };
  540. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (AndroidComponentPeer)
  541. };
  542. ModifierKeys AndroidComponentPeer::currentModifiers = 0;
  543. Point<float> AndroidComponentPeer::lastMousePos;
  544. int64 AndroidComponentPeer::touchesDown = 0;
  545. AndroidComponentPeer* AndroidComponentPeer::frontWindow = nullptr;
  546. //==============================================================================
  547. #define JUCE_VIEW_CALLBACK(returnType, javaMethodName, params, juceMethodInvocation) \
  548. JUCE_JNI_CALLBACK (JUCE_JOIN_MACRO (JUCE_ANDROID_ACTIVITY_CLASSNAME, _00024ComponentPeerView), javaMethodName, returnType, params) \
  549. { \
  550. setEnv (env); \
  551. if (AndroidComponentPeer* peer = (AndroidComponentPeer*) (pointer_sized_uint) host) \
  552. peer->juceMethodInvocation; \
  553. }
  554. JUCE_VIEW_CALLBACK (void, handlePaint, (JNIEnv* env, jobject /*view*/, jlong host, jobject canvas, jobject paint), handlePaintCallback (env, canvas, paint))
  555. JUCE_VIEW_CALLBACK (void, handleMouseDown, (JNIEnv* env, jobject /*view*/, jlong host, jint i, jfloat x, jfloat y, jlong time), handleMouseDownCallback (i, Point<float> ((float) x, (float) y), (int64) time))
  556. JUCE_VIEW_CALLBACK (void, handleMouseDrag, (JNIEnv* env, jobject /*view*/, jlong host, jint i, jfloat x, jfloat y, jlong time), handleMouseDragCallback (i, Point<float> ((float) x, (float) y), (int64) time))
  557. JUCE_VIEW_CALLBACK (void, handleMouseUp, (JNIEnv* env, jobject /*view*/, jlong host, jint i, jfloat x, jfloat y, jlong time), handleMouseUpCallback (i, Point<float> ((float) x, (float) y), (int64) time))
  558. JUCE_VIEW_CALLBACK (void, viewSizeChanged, (JNIEnv* env, jobject /*view*/, jlong host), handleMovedOrResized())
  559. JUCE_VIEW_CALLBACK (void, focusChanged, (JNIEnv* env, jobject /*view*/, jlong host, jboolean hasFocus), handleFocusChangeCallback (hasFocus))
  560. JUCE_VIEW_CALLBACK (void, handleKeyDown, (JNIEnv* env, jobject /*view*/, jlong host, jint k, jint kc), handleKeyDownCallback ((int) k, (int) kc))
  561. JUCE_VIEW_CALLBACK (void, handleKeyUp, (JNIEnv* env, jobject /*view*/, jlong host, jint k, jint kc), handleKeyUpCallback ((int) k, (int) kc))
  562. JUCE_VIEW_CALLBACK (void, handleBackButton, (JNIEnv* env, jobject /*view*/, jlong host), handleBackButtonCallback())
  563. //==============================================================================
  564. ComponentPeer* Component::createNewPeer (int styleFlags, void*)
  565. {
  566. return new AndroidComponentPeer (*this, styleFlags);
  567. }
  568. //==============================================================================
  569. #define JNI_CLASS_MEMBERS(METHOD, STATICMETHOD, FIELD, STATICFIELD) \
  570. METHOD (getRotation, "getRotation", "()I")
  571. DECLARE_JNI_CLASS (Display, "android/view/Display");
  572. #undef JNI_CLASS_MEMBERS
  573. #define JNI_CLASS_MEMBERS(METHOD, STATICMETHOD, FIELD, STATICFIELD) \
  574. METHOD (getDefaultDisplay, "getDefaultDisplay", "()Landroid/view/Display;")
  575. DECLARE_JNI_CLASS (WindowManager, "android/view/WindowManager");
  576. #undef JNI_CLASS_MEMBERS
  577. bool Desktop::canUseSemiTransparentWindows() noexcept
  578. {
  579. return true;
  580. }
  581. double Desktop::getDefaultMasterScale()
  582. {
  583. return 1.0;
  584. }
  585. Desktop::DisplayOrientation Desktop::getCurrentOrientation() const
  586. {
  587. enum
  588. {
  589. ROTATION_0 = 0,
  590. ROTATION_90 = 1,
  591. ROTATION_180 = 2,
  592. ROTATION_270 = 3
  593. };
  594. JNIEnv* env = getEnv();
  595. LocalRef<jstring> windowServiceString (javaString ("window"));
  596. LocalRef<jobject> windowManager = LocalRef<jobject> (env->CallObjectMethod (android.activity, JuceAppActivity.getSystemService, windowServiceString.get()));
  597. if (windowManager.get() != 0)
  598. {
  599. LocalRef<jobject> display = LocalRef<jobject> (env->CallObjectMethod (windowManager, WindowManager.getDefaultDisplay));
  600. if (display.get() != 0)
  601. {
  602. int rotation = env->CallIntMethod (display, Display.getRotation);
  603. switch (rotation)
  604. {
  605. case ROTATION_0: return upright;
  606. case ROTATION_90: return rotatedAntiClockwise;
  607. case ROTATION_180: return upsideDown;
  608. case ROTATION_270: return rotatedClockwise;
  609. }
  610. }
  611. }
  612. jassertfalse;
  613. return upright;
  614. }
  615. bool MouseInputSource::SourceList::addSource()
  616. {
  617. addSource (sources.size(), MouseInputSource::InputSourceType::touch);
  618. return true;
  619. }
  620. bool MouseInputSource::SourceList::canUseTouch()
  621. {
  622. return true;
  623. }
  624. Point<float> MouseInputSource::getCurrentRawMousePosition()
  625. {
  626. return AndroidComponentPeer::lastMousePos;
  627. }
  628. void MouseInputSource::setRawMousePosition (Point<float>)
  629. {
  630. // not needed
  631. }
  632. //==============================================================================
  633. bool KeyPress::isKeyCurrentlyDown (const int /*keyCode*/)
  634. {
  635. // TODO
  636. return false;
  637. }
  638. void ModifierKeys::updateCurrentModifiers() noexcept
  639. {
  640. currentModifiers = AndroidComponentPeer::currentModifiers;
  641. }
  642. ModifierKeys ModifierKeys::getCurrentModifiersRealtime() noexcept
  643. {
  644. return AndroidComponentPeer::currentModifiers;
  645. }
  646. //==============================================================================
  647. // TODO
  648. JUCE_API bool JUCE_CALLTYPE Process::isForegroundProcess() { return true; }
  649. JUCE_API void JUCE_CALLTYPE Process::makeForegroundProcess() {}
  650. JUCE_API void JUCE_CALLTYPE Process::hide() {}
  651. //==============================================================================
  652. void JUCE_CALLTYPE NativeMessageBox::showMessageBoxAsync (AlertWindow::AlertIconType /*iconType*/,
  653. const String& title, const String& message,
  654. Component* /*associatedComponent*/,
  655. ModalComponentManager::Callback* callback)
  656. {
  657. android.activity.callVoidMethod (JuceAppActivity.showMessageBox, javaString (title).get(),
  658. javaString (message).get(), (jlong) (pointer_sized_int) callback);
  659. }
  660. bool JUCE_CALLTYPE NativeMessageBox::showOkCancelBox (AlertWindow::AlertIconType /*iconType*/,
  661. const String& title, const String& message,
  662. Component* /*associatedComponent*/,
  663. ModalComponentManager::Callback* callback)
  664. {
  665. jassert (callback != nullptr); // on android, all alerts must be non-modal!!
  666. android.activity.callVoidMethod (JuceAppActivity.showOkCancelBox, javaString (title).get(),
  667. javaString (message).get(), (jlong) (pointer_sized_int) callback,
  668. javaString (TRANS ("OK")).get(), javaString (TRANS ("Cancel")).get());
  669. return false;
  670. }
  671. int JUCE_CALLTYPE NativeMessageBox::showYesNoCancelBox (AlertWindow::AlertIconType /*iconType*/,
  672. const String& title, const String& message,
  673. Component* /*associatedComponent*/,
  674. ModalComponentManager::Callback* callback)
  675. {
  676. jassert (callback != nullptr); // on android, all alerts must be non-modal!!
  677. android.activity.callVoidMethod (JuceAppActivity.showYesNoCancelBox, javaString (title).get(),
  678. javaString (message).get(), (jlong) (pointer_sized_int) callback);
  679. return 0;
  680. }
  681. int JUCE_CALLTYPE NativeMessageBox::showYesNoBox (AlertWindow::AlertIconType /*iconType*/,
  682. const String& title, const String& message,
  683. Component* /*associatedComponent*/,
  684. ModalComponentManager::Callback* callback)
  685. {
  686. jassert (callback != nullptr); // on android, all alerts must be non-modal!!
  687. android.activity.callVoidMethod (JuceAppActivity.showOkCancelBox, javaString (title).get(),
  688. javaString (message).get(), (jlong) (pointer_sized_int) callback,
  689. javaString (TRANS ("Yes")).get(), javaString (TRANS ("No")).get());
  690. return 0;
  691. }
  692. JUCE_JNI_CALLBACK (JUCE_ANDROID_ACTIVITY_CLASSNAME, alertDismissed, void, (JNIEnv* env, jobject /*activity*/,
  693. jlong callbackAsLong, jint result))
  694. {
  695. setEnv (env);
  696. if (ModalComponentManager::Callback* callback = (ModalComponentManager::Callback*) callbackAsLong)
  697. {
  698. callback->modalStateFinished (result);
  699. delete callback;
  700. }
  701. }
  702. //==============================================================================
  703. void Desktop::setScreenSaverEnabled (const bool isEnabled)
  704. {
  705. android.activity.callVoidMethod (JuceAppActivity.setScreenSaver, isEnabled);
  706. }
  707. bool Desktop::isScreenSaverEnabled()
  708. {
  709. return android.activity.callBooleanMethod (JuceAppActivity.getScreenSaver);
  710. }
  711. //==============================================================================
  712. void Desktop::setKioskComponent (Component* kioskComp, bool enableOrDisable, bool allowMenusAndBars)
  713. {
  714. ignoreUnused (allowMenusAndBars);
  715. if (AndroidComponentPeer* peer = dynamic_cast<AndroidComponentPeer*> (kioskComp->getPeer()))
  716. peer->setFullScreen (enableOrDisable);
  717. else
  718. jassertfalse; // (this should have been checked by the caller)
  719. }
  720. //==============================================================================
  721. static jint getAndroidOrientationFlag (int orientations) noexcept
  722. {
  723. enum
  724. {
  725. SCREEN_ORIENTATION_LANDSCAPE = 0,
  726. SCREEN_ORIENTATION_PORTRAIT = 1,
  727. SCREEN_ORIENTATION_USER = 2,
  728. SCREEN_ORIENTATION_REVERSE_LANDSCAPE = 8,
  729. SCREEN_ORIENTATION_REVERSE_PORTRAIT = 9,
  730. SCREEN_ORIENTATION_USER_LANDSCAPE = 11,
  731. SCREEN_ORIENTATION_USER_PORTRAIT = 12,
  732. };
  733. switch (orientations)
  734. {
  735. case Desktop::upright: return (jint) SCREEN_ORIENTATION_PORTRAIT;
  736. case Desktop::upsideDown: return (jint) SCREEN_ORIENTATION_REVERSE_PORTRAIT;
  737. case Desktop::upright + Desktop::upsideDown: return (jint) SCREEN_ORIENTATION_USER_PORTRAIT;
  738. case Desktop::rotatedAntiClockwise: return (jint) SCREEN_ORIENTATION_LANDSCAPE;
  739. case Desktop::rotatedClockwise: return (jint) SCREEN_ORIENTATION_REVERSE_LANDSCAPE;
  740. case Desktop::rotatedClockwise + Desktop::rotatedAntiClockwise: return (jint) SCREEN_ORIENTATION_USER_LANDSCAPE;
  741. default: return (jint) SCREEN_ORIENTATION_USER;
  742. }
  743. }
  744. void Desktop::allowedOrientationsChanged()
  745. {
  746. android.activity.callVoidMethod (JuceAppActivity.setRequestedOrientation,
  747. getAndroidOrientationFlag (allowedOrientations));
  748. }
  749. //==============================================================================
  750. bool juce_areThereAnyAlwaysOnTopWindows()
  751. {
  752. return false;
  753. }
  754. //==============================================================================
  755. void Desktop::Displays::findDisplays (float masterScale)
  756. {
  757. Display d;
  758. d.isMain = true;
  759. d.dpi = android.dpi;
  760. d.scale = masterScale * (d.dpi / 150.);
  761. d.userArea = d.totalArea = Rectangle<int> (android.screenWidth,
  762. android.screenHeight) / d.scale;
  763. displays.add (d);
  764. }
  765. JUCE_JNI_CALLBACK (JUCE_ANDROID_ACTIVITY_CLASSNAME, setScreenSize, void, (JNIEnv* env, jobject /*activity*/,
  766. jint screenWidth, jint screenHeight,
  767. jint dpi))
  768. {
  769. setEnv (env);
  770. android.screenWidth = screenWidth;
  771. android.screenHeight = screenHeight;
  772. android.dpi = dpi;
  773. const_cast<Desktop::Displays&> (Desktop::getInstance().getDisplays()).refresh();
  774. }
  775. //==============================================================================
  776. Image juce_createIconForFile (const File& /*file*/)
  777. {
  778. return Image();
  779. }
  780. //==============================================================================
  781. void* CustomMouseCursorInfo::create() const { return nullptr; }
  782. void* MouseCursor::createStandardMouseCursor (const MouseCursor::StandardCursorType) { return nullptr; }
  783. void MouseCursor::deleteMouseCursor (void* const /*cursorHandle*/, const bool /*isStandard*/) {}
  784. //==============================================================================
  785. void MouseCursor::showInWindow (ComponentPeer*) const {}
  786. void MouseCursor::showInAllWindows() const {}
  787. //==============================================================================
  788. bool DragAndDropContainer::performExternalDragDropOfFiles (const StringArray& /*files*/, const bool /*canMove*/,
  789. Component* /*srcComp*/)
  790. {
  791. return false;
  792. }
  793. bool DragAndDropContainer::performExternalDragDropOfText (const String& /*text*/, Component* /*srcComp*/)
  794. {
  795. return false;
  796. }
  797. //==============================================================================
  798. void LookAndFeel::playAlertSound()
  799. {
  800. }
  801. //==============================================================================
  802. void SystemClipboard::copyTextToClipboard (const String& text)
  803. {
  804. const LocalRef<jstring> t (javaString (text));
  805. android.activity.callVoidMethod (JuceAppActivity.setClipboardContent, t.get());
  806. }
  807. String SystemClipboard::getTextFromClipboard()
  808. {
  809. const LocalRef<jstring> text ((jstring) android.activity.callObjectMethod (JuceAppActivity.getClipboardContent));
  810. return juceString (text);
  811. }
  812. //==============================================================================
  813. const int extendedKeyModifier = 0x10000;
  814. const int KeyPress::spaceKey = ' ';
  815. const int KeyPress::returnKey = 66;
  816. const int KeyPress::escapeKey = 4;
  817. const int KeyPress::backspaceKey = 67;
  818. const int KeyPress::leftKey = extendedKeyModifier + 1;
  819. const int KeyPress::rightKey = extendedKeyModifier + 2;
  820. const int KeyPress::upKey = extendedKeyModifier + 3;
  821. const int KeyPress::downKey = extendedKeyModifier + 4;
  822. const int KeyPress::pageUpKey = extendedKeyModifier + 5;
  823. const int KeyPress::pageDownKey = extendedKeyModifier + 6;
  824. const int KeyPress::endKey = extendedKeyModifier + 7;
  825. const int KeyPress::homeKey = extendedKeyModifier + 8;
  826. const int KeyPress::deleteKey = extendedKeyModifier + 9;
  827. const int KeyPress::insertKey = -1;
  828. const int KeyPress::tabKey = 61;
  829. const int KeyPress::F1Key = extendedKeyModifier + 10;
  830. const int KeyPress::F2Key = extendedKeyModifier + 11;
  831. const int KeyPress::F3Key = extendedKeyModifier + 12;
  832. const int KeyPress::F4Key = extendedKeyModifier + 13;
  833. const int KeyPress::F5Key = extendedKeyModifier + 14;
  834. const int KeyPress::F6Key = extendedKeyModifier + 16;
  835. const int KeyPress::F7Key = extendedKeyModifier + 17;
  836. const int KeyPress::F8Key = extendedKeyModifier + 18;
  837. const int KeyPress::F9Key = extendedKeyModifier + 19;
  838. const int KeyPress::F10Key = extendedKeyModifier + 20;
  839. const int KeyPress::F11Key = extendedKeyModifier + 21;
  840. const int KeyPress::F12Key = extendedKeyModifier + 22;
  841. const int KeyPress::F13Key = extendedKeyModifier + 23;
  842. const int KeyPress::F14Key = extendedKeyModifier + 24;
  843. const int KeyPress::F15Key = extendedKeyModifier + 25;
  844. const int KeyPress::F16Key = extendedKeyModifier + 26;
  845. const int KeyPress::F17Key = extendedKeyModifier + 50;
  846. const int KeyPress::F18Key = extendedKeyModifier + 51;
  847. const int KeyPress::F19Key = extendedKeyModifier + 52;
  848. const int KeyPress::F20Key = extendedKeyModifier + 53;
  849. const int KeyPress::F21Key = extendedKeyModifier + 54;
  850. const int KeyPress::F22Key = extendedKeyModifier + 55;
  851. const int KeyPress::F23Key = extendedKeyModifier + 56;
  852. const int KeyPress::F24Key = extendedKeyModifier + 57;
  853. const int KeyPress::F25Key = extendedKeyModifier + 58;
  854. const int KeyPress::F26Key = extendedKeyModifier + 59;
  855. const int KeyPress::F27Key = extendedKeyModifier + 60;
  856. const int KeyPress::F28Key = extendedKeyModifier + 61;
  857. const int KeyPress::F29Key = extendedKeyModifier + 62;
  858. const int KeyPress::F30Key = extendedKeyModifier + 63;
  859. const int KeyPress::F31Key = extendedKeyModifier + 64;
  860. const int KeyPress::F32Key = extendedKeyModifier + 65;
  861. const int KeyPress::F33Key = extendedKeyModifier + 66;
  862. const int KeyPress::F34Key = extendedKeyModifier + 67;
  863. const int KeyPress::F35Key = extendedKeyModifier + 68;
  864. const int KeyPress::numberPad0 = extendedKeyModifier + 27;
  865. const int KeyPress::numberPad1 = extendedKeyModifier + 28;
  866. const int KeyPress::numberPad2 = extendedKeyModifier + 29;
  867. const int KeyPress::numberPad3 = extendedKeyModifier + 30;
  868. const int KeyPress::numberPad4 = extendedKeyModifier + 31;
  869. const int KeyPress::numberPad5 = extendedKeyModifier + 32;
  870. const int KeyPress::numberPad6 = extendedKeyModifier + 33;
  871. const int KeyPress::numberPad7 = extendedKeyModifier + 34;
  872. const int KeyPress::numberPad8 = extendedKeyModifier + 35;
  873. const int KeyPress::numberPad9 = extendedKeyModifier + 36;
  874. const int KeyPress::numberPadAdd = extendedKeyModifier + 37;
  875. const int KeyPress::numberPadSubtract = extendedKeyModifier + 38;
  876. const int KeyPress::numberPadMultiply = extendedKeyModifier + 39;
  877. const int KeyPress::numberPadDivide = extendedKeyModifier + 40;
  878. const int KeyPress::numberPadSeparator = extendedKeyModifier + 41;
  879. const int KeyPress::numberPadDecimalPoint = extendedKeyModifier + 42;
  880. const int KeyPress::numberPadEquals = extendedKeyModifier + 43;
  881. const int KeyPress::numberPadDelete = extendedKeyModifier + 44;
  882. const int KeyPress::playKey = extendedKeyModifier + 45;
  883. const int KeyPress::stopKey = extendedKeyModifier + 46;
  884. const int KeyPress::fastForwardKey = extendedKeyModifier + 47;
  885. const int KeyPress::rewindKey = extendedKeyModifier + 48;