]> git.tdb.fi Git - libs/core.git/blob - source/core/android/mainthread.cpp
Use nullptr instead of 0 for pointers
[libs/core.git] / source / core / android / mainthread.cpp
1 #include <cstring>
2 #include <cstdlib>
3 #include <android/window.h>
4 #include <msp/fs/dir.h>
5 #include <msp/fs/path.h>
6 #include <msp/fs/utils.h>
7 #include "application.h"
8 #include "errorlogger.h"
9 #include "mainthread.h"
10
11 using namespace std;
12
13 namespace Msp {
14 namespace Android {
15
16 MainThread::MainThread(ANativeActivity *a):
17         asset_manager(a->assetManager),
18         int_data_path(a->internalDataPath)
19 {
20         attach_to_activity(a);
21         startup_mutex.lock();
22         launch();
23 }
24
25 void MainThread::attach_to_activity(ANativeActivity *a)
26 {
27         activity = a;
28         activity->callbacks->onDestroy = &activity_destroyed;
29         activity->callbacks->onNativeWindowCreated = &native_window_created;
30         activity->callbacks->onNativeWindowResized = &native_window_resized;
31         activity->callbacks->onNativeWindowDestroyed = &native_window_destroyed;
32         activity->callbacks->onInputQueueCreated = &input_queue_created;
33         activity->callbacks->onInputQueueDestroyed = &input_queue_destroyed;
34         activity->instance = this;
35
36         set_window_flags(AWINDOW_FLAG_FULLSCREEN, 0);
37 }
38
39 void MainThread::wait_for_app_created()
40 {
41         MutexLock lock(startup_mutex);
42 }
43
44 void MainThread::resume_startup()
45 {
46         if(starting_up)
47         {
48                 starting_up = false;
49                 startup_mutex.unlock();
50         }
51 }
52
53 JavaVM *MainThread::get_java_vm() const
54 {
55         if(!activity)
56                 return nullptr;
57
58         return activity->vm;
59 }
60
61 void MainThread::set_window_flags(unsigned set, unsigned clear)
62 {
63         ANativeActivity_setWindowFlags(activity, set, clear);
64 }
65
66 void MainThread::main()
67 {
68         /* I have no idea how dependable this is, but it seems to be the only way
69         to get the package name aside from making a Java call through JNI */
70         char *appname = strdup(int_data_path[-2].c_str());
71         char *argv[] = { appname, nullptr };
72         Msp::Android::ErrorLogger err_logger;
73         FS::chdir(FS::dirname(int_data_path));
74         Msp::Application::run(1, argv, this, &app_created);
75         free(appname);
76         if(activity)
77         {
78                 activity->callbacks->onDestroy = &end_process;
79                 ANativeActivity_finish(activity);
80         }
81         else
82                 end_process(0);
83 }
84
85 void MainThread::app_created(void *data)
86 {
87         reinterpret_cast<MainThread *>(data)->resume_startup();
88 }
89
90 void MainThread::activity_destroyed(ANativeActivity *activity)
91 {
92         reinterpret_cast<MainThread *>(activity->instance)->activity = 0;
93 }
94
95 void MainThread::native_window_created(ANativeActivity *activity, ANativeWindow *window)
96 {
97         reinterpret_cast<MainThread *>(activity->instance)->signal_native_window_created.emit(window);
98 }
99
100 void MainThread::native_window_resized(ANativeActivity *activity, ANativeWindow *window)
101 {
102         reinterpret_cast<MainThread *>(activity->instance)->signal_native_window_resized.emit(window);
103 }
104
105 void MainThread::native_window_destroyed(ANativeActivity *activity, ANativeWindow *window)
106 {
107         reinterpret_cast<MainThread *>(activity->instance)->signal_native_window_destroyed.emit(window);
108 }
109
110 void MainThread::input_queue_created(ANativeActivity *activity, AInputQueue *queue)
111 {
112         reinterpret_cast<MainThread *>(activity->instance)->signal_input_queue_created.emit(queue);
113 }
114
115 void MainThread::input_queue_destroyed(ANativeActivity *activity, AInputQueue *queue)
116 {
117         reinterpret_cast<MainThread *>(activity->instance)->signal_input_queue_destroyed.emit(queue);
118 }
119
120 void MainThread::end_process(ANativeActivity *)
121 {
122         exit(0);
123 }
124
125 } // namespace Android
126 } // namespace Msp