2018-03-03 14:37:53 +00:00
|
|
|
//
|
|
|
|
|
// "$Id$"
|
|
|
|
|
//
|
|
|
|
|
// Android Native Application interface
|
|
|
|
|
// for the Fast Light Tool Kit (FLTK).
|
|
|
|
|
//
|
|
|
|
|
// Copyright 2018 by Bill Spitzak and others.
|
|
|
|
|
//
|
|
|
|
|
// This library is free software. Distribution and use rights are outlined in
|
|
|
|
|
// the file "COPYING" which should have been included with this file. If this
|
|
|
|
|
// file is missing or damaged, see the license at:
|
|
|
|
|
//
|
|
|
|
|
// http://www.fltk.org/COPYING.php
|
|
|
|
|
//
|
|
|
|
|
// Please report all bugs and problems on the following page:
|
|
|
|
|
//
|
|
|
|
|
// http://www.fltk.org/str.php
|
|
|
|
|
//
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
\file Fl_Android_Application.H
|
|
|
|
|
\brief Definition of Android Native Application interface
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#ifndef FL_ANDROID_APPLICATION_H
|
|
|
|
|
#define FL_ANDROID_APPLICATION_H
|
|
|
|
|
|
|
|
|
|
#include <FL/Fl.H>
|
|
|
|
|
|
|
|
|
|
extern void (*fl_unlock_function)();
|
|
|
|
|
extern void (*fl_lock_function)();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#include <poll.h>
|
|
|
|
|
#include <pthread.h>
|
|
|
|
|
#include <sched.h>
|
|
|
|
|
|
|
|
|
|
#include <android/configuration.h>
|
|
|
|
|
#include <android/looper.h>
|
|
|
|
|
#include <android/native_activity.h>
|
|
|
|
|
|
|
|
|
|
/**
|
2018-03-12 20:10:49 +00:00
|
|
|
* Static class that manages all interaction between the Android Native Activity
|
|
|
|
|
* and the FLTK library. It also keeps often used data for global access.
|
2018-03-04 13:22:52 +00:00
|
|
|
*
|
|
|
|
|
* On launch, it creates a main thread and communication pipe to
|
|
|
|
|
* the Activity. All FLTK code will run in that thread. Activity
|
2018-03-12 20:10:49 +00:00
|
|
|
* events will be polled by the Screen driver using the provided
|
|
|
|
|
* Android Looper, and will also be routed back to this class as needed.
|
2018-03-04 13:22:52 +00:00
|
|
|
*
|
|
|
|
|
* This code is based on the native activity interface
|
|
|
|
|
* provided by <android/native_activity.h>. It is based on a set
|
|
|
|
|
* of application-provided callbacks that will be called
|
2018-03-03 14:37:53 +00:00
|
|
|
* by the Activity's main thread when certain events occur.
|
|
|
|
|
*
|
2018-03-04 13:22:52 +00:00
|
|
|
* 1/ The application must provide a function named "int main(argc, argv)" that
|
2018-03-03 14:37:53 +00:00
|
|
|
* will be called when the activity is created, in a new thread that is
|
|
|
|
|
* distinct from the activity's main thread.
|
|
|
|
|
*
|
2018-03-12 20:10:49 +00:00
|
|
|
* 2/ The application has access to a static "Fl_Android_Application" class
|
2018-03-03 14:37:53 +00:00
|
|
|
* that contains references to other important objects, e.g. the
|
2018-03-12 20:10:49 +00:00
|
|
|
* ANativeActivity object instance the application is running in.
|
2018-03-03 14:37:53 +00:00
|
|
|
*
|
2018-03-12 20:10:49 +00:00
|
|
|
* 3/ the "Fl_Android_Application" class holds an ALooper instance that already
|
2018-03-03 14:37:53 +00:00
|
|
|
* listens to two important things:
|
|
|
|
|
*
|
|
|
|
|
* - activity lifecycle events (e.g. "pause", "resume"). See APP_CMD_XXX
|
|
|
|
|
* declarations below.
|
|
|
|
|
*
|
|
|
|
|
* - input events coming from the AInputQueue attached to the activity.
|
|
|
|
|
*
|
|
|
|
|
* Each of these correspond to an ALooper identifier returned by
|
|
|
|
|
* ALooper_pollOnce with values of LOOPER_ID_MAIN and LOOPER_ID_INPUT,
|
|
|
|
|
* respectively.
|
|
|
|
|
*
|
2018-03-12 20:10:49 +00:00
|
|
|
* FLTK will add more items to the looper for timers and file and socket
|
|
|
|
|
* communication. (fl_add_timeout, Fl::add_fd(), ...
|
2018-03-03 14:37:53 +00:00
|
|
|
*/
|
2018-03-04 13:22:52 +00:00
|
|
|
class Fl_Android_Application
|
|
|
|
|
{
|
2018-03-07 23:06:55 +00:00
|
|
|
public:
|
|
|
|
|
|
|
|
|
|
enum {
|
|
|
|
|
/**
|
|
|
|
|
* Looper data ID of commands coming from the app's main thread, which
|
|
|
|
|
* is returned as an identifier from ALooper_pollOnce(). The data for this
|
|
|
|
|
* identifier is a pointer to an android_poll_source structure.
|
|
|
|
|
* These can be retrieved and processed with android_app_read_cmd()
|
|
|
|
|
* and android_app_exec_cmd().
|
|
|
|
|
*/
|
|
|
|
|
LOOPER_ID_MAIN = 1,
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Looper data ID of events coming from the AInputQueue of the
|
|
|
|
|
* application's window, which is returned as an identifier from
|
|
|
|
|
* ALooper_pollOnce(). The data for this identifier is a pointer to an
|
|
|
|
|
* android_poll_source structure. These can be read via the inputQueue
|
|
|
|
|
* object of android_app.
|
|
|
|
|
*/
|
2018-03-12 20:10:49 +00:00
|
|
|
LOOPER_ID_INPUT,
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Timer data ID of all timer events coming from the Unix timer_create()
|
|
|
|
|
* and friends, used in fl_add_timeout() and colleagues.
|
|
|
|
|
*/
|
|
|
|
|
LOOPER_ID_TIMER,
|
2018-03-07 23:06:55 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Start of user-defined ALooper identifiers.
|
|
|
|
|
*/
|
2018-03-12 20:10:49 +00:00
|
|
|
LOOPER_ID_USER,
|
2018-03-07 23:06:55 +00:00
|
|
|
};
|
|
|
|
|
|
2018-03-09 21:21:23 +00:00
|
|
|
/**
|
|
|
|
|
* @see android.H Fl_Android_Platform_Event
|
|
|
|
|
*/
|
2018-03-07 23:06:55 +00:00
|
|
|
enum {
|
|
|
|
|
APP_CMD_INPUT_CHANGED,
|
|
|
|
|
APP_CMD_INIT_WINDOW,
|
|
|
|
|
APP_CMD_TERM_WINDOW,
|
|
|
|
|
APP_CMD_WINDOW_RESIZED,
|
|
|
|
|
APP_CMD_WINDOW_REDRAW_NEEDED,
|
|
|
|
|
APP_CMD_CONTENT_RECT_CHANGED,
|
|
|
|
|
APP_CMD_GAINED_FOCUS,
|
|
|
|
|
APP_CMD_LOST_FOCUS,
|
|
|
|
|
APP_CMD_CONFIG_CHANGED,
|
|
|
|
|
APP_CMD_LOW_MEMORY,
|
|
|
|
|
APP_CMD_START,
|
|
|
|
|
APP_CMD_RESUME,
|
|
|
|
|
APP_CMD_SAVE_STATE,
|
|
|
|
|
APP_CMD_PAUSE,
|
|
|
|
|
APP_CMD_STOP,
|
|
|
|
|
APP_CMD_DESTROY,
|
|
|
|
|
};
|
|
|
|
|
|
2018-03-04 13:22:52 +00:00
|
|
|
public:
|
2018-03-12 20:10:49 +00:00
|
|
|
// --- logging
|
2018-03-04 13:22:52 +00:00
|
|
|
static void log_e(const char *text, ...);
|
|
|
|
|
static void log_w(const char *text, ...);
|
|
|
|
|
static void log_i(const char *text, ...);
|
|
|
|
|
static void log_v(const char *text, ...);
|
|
|
|
|
|
2018-03-12 20:10:49 +00:00
|
|
|
// --- application state stuff
|
2018-03-04 13:22:52 +00:00
|
|
|
static int8_t read_cmd();
|
|
|
|
|
static void pre_exec_cmd(int8_t cmd);
|
|
|
|
|
static void post_exec_cmd(int8_t cmd);
|
2018-03-12 20:10:49 +00:00
|
|
|
static int destroy_requested() { return pDestroyRequested; }
|
2018-03-17 19:32:05 +00:00
|
|
|
static const char *get_internal_data_path() { return pActivity->internalDataPath; }
|
|
|
|
|
static const char *get_external_data_path() { return pActivity->externalDataPath; }
|
|
|
|
|
static AAssetManager *get_asset_manager() { return pActivity->assetManager; }
|
2018-03-24 17:08:25 +00:00
|
|
|
static ANativeActivity *get_activity() { return pActivity; }
|
2018-03-12 20:10:49 +00:00
|
|
|
|
|
|
|
|
// --- event handling
|
2018-03-07 22:12:34 +00:00
|
|
|
static AInputQueue *input_event_queue() { return pInputQueue; }
|
2018-03-04 13:22:52 +00:00
|
|
|
|
2018-03-12 20:10:49 +00:00
|
|
|
// --- screen stuff
|
|
|
|
|
static bool copy_screen();
|
2018-03-05 21:26:51 +00:00
|
|
|
static inline ANativeWindow *native_window() { return pNativeWindow; }
|
2018-03-05 22:57:33 +00:00
|
|
|
static inline ANativeWindow_Buffer &graphics_buffer() { return pApplicationWindowBuffer; }
|
2018-03-04 13:22:52 +00:00
|
|
|
|
2018-03-12 20:10:49 +00:00
|
|
|
// --- timer stuff
|
|
|
|
|
static void send_timer_index(uint8_t ix);
|
|
|
|
|
static uint8_t receive_timer_index();
|
|
|
|
|
|
2018-03-05 21:26:51 +00:00
|
|
|
|
2018-03-04 13:22:52 +00:00
|
|
|
protected:
|
|
|
|
|
static void free_saved_state();
|
|
|
|
|
static void print_cur_config();
|
|
|
|
|
static void destroy();
|
|
|
|
|
static void* thread_entry(void* param);
|
|
|
|
|
|
2018-03-12 20:10:49 +00:00
|
|
|
// --- screen handling stuff
|
2018-03-05 22:57:33 +00:00
|
|
|
static void allocate_screen();
|
|
|
|
|
static bool lock_screen();
|
|
|
|
|
static void unlock_and_post_screen();
|
|
|
|
|
static bool screen_is_locked();
|
|
|
|
|
|
2018-03-12 20:10:49 +00:00
|
|
|
// --- timer stuff
|
|
|
|
|
static void create_timer_handler();
|
|
|
|
|
static void destroy_timer_handler();
|
|
|
|
|
|
2018-03-04 13:22:52 +00:00
|
|
|
static ANativeActivity *pActivity;
|
2018-03-05 21:26:51 +00:00
|
|
|
static AConfiguration *pConfig;
|
|
|
|
|
static void *pSavedState;
|
2018-03-04 13:22:52 +00:00
|
|
|
static size_t pSavedStateSize;
|
2018-03-12 20:10:49 +00:00
|
|
|
static ALooper *pAppLooper;
|
2018-03-05 21:26:51 +00:00
|
|
|
static AInputQueue *pInputQueue;
|
|
|
|
|
static ANativeWindow *pNativeWindow;
|
|
|
|
|
static ANativeWindow_Buffer pNativeWindowBuffer;
|
2018-03-05 22:57:33 +00:00
|
|
|
static ANativeWindow_Buffer pApplicationWindowBuffer;
|
2018-03-04 13:22:52 +00:00
|
|
|
static int pActivityState;
|
|
|
|
|
static int pDestroyRequested;
|
|
|
|
|
|
|
|
|
|
// ---- no need to make these visible to the outside ----
|
|
|
|
|
static pthread_mutex_t pMutex;
|
|
|
|
|
static pthread_cond_t pCond;
|
2018-03-07 16:29:31 +00:00
|
|
|
static int pMsgReadPipe;
|
|
|
|
|
static int pMsgWritePipe;
|
2018-03-04 13:22:52 +00:00
|
|
|
static pthread_t pThread;
|
|
|
|
|
static int pRunning;
|
|
|
|
|
static int pStateSaved;
|
|
|
|
|
static int pDestroyed;
|
|
|
|
|
static AInputQueue* pPendingInputQueue;
|
|
|
|
|
static ANativeWindow* pPendingWindow;
|
2018-03-12 20:10:49 +00:00
|
|
|
|
|
|
|
|
// --- timer variables
|
|
|
|
|
static int pTimerReadPipe;
|
|
|
|
|
static int pTimerWritePipe;
|
|
|
|
|
|
2018-03-04 13:22:52 +00:00
|
|
|
};
|
2018-03-03 14:37:53 +00:00
|
|
|
|
2018-03-04 13:22:52 +00:00
|
|
|
|
|
|
|
|
class Fl_Android_Activity : public Fl_Android_Application
|
|
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
static void create(ANativeActivity* activity, void* savedState, size_t savedStateSize);
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
static void set_activity(ANativeActivity *a) { pActivity = a; }
|
|
|
|
|
static void set_callbacks();
|
|
|
|
|
|
|
|
|
|
// ---- Android Native Activity interface
|
|
|
|
|
static void write_cmd(int8_t cmd);
|
|
|
|
|
static void set_input(AInputQueue* inputQueue);
|
|
|
|
|
static void set_window(ANativeWindow* window);
|
|
|
|
|
static void set_activity_state(int8_t cmd);
|
2018-03-12 20:54:27 +00:00
|
|
|
static void close_activity();
|
2018-03-04 13:22:52 +00:00
|
|
|
|
|
|
|
|
// ---- Android Native Activity callbacks ----
|
|
|
|
|
static void onContentRectChanged(ANativeActivity *activity, const ARect *rect);
|
|
|
|
|
static void onNativeWindowRedrawNeeded(ANativeActivity *activity, ANativeWindow *window);
|
|
|
|
|
static void onNativeWindowResized(ANativeActivity *activity, ANativeWindow *window);
|
|
|
|
|
static void onDestroy(ANativeActivity* activity);
|
|
|
|
|
static void onStart(ANativeActivity* activity);
|
|
|
|
|
static void onResume(ANativeActivity* activity);
|
|
|
|
|
static void* onSaveInstanceState(ANativeActivity* activity, size_t* outLen);
|
|
|
|
|
static void onPause(ANativeActivity* activity);
|
|
|
|
|
static void onStop(ANativeActivity* activity);
|
|
|
|
|
static void onConfigurationChanged(ANativeActivity* activity);
|
|
|
|
|
static void onLowMemory(ANativeActivity* activity);
|
|
|
|
|
static void onWindowFocusChanged(ANativeActivity* activity, int focused);
|
|
|
|
|
static void onNativeWindowCreated(ANativeActivity* activity, ANativeWindow* window);
|
|
|
|
|
static void onNativeWindowDestroyed(ANativeActivity* activity, ANativeWindow* window);
|
|
|
|
|
static void onInputQueueCreated(ANativeActivity* activity, AInputQueue* queue);
|
|
|
|
|
static void onInputQueueDestroyed(ANativeActivity* activity, AInputQueue* queue);
|
|
|
|
|
};
|
2018-03-03 14:37:53 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
#endif // FL_ANDROID_APPLICATION_H
|
|
|
|
|
|
|
|
|
|
//
|
|
|
|
|
// End of "$Id$".
|
|
|
|
|
//
|