-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlooper.rs
159 lines (146 loc) · 6.73 KB
/
looper.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
//! Demonstrates how to manage application lifetime using Android's `Looper`
use std::mem::MaybeUninit;
use std::os::unix::prelude::RawFd;
use std::time::Duration;
use log::info;
use ndk::event::{InputEvent, Keycode};
use ndk::looper::{FdEvent, Poll, ThreadLooper};
const U32_SIZE: usize = std::mem::size_of::<u32>();
#[cfg_attr(
target_os = "android",
ndk_glue::main(backtrace = "on", logger(level = "debug"))
)]
fn main() {
// Retrieve the Looper that ndk_glue created for us on the current thread.
// Android uses this to block on events and poll file descriptors with a single mechanism.
let looper =
ThreadLooper::for_thread().expect("ndk-glue did not attach thread looper before main()!");
// First free number after ndk_glue::NDK_GLUE_LOOPER_INPUT_QUEUE_IDENT. This might be fragile.
const CUSTOM_EVENT_IDENT: i32 = ndk_glue::NDK_GLUE_LOOPER_INPUT_QUEUE_IDENT + 1;
fn create_pipe() -> [RawFd; 2] {
let mut ends = MaybeUninit::<[RawFd; 2]>::uninit();
assert_eq!(unsafe { libc::pipe(ends.as_mut_ptr().cast()) }, 0);
unsafe { ends.assume_init() }
}
// Create a Unix pipe to send custom events to the Looper. ndk-glue uses a similar mechanism to deliver
// ANativeActivityCallbacks asynchronously to the Looper through NDK_GLUE_LOOPER_EVENT_PIPE_IDENT.
let custom_event_pipe = create_pipe();
let custom_callback_pipe = create_pipe();
// Attach the reading end of the pipe to the looper, so that it wakes up
// whenever data is available for reading (FdEvent::INPUT)
looper
.as_foreign()
.add_fd(
custom_event_pipe[0],
CUSTOM_EVENT_IDENT,
FdEvent::INPUT,
std::ptr::null_mut(),
)
.expect("Failed to add file descriptor to Looper");
// Attach the reading end of a pipe to a callback, too
looper
.as_foreign()
.add_fd_with_callback(custom_callback_pipe[0], FdEvent::INPUT, |fd| {
let mut recv = !0u32;
assert_eq!(
unsafe { libc::read(fd, &mut recv as *mut _ as *mut _, U32_SIZE) } as usize,
U32_SIZE
);
info!("Read custom event from pipe, in callback: {}", recv);
// Detach this handler by returning `false` once the count reaches 5
recv < 5
})
.expect("Failed to add file descriptor to Looper");
std::thread::spawn(move || {
// Send a "custom event" to the looper every second
for i in 0.. {
let i_addr = &i as *const _ as *const _;
std::thread::sleep(Duration::from_secs(1));
assert_eq!(
unsafe { libc::write(custom_event_pipe[1], i_addr, U32_SIZE) },
U32_SIZE as isize
);
assert_eq!(
unsafe { libc::write(custom_callback_pipe[1], i_addr, U32_SIZE,) },
U32_SIZE as isize
);
}
});
let mut exit = false;
while !exit {
// looper.poll_*_timeout(timeout) to not block indefinitely.
// Pass a timeout of Duration::ZERO to never block.
match looper.poll_all().unwrap() {
Poll::Wake => { /* looper.as_foreign().wake() was called */ }
Poll::Callback => {
/* An event with a registered callback was received.
* Only received when polling for single events with poll_once_*
*/
unreachable!()
}
Poll::Timeout => {
/* Timed out as per poll_*_timeout */
unreachable!()
}
Poll::Event {
ident,
fd,
events: _,
data: _,
} => {
info!("File descriptor event on identifier {}", ident);
match ident {
ndk_glue::NDK_GLUE_LOOPER_EVENT_PIPE_IDENT => {
// One of the callbacks in ANativeActivityCallbacks is called, and delivered
// to this application asynchronously by ndk_glue through a pipe.
// These consist mostly of important lifecycle and window events! Graphics
// applications will create and destroy their output surface/swapchain here.
info!(
"Event pipe yields: {:?}",
ndk_glue::poll_events()
.expect("Looper says event-pipe has data available!")
)
}
ndk_glue::NDK_GLUE_LOOPER_INPUT_QUEUE_IDENT => {
let input_queue = ndk_glue::input_queue();
let input_queue = input_queue.as_ref().expect("Input queue not attached");
assert!(input_queue.has_events().unwrap());
// Consume as many events as possible
while let Some(event) = input_queue.get_event() {
// Pass the event by a possible IME (Input Method Editor, ie. an open keyboard) first
if let Some(event) = input_queue.pre_dispatch(event) {
info!("Input event {:?}", event);
let mut handled = false;
if let InputEvent::KeyEvent(key_event) = &event {
if key_event.key_code() == Keycode::Back {
// Gracefully stop the app when the user presses the back button
exit = true;
handled = true;
}
}
// Let Android know that we did not consume the event
// (Pass true here if you did)
input_queue.finish_event(event, handled);
}
}
}
CUSTOM_EVENT_IDENT => {
// Expect to receive 32-bit numbers to describe events,
// as sent by the thread above
let mut recv = !0u32;
assert_eq!(
unsafe { libc::read(fd, &mut recv as *mut _ as *mut _, U32_SIZE) }
as usize,
U32_SIZE
);
info!("Read custom event from pipe: {}", recv);
}
i => panic!("Unexpected event identifier {}", i),
}
}
}
}
// Stop the activity
#[allow(deprecated)]
ndk_glue::native_activity().finish()
}