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. 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. void handleBackButtonCallback()
  354. {
  355. if (JUCEApplicationBase* const app = JUCEApplicationBase::getInstance())
  356. app->backButtonPressed();
  357. }
  358. //==============================================================================
  359. bool isFocused() const override
  360. {
  361. if (view != nullptr)
  362. return view.callBooleanMethod (ComponentPeerView.hasFocus);
  363. return false;
  364. }
  365. void grabFocus() override
  366. {
  367. if (view != nullptr)
  368. view.callBooleanMethod (ComponentPeerView.requestFocus);
  369. }
  370. void handleFocusChangeCallback (bool hasFocus)
  371. {
  372. if (hasFocus)
  373. handleFocusGain();
  374. else
  375. handleFocusLoss();
  376. }
  377. static const char* getVirtualKeyboardType (TextInputTarget::VirtualKeyboardType type) noexcept
  378. {
  379. switch (type)
  380. {
  381. case TextInputTarget::textKeyboard: return "text";
  382. case TextInputTarget::numericKeyboard: return "number";
  383. case TextInputTarget::decimalKeyboard: return "numberDecimal";
  384. case TextInputTarget::urlKeyboard: return "textUri";
  385. case TextInputTarget::emailAddressKeyboard: return "textEmailAddress";
  386. case TextInputTarget::phoneNumberKeyboard: return "phone";
  387. default: jassertfalse; break;
  388. }
  389. return "text";
  390. }
  391. void textInputRequired (Point<int>, TextInputTarget& target) override
  392. {
  393. view.callVoidMethod (ComponentPeerView.showKeyboard,
  394. javaString (getVirtualKeyboardType (target.getKeyboardType())).get());
  395. }
  396. void dismissPendingTextInput() override
  397. {
  398. view.callVoidMethod (ComponentPeerView.showKeyboard, javaString ("").get());
  399. }
  400. //==============================================================================
  401. void handlePaintCallback (JNIEnv* env, jobject canvas, jobject paint)
  402. {
  403. jobject rect = env->CallObjectMethod (canvas, CanvasMinimal.getClipBounds);
  404. const int left = env->GetIntField (rect, RectClass.left);
  405. const int top = env->GetIntField (rect, RectClass.top);
  406. const int right = env->GetIntField (rect, RectClass.right);
  407. const int bottom = env->GetIntField (rect, RectClass.bottom);
  408. env->DeleteLocalRef (rect);
  409. const Rectangle<int> clip (left, top, right - left, bottom - top);
  410. const int sizeNeeded = clip.getWidth() * clip.getHeight();
  411. if (sizeAllocated < sizeNeeded)
  412. {
  413. buffer.clear();
  414. sizeAllocated = sizeNeeded;
  415. buffer = GlobalRef (env->NewIntArray (sizeNeeded));
  416. }
  417. if (jint* dest = env->GetIntArrayElements ((jintArray) buffer.get(), 0))
  418. {
  419. {
  420. Image temp (new PreallocatedImage (clip.getWidth(), clip.getHeight(),
  421. dest, ! component.isOpaque()));
  422. {
  423. LowLevelGraphicsSoftwareRenderer g (temp);
  424. g.setOrigin (-clip.getPosition());
  425. g.addTransform (AffineTransform::scale (scale));
  426. handlePaint (g);
  427. }
  428. }
  429. env->ReleaseIntArrayElements ((jintArray) buffer.get(), dest, 0);
  430. env->CallVoidMethod (canvas, CanvasMinimal.drawBitmap, (jintArray) buffer.get(), 0, clip.getWidth(),
  431. (jfloat) clip.getX(), (jfloat) clip.getY(),
  432. clip.getWidth(), clip.getHeight(), true, paint);
  433. }
  434. }
  435. void repaint (const Rectangle<int>& userArea) override
  436. {
  437. Rectangle<int> area = userArea * scale;
  438. if (MessageManager::getInstance()->isThisTheMessageThread())
  439. {
  440. view.callVoidMethod (ComponentPeerView.invalidate, area.getX(), area.getY(), area.getRight(), area.getBottom());
  441. }
  442. else
  443. {
  444. struct ViewRepainter : public CallbackMessage
  445. {
  446. ViewRepainter (const GlobalRef& view_, const Rectangle<int>& area_)
  447. : view (view_), area (area_) {}
  448. void messageCallback() override
  449. {
  450. view.callVoidMethod (ComponentPeerView.invalidate, area.getX(), area.getY(),
  451. area.getRight(), area.getBottom());
  452. }
  453. private:
  454. GlobalRef view;
  455. const Rectangle<int> area;
  456. };
  457. (new ViewRepainter (view, area))->post();
  458. }
  459. }
  460. void performAnyPendingRepaintsNow() override
  461. {
  462. // TODO
  463. }
  464. void setAlpha (float /*newAlpha*/) override
  465. {
  466. // TODO
  467. }
  468. StringArray getAvailableRenderingEngines() override
  469. {
  470. return StringArray ("Software Renderer");
  471. }
  472. //==============================================================================
  473. static ModifierKeys currentModifiers;
  474. static Point<float> lastMousePos;
  475. static int64 touchesDown;
  476. private:
  477. //==============================================================================
  478. GlobalRef view;
  479. GlobalRef buffer;
  480. bool usingAndroidGraphics, fullScreen;
  481. int sizeAllocated;
  482. float scale;
  483. static AndroidComponentPeer* frontWindow;
  484. struct PreallocatedImage : public ImagePixelData
  485. {
  486. PreallocatedImage (const int width_, const int height_, jint* data_, bool hasAlpha_)
  487. : ImagePixelData (Image::ARGB, width_, height_), data (data_), hasAlpha (hasAlpha_)
  488. {
  489. if (hasAlpha_)
  490. zeromem (data_, static_cast<size_t> (width * height) * sizeof (jint));
  491. }
  492. ~PreallocatedImage()
  493. {
  494. if (hasAlpha)
  495. {
  496. PixelARGB* pix = (PixelARGB*) data;
  497. for (int i = width * height; --i >= 0;)
  498. {
  499. pix->unpremultiply();
  500. ++pix;
  501. }
  502. }
  503. }
  504. ImageType* createType() const override { return new SoftwareImageType(); }
  505. LowLevelGraphicsContext* createLowLevelContext() override { return new LowLevelGraphicsSoftwareRenderer (Image (this)); }
  506. void initialiseBitmapData (Image::BitmapData& bm, int x, int y, Image::BitmapData::ReadWriteMode /*mode*/) override
  507. {
  508. bm.lineStride = width * static_cast<int> (sizeof (jint));
  509. bm.pixelStride = static_cast<int> (sizeof (jint));
  510. bm.pixelFormat = Image::ARGB;
  511. bm.data = (uint8*) (data + x + y * width);
  512. }
  513. ImagePixelData::Ptr clone() override
  514. {
  515. PreallocatedImage* s = new PreallocatedImage (width, height, 0, hasAlpha);
  516. s->allocatedData.malloc (sizeof (jint) * static_cast<size_t> (width * height));
  517. s->data = s->allocatedData;
  518. memcpy (s->data, data, sizeof (jint) * static_cast<size_t> (width * height));
  519. return s;
  520. }
  521. private:
  522. jint* data;
  523. HeapBlock<jint> allocatedData;
  524. bool hasAlpha;
  525. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (PreallocatedImage)
  526. };
  527. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (AndroidComponentPeer)
  528. };
  529. ModifierKeys AndroidComponentPeer::currentModifiers = 0;
  530. Point<float> AndroidComponentPeer::lastMousePos;
  531. int64 AndroidComponentPeer::touchesDown = 0;
  532. AndroidComponentPeer* AndroidComponentPeer::frontWindow = nullptr;
  533. //==============================================================================
  534. #define JUCE_VIEW_CALLBACK(returnType, javaMethodName, params, juceMethodInvocation) \
  535. JUCE_JNI_CALLBACK (JUCE_JOIN_MACRO (JUCE_ANDROID_ACTIVITY_CLASSNAME, _00024ComponentPeerView), javaMethodName, returnType, params) \
  536. { \
  537. setEnv (env); \
  538. if (AndroidComponentPeer* peer = (AndroidComponentPeer*) (pointer_sized_uint) host) \
  539. peer->juceMethodInvocation; \
  540. }
  541. JUCE_VIEW_CALLBACK (void, handlePaint, (JNIEnv* env, jobject /*view*/, jlong host, jobject canvas, jobject paint), handlePaintCallback (env, canvas, paint))
  542. 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))
  543. 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))
  544. 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))
  545. JUCE_VIEW_CALLBACK (void, viewSizeChanged, (JNIEnv* env, jobject /*view*/, jlong host), handleMovedOrResized())
  546. JUCE_VIEW_CALLBACK (void, focusChanged, (JNIEnv* env, jobject /*view*/, jlong host, jboolean hasFocus), handleFocusChangeCallback (hasFocus))
  547. JUCE_VIEW_CALLBACK (void, handleKeyDown, (JNIEnv* env, jobject /*view*/, jlong host, jint k, jint kc), handleKeyDownCallback ((int) k, (int) kc))
  548. JUCE_VIEW_CALLBACK (void, handleKeyUp, (JNIEnv* env, jobject /*view*/, jlong host, jint k, jint kc), handleKeyUpCallback ((int) k, (int) kc))
  549. JUCE_VIEW_CALLBACK (void, handleBackButton, (JNIEnv* env, jobject /*view*/, jlong host), handleBackButtonCallback())
  550. //==============================================================================
  551. ComponentPeer* Component::createNewPeer (int styleFlags, void*)
  552. {
  553. return new AndroidComponentPeer (*this, styleFlags);
  554. }
  555. //==============================================================================
  556. bool Desktop::canUseSemiTransparentWindows() noexcept
  557. {
  558. return true;
  559. }
  560. double Desktop::getDefaultMasterScale()
  561. {
  562. return 1.0;
  563. }
  564. Desktop::DisplayOrientation Desktop::getCurrentOrientation() const
  565. {
  566. // TODO
  567. return upright;
  568. }
  569. bool MouseInputSource::SourceList::addSource()
  570. {
  571. addSource (sources.size(), MouseInputSource::InputSourceType::touch);
  572. return true;
  573. }
  574. bool MouseInputSource::SourceList::canUseTouch()
  575. {
  576. return true;
  577. }
  578. Point<float> MouseInputSource::getCurrentRawMousePosition()
  579. {
  580. return AndroidComponentPeer::lastMousePos;
  581. }
  582. void MouseInputSource::setRawMousePosition (Point<float>)
  583. {
  584. // not needed
  585. }
  586. //==============================================================================
  587. bool KeyPress::isKeyCurrentlyDown (const int /*keyCode*/)
  588. {
  589. // TODO
  590. return false;
  591. }
  592. void ModifierKeys::updateCurrentModifiers() noexcept
  593. {
  594. currentModifiers = AndroidComponentPeer::currentModifiers;
  595. }
  596. ModifierKeys ModifierKeys::getCurrentModifiersRealtime() noexcept
  597. {
  598. return AndroidComponentPeer::currentModifiers;
  599. }
  600. //==============================================================================
  601. // TODO
  602. JUCE_API bool JUCE_CALLTYPE Process::isForegroundProcess() { return true; }
  603. JUCE_API void JUCE_CALLTYPE Process::makeForegroundProcess() {}
  604. JUCE_API void JUCE_CALLTYPE Process::hide() {}
  605. //==============================================================================
  606. void JUCE_CALLTYPE NativeMessageBox::showMessageBoxAsync (AlertWindow::AlertIconType /*iconType*/,
  607. const String& title, const String& message,
  608. Component* /*associatedComponent*/,
  609. ModalComponentManager::Callback* callback)
  610. {
  611. android.activity.callVoidMethod (JuceAppActivity.showMessageBox, javaString (title).get(),
  612. javaString (message).get(), (jlong) (pointer_sized_int) callback);
  613. }
  614. bool JUCE_CALLTYPE NativeMessageBox::showOkCancelBox (AlertWindow::AlertIconType /*iconType*/,
  615. const String& title, const String& message,
  616. Component* /*associatedComponent*/,
  617. ModalComponentManager::Callback* callback)
  618. {
  619. jassert (callback != nullptr); // on android, all alerts must be non-modal!!
  620. android.activity.callVoidMethod (JuceAppActivity.showOkCancelBox, javaString (title).get(),
  621. javaString (message).get(), (jlong) (pointer_sized_int) callback,
  622. javaString (TRANS ("OK")).get(), javaString (TRANS ("Cancel")).get());
  623. return false;
  624. }
  625. int JUCE_CALLTYPE NativeMessageBox::showYesNoCancelBox (AlertWindow::AlertIconType /*iconType*/,
  626. const String& title, const String& message,
  627. Component* /*associatedComponent*/,
  628. ModalComponentManager::Callback* callback)
  629. {
  630. jassert (callback != nullptr); // on android, all alerts must be non-modal!!
  631. android.activity.callVoidMethod (JuceAppActivity.showYesNoCancelBox, javaString (title).get(),
  632. javaString (message).get(), (jlong) (pointer_sized_int) callback);
  633. return 0;
  634. }
  635. int JUCE_CALLTYPE NativeMessageBox::showYesNoBox (AlertWindow::AlertIconType /*iconType*/,
  636. const String& title, const String& message,
  637. Component* /*associatedComponent*/,
  638. ModalComponentManager::Callback* callback)
  639. {
  640. jassert (callback != nullptr); // on android, all alerts must be non-modal!!
  641. android.activity.callVoidMethod (JuceAppActivity.showOkCancelBox, javaString (title).get(),
  642. javaString (message).get(), (jlong) (pointer_sized_int) callback,
  643. javaString (TRANS ("Yes")).get(), javaString (TRANS ("No")).get());
  644. return 0;
  645. }
  646. JUCE_JNI_CALLBACK (JUCE_ANDROID_ACTIVITY_CLASSNAME, alertDismissed, void, (JNIEnv* env, jobject /*activity*/,
  647. jlong callbackAsLong, jint result))
  648. {
  649. setEnv (env);
  650. if (ModalComponentManager::Callback* callback = (ModalComponentManager::Callback*) callbackAsLong)
  651. {
  652. callback->modalStateFinished (result);
  653. delete callback;
  654. }
  655. }
  656. //==============================================================================
  657. void Desktop::setScreenSaverEnabled (const bool isEnabled)
  658. {
  659. android.activity.callVoidMethod (JuceAppActivity.setScreenSaver, isEnabled);
  660. }
  661. bool Desktop::isScreenSaverEnabled()
  662. {
  663. return android.activity.callBooleanMethod (JuceAppActivity.getScreenSaver);
  664. }
  665. //==============================================================================
  666. void Desktop::setKioskComponent (Component* kioskComp, bool enableOrDisable, bool allowMenusAndBars)
  667. {
  668. ignoreUnused (allowMenusAndBars);
  669. if (AndroidComponentPeer* peer = dynamic_cast<AndroidComponentPeer*> (kioskComp->getPeer()))
  670. peer->setFullScreen (enableOrDisable);
  671. else
  672. jassertfalse; // (this should have been checked by the caller)
  673. }
  674. //==============================================================================
  675. static jint getAndroidOrientationFlag (int orientations) noexcept
  676. {
  677. enum
  678. {
  679. SCREEN_ORIENTATION_LANDSCAPE = 0,
  680. SCREEN_ORIENTATION_PORTRAIT = 1,
  681. SCREEN_ORIENTATION_USER = 2,
  682. SCREEN_ORIENTATION_REVERSE_LANDSCAPE = 8,
  683. SCREEN_ORIENTATION_REVERSE_PORTRAIT = 9,
  684. SCREEN_ORIENTATION_USER_LANDSCAPE = 11,
  685. SCREEN_ORIENTATION_USER_PORTRAIT = 12,
  686. };
  687. switch (orientations)
  688. {
  689. case Desktop::upright: return (jint) SCREEN_ORIENTATION_PORTRAIT;
  690. case Desktop::upsideDown: return (jint) SCREEN_ORIENTATION_REVERSE_PORTRAIT;
  691. case Desktop::upright + Desktop::upsideDown: return (jint) SCREEN_ORIENTATION_USER_PORTRAIT;
  692. case Desktop::rotatedAntiClockwise: return (jint) SCREEN_ORIENTATION_LANDSCAPE;
  693. case Desktop::rotatedClockwise: return (jint) SCREEN_ORIENTATION_REVERSE_LANDSCAPE;
  694. case Desktop::rotatedClockwise + Desktop::rotatedAntiClockwise: return (jint) SCREEN_ORIENTATION_USER_LANDSCAPE;
  695. default: return (jint) SCREEN_ORIENTATION_USER;
  696. }
  697. }
  698. void Desktop::allowedOrientationsChanged()
  699. {
  700. android.activity.callVoidMethod (JuceAppActivity.setRequestedOrientation,
  701. getAndroidOrientationFlag (allowedOrientations));
  702. }
  703. //==============================================================================
  704. bool juce_areThereAnyAlwaysOnTopWindows()
  705. {
  706. return false;
  707. }
  708. //==============================================================================
  709. void Desktop::Displays::findDisplays (float masterScale)
  710. {
  711. Display d;
  712. d.isMain = true;
  713. d.dpi = android.dpi;
  714. d.scale = masterScale * (d.dpi / 150.);
  715. d.userArea = d.totalArea = Rectangle<int> (android.screenWidth,
  716. android.screenHeight) / d.scale;
  717. displays.add (d);
  718. }
  719. JUCE_JNI_CALLBACK (JUCE_ANDROID_ACTIVITY_CLASSNAME, setScreenSize, void, (JNIEnv* env, jobject /*activity*/,
  720. jint screenWidth, jint screenHeight,
  721. jint dpi))
  722. {
  723. setEnv (env);
  724. android.screenWidth = screenWidth;
  725. android.screenHeight = screenHeight;
  726. android.dpi = dpi;
  727. const_cast<Desktop::Displays&> (Desktop::getInstance().getDisplays()).refresh();
  728. }
  729. //==============================================================================
  730. Image juce_createIconForFile (const File& /*file*/)
  731. {
  732. return Image();
  733. }
  734. //==============================================================================
  735. void* CustomMouseCursorInfo::create() const { return nullptr; }
  736. void* MouseCursor::createStandardMouseCursor (const MouseCursor::StandardCursorType) { return nullptr; }
  737. void MouseCursor::deleteMouseCursor (void* const /*cursorHandle*/, const bool /*isStandard*/) {}
  738. //==============================================================================
  739. void MouseCursor::showInWindow (ComponentPeer*) const {}
  740. void MouseCursor::showInAllWindows() const {}
  741. //==============================================================================
  742. bool DragAndDropContainer::performExternalDragDropOfFiles (const StringArray& /*files*/, const bool /*canMove*/)
  743. {
  744. return false;
  745. }
  746. bool DragAndDropContainer::performExternalDragDropOfText (const String& /*text*/)
  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;