Audio plugin host https://kx.studio/carla
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.

juce_android_Windowing.cpp 36KB

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