]> git.tdb.fi Git - libs/core.git/blob - source/core/android/mainthread.cpp
Add window and input management signals for Android
[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         activity(a),
14         starting_up(true)
15 {
16         activity->callbacks->onNativeWindowCreated = &native_window_created;
17         activity->callbacks->onNativeWindowResized = &native_window_resized;
18         activity->callbacks->onNativeWindowDestroyed = &native_window_destroyed;
19         activity->callbacks->onInputQueueCreated = &input_queue_created;
20         activity->callbacks->onInputQueueDestroyed = &input_queue_destroyed;
21         activity->instance = this;
22
23         startup_mutex.lock();
24
25         launch();
26 }
27
28 void MainThread::wait_for_app_created()
29 {
30         MutexLock lock(startup_mutex);
31 }
32
33 void MainThread::resume_startup()
34 {
35         if(starting_up)
36         {
37                 starting_up = false;
38                 startup_mutex.unlock();
39         }
40 }
41
42 void MainThread::main()
43 {
44         /* I have no idea how dependable this is, but it seems to be the only way
45         to get the package name aside from making a Java call through JNI */
46         char *appname = strdup(FS::Path(activity->internalDataPath)[-2].c_str());
47         char *argv[] = { appname, 0 };
48         Msp::Android::ErrorLogger err_logger;
49         Msp::Application::run(1, argv, this, &app_created);
50         free(appname);
51         ANativeActivity_finish(activity);
52 }
53
54 void MainThread::app_created(void *data)
55 {
56         reinterpret_cast<MainThread *>(data)->resume_startup();
57 }
58
59 void MainThread::native_window_created(ANativeActivity *activity, ANativeWindow *window)
60 {
61         reinterpret_cast<MainThread *>(activity->instance)->signal_native_window_created.emit(window);
62 }
63
64 void MainThread::native_window_resized(ANativeActivity *activity, ANativeWindow *window)
65 {
66         reinterpret_cast<MainThread *>(activity->instance)->signal_native_window_resized.emit(window);
67 }
68
69 void MainThread::native_window_destroyed(ANativeActivity *activity, ANativeWindow *window)
70 {
71         reinterpret_cast<MainThread *>(activity->instance)->signal_native_window_destroyed.emit(window);
72 }
73
74 void MainThread::input_queue_created(ANativeActivity *activity, AInputQueue *queue)
75 {
76         reinterpret_cast<MainThread *>(activity->instance)->signal_input_queue_created.emit(queue);
77 }
78
79 void MainThread::input_queue_destroyed(ANativeActivity *activity, AInputQueue *queue)
80 {
81         reinterpret_cast<MainThread *>(activity->instance)->signal_input_queue_destroyed.emit(queue);
82 }
83
84 } // namespace Android
85 } // namespace Msp