-
Notifications
You must be signed in to change notification settings - Fork 55
Expand file tree
/
Copy pathlib.rs
More file actions
694 lines (636 loc) · 30.5 KB
/
lib.rs
File metadata and controls
694 lines (636 loc) · 30.5 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
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
mod audio;
mod custom_event;
mod java;
mod keycodes;
mod navigator;
mod trace;
mod ui;
use custom_event::RuffleEvent;
use jni::{
objects::{JObject, JString},
sys::{self, jint, jobject},
JNIEnv, JavaVM,
};
use keycodes::{android_key_event_to_ruffle_key_descriptor, key_tag_to_key_descriptor};
use std::any::Any;
use std::rc::Rc;
use std::sync::mpsc::Sender;
use std::sync::{mpsc, MutexGuard};
use std::time::Duration;
use std::{
panic,
sync::{Arc, Mutex},
thread,
time::Instant,
};
use wgpu::rwh::{AndroidDisplayHandle, HasWindowHandle, RawDisplayHandle};
use android_activity::input::{InputEvent, KeyAction, MotionAction};
use android_activity::{AndroidApp, AndroidAppWaker, InputStatus, MainEvent, PollEvent};
use backtrace::Backtrace;
use jni::objects::JClass;
use audio::AAudioAudioBackend;
use url::Url;
use ruffle_core::{
events::{LogicalKey, MouseButton, PlayerEvent},
tag_utils::SwfMovie,
Player, PlayerBuilder, ViewportDimensions,
};
use ruffle_frontend_utils::backends::executor::{AsyncExecutor, PollRequester};
use ruffle_frontend_utils::backends::navigator::ExternalNavigatorBackend;
use ruffle_frontend_utils::backends::storage::DiskStorageBackend;
use ruffle_frontend_utils::content::PlayingContent;
use crate::navigator::AndroidNavigatorInterface;
use crate::trace::FileLogBackend;
use java::JavaInterface;
use ruffle_render_wgpu::{backend::WgpuRenderBackend, target::SwapChainTarget};
/// Represents a current Player and any associated state with that player,
/// which may be lost when this Player is closed (dropped)
struct ActivePlayer {
player: Arc<Mutex<Player>>,
executor: Arc<AsyncExecutor<EventSender>>,
}
#[derive(Clone)]
pub struct EventSender {
sender: Sender<RuffleEvent>,
waker: AndroidAppWaker,
}
impl EventSender {
pub fn send(&self, event: RuffleEvent) {
if self.sender.send(event).is_ok() {
self.waker.wake();
}
}
}
impl PollRequester for EventSender {
fn request_poll(&self) {
self.send(RuffleEvent::TaskPoll);
}
}
#[tokio::main]
async fn run(app: AndroidApp) {
let mut last_frame_time = Instant::now();
let mut next_frame_time = Some(Instant::now());
let mut quit = false;
let (sender, receiver) = mpsc::channel::<RuffleEvent>();
let mut native_window: Option<ndk::native_window::NativeWindow> = None;
let mut playerbox: Option<ActivePlayer> = None;
let sender = EventSender {
sender,
waker: app.create_waker(),
};
log::info!("Starting event loop...");
let trace_output;
let android_storage_dir;
unsafe {
let vm = JavaVM::from_raw(app.vm_as_ptr() as *mut sys::JavaVM).expect("JVM must exist");
let activity = JObject::from_raw(app.activity_as_ptr() as jobject);
let mut jni_env = vm.get_env().unwrap();
trace_output = JavaInterface::get_trace_output(&mut jni_env, &activity);
android_storage_dir = JavaInterface::get_android_data_storage_dir(&mut jni_env, &activity);
let _ = jni_env.set_rust_field(activity, "eventLoopHandle", sender.clone());
}
while !quit {
let mut needs_redraw = false;
app.poll_events(
Some(
next_frame_time
.and_then(|next| next.checked_duration_since(last_frame_time))
.unwrap_or_else(|| Duration::from_millis(100)),
),
|event| {
match event {
PollEvent::Main(event) => match event {
MainEvent::Destroy => {
if let Some(player) = playerbox.as_ref() {
let mut player_lock = player.player.lock().unwrap();
player_lock.flush_shared_objects();
}
quit = true;
}
MainEvent::WindowResized { .. } => {
if let Some(player) = playerbox.as_ref() {
let mut player_lock = player.player.lock().unwrap();
let window = native_window
.as_ref()
.expect("native_window should be Some for a WindowResized");
log::info!(
"WindowResized: {} x {}",
window.width(),
window.height()
);
let viewport_scale_factor = app
.config()
.density()
.map(|dpi| dpi as f64 / 160.0)
.unwrap_or(1.0);
let dimensions = ViewportDimensions {
width: window.width() as u32,
height: window.height() as u32,
scale_factor: viewport_scale_factor,
};
player_lock.set_viewport_dimensions(dimensions);
needs_redraw = true;
}
}
MainEvent::Resume { .. } => {
if let Some(player) = playerbox.as_ref() {
if let Some(window) = native_window.as_ref() {
// [NA] For some reason we can get negative sizes during a resume...
if window.width() > 0 && window.height() > 0 {
unsafe {
let mut player = player
.player
.lock()
.unwrap();
let renderer = <dyn Any>::downcast_mut::<WgpuRenderBackend<SwapChainTarget>>(
player.renderer_mut(),
)
.unwrap();
renderer.recreate_surface_unsafe(
wgpu::SurfaceTargetUnsafe::RawHandle {
raw_display_handle:
RawDisplayHandle::Android(
AndroidDisplayHandle::new(),
),
raw_window_handle: window
.window_handle()
.unwrap()
.into(),
},
(window.width() as u32, window.height() as u32),
)
.unwrap();
}
}
}
}
}
MainEvent::InitWindow { .. } => {
native_window = app.native_window();
let window = native_window
.as_ref()
.expect("native_window should be Some after InitWindow");
let viewport_scale_factor = app
.config()
.density()
.map(|dpi| dpi as f64 / 160.0)
.unwrap_or(1.0);
let dimensions = ViewportDimensions {
width: window.width() as u32,
height: window.height() as u32,
scale_factor: viewport_scale_factor,
};
log::info!(
"Init window: {} x {} (is existing: {})",
window.width(),
window.height(),
playerbox.is_some()
);
if let Some(activeplayer) = &playerbox {
let mut player_lock = activeplayer.player.lock().unwrap();
unsafe {
let renderer = <dyn Any>::downcast_mut::<WgpuRenderBackend<SwapChainTarget>>(
player_lock.renderer_mut(),
)
.unwrap();
renderer.recreate_surface_unsafe(
wgpu::SurfaceTargetUnsafe::RawHandle {
raw_display_handle: RawDisplayHandle::Android(
AndroidDisplayHandle::new(),
),
raw_window_handle: window
.window_handle()
.unwrap()
.into(),
},
(window.width() as u32, window.height() as u32),
)
.unwrap();
}
player_lock.set_is_playing(true);
} else {
let renderer = unsafe {
// TODO: make this take an Arc<Window> instead?
WgpuRenderBackend::for_window_unsafe(
wgpu::SurfaceTargetUnsafe::RawHandle {
raw_display_handle: RawDisplayHandle::Android(
AndroidDisplayHandle::new(),
),
raw_window_handle: window
.window_handle()
.unwrap()
.into(),
},
(dimensions.width, dimensions.height),
wgpu::Backends::GL,
wgpu::PowerPreference::HighPerformance,
)
.unwrap()
};
let movie_url = Url::parse("file://movie.swf").unwrap();
let (executor, future_spawner) = AsyncExecutor::new(
sender.clone(),
);
let navigator = ExternalNavigatorBackend::new(
movie_url.clone(),
None,
None,
future_spawner,
None,
true,
Default::default(),
ruffle_core::backend::navigator::SocketMode::Allow,
Rc::new(PlayingContent::DirectFile(movie_url)),
AndroidNavigatorInterface,
);
playerbox = Some(ActivePlayer {
player: PlayerBuilder::new()
.with_renderer(renderer)
.with_audio(AAudioAudioBackend::new().unwrap())
.with_storage(Box::new(DiskStorageBackend::new(android_storage_dir.clone())))
.with_navigator(navigator)
.with_log(FileLogBackend::new(trace_output.as_deref()))
.with_ui(ui::AndroidUiBackend::new(app.clone()))
.with_video(
ruffle_video_software::backend::SoftwareVideoBackend::new(),
)
.build(),
executor,
}
);
let player = &playerbox.as_ref().unwrap().player;
let mut player_lock = player.lock().unwrap();
let (jvm, activity) = get_jvm().unwrap();
let mut env = jvm.attach_current_thread().unwrap();
let url = JavaInterface::get_swf_uri(&mut env, &activity);
let bytes = JavaInterface::get_swf_bytes(&mut env, &activity);
if let Some(bytes) = bytes {
let movie = SwfMovie::from_data(&bytes, url, None).unwrap();
player_lock.mutate_with_update_context(|context| {
context.set_root_movie(movie);
});
} else {
player_lock.fetch_root_movie(url, Vec::new(), Box::new(|_| {}))
}
player_lock.set_is_playing(true); // Desktop player will auto-play.
player_lock.set_letterbox(ruffle_core::config::Letterbox::On);
player_lock.set_viewport_dimensions(dimensions);
last_frame_time = Instant::now();
next_frame_time = Some(Instant::now());
log::info!("MOVIE STARTED");
}
}
MainEvent::TerminateWindow { .. } => {
let player = &playerbox.as_ref().unwrap().player;
let mut player_lock = player.lock().unwrap();
player_lock.set_is_playing(false);
}
MainEvent::InputAvailable => {
if let Ok(mut inputs) = app.input_events_iter() {
while inputs.next(|input| match input {
InputEvent::MotionEvent(event) => {
let window = native_window.as_ref().unwrap();
let pointer = event.pointer_index();
let pointer = event.pointer_at_index(pointer);
let coords: (i32, i32) = get_loc_in_window();
let mut x = pointer.x() as f64 - coords.0 as f64;
let mut y = pointer.y() as f64 - coords.1 as f64;
let view_size = get_view_size().unwrap();
x = x * window.width() as f64 / view_size.0 as f64;
y = y * window.height() as f64 / view_size.1 as f64;
let ruffle_event = match event.action() {
MotionAction::Down | MotionAction::PointerDown | MotionAction::ButtonPress => {
PlayerEvent::MouseDown {
x,
y,
button: MouseButton::Left, // TODO
index: None, // TODO
}
}
MotionAction::Up | MotionAction::PointerUp | MotionAction::ButtonRelease => {
PlayerEvent::MouseUp {
x,
y,
button: MouseButton::Left, // TODO
}
}
MotionAction::Move => PlayerEvent::MouseMove { x, y },
_ => return InputStatus::Unhandled,
};
if let Some(player) = playerbox.as_ref() {
player
.player
.lock()
.unwrap()
.handle_event(ruffle_event);
}
InputStatus::Handled
}
InputEvent::KeyEvent(event) => {
if let Some(player) = playerbox.as_ref() {
let Some(key_descriptor) =
android_key_event_to_ruffle_key_descriptor(event)
else {
return InputStatus::Unhandled;
};
let down;
let ruffle_event = match event.action() {
KeyAction::Down => {
down = true;
PlayerEvent::KeyDown {
key: key_descriptor,
}
}
KeyAction::Up => {
down = false;
PlayerEvent::KeyUp { key: key_descriptor }
}
_ => return InputStatus::Unhandled,
};
player
.player
.lock()
.unwrap()
.handle_event(ruffle_event);
// TODO: Use `KeyEvent.unicode_char` when it's available:
// https://github.com/rust-mobile/android-activity/issues/183
if down {
if let LogicalKey::Character(c) = key_descriptor.logical_key {
let event = PlayerEvent::TextInput { codepoint: c };
player.player.lock().unwrap().handle_event(event);
}
};
needs_redraw = true;
}
InputStatus::Handled
}
InputEvent::TextEvent(state) => {
if let Some(player) = playerbox.as_ref() {
let event = PlayerEvent::Ime(
ruffle_core::events::ImeEvent::Commit(state.text.clone()),
);
player.player.lock().unwrap().handle_event(event);
}
InputStatus::Handled
}
_ => InputStatus::Unhandled,
}) {}
}
}
_ => {} // Something else happened but it's probably not important for now.
},
PollEvent::Wake => {} // A task tried to wake us, we'll recv it below
PollEvent::Timeout => {} // No events happened, we'll tick as normal below
_ => {} // Unknown future event
}
},
);
match receiver.try_recv() {
Err(_) => {}
Ok(RuffleEvent::TaskPoll) => {
if let Some(player) = playerbox.as_ref() {
player.executor.poll_all()
}
}
Ok(RuffleEvent::VirtualKeyEvent {
down,
key_descriptor,
}) => {
if let Some(player) = playerbox.as_ref() {
let event = if down {
PlayerEvent::KeyDown {
key: key_descriptor,
}
} else {
PlayerEvent::KeyUp {
key: key_descriptor,
}
};
player.player.lock().unwrap().handle_event(event);
if down {
// TODO: Add shift/capslock and pass in uppercase characters accordingly
if let LogicalKey::Character(c) = key_descriptor.logical_key {
let event = PlayerEvent::TextInput { codepoint: c };
player.player.lock().unwrap().handle_event(event);
}
}
}
}
Ok(RuffleEvent::RunContextMenuCallback(index)) => {
if let Some(player) = playerbox.as_ref() {
player
.player
.lock()
.unwrap()
.run_context_menu_callback(index);
}
}
Ok(RuffleEvent::ClearContextMenu) => {
if let Some(player) = playerbox.as_ref() {
player.player.lock().unwrap().clear_custom_menu_items();
}
}
Ok(RuffleEvent::RequestContextMenu) => {
if let Some(player) = playerbox.as_ref() {
log::warn!("preparing context menu!");
let items = player.player.lock().unwrap().prepare_context_menu();
let (jvm, activity) = get_jvm().unwrap();
let mut env = jvm.attach_current_thread().unwrap();
JavaInterface::show_context_menu(&mut env, &activity, &items);
}
}
}
let new_time = Instant::now();
let dt = new_time.duration_since(last_frame_time).as_micros();
if dt > 0 {
last_frame_time = new_time;
if let Some(player) = playerbox.as_ref() {
if let Ok(mut player) = player.player.lock() {
player.tick(dt as f64 / 1000.0);
next_frame_time = Some(new_time + player.time_til_next_frame());
needs_redraw = player.needs_render();
let audio =
<dyn Any>::downcast_mut::<AAudioAudioBackend>(player.audio_mut()).unwrap();
audio.recreate_stream_if_needed();
}
} else {
next_frame_time = None;
}
}
if needs_redraw {
if let Some(player) = playerbox.as_ref() {
if let Ok(mut player) = player.player.lock() {
player.render();
}
}
}
}
unsafe {
let vm = JavaVM::from_raw(app.vm_as_ptr() as *mut sys::JavaVM).expect("JVM must exist");
let activity = JObject::from_raw(app.activity_as_ptr() as jobject);
// Ensure that we take the EventSender back, or we'll leak it
let _: Result<EventSender, _> = vm
.get_env()
.unwrap()
.take_rust_field(activity, "eventLoopHandle");
}
}
#[no_mangle]
#[allow(clippy::missing_safety_doc)]
pub unsafe extern "C" fn Java_rs_ruffle_PlayerActivity_keydown(
mut env: JNIEnv,
this: JObject,
key_tag: JString,
) {
let tag: String = env
.get_string(&key_tag)
.expect("Couldn't get java string!")
.into();
let event_loop: MutexGuard<Sender<RuffleEvent>> =
env.get_rust_field(this, "eventLoopHandle").unwrap();
if let Some(desc) = key_tag_to_key_descriptor(&tag) {
let _ = event_loop.send(RuffleEvent::VirtualKeyEvent {
down: true,
key_descriptor: desc,
});
}
}
#[no_mangle]
#[allow(clippy::missing_safety_doc)]
pub unsafe extern "C" fn Java_rs_ruffle_PlayerActivity_keyup(
mut env: JNIEnv,
this: JObject,
key_tag: JString,
) {
let tag: String = env
.get_string(&key_tag)
.expect("Couldn't get java string!")
.into();
let event_loop: MutexGuard<Sender<RuffleEvent>> =
env.get_rust_field(this, "eventLoopHandle").unwrap();
if let Some(desc) = key_tag_to_key_descriptor(&tag) {
let _ = event_loop.send(RuffleEvent::VirtualKeyEvent {
down: false,
key_descriptor: desc,
});
}
}
pub fn get_jvm<'a>() -> Result<(jni::JavaVM, JObject<'a>), Box<dyn std::error::Error>> {
// Create a VM for executing Java calls
let context = ndk_context::android_context();
let activity = unsafe { JObject::from_raw(context.context().cast()) };
let vm = unsafe { jni::JavaVM::from_raw(context.vm().cast()) }?;
Ok((vm, activity))
}
#[no_mangle]
#[allow(clippy::missing_safety_doc)]
pub unsafe extern "C" fn Java_rs_ruffle_PlayerActivity_requestContextMenu(
mut env: JNIEnv,
this: JObject,
) {
let event_loop: MutexGuard<Sender<RuffleEvent>> =
env.get_rust_field(this, "eventLoopHandle").unwrap();
let _ = event_loop.send(RuffleEvent::RequestContextMenu);
}
#[no_mangle]
#[allow(clippy::missing_safety_doc)]
pub unsafe extern "C" fn Java_rs_ruffle_PlayerActivity_runContextMenuCallback(
mut env: JNIEnv,
this: JObject,
index: jint,
) {
let event_loop: MutexGuard<Sender<RuffleEvent>> =
env.get_rust_field(this, "eventLoopHandle").unwrap();
let _ = event_loop.send(RuffleEvent::RunContextMenuCallback(index as usize));
}
#[no_mangle]
#[allow(clippy::missing_safety_doc)]
pub unsafe extern "C" fn Java_rs_ruffle_PlayerActivity_clearContextMenu(
mut env: JNIEnv,
this: JObject,
) {
let event_loop: MutexGuard<Sender<RuffleEvent>> =
env.get_rust_field(this, "eventLoopHandle").unwrap();
let _ = event_loop.send(RuffleEvent::ClearContextMenu);
}
#[no_mangle]
#[allow(clippy::missing_safety_doc)]
pub unsafe extern "C" fn Java_rs_ruffle_PlayerActivity_nativeInit(
mut env: JNIEnv,
class: JClass,
crash_callback: JObject,
) {
let crash_callback = env.new_global_ref(crash_callback).unwrap();
let jvm = env.get_java_vm().unwrap();
android_logger::init_once(
android_logger::Config::default()
.with_max_level(log::LevelFilter::Info)
.with_tag("ruffle")
.with_filter(
android_logger::FilterBuilder::new()
.parse("warn,ruffle=info")
.build(),
),
);
panic::set_hook(Box::new(move |info| {
let backtrace = Backtrace::new();
let thread = thread::current();
let thread = thread.name().unwrap_or("<unnamed>");
let message = match info.payload().downcast_ref::<&'static str>() {
Some(s) => *s,
None => match info.payload().downcast_ref::<String>() {
Some(s) => &**s,
None => "Box<Any>",
},
};
let full = match info.location() {
Some(location) => format!(
"thread '{}' panicked at '{}': {}:{}\n{:?}",
thread,
message,
location.file(),
location.line(),
backtrace
),
None => format!(
"thread '{}' panicked at '{}'\n{:?}",
thread, message, backtrace
),
};
log::error!(target: "panic","{}", full);
let mut env = jvm.attach_current_thread().unwrap();
if env.exception_check().unwrap() {
// There's a pending exception, java will discover this on their own
} else {
let java_message = env.new_string(full).unwrap();
let crash_callback = env.new_global_ref(&crash_callback).unwrap();
env.call_method(
crash_callback,
"onCrash",
"(Ljava/lang/String;)V",
&[(&java_message).into()],
)
.unwrap();
}
}));
JavaInterface::init(&mut env, &class)
}
fn get_loc_in_window() -> (i32, i32) {
let (jvm, activity) = get_jvm().unwrap();
let mut env = jvm.attach_current_thread().unwrap();
// no worky :(
//ndk_glue::native_activity().show_soft_input(true);
JavaInterface::get_loc_in_window(&mut env, &activity)
}
fn get_view_size() -> Result<(i32, i32), Box<dyn std::error::Error>> {
let (jvm, activity) = get_jvm()?;
let mut env = jvm.attach_current_thread()?;
let width = JavaInterface::get_surface_width(&mut env, &activity);
let height = JavaInterface::get_surface_height(&mut env, &activity);
Ok((width, height))
}
#[no_mangle]
fn android_main(app: AndroidApp) {
log::info!("Starting android_main...");
run(app);
}