-
Notifications
You must be signed in to change notification settings - Fork 27
Expand file tree
/
Copy pathlib.rs
More file actions
435 lines (401 loc) · 19.3 KB
/
lib.rs
File metadata and controls
435 lines (401 loc) · 19.3 KB
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
use std::sync::OnceLock;
use android_activity::{
input::{InputEvent, KeyAction, KeyEvent, KeyMapChar, MotionAction},
ndk::{hardware_buffer_format::HardwareBufferFormat, native_window::NativeWindow},
AndroidApp, InputStatus, MainEvent, OnCreateState, PollEvent,
};
use jni::{
objects::{JObject, JString},
refs::Global,
vm::JavaVM,
};
use tracing::{error, info};
jni::bind_java_type! { Context => "android.content.Context" }
jni::bind_java_type! {
Activity => "android.app.Activity",
type_map {
Context => "android.content.Context",
},
is_instance_of {
context: Context
},
}
jni::bind_java_type! {
Toast => "android.widget.Toast",
type_map {
Context => "android.content.Context",
},
methods {
static fn make_text(context: Context, text: JCharSequence, duration: i32) -> Toast,
fn show(),
}
}
// Note: The jni bindings will actually initialize lazily but it can be helpful
// to initialize explicitly to get an up-front error in case there is an issue
// (such as a typo with a method name or incorrect signature) rather than having
// an unpredictable error when using the binding.
fn jni_init(env: &jni::Env) -> jni::errors::Result<()> {
let _ = ContextAPI::get(env, &Default::default())?;
let _ = ActivityAPI::get(env, &Default::default())?;
let _ = ToastAPI::get(env, &Default::default())?;
// .. call other `get` functions for other bindings here as needed ...
Ok(())
}
// Called while Activity.onCreate is running
// May be called multiple times if the activity is destroyed and recreated.
#[unsafe(no_mangle)]
fn android_on_create(state: &OnCreateState) {
static ONCE: OnceLock<()> = OnceLock::new();
ONCE.get_or_init(|| {
use tracing_subscriber::prelude::*;
unsafe { std::env::set_var("RUST_BACKTRACE", "full") };
const DEFAULT_ENV_FILTER: &str = "debug,wgpu_hal=info,winit=info,naga=info";
let filter_layer = tracing_subscriber::EnvFilter::new(DEFAULT_ENV_FILTER);
let android_layer = paranoid_android::layer(env!("CARGO_PKG_NAME"))
.with_ansi(false)
.with_span_events(tracing_subscriber::fmt::format::FmtSpan::CLOSE)
.with_thread_names(true);
tracing_subscriber::registry()
.with(filter_layer)
.with(android_layer)
.init();
});
let vm = unsafe { JavaVM::from_raw(state.vm_as_ptr().cast()) };
// Note: from here on we can also rely on `JavaVM::singleton()` now that we know it's been initialized.
let activity = state.activity_as_ptr() as jni::sys::jobject;
if let Err(err) = vm.attach_current_thread(|env| -> jni::errors::Result<()> {
// Initialize JNI bindings
jni_init(env).expect("Failed to initialize JNI bindings");
// SAFETY:
// - The reference / pointer is at least valid until we return
// - By creating a `Cast` we ensure we can't accidentally delete the reference
let _activity = unsafe { env.as_cast_raw::<Global<JObject>>(&activity)? };
// Do something with the activity on the Java main thread, such as call a method or access a field
Ok(())
}) {
error!("Failed to interact with Android SDK on Java main thread: {err:?}");
}
eprintln!(
"android_on_create called on thread {:?}",
std::thread::current().id()
);
info!(
"android_on_create called on thread {:?}",
std::thread::current().id()
);
}
enum ToastDuration {
Short = 0,
Long = 1,
}
fn send_toast(outer_app: &AndroidApp, msg: impl AsRef<str>, duration: ToastDuration) {
let app = outer_app.clone();
let msg = msg.as_ref().to_string();
outer_app.run_on_java_main_thread(Box::new(move || {
// We initialize JavaVM::singleton at the start of `android_main`
let jvm = jni::JavaVM::singleton().expect("Failed to get singleton JavaVM instance");
// We use `with_top_local_frame` as a minor optimization because it's guaranteed by
// `run_on_java_main_thread` that we already have an underlying JNI attachment and local
// frame. It would also be perfectly reasonable to use `jvm.attach_current_thread()`.
if let Err(err) = jvm.with_top_local_frame(|env| -> jni::errors::Result<()> {
let activity: jni::sys::jobject = app.activity_as_ptr() as _;
let activity = unsafe { env.as_cast_raw::<Global<Activity>>(&activity)? };
let message = JString::new(env, &msg)?;
let toast = Toast::make_text(env, activity.as_ref(), &message, duration as i32)?;
info!("Showing Toast from Rust JNI callback: {msg}");
toast.show(env)?;
Ok(())
}) {
error!("Failed to execute callback on main thread: {err:?}");
}
}));
}
// Called on a dedicated Activity main loop thread, spawned after `android_on_create` returns
// May be called multiple times if the activity is destroyed and recreated.
#[unsafe(no_mangle)]
fn android_main(app: AndroidApp) {
eprintln!(
"android_main started on thread {:?}",
std::thread::current().id()
);
info!(
"android_main started on thread {:?}",
std::thread::current().id()
);
let mut quit = false;
let mut redraw_pending = true;
let mut native_window = None;
let mut combining_accent = None;
send_toast(&app, "Hello from Rust on Android!", ToastDuration::Long);
while !quit {
app.poll_events(
Some(std::time::Duration::from_secs(2)), /* timeout */
|event| {
match event {
PollEvent::Wake => {
info!("Early wake up");
}
PollEvent::Timeout => {
info!("Timed out");
// Real app would probably rely on vblank sync via graphics API...
redraw_pending = true;
}
PollEvent::Main(main_event) => {
info!("Main event: {:?}", main_event);
match main_event {
MainEvent::SaveState { saver, .. } => {
saver.store("foo://bar".as_bytes());
}
MainEvent::Pause => {}
MainEvent::Resume { loader, .. } => {
if let Some(state) = loader.load() {
if let Ok(uri) = String::from_utf8(state) {
info!("Resumed with saved state = {uri:#?}");
}
}
send_toast(&app, "Resumed!", ToastDuration::Short);
}
MainEvent::InitWindow { .. } => {
native_window = app.native_window();
if let Some(nw) = &native_window {
// Set the backing buffer to a known format (without changing
// the size) so that we can safely draw to it in dummy_render().
nw.set_buffers_geometry(
0,
0,
Some(HardwareBufferFormat::R8G8B8A8_UNORM),
)
.unwrap()
}
redraw_pending = true;
}
MainEvent::TerminateWindow { .. } => {
native_window = None;
redraw_pending = false;
}
MainEvent::WindowResized { .. } => {
redraw_pending = true;
}
MainEvent::RedrawNeeded { .. } => {
redraw_pending = true;
}
MainEvent::InputAvailable { .. } => {
redraw_pending = true;
}
MainEvent::ConfigChanged { .. } => {
info!("Config Changed: {:#?}", app.config());
send_toast(&app, "Config Changed!", ToastDuration::Short);
}
MainEvent::LowMemory => {
info!("Low Memory Warning");
send_toast(&app, "Low Memory!", ToastDuration::Short);
}
MainEvent::Destroy => quit = true,
_ => { /* ... */ }
}
}
_ => {}
}
if redraw_pending {
if let Some(native_window) = &native_window {
redraw_pending = false;
// Handle input, via a lending iterator
match app.input_events_iter() {
Ok(mut iter) => loop {
info!("Checking for next input event...");
if !iter.next(|event| {
match event {
InputEvent::KeyEvent(key_event) => {
let combined_key_char = character_map_and_combine_key(
&app,
key_event,
&mut combining_accent,
);
info!("KeyEvent: combined key: {combined_key_char:?}")
}
InputEvent::MotionEvent(motion_event) => {
println!("action = {:?}", motion_event.action());
#[expect(clippy::single_match)]
match motion_event.action() {
MotionAction::Up => {
let pointer = motion_event.pointer_index();
let pointer =
motion_event.pointer_at_index(pointer);
let x = pointer.x();
let y = pointer.y();
println!("POINTER UP {x}, {y}");
if x < 500.0 && y < 500.0 {
println!("Requesting to show keyboard");
send_toast(
&app,
"Requesting to show keyboard",
ToastDuration::Short,
);
app.show_soft_input(true);
} else if x >= 500.0 && y < 500.0 {
println!("Requesting to hide keyboard");
send_toast(
&app,
"Requesting to hide keyboard",
ToastDuration::Short,
);
app.hide_soft_input(false);
} else {
send_toast(
&app,
format!("POINTER UP {x}, {y}"),
ToastDuration::Short,
);
}
}
_ => {}
}
let num_pointers = motion_event.pointer_count();
for i in 0..num_pointers {
let pointer = motion_event.pointer_at_index(i);
println!(
"Pointer[{i}]: id={}, time={}, x={}, y={}",
pointer.pointer_id(),
motion_event.event_time(),
pointer.x(),
pointer.y(),
);
for sample in pointer.history() {
println!(
" History[{}]: x={}, y={}, time={:?}",
sample.history_index(),
sample.x(),
sample.y(),
sample.event_time()
);
}
}
}
InputEvent::TextEvent(state) => {
info!("Input Method State: {state:?}");
}
_ => {}
}
info!("Input Event: {event:?}");
app.run_on_java_main_thread(Box::new(move || {
println!(
"Callback on main thread {:?}",
std::thread::current().id()
);
info!(
"Callback on main thread {:?}",
std::thread::current().id()
);
}));
InputStatus::Unhandled
}) {
info!("No more input available");
break;
}
},
Err(err) => {
error!("Failed to get input events iterator: {err:?}");
}
}
info!("Render...");
dummy_render(native_window);
}
}
},
);
}
}
/// Tries to map the `key_event` to a `KeyMapChar` containing a unicode character or dead key accent
///
/// This shows how to take a `KeyEvent` and look up its corresponding `KeyCharacterMap` and
/// use that to try and map the `key_code` + `meta_state` to a unicode character or a
/// dead key that be combined with the next key press.
fn character_map_and_combine_key(
app: &AndroidApp,
key_event: &KeyEvent,
combining_accent: &mut Option<char>,
) -> Option<KeyMapChar> {
let device_id = key_event.device_id();
let key_map = match app.device_key_character_map(device_id) {
Ok(key_map) => key_map,
Err(err) => {
error!("Failed to look up `KeyCharacterMap` for device {device_id}: {err:?}");
return None;
}
};
match key_map.get(key_event.key_code(), key_event.meta_state()) {
Ok(KeyMapChar::Unicode(unicode)) => {
// Only do dead key combining on key down
if key_event.action() == KeyAction::Down {
let combined_unicode = if let Some(accent) = combining_accent {
match key_map.get_dead_char(*accent, unicode) {
Ok(Some(key)) => {
info!(
"KeyEvent: Combined '{unicode}' with accent '{accent}' to give '{key}'"
);
Some(key)
}
Ok(None) => None,
Err(err) => {
error!(
"KeyEvent: Failed to combine 'dead key' accent '{accent}' with '{unicode}': {err:?}"
);
None
}
}
} else {
info!("KeyEvent: Pressed '{unicode}'");
Some(unicode)
};
*combining_accent = None;
combined_unicode.map(KeyMapChar::Unicode)
} else {
Some(KeyMapChar::Unicode(unicode))
}
}
Ok(KeyMapChar::CombiningAccent(accent)) => {
if key_event.action() == KeyAction::Down {
info!("KeyEvent: Pressed 'dead key' combining accent '{accent}'");
*combining_accent = Some(accent);
}
Some(KeyMapChar::CombiningAccent(accent))
}
Ok(KeyMapChar::None) => {
// Leave any combining_accent state in tact (seems to match how other
// Android apps work)
info!("KeyEvent: Pressed non-unicode key");
None
}
Err(err) => {
error!("KeyEvent: Failed to get key map character: {err:?}");
*combining_accent = None;
None
}
}
}
/// Post a NOP frame to the window
///
/// Since this is a bare minimum test app we don't depend
/// on any GPU graphics APIs but we do need to at least
/// convince Android that we're drawing something and are
/// responsive, otherwise it will stop delivering input
/// events to us.
fn dummy_render(native_window: &NativeWindow) {
let mut lock = native_window.lock(None).unwrap();
let (w, h) = (lock.width(), lock.height());
assert_eq!(
lock.format(),
HardwareBufferFormat::R8G8B8A8_UNORM,
"Expected the buffer format to be R8G8B8A8_UNORM since we set that in `InitWindow` handling"
);
for (y, line) in lock.lines().unwrap().enumerate() {
let r = y * 255 / h;
for (x, pixels) in line.chunks_mut(4).enumerate() {
let g = x * 255 / w;
pixels[0].write(r as u8);
pixels[1].write(g as u8);
pixels[2].write(0);
pixels[3].write(255);
}
}
}