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 41KB

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