feat: try_recv CursorController

Co-authored-by: alemi <me@alemi.dev>
This commit is contained in:
zaaarf 2024-08-07 00:37:58 +02:00
parent 73fe5eb023
commit 490670b8bc
No known key found for this signature in database
GPG key ID: 102E445F4C3F829B
5 changed files with 56 additions and 17 deletions

View file

@ -1,5 +1,6 @@
package mp.code;
import mp.code.data.Cursor;
import mp.code.exceptions.CodeMPLibException;
import java.util.Optional;
@ -49,11 +50,29 @@ public class Client {
}
// TODO - remove everything past this line
public static void main(String[] args) throws CodeMPLibException {
public static void main(String[] args) throws CodeMPLibException, InterruptedException {
Client c = new Client("http://alemi.dev:50053");
c.login(UUID.randomUUID().toString(), "lmaodefaultpassword", "glue");
c.joinWorkspace("glue");
System.out.println("Done!");
Workspace workspace = c.joinWorkspace("glue");
System.out.println(workspace.getWorkspaceId());
while(true) {
Cursor cursor = workspace.getCursor().tryRecv();
if(cursor == null) System.out.println("null!");
else {
System.out.printf(
"sr: %d, sc: %d, er: %d, ec: %d, cursor: %s, buffer: %s\n",
cursor.startRow,
cursor.startCol,
cursor.endRow,
cursor.endCol,
cursor.user,
cursor.buffer
);
}
Thread.sleep(100);
}
//System.out.println("Done!");
}
static {

View file

@ -10,9 +10,9 @@ public class CursorController {
this.ptr = ptr;
}
private static native Cursor recv(long self);
public Cursor recv() {
return recv(this.ptr);
private static native Cursor try_recv(long self);
public Cursor tryRecv() {
return try_recv(this.ptr);
}
private static native void send(long self, Cursor cursor);

View file

@ -1,6 +1,6 @@
use jni::{objects::{JClass, JObject}, sys::{jlong, jobject, jstring}, JNIEnv};
use crate::{api::Controller, buffer::Controller};
use crate::api::Controller;
use super::util::JExceptable;
@ -82,6 +82,6 @@ pub extern "system" fn Java_mp_code_BufferController_free<'local>(
_class: JClass<'local>,
self_ptr: jlong,
) {
unsafe { Box::from_raw(self_ptr as *mut crate::cursor::Controller) };
let _ = unsafe { Box::from_raw(self_ptr as *mut crate::cursor::Controller) };
}

View file

@ -1,4 +1,5 @@
use jni::{objects::{JClass, JObject}, sys::{jlong, jobject}, JNIEnv};
use jni::{objects::{JClass, JObject, JValueGen}, sys::{jlong, jobject}, JNIEnv};
use crate::{api::Controller, ffi::java::util::JExceptable};
/*
* Class: mp_code_CursorController
@ -6,13 +7,32 @@ use jni::{objects::{JClass, JObject}, sys::{jlong, jobject}, JNIEnv};
* Signature: (J)Lmp/code/data/Cursor;
*/
#[no_mangle]
pub extern "system" fn Java_mp_code_CursorController_recv<'local>(
pub extern "system" fn Java_mp_code_CursorController_try_1recv(
mut env: JNIEnv,
_class: JClass<'local>,
_class: JClass,
self_ptr: jlong,
) -> jobject {
todo!()
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()
}
}
}
// public Cursor(int startRow, int startCol, int endRow, int endCol, String buffer, String user) {
/*
* Class: mp_code_CursorController
@ -26,7 +46,7 @@ pub extern "system" fn Java_mp_code_CursorController_send<'local>(
self_ptr: jlong,
input: JObject<'local>,
) {
todo!()
}
/*
@ -40,5 +60,5 @@ pub extern "system" fn Java_mp_code_CursorController_free<'local>(
_class: JClass<'local>,
self_ptr: jlong,
) {
unsafe { Box::from_raw(self_ptr as *mut crate::cursor::Controller) };
let _ = unsafe { Box::from_raw(self_ptr as *mut crate::cursor::Controller) };
}

View file

@ -11,7 +11,7 @@ pub extern "system" fn Java_mp_code_Workspace_free(_env: JNIEnv, _class: JClass,
/// Gets the workspace id.
#[no_mangle]
pub extern "system" fn Java_mp_code_Client_get_1workspace_1id<'local>(
pub extern "system" fn Java_mp_code_Workspace_get_1workspace_1id<'local>(
env: JNIEnv<'local>,
_class: JClass<'local>,
self_ptr: jlong
@ -24,7 +24,7 @@ pub extern "system" fn Java_mp_code_Client_get_1workspace_1id<'local>(
/// Gets a cursor controller by name and returns a pointer to it.
#[no_mangle]
pub extern "system" fn Java_mp_code_Client_get_1cursor<'local>(
pub extern "system" fn Java_mp_code_Workspace_get_1cursor<'local>(
_env: JNIEnv<'local>,
_class: JClass<'local>,
self_ptr: jlong
@ -35,7 +35,7 @@ pub extern "system" fn Java_mp_code_Client_get_1cursor<'local>(
/// Gets a buffer controller by name and returns a pointer to it.
#[no_mangle]
pub extern "system" fn Java_mp_code_Client_get_1buffer<'local>(
pub extern "system" fn Java_mp_code_Workspace_get_1buffer<'local>(
env: JNIEnv<'local>,
_class: JClass<'local>,
self_ptr: jlong,