Lugdunum  0.1.0
MainAndroid.cpp
Go to the documentation of this file.
1 #include <thread>
2 #include <mutex>
5 
6 namespace lug {
7 namespace Main {
8 
9 AndroidApp::AndroidApp(ANativeActivity* activity, void* savedState, size_t savedStateSize)
10  : _savedState{}, _savedStateSize{} {
11  activity->callbacks->onInputQueueCreated = &AndroidApp::onInputQueueCreated;
12  activity->callbacks->onInputQueueDestroyed = &AndroidApp::onInputQueueDestroyed;
13 
14  activity->callbacks->onNativeWindowCreated = &AndroidApp::onNativeWindowCreated;
15  activity->callbacks->onNativeWindowDestroyed = &AndroidApp::onNativeWindowDestroyed;
16  activity->callbacks->onNativeWindowResized = &AndroidApp::onNativeWindowResized;
17  activity->callbacks->onNativeWindowRedrawNeeded = &AndroidApp::onNativeWindowRedrawNeeded;
18  activity->callbacks->onContentRectChanged = &AndroidApp::onContentRectChanged;
19 
20  activity->callbacks->onWindowFocusChanged = &AndroidApp::onWindowFocusChanged;
21  activity->callbacks->onConfigurationChanged = &AndroidApp::onConfigurationChanged;
22  activity->callbacks->onLowMemory = &AndroidApp::onLowMemory;
23  activity->callbacks->onStart = &AndroidApp::onStart;
24  activity->callbacks->onResume = &AndroidApp::onResume;
25  activity->callbacks->onSaveInstanceState = &AndroidApp::onSaveInstanceState;
26  activity->callbacks->onPause = &AndroidApp::onPause;
27  activity->callbacks->onStop = &AndroidApp::onStop;
28  activity->callbacks->onDestroy = &AndroidApp::onDestroy;
29 
30  if (savedState != nullptr) {
31  _savedState = ::operator new(savedStateSize);
32  _savedStateSize = savedStateSize;
33  std::memcpy(_savedState, savedState, savedStateSize);
34  }
35 
36  std::thread t(&AndroidApp::startApplication, this);
37  t.detach();
38 }
39 
41  if (_savedState != nullptr) {
42  ::operator delete(_savedState);
43  _savedState = nullptr;
44  _savedStateSize = 0;
45  }
46 
47  delete this;
48 }
49 
51  main(0, nullptr);
53 }
54 
55 void AndroidApp::onStart(ANativeActivity*) {}
56 
57 void AndroidApp::onResume(ANativeActivity*) {}
58 
59 void* AndroidApp::onSaveInstanceState(ANativeActivity*, size_t*) {
60  return nullptr;
61 }
62 
63 void AndroidApp::onPause(ANativeActivity*) {}
64 
65 void AndroidApp::onStop(ANativeActivity*) {}
66 
67 void AndroidApp::onDestroy(ANativeActivity* ) {
68  std::lock_guard<std::mutex> lock(lug::Window::priv::WindowImpl::androidMutex);
71  lug::Window::priv::WindowImpl::events.push(std::move(e));
72 }
73 
74 void AndroidApp::onWindowFocusChanged(ANativeActivity*, int) {}
75 
76 void AndroidApp::onNativeWindowCreated(ANativeActivity*, ANativeWindow* window) {
77  std::lock_guard<std::mutex> lk(lug::Window::priv::WindowImpl::androidMutex);
80 }
81 
82 void AndroidApp::onNativeWindowResized(ANativeActivity*, ANativeWindow* ) {}
83 
84 void AndroidApp::onNativeWindowRedrawNeeded(ANativeActivity*, ANativeWindow*) {}
85 
86 void AndroidApp::onNativeWindowDestroyed(ANativeActivity*, ANativeWindow* window) {
87  std::lock_guard<std::mutex> lock(lug::Window::priv::WindowImpl::androidMutex);
89 }
90 
91 void AndroidApp::onInputQueueCreated(ANativeActivity*, AInputQueue* input) {
92  std::lock_guard<std::mutex> lock(lug::Window::priv::WindowImpl::androidMutex);
94 }
95 
96 void AndroidApp::onInputQueueDestroyed(ANativeActivity*, AInputQueue*) {
97  std::lock_guard<std::mutex> lock(lug::Window::priv::WindowImpl::androidMutex);
99 }
100 
101 void AndroidApp::onContentRectChanged(ANativeActivity*, const ARect*) {}
102 
103 void AndroidApp::onConfigurationChanged(ANativeActivity*) {}
104 
105 void AndroidApp::onLowMemory(ANativeActivity*) {}
106 
107 } // Main
108 } // lug
109 
110 void ANativeActivity_onCreate(ANativeActivity* activity, void* savedState, size_t savedStateSize) {
111  std::lock_guard<std::mutex> lock(lug::Window::priv::WindowImpl::androidMutex);
112  activity->instance = new lug::Main::AndroidApp(activity, savedState, savedStateSize);
114 }
static void * onSaveInstanceState(ANativeActivity *activity, size_t *outSize)
Definition: MainAndroid.cpp:59
Represents an event.
Definition: Event.hpp:89
int main(int argc, const char *argv[])
static void onNativeWindowCreated(ANativeActivity *activity, ANativeWindow *window)
Definition: MainAndroid.cpp:76
static void onInputQueueDestroyed(ANativeActivity *activity, AInputQueue *queue)
Definition: MainAndroid.cpp:96
static void onStart(ANativeActivity *activity)
Definition: MainAndroid.cpp:55
static void onNativeWindowDestroyed(ANativeActivity *activity, ANativeWindow *window)
Definition: MainAndroid.cpp:86
static void onDestroy(ANativeActivity *activity)
Definition: MainAndroid.cpp:67
static void onInputQueueCreated(ANativeActivity *activity, AInputQueue *queue)
Definition: MainAndroid.cpp:91
static void onConfigurationChanged(ANativeActivity *activity)
static void onNativeWindowRedrawNeeded(ANativeActivity *activity, ANativeWindow *window)
Definition: MainAndroid.cpp:84
static ANativeWindow * nativeWindow
static void onWindowFocusChanged(ANativeActivity *activity, int hasFocus)
Definition: MainAndroid.cpp:74
Type type
The type of the event.
Definition: Event.hpp:110
static void onNativeWindowResized(ANativeActivity *activity, ANativeWindow *window)
Definition: MainAndroid.cpp:82
T e()
Definition: Constant.inl:82
static std::queue< lug::Window::Event > events
void ANativeActivity_onCreate(ANativeActivity *activity, void *savedState, size_t savedStateSize)
static ANativeActivity * activity
static void onPause(ANativeActivity *activity)
Definition: MainAndroid.cpp:63
static void onResume(ANativeActivity *activity)
Definition: MainAndroid.cpp:57
static void onLowMemory(ANativeActivity *activity)
static void onContentRectChanged(ANativeActivity *activity, const ARect *rect)
AndroidApp(ANativeActivity *activity, void *savedState, size_t savedStateSize)
Definition: MainAndroid.cpp:9
static std::condition_variable cv
static void onStop(ANativeActivity *activity)
Definition: MainAndroid.cpp:65