2024-09-17 17:37:22 +02:00
|
|
|
use jni::{objects::{JClass, JObject, JString}, sys::{jboolean, jlong, jobject, jobjectArray, jstring}, JNIEnv};
|
2024-08-07 00:00:24 +02:00
|
|
|
use crate::Workspace;
|
2024-08-06 23:30:00 +02:00
|
|
|
|
2024-09-17 17:37:22 +02:00
|
|
|
use super::{handle_error, null_check, JExceptable, JObjectify};
|
2024-08-06 23:30:00 +02:00
|
|
|
|
2024-09-05 02:45:33 +02:00
|
|
|
/// Get the workspace id.
|
2024-08-07 00:00:24 +02:00
|
|
|
#[no_mangle]
|
2024-08-07 00:37:58 +02:00
|
|
|
pub extern "system" fn Java_mp_code_Workspace_get_1workspace_1id<'local>(
|
2024-08-10 02:45:20 +02:00
|
|
|
mut env: JNIEnv<'local>,
|
2024-08-07 00:00:24 +02:00
|
|
|
_class: JClass<'local>,
|
|
|
|
self_ptr: jlong
|
|
|
|
) -> jstring {
|
2024-08-09 14:11:13 +02:00
|
|
|
let workspace = unsafe { Box::leak(Box::from_raw(self_ptr as *mut Workspace)) };
|
2024-08-10 02:45:20 +02:00
|
|
|
env.new_string(workspace.id()).jexcept(&mut env).as_raw()
|
2024-08-07 00:00:24 +02:00
|
|
|
}
|
|
|
|
|
2024-09-05 02:45:33 +02:00
|
|
|
/// Get a cursor controller by name and returns a pointer to it.
|
2024-08-07 00:00:24 +02:00
|
|
|
#[no_mangle]
|
2024-08-07 00:37:58 +02:00
|
|
|
pub extern "system" fn Java_mp_code_Workspace_get_1cursor<'local>(
|
2024-08-07 10:22:01 +02:00
|
|
|
mut env: JNIEnv<'local>,
|
2024-08-07 00:00:24 +02:00
|
|
|
_class: JClass<'local>,
|
|
|
|
self_ptr: jlong
|
2024-08-07 10:22:01 +02:00
|
|
|
) -> jobject {
|
2024-08-09 14:11:13 +02:00
|
|
|
let workspace = unsafe { Box::leak(Box::from_raw(self_ptr as *mut Workspace)) };
|
2024-09-15 01:56:51 +02:00
|
|
|
workspace.cursor().jobjectify(&mut env).jexcept(&mut env).as_raw()
|
2024-08-07 00:00:24 +02:00
|
|
|
}
|
|
|
|
|
2024-09-05 02:45:33 +02:00
|
|
|
/// Get a buffer controller by name and returns a pointer to it.
|
2024-08-07 00:00:24 +02:00
|
|
|
#[no_mangle]
|
2024-08-07 00:37:58 +02:00
|
|
|
pub extern "system" fn Java_mp_code_Workspace_get_1buffer<'local>(
|
2024-08-07 10:22:01 +02:00
|
|
|
mut env: JNIEnv<'local>,
|
2024-08-07 00:00:24 +02:00
|
|
|
_class: JClass<'local>,
|
|
|
|
self_ptr: jlong,
|
2024-09-17 17:37:22 +02:00
|
|
|
path: JString<'local>
|
2024-08-07 10:22:01 +02:00
|
|
|
) -> jobject {
|
2024-09-17 17:37:22 +02:00
|
|
|
null_check!(env, path, std::ptr::null_mut());
|
2024-08-09 14:11:13 +02:00
|
|
|
let workspace = unsafe { Box::leak(Box::from_raw(self_ptr as *mut Workspace)) };
|
2024-09-17 17:37:22 +02:00
|
|
|
let path = unsafe { env.get_string_unchecked(&path) }
|
2024-08-10 02:45:20 +02:00
|
|
|
.map(|path| path.to_string_lossy().to_string())
|
|
|
|
.jexcept(&mut env);
|
2024-09-15 01:56:51 +02:00
|
|
|
workspace.buffer_by_name(&path)
|
|
|
|
.map(|buf| buf.jobjectify(&mut env).jexcept(&mut env))
|
|
|
|
.unwrap_or_default()
|
|
|
|
.as_raw()
|
2024-08-06 23:30:00 +02:00
|
|
|
}
|
|
|
|
|
2024-09-05 02:45:33 +02:00
|
|
|
/// Get the filetree.
|
2024-08-07 01:34:13 +02:00
|
|
|
#[no_mangle]
|
2024-08-07 01:44:27 +02:00
|
|
|
pub extern "system" fn Java_mp_code_Workspace_get_1file_1tree(
|
2024-08-07 02:43:25 +02:00
|
|
|
mut env: JNIEnv,
|
2024-08-07 01:44:27 +02:00
|
|
|
_class: JClass,
|
2024-08-07 01:34:13 +02:00
|
|
|
self_ptr: jlong,
|
2024-09-13 20:02:42 +02:00
|
|
|
filter: JString,
|
|
|
|
strict: jboolean
|
2024-08-07 02:43:25 +02:00
|
|
|
) -> jobjectArray {
|
2024-08-09 14:11:13 +02:00
|
|
|
let workspace = unsafe { Box::leak(Box::from_raw(self_ptr as *mut Workspace)) };
|
2024-08-21 23:41:22 +02:00
|
|
|
let filter: Option<String> = if filter.is_null() {
|
|
|
|
None
|
|
|
|
} else {
|
|
|
|
Some(
|
|
|
|
env.get_string(&filter)
|
|
|
|
.map(|s| s.into())
|
|
|
|
.jexcept(&mut env)
|
|
|
|
)
|
|
|
|
};
|
|
|
|
|
2024-09-13 20:02:42 +02:00
|
|
|
let file_tree = workspace.filetree(filter.as_deref(), strict != 0);
|
2024-08-10 02:45:20 +02:00
|
|
|
env.find_class("java/lang/String")
|
|
|
|
.and_then(|class| env.new_object_array(file_tree.len() as i32, class, JObject::null()))
|
2024-09-11 15:47:56 +02:00
|
|
|
.inspect(|arr| {
|
2024-08-10 02:45:20 +02:00
|
|
|
for (idx, path) in file_tree.iter().enumerate() {
|
|
|
|
env.new_string(path)
|
2024-09-11 15:47:56 +02:00
|
|
|
.and_then(|path| env.set_object_array_element(arr, idx as i32, path))
|
2024-08-10 02:45:20 +02:00
|
|
|
.jexcept(&mut env)
|
|
|
|
}
|
|
|
|
}).jexcept(&mut env).as_raw()
|
2024-08-07 01:34:13 +02:00
|
|
|
}
|
|
|
|
|
2024-09-17 02:40:03 +02:00
|
|
|
/// Gets a list of the active buffers.
|
|
|
|
#[no_mangle]
|
|
|
|
pub extern "system" fn Java_mp_code_Workspace_active_1buffers(
|
|
|
|
mut env: JNIEnv,
|
|
|
|
_class: JClass,
|
|
|
|
self_ptr: jlong
|
|
|
|
) -> jobjectArray {
|
|
|
|
let workspace = unsafe { Box::leak(Box::from_raw(self_ptr as *mut Workspace)) };
|
|
|
|
let active_buffer_list = workspace.buffer_list();
|
|
|
|
env.find_class("java/lang/String")
|
|
|
|
.and_then(|class| env.new_object_array(active_buffer_list.len() as i32, class, JObject::null()))
|
|
|
|
.inspect(|arr| {
|
|
|
|
for (idx, path) in active_buffer_list.iter().enumerate() {
|
|
|
|
env.new_string(path)
|
|
|
|
.and_then(|path| env.set_object_array_element(arr, idx as i32, path))
|
|
|
|
.jexcept(&mut env)
|
|
|
|
}
|
|
|
|
}).jexcept(&mut env).as_raw()
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Create a new buffer.
|
|
|
|
#[no_mangle]
|
|
|
|
pub extern "system" fn Java_mp_code_Workspace_create_1buffer<'local>(
|
|
|
|
mut env: JNIEnv,
|
|
|
|
_class: JClass<'local>,
|
|
|
|
self_ptr: jlong,
|
2024-09-17 17:37:22 +02:00
|
|
|
path: JString<'local>
|
2024-09-17 02:40:03 +02:00
|
|
|
) {
|
2024-09-17 17:37:22 +02:00
|
|
|
null_check!(env, path, {});
|
2024-09-17 02:40:03 +02:00
|
|
|
let ws = unsafe { Box::leak(Box::from_raw(self_ptr as *mut Workspace)) };
|
2024-09-17 17:37:22 +02:00
|
|
|
let path = unsafe { env.get_string_unchecked(&path) }
|
2024-09-17 02:40:03 +02:00
|
|
|
.map(|path| path.to_string_lossy().to_string())
|
|
|
|
.jexcept(&mut env);
|
|
|
|
super::tokio().block_on(ws.create(&path))
|
|
|
|
.jexcept(&mut env);
|
|
|
|
}
|
|
|
|
|
2024-09-05 02:45:33 +02:00
|
|
|
/// Attach to a buffer and return a pointer to its [crate::buffer::Controller].
|
2024-08-07 01:34:13 +02:00
|
|
|
#[no_mangle]
|
|
|
|
pub extern "system" fn Java_mp_code_Workspace_attach_1to_1buffer<'local>(
|
|
|
|
mut env: JNIEnv,
|
|
|
|
_class: JClass<'local>,
|
2024-08-07 01:44:27 +02:00
|
|
|
self_ptr: jlong,
|
2024-09-17 17:37:22 +02:00
|
|
|
path: JString<'local>
|
2024-08-07 10:22:01 +02:00
|
|
|
) -> jobject {
|
2024-09-17 17:37:22 +02:00
|
|
|
null_check!(env, path, std::ptr::null_mut());
|
2024-08-09 14:11:13 +02:00
|
|
|
let workspace = unsafe { Box::leak(Box::from_raw(self_ptr as *mut Workspace)) };
|
2024-09-17 17:37:22 +02:00
|
|
|
let path = unsafe { env.get_string_unchecked(&path) }
|
2024-08-10 02:45:20 +02:00
|
|
|
.map(|path| path.to_string_lossy().to_string())
|
|
|
|
.jexcept(&mut env);
|
2024-09-16 00:20:03 +02:00
|
|
|
super::tokio().block_on(workspace.attach(&path))
|
2024-09-15 01:56:51 +02:00
|
|
|
.map(|buffer| buffer.jobjectify(&mut env).jexcept(&mut env))
|
|
|
|
.jexcept(&mut env)
|
|
|
|
.as_raw()
|
2024-08-07 01:34:13 +02:00
|
|
|
}
|
|
|
|
|
2024-09-05 02:45:33 +02:00
|
|
|
/// Detach from a buffer.
|
2024-08-08 02:45:52 +02:00
|
|
|
#[no_mangle]
|
|
|
|
pub extern "system" fn Java_mp_code_Workspace_detach_1from_1buffer<'local>(
|
|
|
|
mut env: JNIEnv,
|
|
|
|
_class: JClass<'local>,
|
|
|
|
self_ptr: jlong,
|
2024-09-17 17:37:22 +02:00
|
|
|
path: JString<'local>
|
2024-08-08 02:45:52 +02:00
|
|
|
) -> jobject {
|
2024-09-17 17:37:22 +02:00
|
|
|
null_check!(env, path, std::ptr::null_mut());
|
2024-08-09 14:11:13 +02:00
|
|
|
let workspace = unsafe { Box::leak(Box::from_raw(self_ptr as *mut Workspace)) };
|
2024-09-17 17:37:22 +02:00
|
|
|
let path = unsafe { env.get_string_unchecked(&path) }
|
2024-08-10 02:45:20 +02:00
|
|
|
.map(|path| path.to_string_lossy().to_string())
|
|
|
|
.jexcept(&mut env);
|
2024-09-17 17:37:22 +02:00
|
|
|
workspace.detach(&path)
|
|
|
|
.jobjectify(&mut env)
|
2024-08-10 02:45:20 +02:00
|
|
|
.jexcept(&mut env)
|
2024-08-08 02:45:52 +02:00
|
|
|
.as_raw()
|
|
|
|
}
|
|
|
|
|
2024-09-05 02:45:33 +02:00
|
|
|
/// Update the local buffer list.
|
2024-08-07 01:34:13 +02:00
|
|
|
#[no_mangle]
|
2024-08-07 01:44:27 +02:00
|
|
|
pub extern "system" fn Java_mp_code_Workspace_fetch_1buffers(
|
2024-08-07 01:34:13 +02:00
|
|
|
mut env: JNIEnv,
|
2024-08-07 01:44:27 +02:00
|
|
|
_class: JClass,
|
2024-08-07 01:34:13 +02:00
|
|
|
self_ptr: jlong,
|
|
|
|
) {
|
2024-08-09 14:11:13 +02:00
|
|
|
let workspace = unsafe { Box::leak(Box::from_raw(self_ptr as *mut Workspace)) };
|
2024-09-16 00:20:03 +02:00
|
|
|
super::tokio().block_on(workspace.fetch_buffers()).jexcept(&mut env);
|
2024-08-07 01:34:13 +02:00
|
|
|
}
|
|
|
|
|
2024-09-05 02:45:33 +02:00
|
|
|
/// Update the local user list.
|
2024-08-07 01:34:13 +02:00
|
|
|
#[no_mangle]
|
2024-08-07 01:44:27 +02:00
|
|
|
pub extern "system" fn Java_mp_code_Workspace_fetch_1users(
|
2024-08-07 01:34:13 +02:00
|
|
|
mut env: JNIEnv,
|
2024-08-07 01:44:27 +02:00
|
|
|
_class: JClass,
|
2024-08-07 01:34:13 +02:00
|
|
|
self_ptr: jlong,
|
|
|
|
) {
|
2024-08-09 14:11:13 +02:00
|
|
|
let workspace = unsafe { Box::leak(Box::from_raw(self_ptr as *mut Workspace)) };
|
2024-09-16 00:20:03 +02:00
|
|
|
super::tokio().block_on(workspace.fetch_users()).jexcept(&mut env);
|
2024-08-07 01:34:13 +02:00
|
|
|
}
|
|
|
|
|
2024-09-05 02:45:33 +02:00
|
|
|
/// List users attached to a buffer.
|
2024-08-07 01:34:13 +02:00
|
|
|
#[no_mangle]
|
|
|
|
pub extern "system" fn Java_mp_code_Workspace_list_1buffer_1users<'local>(
|
|
|
|
mut env: JNIEnv,
|
|
|
|
_class: JClass<'local>,
|
|
|
|
self_ptr: jlong,
|
2024-09-17 17:37:22 +02:00
|
|
|
path: JString<'local>,
|
2024-08-07 02:43:25 +02:00
|
|
|
) -> jobjectArray {
|
2024-09-17 17:37:22 +02:00
|
|
|
null_check!(env, path, std::ptr::null_mut());
|
2024-08-09 14:11:13 +02:00
|
|
|
let workspace = unsafe { Box::leak(Box::from_raw(self_ptr as *mut Workspace)) };
|
2024-09-17 17:37:22 +02:00
|
|
|
let buffer = unsafe { env.get_string_unchecked(&path) }
|
2024-08-10 02:45:20 +02:00
|
|
|
.map(|buffer| buffer.to_string_lossy().to_string())
|
|
|
|
.jexcept(&mut env);
|
2024-09-16 00:20:03 +02:00
|
|
|
let users = super::tokio().block_on(workspace.list_buffer_users(&buffer))
|
2024-08-07 02:43:25 +02:00
|
|
|
.jexcept(&mut env);
|
|
|
|
|
2024-09-17 02:40:03 +02:00
|
|
|
if env.exception_check().unwrap_or(false) { // prevent illegal state
|
|
|
|
return std::ptr::null_mut();
|
|
|
|
}
|
|
|
|
|
2024-08-19 11:36:51 +02:00
|
|
|
env.find_class("java/util/UUID")
|
|
|
|
.and_then(|class| env.new_object_array(users.len() as i32, &class, JObject::null()))
|
2024-09-11 15:47:56 +02:00
|
|
|
.inspect(|arr| {
|
2024-08-10 02:45:20 +02:00
|
|
|
for (idx, user) in users.iter().enumerate() {
|
2024-08-19 11:36:51 +02:00
|
|
|
user.id.jobjectify(&mut env)
|
2024-09-11 15:47:56 +02:00
|
|
|
.and_then(|id| env.set_object_array_element(arr, idx as i32, id))
|
2024-08-10 02:45:20 +02:00
|
|
|
.jexcept(&mut env);
|
|
|
|
}
|
|
|
|
}).jexcept(&mut env).as_raw()
|
2024-08-07 01:34:13 +02:00
|
|
|
}
|
|
|
|
|
2024-09-05 02:45:33 +02:00
|
|
|
/// Delete a buffer.
|
2024-08-07 01:34:13 +02:00
|
|
|
#[no_mangle]
|
|
|
|
pub extern "system" fn Java_mp_code_Workspace_delete_1buffer<'local>(
|
|
|
|
mut env: JNIEnv,
|
|
|
|
_class: JClass<'local>,
|
|
|
|
self_ptr: jlong,
|
2024-09-17 17:37:22 +02:00
|
|
|
path: JString<'local>,
|
2024-08-07 01:34:13 +02:00
|
|
|
) {
|
2024-09-17 17:37:22 +02:00
|
|
|
null_check!(env, path, {});
|
2024-08-09 14:11:13 +02:00
|
|
|
let workspace = unsafe { Box::leak(Box::from_raw(self_ptr as *mut Workspace)) };
|
2024-09-17 17:37:22 +02:00
|
|
|
let buffer = unsafe { env.get_string_unchecked(&path) }
|
2024-08-10 02:45:20 +02:00
|
|
|
.map(|buffer| buffer.to_string_lossy().to_string())
|
|
|
|
.jexcept(&mut env);
|
2024-09-16 00:20:03 +02:00
|
|
|
super::tokio().block_on(workspace.delete(&buffer))
|
2024-09-05 02:45:33 +02:00
|
|
|
.jexcept(&mut env);
|
2024-08-07 01:34:13 +02:00
|
|
|
}
|
2024-08-07 10:22:01 +02:00
|
|
|
|
2024-09-05 02:45:33 +02:00
|
|
|
/// Receive a workspace event if present.
|
2024-08-09 14:11:13 +02:00
|
|
|
#[no_mangle]
|
|
|
|
pub extern "system" fn Java_mp_code_Workspace_event(
|
|
|
|
mut env: JNIEnv,
|
|
|
|
_class: JClass,
|
|
|
|
self_ptr: jlong
|
|
|
|
) -> jobject {
|
|
|
|
let workspace = unsafe { Box::leak(Box::from_raw(self_ptr as *mut Workspace)) };
|
2024-09-17 17:37:22 +02:00
|
|
|
let event = super::tokio().block_on(workspace.event());
|
|
|
|
if let Ok(event) = event {
|
|
|
|
event.jobjectify(&mut env).jexcept(&mut env).as_raw()
|
|
|
|
} else {
|
|
|
|
handle_error!(&mut env, event, std::ptr::null_mut())
|
|
|
|
}
|
2024-08-09 14:11:13 +02:00
|
|
|
}
|
|
|
|
|
2024-08-08 00:29:54 +02:00
|
|
|
/// Called by the Java GC to drop a [Workspace].
|
|
|
|
#[no_mangle]
|
|
|
|
pub extern "system" fn Java_mp_code_Workspace_free(_env: JNIEnv, _class: JClass, input: jlong) {
|
|
|
|
let _ = unsafe { Box::from_raw(input as *mut Workspace) };
|
|
|
|
}
|