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
use crate::configuration::Configuration;
use crate::input_queue::InputQueue;
use crate::native_activity::NativeActivity;
use num_enum::{IntoPrimitive, TryFromPrimitive};
use std::convert::TryInto;
use std::marker::PhantomData;
use std::mem::ManuallyDrop;
use std::ops::Deref;
use std::ptr::NonNull;
pub struct AndroidApp {
ptr: NonNull<ffi::native_app_glue::android_app>,
}
unsafe impl Send for AndroidApp {}
unsafe impl Sync for AndroidApp {}
impl AndroidApp {
pub unsafe fn from_ptr(ptr: NonNull<ffi::native_app_glue::android_app>) -> Self {
Self { ptr }
}
pub fn ptr(&self) -> NonNull<ffi::native_app_glue::android_app> {
self.ptr
}
pub fn activity(&self) -> Ref<'_, NativeActivity> {
unsafe {
Ref::new(NativeActivity::from_ptr(
NonNull::new(self.ptr.as_ref().activity).unwrap(),
))
}
}
pub fn input_queue(&self) -> Ref<'_, InputQueue> {
unsafe {
Ref::new(InputQueue::from_ptr(
NonNull::new(self.ptr.as_ref().inputQueue).unwrap(),
))
}
}
pub fn config(&self) -> Ref<'_, Configuration> {
unsafe {
Ref::new(Configuration::from_ptr(
NonNull::new(self.ptr.as_ref().config).unwrap(),
))
}
}
fn read_cmd(&mut self) -> Cmd {
unsafe {
ffi::native_app_glue::android_app_read_cmd(self.ptr.as_ptr())
.try_into()
.unwrap()
}
}
fn pre_exec(&mut self, cmd: Cmd) {
unsafe {
ffi::native_app_glue::android_app_pre_exec_cmd(self.ptr.as_ptr(), cmd.into());
}
}
fn post_exec(&mut self, cmd: Cmd) {
unsafe {
ffi::native_app_glue::android_app_post_exec_cmd(self.ptr.as_ptr(), cmd.into());
}
}
pub fn handle_cmd<T>(&mut self, f: impl FnOnce(&mut Self, Cmd) -> T) -> T {
let cmd = self.read_cmd();
self.pre_exec(cmd);
let res = f(self, cmd);
self.post_exec(cmd);
res
}
pub fn saved_state(&self) -> Option<&[u8]> {
unsafe {
let ptr = self.ptr.as_ref().savedState;
if ptr.is_null() {
None
} else {
Some(std::slice::from_raw_parts(
ptr as *mut u8,
self.ptr.as_ref().savedStateSize,
))
}
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, TryFromPrimitive, IntoPrimitive)]
#[repr(i8)]
pub enum Cmd {
InputChanged = ffi::native_app_glue::APP_CMD_INPUT_CHANGED,
InitWindow = ffi::native_app_glue::APP_CMD_INIT_WINDOW,
TermWindow = ffi::native_app_glue::APP_CMD_TERM_WINDOW,
WindowResized = ffi::native_app_glue::APP_CMD_WINDOW_RESIZED,
WindowRedrawNeeded = ffi::native_app_glue::APP_CMD_WINDOW_REDRAW_NEEDED,
ContentRectChanged = ffi::native_app_glue::APP_CMD_CONTENT_RECT_CHANGED,
GainedFocus = ffi::native_app_glue::APP_CMD_GAINED_FOCUS,
LostFocus = ffi::native_app_glue::APP_CMD_LOST_FOCUS,
ConfigChanged = ffi::native_app_glue::APP_CMD_CONFIG_CHANGED,
LowMemory = ffi::native_app_glue::APP_CMD_LOW_MEMORY,
Start = ffi::native_app_glue::APP_CMD_START,
Resume = ffi::native_app_glue::APP_CMD_RESUME,
SaveState = ffi::native_app_glue::APP_CMD_SAVE_STATE,
Pause = ffi::native_app_glue::APP_CMD_PAUSE,
Stop = ffi::native_app_glue::APP_CMD_STOP,
Destroy = ffi::native_app_glue::APP_CMD_DESTROY,
}
pub struct Ref<'a, T> {
_marker: PhantomData<&'a T>,
data: ManuallyDrop<T>,
}
impl<'a, T> Deref for Ref<'a, T> {
type Target = T;
fn deref(&self) -> &T {
&*self.data
}
}
impl<'a, T> Ref<'a, T> {
fn new(data: T) -> Self {
Self {
_marker: PhantomData,
data: ManuallyDrop::new(data),
}
}
}