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.

1031 lines
40KB

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