2024-08-19 11:36:51 +02:00
|
|
|
use jni::{objects::{JClass, JObject, JValueGen}, sys::{jlong, jobject, jstring}, JNIEnv};
|
2024-08-06 23:55:57 +02:00
|
|
|
|
2024-08-07 00:37:58 +02:00
|
|
|
use crate::api::Controller;
|
2024-08-06 23:55:57 +02:00
|
|
|
|
2024-08-10 02:45:20 +02:00
|
|
|
use super::{JExceptable, RT};
|
2024-08-06 23:55:57 +02:00
|
|
|
|
2024-08-08 00:29:54 +02:00
|
|
|
/// Gets the name of the buffer.
|
2024-08-06 23:55:57 +02:00
|
|
|
#[no_mangle]
|
2024-08-07 01:44:27 +02:00
|
|
|
pub extern "system" fn Java_mp_code_BufferController_get_1name(
|
2024-08-10 02:45:20 +02:00
|
|
|
mut env: JNIEnv,
|
2024-08-07 01:44:27 +02:00
|
|
|
_class: JClass,
|
2024-08-06 23:55:57 +02:00
|
|
|
self_ptr: jlong,
|
|
|
|
) -> jstring {
|
|
|
|
let controller = unsafe { Box::leak(Box::from_raw(self_ptr as *mut crate::buffer::Controller)) };
|
|
|
|
let content = controller.name();
|
|
|
|
env.new_string(content)
|
2024-08-10 02:45:20 +02:00
|
|
|
.jexcept(&mut env)
|
2024-08-06 23:55:57 +02:00
|
|
|
.as_raw()
|
|
|
|
}
|
|
|
|
|
2024-08-08 00:29:54 +02:00
|
|
|
/// Gets the contents of the buffers.
|
2024-08-06 23:55:57 +02:00
|
|
|
#[no_mangle]
|
2024-08-07 01:44:27 +02:00
|
|
|
pub extern "system" fn Java_mp_code_BufferController_get_1content(
|
2024-08-10 02:45:20 +02:00
|
|
|
mut env: JNIEnv,
|
2024-08-07 01:44:27 +02:00
|
|
|
_class: JClass,
|
2024-08-06 23:55:57 +02:00
|
|
|
self_ptr: jlong,
|
|
|
|
) -> jstring {
|
|
|
|
let controller = unsafe { Box::leak(Box::from_raw(self_ptr as *mut crate::buffer::Controller)) };
|
2024-08-13 21:58:40 +02:00
|
|
|
let content = RT.block_on(controller.content())
|
|
|
|
.jexcept(&mut env);
|
2024-08-06 23:55:57 +02:00
|
|
|
env.new_string(content)
|
2024-08-10 02:45:20 +02:00
|
|
|
.jexcept(&mut env)
|
2024-08-06 23:55:57 +02:00
|
|
|
.as_raw()
|
|
|
|
}
|
|
|
|
|
2024-08-08 00:29:54 +02:00
|
|
|
/// Tries to fetch a [crate::api::TextChange], or returns null if there's nothing.
|
2024-08-06 23:55:57 +02:00
|
|
|
#[no_mangle]
|
2024-08-07 01:44:27 +02:00
|
|
|
pub extern "system" fn Java_mp_code_BufferController_try_1recv(
|
2024-08-06 23:55:57 +02:00
|
|
|
mut env: JNIEnv,
|
2024-08-07 01:44:27 +02:00
|
|
|
_class: JClass,
|
2024-08-06 23:55:57 +02:00
|
|
|
self_ptr: jlong,
|
|
|
|
) -> jobject {
|
|
|
|
let controller = unsafe { Box::leak(Box::from_raw(self_ptr as *mut crate::buffer::Controller)) };
|
2024-08-14 00:27:26 +02:00
|
|
|
let change = RT.block_on(controller.try_recv()).jexcept(&mut env);
|
2024-08-07 10:22:01 +02:00
|
|
|
recv_jni(&mut env, change)
|
|
|
|
}
|
|
|
|
|
2024-08-08 00:29:54 +02:00
|
|
|
/// Blocks until it receives a [crate::api::TextChange].
|
2024-08-07 10:22:01 +02:00
|
|
|
#[no_mangle]
|
|
|
|
pub extern "system" fn Java_mp_code_BufferController_recv(
|
|
|
|
mut env: JNIEnv,
|
|
|
|
_class: JClass,
|
|
|
|
self_ptr: jlong,
|
|
|
|
) -> jobject {
|
|
|
|
let controller = unsafe { Box::leak(Box::from_raw(self_ptr as *mut crate::buffer::Controller)) };
|
|
|
|
let change = RT.block_on(controller.recv()).map(Some).jexcept(&mut env);
|
|
|
|
recv_jni(&mut env, change)
|
|
|
|
}
|
|
|
|
|
2024-08-08 00:29:54 +02:00
|
|
|
/// Utility method to convert a [crate::api::TextChange] to its Java equivalent.
|
2024-08-07 10:22:01 +02:00
|
|
|
fn recv_jni(env: &mut JNIEnv, change: Option<crate::api::TextChange>) -> jobject {
|
|
|
|
match change {
|
2024-08-10 02:45:20 +02:00
|
|
|
None => JObject::default(),
|
2024-08-07 01:44:27 +02:00
|
|
|
Some(event) => {
|
2024-08-10 02:45:20 +02:00
|
|
|
let content = env.new_string(event.content).jexcept(env);
|
2024-08-16 01:21:21 +02:00
|
|
|
|
|
|
|
let hash = env.find_class("java/util/OptionalLong").and_then(|class| {
|
|
|
|
if let Some(h) = event.hash {
|
|
|
|
env.call_static_method(class, "of", "(J)Ljava/util/OptionalLong;", &[JValueGen::Long(h)])
|
|
|
|
} else {
|
|
|
|
env.call_static_method(class, "empty", "()Ljava/util/OptionalLong;", &[])
|
|
|
|
}
|
|
|
|
}).and_then(|o| o.l()).jexcept(env);
|
2024-08-10 02:45:20 +02:00
|
|
|
env.find_class("mp/code/data/TextChange")
|
|
|
|
.and_then(|class| {
|
|
|
|
env.new_object(
|
|
|
|
class,
|
2024-08-16 01:21:21 +02:00
|
|
|
"(JJLjava/lang/String;Ljava/util/OptionalLong;)V",
|
2024-08-10 02:45:20 +02:00
|
|
|
&[
|
|
|
|
JValueGen::Long(jlong::from(event.start)),
|
|
|
|
JValueGen::Long(jlong::from(event.end)),
|
|
|
|
JValueGen::Object(&content),
|
2024-08-16 01:21:21 +02:00
|
|
|
JValueGen::Object(&hash)
|
2024-08-10 02:45:20 +02:00
|
|
|
]
|
|
|
|
)
|
|
|
|
}).jexcept(env)
|
2024-08-07 01:44:27 +02:00
|
|
|
}
|
2024-08-10 02:45:20 +02:00
|
|
|
}.as_raw()
|
2024-08-06 23:55:57 +02:00
|
|
|
}
|