@@ -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+ }
0 commit comments