|
| 1 | +package net.kdt.pojavlaunch.utils; |
| 2 | + |
| 3 | +import android.content.Context; |
| 4 | +import android.os.Vibrator; |
| 5 | + |
| 6 | +import top.fifthlight.touchcontroller.proxy.client.LauncherProxyClient; |
| 7 | +import top.fifthlight.touchcontroller.proxy.client.MessageTransport; |
| 8 | +import top.fifthlight.touchcontroller.proxy.client.android.transport.UnixSocketTransportKt; |
| 9 | +import top.fifthlight.touchcontroller.proxy.message.VibrateMessage; |
| 10 | + |
| 11 | +import android.system.ErrnoException; |
| 12 | +import android.system.Os; |
| 13 | +import android.util.Log; |
| 14 | +import android.util.SparseIntArray; |
| 15 | +import android.view.MotionEvent; |
| 16 | +import android.view.View; |
| 17 | + |
| 18 | +import androidx.annotation.NonNull; |
| 19 | +import androidx.core.content.ContextCompat; |
| 20 | + |
| 21 | +import net.kdt.pojavlaunch.prefs.LauncherPreferences; |
| 22 | + |
| 23 | +public class TouchControllerUtils { |
| 24 | + private TouchControllerUtils() { |
| 25 | + } |
| 26 | + |
| 27 | + public static LauncherProxyClient proxyClient; |
| 28 | + private static final String socketName = "Amethyst"; |
| 29 | + |
| 30 | + private static class VibrationHandler implements LauncherProxyClient.VibrationHandler { |
| 31 | + private final Vibrator vibrator; |
| 32 | + |
| 33 | + public VibrationHandler(Vibrator vibrator) { |
| 34 | + this.vibrator = vibrator; |
| 35 | + } |
| 36 | + |
| 37 | + @Override |
| 38 | + @SuppressWarnings("DEPRECATION") |
| 39 | + public void vibrate(@NonNull VibrateMessage.Kind kind) { |
| 40 | + vibrator.vibrate(LauncherPreferences.PREF_TOUCHCONTROLLER_VIBRATE_LENGTH); |
| 41 | + } |
| 42 | + } |
| 43 | + |
| 44 | + private static final SparseIntArray pointerIdMap = new SparseIntArray(); |
| 45 | + private static int nextPointerId = 1; |
| 46 | + |
| 47 | + public static void processTouchEvent(MotionEvent motionEvent, View view) { |
| 48 | + if (proxyClient == null) { |
| 49 | + return; |
| 50 | + } |
| 51 | + int pointerId; |
| 52 | + switch (motionEvent.getActionMasked()) { |
| 53 | + case MotionEvent.ACTION_DOWN: |
| 54 | + pointerId = nextPointerId++; |
| 55 | + pointerIdMap.put(motionEvent.getPointerId(0), pointerId); |
| 56 | + proxyClient.addPointer(pointerId, motionEvent.getX(0) / view.getWidth(), motionEvent.getY(0) / view.getHeight()); |
| 57 | + break; |
| 58 | + case MotionEvent.ACTION_POINTER_DOWN: |
| 59 | + pointerId = nextPointerId++; |
| 60 | + int actionIndex = motionEvent.getActionIndex(); |
| 61 | + pointerIdMap.put(motionEvent.getPointerId(actionIndex), pointerId); |
| 62 | + proxyClient.addPointer(pointerId, motionEvent.getX(actionIndex) / view.getWidth(), motionEvent.getY(actionIndex) / view.getHeight()); |
| 63 | + break; |
| 64 | + case MotionEvent.ACTION_MOVE: |
| 65 | + for (int i = 0; i < motionEvent.getPointerCount(); i++) { |
| 66 | + pointerId = pointerIdMap.get(motionEvent.getPointerId(i)); |
| 67 | + if (pointerId == 0) { |
| 68 | + Log.d("TouchController", "Move pointerId is 0"); |
| 69 | + continue; |
| 70 | + } |
| 71 | + proxyClient.addPointer(pointerId, motionEvent.getX(i) / view.getWidth(), motionEvent.getY(i) / view.getHeight()); |
| 72 | + } |
| 73 | + break; |
| 74 | + case MotionEvent.ACTION_UP: |
| 75 | + case MotionEvent.ACTION_CANCEL: |
| 76 | + if (proxyClient != null) { |
| 77 | + proxyClient.clearPointer(); |
| 78 | + pointerIdMap.clear(); |
| 79 | + } |
| 80 | + break; |
| 81 | + case MotionEvent.ACTION_POINTER_UP: |
| 82 | + if (proxyClient != null) { |
| 83 | + int i = motionEvent.getActionIndex(); |
| 84 | + pointerId = pointerIdMap.get(motionEvent.getPointerId(i)); |
| 85 | + if (pointerId == 0) { |
| 86 | + Log.d("TouchController", "Pointer up pointerId is 0"); |
| 87 | + break; |
| 88 | + } |
| 89 | + pointerIdMap.delete(pointerId); |
| 90 | + proxyClient.removePointer(pointerId); |
| 91 | + } |
| 92 | + break; |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + public static void initialize(Context context) { |
| 97 | + if (proxyClient != null) { |
| 98 | + return; |
| 99 | + } |
| 100 | + try { |
| 101 | + Os.setenv("TOUCH_CONTROLLER_PROXY_SOCKET", socketName, true); |
| 102 | + } catch (ErrnoException e) { |
| 103 | + Log.w("TouchController", "Failed to set TouchController environment variable", e); |
| 104 | + } |
| 105 | + MessageTransport transport = UnixSocketTransportKt.UnixSocketTransport(socketName); |
| 106 | + proxyClient = new LauncherProxyClient(transport); |
| 107 | + proxyClient.run(); |
| 108 | + Vibrator vibrator = ContextCompat.getSystemService(context, Vibrator.class); |
| 109 | + if (vibrator != null) { |
| 110 | + LauncherProxyClient.VibrationHandler vibrationHandler = new VibrationHandler(vibrator); |
| 111 | + proxyClient.setVibrationHandler(vibrationHandler); |
| 112 | + } |
| 113 | + } |
| 114 | +} |
0 commit comments