Terminal feature; raw screenshots.

This commit is contained in:
James Babcock committed 2026-07-04 17:38:53 -07:00
1 parent d2a942f5a3
commit fab6f6689e
19 files changed
+1075 -10

No files matched your search

+2
View File
@@ -3,6 +3,8 @@ import org.apache.tools.ant.taskdefs.condition.Os
// You can add your native dependencies here
dependencies {
// implementation 'androidx.multidex:multidex:2.0.1'
// WebSocket transport for the g2mirror terminal-mirroring client.
implementation 'com.squareup.okhttp3:okhttp:4.12.0'
}
def faceclawSherpaOnnxVersion = "1.13.0"
@@ -30,12 +30,16 @@
NameNotFoundException for any package we cannot see. -->
<uses-permission android:name="android.permission.QUERY_ALL_PACKAGES"/>
<!-- usesCleartextTraffic: the g2mirror terminal-mirroring protocol runs
plain ws:// over tailscale (transport security is the network's job),
which Android's default policy would otherwise block. -->
<application
android:name="com.tns.NativeScriptApplication"
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme"
android:usesCleartextTraffic="true"
android:hardwareAccelerated="true">
<activity
@@ -0,0 +1,116 @@
package com.faceclaw.app;
import android.os.Handler;
import android.os.Looper;
import android.util.Log;
import java.util.concurrent.TimeUnit;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import okhttp3.WebSocket;
import okhttp3.WebSocketListener;
/**
* Thin okhttp WebSocket wrapper for the TypeScript side. Text frames only
* (the g2mirror protocol is JSON-in-text-frames). All listener callbacks are
* posted to the main thread, where the NativeScript runtime expects them.
*/
public class FaceclawWebSocket {
private static final String TAG = "FaceclawWebSocket";
private static final Handler mainHandler = new Handler(Looper.getMainLooper());
private static volatile OkHttpClient sharedClient;
private final WebSocket socket;
private volatile boolean closeRequested;
public FaceclawWebSocket(String url, final FaceclawWebSocketListener listener) {
if (url == null || url.trim().isEmpty()) {
throw new IllegalArgumentException("url is required");
}
if (listener == null) {
throw new IllegalArgumentException("listener is required");
}
Request request = new Request.Builder().url(url.trim()).build();
socket = getClient().newWebSocket(request, new WebSocketListener() {
@Override public void onOpen(WebSocket webSocket, Response response) {
mainHandler.post(() -> {
try {
listener.onOpen();
} catch (Throwable t) {
Log.w(TAG, "listener onOpen failed", t);
}
});
}
@Override public void onMessage(WebSocket webSocket, String text) {
mainHandler.post(() -> {
try {
listener.onTextMessage(text);
} catch (Throwable t) {
Log.w(TAG, "listener onTextMessage failed", t);
}
});
}
@Override public void onClosing(WebSocket webSocket, int code, String reason) {
webSocket.close(code, reason);
}
@Override public void onClosed(WebSocket webSocket, int code, String reason) {
mainHandler.post(() -> {
try {
listener.onClosed(code, reason == null ? "" : reason);
} catch (Throwable t) {
Log.w(TAG, "listener onClosed failed", t);
}
});
}
@Override public void onFailure(WebSocket webSocket, Throwable t, Response response) {
if (closeRequested) {
return;
}
final String message = t == null ? "unknown websocket failure" : String.valueOf(t);
mainHandler.post(() -> {
try {
listener.onFailure(message);
} catch (Throwable inner) {
Log.w(TAG, "listener onFailure failed", inner);
}
});
}
});
}
private static OkHttpClient getClient() {
OkHttpClient client = sharedClient;
if (client == null) {
synchronized (FaceclawWebSocket.class) {
if (sharedClient == null) {
sharedClient = new OkHttpClient.Builder()
.pingInterval(20, TimeUnit.SECONDS)
.build();
}
client = sharedClient;
}
}
return client;
}
public boolean sendText(String message) {
return socket.send(message == null ? "" : message);
}
public void close(int code, String reason) {
closeRequested = true;
try {
if (!socket.close(code, reason)) {
socket.cancel();
}
} catch (Throwable t) {
socket.cancel();
}
}
}
@@ -0,0 +1,9 @@
package com.faceclaw.app;
/** Callbacks for FaceclawWebSocket, delivered on the Android main thread. */
public interface FaceclawWebSocketListener {
void onOpen();
void onTextMessage(String message);
void onClosed(int code, String reason);
void onFailure(String message);
}
@@ -0,0 +1,44 @@
package com.faceclaw.app;
import android.content.Context;
import java.io.File;
import java.io.FileOutputStream;
import java.nio.ByteBuffer;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
/**
* Saves raw screenshots for compression experiments. The output is the exact
* headerless top-down 4bpp frame buffer that gets zlib-compressed on the wire
* (build4bppBmp followed by pack4bppFromBmp), deliberately untransformed — no
* resize, no gamma, no header.
*/
public final class ScreenshotUtil {
private ScreenshotUtil() {}
/** Returns the absolute path of the written file. */
public static String saveRawScreenshot(Context context, ByteBuffer image8bpp, int width, int height) throws java.io.IOException {
if (image8bpp == null || width <= 0 || height <= 0 || image8bpp.remaining() < width * height) {
throw new IllegalArgumentException("invalid screenshot buffer");
}
byte[] gray = new byte[image8bpp.remaining()];
image8bpp.get(gray);
byte[] packed = BmpUtil.pack4bppFromBmp(BmpUtil.build4bppBmp(gray, width, height));
if (packed.length == 0) {
throw new IllegalStateException("failed to pack 4bpp frame");
}
File dir = new File(context.getExternalFilesDir(null), "screenshots");
if (!dir.exists() && !dir.mkdirs()) {
throw new java.io.IOException("failed to create " + dir);
}
String stamp = new SimpleDateFormat("yyyyMMdd-HHmmss-SSS", Locale.US).format(new Date());
File file = new File(dir, "screen-" + stamp + "-" + width + "x" + height + ".4bpp");
try (FileOutputStream out = new FileOutputStream(file)) {
out.write(packed);
}
return file.getAbsolutePath();
}
}