Expose window flags and set fullscreen mode by default
[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         starting_up(true)
20 {
21         attach_to_activity(a);
22         startup_mutex.lock();
23         launch();
24 }
25
26 void MainThread::attach_to_activity(ANativeActivity *a)
27 {
28         activity = a;
29         activity->callbacks->onDestroy = &activity_destroyed;
30         activity->callbacks->onNativeWindowCreated = &native_window_created;
31         activity->callbacks->onNativeWindowResized = &native_window_resized;
32         activity->callbacks->onNativeWindowDestroyed = &native_window_destroyed;
33         activity->callbacks->onInputQueueCreated = &input_queue_created;
34         activity->callbacks->onInputQueueDestroyed = &input_queue_destroyed;
35         activity->instance = this;
36
37         set_window_flags(AWINDOW_FLAG_FULLSCREEN, 0);
38 }
39
40 void MainThread::wait_for_app_created()
41 {
42         MutexLock lock(startup_mutex);
43 }
44
45 void MainThread::resume_startup()
46 {
47         if(starting_up)
48         {
49                 starting_up = false;
50                 startup_mutex.unlock();
51         }
52 }
53
54 void MainThread::set_window_flags(unsigned set, unsigned clear)
55 {
56         ANativeActivity_setWindowFlags(activity, set, clear);
57 }
58
59 void MainThread::main()
60 {
61         /* I have no idea how dependable this is, but it seems to be the only way
62         to get the package name aside from making a Java call through JNI */
63         char *appname = strdup(int_data_path[-2].c_str());
64         char *argv[] = { appname, 0 };
65         Msp::Android::ErrorLogger err_logger;
66         FS::chdir(FS::dirname(int_data_path));
67         Msp::Application::run(1, argv, this, &app_created);
68         free(appname);
69         if(activity)
70         {
71                 activity->callbacks->onDestroy = &end_process;
72                 ANativeActivity_finish(activity);
73         }
74         else
75                 end_process(0);
76 }
77
78 void MainThread::app_created(void *data)
79 {
80         reinterpret_cast<MainThread *>(data)->resume_startup();
81 }
82
83 void MainThread::activity_destroyed(ANativeActivity *activity)
84 {
85         reinterpret_cast<MainThread *>(activity->instance)->activity = 0;
86 }
87
88 void MainThread::native_window_created(ANativeActivity *activity, ANativeWindow *window)
89 {
90         reinterpret_cast<MainThread *>(activity->instance)->signal_native_window_created.emit(window);
91 }
92
93 void MainThread::native_window_resized(ANativeActivity *activity, ANativeWindow *window)
94 {
95         reinterpret_cast<MainThread *>(activity->instance)->signal_native_window_resized.emit(window);
96 }
97
98 void MainThread::native_window_destroyed(ANativeActivity *activity, ANativeWindow *window)
99 {
100         reinterpret_cast<MainThread *>(activity->instance)->signal_native_window_destroyed.emit(window);
101 }
102
103 void MainThread::input_queue_created(ANativeActivity *activity, AInputQueue *queue)
104 {
105         reinterpret_cast<MainThread *>(activity->instance)->signal_input_queue_created.emit(queue);
106 }
107
108 void MainThread::input_queue_destroyed(ANativeActivity *activity, AInputQueue *queue)
109 {
110         reinterpret_cast<MainThread *>(activity->instance)->signal_input_queue_destroyed.emit(queue);
111 }
112
113 void MainThread::end_process(ANativeActivity *)
114 {
115         exit(0);
116 }
117
118 } // namespace Android
119 } // namespace Msp