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.

989 lines
38KB

  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. bool Desktop::canUseSemiTransparentWindows() noexcept
  556. {
  557. return true;
  558. }
  559. double Desktop::getDefaultMasterScale()
  560. {
  561. return 1.0;
  562. }
  563. Desktop::DisplayOrientation Desktop::getCurrentOrientation() const
  564. {
  565. // TODO
  566. return upright;
  567. }
  568. bool MouseInputSource::SourceList::addSource()
  569. {
  570. addSource (sources.size(), MouseInputSource::InputSourceType::touch);
  571. return true;
  572. }
  573. bool MouseInputSource::SourceList::canUseTouch()
  574. {
  575. return true;
  576. }
  577. Point<float> MouseInputSource::getCurrentRawMousePosition()
  578. {
  579. return AndroidComponentPeer::lastMousePos;
  580. }
  581. void MouseInputSource::setRawMousePosition (Point<float>)
  582. {
  583. // not needed
  584. }
  585. //==============================================================================
  586. bool KeyPress::isKeyCurrentlyDown (const int /*keyCode*/)
  587. {
  588. // TODO
  589. return false;
  590. }
  591. void ModifierKeys::updateCurrentModifiers() noexcept
  592. {
  593. currentModifiers = AndroidComponentPeer::currentModifiers;
  594. }
  595. ModifierKeys ModifierKeys::getCurrentModifiersRealtime() noexcept
  596. {
  597. return AndroidComponentPeer::currentModifiers;
  598. }
  599. //==============================================================================
  600. // TODO
  601. JUCE_API bool JUCE_CALLTYPE Process::isForegroundProcess() { return true; }
  602. JUCE_API void JUCE_CALLTYPE Process::makeForegroundProcess() {}
  603. JUCE_API void JUCE_CALLTYPE Process::hide() {}
  604. //==============================================================================
  605. void JUCE_CALLTYPE NativeMessageBox::showMessageBoxAsync (AlertWindow::AlertIconType /*iconType*/,
  606. const String& title, const String& message,
  607. Component* /*associatedComponent*/,
  608. ModalComponentManager::Callback* callback)
  609. {
  610. android.activity.callVoidMethod (JuceAppActivity.showMessageBox, javaString (title).get(),
  611. javaString (message).get(), (jlong) (pointer_sized_int) callback);
  612. }
  613. bool JUCE_CALLTYPE NativeMessageBox::showOkCancelBox (AlertWindow::AlertIconType /*iconType*/,
  614. const String& title, const String& message,
  615. Component* /*associatedComponent*/,
  616. ModalComponentManager::Callback* callback)
  617. {
  618. jassert (callback != nullptr); // on android, all alerts must be non-modal!!
  619. android.activity.callVoidMethod (JuceAppActivity.showOkCancelBox, javaString (title).get(),
  620. javaString (message).get(), (jlong) (pointer_sized_int) callback,
  621. javaString (TRANS ("OK")).get(), javaString (TRANS ("Cancel")).get());
  622. return false;
  623. }
  624. int JUCE_CALLTYPE NativeMessageBox::showYesNoCancelBox (AlertWindow::AlertIconType /*iconType*/,
  625. const String& title, const String& message,
  626. Component* /*associatedComponent*/,
  627. ModalComponentManager::Callback* callback)
  628. {
  629. jassert (callback != nullptr); // on android, all alerts must be non-modal!!
  630. android.activity.callVoidMethod (JuceAppActivity.showYesNoCancelBox, javaString (title).get(),
  631. javaString (message).get(), (jlong) (pointer_sized_int) callback);
  632. return 0;
  633. }
  634. int JUCE_CALLTYPE NativeMessageBox::showYesNoBox (AlertWindow::AlertIconType /*iconType*/,
  635. const String& title, const String& message,
  636. Component* /*associatedComponent*/,
  637. ModalComponentManager::Callback* callback)
  638. {
  639. jassert (callback != nullptr); // on android, all alerts must be non-modal!!
  640. android.activity.callVoidMethod (JuceAppActivity.showOkCancelBox, javaString (title).get(),
  641. javaString (message).get(), (jlong) (pointer_sized_int) callback,
  642. javaString (TRANS ("Yes")).get(), javaString (TRANS ("No")).get());
  643. return 0;
  644. }
  645. JUCE_JNI_CALLBACK (JUCE_ANDROID_ACTIVITY_CLASSNAME, alertDismissed, void, (JNIEnv* env, jobject /*activity*/,
  646. jlong callbackAsLong, jint result))
  647. {
  648. setEnv (env);
  649. if (ModalComponentManager::Callback* callback = (ModalComponentManager::Callback*) callbackAsLong)
  650. {
  651. callback->modalStateFinished (result);
  652. delete callback;
  653. }
  654. }
  655. //==============================================================================
  656. void Desktop::setScreenSaverEnabled (const bool isEnabled)
  657. {
  658. android.activity.callVoidMethod (JuceAppActivity.setScreenSaver, isEnabled);
  659. }
  660. bool Desktop::isScreenSaverEnabled()
  661. {
  662. return android.activity.callBooleanMethod (JuceAppActivity.getScreenSaver);
  663. }
  664. //==============================================================================
  665. void Desktop::setKioskComponent (Component* kioskComp, bool enableOrDisable, bool allowMenusAndBars)
  666. {
  667. ignoreUnused (allowMenusAndBars);
  668. if (AndroidComponentPeer* peer = dynamic_cast<AndroidComponentPeer*> (kioskComp->getPeer()))
  669. peer->setFullScreen (enableOrDisable);
  670. else
  671. jassertfalse; // (this should have been checked by the caller)
  672. }
  673. //==============================================================================
  674. static jint getAndroidOrientationFlag (int orientations) noexcept
  675. {
  676. enum
  677. {
  678. SCREEN_ORIENTATION_LANDSCAPE = 0,
  679. SCREEN_ORIENTATION_PORTRAIT = 1,
  680. SCREEN_ORIENTATION_USER = 2,
  681. SCREEN_ORIENTATION_REVERSE_LANDSCAPE = 8,
  682. SCREEN_ORIENTATION_REVERSE_PORTRAIT = 9,
  683. SCREEN_ORIENTATION_USER_LANDSCAPE = 11,
  684. SCREEN_ORIENTATION_USER_PORTRAIT = 12,
  685. };
  686. switch (orientations)
  687. {
  688. case Desktop::upright: return (jint) SCREEN_ORIENTATION_PORTRAIT;
  689. case Desktop::upsideDown: return (jint) SCREEN_ORIENTATION_REVERSE_PORTRAIT;
  690. case Desktop::upright + Desktop::upsideDown: return (jint) SCREEN_ORIENTATION_USER_PORTRAIT;
  691. case Desktop::rotatedAntiClockwise: return (jint) SCREEN_ORIENTATION_LANDSCAPE;
  692. case Desktop::rotatedClockwise: return (jint) SCREEN_ORIENTATION_REVERSE_LANDSCAPE;
  693. case Desktop::rotatedClockwise + Desktop::rotatedAntiClockwise: return (jint) SCREEN_ORIENTATION_USER_LANDSCAPE;
  694. default: return (jint) SCREEN_ORIENTATION_USER;
  695. }
  696. }
  697. void Desktop::allowedOrientationsChanged()
  698. {
  699. android.activity.callVoidMethod (JuceAppActivity.setRequestedOrientation,
  700. getAndroidOrientationFlag (allowedOrientations));
  701. }
  702. //==============================================================================
  703. bool juce_areThereAnyAlwaysOnTopWindows()
  704. {
  705. return false;
  706. }
  707. //==============================================================================
  708. void Desktop::Displays::findDisplays (float masterScale)
  709. {
  710. Display d;
  711. d.isMain = true;
  712. d.dpi = android.dpi;
  713. d.scale = masterScale * (d.dpi / 150.);
  714. d.userArea = d.totalArea = Rectangle<int> (android.screenWidth,
  715. android.screenHeight) / d.scale;
  716. displays.add (d);
  717. }
  718. JUCE_JNI_CALLBACK (JUCE_ANDROID_ACTIVITY_CLASSNAME, setScreenSize, void, (JNIEnv* env, jobject /*activity*/,
  719. jint screenWidth, jint screenHeight,
  720. jint dpi))
  721. {
  722. setEnv (env);
  723. android.screenWidth = screenWidth;
  724. android.screenHeight = screenHeight;
  725. android.dpi = dpi;
  726. const_cast<Desktop::Displays&> (Desktop::getInstance().getDisplays()).refresh();
  727. }
  728. //==============================================================================
  729. Image juce_createIconForFile (const File& /*file*/)
  730. {
  731. return Image();
  732. }
  733. //==============================================================================
  734. void* CustomMouseCursorInfo::create() const { return nullptr; }
  735. void* MouseCursor::createStandardMouseCursor (const MouseCursor::StandardCursorType) { return nullptr; }
  736. void MouseCursor::deleteMouseCursor (void* const /*cursorHandle*/, const bool /*isStandard*/) {}
  737. //==============================================================================
  738. void MouseCursor::showInWindow (ComponentPeer*) const {}
  739. void MouseCursor::showInAllWindows() const {}
  740. //==============================================================================
  741. bool DragAndDropContainer::performExternalDragDropOfFiles (const StringArray& /*files*/, const bool /*canMove*/,
  742. Component* /*srcComp*/)
  743. {
  744. return false;
  745. }
  746. bool DragAndDropContainer::performExternalDragDropOfText (const String& /*text*/, Component* /*srcComp*/)
  747. {
  748. return false;
  749. }
  750. //==============================================================================
  751. void LookAndFeel::playAlertSound()
  752. {
  753. }
  754. //==============================================================================
  755. void SystemClipboard::copyTextToClipboard (const String& text)
  756. {
  757. const LocalRef<jstring> t (javaString (text));
  758. android.activity.callVoidMethod (JuceAppActivity.setClipboardContent, t.get());
  759. }
  760. String SystemClipboard::getTextFromClipboard()
  761. {
  762. const LocalRef<jstring> text ((jstring) android.activity.callObjectMethod (JuceAppActivity.getClipboardContent));
  763. return juceString (text);
  764. }
  765. //==============================================================================
  766. const int extendedKeyModifier = 0x10000;
  767. const int KeyPress::spaceKey = ' ';
  768. const int KeyPress::returnKey = 66;
  769. const int KeyPress::escapeKey = 4;
  770. const int KeyPress::backspaceKey = 67;
  771. const int KeyPress::leftKey = extendedKeyModifier + 1;
  772. const int KeyPress::rightKey = extendedKeyModifier + 2;
  773. const int KeyPress::upKey = extendedKeyModifier + 3;
  774. const int KeyPress::downKey = extendedKeyModifier + 4;
  775. const int KeyPress::pageUpKey = extendedKeyModifier + 5;
  776. const int KeyPress::pageDownKey = extendedKeyModifier + 6;
  777. const int KeyPress::endKey = extendedKeyModifier + 7;
  778. const int KeyPress::homeKey = extendedKeyModifier + 8;
  779. const int KeyPress::deleteKey = extendedKeyModifier + 9;
  780. const int KeyPress::insertKey = -1;
  781. const int KeyPress::tabKey = 61;
  782. const int KeyPress::F1Key = extendedKeyModifier + 10;
  783. const int KeyPress::F2Key = extendedKeyModifier + 11;
  784. const int KeyPress::F3Key = extendedKeyModifier + 12;
  785. const int KeyPress::F4Key = extendedKeyModifier + 13;
  786. const int KeyPress::F5Key = extendedKeyModifier + 14;
  787. const int KeyPress::F6Key = extendedKeyModifier + 16;
  788. const int KeyPress::F7Key = extendedKeyModifier + 17;
  789. const int KeyPress::F8Key = extendedKeyModifier + 18;
  790. const int KeyPress::F9Key = extendedKeyModifier + 19;
  791. const int KeyPress::F10Key = extendedKeyModifier + 20;
  792. const int KeyPress::F11Key = extendedKeyModifier + 21;
  793. const int KeyPress::F12Key = extendedKeyModifier + 22;
  794. const int KeyPress::F13Key = extendedKeyModifier + 23;
  795. const int KeyPress::F14Key = extendedKeyModifier + 24;
  796. const int KeyPress::F15Key = extendedKeyModifier + 25;
  797. const int KeyPress::F16Key = extendedKeyModifier + 26;
  798. const int KeyPress::numberPad0 = extendedKeyModifier + 27;
  799. const int KeyPress::numberPad1 = extendedKeyModifier + 28;
  800. const int KeyPress::numberPad2 = extendedKeyModifier + 29;
  801. const int KeyPress::numberPad3 = extendedKeyModifier + 30;
  802. const int KeyPress::numberPad4 = extendedKeyModifier + 31;
  803. const int KeyPress::numberPad5 = extendedKeyModifier + 32;
  804. const int KeyPress::numberPad6 = extendedKeyModifier + 33;
  805. const int KeyPress::numberPad7 = extendedKeyModifier + 34;
  806. const int KeyPress::numberPad8 = extendedKeyModifier + 35;
  807. const int KeyPress::numberPad9 = extendedKeyModifier + 36;
  808. const int KeyPress::numberPadAdd = extendedKeyModifier + 37;
  809. const int KeyPress::numberPadSubtract = extendedKeyModifier + 38;
  810. const int KeyPress::numberPadMultiply = extendedKeyModifier + 39;
  811. const int KeyPress::numberPadDivide = extendedKeyModifier + 40;
  812. const int KeyPress::numberPadSeparator = extendedKeyModifier + 41;
  813. const int KeyPress::numberPadDecimalPoint = extendedKeyModifier + 42;
  814. const int KeyPress::numberPadEquals = extendedKeyModifier + 43;
  815. const int KeyPress::numberPadDelete = extendedKeyModifier + 44;
  816. const int KeyPress::playKey = extendedKeyModifier + 45;
  817. const int KeyPress::stopKey = extendedKeyModifier + 46;
  818. const int KeyPress::fastForwardKey = extendedKeyModifier + 47;
  819. const int KeyPress::rewindKey = extendedKeyModifier + 48;