浏览代码

Add Android backend and example (#3446)

duddel 4 年之前
父节点
当前提交
fb85c0341b

+ 11 - 1
.github/workflows/build.yml

@@ -458,10 +458,20 @@ jobs:
         popd
         popd
         make -C examples/example_emscripten_wgpu
         make -C examples/example_emscripten_wgpu
 
 
+  Android:
+    runs-on: ubuntu-18.04
+    steps:
+    - uses: actions/checkout@v2
+
+    - name: Build example_android_opengl3
+      run: |
+        cd examples/example_android_opengl3/android
+        gradle assembleDebug
+
   Discord-CI:
   Discord-CI:
     runs-on: ubuntu-18.04
     runs-on: ubuntu-18.04
     if: always()
     if: always()
-    needs: [Windows, Linux, MacOS, iOS, Emscripten]
+    needs: [Windows, Linux, MacOS, iOS, Emscripten, Android]
     steps:
     steps:
     - uses: dearimgui/github_discord_notifier@latest
     - uses: dearimgui/github_discord_notifier@latest
       with:
       with:

+ 192 - 0
backends/imgui_impl_android.cpp

@@ -0,0 +1,192 @@
+// dear imgui: Platform Binding for Android native app
+// This needs to be used along with the OpenGL 3 Renderer (imgui_impl_opengl3)
+
+// Implemented features:
+//  [X] Platform: Keyboard arrays indexed using AKEYCODE_* codes, e.g. ImGui::IsKeyPressed(AKEYCODE_SPACE).
+//  [ ] Platform: Clipboard support.
+//  [ ] Platform: Gamepad support. Enable with 'io.ConfigFlags |= ImGuiConfigFlags_NavEnableGamepad'.
+//  [ ] Platform: Mouse cursor shape and visibility. Disable with 'io.ConfigFlags |= ImGuiConfigFlags_NoMouseCursorChange'. FIXME: Check if this is even possible with Android.
+
+// You can copy and use unmodified imgui_impl_* files in your project. See main.cpp for an example of using this.
+// If you are new to dear imgui, read examples/README.txt and read the documentation at the top of imgui.cpp.
+// https://github.com/ocornut/imgui
+
+// CHANGELOG
+// (minor and older changes stripped away, please see git history for details)
+//  2021-03-02: Support for physical pointer device input (such as physical mouse)
+//  2020-09-13: Support for Unicode characters
+//  2020-08-31: On-screen and physical keyboard input (ASCII characters only)
+//  2020-03-02: basic draft, touch input
+
+#include "imgui.h"
+#include "imgui_impl_android.h"
+#include <time.h>
+#include <map>
+#include <queue>
+
+// Android
+#include <android/native_window.h>
+#include <android/input.h>
+#include <android/keycodes.h>
+#include <android/log.h>
+
+static double                                   g_Time = 0.0;
+static ANativeWindow*                           g_Window;
+static char                                     g_LogTag[] = "ImguiExample";
+static std::map<int32_t, std::queue<int32_t>>   g_KeyEventQueues; // FIXME: Remove dependency on map and queue once we use upcoming input queue.
+
+int32_t ImGui_ImplAndroid_HandleInputEvent(AInputEvent* inputEvent)
+{
+    ImGuiIO& io = ImGui::GetIO();
+    int32_t event_type = AInputEvent_getType(inputEvent);
+    switch (event_type)
+    {
+    case AINPUT_EVENT_TYPE_KEY:
+    {
+        int32_t event_key_code = AKeyEvent_getKeyCode(inputEvent);
+        int32_t event_action = AKeyEvent_getAction(inputEvent);
+        int32_t event_meta_state = AKeyEvent_getMetaState(inputEvent);
+
+        io.KeyCtrl = ((event_meta_state & AMETA_CTRL_ON) != 0);
+        io.KeyShift = ((event_meta_state & AMETA_SHIFT_ON) != 0);
+        io.KeyAlt = ((event_meta_state & AMETA_ALT_ON) != 0);
+
+        switch (event_action)
+        {
+        // FIXME: AKEY_EVENT_ACTION_DOWN and AKEY_EVENT_ACTION_UP occur at once
+        // as soon as a touch pointer goes up from a key. We use a simple key event queue
+        // and process one event per key per ImGui frame in ImGui_ImplAndroid_NewFrame().
+        // ...or consider ImGui IO queue, if suitable: https://github.com/ocornut/imgui/issues/2787
+        case AKEY_EVENT_ACTION_DOWN:
+        case AKEY_EVENT_ACTION_UP:
+            g_KeyEventQueues[event_key_code].push(event_action);
+            break;
+        default:
+            break;
+        }
+        break;
+    }
+    case AINPUT_EVENT_TYPE_MOTION:
+    {
+        int32_t event_action = AMotionEvent_getAction(inputEvent);
+        int32_t event_pointer_index = (event_action & AMOTION_EVENT_ACTION_POINTER_INDEX_MASK) >> AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT;
+        event_action &= AMOTION_EVENT_ACTION_MASK;
+        switch (event_action)
+        {
+        case AMOTION_EVENT_ACTION_DOWN:
+        case AMOTION_EVENT_ACTION_UP:
+            // Physical mouse buttons (and probably other physical devices) also invoke the actions AMOTION_EVENT_ACTION_DOWN/_UP,
+            // but we have to process them separately to identify the actual button pressed. This is done below via
+            // AMOTION_EVENT_ACTION_BUTTON_PRESS/_RELEASE. Here, we only process "FINGER" input (and "UNKNOWN", as a fallback).
+            if((AMotionEvent_getToolType(inputEvent, event_pointer_index) == AMOTION_EVENT_TOOL_TYPE_FINGER)
+            || (AMotionEvent_getToolType(inputEvent, event_pointer_index) == AMOTION_EVENT_TOOL_TYPE_UNKNOWN))
+            {
+                io.MouseDown[0] = (event_action == AMOTION_EVENT_ACTION_DOWN) ? true : false;
+                io.MousePos = ImVec2(
+                        AMotionEvent_getX(inputEvent, event_pointer_index),
+                        AMotionEvent_getY(inputEvent, event_pointer_index));
+            }
+            break;
+        case AMOTION_EVENT_ACTION_BUTTON_PRESS:
+        case AMOTION_EVENT_ACTION_BUTTON_RELEASE:
+            {
+                int32_t button_state = AMotionEvent_getButtonState(inputEvent);
+                io.MouseDown[0] = (button_state & AMOTION_EVENT_BUTTON_PRIMARY) ? true : false;
+                io.MouseDown[1] = (button_state & AMOTION_EVENT_BUTTON_SECONDARY) ? true : false;
+                io.MouseDown[2] = (button_state & AMOTION_EVENT_BUTTON_TERTIARY) ? true : false;
+            }
+            break;
+        case AMOTION_EVENT_ACTION_HOVER_MOVE: // Hovering: Tool moves while NOT pressed (such as a physical mouse)
+        case AMOTION_EVENT_ACTION_MOVE: // Touch pointer moves while DOWN
+            io.MousePos = ImVec2(
+                AMotionEvent_getX(inputEvent, event_pointer_index),
+                AMotionEvent_getY(inputEvent, event_pointer_index));
+            break;
+        case AMOTION_EVENT_ACTION_SCROLL:
+            io.MouseWheel = AMotionEvent_getAxisValue(inputEvent, AMOTION_EVENT_AXIS_VSCROLL, event_pointer_index);
+            io.MouseWheelH = AMotionEvent_getAxisValue(inputEvent, AMOTION_EVENT_AXIS_HSCROLL, event_pointer_index);
+            break;
+        default:
+            break;
+        }
+    }
+        return 1;
+    default:
+        break;
+    }
+
+    return 0;
+}
+
+bool ImGui_ImplAndroid_Init(ANativeWindow* window)
+{
+    g_Window = window;
+    g_Time = 0.0;
+
+    // Setup back-end capabilities flags
+    ImGuiIO& io = ImGui::GetIO();
+    io.BackendPlatformName = "imgui_impl_android";
+
+    // Keyboard mapping. ImGui will use those indices to peek into the io.KeysDown[] array.
+    io.KeyMap[ImGuiKey_Tab] = AKEYCODE_TAB;
+    io.KeyMap[ImGuiKey_LeftArrow] = AKEYCODE_DPAD_LEFT;   // also covers physical keyboard arrow key
+    io.KeyMap[ImGuiKey_RightArrow] = AKEYCODE_DPAD_RIGHT; // also covers physical keyboard arrow key
+    io.KeyMap[ImGuiKey_UpArrow] = AKEYCODE_DPAD_UP;       // also covers physical keyboard arrow key
+    io.KeyMap[ImGuiKey_DownArrow] = AKEYCODE_DPAD_DOWN;   // also covers physical keyboard arrow key
+    io.KeyMap[ImGuiKey_PageUp] = AKEYCODE_PAGE_UP;
+    io.KeyMap[ImGuiKey_PageDown] = AKEYCODE_PAGE_DOWN;
+    io.KeyMap[ImGuiKey_Home] = AKEYCODE_MOVE_HOME;
+    io.KeyMap[ImGuiKey_End] = AKEYCODE_MOVE_END;
+    io.KeyMap[ImGuiKey_Insert] = AKEYCODE_INSERT;
+    io.KeyMap[ImGuiKey_Delete] = AKEYCODE_FORWARD_DEL;
+    io.KeyMap[ImGuiKey_Backspace] = AKEYCODE_DEL;
+    io.KeyMap[ImGuiKey_Space] = AKEYCODE_SPACE;
+    io.KeyMap[ImGuiKey_Enter] = AKEYCODE_ENTER;
+    io.KeyMap[ImGuiKey_Escape] = AKEYCODE_ESCAPE;
+    io.KeyMap[ImGuiKey_KeyPadEnter] = AKEYCODE_NUMPAD_ENTER;
+    io.KeyMap[ImGuiKey_A] = AKEYCODE_A;
+    io.KeyMap[ImGuiKey_C] = AKEYCODE_C;
+    io.KeyMap[ImGuiKey_V] = AKEYCODE_V;
+    io.KeyMap[ImGuiKey_X] = AKEYCODE_X;
+    io.KeyMap[ImGuiKey_Y] = AKEYCODE_Y;
+    io.KeyMap[ImGuiKey_Z] = AKEYCODE_Z;
+
+    return true;
+}
+
+void ImGui_ImplAndroid_Shutdown()
+{
+}
+
+void ImGui_ImplAndroid_NewFrame()
+{
+    ImGuiIO& io = ImGui::GetIO();
+    IM_ASSERT(io.Fonts->IsBuilt() && "Font atlas not built! It is generally built by the renderer back-end. Missing call to renderer _NewFrame() function? e.g. ImGui_ImplOpenGL3_NewFrame().");
+
+    // Process queued key events
+    // FIXME: This is a workaround for multiple key event actions occuring at once (see above) and can be removed once we use upcoming input queue.
+    for (auto& key_queue : g_KeyEventQueues)
+    {
+        if (key_queue.second.empty())
+            continue;
+        io.KeysDown[key_queue.first] = (key_queue.second.front() == AKEY_EVENT_ACTION_DOWN);
+        key_queue.second.pop();
+    }
+
+    // Setup display size (every frame to accommodate for window resizing)
+    int32_t window_width = ANativeWindow_getWidth(g_Window);
+    int32_t window_height = ANativeWindow_getHeight(g_Window);
+    int display_width = window_width;
+    int display_height = window_height;
+
+    io.DisplaySize = ImVec2((float)window_width, (float)window_height);
+    if (window_width > 0 && window_height > 0)
+        io.DisplayFramebufferScale = ImVec2((float)display_width / window_width, (float)display_height / window_height);
+
+    // Setup time step
+    struct timespec current_timespec;
+    clock_gettime(CLOCK_MONOTONIC, &current_timespec);
+    double current_time = (double)(current_timespec.tv_sec) + (current_timespec.tv_nsec / 1000000000.0);
+    io.DeltaTime = g_Time > 0.0 ? (float)(current_time - g_Time) : (float)(1.0f / 60.0f);
+    g_Time = current_time;
+}

+ 22 - 0
backends/imgui_impl_android.h

@@ -0,0 +1,22 @@
+// dear imgui: Platform Binding for Android native app
+// This needs to be used along with the OpenGL 3 Renderer (imgui_impl_opengl3)
+
+// Implemented features:
+//  [X] Platform: Keyboard arrays indexed using AKEYCODE_* codes, e.g. ImGui::IsKeyPressed(AKEYCODE_SPACE).
+//  [ ] Platform: Clipboard support.
+//  [ ] Platform: Gamepad support. Enable with 'io.ConfigFlags |= ImGuiConfigFlags_NavEnableGamepad'.
+//  [ ] Platform: Mouse cursor shape and visibility. Disable with 'io.ConfigFlags |= ImGuiConfigFlags_NoMouseCursorChange'. FIXME: Check if this is even possible with Android.
+
+// You can copy and use unmodified imgui_impl_* files in your project. See main.cpp for an example of using this.
+// If you are new to dear imgui, read examples/README.txt and read the documentation at the top of imgui.cpp.
+// https://github.com/ocornut/imgui
+
+#pragma once
+
+struct ANativeWindow;
+struct AInputEvent;
+
+IMGUI_IMPL_API int32_t  ImGui_ImplAndroid_HandleInputEvent(AInputEvent* inputEvent);
+IMGUI_IMPL_API bool     ImGui_ImplAndroid_Init(ANativeWindow* window);
+IMGUI_IMPL_API void     ImGui_ImplAndroid_Shutdown();
+IMGUI_IMPL_API void     ImGui_ImplAndroid_NewFrame();

+ 1 - 0
docs/BACKENDS.md

@@ -57,6 +57,7 @@ In the [backends/](https://github.com/ocornut/imgui/blob/master/backends) folder
 
 
 List of Platforms Backends:
 List of Platforms Backends:
 
 
+    imgui_impl_android.cpp    ; Android native app API
     imgui_impl_glfw.cpp       ; GLFW (Windows, macOS, Linux, etc.) http://www.glfw.org/
     imgui_impl_glfw.cpp       ; GLFW (Windows, macOS, Linux, etc.) http://www.glfw.org/
     imgui_impl_osx.mm         ; macOS native API (not as feature complete as glfw/sdl backends)
     imgui_impl_osx.mm         ; macOS native API (not as feature complete as glfw/sdl backends)
     imgui_impl_sdl.cpp        ; SDL2 (Windows, macOS, Linux, iOS, Android) https://www.libsdl.org
     imgui_impl_sdl.cpp        ; SDL2 (Windows, macOS, Linux, iOS, Android) https://www.libsdl.org

+ 4 - 0
docs/EXAMPLES.md

@@ -79,6 +79,10 @@ Changelog, so if you want to update them later it will be easier to catch up wit
 Allegro 5 example. <BR>
 Allegro 5 example. <BR>
 = main.cpp + imgui_impl_allegro5.cpp
 = main.cpp + imgui_impl_allegro5.cpp
 
 
+[example_android_opengl3/](https://github.com/ocornut/imgui/blob/master/examples/example_android_opengl3/) <BR>
+Android + OpenGL3 (ES) example. <BR>
+= main.cpp + imgui_impl_android.cpp + imgui_impl_opengl3.cpp
+
 [example_apple_metal/](https://github.com/ocornut/imgui/blob/master/examples/example_metal/) <BR>
 [example_apple_metal/](https://github.com/ocornut/imgui/blob/master/examples/example_metal/) <BR>
 OSX & iOS + Metal example. <BR>
 OSX & iOS + Metal example. <BR>
 = main.m + imgui_impl_osx.mm + imgui_impl_metal.mm <BR>
 = main.m + imgui_impl_osx.mm + imgui_impl_metal.mm <BR>

+ 1 - 1
docs/README.md

@@ -117,7 +117,7 @@ Integrating Dear ImGui within your custom engine is a matter of 1) wiring mouse/
 
 
 Officially maintained backends/bindings (in repository):
 Officially maintained backends/bindings (in repository):
 - Renderers: DirectX9, DirectX10, DirectX11, DirectX12, Metal, OpenGL/ES/ES2, Vulkan, WebGPU.
 - Renderers: DirectX9, DirectX10, DirectX11, DirectX12, Metal, OpenGL/ES/ES2, Vulkan, WebGPU.
-- Platforms: GLFW, SDL2, Win32, Glut, OSX.
+- Platforms: GLFW, SDL2, Win32, Glut, OSX, Android.
 - Frameworks: Emscripten, Allegro5, Marmalade.
 - Frameworks: Emscripten, Allegro5, Marmalade.
 
 
 [Third-party backends/bindings](https://github.com/ocornut/imgui/wiki/Bindings) wiki page:
 [Third-party backends/bindings](https://github.com/ocornut/imgui/wiki/Bindings) wiki page:

+ 40 - 0
examples/example_android_opengl3/CMakeLists.txt

@@ -0,0 +1,40 @@
+cmake_minimum_required(VERSION 3.6)
+
+project(ImguiExample)
+
+set(CMAKE_CXX_STANDARD 11)
+set(CMAKE_CXX_STANDARD_REQUIRED ON)
+set(CMAKE_CXX_EXTENSIONS OFF)
+
+add_library(${CMAKE_PROJECT_NAME} SHARED
+  ${CMAKE_CURRENT_SOURCE_DIR}/main.cpp
+  ${CMAKE_CURRENT_SOURCE_DIR}/../../imgui.cpp
+  ${CMAKE_CURRENT_SOURCE_DIR}/../../imgui_demo.cpp
+  ${CMAKE_CURRENT_SOURCE_DIR}/../../imgui_draw.cpp
+  ${CMAKE_CURRENT_SOURCE_DIR}/../../imgui_tables.cpp
+  ${CMAKE_CURRENT_SOURCE_DIR}/../../imgui_widgets.cpp
+  ${CMAKE_CURRENT_SOURCE_DIR}/../../backends/imgui_impl_android.cpp
+  ${CMAKE_CURRENT_SOURCE_DIR}/../../backends/imgui_impl_opengl3.cpp
+  ${ANDROID_NDK}/sources/android/native_app_glue/android_native_app_glue.c
+)
+
+set(CMAKE_SHARED_LINKER_FLAGS
+  "${CMAKE_SHARED_LINKER_FLAGS} -u ANativeActivity_onCreate"
+)
+
+target_compile_definitions(${CMAKE_PROJECT_NAME} PRIVATE
+  IMGUI_IMPL_OPENGL_ES3
+)
+
+target_include_directories(${CMAKE_PROJECT_NAME} PRIVATE
+  ${CMAKE_CURRENT_SOURCE_DIR}/../..
+  ${CMAKE_CURRENT_SOURCE_DIR}/../../backends
+  ${ANDROID_NDK}/sources/android/native_app_glue
+)
+
+target_link_libraries(${CMAKE_PROJECT_NAME} PRIVATE
+  android
+  EGL
+  GLESv3
+  log
+)

+ 12 - 0
examples/example_android_opengl3/android/.gitignore

@@ -0,0 +1,12 @@
+.cxx
+.externalNativeBuild
+build/
+*.iml
+
+.idea
+.gradle
+local.properties
+
+# Android Studio puts a Gradle wrapper here, that we don't want:
+gradle/
+gradlew*

+ 34 - 0
examples/example_android_opengl3/android/app/build.gradle

@@ -0,0 +1,34 @@
+apply plugin: 'com.android.application'
+apply plugin: 'kotlin-android'
+
+android {
+    compileSdkVersion 29
+    buildToolsVersion "30.0.3"
+    ndkVersion "21.4.7075529"
+    defaultConfig {
+        applicationId "imgui.example.android"
+        minSdkVersion 23
+        targetSdkVersion 29
+        versionCode 1
+        versionName "1.0"
+    }
+
+    buildTypes {
+        release {
+            minifyEnabled false
+            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt')
+        }
+    }
+
+    externalNativeBuild {
+        cmake {
+            path "../../CMakeLists.txt"
+        }
+    }
+}
+repositories {
+    mavenCentral()
+}
+dependencies {
+    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
+}

+ 24 - 0
examples/example_android_opengl3/android/app/src/main/AndroidManifest.xml

@@ -0,0 +1,24 @@
+<?xml version="1.0" encoding="utf-8"?>
+<manifest xmlns:android="http://schemas.android.com/apk/res/android"
+    package="imgui.example.android">
+
+    <application
+        android:label="ImguiExample"
+        android:allowBackup="false"
+        android:fullBackupContent="false"
+        android:hasCode="true">
+
+        <activity
+            android:name="imgui.example.android.MainActivity"
+            android:theme="@android:style/Theme.NoTitleBar.Fullscreen"
+            android:configChanges="orientation|keyboardHidden|screenSize">
+            <meta-data android:name="android.app.lib_name"
+                android:value="ImguiExample" />
+
+            <intent-filter>
+                <action android:name="android.intent.action.MAIN" />
+                <category android:name="android.intent.category.LAUNCHER" />
+            </intent-filter>
+        </activity>
+    </application>
+</manifest>

+ 40 - 0
examples/example_android_opengl3/android/app/src/main/java/MainActivity.kt

@@ -0,0 +1,40 @@
+package imgui.example.android
+
+import android.app.NativeActivity
+import android.os.Bundle
+import android.content.Context
+import android.view.inputmethod.InputMethodManager
+import android.view.KeyEvent
+import java.util.concurrent.LinkedBlockingQueue
+
+class MainActivity : NativeActivity() {
+    public override fun onCreate(savedInstanceState: Bundle?) {
+        super.onCreate(savedInstanceState)
+    }
+
+    fun showSoftInput() {
+        val inputMethodManager = getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
+        inputMethodManager.showSoftInput(this.window.decorView, 0)
+    }
+
+    fun hideSoftInput() {
+        val inputMethodManager = getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
+        inputMethodManager.hideSoftInputFromWindow(this.window.decorView.windowToken, 0)
+    }
+
+    // Queue for the Unicode characters to be polled from native code (via pollUnicodeChar())
+    private var unicodeCharacterQueue: LinkedBlockingQueue<Int> = LinkedBlockingQueue()
+
+    // We assume dispatchKeyEvent() of the NativeActivity is actually called for every
+    // KeyEvent and not consumed by any View before it reaches here
+    override fun dispatchKeyEvent(event: KeyEvent): Boolean {
+        if (event.action == KeyEvent.ACTION_DOWN) {
+            unicodeCharacterQueue.offer(event.getUnicodeChar(event.metaState))
+        }
+        return super.dispatchKeyEvent(event)
+    }
+
+    fun pollUnicodeChar(): Int {
+        return unicodeCharacterQueue.poll() ?: 0
+    }
+}

+ 24 - 0
examples/example_android_opengl3/android/build.gradle

@@ -0,0 +1,24 @@
+buildscript {
+    ext.kotlin_version = '1.4.30'
+    repositories {
+        google()
+        jcenter()
+
+    }
+    dependencies {
+        classpath 'com.android.tools.build:gradle:4.0.1'
+        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
+
+    }
+}
+
+allprojects {
+    repositories {
+        google()
+        jcenter()
+    }
+}
+
+task clean(type: Delete) {
+    delete rootProject.buildDir
+}

+ 1 - 0
examples/example_android_opengl3/android/settings.gradle

@@ -0,0 +1 @@
+include ':app'

+ 364 - 0
examples/example_android_opengl3/main.cpp

@@ -0,0 +1,364 @@
+// dear imgui: standalone example application for Android + OpenGL ES 3
+// If you are new to dear imgui, see examples/README.txt and documentation at the top of imgui.cpp.
+
+#include "imgui.h"
+#include "imgui_impl_android.h"
+#include "imgui_impl_opengl3.h"
+#include <android/log.h>
+#include <android_native_app_glue.h>
+#include <android/asset_manager.h>
+#include <EGL/egl.h>
+#include <GLES3/gl3.h>
+
+static EGLDisplay           g_EglDisplay = EGL_NO_DISPLAY;
+static EGLSurface           g_EglSurface = EGL_NO_SURFACE;
+static EGLContext           g_EglContext = EGL_NO_CONTEXT;
+static struct android_app*  g_App = NULL;
+static bool                 g_Initialized = false;
+static char                 g_LogTag[] = "ImguiExample";
+
+// Unfortunately, there is no way to show the on-screen input from native code.
+// Therefore, we call showSoftInput() of the main activity implemented in MainActivity.kt via JNI.
+static int showSoftInput()
+{
+    JavaVM* java_vm = g_App->activity->vm;
+    JNIEnv* java_env = NULL;
+
+    jint jni_return = java_vm->GetEnv((void**)&java_env, JNI_VERSION_1_6);
+    if (jni_return == JNI_ERR)
+        return -1;
+
+    jni_return = java_vm->AttachCurrentThread(&java_env, NULL);
+    if (jni_return != JNI_OK)
+        return -2;
+
+    jclass native_activity_clazz = java_env->GetObjectClass(g_App->activity->clazz);
+    if (native_activity_clazz == NULL)
+        return -3;
+
+    jmethodID method_id = java_env->GetMethodID(native_activity_clazz, "showSoftInput", "()V");
+    if (method_id == NULL)
+        return -4;
+
+    java_env->CallVoidMethod(g_App->activity->clazz, method_id);
+
+    jni_return = java_vm->DetachCurrentThread();
+    if (jni_return != JNI_OK)
+        return -5;
+
+    return 0;
+}
+
+// Unfortunately, the native KeyEvent implementation has no getUnicodeChar() function.
+// Therefore, we implement the processing of KeyEvents in MainActivity.kt and poll
+// the resulting Unicode characters here via JNI and send them to Dear ImGui.
+static int pollUnicodeChars()
+{
+    JavaVM* java_vm = g_App->activity->vm;
+    JNIEnv* java_env = NULL;
+
+    jint jni_return = java_vm->GetEnv((void**)&java_env, JNI_VERSION_1_6);
+    if (jni_return == JNI_ERR)
+        return -1;
+
+    jni_return = java_vm->AttachCurrentThread(&java_env, NULL);
+    if (jni_return != JNI_OK)
+        return -2;
+
+    jclass native_activity_clazz = java_env->GetObjectClass(g_App->activity->clazz);
+    if (native_activity_clazz == NULL)
+        return -3;
+
+    jmethodID method_id = java_env->GetMethodID(native_activity_clazz, "pollUnicodeChar", "()I");
+    if (method_id == NULL)
+        return -4;
+
+    // Send the actual characters to Dear ImGui
+    ImGuiIO& io = ImGui::GetIO();
+    jint unicode_character;
+    while ((unicode_character = java_env->CallIntMethod(g_App->activity->clazz, method_id)) != 0)
+    {
+        io.AddInputCharacter(unicode_character);
+    }
+
+    jni_return = java_vm->DetachCurrentThread();
+    if (jni_return != JNI_OK)
+        return -5;
+
+    return 0;
+}
+
+static int GetAssetData(const char* filename, void** outData)
+{
+    int num_bytes = 0;
+    AAsset* asset_descriptor = AAssetManager_open(g_App->activity->assetManager, filename, AASSET_MODE_BUFFER);
+    if(asset_descriptor)
+    {
+        num_bytes = AAsset_getLength(asset_descriptor);
+        *outData = IM_ALLOC(num_bytes);
+        int64_t num_bytes_read = AAsset_read(asset_descriptor, *outData, num_bytes);
+        AAsset_close(asset_descriptor);
+        IM_ASSERT(num_bytes_read == num_bytes);
+    }
+    return num_bytes;
+}
+
+void init(struct android_app* app)
+{
+    if (g_Initialized)
+        return;
+
+    g_App = app;
+    ANativeWindow_acquire(g_App->window);
+
+    // Initialize EGL
+    // This is mostly boilerplate code for EGL...
+    g_EglDisplay = eglGetDisplay(EGL_DEFAULT_DISPLAY);
+
+    if (g_EglDisplay == EGL_NO_DISPLAY)
+        __android_log_print(ANDROID_LOG_ERROR, g_LogTag, "%s", "eglGetDisplay(EGL_DEFAULT_DISPLAY) returned EGL_NO_DISPLAY");
+
+    if (eglInitialize(g_EglDisplay, 0, 0) != EGL_TRUE)
+        __android_log_print(ANDROID_LOG_ERROR, g_LogTag, "%s", "eglInitialize(..) returned with an error");
+
+    const EGLint egl_attributes[] = {
+        EGL_BLUE_SIZE, 8,
+        EGL_GREEN_SIZE, 8,
+        EGL_RED_SIZE, 8,
+        EGL_DEPTH_SIZE, 24,
+        EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
+        EGL_NONE};
+
+    EGLint num_configs = 0;
+    if (eglChooseConfig(g_EglDisplay, egl_attributes, nullptr, 0, &num_configs) != EGL_TRUE)
+        __android_log_print(ANDROID_LOG_ERROR, g_LogTag, "%s", "eglChooseConfig(..) returned with an error");
+
+    if (num_configs == 0)
+        __android_log_print(ANDROID_LOG_ERROR, g_LogTag, "%s", "eglChooseConfig(..) returned 0 matching configs");
+
+    // Get the (first) matching config
+    EGLConfig egl_config;
+    eglChooseConfig(g_EglDisplay, egl_attributes, &egl_config, 1, &num_configs);
+    EGLint egl_format;
+    eglGetConfigAttrib(g_EglDisplay, egl_config, EGL_NATIVE_VISUAL_ID, &egl_format);
+    ANativeWindow_setBuffersGeometry(g_App->window, 0, 0, egl_format);
+
+    const EGLint egl_context_attributes[] = {EGL_CONTEXT_CLIENT_VERSION, 3, EGL_NONE};
+    g_EglContext = eglCreateContext(g_EglDisplay, egl_config, EGL_NO_CONTEXT, egl_context_attributes);
+
+    if (g_EglContext == EGL_NO_CONTEXT)
+        __android_log_print(ANDROID_LOG_ERROR, g_LogTag, "%s", "eglCreateContext(..) returned EGL_NO_CONTEXT");
+
+    g_EglSurface = eglCreateWindowSurface(g_EglDisplay, egl_config, g_App->window, NULL);
+    eglMakeCurrent(g_EglDisplay, g_EglSurface, g_EglSurface, g_EglContext);
+
+    // Dear Imgui
+    IMGUI_CHECKVERSION();
+    ImGui::CreateContext();
+    ImGuiIO& io = ImGui::GetIO();
+    io.IniFilename = NULL;
+    ImGui::StyleColorsDark();
+    ImGui_ImplAndroid_Init(g_App->window);
+    ImGui_ImplOpenGL3_Init("#version 300 es");
+
+    // Load Fonts
+    // - If no fonts are loaded, dear imgui will use the default font. You can also load multiple fonts and use ImGui::PushFont()/PopFont() to select them.
+    // - add_font_from_assets_ttf() will return the ImFont* so you can store it if you need to select the font among multiple.
+    // - If the file cannot be loaded, the function will return NULL. Please handle those errors in your application (e.g. use an assertion, or display an error and quit).
+    // - The fonts will be rasterized at a given size (w/ oversampling) and stored into a texture when calling ImFontAtlas::Build()/GetTexDataAsXXXX(), which ImGui_ImplXXXX_NewFrame below will call.
+    // - Read 'docs/FONTS.md' for more instructions and details.
+    // - Remember that in C/C++ if you want to include a backslash \ in a string literal you need to write a double backslash \\ !
+    // - The TTF files have to be placed into the assets/ directory (android/app/src/main/assets).
+
+    // We load the default font with increased size to improve readability on many devices with "high" DPI.
+    // FIXME: Put some effort into DPI awareness
+    ImFontConfig font_cfg;
+    font_cfg.SizePixels = 22.0f;
+    io.Fonts->AddFontDefault(&font_cfg);
+    //void* font_data;
+    //int font_data_size;
+    //ImFont* font;
+    //font_data_size = GetAssetData("Roboto-Medium.ttf", &font_data);
+    //font = io.Fonts->AddFontFromMemoryTTF(font_data, font_data_size, 16.0f); // Ownership of font_data is transfered to ImGui. Deletion is handled by ImGui.
+    //IM_ASSERT(font != NULL);
+    //font_data_size = GetAssetData("Cousine-Regular.ttf", &font_data);
+    //font = io.Fonts->AddFontFromMemoryTTF(font_data, font_data_size, 15.0f); // Ownership of font_data is transfered to ImGui. Deletion is handled by ImGui.
+    //IM_ASSERT(font != NULL);
+    //font_data_size = GetAssetData("DroidSans.ttf", &font_data);
+    //font = io.Fonts->AddFontFromMemoryTTF(font_data, font_data_size, 16.0f); // Ownership of font_data is transfered to ImGui. Deletion is handled by ImGui.
+    //IM_ASSERT(font != NULL);
+    //font_data_size = GetAssetData("ProggyTiny.ttf", &font_data);
+    //font = io.Fonts->AddFontFromMemoryTTF(font_data, font_data_size, 10.0f); // Ownership of font_data is transfered to ImGui. Deletion is handled by ImGui.
+    //IM_ASSERT(font != NULL);
+    //font_data_size = GetAssetData("ArialUni.ttf", &font_data);
+    //font = io.Fonts->AddFontFromMemoryTTF(font_data, font_data_size, 18.0f, NULL, io.Fonts->GetGlyphRangesJapanese()); // Ownership of font_data is transfered to ImGui. Deletion is handled by ImGui.
+    //IM_ASSERT(font != NULL);
+
+    // Arbitrary scale-up
+    // FIXME: Put some effort into DPI awareness
+    ImGui::GetStyle().ScaleAllSizes(3.0f);
+
+    g_Initialized = true;
+}
+
+void tick()
+{
+    // Our state (Dear Imgui)
+    static bool show_demo_window = true;
+    static bool show_another_window = false;
+    static ImVec4 clear_color = ImVec4(0.45f, 0.55f, 0.60f, 1.00f);
+
+    if (g_EglDisplay != EGL_NO_DISPLAY)
+    {
+        ImGuiIO& io = ImGui::GetIO();
+
+        // Poll Unicode characters via JNI
+        // FIXME: do not call this every frame because of JNI overhead
+        pollUnicodeChars();
+
+        // Open on-screen (soft) input if demanded by Dear ImGui
+        static bool WantTextInputLast = false;
+        if (io.WantTextInput && !WantTextInputLast)
+            showSoftInput();
+        WantTextInputLast = io.WantTextInput;
+
+        // Start the Dear ImGui frame
+        ImGui_ImplOpenGL3_NewFrame();
+        ImGui_ImplAndroid_NewFrame();
+        ImGui::NewFrame();
+
+        // 1. Show the big demo window (Most of the sample code is in ImGui::ShowDemoWindow()! You can browse its code to learn more about Dear ImGui!).
+        if (show_demo_window)
+            ImGui::ShowDemoWindow(&show_demo_window);
+
+        // 2. Show a simple window that we create ourselves. We use a Begin/End pair to created a named window.
+        {
+            static float f = 0.0f;
+            static int counter = 0;
+
+            ImGui::Begin("Hello, world!"); // Create a window called "Hello, world!" and append into it.
+
+            ImGui::Text("This is some useful text.");          // Display some text (you can use a format strings too)
+            ImGui::Checkbox("Demo Window", &show_demo_window); // Edit bools storing our window open/close state
+            ImGui::Checkbox("Another Window", &show_another_window);
+
+            ImGui::SliderFloat("float", &f, 0.0f, 1.0f);             // Edit 1 float using a slider from 0.0f to 1.0f
+            ImGui::ColorEdit3("clear color", (float*)&clear_color); // Edit 3 floats representing a color
+
+            if (ImGui::Button("Button")) // Buttons return true when clicked (most widgets return true when edited/activated)
+                counter++;
+            ImGui::SameLine();
+            ImGui::Text("counter = %d", counter);
+
+            ImGui::Text("Application average %.3f ms/frame (%.1f FPS)", 1000.0f / ImGui::GetIO().Framerate, ImGui::GetIO().Framerate);
+            ImGui::End();
+        }
+
+        // 3. Show another simple window.
+        if (show_another_window)
+        {
+            ImGui::Begin("Another Window", &show_another_window); // Pass a pointer to our bool variable (the window will have a closing button that will clear the bool when clicked)
+            ImGui::Text("Hello from another window!");
+            if (ImGui::Button("Close Me"))
+                show_another_window = false;
+            ImGui::End();
+        }
+
+        // Rendering
+        ImGui::Render();
+        glViewport(0, 0, (int)io.DisplaySize.x, (int)io.DisplaySize.y);
+        glClearColor(clear_color.x, clear_color.y, clear_color.z, clear_color.w);
+        glClear(GL_COLOR_BUFFER_BIT);
+        ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData());
+        eglSwapBuffers(g_EglDisplay, g_EglSurface);
+    }
+}
+
+void shutdown()
+{
+    if (!g_Initialized)
+        return;
+
+    // Cleanup (Dear Imgui)
+    ImGui_ImplOpenGL3_Shutdown();
+    ImGui_ImplAndroid_Shutdown();
+    ImGui::DestroyContext();
+
+    if (g_EglDisplay != EGL_NO_DISPLAY)
+    {
+        eglMakeCurrent(g_EglDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
+
+        if (g_EglContext != EGL_NO_CONTEXT)
+            eglDestroyContext(g_EglDisplay, g_EglContext);
+
+        if (g_EglSurface != EGL_NO_SURFACE)
+            eglDestroySurface(g_EglDisplay, g_EglSurface);
+
+        eglTerminate(g_EglDisplay);
+    }
+
+    g_EglDisplay = EGL_NO_DISPLAY;
+    g_EglContext = EGL_NO_CONTEXT;
+    g_EglSurface = EGL_NO_SURFACE;
+    ANativeWindow_release(g_App->window);
+
+    g_Initialized = false;
+}
+
+static void handleAppCmd(struct android_app* app, int32_t appCmd)
+{
+    switch (appCmd)
+    {
+    case APP_CMD_SAVE_STATE:
+        break;
+    case APP_CMD_INIT_WINDOW:
+        init(app);
+        break;
+    case APP_CMD_TERM_WINDOW:
+        shutdown();
+        break;
+    case APP_CMD_GAINED_FOCUS:
+        break;
+    case APP_CMD_LOST_FOCUS:
+        break;
+    }
+}
+
+static int32_t handleInputEvent(struct android_app* app, AInputEvent* inputEvent)
+{
+    return ImGui_ImplAndroid_HandleInputEvent(inputEvent);
+}
+
+void android_main(struct android_app* app)
+{
+    app->onAppCmd = handleAppCmd;
+    app->onInputEvent = handleInputEvent;
+
+    while (true)
+    {
+        int out_events;
+        struct android_poll_source* out_data;
+
+        // Poll all events. If the app is not visible, this loop blocks until g_Initialized == true.
+        while (ALooper_pollAll(g_Initialized ? 0 : -1, NULL, &out_events, (void**)&out_data) >= 0)
+        {
+            // Process one event
+            if (out_data != NULL)
+                out_data->process(app, out_data);
+
+            // Exit the app by returning from within the infinite loop
+            if (app->destroyRequested != 0)
+            {
+                // shutdown() should have been called already while processing the
+                // app command APP_CMD_TERM_WINDOW. But we play save here
+                if (!g_Initialized)
+                    shutdown();
+
+                return;
+            }
+        }
+
+        // Initiate a new frame
+        tick();
+    }
+}