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.

1092 lines
42KB

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