Skip to content

Commit a7f1a26

Browse files
committed
stuff
1 parent dbefb18 commit a7f1a26

12 files changed

Lines changed: 869 additions & 619 deletions
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#include "android_configuration.hxx"
2+
3+
android_configuration_wrapper::android_configuration_wrapper(AAssetManager& am) :
4+
config(AConfiguration_new())
5+
{
6+
AConfiguration_fromAssetManager(
7+
this->config, //
8+
&am
9+
);
10+
}
11+
12+
android_configuration_wrapper::~android_configuration_wrapper()
13+
{
14+
AConfiguration_delete(this->config);
15+
}
16+
17+
int32_t android_configuration_wrapper::diff(const android_configuration_wrapper& cfg)
18+
{
19+
return AConfiguration_diff(
20+
this->config, //
21+
cfg.config
22+
);
23+
}
24+
25+
int32_t android_configuration_wrapper::get_orientation() const noexcept
26+
{
27+
return AConfiguration_getOrientation(this->config);
28+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#pragma once
2+
3+
#include <android/configuration.h>
4+
5+
namespace {
6+
class android_configuration_wrapper
7+
{
8+
const AConfiguration* config;
9+
10+
public:
11+
android_configuration_wrapper(AAssetManager& am);
12+
13+
~android_configuration_wrapper();
14+
15+
int32_t diff(const android_configuration_wrapper& cfg);
16+
17+
int32_t get_orientation() const noexcept;
18+
};
19+
} // namespace

src/ruisapp/glue/android/android_globals.cxx

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,3 +28,97 @@ void android_globals_wrapper::destroy()
2828
android_globals_wrapper::native_activity->instance = nullptr;
2929
android_globals_wrapper::native_activity = nullptr;
3030
}
31+
32+
namespace {
33+
int on_queue_has_messages(
34+
int fd, //
35+
int events,
36+
void* data
37+
)
38+
{
39+
auto& glob = get_glob();
40+
41+
while (auto m = glob.ui_queue.pop_front()) {
42+
m();
43+
}
44+
45+
return 1; // 1 means do not remove descriptor from looper
46+
}
47+
} // namespace
48+
49+
namespace {
50+
int on_update_timer_expired(
51+
int fd, //
52+
int events,
53+
void* data
54+
)
55+
{
56+
// utki::log_debug([&](auto&o){o << "on_update_timer_expired(): invoked" <<
57+
// std::endl;});
58+
59+
auto& glob = get_glob();
60+
61+
utki::assert(glob.app, SL);
62+
auto& glue = get_glue(*glob.app);
63+
64+
uint32_t dt = glue.updater.get().update();
65+
if (dt == 0) {
66+
// do not arm the timer and do not clear the flag
67+
} else {
68+
glob.fd_flag.clear();
69+
glob.timer.arm(dt);
70+
}
71+
72+
// after updating need to re-render everything
73+
glue.render();
74+
75+
// utki::log_debug([&](auto&o){o << "on_update_timer_expired(): armed timer for " << dt
76+
//<< std::endl;});
77+
78+
return 1; // 1 means do not remove descriptor from looper
79+
}
80+
} // namespace
81+
82+
android_globals_wrapper()
83+
{
84+
// add timer descriptor to looper, this is needed for updatable to work
85+
if (ALooper_addFd(
86+
this->looper,
87+
this->fd_flag.get_fd(),
88+
ALOOPER_POLL_CALLBACK,
89+
ALOOPER_EVENT_INPUT,
90+
&on_update_timer_expired,
91+
0
92+
) == -1)
93+
{
94+
throw std::runtime_error("failed to add timer descriptor to looper");
95+
}
96+
97+
// add UI message queue descriptor to looper
98+
if (ALooper_addFd(
99+
this->looper,
100+
this->ui_queue.get_handle(),
101+
ALOOPER_POLL_CALLBACK,
102+
ALOOPER_EVENT_INPUT,
103+
&on_queue_has_messages,
104+
0
105+
) == -1)
106+
{
107+
throw std::runtime_error("failed to add UI message queue descriptor to looper");
108+
}
109+
}
110+
111+
~android_globals_wrapper()
112+
{
113+
// remove UI message queue descriptor from looper
114+
ALooper_removeFd(
115+
this->looper, //
116+
this->ui_queue.get_handle()
117+
);
118+
119+
// remove fd_flag from looper
120+
ALooper_removeFd(
121+
this->looper, //
122+
this->fd_flag.get_fd()
123+
);
124+
}

src/ruisapp/glue/android/android_globals.hxx

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
11
#pragma once
22

3-
#include <android/asset_manager.h>
4-
#include <android/configuration.h>
53
#include <android/native_activity.h>
64
#include <android/window.h>
75
#include <nitki/queue.hpp>
86
#include <utki/debug.hpp>
97

108
#include "../../application.hpp"
119

10+
#include "android_configuration.hxx"
11+
#include "event_fd.hxx"
1212
#include "java_functions.hxx"
13+
#include "linux_timer.hxx"
1314

1415
namespace {
1516
struct android_globals_wrapper final {
@@ -25,13 +26,39 @@ struct android_globals_wrapper final {
2526
return
2627
}
2728

29+
android_globals_wrapper();
30+
31+
android_globals_wrapper(const android_globals_wrapper&) = delete;
32+
android_globals_wrapper& operator=(const android_globals_wrapper&) = delete;
33+
34+
android_globals_wrapper(android_globals_wrapper&&) = delete;
35+
android_globals_wrapper& operator=(android_globals_wrapper&&) = delete;
36+
37+
~android_globals_wrapper();
38+
2839
java_functions_wrapper java_functions;
2940

41+
// save pointer to current thread's looper
42+
ALooper* looper = []() {
43+
auto l = ALooper_prepare(0);
44+
utki::assert(l, SL);
45+
return l;
46+
}();
47+
48+
event_fd_wrapper fd_flag; // TODO: rename to main_loop_event_fd
49+
linux_timer timer{[&]() {
50+
this->fd_flag.set();
51+
}};
52+
3053
nitki::queue ui_queue;
3154

32-
std::unique_ptr<ruisapp::application> app;
55+
utki::unique_ref<android_configuration_wrapper> cur_android_configuration =
56+
utki::make_unique<android_configuration_wrapper>(*android_configuration_wrapper::native_activity->assetManager);
3357

58+
AInputQueue* input_queue = nullptr;
3459
ANativeWindow* android_window = nullptr;
60+
61+
std::unique_ptr<ruisapp::application> app;
3562
};
3663
} // namespace
3764

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,15 @@
11
#include "application.hxx"
2+
3+
#include "android_globals.hxx"
4+
#include "asset_file.hxx"
5+
6+
std::unique_ptr<papki::file> ruisapp::application::get_res_file(std::string_view path) const
7+
{
8+
utki::assert(android_globals_wrapper::native_activity, SL);
9+
utki::assert(android_globals_wrapper::native_activity->assetManager, SL);
10+
11+
return std::make_unique<asset_file>(
12+
android_globals_wrapper::native_activity->assetManager, //
13+
path
14+
);
15+
}

src/ruisapp/glue/android/application.hxx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ namespace {
1616
class application_glue : public utki::destructable
1717
{
1818
public:
19+
utki::shared_ref<ruis::updater> updater = utki::make_shared<ruis::updater>();
1920
};
2021
} // namespace
2122

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
#include "asset_file.hxx"

0 commit comments

Comments
 (0)