codemp/src/ffi/java/cursor_controller.rs

79 lines
2.8 KiB
Rust
Raw Normal View History

2024-08-07 01:44:27 +02:00
use jni::{objects::{JClass, JObject, JString, JValueGen}, sys::{jlong, jobject}, JNIEnv};
use crate::{api::Controller, ffi::java::util::JExceptable};
#[no_mangle]
pub extern "system" fn Java_mp_code_CursorController_try_1recv(
mut env: JNIEnv,
_class: JClass,
self_ptr: jlong,
) -> jobject {
let controller = unsafe { Box::leak(Box::from_raw(self_ptr as *mut crate::cursor::Controller)) };
match controller.try_recv().jexcept(&mut env) {
None => JObject::null().as_raw(),
Some(event) => {
let class = env.find_class("mp/code/data/Cursor").expect("Couldn't find class!");
env.new_object(
class,
"(IIIILjava/lang/String;Ljava/lang/String;)V",
&[
JValueGen::Int(event.start.0),
JValueGen::Int(event.start.1),
JValueGen::Int(event.end.0),
JValueGen::Int(event.end.1),
JValueGen::Object(&env.new_string(event.buffer).expect("Failed to create String!")),
JValueGen::Object(&env.new_string(event.user.map(|x| x.to_string()).unwrap_or_default()).expect("Failed to create String!"))
]
).expect("failed creating object").into_raw()
}
}
}
#[no_mangle]
pub extern "system" fn Java_mp_code_CursorController_send<'local>(
mut env: JNIEnv,
_class: JClass<'local>,
self_ptr: jlong,
input: JObject<'local>,
) {
2024-08-07 01:44:27 +02:00
let start_row = env.get_field(&input, "startRow", "I").expect("could not get field").i().expect("field was not of expected type");
let start_col = env.get_field(&input, "startCol", "I").expect("could not get field").i().expect("field was not of expected type");
let end_row = env.get_field(&input, "endRow", "I").expect("could not get field").i().expect("field was not of expected type");
let end_col = env.get_field(&input, "endCol", "I").expect("could not get field").i().expect("field was not of expected type");
let buffer = env.get_field(&input, "buffer", "Ljava/lang/String;")
.expect("could not get field")
.l()
.expect("field was not of expected type")
.into();
let buffer = env.get_string(&buffer).expect("Failed to get String!").into();
let user: JString = env.get_field(&input, "user", "Ljava/lang/String;")
.expect("could not get field")
.l()
.expect("field was not of expected type")
.into();
let user = if user.is_null() {
None
} else {
let jstring = env.get_string(&user).expect("Failed to get String!");
Some(uuid::Uuid::parse_str(jstring.to_str().expect("Not valid UTF-8")).expect("Invalid UUI!"))
};
let controller = unsafe { Box::leak(Box::from_raw(self_ptr as *mut crate::cursor::Controller)) };
controller.send(crate::api::Cursor {
start: (start_row, start_col),
end: (end_row, end_col),
buffer,
user
}).jexcept(&mut env);
}
#[no_mangle]
2024-08-07 01:44:27 +02:00
pub extern "system" fn Java_mp_code_CursorController_free(
_env: JNIEnv,
2024-08-07 01:44:27 +02:00
_class: JClass,
self_ptr: jlong,
) {
let _ = unsafe { Box::from_raw(self_ptr as *mut crate::cursor::Controller) };
}