diff --git a/.gitignore b/.gitignore index 2f7896d..71f83fe 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,3 @@ target/ +.vscode +.DS_Store diff --git a/cargo-apk/Cargo.lock b/cargo-apk/Cargo.lock index 2e30b7b..7b2148e 100644 --- a/cargo-apk/Cargo.lock +++ b/cargo-apk/Cargo.lock @@ -1,10 +1,10 @@ [root] name = "cargo-apk" -version = "0.1.5" +version = "0.1.6" dependencies = [ "rustc-serialize 0.3.19 (registry+https://github.com/rust-lang/crates.io-index)", "term 0.4.4 (registry+https://github.com/rust-lang/crates.io-index)", - "toml 0.1.28 (registry+https://github.com/rust-lang/crates.io-index)", + "toml 0.1.30 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] @@ -32,7 +32,7 @@ dependencies = [ [[package]] name = "toml" -version = "0.1.28" +version = "0.1.30" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ "rustc-serialize 0.3.19 (registry+https://github.com/rust-lang/crates.io-index)", @@ -48,3 +48,10 @@ name = "winapi-build" version = "0.1.1" source = "registry+https://github.com/rust-lang/crates.io-index" +[metadata] +"checksum kernel32-sys 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)" = "7507624b29483431c0ba2d82aece8ca6cdba9382bff4ddd0f7490560c056098d" +"checksum rustc-serialize 0.3.19 (registry+https://github.com/rust-lang/crates.io-index)" = "6159e4e6e559c81bd706afe9c8fd68f547d3e851ce12e76b1de7914bab61691b" +"checksum term 0.4.4 (registry+https://github.com/rust-lang/crates.io-index)" = "3deff8a2b3b6607d6d7cc32ac25c0b33709453ca9cceac006caac51e963cf94a" +"checksum toml 0.1.30 (registry+https://github.com/rust-lang/crates.io-index)" = "0590d72182e50e879c4da3b11c6488dae18fccb1ae0c7a3eda18e16795844796" +"checksum winapi 0.2.6 (registry+https://github.com/rust-lang/crates.io-index)" = "4dfaaa8fbdaa618fa6914b59b2769d690dd7521920a18d84b42d254678dd5fd4" +"checksum winapi-build 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "2d315eee3b34aca4797b2da6b13ed88266e6d612562a0c46390af8299fc699bc" diff --git a/cargo-apk/injected-glue/lib.rs b/cargo-apk/injected-glue/lib.rs index 10f4987..23b96c4 100644 --- a/cargo-apk/injected-glue/lib.rs +++ b/cargo-apk/injected-glue/lib.rs @@ -64,6 +64,23 @@ pub unsafe extern fn cargo_apk_injected_glue_load_asset(ptr: *const (), len: usi Box::into_raw(Box::new(data)) as *mut _ } +use ffi::{JNIEnv, ANativeActivity, _JavaVM, JavaVM, JNIInvokeInterface}; +use std::mem::transmute; + +#[no_mangle] +pub unsafe extern fn cargo_apk_injected_glue_attach_jvm() { + let mut env: *mut JNIEnv = unsafe {std::mem::uninitialized()}; + + let a: &ANativeActivity = unsafe {transmute(get_app().activity)}; + let vm: &mut _JavaVM = unsafe {transmute(a.vm)}; + let it: &JNIInvokeInterface = unsafe {transmute(vm.functions)}; + + let f = it.AttachCurrentThread; + let ret = f(vm as *mut JavaVM, &mut env as *mut *mut JNIEnv, 0 as *mut c_void); + + write_log(format!("attach vm result: {}", ret).as_str()); +} + /// This static variable will store the android_app* on creation, and set it back to 0 at /// destruction. /// Apart from this, the static is never written, so there is no risk of race condition. @@ -86,12 +103,20 @@ struct Context { primary_pointer_id: Cell, } +pub mod touch_event; +pub use touch_event::{TouchEvent, TouchEventType, Pointer, PointerState}; + +#[derive(Clone, Copy, Debug)] +pub enum KeyEventAction { + Up, + Down, +} + /// An event triggered by the Android environment. #[derive(Clone, Copy, Debug)] pub enum Event { - EventMotion(Motion), - EventKeyUp, - EventKeyDown, + Touch(TouchEvent), + KeyEvent(KeyEventAction, i32), InitWindow, SaveState, TermWindow, @@ -110,24 +135,6 @@ pub enum Event { Destroy, } -/// Data about a motion event. -#[derive(Clone, Copy, Debug)] -pub struct Motion { - pub action: MotionAction, - pub pointer_id: i32, - pub x: f32, - pub y: f32, -} - -/// The type of pointer action in a motion event. -#[derive(Clone, Copy, Debug)] -pub enum MotionAction { - Down, - Move, - Up, - Cancel, -} - #[cfg(not(target_os = "android"))] use this_platform_is_not_supported; @@ -382,64 +389,19 @@ pub extern fn inputs_callback(_: *mut ffi::android_app, event: *const ffi::AInpu let action_code = action & ffi::AMOTION_EVENT_ACTION_MASK; match etype { - ffi::AINPUT_EVENT_TYPE_KEY => match action_code { - ffi::AKEY_EVENT_ACTION_DOWN => { send_event(Event::EventKeyDown); }, - ffi::AKEY_EVENT_ACTION_UP => send_event(Event::EventKeyUp), - _ => write_log(&format!("unknown input-event-type:{} action_code:{}", etype, action_code)), + ffi::AINPUT_EVENT_TYPE_KEY => { + let key_code = unsafe { ffi::AKeyEvent_getKeyCode(event) }; + match action_code { + ffi::AKEY_EVENT_ACTION_DOWN => send_event(Event::KeyEvent(KeyEventAction::Down, key_code)), + ffi::AKEY_EVENT_ACTION_UP => send_event(Event::KeyEvent(KeyEventAction::Up, key_code)), + _ => write_log(&format!("unknown input-event-type:{} action_code:{}", etype, action_code)), + } }, + ffi::AINPUT_EVENT_TYPE_MOTION => { - let motion_action = match action_code { - ffi::AMOTION_EVENT_ACTION_DOWN | - ffi::AMOTION_EVENT_ACTION_POINTER_DOWN => MotionAction::Down, - ffi::AMOTION_EVENT_ACTION_UP | - ffi::AMOTION_EVENT_ACTION_POINTER_UP => MotionAction::Up, - ffi::AMOTION_EVENT_ACTION_MOVE => MotionAction::Move, - ffi::AMOTION_EVENT_ACTION_CANCEL => MotionAction::Cancel, - _ => { - write_log(&format!("unknown action_code:{}", action_code)); - return 0 - } - }; - let context = get_context(); - let idx = ((action & ffi::AMOTION_EVENT_ACTION_POINTER_INDEX_MASK) - >> ffi::AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT) - as usize; - - let pointer_id = unsafe { ffi::AMotionEvent_getPointerId(event, idx) }; - if action_code == ffi::AMOTION_EVENT_ACTION_DOWN { - context.primary_pointer_id.set(pointer_id); - } - let primary_pointer_id = context.primary_pointer_id.get(); - let multitouch = context.multitouch.get(); - - match motion_action { - MotionAction::Down | MotionAction::Up | MotionAction::Cancel => { - if multitouch || pointer_id == primary_pointer_id { - send_event(Event::EventMotion(Motion { - action: motion_action, - pointer_id: pointer_id, - x: unsafe { ffi::AMotionEvent_getX(event, idx) }, - y: unsafe { ffi::AMotionEvent_getY(event, idx) }, - })); - } - } - MotionAction::Move => { - // A move event may have multiple changed pointers. Send an event for each. - let pointer_count = unsafe { ffi::AMotionEvent_getPointerCount(event) }; - for idx in 0..pointer_count { - let pointer_id = unsafe { ffi::AMotionEvent_getPointerId(event, idx) }; - if multitouch || pointer_id == primary_pointer_id { - send_event(Event::EventMotion(Motion { - action: motion_action, - pointer_id: pointer_id, - x: unsafe { ffi::AMotionEvent_getX(event, idx) }, - y: unsafe { ffi::AMotionEvent_getY(event, idx) }, - })); - } - } - } - } + send_event(Event::Touch(TouchEvent::from_input_event(event))) }, + _ => write_log(&format!("unknown input-event-type:{} action_code:{}", etype, action_code)), } 0 diff --git a/cargo-apk/injected-glue/touch_event.rs b/cargo-apk/injected-glue/touch_event.rs new file mode 100644 index 0000000..8737f78 --- /dev/null +++ b/cargo-apk/injected-glue/touch_event.rs @@ -0,0 +1,151 @@ +use ffi; + +#[derive(Clone, Copy, Debug)] +pub enum TouchEventType{ + Down, + PointerDown, + Move, + PointerUp, + Up, + Cancel, +} + +impl TouchEventType{ + fn from_input_event(event: *const ffi::AInputEvent) -> TouchEventType{ + + // let action = unsafe {ffi::AMotionEvent_getAction(event)} & ffi::AMOTION_EVENT_ACTION_MASK; + + match unsafe {ffi::AMotionEvent_getAction(event)} & ffi::AMOTION_EVENT_ACTION_MASK { + // match action { + ffi::AMOTION_EVENT_ACTION_DOWN => //| ffi::AMOTION_EVENT_ACTION_POINTER_DOWN => + TouchEventType::Down, + ffi::AMOTION_EVENT_ACTION_MOVE => + TouchEventType::Move, + ffi::AMOTION_EVENT_ACTION_POINTER_DOWN => + TouchEventType::PointerDown, + ffi::AMOTION_EVENT_ACTION_POINTER_UP => + TouchEventType::PointerUp, + ffi::AMOTION_EVENT_ACTION_UP => //| ffi::AMOTION_EVENT_ACTION_POINTER_UP => + TouchEventType::Up, + _ => TouchEventType::Cancel + } + + // if action == ffi::AMOTION_EVENT_ACTION_DOWN { + // TouchEventType::Down + // } else if action == ffi::AMOTION_EVENT_ACTION_POINTER_DOWN { + // TouchEventType::PointerDown + // } else if action == ffi::AMOTION_EVENT_ACTION_UP { + // TouchEventType::Up + // } else if action == ffi::AMOTION_EVENT_ACTION_POINTER_UP { + // TouchEventType::PointerUp + // } else if action == ffi::AMOTION_EVENT_ACTION_MOVE{ + // TouchEventType::Move + // } else{ + // TouchEventType::Cancel + // } + } +} + +#[derive(Clone, Copy, Debug)] +pub struct TouchEvent { + pub event_type: TouchEventType, + pub timestamp: i64, + pub num_pointers: u8, + pub p0: Pointer, + pub p1: Option, + pub p2: Option, + pub p3: Option, + pub flag: i32, +} + +impl TouchEvent { + + pub fn from_input_event(event: *const ffi::AInputEvent) -> TouchEvent { + + let n = unsafe {ffi::AMotionEvent_getPointerCount(event)}; + + TouchEvent { + timestamp: unsafe {ffi::AMotionEvent_getEventTime(event)}, + p0: Pointer::from_input_event(event, 0), + num_pointers: n as u8, + p1: if n > 1 {Some(Pointer::from_input_event(event, 1))} else {None}, + p2: if n > 2 {Some(Pointer::from_input_event(event, 2))} else {None}, + p3: if n > 3 {Some(Pointer::from_input_event(event, 3))} else {None}, + event_type: TouchEventType::from_input_event(event), + flag: 0, + } + } +} + + +#[derive(Clone, Copy, Debug)] +pub enum PointerState{ + Released, + Pressed, + Moved, + Stationary, + Cancelled, +} + +impl PointerState { + fn from_input_event(event: *const ffi::AInputEvent, pointer_idx: usize) -> PointerState { + let action = unsafe {ffi::AMotionEvent_getAction(event)}; + + // primary pointer; + if action == ffi::AMOTION_EVENT_ACTION_DOWN { + return PointerState::Pressed; + }else if action == ffi::AMOTION_EVENT_ACTION_UP { + return PointerState::Released; + } + + // actions regardless of pointer index; + if action == ffi::AMOTION_EVENT_ACTION_MOVE { + return PointerState::Moved; + }else if action == ffi::AMOTION_EVENT_ACTION_CANCEL { + return PointerState::Cancelled; + } + + // index where the action occured; + let action_idx = (action & ffi::AMOTION_EVENT_ACTION_POINTER_INDEX_MASK) >> ffi::AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT; + if (pointer_idx as i32) != action_idx { + return PointerState::Stationary; + } + + let action_masked = action & ffi::AMOTION_EVENT_ACTION_MASK; + + if action_masked == ffi::AMOTION_EVENT_ACTION_POINTER_DOWN { + return PointerState::Pressed; + }else if action_masked == ffi::AMOTION_EVENT_ACTION_POINTER_UP { + return PointerState::Released; + } + + PointerState::Stationary + } +} + +#[derive(Clone, Copy, Debug)] +pub struct Pointer { + pub state: PointerState, + pub x: f32, + pub y: f32, + pub id: i32, + pub pressure: f32, + pub vertical_radius: f32, + pub horizontal_radius: f32, + pub rotation_angle: f32, +} + +impl Pointer { + fn from_input_event(event: *const ffi::AInputEvent, idx:usize) -> Pointer { + Pointer { + state: PointerState::from_input_event(event, idx), + id: unsafe {ffi::AMotionEvent_getPointerId(event, idx)}, + x: unsafe {ffi::AMotionEvent_getX(event, idx)}, + y: unsafe {ffi::AMotionEvent_getY(event, idx)}, + vertical_radius: unsafe {ffi::AMotionEvent_getTouchMajor(event, idx)} / 2.0, + horizontal_radius: unsafe {ffi::AMotionEvent_getTouchMinor(event, idx)} / 2.0, + pressure: 0.0, + rotation_angle:0.0, + } + } +} diff --git a/cargo-apk/src/build.rs b/cargo-apk/src/build.rs index 7b23fd9..21ce6c6 100644 --- a/cargo-apk/src/build.rs +++ b/cargo-apk/src/build.rs @@ -251,6 +251,21 @@ pub fn build(manifest_path: &Path, config: &Config) -> BuildResult { shared_objects_to_load }; + // copy external java jars to android-artifacts/build/libs; + if let Some(ref src) = config.jar_libs_path { + if !fs::metadata(src).is_err() { + if let Ok(paths) = fs::read_dir(src) { + let dst = android_artifacts_dir.join("build/libs"); + for f in paths { + if let Ok(f) = f { + let d = dst.join(f.file_name()); + fs::copy(f.path(), d).unwrap(); + } + } + } + } + } + // Write the Java source // FIXME: duh, the file will be replaced every time, so this only works with one target build_java_src(&android_artifacts_dir, &config, @@ -281,6 +296,9 @@ fn build_android_artifacts_dir(path: &Path, config: &Config) { let mut ffi = File::create(path.join("injected-glue/ffi.rs")).unwrap(); ffi.write_all(&include_bytes!("../injected-glue/ffi.rs")[..]).unwrap(); + + let mut touch = File::create(path.join("injected-glue/touch_event.rs")).unwrap(); + touch.write_all(&include_bytes!("../injected-glue/touch_event.rs")[..]).unwrap(); } build_linker(path); @@ -327,6 +345,8 @@ fn build_java_src<'a, I>(path: &Path, config: &Config, libs: I) let mut file = File::create(&file).unwrap(); let mut libs_string = String::new(); + libs_string.push_str("System.loadLibrary(\"main\");\n"); + for name in libs { // Strip off the 'lib' prefix and ".so" suffix. let line = format!(" System.loadLibrary(\"{}\");\n", @@ -445,6 +465,7 @@ fn build_build_xml(path: &Path, config: &Config) { write!(file, r#" + diff --git a/cargo-apk/src/config.rs b/cargo-apk/src/config.rs index 522619e..2b93846 100644 --- a/cargo-apk/src/config.rs +++ b/cargo-apk/src/config.rs @@ -39,6 +39,8 @@ pub struct Config { /// /// The assets can later be loaded with the runtime library. pub assets_path: Option, + /// The external jar path; + pub jar_libs_path: Option, /// If `Some`, a path that contains the list of resources to ship as part of the package. /// @@ -89,6 +91,10 @@ pub fn load(manifest_path: &Path) -> Config { the $ANDROID_HOME environment variable.") }; + let jar_libs_path = { + manifest_content.as_ref().and_then(|a| a.jar_libs_path.as_ref()) + .map(|p| manifest_path.parent().unwrap().join(p)) + }; // For the moment some fields of the config are dummies. Config { @@ -101,10 +107,11 @@ pub fn load(manifest_path: &Path) -> Config { package_label: manifest_content.as_ref().and_then(|a| a.label.clone()) .unwrap_or_else(|| package_name.clone()), package_icon: manifest_content.as_ref().and_then(|a| a.icon.clone()), - build_targets: vec!["arm-linux-androideabi".to_owned()], + build_targets: vec!["armv7-linux-androideabi".to_owned()], android_version: manifest_content.as_ref().and_then(|a| a.android_version).unwrap_or(18), assets_path: manifest_content.as_ref().and_then(|a| a.assets.as_ref()) .map(|p| manifest_path.parent().unwrap().join(p)), + jar_libs_path: jar_libs_path, res_path: manifest_content.as_ref().and_then(|a| a.res.as_ref()) .map(|p| manifest_path.parent().unwrap().join(p)), release: false, @@ -144,6 +151,7 @@ struct TomlAndroid { label: Option, icon: Option, assets: Option, + jar_libs_path: Option, res: Option, android_version: Option, fullscreen: Option, diff --git a/examples/basic/Cargo.lock b/examples/basic/Cargo.lock deleted file mode 100644 index 046b908..0000000 --- a/examples/basic/Cargo.lock +++ /dev/null @@ -1,11 +0,0 @@ -[root] -name = "android_glue_example" -version = "0.1.0" -dependencies = [ - "android_glue 0.2.0", -] - -[[package]] -name = "android_glue" -version = "0.2.0" - diff --git a/examples/basic/Cargo.toml b/examples/basic/Cargo.toml deleted file mode 100644 index 60c35a4..0000000 --- a/examples/basic/Cargo.toml +++ /dev/null @@ -1,14 +0,0 @@ -[package] -name = "android_glue_example" -version = "0.1.0" -authors = ["Pierre Krieger "] - -[package.metadata.android] -label = "Basic android-rs-glue example" - -[[bin]] -name = "example" -path = "src/basic.rs" - -[dependencies.android_glue] -path = "../../glue" diff --git a/examples/basic/src/basic.rs b/examples/basic/src/basic.rs deleted file mode 100644 index 0ed61c7..0000000 --- a/examples/basic/src/basic.rs +++ /dev/null @@ -1,6 +0,0 @@ -extern crate android_glue; - -fn main() { - android_glue::write_log("main() has been called"); - loop {} -} diff --git a/examples/use_assets/Cargo.toml b/examples/use_assets/Cargo.toml deleted file mode 100644 index 8dd86e1..0000000 --- a/examples/use_assets/Cargo.toml +++ /dev/null @@ -1,11 +0,0 @@ -[package] -name = "android_glue_assets_example" -version = "0.1.0" -authors = ["Pierre Krieger "] - -[package.metadata.android] -label = "Using assets android-rs-glue example" -assets = "assets" - -[dependencies.android_glue] -path = "../../glue" diff --git a/examples/use_assets/assets/test_asset b/examples/use_assets/assets/test_asset deleted file mode 100644 index 44e1b76..0000000 --- a/examples/use_assets/assets/test_asset +++ /dev/null @@ -1,3 +0,0 @@ -str1 -str2 -str3 diff --git a/examples/use_assets/src/fs.rs b/examples/use_assets/src/fs.rs deleted file mode 100644 index 2678926..0000000 --- a/examples/use_assets/src/fs.rs +++ /dev/null @@ -1,26 +0,0 @@ -use std::path::{Path}; -use std::io::{Cursor}; - -#[cfg(not(target_os = "android"))] -pub fn load>(path: P) -> Cursor> { - use std::fs::{File}; - use std::io::{Read}; - - let mut buf = Vec::new(); - let fullpath = &Path::new("assets").join(&path); - let mut file = File::open(&fullpath).unwrap(); - file.read_to_end(&mut buf).unwrap(); - Cursor::new(buf) -} - -#[cfg(target_os = "android")] -pub fn load>(path: P) -> Cursor> { - use android_glue; - - let filename = path.as_ref().to_str() - .expect("Can`t convert Path to &str"); - match android_glue::load_asset(filename) { - Ok(buf) => Cursor::new(buf), - Err(_) => panic!("Can`t load asset '{}'", filename), - } -} diff --git a/examples/use_assets/src/main.rs b/examples/use_assets/src/main.rs deleted file mode 100644 index 34b389a..0000000 --- a/examples/use_assets/src/main.rs +++ /dev/null @@ -1,13 +0,0 @@ -extern crate android_glue; - -mod fs; - -use std::io::BufRead; - -fn main() { - let f = fs::load("test_asset"); - for line in f.lines() { - println!("{:?}", line); - } - loop {} -} diff --git a/examples/use_icon/Cargo.toml b/examples/use_icon/Cargo.toml deleted file mode 100644 index 78d14a1..0000000 --- a/examples/use_icon/Cargo.toml +++ /dev/null @@ -1,12 +0,0 @@ -[package] -name = "android_glue_icon_example" -version = "0.1.0" -authors = ["Pierre Krieger "] - -[package.metadata.android] -label = "Using icon android-rs-glue example" -res = "res" -icon = "@mipmap/ic_launcher" - -[dependencies.android_glue] -path = "../../glue" \ No newline at end of file diff --git a/examples/use_icon/res/mipmap-hdpi/ic_launcher.png b/examples/use_icon/res/mipmap-hdpi/ic_launcher.png deleted file mode 100644 index 30ffc5e..0000000 Binary files a/examples/use_icon/res/mipmap-hdpi/ic_launcher.png and /dev/null differ diff --git a/examples/use_icon/res/mipmap-mdpi/ic_launcher.png b/examples/use_icon/res/mipmap-mdpi/ic_launcher.png deleted file mode 100644 index 7c4fe22..0000000 Binary files a/examples/use_icon/res/mipmap-mdpi/ic_launcher.png and /dev/null differ diff --git a/examples/use_icon/res/mipmap-xhdpi/ic_launcher.png b/examples/use_icon/res/mipmap-xhdpi/ic_launcher.png deleted file mode 100644 index 172616a..0000000 Binary files a/examples/use_icon/res/mipmap-xhdpi/ic_launcher.png and /dev/null differ diff --git a/examples/use_icon/res/mipmap-xxhdpi/ic_launcher.png b/examples/use_icon/res/mipmap-xxhdpi/ic_launcher.png deleted file mode 100644 index 2149348..0000000 Binary files a/examples/use_icon/res/mipmap-xxhdpi/ic_launcher.png and /dev/null differ diff --git a/examples/use_icon/res/mipmap-xxxhdpi/ic_launcher.png b/examples/use_icon/res/mipmap-xxxhdpi/ic_launcher.png deleted file mode 100644 index d7e4274..0000000 Binary files a/examples/use_icon/res/mipmap-xxxhdpi/ic_launcher.png and /dev/null differ diff --git a/examples/use_icon/src/main.rs b/examples/use_icon/src/main.rs deleted file mode 100644 index 0aa524c..0000000 --- a/examples/use_icon/src/main.rs +++ /dev/null @@ -1,6 +0,0 @@ -extern crate android_glue; - -fn main() { - android_glue::write_log("main() has been called, did you like the icon?"); - loop {} -} \ No newline at end of file diff --git a/glue/Cargo.toml b/glue/Cargo.toml index 6adf1cf..faaf279 100644 --- a/glue/Cargo.toml +++ b/glue/Cargo.toml @@ -6,3 +6,6 @@ authors = ["Pierre Krieger "] license = "MIT" description = "Glue for the Android JNI" repository = "https://github.com/tomaka/android-rs-glue" + +[dependencies] +libc = "*" diff --git a/glue/src/lib.rs b/glue/src/lib.rs index 642b09b..67f7a6f 100644 --- a/glue/src/lib.rs +++ b/glue/src/lib.rs @@ -1,26 +1,35 @@ #![cfg(target_os = "android")] +use std::mem; +use std::os::raw::c_void; +use std::sync::mpsc::Sender; + extern { fn cargo_apk_injected_glue_get_native_window() -> *const c_void; - fn cargo_apk_injected_glue_add_sender(sender: *mut ()); - fn cargo_apk_injected_glue_add_sender_missing(sender: *mut ()); + fn cargo_apk_injected_glue_add_sender(sender: *mut c_void); + fn cargo_apk_injected_glue_add_sender_missing(sender: *mut c_void); fn cargo_apk_injected_glue_set_multitouch(multitouch: bool); - fn cargo_apk_injected_glue_write_log(ptr: *const (), len: usize); - fn cargo_apk_injected_glue_load_asset(ptr: *const (), len: usize) -> *mut c_void; + fn cargo_apk_injected_glue_write_log(ptr: *const c_void, len: usize); + // fn cargo_apk_injected_glue_attach_jvm() ; + fn cargo_apk_injected_glue_load_asset(ptr: *const c_void, len: usize) -> *mut c_void; } -use std::mem; -use std::os::raw::c_void; -use std::sync::mpsc::Sender; //pub use cargo_apk_injected_glue::ffi; +mod touch_event; +pub use touch_event::{TouchEvent, TouchEventType, Pointer, PointerState}; -/// An event triggered by the Android environment. #[derive(Clone, Copy, Debug)] +pub enum KeyEventAction { + Up, + Down, +} + +/// An event triggered by the Android environment. +#[derive(Copy, Clone, Debug)] pub enum Event { - EventMotion(Motion), - EventKeyUp, - EventKeyDown, + Touch(TouchEvent), + KeyEvent(KeyEventAction, i32), InitWindow, SaveState, TermWindow, @@ -39,24 +48,6 @@ pub enum Event { Destroy, } -/// Data about a motion event. -#[derive(Clone, Copy, Debug)] -pub struct Motion { - pub action: MotionAction, - pub pointer_id: i32, - pub x: f32, - pub y: f32, -} - -/// The type of pointer action in a motion event. -#[derive(Clone, Copy, Debug)] -pub enum MotionAction { - Down, - Move, - Up, - Cancel, -} - pub enum AssetError { AssetMissing, EmptyBuffer, @@ -113,6 +104,13 @@ pub fn write_log(message: &str) { } } +// #[inline] +// pub fn attach_jvm(){ +// unsafe { +// cargo_apk_injected_glue_attach_jvm() +// } +// } + #[inline] pub fn load_asset(filename: &str) -> Result, AssetError> { unsafe { @@ -122,3 +120,17 @@ pub fn load_asset(filename: &str) -> Result, AssetError> { *data } } + +extern crate libc; +mod ffi; +use ffi::{AConfiguration_getDensity, AConfiguration_new, AConfiguration_delete}; + +#[inline] +pub fn get_screen_density() -> i32{ + unsafe { + let config = AConfiguration_new(); + let ret = AConfiguration_getDensity(config); + AConfiguration_delete(config); + ret + } +} diff --git a/glue/src/touch_event.rs b/glue/src/touch_event.rs new file mode 100644 index 0000000..72b0368 --- /dev/null +++ b/glue/src/touch_event.rs @@ -0,0 +1,42 @@ +#[derive(Clone, Copy, Debug)] +pub enum TouchEventType{ + Down, + PointerDown, + Move, + PointerUp, + Up, + Cancel, +} + +#[derive(Clone, Copy, Debug)] +pub struct TouchEvent { + pub event_type: TouchEventType, + pub timestamp: i64, + pub num_pointers: u8, + pub p0: Pointer, + pub p1: Option, + pub p2: Option, + pub p3: Option, + pub flag: i32, +} + +#[derive(Clone, Copy, Debug)] +pub enum PointerState{ + Released, + Pressed, + Moved, + Stationary, + Cancelled, +} + +#[derive(Clone, Copy, Debug)] +pub struct Pointer { + pub state: PointerState, + pub x: f32, + pub y: f32, + pub id: i32, + pub pressure: f32, + pub vertical_radius: f32, + pub horizontal_radius: f32, + pub rotation_angle: f32, +}