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.

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