2024-08-06 23:30:00 +02:00
|
|
|
package mp.code;
|
|
|
|
|
|
|
|
import mp.code.data.Cursor;
|
2024-10-10 12:04:20 +02:00
|
|
|
import mp.code.data.Selection;
|
2024-09-05 02:45:33 +02:00
|
|
|
import mp.code.exceptions.ControllerException;
|
2024-08-06 23:30:00 +02:00
|
|
|
|
2024-08-07 10:22:01 +02:00
|
|
|
import java.util.Optional;
|
2024-09-17 23:16:39 +02:00
|
|
|
import java.util.function.Consumer;
|
2024-08-07 10:22:01 +02:00
|
|
|
|
2024-09-17 23:16:39 +02:00
|
|
|
/**
|
|
|
|
* Allows interaction with the CodeMP cursor position tracking system.
|
|
|
|
* <p>
|
|
|
|
* It is generally safer to avoid storing this directly, see the api notes for {@link Workspace}.
|
|
|
|
*/
|
|
|
|
public final class CursorController {
|
2024-08-06 23:30:00 +02:00
|
|
|
private final long ptr;
|
|
|
|
|
|
|
|
CursorController(long ptr) {
|
|
|
|
this.ptr = ptr;
|
2024-09-18 15:36:11 +02:00
|
|
|
Extensions.CLEANER.register(this, () -> free(ptr));
|
2024-08-06 23:30:00 +02:00
|
|
|
}
|
|
|
|
|
2024-09-05 02:45:33 +02:00
|
|
|
private static native Cursor try_recv(long self) throws ControllerException;
|
2024-09-17 23:16:39 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Tries to get a {@link Cursor} update from the queue if any were present, and returns
|
|
|
|
* an empty optional otherwise.
|
|
|
|
* @return the first cursor event in queue, if any are present
|
|
|
|
* @throws ControllerException if the controller was stopped
|
|
|
|
*/
|
2024-09-05 02:45:33 +02:00
|
|
|
public Optional<Cursor> tryRecv() throws ControllerException {
|
2024-08-07 10:22:01 +02:00
|
|
|
return Optional.ofNullable(try_recv(this.ptr));
|
|
|
|
}
|
|
|
|
|
2024-09-05 02:45:33 +02:00
|
|
|
private static native Cursor recv(long self) throws ControllerException;
|
2024-09-17 23:16:39 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Blocks until a {@link Cursor} update is available and returns it.
|
|
|
|
* @return the cursor update that occurred
|
|
|
|
* @throws ControllerException if the controller was stopped
|
|
|
|
*/
|
2024-09-05 02:45:33 +02:00
|
|
|
public Cursor recv() throws ControllerException {
|
2024-08-07 10:22:01 +02:00
|
|
|
return recv(this.ptr);
|
2024-08-06 23:30:00 +02:00
|
|
|
}
|
|
|
|
|
2024-10-10 12:04:20 +02:00
|
|
|
private static native void send(long self, Selection cursor) throws ControllerException;
|
2024-09-17 23:16:39 +02:00
|
|
|
|
|
|
|
/**
|
2024-10-10 12:04:20 +02:00
|
|
|
* Tries to send a {@link Selection} update.
|
2024-09-17 23:16:39 +02:00
|
|
|
* @throws ControllerException if the controller was stopped
|
|
|
|
*/
|
2024-10-10 12:04:20 +02:00
|
|
|
public void send(Selection cursor) throws ControllerException {
|
2024-09-17 23:16:39 +02:00
|
|
|
send(this.ptr, cursor);
|
2024-08-06 23:30:00 +02:00
|
|
|
}
|
|
|
|
|
2024-09-17 23:16:39 +02:00
|
|
|
private static native void callback(long self, Consumer<CursorController> cb);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Registers a callback to be invoked whenever a {@link Cursor} update occurs.
|
|
|
|
* This will not work unless a Java thread has been dedicated to the event loop.
|
|
|
|
* @see Extensions#drive(boolean)
|
|
|
|
*/
|
|
|
|
public void callback(Consumer<CursorController> cb) {
|
|
|
|
callback(this.ptr, cb);
|
2024-09-15 02:00:04 +02:00
|
|
|
}
|
|
|
|
|
2024-09-17 02:40:03 +02:00
|
|
|
private static native void clear_callback(long self);
|
2024-09-17 23:16:39 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Clears the registered callback.
|
|
|
|
* @see #callback(Consumer)
|
|
|
|
*/
|
2024-09-17 02:40:03 +02:00
|
|
|
public void clearCallback() {
|
|
|
|
clear_callback(this.ptr);
|
|
|
|
}
|
|
|
|
|
2024-09-17 23:16:39 +02:00
|
|
|
private static native void poll(long self) throws ControllerException;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Blocks until a {@link Cursor} update is available.
|
|
|
|
* @throws ControllerException if the controller was stopped
|
|
|
|
*/
|
|
|
|
public void poll() throws ControllerException {
|
2024-09-17 02:40:03 +02:00
|
|
|
poll(this.ptr);
|
|
|
|
}
|
|
|
|
|
2024-08-06 23:30:00 +02:00
|
|
|
private static native void free(long self);
|
2024-09-17 02:40:03 +02:00
|
|
|
|
|
|
|
static {
|
2024-09-18 14:54:54 +02:00
|
|
|
NativeUtils.loadLibraryIfNeeded();
|
2024-09-17 02:40:03 +02:00
|
|
|
}
|
2024-08-06 23:30:00 +02:00
|
|
|
}
|