]> git.tdb.fi Git - libs/core.git/blob - source/core/android/mainthread.cpp
Deal with the activity being recreated during process lifetime
[libs/core.git] / source / core / android / mainthread.cpp
1 #include <cstring>
2 #include <msp/fs/path.h>
3 #include "application.h"
4 #include "errorlogger.h"
5 #include "mainthread.h"
6
7 using namespace std;
8
9 namespace Msp {
10 namespace Android {
11
12 MainThread::MainThread(ANativeActivity *a):
13         starting_up(true)
14 {
15         attach_to_activity(a);
16         startup_mutex.lock();
17         launch();
18 }
19
20 void MainThread::attach_to_activity(ANativeActivity *a)
21 {
22         activity = a;
23         activity->callbacks->onDestroy = &activity_destroyed;
24         activity->callbacks->onNativeWindowCreated = &native_window_created;
25         activity->callbacks->onNativeWindowResized = &native_window_resized;
26         activity->callbacks->onNativeWindowDestroyed = &native_window_destroyed;
27         activity->callbacks->onInputQueueCreated = &input_queue_created;
28         activity->callbacks->onInputQueueDestroyed = &input_queue_destroyed;
29         activity->instance = this;
30 }
31
32 void MainThread::wait_for_app_created()
33 {
34         MutexLock lock(startup_mutex);
35 }
36
37 void MainThread::resume_startup()
38 {
39         if(starting_up)
40         {
41                 starting_up = false;
42                 startup_mutex.unlock();
43         }
44 }
45
46 void MainThread::main()
47 {
48         /* I have no idea how dependable this is, but it seems to be the only way
49         to get the package name aside from making a Java call through JNI */
50         char *appname = strdup(FS::Path(activity->internalDataPath)[-2].c_str());
51         char *argv[] = { appname, 0 };
52         Msp::Android::ErrorLogger err_logger;
53         Msp::Application::run(1, argv, this, &app_created);
54         free(appname);
55         if(activity)
56                 ANativeActivity_finish(activity);
57 }
58
59 void MainThread::app_created(void *data)
60 {
61         reinterpret_cast<MainThread *>(data)->resume_startup();
62 }
63
64 void MainThread::activity_destroyed(ANativeActivity *activity)
65 {
66         reinterpret_cast<MainThread *>(activity->instance)->activity = 0;
67 }
68
69 void MainThread::native_window_created(ANativeActivity *activity, ANativeWindow *window)
70 {
71         reinterpret_cast<MainThread *>(activity->instance)->signal_native_window_created.emit(window);
72 }
73
74 void MainThread::native_window_resized(ANativeActivity *activity, ANativeWindow *window)
75 {
76         reinterpret_cast<MainThread *>(activity->instance)->signal_native_window_resized.emit(window);
77 }
78
79 void MainThread::native_window_destroyed(ANativeActivity *activity, ANativeWindow *window)
80 {
81         reinterpret_cast<MainThread *>(activity->instance)->signal_native_window_destroyed.emit(window);
82 }
83
84 void MainThread::input_queue_created(ANativeActivity *activity, AInputQueue *queue)
85 {
86         reinterpret_cast<MainThread *>(activity->instance)->signal_input_queue_created.emit(queue);
87 }
88
89 void MainThread::input_queue_destroyed(ANativeActivity *activity, AInputQueue *queue)
90 {
91         reinterpret_cast<MainThread *>(activity->instance)->signal_input_queue_destroyed.emit(queue);
92 }
93
94 } // namespace Android
95 } // namespace Msp