diff --git a/app/src/main/java/com/openterface/AOS/activity/MainActivity.java b/app/src/main/java/com/openterface/AOS/activity/MainActivity.java index bb3a34e..caf8e94 100644 --- a/app/src/main/java/com/openterface/AOS/activity/MainActivity.java +++ b/app/src/main/java/com/openterface/AOS/activity/MainActivity.java @@ -43,6 +43,9 @@ import android.content.res.Configuration; import android.os.Bundle; import android.os.IBinder; +import android.os.Looper; +import android.view.InputDevice; +import com.openterface.AOS.serial.BtMouseInputHandler; import androidx.annotation.NonNull; import androidx.annotation.Nullable; @@ -84,6 +87,7 @@ import com.openterface.AOS.serial.CustomTouchListener; import com.openterface.AOS.serial.CH9329Function; import com.openterface.AOS.serial.UsbDeviceManager; +import com.openterface.AOS.serial.CoordinateConverter; import com.openterface.AOS.target.CH9329MSKBMap; import com.openterface.AOS.target.HidManager; import com.openterface.AOS.target.KeyBoardManager; @@ -356,6 +360,17 @@ public enum ModuleType { NONE, KEYBOARD, MOUSE, IME, SETTINGS } public AspectRatioSurfaceView cameraViewSecond; private RelativeLayout thumbnail_container; + /** + * Owns the landscape drawer/PiP controller. It is created once per activity and only + * referenced from here, so it dies with the activity instead of leaking through a + * static field. + */ + private ZoomLayoutDeal zoomLayoutDeal; + + public ZoomLayoutDeal getZoomLayoutDeal() { + return zoomLayoutDeal; + } + @SuppressLint("ClickableViewAccessibility")//add @Override protected void onCreate(Bundle savedInstanceState) { @@ -375,6 +390,14 @@ protected void onCreate(Bundle savedInstanceState) { mBinding = ActivityMainBinding.inflate(getLayoutInflater()); setContentView(mBinding.getRoot()); + // Ensure Activity can receive focus for hardware keyboard events + getWindow().getDecorView().setFocusable(true); + getWindow().getDecorView().setFocusableInTouchMode(true); + getWindow().getDecorView().requestFocus(); + + // Detect connected input devices (for debugging) + detectInputDevices(); + checkCameraHelper(); setListeners(); @@ -393,9 +416,14 @@ protected void onCreate(Bundle savedInstanceState) { // Initialize the mouse event worker thread HidManager.initMouse(); - // Load saved mouse speed multiplier (default 50 → 1.0x) - int initialMouseSpeed = getSharedPreferences(PREF_SETTINGS, MODE_PRIVATE).getInt(KEY_MOUSE_SPEED, 50); - HidManager.setMouseSpeedMultiplier(progressToMouseSpeed(initialMouseSpeed)); + // Initialize mouse mode at startup using MouseModeManager + com.openterface.AOS.target.MouseMode savedMode = + com.openterface.AOS.utils.MouseModePreferences.loadMode(this); + boolean trackpadSensitivity = + com.openterface.AOS.utils.MouseModePreferences.loadTrackpadSensitivity(this); + com.openterface.AOS.target.MouseModeManager.setMode(savedMode); + com.openterface.AOS.target.MouseModeManager.setTrackpadSensitivity(trackpadSensitivity); + Log.d(TAG, "Mouse mode initialized: " + savedMode + ", trackpad sensitivity: " + trackpadSensitivity); // Initialize VNC server components vncConfig = new VncServerConfig(this); @@ -419,6 +447,26 @@ protected void onCreate(Bundle savedInstanceState) { mBinding.viewMainPreview.setOnTouchListener(mCustomTouchListener); + // Portrait preview is a centered aspect-ratio band, so pinches landing on the + // black area around it need their own zoom handler (no-op in landscape layout) + CustomTouchListener.attachVideoAreaZoomFallback( + findViewById(R.id.video_area_container)); + + // Register BT mouse connection listener to hide/show cursor + CustomTouchListener.setOnBtMouseConnectionListener(new CustomTouchListener.OnBtMouseConnectionListener() { + @Override + public void onBtMouseConnected(boolean connected) { + runOnUiThread(() -> { + // When reconnecting, explicitly cancel any pending cursor restore + // and re-hide cursor to prevent flash from system auto-restore + if (connected) { + CustomTouchListener.cancelBtMouseCursorRestore(); + } + applyBtMousePointerCapture(connected); + }); + } + }); + // Setting up the gesture detector gestureDetector = new GestureDetector(this, new GestureListener()); @@ -529,7 +577,7 @@ public void onDataRead(byte[] data, int length) { //Zoom layout deal setCameraViewSecond(); - ZoomLayoutDeal ZoomLayoutDeal = new ZoomLayoutDeal(this, mCameraHelper, mBinding); + zoomLayoutDeal = new ZoomLayoutDeal(this, mCameraHelper, mBinding); HidManager.setKeyBoardLanguage(); @@ -688,11 +736,221 @@ public void surfaceDestroyed(@NonNull SurfaceHolder holder) { } + /** + * Detect connected input devices for debugging + */ + private void detectInputDevices() { + int[] deviceIds = InputDevice.getDeviceIds(); + Log.d(TAG, "=== Connected Input Devices ==="); + for (int deviceId : deviceIds) { + InputDevice device = InputDevice.getDevice(deviceId); + if (device != null) { + boolean isKeyboard = (device.getSources() & InputDevice.SOURCE_KEYBOARD) != 0; + Log.d(TAG, "Device: " + device.getName() + + ", sources: " + device.getSources() + + ", isKeyboard: " + isKeyboard + + ", vendorId: " + device.getVendorId() + + ", productId: " + device.getProductId()); + } + } + Log.d(TAG, "=== End Input Devices ==="); + } + + // Key state tracking for hardware keyboard + private int mLastHardwareKeyCode = 0; + private boolean mHardwareKeyPressed = false; // Track if key is currently pressed + + @Override + public boolean dispatchKeyEvent(KeyEvent event) { + // Check if this is a hardware keyboard or mouse (Bluetooth, USB, etc.) + if (event.getDevice() != null) { + int sources = event.getDevice().getSources(); + + // ---- Hardware Keyboard ---- + if ((sources & InputDevice.SOURCE_KEYBOARD) != 0) { + int keyCode = event.getKeyCode(); + + if (event.getAction() == KeyEvent.ACTION_DOWN) { + // Only process if this is the first press (not auto-repeat) + // and the same key is not already pressed + if (event.getRepeatCount() == 0 && (!mHardwareKeyPressed || keyCode != mLastHardwareKeyCode)) { + mLastHardwareKeyCode = keyCode; + mHardwareKeyPressed = true; + Log.d(TAG, "Hardware key down: " + keyCode + " (" + KeyEvent.keyCodeToString(keyCode) + ")"); + return handleHardwareKeyDown(event); + } else { + // Auto-repeat from the keyboard. The target already repeats on + // its own because the key stays down, so there is nothing to send. + return true; + } + } else if (event.getAction() == KeyEvent.ACTION_UP) { + if (keyCode == mLastHardwareKeyCode) { + mHardwareKeyPressed = false; + } + return handleHardwareKeyUp(event); + } + return true; + } + + // ---- Bluetooth Mouse side buttons (BACK / FORWARD) ---- + if ((sources & InputDevice.SOURCE_MOUSE) != 0) { + int keyCode = event.getKeyCode(); + if (keyCode == KeyEvent.KEYCODE_BACK || keyCode == KeyEvent.KEYCODE_FORWARD) { + if (event.getAction() == KeyEvent.ACTION_DOWN && event.getRepeatCount() == 0) { + Log.d(TAG, "BT Mouse side button DOWN: " + KeyEvent.keyCodeToString(keyCode)); + // Map BACK → mouse button 4 (browser back), FORWARD → button 5 + String buttonKey = (keyCode == KeyEvent.KEYCODE_BACK) ? "SecBackData" : "SecForwardData"; + try { + com.openterface.AOS.target.HidManager.sendHexRelData(buttonKey, 0, 0, 0, 0); + } catch (Exception e) { + Log.e(TAG, "BT Mouse side button error: " + e.getMessage()); + } + return true; + } else if (event.getAction() == KeyEvent.ACTION_UP) { + Log.d(TAG, "BT Mouse side button UP: " + KeyEvent.keyCodeToString(keyCode)); + try { + com.openterface.AOS.target.HidManager.releaseMSRelData(); + } catch (Exception e) { + Log.e(TAG, "BT Mouse side button release error: " + e.getMessage()); + } + return true; + } + } + } + } + + // Non-hardware keyboard events, let parent handle + return super.dispatchKeyEvent(event); + } + + /** + * Handle hardware keyboard key down event. + * Sends a press only. The target OS generates its own auto-repeat while the + * key stays down, which is what makes holding a key repeat. The matching + * release is sent from handleHardwareKeyUp(). + */ + private boolean handleHardwareKeyDown(KeyEvent event) { + int keyCode = event.getKeyCode(); + + // Ctrl/Shift/Alt/Meta have no key slot of their own: their state travels in + // the modifier byte of the next real key press. Sending one on its own puts + // E0/E1/E2 into the key field, and the target then sees the following key + // arrive while that bogus key is still held, so it drops it. + if (isModifierOnlyKey(keyCode)) { + return true; + } + + // Use HidManager path (same as on-screen keyboard buttons) + String functionKey = HidManager.getFunctionKey(event, keyCode); + String keyName = HidManager.getKeyName(keyCode); + + if (keyName != null && usbDeviceManager != null) { + HidManager.sendKeyBoardPressQueued(functionKey, keyName); + return true; + } + Log.w(TAG, "Cannot send key - keyName: " + keyName + + ", usbDeviceManager: " + (usbDeviceManager != null)); + + return true; + } + + /** + * Modifier keys only contribute to the modifier byte of other keys. + */ + private static boolean isModifierOnlyKey(int keyCode) { + switch (keyCode) { + case KeyEvent.KEYCODE_CTRL_LEFT: + case KeyEvent.KEYCODE_CTRL_RIGHT: + case KeyEvent.KEYCODE_SHIFT_LEFT: + case KeyEvent.KEYCODE_SHIFT_RIGHT: + case KeyEvent.KEYCODE_ALT_LEFT: + case KeyEvent.KEYCODE_ALT_RIGHT: + case KeyEvent.KEYCODE_META_LEFT: + case KeyEvent.KEYCODE_META_RIGHT: + return true; + default: + return false; + } + } + + /** + * Handle hardware keyboard key up event. + * Releases every key, pairing with the press sent on ACTION_DOWN. + */ + private boolean handleHardwareKeyUp(KeyEvent event) { + HidManager.sendKeyBoardReleaseQueued(); + return true; + } + @Override public boolean onGenericMotionEvent(MotionEvent event) { return CustomTouchListener.handleGenericMotionEvent(event) || super.onGenericMotionEvent(event); } + // Catch BT mouse events at dispatch level (before view consumes them) + @Override + public boolean dispatchGenericMotionEvent(MotionEvent event) { + int source = event.getSource(); + boolean isMouse = (source & android.view.InputDevice.SOURCE_MOUSE) != 0; + boolean isTouchpad = (source & android.view.InputDevice.SOURCE_TOUCHPAD) != 0; + boolean isTouchscreen = (source & android.view.InputDevice.SOURCE_TOUCHSCREEN) != 0; + int toolType = event.getPointerCount() > 0 ? event.getToolType(0) : -1; + + // Log ALL motion events to diagnose event routing (throttle via action filter) + if (isMouse || isTouchpad) { + Log.d(TAG, "dispatchGenericMotionEvent: isMouse=" + isMouse + " isTouchpad=" + isTouchpad + + " action=" + event.getAction() + + " source=0x" + Integer.toHexString(source) + + " toolType=" + toolType + + " pointerCount=" + event.getPointerCount()); + } + + if (isMouse) { + if (CustomTouchListener.handleGenericMotionEvent(event)) { + return true; + } + } + return super.dispatchGenericMotionEvent(event); + } + + // Capture touch events during BT mouse button hold + // Android may send move events as ACTION_MOVE (touch events) when button is pressed + @Override + public boolean dispatchTouchEvent(MotionEvent event) { + int source = event.getSource(); + int toolType = event.getPointerCount() > 0 ? event.getToolType(0) : -1; + int action = event.getActionMasked(); + boolean isMouseTool = (toolType == MotionEvent.TOOL_TYPE_MOUSE); + + // Log ALL touch events when BT mouse is connected or button is pressed + if (isMouseTool || BtMouseInputHandler.isBtMouseConnected()) { + Log.d(TAG, "dispatchTouchEvent: toolType=" + toolType + + " action=" + action + + " source=0x" + Integer.toHexString(source) + + " x=" + event.getX() + " y=" + event.getY() + + " buttonState=" + event.getButtonState() + + " btMouseButtonPressed=" + BtMouseInputHandler.isBtMouseButtonPressed()); + } + + // Forward mouse-tool touch events to BT mouse handler during drag + // Some Android versions send BT mouse drag moves as ACTION_MOVE with TOOL_TYPE_MOUSE + // instead of ACTION_HOVER_MOVE generic motion events + if (isMouseTool && BtMouseInputHandler.isBtMouseButtonPressed() + && action == MotionEvent.ACTION_MOVE) { + if (CustomTouchListener.handleGenericMotionEvent(event)) { + return true; + } + } + + return super.dispatchTouchEvent(event); + } + + // API 31+: Captured pointer events from BT mouse (system cursor hidden) + public boolean onCapturedPointerEvent(MotionEvent event) { + Log.d(TAG, "onCapturedPointerEvent: action=" + event.getAction() + " source=0x" + Integer.toHexString(event.getSource())); + return CustomTouchListener.handleGenericMotionEvent(event); + } + @Override public boolean onKeyMultiple(int keyCode, int count, KeyEvent event) { String functionKey = HidManager.getFunctionKey(event, keyCode); @@ -871,12 +1129,21 @@ protected void onNewIntent(Intent intent) { protected void onStart() { super.onStart(); + // Request focus to ensure Activity can receive hardware keyboard events + getWindow().getDecorView().requestFocus(); + initPreviewView(); } @Override protected void onStop() { super.onStop(); + // Focus can be lost before ACTION_UP arrives. Drop any held key so the + // target does not keep repeating while the app sits in the background. + HidManager.sendKeyBoardReleaseQueued(); + // A locked keycap keeps a mouse button down in the host for the same reason. + HidManager.unlockAll(); + HidManager.releaseMSRelData(); if (mIsRecording) { toggleVideoRecord(false); } @@ -902,6 +1169,9 @@ protected void onDestroy() { // Release the mouse event worker thread HidManager.releaseMouse(); + // Stop the keycap frame queue + mouseStripExecutor.shutdownNow(); + // Cleanup debug broadcast receiver if (debugReceiver != null) { unregisterReceiver(debugReceiver); @@ -923,6 +1193,11 @@ public void onConfigurationChanged(Configuration newConfig) { Log.d(TAG, "Configuration changed: " + (isPortraitMode ? "PORTRAIT" : "LANDSCAPE")); + // The layout is rebuilt below, so the new keycaps are all unlocked. Release the + // buttons the old ones kept pressed, otherwise every later move is a drag. + HidManager.unlockAll(); + HidManager.releaseMSRelData(); + // Reload layout for new orientation reloadLayoutForOrientation(); @@ -936,6 +1211,9 @@ public void onConfigurationChanged(Configuration newConfig) { resizePreviewView(mCameraHelper.getPreviewSize()); } + // Notify CustomTouchListener about configuration change (for black border overlay) + CustomTouchListener.onConfigurationChanged(); + Log.d(TAG, "Layout reloaded, module state: " + currentModule); } @@ -984,6 +1262,14 @@ private void reloadLayoutForOrientation() { // Re-attach CustomTouchListener to the new preview view after layout re-inflation mCustomTouchListener = new CustomTouchListener(this, usbDeviceManager); mBinding.viewMainPreview.setOnTouchListener(mCustomTouchListener); + CustomTouchListener.attachVideoAreaZoomFallback( + findViewById(R.id.video_area_container)); + + // Update black border overlay for new orientation + // This will show overlay if BT mouse is connected, and update video rect + if (mBinding.blackBorderOverlay != null) { + CustomTouchListener.updateBlackBorderOverlayVisibility(); + } // Re-setup DrawerLayoutDeal for new layout (only in landscape) if (!nowPortrait) { @@ -996,9 +1282,11 @@ private void reloadLayoutForOrientation() { Log.w(TAG, "reloadLayoutForOrientation: DrawerLayoutDeal failed (expected in portrait): " + e.getMessage()); } } + // Replace the controller of the previous layout: it still points at the views of + // the tree that was just thrown away. + zoomLayoutDeal = null; try { - com.openterface.AOS.drawerLayout.ZoomLayoutDeal zoomLayoutDeal = - new com.openterface.AOS.drawerLayout.ZoomLayoutDeal(this, mCameraHelper, mBinding); + zoomLayoutDeal = new com.openterface.AOS.drawerLayout.ZoomLayoutDeal(this, mCameraHelper, mBinding); Log.d(TAG, "reloadLayoutForOrientation: ZoomLayoutDeal created"); } catch (Exception e) { Log.w(TAG, "reloadLayoutForOrientation: ZoomLayoutDeal failed: " + e.getMessage()); @@ -1070,6 +1358,14 @@ private void reloadLayoutForOrientation() { // Update UI controls to reflect current state updateUIControls(); + + // Re-install the captured-pointer hook after layout reload (BT mouse state persists + // across rotation) + if (com.openterface.AOS.serial.CustomTouchListener.isBtMouseConnected()) { + applyBtMousePointerCapture(true); + Log.d(TAG, "reloadLayoutForOrientation: BT mouse pointer capture re-applied"); + } + Log.d(TAG, "reloadLayoutForOrientation: COMPLETE"); } @@ -1116,7 +1412,9 @@ private void setListeners() { @Override public void onGlobalLayout() { Log.d(TAG,"keyBoardView.getWidth():"+keyBoardView.getWidth() + "keyBoardView.getHeight():"+keyBoardView.getHeight()); - ZoomLayoutDeal.getViewWidthHeight(keyBoardView.getWidth(), keyBoardView.getHeight()); + if (zoomLayoutDeal != null) { + zoomLayoutDeal.getViewWidthHeight(keyBoardView.getWidth(), keyBoardView.getHeight()); + } } }); keyBoardView.setVisibility(View.VISIBLE); @@ -1289,23 +1587,52 @@ private void initPreviewView() { Log.w(TAG, "initPreviewView: mBinding or viewMainPreview is null, skipping"); return; } - // Portrait mode: disable aspect ratio constraint, TextureView fills container, Matrix handles display transform - // Landscape mode: keep original aspect ratio constraint behavior - if (!isPortraitMode) { - mBinding.viewMainPreview.setAspectRatio(mPreviewWidth, mPreviewHeight); - } else { - mBinding.viewMainPreview.setAspectRatioEnabled(false); + // Both portrait and landscape: set aspect ratio to match camera buffer. + // Portrait mode: view will be centered with black bars top/bottom (landscape aspect in portrait screen) + // Landscape mode: view will fill width with correct height + mBinding.viewMainPreview.setAspectRatio(mPreviewWidth, mPreviewHeight); + if (isPortraitMode) { + // In portrait, ensure TextureView uses MATCH_PARENT width so aspect ratio calculates from width ViewGroup.LayoutParams lp = mBinding.viewMainPreview.getLayoutParams(); lp.width = ViewGroup.LayoutParams.MATCH_PARENT; - lp.height = ViewGroup.LayoutParams.MATCH_PARENT; + lp.height = ViewGroup.LayoutParams.WRAP_CONTENT; mBinding.viewMainPreview.setLayoutParams(lp); } Log.d(TAG, "initPreviewView: mPreviewWidth=" + mPreviewWidth + " mPreviewHeight=" + mPreviewHeight + " isPortrait=" + isPortraitMode); + + // Add layout listener to update HID mapping when the view size changes + mBinding.viewMainPreview.addOnLayoutChangeListener((v, left, top, right, bottom, oldLeft, oldTop, oldRight, oldBottom) -> { + if (left != oldLeft || top != oldTop || right != oldRight || bottom != oldBottom) { + // CRITICAL: Update HID coordinate mapping with actual displayed dimensions + // NOT the SurfaceTexture buffer dimensions (which are video resolution) + int viewWidth = right - left; + int viewHeight = bottom - top; + if (viewWidth > 0 && viewHeight > 0) { + HidManager.width_height(viewWidth, viewHeight); + Log.d(TAG, "Layout changed: updating HID dimensions to " + viewWidth + "x" + viewHeight + + " (was buffer size, now actual display size)"); + } + } + }); + mBinding.viewMainPreview.setSurfaceTextureListener(new TextureView.SurfaceTextureListener() { @Override public void onSurfaceTextureAvailable(@NonNull SurfaceTexture surface, int width, int height) { - HidManager.width_height(width, height); - Log.d(TAG, "onSurfaceTextureAvailable: width=" + width + " height=" + height + " mIsCameraConnected=" + mIsCameraConnected + " mMainSurfaceAdded=" + mMainSurfaceAdded); + // Use actual TextureView display dimensions for HID coordinate mapping, + // NOT the SurfaceTexture buffer dimensions (which are video resolution like 1920x1080) + // In portrait mode, TextureView displays at ~1080x607, not 1920x1080 + int viewWidth = mBinding.viewMainPreview.getWidth(); + int viewHeight = mBinding.viewMainPreview.getHeight(); + if (viewWidth > 0 && viewHeight > 0) { + HidManager.width_height(viewWidth, viewHeight); + Log.d(TAG, "onSurfaceTextureAvailable: using TextureView display dimensions " + viewWidth + "x" + viewHeight + + " (buffer=" + width + "x" + height + ")"); + } else { + // View not laid out yet, OnLayoutChangeListener will update dimensions + HidManager.width_height(width, height); + Log.d(TAG, "onSurfaceTextureAvailable: TextureView not laid out yet, using buffer " + width + "x" + height + + " (will update on layout)"); + } if (mCameraHelper != null && mIsCameraConnected && !mMainSurfaceAdded) { // Camera is already connected but surface hasn't been added yet. // This happens when app launches in portrait and onCameraOpen fired @@ -1328,10 +1655,8 @@ public void onSurfaceTextureAvailable(@NonNull SurfaceTexture surface, int width // fires AFTER this method returns. Setting the buffer here ensures the Surface // is created with the correct camera resolution. setSurfaceTextureBufferSize(surface, mPreviewWidth, mPreviewHeight); - // Portrait mode: skip setAspectRatio, Matrix handles display transform - if (!isPortraitMode) { - mBinding.viewMainPreview.setAspectRatio(mPreviewWidth, mPreviewHeight); - } + // Set aspect ratio for correct proportions in both modes + mBinding.viewMainPreview.setAspectRatio(mPreviewWidth, mPreviewHeight); // Create Surface from SurfaceTexture android.view.Surface previewSurface = new android.view.Surface(surface); @@ -1370,10 +1695,8 @@ public void onSurfaceTextureAvailable(@NonNull SurfaceTexture surface, int width // Set buffer size BEFORE creating Surface - ensures Surface uses camera resolution setSurfaceTextureBufferSize(surface, mPreviewWidth, mPreviewHeight); - // Portrait mode: skip setAspectRatio, Matrix handles display transform - if (!isPortraitMode) { - mBinding.viewMainPreview.setAspectRatio(mPreviewWidth, mPreviewHeight); - } + // Set aspect ratio for correct proportions in both modes + mBinding.viewMainPreview.setAspectRatio(mPreviewWidth, mPreviewHeight); android.view.Surface previewSurface = new android.view.Surface(surface); // Use RendererHolder pipeline to enable frame distribution to multiple surfaces (including PIP) mCameraHelper.addSurface(previewSurface, false); @@ -1431,6 +1754,8 @@ public void onSurfaceTextureSizeChanged(@NonNull SurfaceTexture surface, int wid // Still need to set buffer size to camera native resolution to ensure clarity when zoomed. Log.d(TAG, "onSurfaceTextureSizeChanged: portrait mode, updating buffer size without surface recreation"); setSurfaceTextureBufferSize(surface, mPreviewWidth, mPreviewHeight); + // Reset learned cursor range since TextureView size changed + CoordinateConverter.resetLearnedRange(); // Update Matrix to recalculate stretch correction if (mCustomTouchListener != null) { mBinding.viewMainPreview.post(() -> mCustomTouchListener.applyPortraitZoomTransform()); @@ -1685,10 +2010,8 @@ public void onCameraOpen(UsbDevice device) { } // Step 2: Set aspect ratio on the view (controls how the view is measured) - // Portrait mode: skip setAspectRatio, Matrix handles display transform - if (!isPortraitMode) { - mBinding.viewMainPreview.setAspectRatio(mPreviewWidth, mPreviewHeight); - } + // Both modes: use aspect ratio to ensure correct proportions + mBinding.viewMainPreview.setAspectRatio(mPreviewWidth, mPreviewHeight); // Step 3: Set SurfaceTexture buffer size to camera resolution BEFORE creating // the Surface. This is the critical step - the buffer size is read by @@ -1781,7 +2104,9 @@ public void onCameraClose(UsbDevice device) { closeAllDialogFragment(); - ZoomLayoutDeal.zoomOut(); + if (zoomLayoutDeal != null) { + zoomLayoutDeal.zoomOut(); + } } @Override @@ -1841,10 +2166,8 @@ private void resizePreviewView(Size size) { } Log.d(TAG, "resizePreviewView: " + mPreviewWidth + "x" + mPreviewHeight + " (mMainSurfaceAdded=" + mMainSurfaceAdded + " isPortrait=" + isPortraitMode + ")"); // Set the aspect ratio of TextureView to match the aspect ratio of the camera - // Portrait mode: skip setAspectRatio, Matrix handles display transform - if (!isPortraitMode) { - mBinding.viewMainPreview.setAspectRatio(mPreviewWidth, mPreviewHeight); - } + // Both modes: use aspect ratio to ensure correct proportions + mBinding.viewMainPreview.setAspectRatio(mPreviewWidth, mPreviewHeight); // Update SurfaceTexture buffer to chip resolution for better zoom quality. // IMPORTANT: Only update buffer size if no Surface has been created from this // SurfaceTexture yet. Once a Surface is created (mMainSurfaceAdded=true), the @@ -2335,11 +2658,12 @@ private void startVncServer() { } } - // Store VNC resolution for mouse coordinate mapping + // Store VNC resolution for VNC mouse coordinate mapping (NOT for HID mouse) vncServerWidth = width; vncServerHeight = height; - Log.e(TAG, "VNC resolution set to: " + width + "x" + height); - HidManager.width_height(width, height); + Log.d(TAG, "VNC resolution set to: " + width + "x" + height); + // DO NOT call HidManager.width_height() here - it would override TextureView dimensions + // and break touch screen coordinate mapping. VNC uses its own vncServerWidth/Height. boolean started = vncService.startServer(vncConfig.getPassword(), vncConfig.getPort(), width, height, vncConfig.getEncoding(), vncConfig.getQualityLevel(), vncConfig.getCompressLevel()); if (started && mCameraHelper != null) { @@ -2404,12 +2728,10 @@ public void onSettingsChanged() { private int lastVncButtonMask = 0; private void handleVncMouseEvent(int buttonMask, int x, int y) { - // Set MouseManager dimensions to VNC resolution for proper normalization - if (vncServerWidth > 0 && vncServerHeight > 0) { - HidManager.width_height(vncServerWidth, vncServerHeight); - } + // DO NOT call HidManager.width_height() here - it would override TextureView dimensions + // and break touch screen coordinate mapping. VNC uses its own vncServerWidth/Height. - Log.e(TAG, "VNC mouse: button=" + buttonMask + " pos=" + x + "," + y); + Log.d(TAG, "VNC mouse: button=" + buttonMask + " pos=" + x + "," + y); // Button state change detection if (buttonMask != lastVncButtonMask) { @@ -2423,9 +2745,9 @@ private void handleVncMouseEvent(int buttonMask, int x, int y) { Log.e(TAG, "VNC mouse click: " + mouseClick); HidManager.sendHexAbsButtonClickData(mouseClick, x, y); } else { - // Button release - send release (null click at position) + // Button release - a plain move would keep the held button pressed Log.e(TAG, "VNC mouse release"); - HidManager.sendHexAbsData(x, y); + HidManager.sendHexAbsReleaseData(x, y); } lastVncButtonMask = buttonMask; } else if (buttonMask == 0) { @@ -2569,11 +2891,12 @@ private void startWebRtcServer() { // No rotation needed - the device orientation doesn't affect the captured content. int rotation = 0; - // Store WebRTC resolution for mouse coordinate mapping + // Store WebRTC resolution for WebRTC mouse coordinate mapping (NOT for HID mouse) webRtcServerWidth = width; webRtcServerHeight = height; - Log.i(TAG, "WebRTC resolution set to: " + width + "x" + height); - HidManager.width_height(width, height); + Log.d(TAG, "WebRTC resolution set to: " + width + "x" + height); + // DO NOT call HidManager.width_height() here - it would override TextureView dimensions + // and break touch screen coordinate mapping. WebRTC uses its own webRtcServerWidth/Height. boolean started = webRtcService.startServer( config.getSignallingPort(), @@ -3184,24 +3507,12 @@ public void onMouseClick(int buttonMask) { default: clickType = "SecNullData"; } - sendMouseClick(clickType); + sendMouseStripPress(clickType); } @Override public void onMouseRelease() { - String releaseData = com.openterface.AOS.target.CH9329MSKBMap.getKeyCodeMap().get("prefix1") + - com.openterface.AOS.target.CH9329MSKBMap.getKeyCodeMap().get("prefix2") + - com.openterface.AOS.target.CH9329MSKBMap.getKeyCodeMap().get("address") + - com.openterface.AOS.target.CH9329MSKBMap.CmdData().get("CmdMS_REL") + - com.openterface.AOS.target.CH9329MSKBMap.DataLen().get("DataLenRelMS") + - com.openterface.AOS.target.CH9329MSKBMap.MSRelData().get("FirstData") + - com.openterface.AOS.target.CH9329MSKBMap.MSRelData().get("SecNullData") + - "00" + "00" + "00"; - releaseData += com.openterface.AOS.serial.CH9329Function.makeChecksum(releaseData); - byte[] bytes = com.openterface.AOS.serial.CH9329Function.hexStringToByteArray(releaseData); - if (usbDeviceManager != null && usbDeviceManager.isConnected()) { - usbDeviceManager.writeData(bytes); - } + sendMouseStripRelease(); } @Override @@ -3219,6 +3530,10 @@ public void onScrollDown() { // Deprecated: scroll handled by BasicPortraitScrollStripView } }); + + // A locked keycap has to keep its button pressed across gestures; report the + // lock state separately from the press/release callbacks. + portraitMouseStrip.setOnLockChangeListener(this::applyKeycapLock); } // Setup scroll strip @@ -3245,42 +3560,77 @@ public void onStripScroll(int deltaX, int deltaY) { } /** - * Send a mouse click via CH9329 HID protocol + * Keycap press/release frames share one queue: they used to run on two independent + * threads, so a fast tap could deliver the release before the press and leave the + * button down in the host. */ - private void sendMouseClick(String clickType) { - new Thread(() -> { - try { - String clickData = com.openterface.AOS.target.CH9329MSKBMap.getKeyCodeMap().get("prefix1") + - com.openterface.AOS.target.CH9329MSKBMap.getKeyCodeMap().get("prefix2") + - com.openterface.AOS.target.CH9329MSKBMap.getKeyCodeMap().get("address") + - com.openterface.AOS.target.CH9329MSKBMap.CmdData().get("CmdMS_REL") + - com.openterface.AOS.target.CH9329MSKBMap.DataLen().get("DataLenRelMS") + - com.openterface.AOS.target.CH9329MSKBMap.MSRelData().get("FirstData") + - clickType + "00" + "00" + "00"; - clickData += com.openterface.AOS.serial.CH9329Function.makeChecksum(clickData); - byte[] bytes = com.openterface.AOS.serial.CH9329Function.hexStringToByteArray(clickData); - if (usbDeviceManager != null && usbDeviceManager.isConnected()) { - usbDeviceManager.writeData(bytes); - } - // Auto release after 30ms - Thread.sleep(30); - String releaseData = com.openterface.AOS.target.CH9329MSKBMap.getKeyCodeMap().get("prefix1") + - com.openterface.AOS.target.CH9329MSKBMap.getKeyCodeMap().get("prefix2") + - com.openterface.AOS.target.CH9329MSKBMap.getKeyCodeMap().get("address") + - com.openterface.AOS.target.CH9329MSKBMap.CmdData().get("CmdMS_REL") + - com.openterface.AOS.target.CH9329MSKBMap.DataLen().get("DataLenRelMS") + - com.openterface.AOS.target.CH9329MSKBMap.MSRelData().get("FirstData") + - com.openterface.AOS.target.CH9329MSKBMap.MSRelData().get("SecNullData") + - "00" + "00" + "00"; - releaseData += com.openterface.AOS.serial.CH9329Function.makeChecksum(releaseData); - bytes = com.openterface.AOS.serial.CH9329Function.hexStringToByteArray(releaseData); - if (usbDeviceManager != null && usbDeviceManager.isConnected()) { - usbDeviceManager.writeData(bytes); - } - } catch (Exception e) { - Log.e(TAG, "Error sending mouse click: " + e.getMessage()); + private final java.util.concurrent.ExecutorService mouseStripExecutor = + java.util.concurrent.Executors.newSingleThreadExecutor(); + + /** + * Press a keycap button. The button stays pressed until sendMouseStripRelease(): the + * long-press lock and the "hold L and drag" gesture both rely on it. Releasing it + * from this side after 30 ms made every hold-to-drag impossible. + */ + private void sendMouseStripPress(String clickType) { + HidManager.setHeldButton(clickType); + String hex = CH9329MSKBMap.MSAbsData().get(clickType); + final String buttonByte = (hex == null) ? "00" : hex; + mouseStripExecutor.execute(() -> writeMouseStripFrame(buttonByte)); + } + + /** + * Release the keycap buttons. Only the buttons that are still locked stay pressed, + * so unlocking one keycap does not drop another one. + */ + private void sendMouseStripRelease() { + HidManager.setHeldButton(null); + final String buttonByte = String.format("%02X", HidManager.getLockedMask()); + mouseStripExecutor.execute(() -> writeMouseStripFrame(buttonByte)); + } + + /** One relative-mode frame carrying a raw CH9329 button byte. */ + private void writeMouseStripFrame(String buttonHex) { + try { + String data = CH9329MSKBMap.getKeyCodeMap().get("prefix1") + + CH9329MSKBMap.getKeyCodeMap().get("prefix2") + + CH9329MSKBMap.getKeyCodeMap().get("address") + + CH9329MSKBMap.CmdData().get("CmdMS_REL") + + CH9329MSKBMap.DataLen().get("DataLenRelMS") + + CH9329MSKBMap.MSRelData().get("FirstData") + + buttonHex + "00" + "00" + "00"; + data += CH9329Function.makeChecksum(data); + byte[] bytes = CH9329Function.hexStringToByteArray(data); + if (usbDeviceManager != null && usbDeviceManager.isConnected()) { + usbDeviceManager.writeData(bytes); } - }).start(); + } catch (Exception e) { + Log.e(TAG, "Error sending keycap frame: " + e.getMessage()); + } + } + + /** Keep a locked keycap pressed in the host independently of the touch gestures. */ + private void applyKeycapLock(int buttonMask, boolean locked) { + String button = keycapName(buttonMask); + if (button == null) return; + if (locked) { + HidManager.lockButton(button); + } else { + HidManager.unlockButton(button); + } + } + + private static String keycapName(int buttonMask) { + switch (buttonMask) { + case com.openterface.AOS.view.MouseControlStripView.BTN_LEFT: + return "SecLeftData"; + case com.openterface.AOS.view.MouseControlStripView.BTN_MIDDLE: + return "SecMiddleData"; + case com.openterface.AOS.view.MouseControlStripView.BTN_RIGHT: + return "SecRightData"; + default: + return null; + } } /** @@ -3903,48 +4253,13 @@ public void onMouseClick(int buttonMask) { clickType = "SecNullData"; } Log.d(TAG, "Mouse strip button click: " + clickType); - new Thread(() -> { - try { - String clickData = CH9329MSKBMap.getKeyCodeMap().get("prefix1") + - CH9329MSKBMap.getKeyCodeMap().get("prefix2") + - CH9329MSKBMap.getKeyCodeMap().get("address") + - CH9329MSKBMap.CmdData().get("CmdMS_REL") + - CH9329MSKBMap.DataLen().get("DataLenRelMS") + - CH9329MSKBMap.MSRelData().get("FirstData") + - clickType + "00" + "00" + "00"; - clickData += CH9329Function.makeChecksum(clickData); - byte[] bytes = CH9329Function.hexStringToByteArray(clickData); - if (usbDeviceManager != null && usbDeviceManager.isConnected()) { - usbDeviceManager.writeData(bytes); - } - } catch (Exception e) { - Log.e(TAG, "Error sending mouse click: " + e.getMessage()); - } - }).start(); + sendMouseStripPress(clickType); } @Override public void onMouseRelease() { Log.d(TAG, "Mouse strip button release"); - new Thread(() -> { - try { - String releaseData = CH9329MSKBMap.getKeyCodeMap().get("prefix1") + - CH9329MSKBMap.getKeyCodeMap().get("prefix2") + - CH9329MSKBMap.getKeyCodeMap().get("address") + - CH9329MSKBMap.CmdData().get("CmdMS_REL") + - CH9329MSKBMap.DataLen().get("DataLenRelMS") + - CH9329MSKBMap.MSRelData().get("FirstData") + - CH9329MSKBMap.MSRelData().get("SecNullData") + - "00" + "00" + "00"; - releaseData += CH9329Function.makeChecksum(releaseData); - byte[] bytes = CH9329Function.hexStringToByteArray(releaseData); - if (usbDeviceManager != null && usbDeviceManager.isConnected()) { - usbDeviceManager.writeData(bytes); - } - } catch (Exception e) { - Log.e(TAG, "Error releasing mouse button: " + e.getMessage()); - } - }).start(); + sendMouseStripRelease(); } @Override @@ -3962,6 +4277,9 @@ public void onScrollDown() { // Deprecated: scroll handled by BasicPortraitScrollStripView } }); + + // A locked keycap has to keep its button pressed across gestures. + mouseControlStripView.setOnLockChangeListener(this::applyKeycapLock); } // Setup landscape scroll strip @@ -4066,8 +4384,6 @@ public int getTouchPadOpacity() { private final String KEY_SOUND_VOLUME = "key_sound_volume"; private final String KEY_VIBRATE_ENABLED = "key_vibrate_enabled"; private final String KEY_VIBRATE_INTENSITY = "key_vibrate_intensity"; - private final String KEY_SCROLL_SPEED = "scroll_speed"; - private final String KEY_MOUSE_SPEED = "mouse_speed"; private final String KEY_PRE_VALIDATE = "pre_validate_enabled"; private final String KEY_AUTO_SAVE = "auto_save_enabled"; @@ -4083,12 +4399,6 @@ private void setupSettingsModule(View view) { LinearLayout vibrateIntensityLayout = view.findViewById(R.id.settings_vibrate_intensity_layout); SeekBar vibrateSeekbar = view.findViewById(R.id.settings_vibrate_intensity_seekbar); - // === Mouse Settings === - SeekBar scrollSpeedSeekbar = view.findViewById(R.id.settings_scroll_speed_seekbar); - TextView scrollSpeedValue = view.findViewById(R.id.settings_scroll_speed_value); - SeekBar mouseSpeedSeekbar = view.findViewById(R.id.settings_mouse_speed_seekbar); - TextView mouseSpeedValue = view.findViewById(R.id.settings_mouse_speed_value); - // === IME Settings === androidx.appcompat.widget.SwitchCompat preValidateSwitch = view.findViewById(R.id.settings_pre_validate_switch); androidx.appcompat.widget.SwitchCompat autoSaveSwitch = view.findViewById(R.id.settings_auto_save_switch); @@ -4098,8 +4408,6 @@ private void setupSettingsModule(View view) { int soundVolume = prefs.getInt(KEY_SOUND_VOLUME, 80); boolean vibrateEnabled = prefs.getBoolean(KEY_VIBRATE_ENABLED, true); int vibrateIntensity = prefs.getInt(KEY_VIBRATE_INTENSITY, 60); - int scrollSpeed = prefs.getInt(KEY_SCROLL_SPEED, 50); - int mouseSpeed = prefs.getInt(KEY_MOUSE_SPEED, 50); boolean preValidate = prefs.getBoolean(KEY_PRE_VALIDATE, true); boolean autoSave = prefs.getBoolean(KEY_AUTO_SAVE, false); @@ -4110,12 +4418,6 @@ private void setupSettingsModule(View view) { if (vibrateSwitch != null) vibrateSwitch.setChecked(vibrateEnabled); if (vibrateSeekbar != null) vibrateSeekbar.setProgress(vibrateIntensity); if (vibrateIntensityLayout != null) vibrateIntensityLayout.setVisibility(vibrateEnabled ? View.VISIBLE : View.GONE); - if (scrollSpeedSeekbar != null) scrollSpeedSeekbar.setProgress(scrollSpeed); - if (scrollSpeedValue != null) scrollSpeedValue.setText(getScrollSpeedLabel(scrollSpeed)); - if (mouseSpeedSeekbar != null) mouseSpeedSeekbar.setProgress(mouseSpeed); - if (mouseSpeedValue != null) mouseSpeedValue.setText(getMouseSpeedLabel(mouseSpeed)); - // Apply saved mouse speed multiplier to both Java and Core mouse implementations - com.openterface.AOS.target.HidManager.setMouseSpeedMultiplier(progressToMouseSpeed(mouseSpeed)); if (preValidateSwitch != null) preValidateSwitch.setChecked(preValidate); if (autoSaveSwitch != null) autoSaveSwitch.setChecked(autoSave); @@ -4157,35 +4459,32 @@ private void setupSettingsModule(View view) { }); } - // === Scroll Speed === - if (scrollSpeedSeekbar != null) { - scrollSpeedSeekbar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() { - @Override - public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) { - if (fromUser) { - prefs.edit().putInt(KEY_SCROLL_SPEED, progress).apply(); - if (scrollSpeedValue != null) scrollSpeedValue.setText(getScrollSpeedLabel(progress)); - } - } - @Override public void onStartTrackingTouch(SeekBar seekBar) {} - @Override public void onStopTrackingTouch(SeekBar seekBar) {} + // === Mouse Control Mode Buttons === + Button btnAbsoluteMode = view.findViewById(R.id.settings_mouse_mode_absolute); + Button btnRelativeMode = view.findViewById(R.id.settings_mouse_mode_relative); + + // Load current mouse mode from MouseModeManager + com.openterface.AOS.target.MouseMode currentMode = com.openterface.AOS.target.MouseModeManager.getMode(); + updateMouseModeButtons(btnAbsoluteMode, btnRelativeMode, currentMode); + + if (btnAbsoluteMode != null) { + btnAbsoluteMode.setOnClickListener(v -> { + com.openterface.AOS.target.MouseModeManager.setMode(com.openterface.AOS.target.MouseMode.ABSOLUTE); + com.openterface.AOS.utils.MouseModePreferences.save(MainActivity.this, + com.openterface.AOS.target.MouseMode.ABSOLUTE, + com.openterface.AOS.target.MouseModeManager.isTrackpadSensitivityEnabled()); + updateMouseModeButtons(btnAbsoluteMode, btnRelativeMode, com.openterface.AOS.target.MouseMode.ABSOLUTE); + Log.d(TAG, "Switched to absolute mode from settings panel"); }); } - - // === Mouse Move Speed === - if (mouseSpeedSeekbar != null) { - mouseSpeedSeekbar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() { - @Override - public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) { - if (fromUser) { - prefs.edit().putInt(KEY_MOUSE_SPEED, progress).apply(); - if (mouseSpeedValue != null) mouseSpeedValue.setText(getMouseSpeedLabel(progress)); - // Apply multiplier immediately so it takes effect on next movement - com.openterface.AOS.target.HidManager.setMouseSpeedMultiplier(progressToMouseSpeed(progress)); - } - } - @Override public void onStartTrackingTouch(SeekBar seekBar) {} - @Override public void onStopTrackingTouch(SeekBar seekBar) {} + if (btnRelativeMode != null) { + btnRelativeMode.setOnClickListener(v -> { + com.openterface.AOS.target.MouseModeManager.setMode(com.openterface.AOS.target.MouseMode.RELATIVE); + com.openterface.AOS.utils.MouseModePreferences.save(MainActivity.this, + com.openterface.AOS.target.MouseMode.RELATIVE, + com.openterface.AOS.target.MouseModeManager.isTrackpadSensitivityEnabled()); + updateMouseModeButtons(btnAbsoluteMode, btnRelativeMode, com.openterface.AOS.target.MouseMode.RELATIVE); + Log.d(TAG, "Switched to relative mode from settings panel"); }); } @@ -4291,8 +4590,10 @@ public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) { LinearLayout baudrateLayout = view.findViewById(R.id.settings_baudrate_layout); TextView baudrateValue = view.findViewById(R.id.settings_baudrate_value); if (baudrateLayout != null && baudrateValue != null) { - int currentBaudrate = usbDeviceManager != null ? usbDeviceManager.getCurrentBaudrate() : 0; - baudrateValue.setText(String.valueOf(currentBaudrate > 0 ? currentBaudrate : 115200)); + // Use preferredBaudrate (user's selection) as the source of truth for UI display + // getCurrentBaudrate() returns -1 when disconnected, which would show wrong value + int preferredBaudrate = usbDeviceManager != null ? usbDeviceManager.getPreferredBaudrate() : 115200; + baudrateValue.setText(String.valueOf(preferredBaudrate > 0 ? preferredBaudrate : 115200)); baudrateLayout.setOnClickListener(v -> showBaudrateDialogInPortrait(baudrateValue)); } @@ -4356,38 +4657,65 @@ public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) { } } - private String getScrollSpeedLabel(int progress) { - if (progress <= 20) return getString(R.string.speed_very_slow); - if (progress <= 40) return getString(R.string.speed_slow); - if (progress <= 60) return getString(R.string.speed_medium); - if (progress <= 80) return getString(R.string.speed_fast); - return getString(R.string.speed_very_fast); - } - /** - * Convert SeekBar progress (0-100) to mouse speed multiplier (0.25x-3.0x). - * Uses piecewise linear mapping: - * 0-50 → 0.25x to 1.0x (slow range, finer control) - * 50-100 → 1.0x to 3.0x (fast range, wider spread) - * Default progress=50 maps to multiplier=1.0x. + * Update mouse mode button states (selected/unselected). + * Uses MouseMode enum for clear state representation. */ - private float progressToMouseSpeed(int progress) { - if (progress <= 50) { - return 0.25f + (progress / 50f) * 0.75f; - } else { - return 1.0f + ((progress - 50) / 50f) * 2.0f; + private void updateMouseModeButtons(Button btnAbsolute, Button btnRelative, com.openterface.AOS.target.MouseMode mode) { + int selectedColor = getResources().getColor(R.color.primary); + int defaultColor = getResources().getColor(R.color.text_primary); + + if (btnAbsolute != null) { + boolean isAbs = (mode == com.openterface.AOS.target.MouseMode.ABSOLUTE); + btnAbsolute.setSelected(isAbs); + btnAbsolute.setTextColor(isAbs ? 0xFFFFFFFF : defaultColor); + } + if (btnRelative != null) { + boolean isRel = (mode == com.openterface.AOS.target.MouseMode.RELATIVE); + btnRelative.setSelected(isRel); + btnRelative.setTextColor(isRel ? 0xFFFFFFFF : defaultColor); } } - private String getMouseSpeedLabel(int progress) { - float multiplier = progressToMouseSpeed(progress); - String speed; - if (progress <= 15) speed = getString(R.string.speed_very_slow); - else if (progress <= 35) speed = getString(R.string.speed_slow); - else if (progress <= 65) speed = getString(R.string.speed_normal); - else if (progress <= 85) speed = getString(R.string.speed_fast); - else speed = getString(R.string.speed_very_fast); - return speed + " " + String.format("%.1f", multiplier) + "x"; + /** + * Hook the Bluetooth mouse into the preview while it is in use: it installs the + * captured-pointer listener that forwards relative motion to the target. + * + * The green viewport indicator in the PiP is deliberately left alone. It used to be + * hidden from here, and because "connected" is really a ten second activity window, + * every mouse move made the indicator disappear and every pause brought it back. + */ + private void applyBtMousePointerCapture(boolean btMouseConnected) { + Log.d(TAG, "applyBtMousePointerCapture: btMouseConnected=" + btMouseConnected + + " screenWidth=" + MouseManager.screenWidth + + " screenHeight=" + MouseManager.screenHeight); + + // NOTE: System cursor is now visible intentionally. + // Users watch the phone screen - seeing the local cursor enter black borders + // makes the Clamp behavior (remote cursor stops at edge) intuitive. + // Dual cursor is not a problem: remote cursor is inside video, local cursor + // is on phone screen, visually distinguishable. + + // Use OnCapturedPointerListener to handle BT mouse events that may be + // captured by the system (API 24+). This replaces requestPointerCapture() + // which caused phone reboots on some devices (kernel USB/BT driver conflict). + if (mBinding != null && mBinding.viewMainPreview != null + && android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.N) { + if (btMouseConnected) { + mBinding.viewMainPreview.setOnCapturedPointerListener((view, event) -> { + Log.d(TAG, "OnCapturedPointerListener: action=" + event.getAction() + + " source=0x" + Integer.toHexString(event.getSource())); + return CustomTouchListener.handleGenericMotionEvent(event); + }); + // Ensure view can receive captured pointer events + mBinding.viewMainPreview.setFocusable(true); + mBinding.viewMainPreview.requestFocus(); + Log.d(TAG, "OnCapturedPointerListener set for BT mouse events"); + } else { + mBinding.viewMainPreview.setOnCapturedPointerListener(null); + Log.d(TAG, "OnCapturedPointerListener removed"); + } + } } /** @@ -4499,14 +4827,6 @@ public int getVibrateIntensity() { return getSharedPreferences(PREF_SETTINGS, MODE_PRIVATE).getInt(KEY_VIBRATE_INTENSITY, 60); } - public int getScrollSpeed() { - return getSharedPreferences(PREF_SETTINGS, MODE_PRIVATE).getInt(KEY_SCROLL_SPEED, 50); - } - - public int getMouseSpeed() { - return getSharedPreferences(PREF_SETTINGS, MODE_PRIVATE).getInt(KEY_MOUSE_SPEED, 50); - } - // ============================================================ // SettingsFloatingFragment.SettingsCallback implementation // ============================================================ @@ -4549,17 +4869,6 @@ public void setVibrateIntensity(int intensity) { } } - @Override - public void setScrollSpeed(int speed) { - getSharedPreferences(PREF_SETTINGS, MODE_PRIVATE).edit().putInt(KEY_SCROLL_SPEED, speed).apply(); - } - - @Override - public void setMouseSpeed(int speed) { - getSharedPreferences(PREF_SETTINGS, MODE_PRIVATE).edit().putInt(KEY_MOUSE_SPEED, speed).apply(); - HidManager.setMouseSpeedMultiplier(progressToMouseSpeed(speed)); - } - /** * Show floating settings dialog instead of bottom drawer. */ diff --git a/app/src/main/java/com/openterface/AOS/drawerLayout/DrawerLayoutDeal.java b/app/src/main/java/com/openterface/AOS/drawerLayout/DrawerLayoutDeal.java index 3c2ddd2..5e64d44 100644 --- a/app/src/main/java/com/openterface/AOS/drawerLayout/DrawerLayoutDeal.java +++ b/app/src/main/java/com/openterface/AOS/drawerLayout/DrawerLayoutDeal.java @@ -1,6 +1,5 @@ package com.openterface.AOS.drawerLayout; -import android.annotation.SuppressLint; import android.app.AlertDialog; import android.content.Context; import android.content.pm.PackageInfo; @@ -19,20 +18,26 @@ import android.widget.RadioGroup; import android.widget.TextView; -import androidx.core.content.ContextCompat; -import androidx.core.view.GravityCompat; import androidx.drawerlayout.widget.DrawerLayout; -import androidx.fragment.app.Fragment; -import androidx.fragment.app.FragmentTransaction; import com.google.android.material.floatingactionbutton.FloatingActionButton; import com.openterface.AOS.R; import com.openterface.AOS.activity.MainActivity; -import com.openterface.AOS.serial.CustomTouchListener; import com.openterface.AOS.serial.UsbDeviceManager; +import com.openterface.AOS.target.MouseMode; +import com.openterface.AOS.target.MouseModeManager; +import com.openterface.AOS.utils.MouseModePreferences; import com.openterface.AOS.vnc.VncServerConfig; -public class DrawerLayoutDeal extends Fragment { +/** + * Helper that wires the landscape drawer/settings panel. + *

+ * This is a plain controller, not a fragment: it is only ever created with + * {@code new} and never handed to a FragmentManager. It used to extend + * Fragment without a no-arg constructor, which would have crashed the moment + * anything did try to restore it through the framework. + */ +public class DrawerLayoutDeal { private static final String TAG = "OP-UI"; private final MainActivity activity; private final Context context; @@ -40,22 +45,15 @@ public class DrawerLayoutDeal extends Fragment { private final FloatingActionButton set_up_button; private final DrawerLayout drawer_layout; - private final Drawable Abs_ctrl_default_button_drawable; + // Mouse mode button drawables + private final Drawable btnAbsoluteDrawable; + private final Drawable btnRelativeDrawable; - private final Drawable Abs_ctrl_drag_button_drawable; - - private final Drawable Rel_ctrl_button_drawable; - - private final Drawable Abs_ctrl_trackpad_button_drawable; - - private boolean KeyMouse_state = false; - private boolean keyMouseAbsCtrlState = false; private boolean mIsRecording; - private final Button Abs_ctrl_default_button; - private final Button Abs_ctrl_drag_button; - private final Button Rel_ctrl_button; - private final Button Abs_ctrl_trackpad_button; + // Mouse mode buttons + private final Button btnAbsolute; + private final Button btnRelative; private final Button action_device; private final Button action_safely_eject; private final Button action_control; @@ -88,17 +86,18 @@ public DrawerLayoutDeal(MainActivity activity , Bundle savedInstanceState, boole set_up_button = activity.findViewById(R.id.set_up_button); drawer_layout = activity.findViewById(R.id.drawer_layout); - Abs_ctrl_default_button = activity.findViewById(R.id.Abs_ctrl_default_button); - Abs_ctrl_default_button_drawable = Abs_ctrl_default_button.getCompoundDrawables()[1]; - Abs_ctrl_drag_button = activity.findViewById(R.id.Abs_ctrl_drag_button); - Abs_ctrl_drag_button_drawable = Abs_ctrl_drag_button.getCompoundDrawables()[1]; - Rel_ctrl_button = activity.findViewById(R.id.Rel_ctrl_button); - Rel_ctrl_button_drawable = Rel_ctrl_button.getCompoundDrawables()[1]; - Abs_ctrl_trackpad_button = activity.findViewById(R.id.Abs_ctrl_trackpad_button); - Abs_ctrl_trackpad_button_drawable = Abs_ctrl_trackpad_button.getCompoundDrawables()[1]; + // Initialize mouse mode buttons + btnAbsolute = activity.findViewById(R.id.btn_absolute); + btnAbsoluteDrawable = btnAbsolute != null ? btnAbsolute.getCompoundDrawables()[1] : null; + btnRelative = activity.findViewById(R.id.btn_relative); + btnRelativeDrawable = btnRelative != null ? btnRelative.getCompoundDrawables()[1] : null; - Rel_ctrl_button_drawable.setColorFilter(ContextCompat.getColor(context,android.R.color.holo_red_light), PorterDuff.Mode.SRC_IN); - Rel_ctrl_button.setTextColor(ContextCompat.getColor(context,android.R.color.holo_red_light)); + // Initialize mouse mode from preferences + MouseMode savedMode = MouseModePreferences.loadMode(context); + boolean trackpadSensitivity = MouseModePreferences.loadTrackpadSensitivity(context); + MouseModeManager.setMode(savedMode); + MouseModeManager.setTrackpadSensitivity(trackpadSensitivity); + updateMouseModeUI(); action_device = activity.findViewById(R.id.action_device); action_safely_eject = activity.findViewById(R.id.action_safely_eject); @@ -130,7 +129,7 @@ public DrawerLayoutDeal(MainActivity activity , Bundle savedInstanceState, boole String appVersion = packageInfo.versionName; app_version.setText(appVersion); } catch (PackageManager.NameNotFoundException e) { - e.printStackTrace(); + Log.e(TAG, "Cannot read app version: " + e.getMessage(), e); app_version.setText(R.string.unknown); } CameraVideoDeal(); @@ -174,99 +173,64 @@ public void onClick(View v) { } }); - Abs_ctrl_default_button.setOnClickListener(v -> { - - if (Abs_ctrl_default_button_drawable != null) { - - Abs_ctrl_default_button_drawable.setColorFilter(ContextCompat.getColor(context,android.R.color.holo_red_light), PorterDuff.Mode.SRC_IN); - Abs_ctrl_default_button.setTextColor(ContextCompat.getColor(context,android.R.color.holo_red_light)); - - Abs_ctrl_drag_button_drawable.setColorFilter(ContextCompat.getColor(context,android.R.color.white), PorterDuff.Mode.SRC_IN); - Abs_ctrl_drag_button.setTextColor(ContextCompat.getColor(context,android.R.color.white)); - - Rel_ctrl_button_drawable.setColorFilter(ContextCompat.getColor(context,android.R.color.white), PorterDuff.Mode.SRC_IN); - Rel_ctrl_button.setTextColor(ContextCompat.getColor(context,android.R.color.white)); - - Abs_ctrl_trackpad_button_drawable.setColorFilter(ContextCompat.getColor(context,android.R.color.white), PorterDuff.Mode.SRC_IN); - Abs_ctrl_trackpad_button.setTextColor(ContextCompat.getColor(context,android.R.color.white)); - - KeyMouse_state = true; - keyMouseAbsCtrlState = false; - CustomTouchListener.KeyMouse_state(KeyMouse_state, keyMouseAbsCtrlState); - CustomTouchListener.setVirtualTrackpadMode(false); - - } - }); - - Abs_ctrl_drag_button.setOnClickListener(v -> { - - if (Abs_ctrl_drag_button_drawable != null) { - - Abs_ctrl_drag_button_drawable.setColorFilter(ContextCompat.getColor(context,android.R.color.holo_red_light), PorterDuff.Mode.SRC_IN); - Abs_ctrl_drag_button.setTextColor(ContextCompat.getColor(context,android.R.color.holo_red_light)); - - Abs_ctrl_default_button_drawable.setColorFilter(ContextCompat.getColor(context,android.R.color.white), PorterDuff.Mode.SRC_IN); - Abs_ctrl_default_button.setTextColor(ContextCompat.getColor(context,android.R.color.white)); - - Rel_ctrl_button_drawable.setColorFilter(ContextCompat.getColor(context,android.R.color.white), PorterDuff.Mode.SRC_IN); - Rel_ctrl_button.setTextColor(ContextCompat.getColor(context,android.R.color.white)); - - Abs_ctrl_trackpad_button_drawable.setColorFilter(ContextCompat.getColor(context,android.R.color.white), PorterDuff.Mode.SRC_IN); - Abs_ctrl_trackpad_button.setTextColor(ContextCompat.getColor(context,android.R.color.white)); - - KeyMouse_state = true; - keyMouseAbsCtrlState = true; - CustomTouchListener.KeyMouse_state(KeyMouse_state, keyMouseAbsCtrlState); - CustomTouchListener.setVirtualTrackpadMode(false); - - } - }); - - Rel_ctrl_button.setOnClickListener(v -> { + // Absolute mode button click handler + if (btnAbsolute != null) { + btnAbsolute.setOnClickListener(v -> { + MouseModeManager.setMode(MouseMode.ABSOLUTE); + MouseModePreferences.save(context, MouseMode.ABSOLUTE, + MouseModeManager.isTrackpadSensitivityEnabled()); + updateMouseModeUI(); + Log.d(TAG, "Switched to absolute mode"); + }); + } - if (Rel_ctrl_button_drawable != null) { - Log.d(TAG,"in this rel button"); - Rel_ctrl_button_drawable.setColorFilter(ContextCompat.getColor(context,android.R.color.holo_red_light), PorterDuff.Mode.SRC_IN); - Rel_ctrl_button.setTextColor(ContextCompat.getColor(context,android.R.color.holo_red_light)); + // Relative mode button click handler + if (btnRelative != null) { + btnRelative.setOnClickListener(v -> { + MouseModeManager.setMode(MouseMode.RELATIVE); + MouseModePreferences.save(context, MouseMode.RELATIVE, + MouseModeManager.isTrackpadSensitivityEnabled()); + updateMouseModeUI(); + Log.d(TAG, "Switched to relative mode"); + }); + } + } - Abs_ctrl_drag_button_drawable.setColorFilter(ContextCompat.getColor(context,android.R.color.white), PorterDuff.Mode.SRC_IN); - Abs_ctrl_drag_button.setTextColor(ContextCompat.getColor(context,android.R.color.white)); + /** + * Update the mouse mode button UI to highlight the selected mode. + * Uses MouseModeManager as the single source of truth. + */ + private void updateMouseModeUI() { + if (btnAbsolute == null || btnRelative == null) { + return; + } - Abs_ctrl_default_button_drawable.setColorFilter(ContextCompat.getColor(context,android.R.color.white), PorterDuff.Mode.SRC_IN); - Abs_ctrl_default_button.setTextColor(ContextCompat.getColor(context,android.R.color.white)); + int white = context.getResources().getColor(android.R.color.white); + int highlight = context.getResources().getColor(android.R.color.holo_red_light); - Abs_ctrl_trackpad_button_drawable.setColorFilter(ContextCompat.getColor(context,android.R.color.white), PorterDuff.Mode.SRC_IN); - Abs_ctrl_trackpad_button.setTextColor(ContextCompat.getColor(context,android.R.color.white)); + // Reset both buttons to white. The drawables come from the layout and may be + // absent, in which case only the label colour carries the state. + if (btnAbsoluteDrawable != null) { + btnAbsoluteDrawable.setColorFilter(white, PorterDuff.Mode.SRC_IN); + } + btnAbsolute.setTextColor(white); + if (btnRelativeDrawable != null) { + btnRelativeDrawable.setColorFilter(white, PorterDuff.Mode.SRC_IN); + } + btnRelative.setTextColor(white); - KeyMouse_state = false; - keyMouseAbsCtrlState = false; - CustomTouchListener.KeyMouse_state(KeyMouse_state, keyMouseAbsCtrlState); - CustomTouchListener.setVirtualTrackpadMode(false); + // Highlight the active mode + if (MouseModeManager.isAbsoluteMode()) { + if (btnAbsoluteDrawable != null) { + btnAbsoluteDrawable.setColorFilter(highlight, PorterDuff.Mode.SRC_IN); } - }); - - Abs_ctrl_trackpad_button.setOnClickListener(v -> { - - if (Abs_ctrl_trackpad_button_drawable != null) { - Log.d(TAG,"in this trackpad button"); - Abs_ctrl_trackpad_button_drawable.setColorFilter(ContextCompat.getColor(context,android.R.color.holo_red_light), PorterDuff.Mode.SRC_IN); - Abs_ctrl_trackpad_button.setTextColor(ContextCompat.getColor(context,android.R.color.holo_red_light)); - - Abs_ctrl_default_button_drawable.setColorFilter(ContextCompat.getColor(context,android.R.color.white), PorterDuff.Mode.SRC_IN); - Abs_ctrl_default_button.setTextColor(ContextCompat.getColor(context,android.R.color.white)); - - Abs_ctrl_drag_button_drawable.setColorFilter(ContextCompat.getColor(context,android.R.color.white), PorterDuff.Mode.SRC_IN); - Abs_ctrl_drag_button.setTextColor(ContextCompat.getColor(context,android.R.color.white)); - - Rel_ctrl_button_drawable.setColorFilter(ContextCompat.getColor(context,android.R.color.white), PorterDuff.Mode.SRC_IN); - Rel_ctrl_button.setTextColor(ContextCompat.getColor(context,android.R.color.white)); - - KeyMouse_state = false; - keyMouseAbsCtrlState = false; - CustomTouchListener.KeyMouse_state(KeyMouse_state, keyMouseAbsCtrlState); - CustomTouchListener.setVirtualTrackpadMode(true); + btnAbsolute.setTextColor(highlight); + } else { + if (btnRelativeDrawable != null) { + btnRelativeDrawable.setColorFilter(highlight, PorterDuff.Mode.SRC_IN); } - }); + btnRelative.setTextColor(highlight); + } } private void CameraVideoDeal(){ @@ -346,7 +310,7 @@ private void showBaudrateDialog() { if (currentBaudrate > 0) { currentText = context.getString(R.string.baudrate_current, String.valueOf(currentBaudrate)); } else { - currentText = "Current: Not connected"; + currentText = context.getString(R.string.baudrate_not_connected); } currentBaudrateText.setText(currentText); diff --git a/app/src/main/java/com/openterface/AOS/drawerLayout/ZoomLayoutDeal.java b/app/src/main/java/com/openterface/AOS/drawerLayout/ZoomLayoutDeal.java index ad75559..66f021f 100644 --- a/app/src/main/java/com/openterface/AOS/drawerLayout/ZoomLayoutDeal.java +++ b/app/src/main/java/com/openterface/AOS/drawerLayout/ZoomLayoutDeal.java @@ -25,66 +25,70 @@ package com.openterface.AOS.drawerLayout; import android.annotation.SuppressLint; -import android.graphics.Bitmap; -import android.graphics.Matrix; import android.util.DisplayMetrics; import android.util.Log; import android.view.MotionEvent; -import android.view.SurfaceHolder; import android.view.View; import android.view.ViewGroup; -import android.widget.Button; import android.widget.FrameLayout; import android.widget.ImageButton; -import android.widget.ImageView; import android.widget.LinearLayout; -import androidx.annotation.NonNull; import androidx.drawerlayout.widget.DrawerLayout; import com.openterface.AOS.ICameraHelper; import com.openterface.AOS.R; import com.openterface.AOS.activity.MainActivity; import com.openterface.AOS.databinding.ActivityMainBinding; -import com.openterface.AOS.target.HidManager; // import com.openterface.AOS.target.MouseManager; import com.serenegiant.widget.AspectRatioSurfaceView; public class ZoomLayoutDeal { private static final String TAG = "OP-UI"; - private static MainActivity activity; - private static DrawerLayout drawerLayout; - private static View thumbnailContainer; - private static View indicatorView; - private static float ratioX; - private static float ratioY; + + /** + * Per-frame trace for the indicator/scroll mapping. This runs on every MOVE + * event while the thumbnail is dragged, so the output stays off by default. + */ + private static final boolean DEBUG_LAYOUT_LOG = false; + + /** Below this the indicator is not worth re-laying out during a pinch. */ + private static final int INDICATOR_SIZE_THRESHOLD = 3; + + // Everything below belongs to the activity that created this controller. Holding it + // statically kept the whole activity, its views and window alive after onDestroy, so + // the members are instance members and the activity owns the single instance. + private MainActivity activity; + private DrawerLayout drawerLayout; + private View thumbnailContainer; + private View indicatorView; + private float ratioX; + private float ratioY; private final AspectRatioSurfaceView cameraViewSecond; - private static ICameraHelper mCameraHelper; + private ICameraHelper mCameraHelper; private ActivityMainBinding mBinding; - private static int setMaxViewX; - private static int setMaxViewY; - private static int screenWidth; - private static int screenHeight; + private int screenWidth; + private int screenHeight; - private static ImageButton dragButton; + private ImageButton dragButton; private float lastTouchX, lastTouchY; // Drag offset to prevent indicator jumping when starting to drag - private static float dragOffsetX = 0f; - private static float dragOffsetY = 0f; + private float dragOffsetX = 0f; + private float dragOffsetY = 0f; private boolean isKeyboardScaled = false; // Track scaling state - private static int originalWidth; - private static int originalHeight; // Store original dimensions + private int originalWidth; + private int originalHeight; // Store original dimensions private LinearLayout keyBoardView; private ImageButton keyBoardZoomInOutButton; private ImageButton dragHandle; private boolean isPortraitMode = false; // Track if in portrait mode - public static void getViewWidthHeight(int originalWidth, int originalHeight){ + public void getViewWidthHeight(int originalWidth, int originalHeight){ - ZoomLayoutDeal.originalWidth = originalWidth; - ZoomLayoutDeal.originalHeight = originalHeight; + this.originalWidth = originalWidth; + this.originalHeight = originalHeight; } public ZoomLayoutDeal(MainActivity activity, ICameraHelper mCameraHelper, ActivityMainBinding mBinding) { @@ -223,7 +227,7 @@ private void setupDragHandle() { }); } - public static void enlargeView(){ + public void enlargeView(){ // Guard against missing views if (thumbnailContainer == null || dragButton == null || activity == null) { Log.d(TAG, "enlargeView: views not initialized, skipping"); @@ -251,13 +255,11 @@ public static void enlargeView(){ }); } - // Now try to add surface if available + // Now try to add surface if available. Not having the binding view is no reason + // to skip the PiP setup below: the indicator must still be sized and draggable. if (activity.mBinding == null || activity.mBinding.cameraViewSecond == null) { Log.d(TAG, "enlargeView: cameraViewSecond not available"); - return; - } - - if (activity.mBinding.cameraViewSecond.getHolder() != null && + } else if (activity.mBinding.cameraViewSecond.getHolder() != null && activity.mBinding.cameraViewSecond.getHolder().getSurface() != null) { activity.mCameraHelper.addSurface(activity.mBinding.cameraViewSecond.getHolder().getSurface(), false); } @@ -267,7 +269,7 @@ public static void enlargeView(){ } - public static void zoomOut(){ + public void zoomOut(){ // Guard against missing views if (thumbnailContainer == null || dragButton == null || activity == null) { Log.d(TAG, "zoomOut: views not initialized, skipping"); @@ -342,28 +344,35 @@ private void setupDragButton() { }); } - private static void resetIndicatorToCenter() { + private void resetIndicatorToCenter() { // Guard against null views if (activity == null || activity.cameraViewSecond == null || indicatorView == null) { Log.d(TAG, "resetIndicatorToCenter: views not available, skipping"); return; } - // Calculate center position of cameraViewSecond - float centerX = activity.cameraViewSecond.getWidth() / 2f - indicatorView.getWidth() / 2f; - float centerY = activity.cameraViewSecond.getHeight() / 2f - indicatorView.getHeight() / 2f; + // Use the requested sizes rather than the measured ones: this runs right after + // the indicator was resized, before the new size has been laid out. + float pipWidth = pipWidth(); + float pipHeight = pipHeight(); + float indicatorWidth = indicatorWidth(); + float indicatorHeight = indicatorHeight(); + if (pipWidth <= 0 || pipHeight <= 0) { + return; + } + + float centerX = pipWidth / 2f - indicatorWidth / 2f; + float centerY = pipHeight / 2f - indicatorHeight / 2f; // Ensure the position stays within bounds - indicatorView.setX(Math.max(0, Math.min(centerX, activity.cameraViewSecond.getWidth() - indicatorView.getWidth()))); - indicatorView.setY(Math.max(0, Math.min(centerY, activity.cameraViewSecond.getHeight() - indicatorView.getHeight()))); + indicatorView.setX(Math.max(0, Math.min(centerX, pipWidth - indicatorWidth))); + indicatorView.setY(Math.max(0, Math.min(centerY, pipHeight - indicatorHeight))); // Sync main view to match the centered indicator - syncMainViewPosition(centerX + indicatorView.getWidth() / 2f, centerY + indicatorView.getHeight() / 2f); - - Log.d(TAG, "Indicator reset to center: x=" + indicatorView.getX() + ", y=" + indicatorView.getY()); + syncMainViewPosition(centerX + indicatorWidth / 2f, centerY + indicatorHeight / 2f); } - private static void showThumbnailWindow() { + private void showThumbnailWindow() { // Guard against null views before calling sub-methods if (activity == null || activity.mBinding == null || activity.mBinding.viewMainPreview == null) { Log.d(TAG, "showThumbnailWindow: views not available, skipping"); @@ -371,76 +380,158 @@ private static void showThumbnailWindow() { } // Generate thumbnail - generateThumbnail(); - - // Set drag and drop listening setupThumbnailDrag(); // Initialize indicator position based on current main view state initializeIndicatorPosition(); + + // On the frame the container is made visible it has no measured size yet, so the + // indicator was sized from the requested layout values. Size it again from the real + // PiP size as soon as the first layout pass lands. + if (thumbnailContainer != null) { + thumbnailContainer.addOnLayoutChangeListener(new View.OnLayoutChangeListener() { + @Override + public void onLayoutChange(View v, int left, int top, int right, int bottom, + int oldLeft, int oldTop, int oldRight, int oldBottom) { + if (right - left <= 0 || bottom - top <= 0) { + return; + } + v.removeOnLayoutChangeListener(this); + applyIndicatorSize(currentZoomScale()); + resetIndicatorToCenter(); + } + }); + } } - private static void generateThumbnail() { - if (activity == null || activity.mBinding == null || activity.mBinding.viewMainPreview == null) { - Log.e(TAG, "TextureView is not available"); + private void initializeIndicatorPosition() { + if (indicatorView == null || activity == null || activity.cameraViewSecond == null || + activity.mBinding == null || activity.mBinding.viewMainPreview == null) { return; } - if (!activity.mBinding.viewMainPreview.isAvailable()) { - Log.e(TAG, "TextureView is not available"); - return; + applyIndicatorSize(currentZoomScale()); + + // Position indicator at center initially + resetIndicatorToCenter(); + } + + /** + * Size of the PiP. The container is what the user sees, so it is the reference; the + * surface view can measure differently. Falls back to the requested layout size while + * the container has not been laid out yet. + */ + private float pipWidth() { + if (thumbnailContainer != null && thumbnailContainer.getWidth() > 0) { + return thumbnailContainer.getWidth(); + } + if (activity != null && activity.cameraViewSecond != null && activity.cameraViewSecond.getWidth() > 0) { + return activity.cameraViewSecond.getWidth(); + } + if (activity != null && activity.cameraViewSecond != null) { + ViewGroup.LayoutParams params = activity.cameraViewSecond.getLayoutParams(); + if (params != null && params.width > 0) return params.width; } + if (thumbnailContainer != null && thumbnailContainer.getLayoutParams() != null) { + return Math.max(0, thumbnailContainer.getLayoutParams().width); + } + return 0f; + } - try { - Bitmap mainBitmap = activity.mBinding.viewMainPreview.getBitmap(); - if (mainBitmap == null) { - Log.e(TAG, "Failed to get bitmap from TextureView"); - return; - } - } catch (Exception e) { - Log.e(TAG, "Exception getting bitmap: " + e.getMessage()); + private float pipHeight() { + if (thumbnailContainer != null && thumbnailContainer.getHeight() > 0) { + return thumbnailContainer.getHeight(); + } + if (activity != null && activity.cameraViewSecond != null && activity.cameraViewSecond.getHeight() > 0) { + return activity.cameraViewSecond.getHeight(); + } + if (activity != null && activity.cameraViewSecond != null) { + ViewGroup.LayoutParams params = activity.cameraViewSecond.getLayoutParams(); + if (params != null && params.height > 0) return params.height; + } + if (thumbnailContainer != null && thumbnailContainer.getLayoutParams() != null) { + return Math.max(0, thumbnailContainer.getLayoutParams().height); } + return 0f; } /** - * Initialize indicator position based on current main view zoom/pan state - * This ensures the green box shows the correct area of the main view + * Indicator size as currently requested. The measured size is stale right after a + * resize, so the layout parameters win when they are set. */ - private static void initializeIndicatorPosition() { - if (indicatorView == null || activity == null || activity.cameraViewSecond == null || - activity.mBinding == null || activity.mBinding.viewMainPreview == null) { - return; + private float indicatorWidth() { + if (indicatorView == null) return 0f; + ViewGroup.LayoutParams params = indicatorView.getLayoutParams(); + return params != null && params.width > 0 ? params.width : indicatorView.getWidth(); + } + + private float indicatorHeight() { + if (indicatorView == null) return 0f; + ViewGroup.LayoutParams params = indicatorView.getLayoutParams(); + return params != null && params.height > 0 ? params.height : indicatorView.getHeight(); + } + + /** + * The zoom that is actually applied to what the user sees: landscape scales the + * drawer, portrait scales the image matrix. Reading the portrait scale in landscape + * always returned 1.0 and the indicator never matched the real viewport. + */ + private float currentZoomScale() { + if (drawerLayout != null) { + return Math.max(1f, drawerLayout.getScaleX()); } + return Math.max(1f, getCurrentPortraitZoomScale()); + } - // Get current zoom scale from CustomTouchListener - float currentScale = getCurrentPortraitZoomScale(); + /** + * The PiP shows the whole frame and the indicator the part of it that is on screen, + * so the indicator is the PiP size divided by the zoom. + * + * @return the applied indicator size as {width, height} + */ + private float[] applyIndicatorSize(float zoomScale) { + float pipWidth = pipWidth(); + float pipHeight = pipHeight(); + if (indicatorView == null || pipWidth <= 0 || pipHeight <= 0) { + return new float[]{0f, 0f}; + } - // Calculate indicator size based on zoom scale - // When zoomed in, the visible area is smaller, so indicator should be smaller - float indicatorWidth = activity.cameraViewSecond.getWidth() / currentScale; - float indicatorHeight = activity.cameraViewSecond.getHeight() / currentScale; + float scale = Math.max(1f, zoomScale); + float width = Math.max(20f, Math.min(pipWidth / scale, pipWidth)); + float height = Math.max(20f, Math.min(pipHeight / scale, pipHeight)); - // Constrain indicator size - indicatorWidth = Math.max(20f, Math.min(indicatorWidth, activity.cameraViewSecond.getWidth())); - indicatorHeight = Math.max(20f, Math.min(indicatorHeight, activity.cameraViewSecond.getHeight())); + ViewGroup.LayoutParams params = indicatorView.getLayoutParams(); + if (params != null + && (Math.abs(params.width - (int) width) > INDICATOR_SIZE_THRESHOLD + || Math.abs(params.height - (int) height) > INDICATOR_SIZE_THRESHOLD)) { + params.width = (int) width; + params.height = (int) height; + indicatorView.setLayoutParams(params); + // Fires only when the size really changes, so a pinch produces a handful + // of lines. Useful to confirm the indicator tracks the real zoom. + Log.d(TAG, "indicator size " + params.width + "x" + params.height + + " scale=" + scale + " pip=" + pipWidth + "x" + pipHeight); + } - indicatorView.getLayoutParams().width = (int) indicatorWidth; - indicatorView.getLayoutParams().height = (int) indicatorHeight; - indicatorView.requestLayout(); + return new float[]{width, height}; + } - // Position indicator at center initially - resetIndicatorToCenter(); + /** + * Called while the landscape zoom changes so the indicator tracks the viewport. + */ + public void updateIndicatorSize(float zoomScale) { + applyIndicatorSize(zoomScale); } /** * Get current portrait zoom scale from CustomTouchListener */ - private static float getCurrentPortraitZoomScale() { + private float getCurrentPortraitZoomScale() { return com.openterface.AOS.serial.CustomTouchListener.getPortraitZoomScale(); } @SuppressLint("ClickableViewAccessibility") - private static void setupThumbnailDrag() { + private void setupThumbnailDrag() { // Guard against null views if (activity == null || activity.cameraViewSecond == null) { Log.d(TAG, "setupThumbnailDrag: cameraViewSecond not available, skipping"); @@ -450,13 +541,13 @@ private static void setupThumbnailDrag() { activity.cameraViewSecond.setOnTouchListener((v, event) -> { switch (event.getActionMasked()) { case MotionEvent.ACTION_DOWN: + if (indicatorView == null) { + return true; + } // Record the offset between touch point and indicator position // to prevent jumping when starting to drag - if (indicatorView != null) { - dragOffsetX = event.getX() - indicatorView.getX(); - dragOffsetY = event.getY() - indicatorView.getY(); - } - // Update indicator position with offset preserved + dragOffsetX = event.getX() - indicatorView.getX(); + dragOffsetY = event.getY() - indicatorView.getY(); updateIndicatorPosition(event.getX(), event.getY()); syncMainViewPosition( indicatorView.getX() + indicatorView.getWidth() / 2f, @@ -465,6 +556,9 @@ private static void setupThumbnailDrag() { return true; case MotionEvent.ACTION_MOVE: + if (indicatorView == null) { + return true; + } // Dragging - update position with offset preserved updateIndicatorPosition(event.getX(), event.getY()); syncMainViewPosition( @@ -474,30 +568,33 @@ private static void setupThumbnailDrag() { return true; case MotionEvent.ACTION_UP: - setMouseLocation(setMaxViewX, setMaxViewY); + setMouseLocation(); return true; } return false; }); } - private static void setMouseLocation(int setMaxViewX, int setMaxViewY) { - // Delegate to CustomTouchListener to calculate viewport center based on current zoom/pan state + private void setMouseLocation() { + // Always recentre after the drag: the whole point of dragging the viewport is to + // bring that part of the frame under the cursor. This used to be skipped while the + // BT mouse was "connected", and since that flag is a ten second activity window, + // simply moving the mouse left the cursor wherever it happened to be. com.openterface.AOS.serial.CustomTouchListener.moveMouseToViewportCenter(); Log.d(TAG, "setMouseLocation: moved to viewport center"); } - private static void updateIndicatorPosition(float x, float y) { + private void updateIndicatorPosition(float x, float y) { // Guard against null views if (indicatorView == null || activity == null || activity.cameraViewSecond == null) { return; } // Get the dimensions - float pipWidth = activity.cameraViewSecond.getWidth(); - float pipHeight = activity.cameraViewSecond.getHeight(); - float indicatorWidth = indicatorView.getWidth(); - float indicatorHeight = indicatorView.getHeight(); + float pipWidth = pipWidth(); + float pipHeight = pipHeight(); + float indicatorWidth = indicatorWidth(); + float indicatorHeight = indicatorHeight(); // Calculate the position using the drag offset to prevent jumping float indicatorX = x - dragOffsetX; @@ -530,7 +627,7 @@ private static void updateIndicatorPosition(float x, float y) { * @param translateX Horizontal translation of the main view * @param translateY Vertical translation of the main view */ - public static void updateIndicatorFromMainView(float zoomScale, float translateX, float translateY) { + public void updateIndicatorFromMainView(float zoomScale, float translateX, float translateY) { if (indicatorView == null || activity == null || activity.cameraViewSecond == null || activity.mBinding == null || activity.mBinding.viewMainPreview == null) { return; @@ -542,22 +639,9 @@ public static void updateIndicatorFromMainView(float zoomScale, float translateX if (pipWidth <= 0 || pipHeight <= 0) return; // Calculate indicator size based on zoom scale - float indicatorWidth = pipWidth / zoomScale; - float indicatorHeight = pipHeight / zoomScale; - indicatorWidth = Math.max(20f, Math.min(indicatorWidth, pipWidth)); - indicatorHeight = Math.max(20f, Math.min(indicatorHeight, pipHeight)); - - // Update indicator size only when changed significantly (avoid layout thrash during pinch) - final int INDICATOR_SIZE_THRESHOLD = 3; - ViewGroup.LayoutParams params = indicatorView.getLayoutParams(); - int newWidth = (int) indicatorWidth; - int newHeight = (int) indicatorHeight; - if (Math.abs(params.width - newWidth) > INDICATOR_SIZE_THRESHOLD - || Math.abs(params.height - newHeight) > INDICATOR_SIZE_THRESHOLD) { - params.width = newWidth; - params.height = newHeight; - indicatorView.setLayoutParams(params); - } + float[] size = applyIndicatorSize(zoomScale); + float indicatorWidth = size[0]; + float indicatorHeight = size[1]; if (zoomScale <= 1.0f) { // Not zoomed, center the indicator @@ -602,66 +686,49 @@ public static void updateIndicatorFromMainView(float zoomScale, float translateX } - private static void syncMainViewPosition(float thumbX, float thumbY) { + private void syncMainViewPosition(float thumbX, float thumbY) { // Guard against null views - especially drawerLayout which is null in portrait mode if (indicatorView == null || thumbnailContainer == null || activity == null || activity.mBinding == null || activity.mBinding.viewMainPreview == null || activity.cameraViewSecond == null) { - Log.d(TAG, "syncMainViewPosition: views not available, skipping"); + if (DEBUG_LAYOUT_LOG) { + Log.d(TAG, "syncMainViewPosition: views not available, skipping"); + } return; } - int[] location = new int[2]; - indicatorView.getLocationInWindow(location); - - int[] containerLocation = new int[2]; - thumbnailContainer.getLocationInWindow(containerLocation); - - // Computation center coordinate - float centerX = location[0] - containerLocation[0] + indicatorView.getWidth() / 2f; - float centerY = location[1] - containerLocation[1] + indicatorView.getHeight() / 2f; - - Log.d(TAG, "X: " + centerX + ", Y: " + centerY); - - Log.d(TAG, "sizeThumbX: " + thumbX + " sizeThumbY: " + thumbY); - // Calculate where the main view should scroll to + // This runs for every MOVE event while the thumbnail is dragged, so it + // stays free of logging and of work that is not needed for the mapping. ratioX = (float) (activity.mBinding.viewMainPreview.getWidth()) / activity.cameraViewSecond.getWidth(); ratioY = (float) (activity.mBinding.viewMainPreview.getHeight()) / activity.cameraViewSecond.getHeight(); - Log.d(TAG, "ratioX: " + ratioX + ", ratioY: " + ratioY); float mainX = thumbX * ratioX; float mainY = thumbY * ratioY; // In landscape mode, use drawerLayout to scroll the main view if (drawerLayout != null) { - float currentScale = drawerLayout.getScaleX(); - Log.d(TAG, "currentScale: " + currentScale); - Log.d(TAG, "ratioX: " + ratioX + ", ratioY: " + ratioY); - - Log.d(TAG, "mainX: " + mainX + " mainY: " + mainY); - // Update the main view location - Log.d(TAG, "mainX - drawerLayout.getWidth()/2f: " + (mainX - drawerLayout.getWidth()/2f)); - Log.d(TAG, "mainY - drawerLayout.getHeight()/2f: " + (mainY - drawerLayout.getHeight()/2f)); + int scrollX; if((mainX - drawerLayout.getWidth()/2f) < -(drawerLayout.getWidth()/ ratioX)){ - setMaxViewX = (int)(-(drawerLayout.getWidth()/ ratioX)); + scrollX = (int)(-(drawerLayout.getWidth()/ ratioX)); } else if ((mainX - drawerLayout.getWidth()/2f) > (drawerLayout.getWidth()/ ratioX)) { - setMaxViewX = (int)((drawerLayout.getWidth()/ ratioX)); + scrollX = (int)((drawerLayout.getWidth()/ ratioX)); }else { - setMaxViewX = (int)(mainX - drawerLayout.getWidth()/2f); + scrollX = (int)(mainX - drawerLayout.getWidth()/2f); } + int scrollY; if((mainY - drawerLayout.getHeight()/2f) < -(drawerLayout.getHeight()/ ratioY)){ - setMaxViewY = (int)(-(drawerLayout.getHeight()/ ratioY)); + scrollY = (int)(-(drawerLayout.getHeight()/ ratioY)); } else if ((mainY - drawerLayout.getHeight()/2f) > (drawerLayout.getHeight()/ ratioY)) { - setMaxViewY = (int)((drawerLayout.getHeight()/ ratioY)); + scrollY = (int)((drawerLayout.getHeight()/ ratioY)); }else { - setMaxViewY = (int)(mainY - drawerLayout.getHeight()/2f); + scrollY = (int)(mainY - drawerLayout.getHeight()/2f); + } + if (DEBUG_LAYOUT_LOG) { + Log.d(TAG, "syncMainViewPosition: scrollX=" + scrollX + + " scrollY=" + scrollY); } - Log.d(TAG, "setMaxViewX: " + setMaxViewX + " setMaxViewY: " + setMaxViewY); - drawerLayout.scrollTo( - (setMaxViewX), - (setMaxViewY) - ); + drawerLayout.scrollTo(scrollX, scrollY); } else { // In portrait mode, use CustomTouchListener's zoom state float currentZoomScale = com.openterface.AOS.serial.CustomTouchListener.getPortraitZoomScale(); @@ -726,13 +793,11 @@ private static void syncMainViewPosition(float thumbX, float thumbY) { com.openterface.AOS.serial.CustomTouchListener.setPortraitPan(newTranslateX, newTranslateY); com.openterface.AOS.serial.CustomTouchListener.applyCurrentPortraitTransform(); - // Store for setMouseLocation calculation - setMaxViewX = (int) newTranslateX; - setMaxViewY = (int) newTranslateY; - - Log.d(TAG, "Portrait mode: normalizedX=" + normalizedX + " normalizedY=" + normalizedY - + " translateX=" + newTranslateX + " translateY=" + newTranslateY - + " viewWidth=" + viewWidth + " viewHeight=" + viewHeight); + if (DEBUG_LAYOUT_LOG) { + Log.d(TAG, "Portrait mode: normalizedX=" + normalizedX + " normalizedY=" + normalizedY + + " translateX=" + newTranslateX + " translateY=" + newTranslateY + + " viewWidth=" + viewWidth + " viewHeight=" + viewHeight); + } } } diff --git a/app/src/main/java/com/openterface/AOS/fragment/SettingsFloatingFragment.java b/app/src/main/java/com/openterface/AOS/fragment/SettingsFloatingFragment.java index 9065a78..07901f0 100644 --- a/app/src/main/java/com/openterface/AOS/fragment/SettingsFloatingFragment.java +++ b/app/src/main/java/com/openterface/AOS/fragment/SettingsFloatingFragment.java @@ -140,13 +140,9 @@ public interface SettingsCallback { int getSoundVolume(); boolean isVibrateEnabled(); int getVibrateIntensity(); - int getScrollSpeed(); - int getMouseSpeed(); void setSoundEnabled(boolean enabled); void setSoundVolume(int volume); void setVibrateEnabled(boolean enabled); void setVibrateIntensity(int intensity); - void setScrollSpeed(int speed); - void setMouseSpeed(int speed); } } \ No newline at end of file diff --git a/app/src/main/java/com/openterface/AOS/jni/KeymodJNI.java b/app/src/main/java/com/openterface/AOS/jni/KeymodJNI.java index 7da62f5..023f53b 100644 --- a/app/src/main/java/com/openterface/AOS/jni/KeymodJNI.java +++ b/app/src/main/java/com/openterface/AOS/jni/KeymodJNI.java @@ -22,6 +22,8 @@ public class KeymodJNI { public static final int MS_BTN_LEFT = 0x01; public static final int MS_BTN_RIGHT = 0x02; public static final int MS_BTN_MIDDLE = 0x04; + public static final int MS_BTN_BACK = 0x08; + public static final int MS_BTN_FORWARD = 0x10; // Packet sizes public static final int PKT_KEYBOARD_SIZE = 14; diff --git a/app/src/main/java/com/openterface/AOS/serial/BtMouseInputHandler.java b/app/src/main/java/com/openterface/AOS/serial/BtMouseInputHandler.java new file mode 100644 index 0000000..e738afb --- /dev/null +++ b/app/src/main/java/com/openterface/AOS/serial/BtMouseInputHandler.java @@ -0,0 +1,721 @@ +package com.openterface.AOS.serial; + +import android.os.Handler; +import android.util.DisplayMetrics; +import android.util.Log; +import android.view.InputDevice; +import android.view.MotionEvent; +import android.view.View; + +import com.openterface.AOS.activity.MainActivity; +import com.openterface.AOS.target.HidManager; +import com.openterface.AOS.target.MouseManager; +import com.openterface.AOS.target.MouseModeManager; + +/** + * Handles Bluetooth mouse input events. + * Responsible for: + * - Processing GenericMotionEvent from BT mouse + * - Converting BT mouse coordinates to HID coordinates + * - Tracking virtual HID state for edge accumulation + * - Button press/release handling + * - Connection state management + */ +public class BtMouseInputHandler { + private static final String TAG = "OP-BT-MOUSE"; + + // ===== Constants ===== + public static final int HID_LOGICAL_MAX = 4095; // CH9329 uses 12-bit coordinates + private static final float EDGE_THRESHOLD = 80f; // pixels from edge to consider "at edge" + private static final long BT_MOUSE_DISCONNECT_TIMEOUT_MS = 10000; + private static final long CURSOR_RESTORE_DELAY_MS = 2000; + private static final long MIN_BUTTON_HOLD_MS = 50; + + // ===== State ===== + private static boolean btMouseConnected = false; + private static OnBtMouseConnectionListener btMouseConnectionListener; + + // Button state + private static boolean btMouseLeftDown = false; + private static boolean btMouseRightDown = false; + private static boolean btMouseMiddleDown = false; + private static boolean btMouseDragging = false; + private static long btMouseLastPressTime = 0; + + // Position tracking + private static float btMouseLastX = 0f; + private static float btMouseLastY = 0f; + private static float btMouseLastScreenX = 0f; + private static float btMouseLastScreenY = 0f; + private static boolean wasAtEdge = false; + + // Device ID tracking to distinguish BT mouse from system cursor + private static int btMouseDeviceId = -1; + private static boolean btMouseDeviceIdSet = false; + + // Virtual HID coordinate system + private static float virtualHidX = HID_LOGICAL_MAX / 2f; + private static float virtualHidY = HID_LOGICAL_MAX / 2f; + private static boolean virtualHidInitialized = false; + + // Track last sent HID coordinates to avoid duplicates + private static float lastSentHidX = -1f; + private static float lastSentHidY = -1f; + + // Relative mode tracking + private static float btMouseRelPrevX = 0f; + private static float btMouseRelPrevY = 0f; + private static boolean btMouseRelInitialized = false; + + // Touch ignore window + private static volatile long btMouseLastEventTime = 0; + private static final long TOUCH_IGNORE_WINDOW_MS = 300; + + // Disconnect/cursor restore handlers + private static Handler btMouseDisconnectHandler; + private static Runnable btMouseDisconnectRunnable; + private static Handler btMouseCursorRestoreHandler; + private static Runnable btMouseCursorRestoreRunnable; + + // Callback for UI updates (black border, cursor visibility) + private static Callbacks callbacks; + + // Reference to activity for view lookups + private static MainActivity activity; + + // ===== Callback interface ===== + public interface Callbacks { + void onBlackBorderVisibilityChanged(boolean show); + void onSystemGestureExclusionChanged(boolean enabled); + void onBlackBorderHintUpdate(float cursorX, float cursorY); + } + + /** + * Callback interface for Bluetooth mouse connection state changes. + */ + public interface OnBtMouseConnectionListener { + void onBtMouseConnected(boolean connected); + } + + // ===== Initialization ===== + + public static void init(MainActivity mainActivity, Callbacks cb) { + activity = mainActivity; + callbacks = cb; + } + + // ===== Public API ===== + + public static void setOnBtMouseConnectionListener(OnBtMouseConnectionListener listener) { + btMouseConnectionListener = listener; + } + + public static boolean isBtMouseConnected() { + return btMouseConnected; + } + + public static boolean isBtMouseButtonPressed() { + return btMouseLeftDown || btMouseRightDown || btMouseMiddleDown || btMouseDragging; + } + + public static boolean shouldIgnoreTouch() { + return isBtMouseButtonPressed(); + } + + public static float getBtMouseLastX() { + return btMouseLastX; + } + + public static float getBtMouseLastY() { + return btMouseLastY; + } + + public static void setBtMouseLastPosition(float x, float y) { + btMouseLastX = x; + btMouseLastY = y; + } + + public static float getVirtualHidX() { + return virtualHidX; + } + + public static float getVirtualHidY() { + return virtualHidY; + } + + /** + * Handle GenericMotionEvent from Android input system. + * @return true if event was consumed + */ + public static boolean handleGenericMotionEvent(MotionEvent event) { + int source = event.getSource(); + int action = event.getAction(); + + // Check if this event is from a mouse device + boolean isMouseDevice = (source & InputDevice.SOURCE_MOUSE) == InputDevice.SOURCE_MOUSE; + + // Also check tool type + boolean hasMouseTool = false; + for (int i = 0; i < event.getPointerCount(); i++) { + if (event.getToolType(i) == MotionEvent.TOOL_TYPE_MOUSE) { + hasMouseTool = true; + break; + } + } + + if (!isMouseDevice && !hasMouseTool) { + return false; + } + + // Mark BT mouse as connected + setBtMouseConnected(true); + resetBtMouseDisconnectTimeout(); + btMouseLastEventTime = System.currentTimeMillis(); + + // Log event info + Log.d(TAG, "BT Mouse event: action=" + action + " source=0x" + Integer.toHexString(source)); + + // Handle different event types + if (action == MotionEvent.ACTION_SCROLL) { + return handleScroll(event); + } else if (action == MotionEvent.ACTION_BUTTON_PRESS) { + Log.d(TAG, "Dispatching BUTTON_PRESS to handleButton"); + return handleButton(event, true); + } else if (action == MotionEvent.ACTION_BUTTON_RELEASE) { + Log.d(TAG, "Dispatching BUTTON_RELEASE to handleButton"); + return handleButton(event, false); + } else if (action == MotionEvent.ACTION_HOVER_MOVE || action == MotionEvent.ACTION_MOVE) { + return handleMove(event); + } + + return true; + } + + // ===== Event handlers ===== + + private static boolean handleScroll(MotionEvent event) { + float scrollAmount = event.getAxisValue(MotionEvent.AXIS_VSCROLL); + if (scrollAmount != 0) { + HidManager.handleTwoFingerPanSlideUpDown(scrollAmount); + } + return true; + } + + private static boolean handleMove(MotionEvent event) { + // Track the BT mouse device ID on first move event + // BUT only if not already set by button event (button event is more reliable) + if (!btMouseDeviceIdSet) { + btMouseDeviceId = event.getDeviceId(); + btMouseDeviceIdSet = true; + Log.d(TAG, "BT mouse device ID set from move event: " + btMouseDeviceId); + } else if (event.getDeviceId() != btMouseDeviceId) { + // Ignore move events from other devices (e.g., system cursor) + Log.d(TAG, "Ignoring move from device " + event.getDeviceId() + " (expected " + btMouseDeviceId + ")"); + return false; + } + + // ===== Process historical positions ===== + // Android batches move events: multiple hardware reports are merged into one MotionEvent. + // Historical positions (getHistoricalX/Y) must be processed to avoid cursor jumping. + // This is especially critical during drag — without it, only the final position is sent, + // making window resize/drag show only start and end with no intermediate feedback. + boolean isDragging = btMouseDragging && (btMouseLeftDown || btMouseRightDown || btMouseMiddleDown); + int historySize = event.getHistorySize(); + + if (isDragging && historySize > 0) { + int buttonState = event.getButtonState(); + boolean anyButtonDown = (buttonState & MotionEvent.BUTTON_PRIMARY) != 0 + || (buttonState & MotionEvent.BUTTON_SECONDARY) != 0 + || (buttonState & MotionEvent.BUTTON_TERTIARY) != 0; + int dragButtonState = buttonState; + if (!anyButtonDown) { + dragButtonState = 0; + if (btMouseLeftDown) dragButtonState |= MotionEvent.BUTTON_PRIMARY; + if (btMouseRightDown) dragButtonState |= MotionEvent.BUTTON_SECONDARY; + if (btMouseMiddleDown) dragButtonState |= MotionEvent.BUTTON_TERTIARY; + } + + for (int i = 0; i < historySize; i++) { + float histScreenX = event.getHistoricalX(i); + float histScreenY = event.getHistoricalY(i); + float[] histViewCoords = toViewCoords(histScreenX, histScreenY); + sendBtMouseDragMove(histViewCoords[0], histViewCoords[1], dragButtonState); + btMouseLastX = histViewCoords[0]; + btMouseLastY = histViewCoords[1]; + btMouseLastScreenX = histScreenX; + btMouseLastScreenY = histScreenY; + } + Log.d(TAG, "handleMove: processed " + historySize + " historical drag positions"); + } + + float screenX = event.getX(); + float screenY = event.getY(); + + // Get screen dimensions for edge detection + float screenWidthPx = 0, screenHeightPx = 0; + if (activity != null) { + DisplayMetrics metrics = new DisplayMetrics(); + activity.getWindowManager().getDefaultDisplay().getRealMetrics(metrics); + screenWidthPx = metrics.widthPixels; + screenHeightPx = metrics.heightPixels; + } + + // Detect cursor at screen edges + boolean atLeft = screenX <= EDGE_THRESHOLD; + boolean atRight = screenX >= screenWidthPx - EDGE_THRESHOLD; + boolean atTop = screenY <= EDGE_THRESHOLD; + boolean atBottom = screenY >= screenHeightPx - EDGE_THRESHOLD; + boolean nowAtEdge = atLeft || atRight || atTop || atBottom; + + // Get relative axis values + float relX = event.getAxisValue(MotionEvent.AXIS_RELATIVE_X); + float relY = event.getAxisValue(MotionEvent.AXIS_RELATIVE_Y); + + // Convert to view-local coordinates + float[] viewCoords = toViewCoords(screenX, screenY); + float cursorX = viewCoords[0]; + float cursorY = viewCoords[1]; + + // Update tracked position + btMouseLastX = cursorX; + btMouseLastY = cursorY; + + int buttonState = event.getButtonState(); + boolean anyButtonDown = (buttonState & MotionEvent.BUTTON_PRIMARY) != 0 + || (buttonState & MotionEvent.BUTTON_SECONDARY) != 0 + || (buttonState & MotionEvent.BUTTON_TERTIARY) != 0; + + // If dragging (any button held down), send drag move + // Some BT mice don't report button state in move events during drag, + // so we also check cached button state + isDragging = btMouseDragging && (btMouseLeftDown || btMouseRightDown || btMouseMiddleDown); + Log.d(TAG, "handleMove drag check: btMouseDragging=" + btMouseDragging + + " leftDown=" + btMouseLeftDown + " rightDown=" + btMouseRightDown + + " middleDown=" + btMouseMiddleDown + " isDragging=" + isDragging); + + if (isDragging) { + // Reconstruct buttonState from cached state if event doesn't have it + int dragButtonState = buttonState; + if (!anyButtonDown) { + dragButtonState = 0; + if (btMouseLeftDown) dragButtonState |= MotionEvent.BUTTON_PRIMARY; + if (btMouseRightDown) dragButtonState |= MotionEvent.BUTTON_SECONDARY; + if (btMouseMiddleDown) dragButtonState |= MotionEvent.BUTTON_TERTIARY; + } + sendBtMouseDragMove(cursorX, cursorY, dragButtonState); + wasAtEdge = nowAtEdge; + btMouseLastScreenX = screenX; + btMouseLastScreenY = screenY; + return true; + } + + // Normal move + Log.d(TAG, "handleMove: NORMAL move path"); + float viewW = CoordinateConverter.getTouchViewWidth(); + float viewH = CoordinateConverter.getTouchViewHeight(); + + // Get screen dimensions for debugging + android.util.DisplayMetrics metrics = new android.util.DisplayMetrics(); + if (activity != null && activity.getWindowManager() != null) { + activity.getWindowManager().getDefaultDisplay().getRealMetrics(metrics); + } + Log.d(TAG, "handleMove: screenDimensions=(" + metrics.widthPixels + "," + metrics.heightPixels + + ") viewDimensions=(" + viewW + "," + viewH + ")"); + + // Update black border hint + if (callbacks != null) { + callbacks.onBlackBorderHintUpdate(cursorX, cursorY); + } + + if (MouseModeManager.isAbsoluteMode()) { + handleAbsoluteMode(cursorX, cursorY, viewW, viewH, + screenX, screenY, screenWidthPx, screenHeightPx, + relX, relY, atLeft, atRight, atTop, atBottom); + } else { + handleRelativeMode(cursorX, cursorY, relX, relY, nowAtEdge); + } + + wasAtEdge = nowAtEdge; + btMouseLastScreenX = screenX; + btMouseLastScreenY = screenY; + return true; + } + + private static void handleAbsoluteMode(float cursorX, float cursorY, + float viewW, float viewH, float screenX, float screenY, + float screenW, float screenH, float relX, float relY, + boolean atLeft, boolean atRight, boolean atTop, boolean atBottom) { + + // Step 1: Apply inverse portrait zoom/pan transform to get content coordinates + // This handles the case where video is zoomed/panned in portrait mode + float[] contentCoords = CoordinateConverter.mapToContentCoords( + cursorX, cursorY, viewW, viewH, + CoordinateConverter.isPortraitMode(), + 1.0f, 0f, 0f); + float contentX = contentCoords[0]; + float contentY = contentCoords[1]; + + // Step 2: Get video rect for coordinate conversion + float bufferW = 0, bufferH = 0; + if (activity != null) { + bufferW = activity.getPreviewWidth(); + bufferH = activity.getPreviewHeight(); + } + float[] videoRect = CoordinateConverter.getVideoRectInView(viewW, viewH, bufferW, bufferH); + + // Step 3: Map content coordinates to HID [0, 4095]. + // Content coords are used so that only the video area maps to the target. + // Black bar areas are clamped to edges — cursor stays at boundary, no jump. + float[] hidCoords = CoordinateConverter.normalizeBtMouseToHid(contentX, contentY, viewW, viewH, videoRect); + virtualHidX = hidCoords[0]; + virtualHidY = hidCoords[1]; + virtualHidInitialized = true; + + // Send HID data if changed + int hidX = (int) virtualHidX; + int hidY = (int) virtualHidY; + + // DETAILED DEBUG: Log full coordinate chain + Log.d(TAG, "handleAbsoluteMode: " + + "screen=(" + screenX + "," + screenY + ")" + + " viewCoords=(" + cursorX + "," + cursorY + ")" + + " contentCoords=(" + contentX + "," + contentY + ")" + + " viewSize=(" + viewW + "," + viewH + ")" + + " videoRect=(" + videoRect[0] + "," + videoRect[1] + "," + videoRect[2] + "," + videoRect[3] + ")" + + " hidCoords=(" + hidX + "," + hidY + ")"); + + if (hidX != lastSentHidX || hidY != lastSentHidY) { + HidManager.sendBtMouseHidAbsData(hidX, hidY); + lastSentHidX = hidX; + lastSentHidY = hidY; + } + } + + private static void handleRelativeMode(float cursorX, float cursorY, + float relX, float relY, boolean nowAtEdge) { + + Log.d(TAG, "handleRelativeMode: cursor=(" + cursorX + "," + cursorY + + ") rel=(" + relX + "," + relY + ") atEdge=" + nowAtEdge); + + // First sample after (re)connect: only learn the baseline. With 0f as the + // previous position the absolute cursor coordinate became a delta and the + // host cursor jumped up to 127px on the first movement. + if (!btMouseRelInitialized) { + btMouseRelPrevX = cursorX; + btMouseRelPrevY = cursorY; + btMouseRelInitialized = true; + Log.d(TAG, "handleRelativeMode: initialized baseline at (" + cursorX + "," + cursorY + ")"); + return; + } + + if (nowAtEdge && (relX != 0 || relY != 0)) { + float scaledRelX = relX * 2.0f; + float scaledRelY = relY * 2.0f; + HidManager.sendHexRelData("SecNullData", + btMouseRelPrevX + scaledRelX, + btMouseRelPrevY + scaledRelY, + btMouseRelPrevX, btMouseRelPrevY); + btMouseRelPrevX += scaledRelX; + btMouseRelPrevY += scaledRelY; + } else { + HidManager.sendHexRelData("SecNullData", cursorX, cursorY, + btMouseRelPrevX, btMouseRelPrevY); + btMouseRelPrevX = cursorX; + btMouseRelPrevY = cursorY; + } + } + + private static boolean handleButton(MotionEvent event, boolean pressed) { + Log.d(TAG, "handleButton ENTER: pressed=" + pressed + " deviceId=" + event.getDeviceId() + + " btMouseDeviceId=" + btMouseDeviceId + " btMouseDeviceIdSet=" + btMouseDeviceIdSet + + " buttonState=" + event.getButtonState() + + " relX=" + event.getAxisValue(MotionEvent.AXIS_RELATIVE_X) + + " relY=" + event.getAxisValue(MotionEvent.AXIS_RELATIVE_Y) + + " pointerCount=" + event.getPointerCount()); + + // Track the BT mouse device ID from button events (reliable source) + // Button events only come from actual mouse devices, not system cursor + if (!btMouseDeviceIdSet) { + btMouseDeviceId = event.getDeviceId(); + btMouseDeviceIdSet = true; + Log.d(TAG, "BT mouse device ID set from button event: " + btMouseDeviceId); + } + // Only accept button events from the BT mouse device + else if (event.getDeviceId() != btMouseDeviceId) { + Log.d(TAG, "Ignoring button event from device " + event.getDeviceId() + " (expected " + btMouseDeviceId + ")"); + return false; + } + + int buttonState = event.getButtonState(); + + // Always use event's own coordinates - convert to view coordinates + // This avoids pollution from system cursor movements in black bars + float[] viewCoords = toViewCoords(event.getX(), event.getY()); + float x = viewCoords[0]; + float y = viewCoords[1]; + + if (pressed) { + if ((buttonState & MotionEvent.BUTTON_PRIMARY) != 0) { + btMouseLeftDown = true; + btMouseDragging = true; + } + if ((buttonState & MotionEvent.BUTTON_SECONDARY) != 0) { + btMouseRightDown = true; + btMouseDragging = true; + } + if ((buttonState & MotionEvent.BUTTON_TERTIARY) != 0) { + btMouseMiddleDown = true; + btMouseDragging = true; + } + + // Send button press with position + sendBtMouseButtonClick(x, y); + } else { + btMouseLeftDown = false; + btMouseRightDown = false; + btMouseMiddleDown = false; + btMouseDragging = false; + + // Send button release + releaseBtMouseButtons(x, y); + } + + return true; + } + + // ===== Send methods ===== + + private static void sendBtMouseButtonClick(float x, float y) { + String buttonKey; + if (btMouseLeftDown) { + buttonKey = "SecLeftData"; + } else if (btMouseRightDown) { + buttonKey = "SecRightData"; + } else if (btMouseMiddleDown) { + buttonKey = "SecMiddleData"; + } else { + return; + } + + // Apply portrait zoom/pan transform first, then normalize to HID + float viewW = CoordinateConverter.getTouchViewWidth(); + float viewH = CoordinateConverter.getTouchViewHeight(); + + // Step 1: Apply inverse portrait zoom/pan transform + float[] contentCoords = CoordinateConverter.mapToContentCoords( + x, y, viewW, viewH, + CoordinateConverter.isPortraitMode(), + 1.0f, 0f, 0f); + + // Step 2: Normalize to HID coordinates + float bufferW = 0, bufferH = 0; + if (activity != null) { + bufferW = activity.getPreviewWidth(); + bufferH = activity.getPreviewHeight(); + } + float[] videoRect = CoordinateConverter.getVideoRectInView(viewW, viewH, bufferW, bufferH); + float[] hidCoords = CoordinateConverter.normalizeBtMouseToHid(contentCoords[0], contentCoords[1], viewW, viewH, videoRect); + float hidX = hidCoords[0]; + float hidY = hidCoords[1]; + + // Use calculated HID coordinates from button event + if (MouseModeManager.isAbsoluteMode()) { + HidManager.sendBtMouseHidAbsButtonClickData(buttonKey, hidX, hidY); + } else { + HidManager.sendHexRelData(buttonKey, 0, 0, 0, 0); + } + } + + private static void sendBtMouseDragMove(float x, float y, int buttonState) { + String buttonKey; + if ((buttonState & MotionEvent.BUTTON_PRIMARY) != 0) { + buttonKey = "SecLeftData"; + } else if ((buttonState & MotionEvent.BUTTON_SECONDARY) != 0) { + buttonKey = "SecRightData"; + } else if ((buttonState & MotionEvent.BUTTON_TERTIARY) != 0) { + buttonKey = "SecMiddleData"; + } else { + return; + } + + float viewW = CoordinateConverter.getTouchViewWidth(); + float viewH = CoordinateConverter.getTouchViewHeight(); + + if (MouseModeManager.isAbsoluteMode()) { + // Apply portrait zoom/pan transform first + float[] contentCoords = CoordinateConverter.mapToContentCoords( + x, y, viewW, viewH, + CoordinateConverter.isPortraitMode(), + 1.0f, 0f, 0f); + + // Then normalize to HID coordinates + float bufferW = 0, bufferH = 0; + if (activity != null) { + bufferW = activity.getPreviewWidth(); + bufferH = activity.getPreviewHeight(); + } + float[] videoRect = CoordinateConverter.getVideoRectInView(viewW, viewH, bufferW, bufferH); + float[] hidCoords = CoordinateConverter.normalizeBtMouseToHid(contentCoords[0], contentCoords[1], viewW, viewH, videoRect); + virtualHidX = hidCoords[0]; + virtualHidY = hidCoords[1]; + + HidManager.sendBtMouseHidAbsButtonClickData(buttonKey, virtualHidX, virtualHidY); + } else { + // Relative mode: a drag frame must carry the same delta a plain move would. + // Sending (0,0) froze the host cursor while the button was held, so a BT + // mouse drag looked like "nothing happens". + if (!btMouseRelInitialized) { + btMouseRelPrevX = x; + btMouseRelPrevY = y; + btMouseRelInitialized = true; + } + HidManager.sendHexRelData(buttonKey, x, y, btMouseRelPrevX, btMouseRelPrevY); + btMouseRelPrevX = x; + btMouseRelPrevY = y; + } + } + + private static void releaseBtMouseButtons(float x, float y) { + // Runs on every button release, so no stack trace here. + Log.d(TAG, "releaseBtMouseButtons x=" + x + " y=" + y); + if (MouseModeManager.isAbsoluteMode()) { + // Apply portrait zoom/pan transform first, then normalize to HID + float viewW = CoordinateConverter.getTouchViewWidth(); + float viewH = CoordinateConverter.getTouchViewHeight(); + + // Step 1: Apply inverse portrait zoom/pan transform + float[] contentCoords = CoordinateConverter.mapToContentCoords( + x, y, viewW, viewH, + CoordinateConverter.isPortraitMode(), + 1.0f, 0f, 0f); + + // Step 2: Normalize to HID coordinates + float bufferW = 0, bufferH = 0; + if (activity != null) { + bufferW = activity.getPreviewWidth(); + bufferH = activity.getPreviewHeight(); + } + float[] videoRect = CoordinateConverter.getVideoRectInView(viewW, viewH, bufferW, bufferH); + float[] hidCoords = CoordinateConverter.normalizeBtMouseToHid(contentCoords[0], contentCoords[1], viewW, viewH, videoRect); + + Log.d(TAG, "releaseBtMouseButtons: viewCoords=(" + x + "," + y + + ") contentCoords=(" + contentCoords[0] + "," + contentCoords[1] + + ") hidCoords=(" + hidCoords[0] + "," + hidCoords[1] + ")"); + + HidManager.sendBtMouseHidAbsReleaseData(hidCoords[0], hidCoords[1]); + } else { + HidManager.releaseMSRelData(); + } + } + + // ===== Connection state ===== + + private static void setBtMouseConnected(boolean connected) { + if (btMouseConnected == connected) { + return; + } + btMouseConnected = connected; + Log.d(TAG, "BT Mouse connection state changed: " + connected); + + if (callbacks != null) { + callbacks.onBlackBorderVisibilityChanged(connected); + callbacks.onSystemGestureExclusionChanged(connected); + } + + if (connected) { + if (btMouseCursorRestoreHandler != null && btMouseCursorRestoreRunnable != null) { + btMouseCursorRestoreHandler.removeCallbacks(btMouseCursorRestoreRunnable); + } + if (btMouseConnectionListener != null) { + btMouseConnectionListener.onBtMouseConnected(true); + } + } else { + // Delayed disconnect notification + if (btMouseCursorRestoreHandler == null) { + btMouseCursorRestoreHandler = new Handler(android.os.Looper.getMainLooper()); + } + if (btMouseCursorRestoreRunnable == null) { + btMouseCursorRestoreRunnable = () -> { + if (btMouseConnectionListener != null) { + btMouseConnectionListener.onBtMouseConnected(false); + } + }; + } + btMouseCursorRestoreHandler.removeCallbacks(btMouseCursorRestoreRunnable); + btMouseCursorRestoreHandler.postDelayed(btMouseCursorRestoreRunnable, CURSOR_RESTORE_DELAY_MS); + } + } + + public static void cancelBtMouseCursorRestore() { + if (btMouseCursorRestoreHandler != null && btMouseCursorRestoreRunnable != null) { + btMouseCursorRestoreHandler.removeCallbacks(btMouseCursorRestoreRunnable); + } + } + + private static void resetBtMouseDisconnectTimeout() { + if (btMouseDisconnectHandler == null) { + btMouseDisconnectHandler = new Handler(android.os.Looper.getMainLooper()); + btMouseDisconnectRunnable = () -> { + setBtMouseConnected(false); + btMouseLeftDown = false; + btMouseRightDown = false; + btMouseMiddleDown = false; + btMouseDragging = false; + btMouseRelInitialized = false; + CoordinateConverter.resetLearnedRange(); + virtualHidInitialized = false; + lastSentHidX = -1f; + lastSentHidY = -1f; + // Reset device ID tracking + btMouseDeviceId = -1; + btMouseDeviceIdSet = false; + }; + } + btMouseDisconnectHandler.removeCallbacks(btMouseDisconnectRunnable); + btMouseDisconnectHandler.postDelayed(btMouseDisconnectRunnable, BT_MOUSE_DISCONNECT_TIMEOUT_MS); + + if (btMouseCursorRestoreHandler != null && btMouseCursorRestoreRunnable != null) { + btMouseCursorRestoreHandler.removeCallbacks(btMouseCursorRestoreRunnable); + } + } + + // ===== Coordinate conversion ===== + + private static float[] toViewCoords(float screenX, float screenY) { + if (activity != null && activity.mBinding != null + && activity.mBinding.viewMainPreview != null) { + // Use getLocationInWindow - returns position relative to window content area + // This matches the absolute screen position (verified via dumpsys) + int[] loc = new int[2]; + activity.mBinding.viewMainPreview.getLocationInWindow(loc); + + // Calculate view-relative coordinates + float viewX = screenX - loc[0]; + float viewY = screenY - loc[1]; + + // Detailed logging for coordinate debugging + Log.d(TAG, "toViewCoords: screen=(" + screenX + "," + screenY + + ") viewLocInWindow=(" + loc[0] + "," + loc[1] + + ") viewCoords=(" + viewX + "," + viewY + ")"); + return new float[]{viewX, viewY}; + } + return new float[]{screenX, screenY}; + } + + // ===== Reset ===== + + public static void reset() { + btMouseLeftDown = false; + btMouseRightDown = false; + btMouseMiddleDown = false; + btMouseDragging = false; + virtualHidX = HID_LOGICAL_MAX / 2f; + virtualHidY = HID_LOGICAL_MAX / 2f; + virtualHidInitialized = false; + lastSentHidX = -1f; + lastSentHidY = -1f; + CoordinateConverter.resetLearnedRange(); + btMouseDeviceId = -1; + btMouseDeviceIdSet = false; + } +} diff --git a/app/src/main/java/com/openterface/AOS/serial/CH9329Function.java b/app/src/main/java/com/openterface/AOS/serial/CH9329Function.java index 9458fdc..5cc3d0d 100644 --- a/app/src/main/java/com/openterface/AOS/serial/CH9329Function.java +++ b/app/src/main/java/com/openterface/AOS/serial/CH9329Function.java @@ -30,6 +30,13 @@ public class CH9329Function { private static final String TAG = "OP-SERIAL"; private static UsbDeviceManager usbDeviceManager; + /** + * 逐包 hex 日志开关。 + * 该日志位于每次 HID 报文下发的热路径上(鼠标拖动时 60~120 次/秒),常开会带来逐包 + * String 重组与日志输出开销。排查键鼠报文时临时改为 true 即可。 + */ + public static final boolean DEBUG_SEND_LOG = false; + public static String makeChecksum(String data) { int total = 0; @@ -58,6 +65,9 @@ public static byte[] hexStringToByteArray(String ByteData) { } public static void checkSendLogData(String sendKBData) { + if (!DEBUG_SEND_LOG) { + return; + } StringBuilder check_send_data = new StringBuilder(); for (int i = 0; i < sendKBData.length(); i += 2) { if (i + 2 <= sendKBData.length()) { @@ -70,6 +80,9 @@ public static void checkSendLogData(String sendKBData) { } public static void ReleaseSendLogData(String releaseKBData) { + if (!DEBUG_SEND_LOG) { + return; + } StringBuilder check_release_data = new StringBuilder(); for (int i = 0; i < releaseKBData.length(); i += 2) { check_release_data.append(releaseKBData.substring(i, i + 2)).append(" "); diff --git a/app/src/main/java/com/openterface/AOS/serial/CoordinateConverter.java b/app/src/main/java/com/openterface/AOS/serial/CoordinateConverter.java new file mode 100644 index 0000000..76079f1 --- /dev/null +++ b/app/src/main/java/com/openterface/AOS/serial/CoordinateConverter.java @@ -0,0 +1,344 @@ +package com.openterface.AOS.serial; + +import android.util.Log; +import android.view.View; + +import com.openterface.AOS.R; +import com.openterface.AOS.activity.MainActivity; +import com.openterface.AOS.target.MouseManager; + +/** + * Coordinate conversion utilities. + * Handles all coordinate transformations between: + * - Screen coordinates (Android system) + * - View coordinates (TextureView) + * - Content coordinates (viewport-aware) + * - HID coordinates (0-4095 for CH9329) + * + * This is a pure utility class - no Android dependencies beyond basic types. + */ +public class CoordinateConverter { + private static final String TAG = "OP-COORD"; + + // ===== Activity reference for view lookups ===== + private static MainActivity activity; + private static boolean isPortraitMode = false; + + // ===== Portrait mode zoom/pan state ===== + // Single source of truth is ZoomPanManager: the accessors below delegate to it so + // coordinate mapping can never drift away from the transform actually applied. + + // ===== BT Mouse learned range ===== + private static float learnedMinX = Float.MAX_VALUE; + private static float learnedMaxX = Float.MIN_VALUE; + private static float learnedMinY = Float.MAX_VALUE; + private static float learnedMaxY = Float.MIN_VALUE; + private static boolean rangeLearned = false; + + // Track view size to detect when it changes (triggers learned range reset) + private static float lastViewWidth = 0; + private static float lastViewHeight = 0; + + // CH9329 protocol uses 12-bit coordinates (0..4095) for absolute mode + public static final int HID_LOGICAL_MAX = 4095; + + private CoordinateConverter() {} // Utility class + + // ===== Initialization ===== + + /** + * Initialize with activity reference for view lookups. + */ + public static void init(MainActivity mainActivity, boolean portrait) { + activity = mainActivity; + isPortraitMode = portrait; + } + + /** + * Set portrait mode state. + */ + public static void setPortraitMode(boolean portrait) { + isPortraitMode = portrait; + } + + /** + * Check if device is in portrait mode. + */ + public static boolean isPortraitMode() { + return isPortraitMode; + } + + /** + * Get the touch target view width (main preview TextureView). + */ + public static float getTouchViewWidth() { + if (activity != null && activity.mBinding != null + && activity.mBinding.viewMainPreview != null) { + float w = activity.mBinding.viewMainPreview.getWidth(); + if (w > 0) return w; + } + return MouseManager.screenWidth; + } + + /** + * Get the touch target view height (main preview TextureView). + */ + public static float getTouchViewHeight() { + if (activity != null && activity.mBinding != null + && activity.mBinding.viewMainPreview != null) { + float h = activity.mBinding.viewMainPreview.getHeight(); + if (h > 0) return h; + } + return MouseManager.screenHeight; + } + + // ===== Portrait zoom/pan getters/setters ===== + + public static float getPortraitZoomScale() { + return ZoomPanManager.getPortraitZoomScale(); + } + + public static void setPortraitPan(float translateX, float translateY) { + ZoomPanManager.setPortraitPan(translateX, translateY); + } + + public static void setPortraitZoomScale(float scale) { + ZoomPanManager.setPortraitZoomScale(scale); + } + + public static float getPortraitTranslateX() { + return ZoomPanManager.getPortraitTranslateX(); + } + + public static float getPortraitTranslateY() { + return ZoomPanManager.getPortraitTranslateY(); + } + + public static void resetPortraitZoom() { + ZoomPanManager.reset(); + } + + // ===== Video rect calculation ===== + + /** + * Get the video rectangle within the view. + * Returns [offsetX, offsetY, videoWidth, videoHeight] in view-local coordinates. + */ + public static float[] getVideoRectInView(float viewWidth, float viewHeight, + float bufferWidth, float bufferHeight) { + float offsetX = 0, offsetY = 0; + float videoW = viewWidth, videoH = viewHeight; + + if (bufferWidth > 0 && bufferHeight > 0) { + // Universal formula: fit video within view using min scale + float minScale = Math.min(viewWidth / bufferWidth, viewHeight / bufferHeight); + videoW = bufferWidth * minScale; + videoH = bufferHeight * minScale; + offsetX = (viewWidth - videoW) / 2f; + offsetY = (viewHeight - videoH) / 2f; + } + + return new float[]{offsetX, offsetY, videoW, videoH}; + } + + /** + * Get full video rect (same as getVideoRectInView, kept for compatibility). + */ + public static float[] getFullVideoRect(float viewW, float viewH, + float bufferWidth, float bufferHeight) { + return getVideoRectInView(viewW, viewH, bufferWidth, bufferHeight); + } + + /** + * Get visible rect (same as getVideoRectInView, kept for compatibility). + */ + public static float[] getVisibleRect(float viewW, float viewH, + float bufferWidth, float bufferHeight) { + return getVideoRectInView(viewW, viewH, bufferWidth, bufferHeight); + } + + // ===== Coordinate conversion ===== + + /** + * Convert screen coordinates to view-local coordinates. + * @param screenX Screen X coordinate + * @param screenY Screen Y coordinate + * @param textureView The TextureView to map coordinates to + * @return View-local coordinates [x, y] + */ + public static float[] toViewCoords(float screenX, float screenY, + com.serenegiant.widget.AspectRatioTextureView textureView) { + if (textureView != null) { + int[] loc = new int[2]; + textureView.getLocationOnScreen(loc); + return new float[]{screenX - loc[0], screenY - loc[1]}; + } + return new float[]{screenX, screenY}; + } + + /** + * Convert touch coordinates to content coordinates (viewport-aware). + * Used by finger touch in absolute mode. + * + * @param touchX Touch X in view coordinates + * @param touchY Touch Y in view coordinates + * @param viewWidth View width + * @param viewHeight View height + * @param isPortrait Whether device is in portrait mode + * @param landscapeDrawerScaleX DrawerLayout scaleX in landscape mode (1.0 if not zoomed) + * @param landscapeScrollX DrawerLayout scrollX in landscape mode + * @param landscapeScrollY DrawerLayout scrollY in landscape mode + * @return Content coordinates in [0, viewWidth] x [0, viewHeight] + */ + public static float[] mapToContentCoords(float touchX, float touchY, + float viewWidth, float viewHeight, + boolean isPortrait, + float landscapeDrawerScaleX, + float landscapeScrollX, + float landscapeScrollY) { + float contentX = touchX; + float contentY = touchY; + + // Apply inverse viewport transform (undo zoom/pan) + if (isPortrait) { + // View matrix: viewX = (contentX - pivotX) * zoomScale + pivotX + translateX + // Inverse: contentX = (viewX - translateX - pivotX) / zoomScale + pivotX + float zoomScale = Math.max(1.0f, ZoomPanManager.getPortraitZoomScale()); + if (zoomScale > 1.0f) { + float translateX = ZoomPanManager.getPortraitTranslateX(); + float translateY = ZoomPanManager.getPortraitTranslateY(); + float pivotX = viewWidth / 2f; + float pivotY = viewHeight / 2f; + contentX = (touchX - translateX - pivotX) / zoomScale + pivotX; + contentY = (touchY - translateY - pivotY) / zoomScale + pivotY; + } + } else { + // Landscape mode: drawerLayout uses scaleX (pivot at 0,0) + scrollTo + float scaleX = Math.max(1.0f, landscapeDrawerScaleX); + if (scaleX > 1.0f) { + contentX = (touchX + landscapeScrollX) / scaleX; + contentY = (touchY + landscapeScrollY) / scaleX; + } + } + + // Clamp to valid content range + contentX = Math.max(0, Math.min(contentX, viewWidth)); + contentY = Math.max(0, Math.min(contentY, viewHeight)); + + return new float[]{contentX, contentY}; + } + + /** + * Normalize BT mouse view coordinates to HID absolute coordinates [0, 4095]. + * Uses the VIDEO RECT for mapping so that: + * - Video area maps exactly to [0, 4095] on target computer + * - Black bars are clamped to edges (cursor stops at boundary) + * + * @param viewX View X coordinate + * @param viewY View Y coordinate + * @param viewWidth View width + * @param viewHeight View height + * @param videoRect Video rect from getVideoRectInView() [offsetX, offsetY, videoW, videoH] + * @return [hidX, hidY] in range [0, 4095] + */ + public static float[] normalizeBtMouseToHid(float viewX, float viewY, + float viewWidth, float viewHeight, + float[] videoRect) { + // Use video rect for mapping (not full view) + // videoRect = [offsetX, offsetY, videoWidth, videoHeight] + float videoOffsetX = videoRect[0]; + float videoOffsetY = videoRect[1]; + float videoWidth = videoRect[2]; + float videoHeight = videoRect[3]; + + // Safety check: prevent division by zero + if (videoWidth <= 0 || videoHeight <= 0) { + // Fallback to view dimensions + videoWidth = viewWidth; + videoHeight = viewHeight; + videoOffsetX = 0; + videoOffsetY = 0; + } + + // Normalize relative to video area + // Coordinates within video: [0, 1] → [0, 4095] + // Coordinates in black bars: <0 or >1 → clamped to 0 or 4095 + float normX = (viewX - videoOffsetX) / videoWidth; + float normY = (viewY - videoOffsetY) / videoHeight; + + // Clamp to [0, 1] - this clamps black bars to edges + normX = Math.max(0f, Math.min(1f, normX)); + normY = Math.max(0f, Math.min(1f, normY)); + + float hidX = normX * HID_LOGICAL_MAX; + float hidY = normY * HID_LOGICAL_MAX; + + // Debug log + android.util.Log.d("OP-COORD", "normalizeBtMouseToHid: view=(" + viewX + "," + viewY + + ") videoRect=[" + videoOffsetX + "," + videoOffsetY + "," + videoWidth + "," + videoHeight + "]" + + " norm=(" + normX + "," + normY + ")" + + " hid=(" + hidX + "," + hidY + ")"); + + return new float[]{hidX, hidY}; + } + + /** + * Update learned cursor reachable range. + * Called on each BT mouse move to track min/max positions. + * Auto-resets when view size changes (e.g., aspect ratio adjustment). + */ + public static void updateLearnedRange(float cursorX, float cursorY, float viewW, float viewH) { + // Detect view size change → reset learned range + if (viewW != lastViewWidth || viewH != lastViewHeight) { + if (lastViewWidth > 0 && lastViewHeight > 0) { + Log.d(TAG, "View size changed: " + lastViewWidth + "x" + lastViewHeight + + " -> " + viewW + "x" + viewH + ", resetting learned range"); + } + resetLearnedRange(); + lastViewWidth = viewW; + lastViewHeight = viewH; + } + + // Sanity check: only learn positions within view bounds + if (cursorX >= 0 && cursorX <= viewW) { + if (cursorX < learnedMinX) { + learnedMinX = cursorX; + rangeLearned = true; + Log.d(TAG, "Range learned: minX=" + learnedMinX); + } + if (cursorX > learnedMaxX) { + learnedMaxX = cursorX; + rangeLearned = true; + Log.d(TAG, "Range learned: maxX=" + learnedMaxX); + } + } + if (cursorY >= 0 && cursorY <= viewH) { + if (cursorY < learnedMinY) { + learnedMinY = cursorY; + } + if (cursorY > learnedMaxY) { + learnedMaxY = cursorY; + } + } + } + + /** + * Reset learned range (e.g., on orientation change). + */ + public static void resetLearnedRange() { + learnedMinX = Float.MAX_VALUE; + learnedMaxX = Float.MIN_VALUE; + learnedMinY = Float.MAX_VALUE; + learnedMaxY = Float.MIN_VALUE; + rangeLearned = false; + lastViewWidth = 0; + lastViewHeight = 0; + } + + /** + * Check if range has been learned. + */ + public static boolean isRangeLearned() { + return rangeLearned; + } +} diff --git a/app/src/main/java/com/openterface/AOS/serial/CustomTouchListener.java b/app/src/main/java/com/openterface/AOS/serial/CustomTouchListener.java index 86a333c..091f355 100644 --- a/app/src/main/java/com/openterface/AOS/serial/CustomTouchListener.java +++ b/app/src/main/java/com/openterface/AOS/serial/CustomTouchListener.java @@ -24,28 +24,28 @@ */ package com.openterface.AOS.serial; -import android.graphics.Matrix; import android.os.Handler; -import android.os.Looper; -import android.util.DisplayMetrics; import android.util.Log; import android.view.MotionEvent; import android.view.View; -import android.view.ViewGroup; -import android.view.WindowManager; -import android.widget.TextView; - -import androidx.drawerlayout.widget.DrawerLayout; import com.openterface.AOS.R; import com.openterface.AOS.activity.MainActivity; -import com.openterface.AOS.drawerLayout.ZoomLayoutDeal; import com.openterface.AOS.target.HidManager; -import com.openterface.AOS.target.MouseManager; -import com.serenegiant.widget.AspectRatioTextureView; /** - * CustomTouchListener — handles touch events on the full screen (pointer mode). + * CustomTouchListener — thin facade that delegates to specialized handlers. + * + * Architecture: + * - BtMouseInputHandler: handles Bluetooth mouse input (move, click, drag, connection) + * - TouchInputHandler: handles finger touch gestures (tap, double-tap, drag, two-finger) + * - CoordinateConverter: coordinate conversion utilities (view → content → HID) + * - ZoomPanManager: zoom/pan state management for portrait and landscape modes + * + * This class retains: + * - Public API (onTouch, handleGenericMotionEvent, static methods for MainActivity) + * - Black border overlay management (show/hide, video rect update) + * - System cursor management (transparent pointer icon for boundary removal) * * Gesture mapping (aligned with KeyCmd TouchPadView): * 1-finger tap → left click @@ -57,1561 +57,494 @@ public class CustomTouchListener implements View.OnTouchListener { private static final String TAG = "OP-MOUSE"; - private static boolean KeyMouse_state, keyMouseAbsCtrl; - private static boolean virtualTrackpadMode = false; - - /** - * Set mouse control mode. - * @param keyMouseState true = absolute mode, false = relative/trackpad - * @param keyMouseAbsCtrlState true = absolute drag mode - */ - public static void KeyMouse_state(boolean keyMouseState, boolean keyMouseAbsCtrlState) { - KeyMouse_state = keyMouseState; - keyMouseAbsCtrl = keyMouseAbsCtrlState; - } - - /** - * Enable/disable virtual trackpad mode. - * When enabled, all drags send relative movements (like a laptop trackpad), - * and sensitivity is scaled by 1/zoomScale for precise control when zoomed in. - */ - public static void setVirtualTrackpadMode(boolean enabled) { - virtualTrackpadMode = enabled; - } - public static boolean isVirtualTrackpadMode() { - return virtualTrackpadMode; - } - - //system module + // ===== Static references ===== private static UsbDeviceManager usbDeviceManager; - private Handler handler = new Handler(Looper.getMainLooper()); - private Runnable twoFingerPressRunnable; - private TextView floatingLabel; private static MainActivity activity; - //Event processing time - private static final long DOUBLE_CLICK_TIME_DELTA = 300; - private static final long MOVE_THROTTLE_MS = 16; - - //coordinate record - private float firstClickX, firstClickY; - private float startX1, startX2, startY1, startY2; - private float currentX, currentY; - private float startMoveMSX, startMoveMSY; - private static float lastMoveMSX, lastMoveMSY; - private float lastMoveX, lastMoveY; // Track last move position for delta calculation - private boolean suppressSingleTapFromDoubleTap = false; - - //state sign - private boolean DrawMode = false; - private boolean isPanning = false; - private boolean isLongPress = false; - private boolean hasHandledMove = false; - private boolean isDoubleClickPhase = false; - private boolean mouseLeftClick = false; - private boolean mouseRightClick = false; - private boolean mouseScrollClick = false; - - //time sign - private long longPressStartTime; - private long lastClickTime = 0; - private long lastMoveTime = 0; - private long ignoreMoveUntil = 0; - private long firstClickTime = 0; - - //click scope - private static final float CLICK_RADIUS = 100f; - private static final float DRAG_THRESHOLD = 10f; - - //zoom set - private final DrawerLayout drawerLayout; + // ===== Instance handlers ===== + private final TouchInputHandler touchHandler; + + // ===== Portrait mode detection ===== private static boolean isPortraitMode; - private long rightClickCurrentTime; - private long rightReleaseCurrentTime; - private long dragModeStartTime; - private long dragModeEndTime; - private boolean hasEnteredDragMode = false; - - // ===== TouchPad-style gesture detection ===== - // 1-finger tap tracking - private float tapDownX, tapDownY; - private long tapDownTime; - private boolean tapCancelled = false; - private Runnable pendingSingleTap = null; - private long lastSingleTapTime = 0; - private float lastSingleTapX, lastSingleTapY; - - // 2-finger scroll tracking (KeyCmd-style with accumulator) - private float twoFingerDownCenterX, twoFingerDownCenterY; - private float twoFingerPrevX, twoFingerPrevY; - private float twoFingerScrollAccumX = 0f; - private float twoFingerScrollAccumY = 0f; - private boolean twoFingerMoved = false; - private boolean isTwoFingerScrolling = false; - - // Gesture phase tracking — prevents single-click from firing after two-finger gesture - private boolean inTwoFingerSequence = false; - - // Pending initial absolute position send — delayed to allow 2-finger gesture detection - // so we don't send mouse data when the user intends to pan/scroll the view - private Runnable pendingInitialAbsDown = null; - private boolean initialAbsDownSent = false; - - // Two-finger right-click button state - private boolean rightButtonPressed = false; // Whether right button is currently pressed - private boolean isTwoFingerClick = false; // Whether in click mode (not dragged) - private boolean twoFingerDragConfirmed = false; // Confirmed as drag (no longer send right click) - - // Two-finger pinch-to-zoom state - private float twoFingerStartDist = 0f; // Initial distance between two fingers - private boolean isPinching = false; // Currently in pinch-to-zoom mode - private float finger0StartX, finger0StartY; // Finger 0 starting position - private float finger1StartX, finger1StartY; // Finger 1 starting position - private static final float PINCH_THRESHOLD = 15f; // px distance change to start zoom - - // Timing thresholds (aligned with KeyCmd TouchPadView) - private static final long TAP_DELAY_MS = 150; // wait before confirming single tap - private static final long TAP_TIMEOUT_MS = 400; // finger must lift within this time - private static final long DOUBLE_TAP_TIMEOUT_MS = 300; - private static final long LONG_PRESS_TIMEOUT_MS = 500; // Enter drag mode after 500ms - private static final float TAP_MOVE_THRESHOLD_PX = 10f; // px — movement cancels tap and starts drag - private static final float TWO_FINGER_TAP_MOVE_THRESHOLD = 12f; - - // Drag mode state - private boolean isDragMode = false; // Long-press drag mode (left button held until tap) - private Runnable longPressRunnable = null; + // ===== Transparent PointerIcon ===== + // Cached 1x1 transparent PointerIcon - eliminates system cursor boundary constraint + // Default arrow is ~136px wide, causing left/right edge offset of 63-73px + private static android.view.PointerIcon sTransparentPointerIcon; + + // Track if cursor is in black border (for dynamic visibility) + private static boolean wasCursorInBlackBorder = true; + + // ===== Constructor ===== public CustomTouchListener(MainActivity activity, UsbDeviceManager usbDeviceManager) { CustomTouchListener.usbDeviceManager = usbDeviceManager; CustomTouchListener.activity = activity; - floatingLabel = activity.findViewById(R.id.floating_label); - drawerLayout = activity.findViewById(R.id.drawer_layout); - // Detect portrait mode: portrait layout has module_selector_bar, landscape doesn't - isPortraitMode = (activity.findViewById(R.id.module_selector_bar) != null); + + // Detect portrait mode using screen orientation + isPortraitMode = isCurrentlyPortrait(); Log.d(TAG, "CustomTouchListener: isPortraitMode=" + isPortraitMode); - } - public static boolean handleGenericMotionEvent(MotionEvent event){ - if (event.getAction() == MotionEvent.ACTION_SCROLL) { - float scrollAmount = event.getAxisValue(MotionEvent.AXIS_VSCROLL); - if (scrollAmount != 0) { - Log.d(TAG, "Scroll amount: " + scrollAmount); - HidManager.handleTwoFingerPanSlideUpDown(scrollAmount); + // Initialize cached 1x1 transparent PointerIcon + android.graphics.Bitmap transparentBitmap = android.graphics.Bitmap.createBitmap(1, 1, android.graphics.Bitmap.Config.ARGB_8888); + transparentBitmap.eraseColor(0x00000000); + sTransparentPointerIcon = android.view.PointerIcon.create(transparentBitmap, 0, 0); + + // Create and configure TouchInputHandler + touchHandler = new TouchInputHandler(); + touchHandler.setZoomCallback(zoomFactor -> { + ZoomPanManager.adjustZoom(zoomFactor); + }); + + // Initialize CoordinateConverter + CoordinateConverter.init(activity, isPortraitMode); + + // Initialize BtMouseInputHandler with callbacks + BtMouseInputHandler.init(activity, new BtMouseInputHandler.Callbacks() { + @Override + public void onBlackBorderVisibilityChanged(boolean show) { + updateBlackBorderOverlayVisibility(); } - return true; - } - if (event.getAction() == MotionEvent.ACTION_HOVER_MOVE || - event.getAction() == MotionEvent.ACTION_MOVE) { - float cursorX = event.getX(); - float cursorY = event.getY(); - for (int i = 0; i < event.getPointerCount(); i++) { - int toolType = event.getToolType(i); - switch (toolType) { - case MotionEvent.TOOL_TYPE_MOUSE: - if(KeyMouse_state && !virtualTrackpadMode){ - // Map touch coords to content coords (viewport-aware) - float[] content = mapToContentCoords(cursorX, cursorY, - getTouchViewWidth(), getTouchViewHeight()); - if(keyMouseAbsCtrl){ - HidManager.sendHexAbsDragData(content[0], content[1]); - }else { - HidManager.sendHexAbsData(content[0], content[1]); - } - }else { - // Feature 2: Apply zoom-aware sensitivity scaling to deltas - float zoomScale = getEffectiveZoomScale(); - float effectiveDeltaScale = 1.0f / zoomScale; - float adjX = lastMoveMSX + (cursorX - lastMoveMSX) * effectiveDeltaScale; - float adjY = lastMoveMSY + (cursorY - lastMoveMSY) * effectiveDeltaScale; - HidManager.sendHexRelData("SecNullData", adjX, adjY, lastMoveMSX, lastMoveMSY); - lastMoveMSX = cursorX; - lastMoveMSY = cursorY; - } - break; - default: - break; - } + @Override + public void onSystemGestureExclusionChanged(boolean enabled) { + updateSystemGestureExclusion(enabled); } - } - return false; - } - @Override - public boolean onTouch(View v, MotionEvent event) { - int action = event.getActionMasked(); - int pointerCount = event.getPointerCount(); - - // ---- Track gesture phase ---- - // Once two fingers are detected, stay in two-finger event routing until - // ALL fingers are lifted. This handles non-simultaneous finger lifts. - // Key insight: ACTION_POINTER_DOWN starts the sequence; the subsequent - // ACTION_POINTER_UP is "first finger lifts" (still 1 finger down); - // final ACTION_UP (or ACTION_CANCEL) is "last finger lifts". - if (action == MotionEvent.ACTION_POINTER_DOWN || pointerCount == 2) { - inTwoFingerSequence = true; - // User is starting a 2-finger gesture (scroll/pinch) — cancel any pending - // initial absolute position from the first finger so we don't send mouse data - // when the user's intent is to move the viewpoint - cancelPendingInitialAbsDown(); - } - - if (inTwoFingerSequence) { - // Only exit when we see the final lift event AND no fingers remain - if ((action == MotionEvent.ACTION_UP || action == MotionEvent.ACTION_CANCEL) && pointerCount <= 1) { - // Still route this last event to two-finger handler, then clear flag - boolean result = handleTwoFingerGesture(event); - inTwoFingerSequence = false; - return result; + @Override + public void onBlackBorderHintUpdate(float cursorX, float cursorY) { + updateBlackBorderCursorState(cursorX, cursorY); + } + }); + + // Initialize ZoomPanManager + // IMPORTANT: the portrait layout has no drawer_layout at all, so the manager must + // still be initialized (with a null drawer) — otherwise ZoomPanManager keeps + // isPortraitMode=false and silently sends every pinch down the landscape path, + // where drawerLayout==null makes it a no-op (portrait pinch did nothing). + androidx.drawerlayout.widget.DrawerLayout drawerLayoutRef = null; + try { + View drawerView = activity.findViewById(R.id.drawer_layout); + if (drawerView instanceof androidx.drawerlayout.widget.DrawerLayout) { + drawerLayoutRef = (androidx.drawerlayout.widget.DrawerLayout) drawerView; } - return handleTwoFingerGesture(event); + } catch (Exception e) { + Log.e(TAG, "Failed to resolve drawerLayout: " + e.getMessage()); } - // Handle 1-finger touchpad gestures - return handleOneFingerGesture(event); + // No overlay callback: the black border overlay is fully transparent now (it no + // longer draws a mask or a hint), so a zoom step has nothing to refresh there. + ZoomPanManager.init(activity, drawerLayoutRef, isPortraitMode, null); + + // In portrait mode, overlay should be VISIBLE but transparent (alpha=0) + // This allows it to receive cursor events for transparent PointerIcon + if (isPortraitMode && activity.mBinding != null && activity.mBinding.blackBorderOverlay != null) { + activity.mBinding.blackBorderOverlay.setVisibility(View.VISIBLE); + activity.mBinding.blackBorderOverlay.setAlpha(0f); + } } /** - * 1-finger gesture handler (tap/click/double-tap/move/drag). - * KeyCmd logic: - * - Move: just move mouse (no button pressed) - * - Long press (>500ms) then move: drag mode (left button held) - * - Tap: left click - * - Double tap: double click + * Check if device is currently in portrait orientation. + * + * Layout based detection is authoritative (layout-port defines module_selector_bar); + * the display metrics are only used as a fallback, because they may still report the + * previous orientation while onConfigurationChanged() is running. */ - private boolean handleOneFingerGesture(MotionEvent event) { - int action = event.getActionMasked(); - float x = event.getX(); - float y = event.getY(); - - switch (action) { - case MotionEvent.ACTION_DOWN: - tapDownX = x; - tapDownY = y; - tapDownTime = event.getEventTime(); - tapCancelled = false; - suppressSingleTapFromDoubleTap = false; - initialAbsDownSent = false; - - // Cancel any pending long press - if (longPressRunnable != null) { - handler.removeCallbacks(longPressRunnable); - longPressRunnable = null; - } - - detectDoubleClick(event); - initHandActionDownMouse(event); - // Track button state but DON'T send absolute position yet — delay it - // so we can cancel if a second finger comes down (2-finger scroll/pan) - trackButtonState(event); - - // Schedule delayed initial abs down — fires only if no 2nd finger arrives - pendingInitialAbsDown = () -> { - if (!initialAbsDownSent && KeyMouse_state && !virtualTrackpadMode) { - try { - float[] content = mapToContentCoords(startMoveMSX, startMoveMSY, - getTouchViewWidth(), getTouchViewHeight()); - HidManager.sendHexAbsData(content[0], content[1]); - } catch (Exception e) { - Log.e(TAG, "pendingInitialAbsDown error: " + e.getMessage(), e); - } - initialAbsDownSent = true; - } - }; - handler.postDelayed(pendingInitialAbsDown, 80); - - // Schedule long press detection for drag mode (KeyCmd-style: toggle drag on long press) - longPressRunnable = () -> { - if (!tapCancelled) { - isDragMode = true; - // In KeyCmd, drag mode is toggled and stays on until tap exits - Log.d(TAG, "Long press detected -> enter drag mode"); - } - }; - handler.postDelayed(longPressRunnable, LONG_PRESS_TIMEOUT_MS); - break; - - case MotionEvent.ACTION_MOVE: - float distMoved = dist(x, y, tapDownX, tapDownY); - if (distMoved > TAP_MOVE_THRESHOLD_PX && !tapCancelled && !isDragMode) { - // Movement cancels tap detection, but does NOT auto-enter drag mode - // Drag mode should only be entered via long press (500ms) - cancelPendingSingleTap(); - tapCancelled = true; - // Cancel long press runnable since tap is cancelled - if (longPressRunnable != null) { - handler.removeCallbacks(longPressRunnable); - longPressRunnable = null; - } - } - - // Only drag if explicitly in drag mode (long press triggered) - if (isDragMode) { - // In drag mode: move with left button held - handleDragMove(x, y); - } else { - // Normal move: just move mouse (no button pressed) - handleNormalMove(x, y); - } - lastMoveX = x; - lastMoveY = y; - break; - - case MotionEvent.ACTION_UP: - // Safety: if we're somehow in a two-finger sequence, don't process single-finger - if (inTwoFingerSequence) { - return true; - } - - // Cancel long press detection - if (longPressRunnable != null) { - handler.removeCallbacks(longPressRunnable); - longPressRunnable = null; - } - - // Cancel pending initial abs down - cancelPendingInitialAbsDown(); - - long duration = event.getEventTime() - tapDownTime; - float distLifted = dist(x, y, tapDownX, tapDownY); - - // KeyCmd-style: if double-tap just fired, suppress single-tap and release only - if (suppressSingleTapFromDoubleTap) { - suppressSingleTapFromDoubleTap = false; - if (isDragMode) { - releaseMouseAndReset(); - } - isDragMode = false; - lastMoveX = 0; - lastMoveY = 0; - break; - } - - if (isDragMode) { - Log.d(TAG, "Drag ended, releasing left button"); - releaseMouseAndReset(); - isDragMode = false; - lastMoveMSX = 0; - lastMoveMSY = 0; - break; - } - - // Check for tap (KeyCmd-style) - boolean validTap = !tapCancelled - && duration < TAP_TIMEOUT_MS - && distLifted <= TAP_MOVE_THRESHOLD_PX; - - if (validTap) { - long now = event.getEventTime(); - long timeSinceLastTap = now - lastSingleTapTime; - float distSinceLastTap = dist(x, y, lastSingleTapX, lastSingleTapY); - - if (timeSinceLastTap < DOUBLE_TAP_TIMEOUT_MS && distSinceLastTap < TAP_MOVE_THRESHOLD_PX * 2) { - cancelPendingSingleTap(); - handleDoubleClick(); - lastSingleTapTime = 0; - } else { - cancelPendingSingleTap(); - pendingSingleTap = () -> { - handleSingleClick(x, y); - pendingSingleTap = null; - }; - lastSingleTapTime = now; - lastSingleTapX = x; - lastSingleTapY = y; - handler.postDelayed(pendingSingleTap, TAP_DELAY_MS); - } - } - - // Only release mouse if not in a drag/tap-canceled sequence - if (!tapCancelled) { - handActionUpMouse(event); - } else { - HidManager.releaseMSRelData(); - lastMoveMSX = 0; - lastMoveMSY = 0; - } - break; - - case MotionEvent.ACTION_CANCEL: - // Safety: if we're in a two-finger sequence, don't process - if (inTwoFingerSequence) { - return true; - } - - if (longPressRunnable != null) { - handler.removeCallbacks(longPressRunnable); - longPressRunnable = null; - } - cancelPendingInitialAbsDown(); - cancelPendingSingleTap(); - if (isDragMode) { - releaseMouseAndReset(); - isDragMode = false; - } else { - HidManager.releaseMSRelData(); - } - lastMoveMSX = 0; - lastMoveMSY = 0; - lastMoveX = 0; - lastMoveY = 0; - break; + private static boolean isCurrentlyPortrait() { + if (activity == null) return false; + try { + if (activity.findViewById(R.id.module_selector_bar) != null) { + return true; + } + if (activity.findViewById(R.id.drawer_layout) != null) { + return false; + } + } catch (Exception ignored) { + // fall through to metrics } - return true; + android.util.DisplayMetrics metrics = new android.util.DisplayMetrics(); + activity.getWindowManager().getDefaultDisplay().getRealMetrics(metrics); + return metrics.heightPixels > metrics.widthPixels; } - // Portrait zoom tracking - private static float mPortraitZoomScale = 1.0f; - private static float mPortraitTranslateX = 0f; - private static float mPortraitTranslateY = 0f; + // ===== Public touch API ===== + + @Override + public boolean onTouch(View v, MotionEvent event) { + // Delegate to TouchInputHandler + return touchHandler.onTouch(event); + } /** - * Get current portrait zoom scale (called by ZoomLayoutDeal) + * Handle GenericMotionEvent for Bluetooth mouse. + * Called from MainActivity's dispatchGenericMotionEvent. + * @return true if event was consumed */ - public static float getPortraitZoomScale() { - return mPortraitZoomScale; + public static boolean handleGenericMotionEvent(MotionEvent event) { + // Delegate to BtMouseInputHandler + return BtMouseInputHandler.handleGenericMotionEvent(event); } + // ===== BT Mouse state accessors (delegate to BtMouseInputHandler) ===== + /** - * Set portrait pan translation from external source (e.g., PiP indicator drag) - * Called by ZoomLayoutDeal.syncMainViewPosition + * Check if any Bluetooth mouse button is currently pressed. */ - public static void setPortraitPan(float translateX, float translateY) { - mPortraitTranslateX = translateX; - mPortraitTranslateY = translateY; + public static boolean isBtMouseButtonPressed() { + return BtMouseInputHandler.isBtMouseButtonPressed(); } /** - * Apply the current portrait zoom transform to the view - * Called by ZoomLayoutDeal after updating pan values (e.g., when PiP indicator is dragged) - * Includes stretch correction to prevent video distortion + * Check if touch events should be ignored based on recent BT mouse activity. */ - public static void applyCurrentPortraitTransform() { - if (mPortraitZoomScale > 1.0f && activity != null && activity.mBinding != null - && activity.mBinding.viewMainPreview != null) { - AspectRatioTextureView textureView = activity.mBinding.viewMainPreview; - float viewWidth = textureView.getWidth(); - float viewHeight = textureView.getHeight(); - - if (viewWidth <= 0 || viewHeight <= 0) return; - - // Get buffer dimensions for stretch correction - float bufferWidth = activity.getPreviewWidth(); - float bufferHeight = activity.getPreviewHeight(); - if (bufferWidth <= 0 || bufferHeight <= 0) return; - - Matrix matrix = new Matrix(); - - // Check if stretch correction is needed - float surfaceAspect = viewWidth / viewHeight; - float bufferAspect = bufferWidth / bufferHeight; - boolean needsCorrection = Math.abs(surfaceAspect - bufferAspect) > 0.01f; - - if (needsCorrection) { - // Correct SurfaceTexture stretch: maintain aspect ratio, fill width, center vertically - float correctionScaleY = (bufferHeight * viewWidth) / (viewHeight * bufferWidth); - matrix.setScale(1.0f, correctionScaleY); - float correctedHeight = viewHeight * correctionScaleY; - float translateY = (viewHeight - correctedHeight) / 2f; - matrix.postTranslate(0, translateY); - } - - // Apply zoom and pan - float pivotX = viewWidth / 2f; - float pivotY = viewHeight / 2f; - float scale = mPortraitZoomScale; - matrix.postScale(scale, scale, pivotX, pivotY); - matrix.postTranslate(mPortraitTranslateX, mPortraitTranslateY); + public static boolean shouldIgnoreTouch() { + return BtMouseInputHandler.shouldIgnoreTouch(); + } - textureView.setTransform(matrix); - textureView.invalidate(); - } + public static boolean isBtMouseConnected() { + return BtMouseInputHandler.isBtMouseConnected(); } /** - * Move the mouse cursor to the center of the currently visible viewport. - * Used after two-finger pan ends or PiP indicator drag ends, so the mouse follows the view. - * - * How it works: Uses mapToContentCoords to inverse-transform the viewport center - * (viewWidth/2, viewHeight/2) back to content coordinates, then scales to HID coordinates. + * Callback interface for Bluetooth mouse connection state changes. + * Delegated to BtMouseInputHandler. */ - public static void moveMouseToViewportCenter() { - if (activity == null || activity.mBinding == null - || activity.mBinding.viewMainPreview == null) { - return; - } + public interface OnBtMouseConnectionListener { + void onBtMouseConnected(boolean connected); + } - float viewWidth = activity.mBinding.viewMainPreview.getWidth(); - float viewHeight = activity.mBinding.viewMainPreview.getHeight(); - if (viewWidth <= 0 || viewHeight <= 0) return; + public static void setOnBtMouseConnectionListener(OnBtMouseConnectionListener listener) { + BtMouseInputHandler.setOnBtMouseConnectionListener(listener != null + ? connected -> listener.onBtMouseConnected(connected) + : null); + } - // Use mapToContentCoords to inverse-transform the viewport center to content coordinates - float[] content = mapToContentCoords(viewWidth / 2f, viewHeight / 2f, viewWidth, viewHeight); + public static void cancelBtMouseCursorRestore() { + BtMouseInputHandler.cancelBtMouseCursorRestore(); + } - // Content coords are in range [0, viewWidth] x [0, viewHeight] - // HidManager.sendHexAbsData expects HID reference coordinates - // Scale proportionally to convert - int hidWidth = HidManager.getScreenWidth(); - int hidHeight = HidManager.getScreenHeight(); - float hidX = content[0] / viewWidth * hidWidth; - float hidY = content[1] / viewHeight * hidHeight; + public static float getBtMouseLastX() { + return BtMouseInputHandler.getBtMouseLastX(); + } - HidManager.sendHexAbsData(hidX, hidY); - Log.d(TAG, "moveMouseToViewportCenter: content=(" + content[0] + "," + content[1] - + ") hid=(" + hidX + "," + hidY + ")"); + public static float getBtMouseLastY() { + return BtMouseInputHandler.getBtMouseLastY(); } - /** - * Normal mouse move (no button pressed) - * Feature 2: Applies zoom-aware sensitivity scaling to relative mode deltas. - * Feature 3: In virtual trackpad mode, uses relative movement regardless of mode. - */ - private void handleNormalMove(float x, float y) { - if (KeyMouse_state && !virtualTrackpadMode) { - // Absolute mode: map touch coords to content coords (viewport-aware) - firePendingInitialAbsDown(); - float[] content = mapToContentCoords(x, y, getTouchViewWidth(), getTouchViewHeight()); - HidManager.sendHexAbsData(content[0], content[1]); - } else { - // Relative mode or virtual trackpad mode: apply zoom-aware scaling - float zoomScale = getEffectiveZoomScale(); - float effectiveDeltaScale = 1.0f / zoomScale; - float adjX = lastMoveMSX + (x - lastMoveMSX) * effectiveDeltaScale; - float adjY = lastMoveMSY + (y - lastMoveMSY) * effectiveDeltaScale; - HidManager.sendHexRelData("SecNullData", adjX, adjY, lastMoveMSX, lastMoveMSY); - lastMoveMSX = x; - lastMoveMSY = y; - } + public static void setBtMouseLastPosition(float x, float y) { + BtMouseInputHandler.setBtMouseLastPosition(x, y); } - /** - * Drag mode mouse move (left button held) - * Feature 2: Applies zoom-aware sensitivity scaling to relative mode deltas. - * Feature 3: In virtual trackpad mode, uses relative movement regardless of mode. - */ - private void handleDragMove(float x, float y) { - if (KeyMouse_state && !virtualTrackpadMode) { - // In drag mode with absolute: map touch coords to content coords (viewport-aware) - firePendingInitialAbsDown(); - float[] content = mapToContentCoords(x, y, getTouchViewWidth(), getTouchViewHeight()); - HidManager.sendHexAbsButtonClickData("SecLeftData", content[0], content[1]); - } else { - // In drag mode with relative or virtual trackpad: apply zoom-aware scaling - float zoomScale = getEffectiveZoomScale(); - float effectiveDeltaScale = 1.0f / zoomScale; - float adjX = lastMoveMSX + (x - lastMoveMSX) * effectiveDeltaScale; - float adjY = lastMoveMSY + (y - lastMoveMSY) * effectiveDeltaScale; - HidManager.sendHexRelData("SecLeftData", adjX, adjY, lastMoveMSX, lastMoveMSY); - lastMoveMSX = x; - lastMoveMSY = y; - } + public static float getVirtualHidX() { + return BtMouseInputHandler.getVirtualHidX(); } - /** - * Get the effective zoom scale (portrait or landscape, whichever is active). - * Returns >= 1.0f. Used for dynamic sensitivity scaling (Feature 2). - */ - private static float getEffectiveZoomScale() { - if (isPortraitMode) { - return Math.max(1.0f, mPortraitZoomScale); - } else if (activity != null) { - View drawer = activity.findViewById(R.id.drawer_layout); - if (drawer != null) { - return Math.max(1.0f, drawer.getScaleX()); - } - } - return 1.0f; + public static float getVirtualHidY() { + return BtMouseInputHandler.getVirtualHidY(); } - /** - * Map touch coordinates (view space) to camera content coordinates. - * - * Key insight: The camera content fills the TextureView's measured layout bounds - * (aspect ratio is maintained by AspectRatioTextureView's onMeasure). The zoom - * Matrix transform (scale around center pivot + translate) visually scales the - * content — to map a touch position back to the original content space, we apply - * the INVERSE transform. - * - * The viewport center on screen and the HID cursor position represent the same - * point on the target screen. - * - * Returns coordinates in [0, viewWidth] × [0, viewHeight] range (matches the - * camera content space), which MouseManager then converts to HID [0, 4095]. - */ - private static float[] mapToContentCoords(float touchX, float touchY, - float viewWidth, float viewHeight) { - float contentX = touchX; - float contentY = touchY; - - if (isPortraitMode) { - // Apply inverse viewport transform: - // viewX = (contentX - pivotX) * zoomScale + pivotX + translateX - // → contentX = (viewX - translateX - pivotX) / zoomScale + pivotX - float zoomScale = Math.max(1.0f, mPortraitZoomScale); - if (zoomScale > 1.0f) { - float pivotX = viewWidth / 2f; - float pivotY = viewHeight / 2f; - contentX = (touchX - mPortraitTranslateX - pivotX) / zoomScale + pivotX; - contentY = (touchY - mPortraitTranslateY - pivotY) / zoomScale + pivotY; - } - } else if (activity != null) { - // Landscape mode: drawerLayout uses scaleX (pivot at 0,0) + scrollTo - View drawer = activity.findViewById(R.id.drawer_layout); - if (drawer != null) { - float scaleX = Math.max(1.0f, drawer.getScaleX()); - if (scaleX > 1.0f) { - float scrollX = drawer.getScrollX(); - float scrollY = drawer.getScrollY(); - contentX = (touchX + scrollX) / scaleX; - contentY = (touchY + scrollY) / scaleX; - } - } - } + // ===== Portrait zoom/pan (delegate to ZoomPanManager) ===== - // Clamp to valid content range - contentX = Math.max(0, Math.min(contentX, viewWidth)); - contentY = Math.max(0, Math.min(contentY, viewHeight)); + public static float getPortraitZoomScale() { + return ZoomPanManager.getPortraitZoomScale(); + } - return new float[]{contentX, contentY}; + public static void setPortraitPan(float translateX, float translateY) { + ZoomPanManager.setPortraitPan(translateX, translateY); } - /** - * Get the touch target view width (main preview TextureView). - */ - private static float getTouchViewWidth() { - if (activity != null && activity.mBinding != null - && activity.mBinding.viewMainPreview != null) { - float w = activity.mBinding.viewMainPreview.getWidth(); - if (w > 0) return w; - } - return MouseManager.screenWidth; + public static void setPortraitZoomScale(float scale) { + ZoomPanManager.setPortraitZoomScale(scale); } - /** - * Get the touch target view height (main preview TextureView). - */ - private static float getTouchViewHeight() { - if (activity != null && activity.mBinding != null - && activity.mBinding.viewMainPreview != null) { - float h = activity.mBinding.viewMainPreview.getHeight(); - if (h > 0) return h; - } - return MouseManager.screenHeight; + public static float getPortraitTranslateX() { + return ZoomPanManager.getPortraitTranslateX(); } - /** - * Get the floating label view, re-fetching if null. - * The view reference can become stale after layout reload (e.g., orientation change). - */ - private TextView getFloatingLabel() { - if (floatingLabel == null && activity != null) { - floatingLabel = activity.findViewById(R.id.floating_label); - } - return floatingLabel; + public static float getPortraitTranslateY() { + return ZoomPanManager.getPortraitTranslateY(); + } + + public static void applyCurrentPortraitTransform() { + ZoomPanManager.applyCurrentPortraitTransform(); } /** - * Fire the pending initial absolute position immediately (if not already sent). - * Called before sending move/click data in absolute mode to ensure the cursor - * is at the correct position. + * Apply portrait zoom transform to the TextureView. + * Instance method for backwards compatibility with MainActivity. */ - private void firePendingInitialAbsDown() { - if (!initialAbsDownSent && pendingInitialAbsDown != null) { - handler.removeCallbacks(pendingInitialAbsDown); - pendingInitialAbsDown.run(); - } + public void applyPortraitZoomTransform() { + ZoomPanManager.applyPortraitZoomTransform(); + } + + public static boolean isPortraitMode() { + return isPortraitMode; } + // ===== Touch view dimensions (delegate to CoordinateConverter) ===== + /** - * Cancel the pending initial absolute position send. - * Called when a 2-finger gesture is detected so we don't send mouse data - * when the user is trying to move the viewpoint. + * Get the touch view width for coordinate conversion. */ - private void cancelPendingInitialAbsDown() { - if (pendingInitialAbsDown != null) { - handler.removeCallbacks(pendingInitialAbsDown); - pendingInitialAbsDown = null; - } + public static float getTouchViewWidth() { + return CoordinateConverter.getTouchViewWidth(); } /** - * Track external mouse button state without sending absolute position. - * The position send is delayed to avoid sending data during 2-finger gestures. + * Get the touch view height for coordinate conversion. */ - private void trackButtonState(MotionEvent event) { - int buttonState = event.getButtonState(); - if ((buttonState & MotionEvent.BUTTON_PRIMARY) != 0) { - mouseLeftClick = true; - } - if ((buttonState & MotionEvent.BUTTON_SECONDARY) != 0) { - mouseRightClick = true; - } - if ((buttonState & MotionEvent.BUTTON_TERTIARY) != 0) { - mouseScrollClick = true; - } + public static float getTouchViewHeight() { + return CoordinateConverter.getTouchViewHeight(); } /** - * Reset portrait zoom and pan state + * Get effective zoom scale (minimum 1.0). */ - private void resetPortraitZoom() { - mPortraitZoomScale = 1.0f; - mPortraitTranslateX = 0f; - mPortraitTranslateY = 0f; - applyPortraitZoomTransform(); + public static float getEffectiveZoomScale() { + return ZoomPanManager.getEffectiveZoomScale(); } /** - * 2-finger gesture handler — clean right-click press/release + scroll. + * Attach a pinch-to-zoom fallback to the video area container. + * + * In portrait the preview keeps the camera aspect ratio, so it is a short band + * centered inside the much taller video container. Touches landing on the black + * area around that band never reach viewMainPreview, so the pinch was ignored + * whenever the two fingers were not both on the band. * - * Behavior: - * - Two fingers press → right button DOWN - * - Two fingers drag → scroll only (right button released if pressed) - * - Two fingers release → right button UP (only if still pressed, meaning no drag) + * A ViewGroup still offers a touch to its own listener only when no child consumed + * it, so this listener runs exactly in the black area and never competes with the + * preview's own listener. It only adjusts zoom (no coordinates are needed), and it + * consumes the gesture so the trailing single finger cannot move the mouse. */ - private boolean handleTwoFingerGesture(MotionEvent event) { - int action = event.getActionMasked(); - - switch (action) { - case MotionEvent.ACTION_POINTER_DOWN: - // Second finger came down — cancel any pending single-tap AND long press - cancelPendingSingleTap(); - if (longPressRunnable != null) { - handler.removeCallbacks(longPressRunnable); - longPressRunnable = null; - } + public static void attachVideoAreaZoomFallback(View videoArea) { + if (videoArea == null) return; - twoFingerDownCenterX = (event.getX(0) + event.getX(1)) / 2f; - twoFingerDownCenterY = (event.getY(0) + event.getY(1)) / 2f; - twoFingerPrevX = twoFingerDownCenterX; - twoFingerPrevY = twoFingerDownCenterY; - twoFingerScrollAccumX = 0f; - twoFingerScrollAccumY = 0f; - twoFingerMoved = false; - isTwoFingerScrolling = false; - isTwoFingerClick = true; - rightButtonPressed = false; - rightClickCurrentTime = System.currentTimeMillis(); - - // Record initial distance between two fingers for pinch-to-zoom - twoFingerStartDist = calculateDistance( - event.getX(0), event.getY(0), - event.getX(1), event.getY(1)); - isPinching = false; - - // Record individual finger start positions for direction analysis - finger0StartX = event.getX(0); - finger0StartY = event.getY(0); - finger1StartX = event.getX(1); - finger1StartY = event.getY(1); - - Log.d(TAG, "Two fingers pressed — waiting to confirm tap or drag"); - break; - - case MotionEvent.ACTION_MOVE: - int activePointers = event.getPointerCount(); - - if (activePointers == 2) { - float cx = (event.getX(0) + event.getX(1)) / 2f; - float cy = (event.getY(0) + event.getY(1)) / 2f; - - // --- Gesture classification: compare distance change vs center movement --- - float currentDist = calculateDistance( - event.getX(0), event.getY(0), - event.getX(1), event.getY(1)); - float distChange = Math.abs(currentDist - twoFingerStartDist); - float centerDx = cx - twoFingerDownCenterX; - float centerDy = cy - twoFingerDownCenterY; - float centerMove = (float) Math.sqrt(centerDx * centerDx + centerDy * centerDy); - - // Only classify after enough total movement - float totalActivity = distChange + centerMove; - if (totalActivity > PINCH_THRESHOLD && !isPinching && !twoFingerDragConfirmed) { - if (distChange > centerMove) { - // Distance changed more than center moved → pinch/zoom - isPinching = true; - twoFingerDragConfirmed = true; - isTwoFingerClick = false; - Log.d(TAG, "Pinch: distChange=" + distChange + " centerMove=" + centerMove); - } else { - // Center moved more than distance → scroll - twoFingerMoved = true; - twoFingerDragConfirmed = true; - isTwoFingerClick = false; - isTwoFingerScrolling = true; - Log.d(TAG, "Scroll: distChange=" + distChange + " centerMove=" + centerMove); - } - } + videoArea.setOnTouchListener(new View.OnTouchListener() { + private float prevDist = 0f; + private boolean pinching = false; - if (isPinching) { - float zoomFactor = currentDist / twoFingerStartDist; - adjustZoom(zoomFactor); - twoFingerStartDist = currentDist; - } else { - float dx = cx - twoFingerPrevX; - float dy = cy - twoFingerPrevY; - - twoFingerScrollAccumX += dx / 3f; - twoFingerScrollAccumY += -dy / 3f; - - int scrollX = (int) twoFingerScrollAccumX; - int scrollY = (int) twoFingerScrollAccumY; - - if (scrollX != 0) twoFingerScrollAccumX -= scrollX; - if (scrollY != 0) twoFingerScrollAccumY -= scrollY; + private float distance(MotionEvent event) { + float dx = event.getX(0) - event.getX(1); + float dy = event.getY(0) - event.getY(1); + return (float) Math.sqrt(dx * dx + dy * dy); + } - if (scrollX != 0 || scrollY != 0) { - HidManager.handleTwoFingerPanSlideUpDown(scrollY); + @Override + public boolean onTouch(View v, MotionEvent event) { + switch (event.getActionMasked()) { + case MotionEvent.ACTION_DOWN: + // Must consume, otherwise the second finger of this gesture + // would not be delivered here. + pinching = false; + prevDist = 0f; + return true; + + case MotionEvent.ACTION_POINTER_DOWN: + if (event.getPointerCount() == 2) { + prevDist = distance(event); + pinching = true; + Log.d(TAG, "Fallback pinch started: dist=" + prevDist); } - - twoFingerPrevX = cx; - twoFingerPrevY = cy; - } - } else if (activePointers == 1) { - // First finger already lifted, second finger still dragging - isTwoFingerClick = false; - twoFingerDragConfirmed = true; - - float cx = event.getX(0); - float cy = event.getY(0); - float dx = cx - twoFingerPrevX; - float dy = cy - twoFingerPrevY; - - if (Math.abs(dx) > 0f || Math.abs(dy) > 0f) { - isTwoFingerScrolling = true; - } - - twoFingerScrollAccumX += dx / 3f; - twoFingerScrollAccumY += -dy / 3f; - - int scrollX = (int) twoFingerScrollAccumX; - int scrollY = (int) twoFingerScrollAccumY; - - if (scrollX != 0) twoFingerScrollAccumX -= scrollX; - if (scrollY != 0) twoFingerScrollAccumY -= scrollY; - - if (scrollX != 0 || scrollY != 0) { - HidManager.handleTwoFingerPanSlideUpDown(scrollY); - } - - twoFingerPrevX = cx; - twoFingerPrevY = cy; - } - break; - - case MotionEvent.ACTION_POINTER_UP: - // First finger lifted — check if this was a tap using the permanent drag flag - if (twoFingerDragConfirmed) { - // Already confirmed as drag/pinch — do NOT send right click - Log.d(TAG, "First finger lifted during drag/pinch — no right click"); - } else if (isTwoFingerClick) { - // Still in tap mode — send RIGHT CLICK - if (KeyMouse_state && !virtualTrackpadMode) { - float[] content = mapToContentCoords(twoFingerDownCenterX, twoFingerDownCenterY, - getTouchViewWidth(), getTouchViewHeight()); - HidManager.sendHexAbsButtonClickData("SecRightData", content[0], content[1]); - HidManager.sendHexAbsData(content[0], content[1]); - } else { - HidManager.sendHexRelData("SecRightData", - twoFingerDownCenterX, twoFingerDownCenterY, 0, 0); - HidManager.releaseMSRelData(); - } - Log.d(TAG, "Two-finger tap confirmed on POINTER_UP → RIGHT CLICK"); - } - - // Update twoFingerPrevX/Y to the remaining finger's position - // to avoid a large scroll delta jump when the remaining finger moves - if (event.getPointerCount() == 1) { - twoFingerPrevX = event.getX(0); - twoFingerPrevY = event.getY(0); - } - - // Reset scroll accumulators and pinch state - twoFingerScrollAccumX = 0f; - twoFingerScrollAccumY = 0f; - isPinching = false; - rightReleaseCurrentTime = System.currentTimeMillis(); - Log.d(TAG, "ACTION_POINTER_UP"); - break; - - case MotionEvent.ACTION_UP: - // Last finger lifted — final cleanup - // Only send right click if drag/pinch was never confirmed - if (!twoFingerDragConfirmed) { - // Confirmed tap — send RIGHT CLICK - if (KeyMouse_state && !virtualTrackpadMode) { - float[] content = mapToContentCoords(twoFingerDownCenterX, twoFingerDownCenterY, - getTouchViewWidth(), getTouchViewHeight()); - HidManager.sendHexAbsButtonClickData("SecRightData", content[0], content[1]); - HidManager.sendHexAbsData(content[0], content[1]); - } else { - HidManager.sendHexRelData("SecRightData", - twoFingerDownCenterX, twoFingerDownCenterY, 0, 0); - HidManager.releaseMSRelData(); - } - Log.d(TAG, "Two-finger final lift → RIGHT CLICK"); - } else { - // Two-finger drag ended — if zoomed in, move cursor to viewport center - if (isPortraitMode && mPortraitZoomScale > 1.0f - && KeyMouse_state && !virtualTrackpadMode) { - moveMouseToViewportCenter(); - } - Log.d(TAG, "Two-finger drag/pinch ended — no right click"); + return true; + + case MotionEvent.ACTION_MOVE: + if (pinching && event.getPointerCount() == 2 && prevDist > 0f) { + float dist = distance(event); + if (dist > 0f) { + float factor = Math.max(0.7f, Math.min(1.4f, dist / prevDist)); + if (Math.abs(factor - 1f) > 0.001f) { + ZoomPanManager.adjustZoom(factor); + } + prevDist = dist; + } + } + return true; + + case MotionEvent.ACTION_POINTER_UP: + case MotionEvent.ACTION_UP: + case MotionEvent.ACTION_CANCEL: + pinching = false; + prevDist = 0f; + return true; } - - // Full reset - twoFingerScrollAccumX = 0f; - twoFingerScrollAccumY = 0f; - twoFingerMoved = false; - isTwoFingerScrolling = false; - isTwoFingerClick = false; - twoFingerDragConfirmed = false; - rightButtonPressed = false; - isPinching = false; - break; - } - return true; + return true; + } + }); } + // ===== Black border overlay management ===== + /** - * Release right button — sends release command for both abs and rel modes. + * Update the black border overlay visibility. + * Shows overlay when BT mouse is connected, hides when disconnected. + * In portrait mode, video fills the entire TextureView (via Matrix rotation), + * so there are no black borders to show — but still need to hide system cursor. */ - private void releaseRightButton() { - if (KeyMouse_state && !virtualTrackpadMode) { - float[] content = mapToContentCoords(lastMoveMSX, lastMoveMSY, - getTouchViewWidth(), getTouchViewHeight()); - HidManager.sendHexAbsData(content[0], content[1]); - } else { - HidManager.releaseMSRelData(); + public static void updateBlackBorderOverlayVisibility() { + if (activity == null || activity.mBinding == null) { + return; } - } - private void handleSingleClick(float x, float y) { - Log.d(TAG, "Single click at (" + x + "," + y + ")"); - if (KeyMouse_state && !virtualTrackpadMode) { - // Absolute mode: map coords to content space (viewport-aware), click at touched position - firePendingInitialAbsDown(); - float[] content = mapToContentCoords(x, y, getTouchViewWidth(), getTouchViewHeight()); - Log.d(TAG, "handleSingleClick: content=(" + content[0] + "," + content[1] + ")"); - try { - HidManager.sendHexAbsButtonClickData("SecLeftData", content[0], content[1]); - final float cx = content[0], cy = content[1]; - handler.postDelayed(() -> { - try { - HidManager.sendHexAbsData(cx, cy); - } catch (Exception e) { - Log.e(TAG, "handleSingleClick release error: " + e.getMessage()); - } - }, 50); - } catch (Exception e) { - Log.e(TAG, "handleSingleClick error: " + e.getMessage(), e); - } - } else { - // Relative mode or virtual trackpad: send 0-delta click at cursor's current position - HidManager.sendHexRelData("SecLeftData", lastMoveMSX, lastMoveMSY, lastMoveMSX, lastMoveMSY); - handler.postDelayed(() -> HidManager.releaseMSRelData(), 50); - } - } + boolean btConnected = BtMouseInputHandler.isBtMouseConnected(); - private void handleDoubleClick() { - Log.d(TAG, "Double click!"); - // KeyCmd-style: suppress single-tap after double-tap - suppressSingleTapFromDoubleTap = true; - if (KeyMouse_state && !virtualTrackpadMode) { - try { - float[] content = mapToContentCoords(startMoveMSX, startMoveMSY, - getTouchViewWidth(), getTouchViewHeight()); - HidManager.handleDoubleClickAbs(content[0], content[1]); - } catch (Exception e) { - Log.e(TAG, "handleDoubleClick error: " + e.getMessage(), e); + if (btConnected) { + // Keep the (fully transparent) overlay visible in both orientations so its + // pointer icon also applies outside the preview — the system cursor must not + // reappear there. Setting the icon on a hidden view has no effect. + if (activity.mBinding.blackBorderOverlay != null) { + activity.mBinding.blackBorderOverlay.showOverlay(); + if (sTransparentPointerIcon != null) { + activity.mBinding.blackBorderOverlay.setPointerIcon(sTransparentPointerIcon); + } + } + if (sTransparentPointerIcon != null && activity.mBinding.viewMainPreview != null) { + activity.mBinding.viewMainPreview.setPointerIcon(sTransparentPointerIcon); } } else { - HidManager.handleDoubleClickRel(); - } - } - - private void cancelPendingSingleTap() { - if (pendingSingleTap != null) { - handler.removeCallbacks(pendingSingleTap); - pendingSingleTap = null; + // BT disconnected: hide overlay + if (activity.mBinding.blackBorderOverlay != null) { + activity.mBinding.blackBorderOverlay.hideOverlay(); + } } } - private static float dist(float x1, float y1, float x2, float y2) { - float dx = x1 - x2; - float dy = y1 - y2; - return (float) Math.sqrt(dx * dx + dy * dy); - } - /** - * Release mouse state — sends appropriate release command for both abs and rel modes. + * Called when screen configuration changes (e.g., rotation). */ - private void releaseMouseAndReset() { - if (KeyMouse_state && !virtualTrackpadMode) { - // In absolute mode: map coords to content space, send move to current position - float[] content = mapToContentCoords(lastMoveMSX, lastMoveMSY, - getTouchViewWidth(), getTouchViewHeight()); - HidManager.sendHexAbsData(content[0], content[1]); - } else { - // In relative mode or virtual trackpad: just release buttons, no movement - HidManager.releaseMSRelData(); - } - } - - // --- Original methods below (unchanged) --- - - private void handActionDownMouse(MotionEvent event){ - detectDoubleClick(event); + public static void onConfigurationChanged() { + boolean newPortraitMode = isCurrentlyPortrait(); + if (newPortraitMode != isPortraitMode) { + Log.d(TAG, "Configuration changed: isPortraitMode " + isPortraitMode + " -> " + newPortraitMode); + isPortraitMode = newPortraitMode; + ZoomPanManager.setPortraitMode(newPortraitMode); - initHandActionDownMouse(event); + // Reset learned cursor range + CoordinateConverter.resetLearnedRange(); - int buttonState = event.getButtonState(); - if ((buttonState & MotionEvent.BUTTON_PRIMARY) != 0) { - mouseLeftClick = true; - Log.d(TAG, "Left button pressed"); - } - if ((buttonState & MotionEvent.BUTTON_SECONDARY) != 0) { - mouseRightClick = true; - Log.d(TAG, "Right button pressed"); - } - if ((buttonState & MotionEvent.BUTTON_TERTIARY) != 0) { - mouseScrollClick = true; - Log.d(TAG, "Middle button pressed"); - } - - if (KeyMouse_state && !virtualTrackpadMode){ - try { - float[] content = mapToContentCoords(startMoveMSX, startMoveMSY, - getTouchViewWidth(), getTouchViewHeight()); - HidManager.sendHexAbsData(content[0], content[1]); - } catch (Exception e) { - Log.e(TAG, "handActionDownMouse error: " + e.getMessage(), e); - } - } - } - - //deal double click move - private void detectDoubleClick(MotionEvent event) { - long currentTime = System.currentTimeMillis(); - if (currentTime - firstClickTime < DOUBLE_CLICK_TIME_DELTA - && Math.abs(event.getX() - firstClickX) < CLICK_RADIUS - && Math.abs(event.getY() - firstClickY) < CLICK_RADIUS) { - isDoubleClickPhase = true; - Log.d(TAG, "this is isDoubleClickPhase"); - } else { - firstClickTime = currentTime; - firstClickX = event.getX(); - firstClickY = event.getY(); - isDoubleClickPhase = false; + // The layout is re-inflated on rotation, so zoom/pan state cannot survive + // in either direction — otherwise the manager reports a zoom level that the + // freshly laid out preview does not have (next pinch starts from a bogus scale). + ZoomPanManager.reset(); + Log.d(TAG, "Reset portrait zoom state due to rotation"); } } - //init handActionDownMouse state - private void initHandActionDownMouse(MotionEvent event){ - isLongPress = false; - currentX = event.getX(); - currentY = event.getY(); - startMoveMSX = event.getX(); - startMoveMSY = event.getY(); - longPressStartTime = System.currentTimeMillis(); - } - - private void handActionPointerDownMouse(MotionEvent event){ - if (event.getPointerCount() == 2) { - rightClickCurrentTime = System.currentTimeMillis(); - startX1 = event.getX(0); - startX2 = event.getX(1); - startY1 = event.getY(0); - startY2 = event.getY(1); - isPanning = true; - hasHandledMove = false; - } - } + /** + * Track whether the mouse is over the black border and update the system cursor. + * In portrait mode the video fills the entire view, so the cursor is always over the video. + */ + private static void updateBlackBorderCursorState(float viewX, float viewY) { + if (isPortraitMode) return; - private void handActionMoveMouse(MotionEvent event){ - long currentTime = System.currentTimeMillis(); - if (currentTime - lastMoveTime < MOVE_THROTTLE_MS) { + if (activity == null || activity.mBinding == null + || activity.mBinding.blackBorderOverlay == null + || activity.mBinding.viewMainPreview == null) { return; } - lastMoveTime = currentTime; - - // Added double-click drag and drop processing - if (isDoubleClickPhase && event.getPointerCount() == 1) { - if (!hasEnteredDragMode){ - dragModeStartTime = System.currentTimeMillis(); - hasEnteredDragMode = true; - Log.d(TAG, "dragModeStartTime: " + dragModeStartTime); - } - startMoveMSX = event.getX(); - startMoveMSY = event.getY(); - - if (KeyMouse_state && !virtualTrackpadMode) { - // Map to content coords (viewport-aware) for all absolute sends in this block - float[] content = mapToContentCoords(startMoveMSX, startMoveMSY, - getTouchViewWidth(), getTouchViewHeight()); - HidManager.sendHexAbsDragData(content[0], content[1]); - } else { - // Relative mode or virtual trackpad: apply zoom-aware scaling - float zoomScale = getEffectiveZoomScale(); - float effectiveDeltaScale = 1.0f / zoomScale; - float adjX = lastMoveMSX + (startMoveMSX - lastMoveMSX) * effectiveDeltaScale; - float adjY = lastMoveMSY + (startMoveMSY - lastMoveMSY) * effectiveDeltaScale; - HidManager.sendHexRelData("SecLeftData", adjX, adjY, lastMoveMSX, lastMoveMSY); - lastMoveMSX = startMoveMSX; - lastMoveMSY = startMoveMSY; - } - TextView label = getFloatingLabel(); - if (label != null) { - label.setVisibility(View.VISIBLE); - } + float viewW = activity.mBinding.viewMainPreview.getWidth(); + float viewH = activity.mBinding.viewMainPreview.getHeight(); + if (viewW <= 0 || viewH <= 0) { return; } - if (isPanning && event.getPointerCount() == 2) { //deal Middle click - - float zoomX1 = event.getX(0); - float zoomY1 = event.getY(0); - float zoomX2 = event.getX(1); - float zoomY2 = event.getY(1); - - // Calculate initial and current distances between fingers - float initialDistance = calculateDistance(startX1, startY1, startX2, startY2); - float currentDistance = calculateDistance(zoomX1, zoomY1, zoomX2, zoomY2); - - float ZOOM_SENSITIVITY = 5f; // Minimum distance change to trigger zoom + float bufferW = activity.getPreviewWidth(); + float bufferH = activity.getPreviewHeight(); + float[] videoRect = CoordinateConverter.getVideoRectInView(viewW, viewH, bufferW, bufferH); - // Check if the distance change is significant enough - if (Math.abs(currentDistance - initialDistance) > ZOOM_SENSITIVITY) { - // Check if enough time has elapsed since the last move - if (currentTime - lastMoveTime >= 16) { - handler.removeCallbacks(twoFingerPressRunnable); - Log.d(TAG, "ACTION_MOVE: Pinch to Zoom"); + float visLeft = videoRect[0]; + float visTop = videoRect[1]; + float visW = videoRect[2]; + float visH = videoRect[3]; - // Calculate the zoom factor (ratio of current to initial distance) - float zoomFactor = currentDistance / initialDistance; - - // Apply zoom to your view (e.g., drawerLayout or viewMainPreview) - adjustZoom(zoomFactor); - - // Update the starting points to the current ones for the next move - startX1 = zoomX1; - startY1 = zoomY1; - startX2 = zoomX2; - startY2 = zoomY2; - - hasHandledMove = true; - lastMoveTime = currentTime; - } - } - } else if (!isLongPress) { - startMoveMSX = event.getX(); - startMoveMSY = event.getY(); - - float distance = (float) Math.sqrt(Math.pow(currentX - startMoveMSX, 2) + Math.pow(currentY - startMoveMSY, 2)); - - if (currentTime < ignoreMoveUntil) { - return; - } - - if (KeyMouse_state && !virtualTrackpadMode) { - // Map to content coords (viewport-aware) for all absolute sends below - float[] absContent = mapToContentCoords(startMoveMSX, startMoveMSY, - getTouchViewWidth(), getTouchViewHeight()); - float absX = absContent[0], absY = absContent[1]; - - if (keyMouseAbsCtrl){ - HidManager.sendHexAbsDragData(absX, absY); - TextView label = getFloatingLabel(); - if (label != null) { - label.setVisibility(View.VISIBLE); - } - }else { - if (DrawMode && currentTime - longPressStartTime >= 2000 && distance < 30){ - HidManager.handleTwoPress(); - TextView label = getFloatingLabel(); - if (label != null) { - label.setVisibility(View.GONE); - } - return; - } else if (DrawMode) { - HidManager.sendHexAbsDragData(absX, absY); - return; - } - - if (distance < 30) { - if (currentTime - longPressStartTime >= 1000){ - HidManager.sendHexAbsDragData(absX, absY); - DrawMode = true; - } - } else { - if (mouseLeftClick){ - Log.d(TAG, "mouse left click"); - HidManager.sendHexAbsButtonClickData("SecLeftData", absX, absY); - }else if (mouseRightClick){ - Log.d(TAG, "mouse right click"); - HidManager.sendHexAbsButtonClickData("SecRightData", absX, absY); - } else if (mouseScrollClick) { - Log.d(TAG, "mouse scroll click"); - HidManager.sendHexAbsButtonClickData("SecMiddleData", absX, absY); - }else { - HidManager.sendHexAbsData(absX, absY); - } - longPressStartTime = currentTime; - } - } - } else { - // Relative mode or virtual trackpad: apply zoom-aware scaling - float zoomScale = getEffectiveZoomScale(); - float effectiveDeltaScale = 1.0f / zoomScale; - float adjX = lastMoveMSX + (startMoveMSX - lastMoveMSX) * effectiveDeltaScale; - float adjY = lastMoveMSY + (startMoveMSY - lastMoveMSY) * effectiveDeltaScale; - HidManager.sendHexRelData("SecNullData", adjX, adjY, lastMoveMSX, lastMoveMSY); - lastMoveMSX = startMoveMSX; - lastMoveMSY = startMoveMSY; - } - } - } + boolean inBlackBorder = viewX < visLeft || viewX > visLeft + visW + || viewY < visTop || viewY > visTop + visH; - // Helper method to calculate distance between two points - private float calculateDistance(float x1, float y1, float x2, float y2) { - float dx = x2 - x1; - float dy = y2 - y1; - return (float) Math.sqrt(dx * dx + dy * dy); + updateSystemCursorVisibility(inBlackBorder); } /** - * Apply zoom using TextureView.setTransform(Matrix) for GPU-quality scaling. - * - * 竖屏模式下,TextureView 始终保持 MATCH_PARENT 填满 video_area_container。 - * SurfaceTexture 的实际尺寸 = screenWidth × containerHeight, - * 会把 1920×1080 的 buffer 拉伸到非 16:9 比例。 - * Matrix 负责:纠正拉伸 → 垂直居中 → 缩放 → 平移。 - * 这样不改变 TextureView 物理尺寸,避免触发 onSurfaceTextureSizeChanged 导致崩溃。 + * Update system cursor visibility based on whether cursor is in black border. */ - /** - * 应用竖屏 Matrix 变换(纠正拉伸 + 缩放 + 平移)。 - * 公开方法,供 MainActivity 在 surface 就绪 / 尺寸变化时调用。 - */ - public void applyPortraitZoomTransform() { - if (activity == null || activity.mBinding == null || activity.mBinding.viewMainPreview == null) { - return; - } - - AspectRatioTextureView textureView = activity.mBinding.viewMainPreview; - - // Get actual SurfaceTexture dimensions (= TextureView visible area) - float surfaceWidth = textureView.getWidth(); - float surfaceHeight = textureView.getHeight(); - - if (surfaceWidth <= 0 || surfaceHeight <= 0) { + private static void updateSystemCursorVisibility(boolean inBlackBorder) { + if (activity == null || activity.mBinding == null + || activity.mBinding.viewMainPreview == null) { return; } - - // Get camera buffer dimensions - float bufferWidth = activity.getPreviewWidth(); - float bufferHeight = activity.getPreviewHeight(); - if (bufferWidth <= 0 || bufferHeight <= 0) { + if (android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.N) { return; } - // Create Matrix - Matrix matrix = new Matrix(); - - // Check if stretch correction is needed: - // If SurfaceTexture aspect ratio ≠ buffer aspect ratio, it means the content is stretched - float surfaceAspect = surfaceWidth / surfaceHeight; - float bufferAspect = bufferWidth / bufferHeight; - boolean needsCorrection = Math.abs(surfaceAspect - bufferAspect) > 0.01f; - - if (needsCorrection) { - // SurfaceTexture stretches buffer (bufferWidth × bufferHeight) to surface (surfaceWidth × surfaceHeight) - // Stretch ratio: stretchX = surfaceWidth/bufferWidth, stretchY = surfaceHeight/bufferHeight - // - // Correction goal: Keep video at original 16:9 aspect ratio, fill surfaceWidth, calculate height proportionally, center vertically - // - // Step 1: Undo stretch → scaleX = bufferWidth/surfaceWidth, scaleY = bufferHeight/surfaceHeight - // Step 2: Uniform scale to fill width → scale = surfaceWidth/bufferWidth - // Combined: - // scaleX = (bufferWidth/surfaceWidth) * (surfaceWidth/bufferWidth) = 1.0 - // scaleY = (bufferHeight/surfaceHeight) * (surfaceWidth/bufferWidth) - // = (bufferHeight * surfaceWidth) / (surfaceHeight * bufferWidth) - float correctionScaleY = (bufferHeight * surfaceWidth) / (surfaceHeight * bufferWidth); - matrix.setScale(1.0f, correctionScaleY); - - // Corrected content height - float correctedHeight = surfaceHeight * correctionScaleY; - // Vertical centering - float translateY = (surfaceHeight - correctedHeight) / 2f; - matrix.postTranslate(0, translateY); - // Horizontal: corrected width = surfaceWidth, already filled, no extra translation needed - } - // If no correction needed (surfaceAspect ≈ bufferAspect), Matrix starts from identity - - // Zoom (pivot at SurfaceTexture center) - float zoomScale = mPortraitZoomScale; - float pivotX = surfaceWidth / 2f; - float pivotY = surfaceHeight / 2f; - matrix.postScale(zoomScale, zoomScale, pivotX, pivotY); - - // Pan (user drag) - matrix.postTranslate(mPortraitTranslateX, mPortraitTranslateY); - - // Apply Matrix - textureView.setTransform(matrix); - textureView.invalidate(); - - // Sync PiP indicator - ZoomLayoutDeal.updateIndicatorFromMainView( - zoomScale, - mPortraitTranslateX, - mPortraitTranslateY - ); - - Log.d(TAG, "Portrait zoom applied: scale=" + zoomScale + - " surface=" + surfaceWidth + "x" + surfaceHeight + - " needsCorrection=" + needsCorrection + - " translateX=" + mPortraitTranslateX + - " translateY=" + mPortraitTranslateY); - } - - /** - * Ensure TextureView fills video_area_container (MATCH_PARENT) in portrait mode. - * No longer dynamically change TextureView size to avoid triggering onSurfaceTextureSizeChanged. - * All zoom/pan/stretch correction is handled by Matrix in applyPortraitZoomTransform(). - * - * @return null (no longer returns target size since TextureView size is not changed) - */ - private int[] expandTextureViewToTarget() { - if (activity == null || activity.mBinding == null || activity.mBinding.viewMainPreview == null) { - return null; - } - - AspectRatioTextureView textureView = activity.mBinding.viewMainPreview; - - // Ensure TextureView is MATCH_PARENT in portrait mode, disable aspect ratio constraint - if (isPortraitMode && textureView.isAspectRatioEnabled()) { - textureView.setAspectRatioEnabled(false); - ViewGroup.LayoutParams params = textureView.getLayoutParams(); - if (params.width != ViewGroup.LayoutParams.MATCH_PARENT - || params.height != ViewGroup.LayoutParams.MATCH_PARENT) { - params.width = ViewGroup.LayoutParams.MATCH_PARENT; - params.height = ViewGroup.LayoutParams.MATCH_PARENT; - textureView.setLayoutParams(params); - Log.d(TAG, "Set TextureView to MATCH_PARENT for portrait Matrix mode"); + if (inBlackBorder != wasCursorInBlackBorder) { + wasCursorInBlackBorder = inBlackBorder; + if (sTransparentPointerIcon != null) { + activity.mBinding.viewMainPreview.setPointerIcon(sTransparentPointerIcon); + Log.d(TAG, "System cursor hidden (cached transparent 1x1 icon)"); } } - - return null; // No longer change size, return null } /** - * Reset TextureView when zoomed out. - * In portrait mode, TextureView always stays MATCH_PARENT, no need to restore. - * Only ensure aspect ratio constraint is disabled (Matrix handles all display transforms). + * Move the mouse cursor to the point of the target screen that is currently shown + * at the middle of the phone screen. */ - private void resetTextureViewHeight() { - if (activity == null || activity.mBinding == null || activity.mBinding.viewMainPreview == null) { + public static void moveMouseToViewportCenter() { + if (activity == null || activity.mBinding == null + || activity.mBinding.viewMainPreview == null) { return; } - AspectRatioTextureView textureView = activity.mBinding.viewMainPreview; - // Keep aspect ratio constraint disabled in portrait mode - if (isPortraitMode && textureView.isAspectRatioEnabled()) { - textureView.setAspectRatioEnabled(false); - ViewGroup.LayoutParams params = textureView.getLayoutParams(); - if (params.height != ViewGroup.LayoutParams.MATCH_PARENT - || params.width != ViewGroup.LayoutParams.MATCH_PARENT) { - params.height = ViewGroup.LayoutParams.MATCH_PARENT; - params.width = ViewGroup.LayoutParams.MATCH_PARENT; - textureView.setLayoutParams(params); - } - Log.d(TAG, "Reset TextureView to MATCH_PARENT (portrait Matrix mode)"); - } - } - - // Method to apply zoom to your view - private void adjustZoom(float zoomFactor) { - // In portrait mode, use TextureView.setTransform(Matrix) for quality scaling - if (isPortraitMode) { - if (activity == null || activity.mBinding == null || activity.mBinding.viewMainPreview == null) { - return; - } + float viewWidth = activity.mBinding.viewMainPreview.getWidth(); + float viewHeight = activity.mBinding.viewMainPreview.getHeight(); + if (viewWidth <= 0 || viewHeight <= 0) return; - // Define scale limits - float MIN_SCALE = 1f; - // Dynamically calculate max scale: video display height should fill the available container height - // Container height = screenHeight - top_bar(52dp) - bottom_bar(56dp) ≈ screenHeight * 0.95 - // Video natural height = screenWidth / (bufferWidth / bufferHeight) - // maxScale = containerHeight / naturalHeight - final float CONTAINER_HEIGHT_RATIO = 0.95f; // Available height as ratio of screen height - float MAX_SCALE = 3.0f; // Default value, calculated dynamically below - if (activity.getPreviewWidth() > 0 && activity.getPreviewHeight() > 0) { - android.util.DisplayMetrics metrics = new android.util.DisplayMetrics(); - activity.getWindowManager().getDefaultDisplay().getRealMetrics(metrics); - float screenWidth = metrics.widthPixels; - float screenHeight = metrics.heightPixels; - float bufferAspect = (float) activity.getPreviewWidth() / activity.getPreviewHeight(); - float naturalHeight = screenWidth / bufferAspect; - float containerHeight = screenHeight * CONTAINER_HEIGHT_RATIO; - MAX_SCALE = containerHeight / naturalHeight; - if (MAX_SCALE < MIN_SCALE) MAX_SCALE = MIN_SCALE; - } + float centerX = viewWidth / 2f; + float centerY = viewHeight / 2f; - // Calculate new scale - float currentScale = mPortraitZoomScale; - float newScale = currentScale * zoomFactor; - - // Clamp within bounds - if (newScale < MIN_SCALE) newScale = MIN_SCALE; - if (newScale > MAX_SCALE) newScale = MAX_SCALE; - - // Store scale for auto-pan calculation - mPortraitZoomScale = newScale; - - // If zooming out to minimum, hide PiP and reset translation - if (newScale <= MIN_SCALE) { - mPortraitTranslateX = 0f; - mPortraitTranslateY = 0f; - ZoomLayoutDeal.zoomOut(); - resetTextureViewHeight(); // Reset TextureView height when zoomed out - } else { - // Show PiP when zoomed in - ZoomLayoutDeal.enlargeView(); + // Landscape pans the video by scrolling the drawer, so the middle of the screen + // is a shifted content point. Without this shift the cursor was placed in the + // middle of the target desktop instead of the middle of what is on screen. + if (!isPortraitMode) { + androidx.drawerlayout.widget.DrawerLayout drawer = ZoomPanManager.getDrawerLayout(); + if (drawer != null) { + centerX += drawer.getScrollX(); + centerY += drawer.getScrollY(); } - - // Apply transform using Matrix (GPU-quality scaling) - applyPortraitZoomTransform(); - - Log.d(TAG, "Portrait zoom adjusted: newScale=" + newScale + - " translateX=" + mPortraitTranslateX + - " translateY=" + mPortraitTranslateY); - return; } - // Landscape mode - use existing drawerLayout zoom logic - if (drawerLayout == null) { - Log.d(TAG, "adjustZoom: drawerLayout not available, skipping zoom"); - return; - } - - // Define scale limits - float MIN_SCALE = 1f; // Minimum zoom level - float MAX_SCALE = 2.0f; // Maximum zoom level - - // Get current scale from the view (assuming drawerLayout is being scaled) - float currentScale = drawerLayout.getScaleX(); // Assume uniform scaling (X = Y) - float newScale = currentScale * zoomFactor; - - // Clamp the new scale within bounds - if (newScale < MIN_SCALE){ - newScale = MIN_SCALE; - ZoomLayoutDeal.zoomOut(); - } - - if (newScale > MAX_SCALE){ - newScale = MAX_SCALE; - ZoomLayoutDeal.enlargeView(); - } - - // If zoomed in at all, show the thumbnail window - if (newScale > MIN_SCALE) { - ZoomLayoutDeal.enlargeView(); - } - - // Apply the new scale - drawerLayout.setScaleX(newScale); - drawerLayout.setScaleY(newScale); - - // Ensure pivot is centered for proper scaling - drawerLayout.setPivotX(drawerLayout.getWidth() / 2f); - drawerLayout.setPivotY(drawerLayout.getHeight() / 2f); - - // Enforce clipping to prevent white edges from showing - drawerLayout.setClipChildren(true); - drawerLayout.setClipToPadding(true); + // The point is already a view-local coordinate (Android reverses the parent matrix + // before dispatch), so the landscape drawer scale must NOT be applied here. + float[] content = CoordinateConverter.mapToContentCoords( + centerX, centerY, viewWidth, viewHeight, isPortraitMode, 1.0f, 0f, 0f); - Log.d(TAG, "Zoom adjusted: newScale=" + newScale); + // Send it exactly like a tap does: sendHexAbsData takes view coordinates and + // converts them to the 12-bit HID range itself. Scaling by the view size here + // first and letting it be scaled a second time was a no-op at the centre but + // relied on screenWidth matching the view width. + HidManager.sendHexAbsData(content[0], content[1]); } - private void handActionPointerUpMouse(MotionEvent event){ - if (event.getPointerCount() == 2) { - if ((rightReleaseCurrentTime - rightClickCurrentTime < 500) && !hasHandledMove) { - HidManager.handleTwoPress();//deal right click - Log.d(TAG, "this release the right click"); - } - isPanning = false; - hasHandledMove = false; - Log.d(TAG, "ACTION_POINTER_UP"); - ignoreMoveUntil = System.currentTimeMillis() + 200; - rightReleaseCurrentTime = System.currentTimeMillis(); + // ===== System gesture exclusion ===== - } + /** + * Update system gesture exclusion rects to prevent Android system gestures + * from intercepting BT mouse at screen edges. + */ + private static void updateSystemGestureExclusion(boolean enable) { + // This is handled by the activity or can be delegated + // For now, this is a no-op placeholder + Log.d(TAG, "updateSystemGestureExclusion: " + enable); } - private void handActionUpMouse(MotionEvent event){ - // Map to content coords once for all absolute sends below (viewport-aware) - float[] upContent = mapToContentCoords(startMoveMSX, startMoveMSY, - getTouchViewWidth(), getTouchViewHeight()); - float upX = upContent[0], upY = upContent[1]; - - // double-click end processing - dragModeEndTime = System.currentTimeMillis(); - if (isDoubleClickPhase && ( dragModeEndTime - dragModeStartTime) > 500) { - if (KeyMouse_state && !virtualTrackpadMode){ - HidManager.sendHexAbsData(upX, upY);//release abs state - }else { - HidManager.sendHexRelData("SecNullData", startMoveMSX, startMoveMSY, lastMoveMSX, lastMoveMSY); - } - isDoubleClickPhase = false; - TextView label = getFloatingLabel(); - if (label != null) { - label.setVisibility(View.GONE); - } - hasEnteredDragMode = false; - lastMoveMSX = 0; - lastMoveMSY = 0; - return; - } - - long clickTime = System.currentTimeMillis(); - if (clickTime - lastClickTime <= DOUBLE_CLICK_TIME_DELTA) { - Log.d(TAG, "Double click at the same position"); - - if (KeyMouse_state && !virtualTrackpadMode) { - HidManager.handleDoubleClickAbs(upX, upY); - } else { - HidManager.handleDoubleClickRel(); - } - - } - - if (KeyMouse_state && !virtualTrackpadMode) { - HidManager.sendHexAbsData(upX, upY); - }else{ - HidManager.sendHexRelData("SecNullData", startMoveMSX, startMoveMSY, lastMoveMSX, lastMoveMSY); - } - DrawMode = false; - TextView label = getFloatingLabel(); - if (label != null) { - label.setVisibility(View.GONE); - } - longPressStartTime = 0; - lastMoveMSX = 0; - lastMoveMSY = 0; - lastClickTime = clickTime; - isPanning = false; - hasEnteredDragMode = false; - resetMouseClickState(); - } + // ===== Reset ===== - //reset mouse click state - private void resetMouseClickState(){ - mouseLeftClick = false; - mouseRightClick = false; - mouseScrollClick = false; + public static void reset() { + BtMouseInputHandler.reset(); + CoordinateConverter.resetLearnedRange(); + ZoomPanManager.reset(); } } diff --git a/app/src/main/java/com/openterface/AOS/serial/TouchInputHandler.java b/app/src/main/java/com/openterface/AOS/serial/TouchInputHandler.java new file mode 100644 index 0000000..6486b4f --- /dev/null +++ b/app/src/main/java/com/openterface/AOS/serial/TouchInputHandler.java @@ -0,0 +1,770 @@ +package com.openterface.AOS.serial; + +import android.os.Handler; +import android.util.Log; +import android.view.InputDevice; +import android.view.MotionEvent; + +import com.openterface.AOS.target.HidManager; +import com.openterface.AOS.target.MouseModeManager; + +/** + * Handles finger touch input events on the video view. + * Implements KeyCmd-style gesture mapping: + * - 1-finger tap → left click + * - 1-finger double tap → double left click + * - 1-finger double tap and hold → drag mode + * - 1-finger move → mouse move + * - 2-finger tap → right click + * - 2-finger drag → scroll + * - 2-finger pinch → zoom + */ +public class TouchInputHandler { + private static final String TAG = "OP-TOUCH"; + + // ===== Timing constants ===== + private static final long TAP_DELAY_MS = 150; + private static final long TAP_TIMEOUT_MS = 400; + private static final long DOUBLE_TAP_TIMEOUT_MS = 400; + + // ===== Distance thresholds ===== + private static final float TAP_MOVE_THRESHOLD_PX = 10f; + private static final float DOUBLE_TAP_SLOP_PX = 32f; + private static final float TWO_FINGER_TAP_MAX_PRESS_GAP_MS = 100f; + // ===== Two-finger classification ===== + // A gesture is classified once, from the deltas accumulated since the first MOVE. + // Classifying on per-frame deltas made slow scrolling impossible: the centre never + // moved far within a single frame, so the gesture stayed "undecided" and the finger + // lift was reported as a right click. + private static final int TWO_FINGER_UNCLASSIFIED = 0; + private static final int TWO_FINGER_SCROLL = 1; + private static final int TWO_FINGER_PINCH = 2; + + private static final int AXIS_FREE = 0; + private static final int AXIS_HORIZONTAL = 1; + private static final int AXIS_VERTICAL = 2; + + private static final float SCROLL_MIN_TRAVEL_PX = 5f; // Δcenter must exceed this + private static final float PINCH_MIN_TRAVEL_PX = 8f; // Δdist must exceed this + private static final float GESTURE_DOMINANCE_RATIO = 1.8f; + private static final float AXIS_LOCK_ANGLE_DEG = 30f; + + // ===== State ===== + private final Handler handler = new Handler(); + + // Coordinate tracking + private float startMoveMSX, startMoveMSY; + private static float lastMoveMSX, lastMoveMSY; + private float lastMoveX, lastMoveY; + + // Gesture state + private boolean pendingDoubleTapDrag = false; + + // One-finger tap tracking + private float tapDownX, tapDownY; + private long tapDownTime; + private boolean tapCancelled = false; + private Runnable pendingSingleTap = null; + private long lastSingleTapTime = 0; + private float lastSingleTapX, lastSingleTapY; + + // Two-finger scroll tracking + private float twoFingerDownCenterX, twoFingerDownCenterY; + private float twoFingerPrevX, twoFingerPrevY; + private float twoFingerScrollAccumX = 0f; + private float twoFingerScrollAccumY = 0f; + private int twoFingerMode = TWO_FINGER_UNCLASSIFIED; + private int twoFingerAxis = AXIS_FREE; + private boolean inTwoFingerSequence = false; + private boolean isInitialTwoFingerMove = false; + + // Pending initial absolute position send + private Runnable pendingInitialAbsDown = null; + private boolean initialAbsDownSent = false; + + // Two-finger right-click state + private boolean isTwoFingerClick = false; + + // Pinch-to-zoom state + private float twoFingerStartDist = 0f; + private float twoFingerPrevDist = 0f; + + // Drag mode state — entered only by double-tap-and-drag + private boolean isDragMode = false; + + // Callback for zoom operations + private ZoomCallback zoomCallback; + + public interface ZoomCallback { + void onZoomAdjust(float zoomFactor); + } + + // ===== Initialization ===== + + public void setZoomCallback(ZoomCallback callback) { + this.zoomCallback = callback; + } + + // ===== Public API ===== + + /** + * Handle touch event. + * @return true if event was consumed + */ + public boolean onTouch(MotionEvent event) { + int action = event.getActionMasked(); + float x = event.getX(); + float y = event.getY(); + int toolType = event.getToolType(0); + int source = event.getSource(); + boolean isMouseEvent = (toolType == MotionEvent.TOOL_TYPE_MOUSE) + || ((source & InputDevice.SOURCE_MOUSE) == InputDevice.SOURCE_MOUSE); + + Log.d(TAG, "TOUCH_EVENT: action=" + action + " x=" + x + " y=" + y + + " toolType=" + toolType + " source=0x" + Integer.toHexString(source) + + " isMouseEvent=" + isMouseEvent + + " btMouseConnected=" + BtMouseInputHandler.isBtMouseConnected()); + + // Ignore mouse-generated touch events + // Android maps mouse button events to both GenericMotionEvent and MotionEvent + // We only want to process them via the BT mouse path (handleGenericMotionEvent), + // not the touch path. This applies regardless of BT mouse connection state + // because the event itself came from a mouse device (toolType=3 or SOURCE_MOUSE). + if (isMouseEvent) { + Log.d(TAG, "TOUCH_IGNORED: mouse event (toolType=" + toolType + " source=0x" + + Integer.toHexString(source) + ")"); + return false; + } + + // Ignore touch if BT mouse button is pressed (legacy protection) + if (BtMouseInputHandler.shouldIgnoreTouch()) { + Log.d(TAG, "TOUCH_IGNORED: shouldIgnoreTouch=true"); + return false; + } + + int pointerCount = event.getPointerCount(); + + // Handle two-finger gestures first + if (pointerCount >= 2 || action == MotionEvent.ACTION_POINTER_UP) { + return handleTwoFingerGesture(event); + } + + // Handle single-finger gestures + if (pointerCount == 1) { + return handleOneFingerGesture(event); + } + + return false; + } + + // ===== One-finger gesture handling ===== + + private boolean handleOneFingerGesture(MotionEvent event) { + int action = event.getActionMasked(); + float x = event.getX(); + float y = event.getY(); + + // A two-finger gesture can leave one finger on the screen. Swallow its + // remaining events, otherwise a finished pinch would move the mouse or + // even fire a click when that finger is lifted. + if (inTwoFingerSequence) { + if (action == MotionEvent.ACTION_UP || action == MotionEvent.ACTION_CANCEL) { + inTwoFingerSequence = false; + resetTwoFingerState(); + lastMoveX = 0; + lastMoveY = 0; + lastMoveMSX = 0; + lastMoveMSY = 0; + } + return true; + } + + switch (action) { + case MotionEvent.ACTION_DOWN: + // New gesture: clear any stale two-finger state (e.g. previous + // gesture was cancelled without a POINTER_UP event). + resetTwoFingerState(); + handleOneFingerDown(event, x, y); + break; + + case MotionEvent.ACTION_MOVE: + handleOneFingerMove(event, x, y); + break; + + case MotionEvent.ACTION_UP: + handleOneFingerUp(event, x, y); + break; + + case MotionEvent.ACTION_CANCEL: + handleOneFingerCancel(); + break; + } + return true; + } + + private void handleOneFingerDown(MotionEvent event, float x, float y) { + tapDownX = x; + tapDownY = y; + tapDownTime = event.getEventTime(); + tapCancelled = false; + pendingDoubleTapDrag = false; + initialAbsDownSent = false; + + // Sync the relative-mode baselines to this DOWN position. Several gesture exit + // paths zero them (cancel, double-tap-drag release, two-finger sequence end), so + // without this the first MOVE of the next gesture is measured against 0 and jumps. + lastMoveX = x; + lastMoveY = y; + lastMoveMSX = x; + lastMoveMSY = y; + + initHandActionDownMouse(event); + + // Second press inside the double-tap window → enter the drag *candidate* state. + // It only becomes a real drag once the finger moves past TAP_MOVE_THRESHOLD_PX + // (see handleOneFingerMove). A press that stays put is the second click of a + // double click (see handleOneFingerUp). + if (lastSingleTapTime > 0) { + long timeSinceLastTap = event.getEventTime() - lastSingleTapTime; + float distSinceLastTap = dist(x, y, lastSingleTapX, lastSingleTapY); + if (timeSinceLastTap < DOUBLE_TAP_TIMEOUT_MS && distSinceLastTap < DOUBLE_TAP_SLOP_PX) { + // Flush the first click now, so both clicks land inside the host's + // double-click window even when the first one is still queued. + flushPendingSingleTap(); + pendingDoubleTapDrag = true; + Log.d(TAG, "Double-tap candidate at (" + x + "," + y + ")"); + } + } + + // Schedule delayed initial abs down + pendingInitialAbsDown = () -> { + if (!initialAbsDownSent && MouseModeManager.isAbsoluteMode()) { + try { + float viewW = CoordinateConverter.getTouchViewWidth(); + float viewH = CoordinateConverter.getTouchViewHeight(); + float[] content = CoordinateConverter.mapToContentCoords( + startMoveMSX, startMoveMSY, viewW, viewH, + CoordinateConverter.isPortraitMode(), + 1.0f, 0, 0); + HidManager.sendHexAbsData(content[0], content[1]); + } catch (Exception e) { + Log.e(TAG, "pendingInitialAbsDown error: " + e.getMessage(), e); + } + initialAbsDownSent = true; + } + }; + handler.postDelayed(pendingInitialAbsDown, 80); + } + + private void handleOneFingerMove(MotionEvent event, float x, float y) { + float distMoved = dist(x, y, tapDownX, tapDownY); + if (distMoved > TAP_MOVE_THRESHOLD_PX && !tapCancelled && !isDragMode) { + cancelPendingSingleTap(); + tapCancelled = true; + + // Double-tap drag candidate: lock the drag only after the finger actually + // moves. Baselines are already kept current by handleNormalMove(), so the + // first drag frame must NOT re-sync them or the movement is applied twice. + if (pendingDoubleTapDrag) { + pendingDoubleTapDrag = false; + isDragMode = true; + Log.d(TAG, "Double-tap-drag: drag locked after " + distMoved + "px"); + } + } + + if (isDragMode) { + handleDragMove(x, y); + } else { + handleNormalMove(x, y); + } + lastMoveX = x; + lastMoveY = y; + } + + private void handleOneFingerUp(MotionEvent event, float x, float y) { + cancelPendingInitialAbsDown(); + + long duration = event.getEventTime() - tapDownTime; + float distLifted = dist(x, y, tapDownX, tapDownY); + + // Locked drag (entered in handleOneFingerMove) → release the left button. + // All four baselines are zeroed because the next DOWN re-syncs them. + if (isDragMode) { + Log.d(TAG, ">>> DRAG END: releasing left button"); + releaseMouseAndReset(); + isDragMode = false; + lastMoveX = 0; + lastMoveY = 0; + lastMoveMSX = 0; + lastMoveMSY = 0; + return; + } + + // Second press of a double tap that never moved: that press IS the second click. + // The first click was already flushed when the second DOWN arrived. + if (pendingDoubleTapDrag) { + pendingDoubleTapDrag = false; + Log.d(TAG, "Double tap → second click at (" + tapDownX + "," + tapDownY + ")"); + handleSingleClick(tapDownX, tapDownY); + lastSingleTapTime = 0; + lastMoveX = 0; + lastMoveY = 0; + lastMoveMSX = 0; + lastMoveMSY = 0; + return; + } + + // Check for tap + boolean validTap = !tapCancelled + && duration < TAP_TIMEOUT_MS + && distLifted <= TAP_MOVE_THRESHOLD_PX; + + if (validTap) { + // Delay the click so a following second press can flush it as the first + // click of a double click. The click lands at the DOWN position, not where + // the finger happened to lift. + cancelPendingSingleTap(); + pendingSingleTap = () -> { + handleSingleClick(tapDownX, tapDownY); + pendingSingleTap = null; + }; + lastSingleTapTime = event.getEventTime(); + lastSingleTapX = tapDownX; + lastSingleTapY = tapDownY; + handler.postDelayed(pendingSingleTap, TAP_DELAY_MS); + } + + if (!tapCancelled) { + handActionUpMouse(event); + } else { + HidManager.releaseMSRelData(); + lastMoveMSX = 0; + lastMoveMSY = 0; + } + } + + private void handleOneFingerCancel() { + cancelPendingInitialAbsDown(); + cancelPendingSingleTap(); + pendingDoubleTapDrag = false; + if (isDragMode) { + releaseMouseAndReset(); + isDragMode = false; + } else { + HidManager.releaseMSRelData(); + } + lastMoveMSX = 0; + lastMoveMSY = 0; + lastMoveX = 0; + lastMoveY = 0; + } + + // ===== Move handlers ===== + + private void handleNormalMove(float x, float y) { + if (MouseModeManager.isAbsoluteMode()) { + firePendingInitialAbsDown(); + float viewW = CoordinateConverter.getTouchViewWidth(); + float viewH = CoordinateConverter.getTouchViewHeight(); + float[] content = CoordinateConverter.mapToContentCoords( + x, y, viewW, viewH, + CoordinateConverter.isPortraitMode(), + 1.0f, 0, 0); + HidManager.sendHexAbsData(content[0], content[1]); + lastMoveMSX = x; + lastMoveMSY = y; + } else { + float zoomScale = ZoomPanManager.getEffectiveZoomScale(); + float effectiveDeltaScale = 1.0f / zoomScale; + float adjX = lastMoveMSX + (x - lastMoveX) * effectiveDeltaScale; + float adjY = lastMoveMSY + (y - lastMoveY) * effectiveDeltaScale; + HidManager.sendHexRelData("SecNullData", adjX, adjY, lastMoveMSX, lastMoveMSY); + lastMoveMSX = x; + lastMoveMSY = y; + } + } + + private void handleDragMove(float x, float y) { + if (MouseModeManager.isAbsoluteMode()) { + firePendingInitialAbsDown(); + float viewW = CoordinateConverter.getTouchViewWidth(); + float viewH = CoordinateConverter.getTouchViewHeight(); + float[] content = CoordinateConverter.mapToContentCoords( + x, y, viewW, viewH, + CoordinateConverter.isPortraitMode(), + 1.0f, 0, 0); + HidManager.sendHexAbsButtonClickData("SecLeftData", content[0], content[1]); + lastMoveMSX = x; + lastMoveMSY = y; + } else { + float zoomScale = ZoomPanManager.getEffectiveZoomScale(); + float effectiveDeltaScale = 1.0f / zoomScale; + float adjX = lastMoveMSX + (x - lastMoveX) * effectiveDeltaScale; + float adjY = lastMoveMSY + (y - lastMoveY) * effectiveDeltaScale; + HidManager.sendHexRelData("SecLeftData", adjX, adjY, lastMoveMSX, lastMoveMSY); + lastMoveMSX = x; + lastMoveMSY = y; + } + } + + // ===== Two-finger gesture handling ===== + + private boolean handleTwoFingerGesture(MotionEvent event) { + int action = event.getActionMasked(); + + switch (action) { + case MotionEvent.ACTION_POINTER_DOWN: + handleTwoFingerDown(event); + break; + + case MotionEvent.ACTION_MOVE: + handleTwoFingerMove(event); + break; + + case MotionEvent.ACTION_POINTER_UP: + handleTwoFingerPointerUp(event); + break; + + case MotionEvent.ACTION_CANCEL: + // Gesture aborted by the system or a parent view. Without this reset + // the stuck pinch flags turned every later two-finger gesture into + // a scroll gesture, so pinch appeared to work only once. + Log.d(TAG, "Two-finger gesture cancelled, resetting state"); + inTwoFingerSequence = false; + resetTwoFingerState(); + break; + } + return true; + } + + private void handleTwoFingerDown(MotionEvent event) { + cancelPendingSingleTap(); + // A pinch can start inside the 80ms window of the initial absolute-mode report; + // leaving it queued sends that report and jumps the cursor to finger 0. + cancelPendingInitialAbsDown(); + + // Until the last finger is lifted, single-finger events belong to this + // two-finger gesture and must not be handled as mouse input. + inTwoFingerSequence = true; + + // A second finger during a double-tap drag abandons the drag: the scroll and + // pinch frames would otherwise fight the pressed left button. + if (isDragMode) { + Log.d(TAG, "Two fingers down during drag → releasing left button"); + releaseMouseAndReset(); + isDragMode = false; + lastMoveX = 0; + lastMoveY = 0; + lastMoveMSX = 0; + lastMoveMSY = 0; + } + + twoFingerDownCenterX = (event.getX(0) + event.getX(1)) / 2f; + twoFingerDownCenterY = (event.getY(0) + event.getY(1)) / 2f; + twoFingerPrevX = twoFingerDownCenterX; + twoFingerPrevY = twoFingerDownCenterY; + twoFingerScrollAccumX = 0f; + twoFingerScrollAccumY = 0f; + twoFingerMode = TWO_FINGER_UNCLASSIFIED; + twoFingerAxis = AXIS_FREE; + // A right click needs both fingers to land together. A second finger arriving + // late (more than 100ms after the first) is a deliberate two-finger gesture. + isTwoFingerClick = (event.getEventTime() - event.getDownTime()) <= TWO_FINGER_TAP_MAX_PRESS_GAP_MS; + + // Record initial distance for pinch + twoFingerStartDist = calculateDistance( + event.getX(0), event.getY(0), + event.getX(1), event.getY(1)); + twoFingerPrevDist = twoFingerStartDist; + + // Always start a new two-finger gesture unclassified, otherwise flags left + // over from the previous gesture would disable pinch permanently. + isInitialTwoFingerMove = true; + } + + private void handleTwoFingerMove(MotionEvent event) { + // ACTION_MOVE only reaches this method while two fingers are down; the single + // finger left over after a POINTER_UP is swallowed by inTwoFingerSequence. + if (event.getPointerCount() != 2) { + return; + } + + float cx = (event.getX(0) + event.getX(1)) / 2f; + float cy = (event.getY(0) + event.getY(1)) / 2f; + float currentDist = calculateDistance( + event.getX(0), event.getY(0), + event.getX(1), event.getY(1)); + + if (isInitialTwoFingerMove) { + // Re-base on the first MOVE — the DOWN position is the sloppy landing spot, + // and classifying from it would count hand placement as gesture travel. + isInitialTwoFingerMove = false; + twoFingerDownCenterX = cx; + twoFingerDownCenterY = cy; + twoFingerStartDist = currentDist; + twoFingerPrevDist = currentDist; + twoFingerPrevX = cx; + twoFingerPrevY = cy; + return; + } + + // Classify from gesture-wide travel, not from this frame's delta. + float totalCenterX = cx - twoFingerDownCenterX; + float totalCenterY = cy - twoFingerDownCenterY; + float totalCenterDelta = (float) Math.hypot(totalCenterX, totalCenterY); + float totalDistDelta = Math.abs(currentDist - twoFingerStartDist); + + if (twoFingerMode == TWO_FINGER_UNCLASSIFIED) { + if (totalDistDelta > PINCH_MIN_TRAVEL_PX + && totalDistDelta > GESTURE_DOMINANCE_RATIO * totalCenterDelta) { + twoFingerMode = TWO_FINGER_PINCH; + isTwoFingerClick = false; + Log.d(TAG, "Gesture → PINCH (Δdist=" + totalDistDelta + + " Δcenter=" + totalCenterDelta + ")"); + } else if (totalCenterDelta > SCROLL_MIN_TRAVEL_PX + && totalCenterDelta > GESTURE_DOMINANCE_RATIO * totalDistDelta) { + twoFingerMode = TWO_FINGER_SCROLL; + isTwoFingerClick = false; + twoFingerAxis = lockAxis(totalCenterX, totalCenterY); + Log.d(TAG, "Gesture → SCROLL axis=" + twoFingerAxis + + " (Δcenter=" + totalCenterDelta + " Δdist=" + totalDistDelta + ")"); + } + // Below both thresholds the gesture stays undecided: hysteresis, so a tap + // with a few pixels of hand jitter is still a right click. + } + + if (twoFingerMode == TWO_FINGER_PINCH) { + // Incremental zoom: compare against last frame's distance + if (twoFingerPrevDist > 0f && currentDist > 0f) { + float zoomFactor = currentDist / twoFingerPrevDist; + // Guard against a single huge jump (finger teleport / dropped frame) + zoomFactor = Math.max(0.7f, Math.min(1.4f, zoomFactor)); + if (zoomCallback != null && Math.abs(zoomFactor - 1f) > 0.001f) { + zoomCallback.onZoomAdjust(zoomFactor); + } + } + } else if (twoFingerMode == TWO_FINGER_SCROLL) { + // Per-frame deltas drive the scroll output; the locked axis discards the + // other one so diagonal jitter cannot bleed into both directions. + float dx = cx - twoFingerPrevX; + float dy = cy - twoFingerPrevY; + + if (twoFingerAxis != AXIS_VERTICAL) { + twoFingerScrollAccumX += dx / 3f; + } + if (twoFingerAxis != AXIS_HORIZONTAL) { + twoFingerScrollAccumY += -dy / 3f; + } + + // CH9329 relative mouse reports carry five data bytes — fixed flag, buttons, + // X, Y, wheel — so there is no AC Pan byte for horizontal scrolling. The + // horizontal accumulator is drained but never sent; locking the axis still + // keeps diagonal jitter out of the vertical wheel. + int scrollX = (int) twoFingerScrollAccumX; + int scrollY = (int) twoFingerScrollAccumY; + + if (scrollX != 0) twoFingerScrollAccumX -= scrollX; + if (scrollY != 0) twoFingerScrollAccumY -= scrollY; + + if (scrollX != 0 || scrollY != 0) { + HidManager.handleTwoFingerPanSlideUpDown(scrollY); + } + } + + twoFingerPrevDist = currentDist; + twoFingerPrevX = cx; + twoFingerPrevY = cy; + } + + /** + * Lock the scroll to the dominant axis when the initial direction is within + * {@link #AXIS_LOCK_ANGLE_DEG} of it; otherwise keep both axes live. + */ + private int lockAxis(float totalDx, float totalDy) { + float absX = Math.abs(totalDx); + float absY = Math.abs(totalDy); + if (absX < 0.001f && absY < 0.001f) { + return AXIS_FREE; + } + float angleFromHorizontal = (float) Math.toDegrees(Math.atan2(absY, absX)); + if (angleFromHorizontal <= AXIS_LOCK_ANGLE_DEG) { + return AXIS_HORIZONTAL; + } + if (angleFromHorizontal >= 90f - AXIS_LOCK_ANGLE_DEG) { + return AXIS_VERTICAL; + } + return AXIS_FREE; + } + + private void handleTwoFingerPointerUp(MotionEvent event) { + // Capture state before reset for right-click detection + boolean wasClassified = twoFingerMode != TWO_FINGER_UNCLASSIFIED; + boolean wasClick = isTwoFingerClick; + + // ACTION_POINTER_UP still reports the lifting pointer in getPointerCount(), + // so the number of fingers that remain down is count - 1. Using the raw count + // kept the classification flags alive forever, which made pinch work only once + // and behave like a scroll afterwards. + int remaining = event.getPointerCount() - 1; + if (remaining <= 1) { + // Reset so the next pinch/scroll gesture can be recognized + resetTwoFingerState(); + } + + if (wasClassified) { + // Already scroll/pinch - no right click + } else if (wasClick) { + // Two-finger tap - send RIGHT CLICK + if (MouseModeManager.isAbsoluteMode()) { + float viewW = CoordinateConverter.getTouchViewWidth(); + float viewH = CoordinateConverter.getTouchViewHeight(); + float[] content = CoordinateConverter.mapToContentCoords( + twoFingerDownCenterX, twoFingerDownCenterY, viewW, viewH, + CoordinateConverter.isPortraitMode(), + 1.0f, 0, 0); + HidManager.sendHexAbsButtonClickData("SecRightData", content[0], content[1]); + HidManager.sendHexAbsReleaseData(content[0], content[1]); + } else { + // Same as the single tap: no movement along with the click. + HidManager.sendHexRelData("SecRightData", 0, 0, 0, 0); + HidManager.releaseMSRelData(); + } + Log.d(TAG, "Two-finger tap confirmed → RIGHT CLICK"); + } + } + + /** + * Clear every two-finger gesture flag so the next gesture starts clean. + * inTwoFingerSequence is intentionally left untouched - it is cleared when + * the last finger of the gesture is lifted. + */ + private void resetTwoFingerState() { + twoFingerMode = TWO_FINGER_UNCLASSIFIED; + twoFingerAxis = AXIS_FREE; + isTwoFingerClick = false; + twoFingerStartDist = 0f; + twoFingerPrevDist = 0f; + twoFingerScrollAccumX = 0f; + twoFingerScrollAccumY = 0f; + isInitialTwoFingerMove = false; + } + + // ===== Helper methods ===== + + private void firePendingInitialAbsDown() { + if (pendingInitialAbsDown != null && !initialAbsDownSent) { + handler.removeCallbacks(pendingInitialAbsDown); + pendingInitialAbsDown.run(); + pendingInitialAbsDown = null; + } + } + + private void cancelPendingInitialAbsDown() { + if (pendingInitialAbsDown != null) { + handler.removeCallbacks(pendingInitialAbsDown); + pendingInitialAbsDown = null; + } + } + + private void cancelPendingSingleTap() { + if (pendingSingleTap != null) { + handler.removeCallbacks(pendingSingleTap); + pendingSingleTap = null; + } + } + + /** + * Run the queued single click immediately instead of waiting for TAP_DELAY_MS. + * Called when the second press of a double tap arrives, so the first click is + * already on the wire and the second one lands inside the host's double-click window. + */ + private void flushPendingSingleTap() { + if (pendingSingleTap != null) { + handler.removeCallbacks(pendingSingleTap); + pendingSingleTap.run(); + pendingSingleTap = null; + } + } + + private void handleSingleClick(float x, float y) { + if (MouseModeManager.isAbsoluteMode()) { + float viewW = CoordinateConverter.getTouchViewWidth(); + float viewH = CoordinateConverter.getTouchViewHeight(); + float[] content = CoordinateConverter.mapToContentCoords( + x, y, viewW, viewH, + CoordinateConverter.isPortraitMode(), + 1.0f, 0, 0); + HidManager.sendHexAbsButtonClickData("SecLeftData", content[0], content[1]); + HidManager.sendHexAbsReleaseData(content[0], content[1]); + } else { + // Relative mode: a click must carry no movement. The previous form + // passed (x, y, 0, 0), so the view coordinate became a delta and was + // clamped to 127px — the host cursor jumped on every tap. + HidManager.sendHexRelData("SecLeftData", 0, 0, 0, 0); + HidManager.releaseMSRelData(); + } + Log.d(TAG, "Single tap → LEFT CLICK"); + } + + private void releaseMouseAndReset() { + if (MouseModeManager.isAbsoluteMode()) { + float viewW = CoordinateConverter.getTouchViewWidth(); + float viewH = CoordinateConverter.getTouchViewHeight(); + float[] content = CoordinateConverter.mapToContentCoords( + lastMoveMSX, lastMoveMSY, viewW, viewH, + CoordinateConverter.isPortraitMode(), + 1.0f, 0, 0); + HidManager.sendHexAbsReleaseData(content[0], content[1]); + } else { + HidManager.releaseMSRelData(); + } + } + + private void initHandActionDownMouse(MotionEvent event) { + startMoveMSX = event.getX(); + startMoveMSY = event.getY(); + } + + private void handActionUpMouse(MotionEvent event) { + float viewW = CoordinateConverter.getTouchViewWidth(); + float viewH = CoordinateConverter.getTouchViewHeight(); + float[] content = CoordinateConverter.mapToContentCoords( + startMoveMSX, startMoveMSY, viewW, viewH, + CoordinateConverter.isPortraitMode(), + 1.0f, 0, 0); + + if (MouseModeManager.isAbsoluteMode()) { + HidManager.sendHexAbsReleaseData(content[0], content[1]); + } else { + HidManager.releaseMSRelData(); + } + } + + private static float dist(float x1, float y1, float x2, float y2) { + float dx = x2 - x1; + float dy = y2 - y1; + return (float) Math.sqrt(dx * dx + dy * dy); + } + + private float calculateDistance(float x1, float y1, float x2, float y2) { + float dx = x2 - x1; + float dy = y2 - y1; + return (float) Math.sqrt(dx * dx + dy * dy); + } + + // ===== Reset ===== + + public void reset() { + isDragMode = false; + inTwoFingerSequence = false; + resetTwoFingerState(); + tapCancelled = false; + pendingDoubleTapDrag = false; + cancelPendingSingleTap(); + cancelPendingInitialAbsDown(); + lastMoveMSX = 0; + lastMoveMSY = 0; + lastMoveX = 0; + lastMoveY = 0; + } +} diff --git a/app/src/main/java/com/openterface/AOS/serial/UsbDeviceManager.java b/app/src/main/java/com/openterface/AOS/serial/UsbDeviceManager.java index 71a04eb..36fba05 100644 --- a/app/src/main/java/com/openterface/AOS/serial/UsbDeviceManager.java +++ b/app/src/main/java/com/openterface/AOS/serial/UsbDeviceManager.java @@ -71,13 +71,16 @@ public class UsbDeviceManager { private TextView tvReceivedData; private int currentBaudrate = -1; // Track the working baudrate private int preferredBaudrate = DEFAULT_BAUDRATE; + private boolean userSelectedBaudrate = false; // Track if user explicitly selected baudrate private UsbDevice pendingDevice; // Device waiting for connection private volatile boolean isConnecting = false; // Prevents double-connect from fallback + onDeviceOpen + private volatile boolean isReconnecting = false; // Single-flight guard for baudrate reconnects private volatile long isConnectingTimestamp = 0; // Tracks when isConnecting was set — used to detect stuck state // Stored for bulkTransfer fallback when port.write()'s testConnection fails on CH340 private UsbDeviceConnection serialConnection; private UsbEndpoint outEndpoint; + private UsbEndpoint inEndpoint; private static final int WRITE_WAIT_MILLIS = 2000; private static final int READ_WAIT_MILLIS = 2000; @@ -138,10 +141,19 @@ private UsbDevice detectOpenterfaceDevice() { {0x1A86, 0xFE0C} // Openterface Mini-KVM v2 Serial (supports 115200 only) }; + // CH9329 HID keyboard/mouse devices + int[][] ch9329Ids = { + {0x1A86, 0xE016}, // CH9329 HID Keyboard/Mouse + {0x1A86, 0xE017}, // CH9329 variant + {0x1A86, 0xE018}, // CH9329 variant + {0x1A86, 0xE019}, // CH9329 variant + }; + // Log supported device types Log.d(TAG, "Supported Openterface Serial Devices:"); Log.d(TAG, " - 1A86:7523 (Mini-KVM v1 - supports 9600 and 115200 baud)"); Log.d(TAG, " - 1A86:FE0C (Mini-KVM v2 - supports 115200 baud only)"); + Log.d(TAG, " - CH9329 HID: 1A86:E016/E017/E018/E019"); Log.d(TAG, " - Composite UVC+serial devices (345F:2109, 345F:2132, 534D:2109)"); UsbDevice compositeCandidate = null; @@ -190,6 +202,17 @@ private UsbDevice detectOpenterfaceDevice() { } } + // Priority 1.5: Check CH9329 HID keyboard/mouse devices + for (int[] ids : ch9329Ids) { + if (vid == ids[0] && pid == ids[1]) { + Log.i(TAG, "✅ Found CH9329 HID device: " + + String.format("%04X:%04X", vid, pid) + " - " + productName); + boolean hasPermission = usbManager.hasPermission(device); + Log.d(TAG, "Permission status for " + String.format("%04X:%04X", vid, pid) + ": " + hasPermission); + return device; + } + } + // Priority 2: Check composite UVC+serial devices (same VID/PID for both) for (int[] ids : COMPOSITE_DEVICE_IDS) { if (vid == ids[0] && pid == ids[1]) { @@ -258,26 +281,37 @@ private boolean hasSerialInterface(UsbDevice device) { */ private void storeBulkTransferResources(UsbDeviceConnection connection, UsbDevice device) { serialConnection = connection; - // Find the bulk OUT endpoint on the serial interface (class 255 / CDC / CDC-data) + // Find the bulk OUT and IN endpoints on the serial interface (class 255 / CDC / CDC-data) for (int i = 0; i < device.getInterfaceCount(); i++) { UsbInterface intf = device.getInterface(i); int intfClass = intf.getInterfaceClass(); if (intfClass == 255 || intfClass == 2 || intfClass == 10) { for (int j = 0; j < intf.getEndpointCount(); j++) { UsbEndpoint ep = intf.getEndpoint(j); - if (ep.getDirection() == UsbConstants.USB_DIR_OUT - && ep.getType() == UsbConstants.USB_ENDPOINT_XFER_BULK) { - outEndpoint = ep; - Log.d(TAG, "Found OUT endpoint: addr=0x" + - Integer.toHexString(ep.getAddress()) + - " maxPacket=" + ep.getMaxPacketSize() + - " on interface class=" + intfClass); - return; + if (ep.getType() == UsbConstants.USB_ENDPOINT_XFER_BULK) { + if (ep.getDirection() == UsbConstants.USB_DIR_OUT) { + outEndpoint = ep; + Log.d(TAG, "Found OUT endpoint: addr=0x" + + Integer.toHexString(ep.getAddress()) + + " maxPacket=" + ep.getMaxPacketSize() + + " on interface class=" + intfClass); + } else if (ep.getDirection() == UsbConstants.USB_DIR_IN) { + inEndpoint = ep; + Log.d(TAG, "Found IN endpoint: addr=0x" + + Integer.toHexString(ep.getAddress()) + + " maxPacket=" + ep.getMaxPacketSize() + + " on interface class=" + intfClass); + } } } } } - Log.w(TAG, "No bulk OUT endpoint found — bulkTransfer fallback unavailable"); + if (outEndpoint == null) { + Log.w(TAG, "No bulk OUT endpoint found — bulkTransfer fallback unavailable"); + } + if (inEndpoint == null) { + Log.w(TAG, "No bulk IN endpoint found — bulkTransfer read fallback unavailable"); + } } /** @@ -1312,35 +1346,30 @@ private boolean initializeFE0CPort(UsbDeviceConnection connection, int baudrate) } Log.d(TAG, "✅ FE0C interface claimed successfully"); - + // Store connection parameters for direct USB communication fe0cConnection = connection; fe0cInterface = dataInterface; currentBaudrate = baudrate; + // Reset FE0C safety counters on new connection + fe0cWriteCount = 0; + fe0cLastWriteTimeMs = 0; + fe0cOutEndpoint = null; // Force re-scan on first write + fe0cConsecutiveFailures = 0; // Reset circuit breaker + fe0cCircuitBreakerTrippedTimeMs = 0; + // Set up port reference (we still need this for compatibility) port = driver.getPorts().get(0); - Log.d(TAG, "Testing FE0C communication capability..."); - - // Test with a simple reset command that FE0C should handle - byte[] testCommand = {0x57, (byte)0xAB, 0x00, 0x02, 0x05, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x07}; // Reset command with checksum - - Log.d(TAG, "Sending FE0C test command: " + bytesToHex(testCommand)); - - // Test the connection with our special FE0C write method - boolean testResult = testFE0CConnection(testCommand); - - if (testResult) { - Log.i(TAG, "✅ FE0C device initialized successfully for command-only operation"); - Log.i(TAG, "Note: FE0C uses direct USB communication, not standard serial"); - return true; - } else { - Log.w(TAG, "⚠️ FE0C test command failed, but continuing anyway"); - Log.w(TAG, "FE0C device may still work for actual keyboard commands"); - // Don't fail here - FE0C might not respond to test commands but still work for keyboard data - return true; - } + // CRITICAL: Skip test command on REDMI 17C and similar devices + // The bulkTransfer() call during test can trigger kernel panic on some devices + // Instead, we trust the connection is valid and let the circuit breaker + // in writeFE0CData() handle any issues during actual data transfer + Log.i(TAG, "✅ FE0C device initialized successfully (skipping test command for safety)"); + Log.i(TAG, "Note: FE0C uses direct USB communication, not standard serial"); + Log.i(TAG, "Note: Test command skipped to prevent kernel panic on REDMI/similar devices"); + return true; } catch (Exception e) { Log.e(TAG, "❌ FE0C initialization failed: " + e.getMessage(), e); @@ -1361,6 +1390,20 @@ private boolean initializeFE0CPort(UsbDeviceConnection connection, int baudrate) // Add fields to store FE0C direct connection private UsbDeviceConnection fe0cConnection = null; private UsbInterface fe0cInterface = null; + private UsbEndpoint fe0cOutEndpoint = null; // Cached output endpoint + + // USB safety: rate limiting and connection health tracking + private volatile long fe0cLastWriteTimeMs = 0; + private volatile int fe0cWriteCount = 0; + private static final long FE0C_MIN_WRITE_INTERVAL_MS = 5; // Min 5ms between writes + private static final int FE0C_WARMUP_WRITES = 3; // First few writes use longer timeout + + // CRITICAL: Circuit breaker for REDMI 17C and similar devices with kernel USB bugs + // If we detect consecutive write failures, stop all USB writes to prevent kernel panic + private volatile int fe0cConsecutiveFailures = 0; + private static final int MAX_CONSECUTIVE_FAILURES = 3; // Trip circuit breaker after 3 failures + private volatile long fe0cCircuitBreakerTrippedTimeMs = 0; + private static final long CIRCUIT_BREAKER_COOLDOWN_MS = 30000; // 30 seconds before retry /** * Test FE0C connection by sending a test command @@ -1413,67 +1456,109 @@ private boolean writeFE0CData(byte[] data, int timeout) { Log.e(TAG, "❌ FE0C direct connection not available"); return false; } - - // Enhanced logging for FE0C - Log.d(TAG, "=== FE0C DIRECT USB WRITE ==="); - Log.d(TAG, "Data Length: " + data.length + " bytes"); - Log.d(TAG, "Timeout: " + timeout + "ms"); - Log.d(TAG, "Raw Hex Data: " + bytesToHex(data)); - - // Decode keyboard data if it looks like CH9329 keyboard command - if (data.length >= 5 && data[0] == 0x57 && data[1] == (byte)0xAB) { - Log.d(TAG, ">>> FE0C CH9329 Keyboard Command <<<"); - Log.d(TAG, "Header: " + String.format("%02X %02X", data[0] & 0xFF, data[1] & 0xFF)); - if (data.length >= 13) { - Log.d(TAG, "Address: " + String.format("%02X", data[2] & 0xFF)); - Log.d(TAG, "Command: " + String.format("%02X", data[3] & 0xFF)); - Log.d(TAG, "Length: " + String.format("%02X", data[4] & 0xFF)); - Log.d(TAG, "Modifier: " + String.format("%02X", data[5] & 0xFF)); - Log.d(TAG, "Reserved: " + String.format("%02X", data[6] & 0xFF)); - Log.d(TAG, "Key Code: " + String.format("%02X", data[7] & 0xFF)); - Log.d(TAG, "Key 2-6: " + String.format("%02X %02X %02X %02X %02X", - data[8] & 0xFF, data[9] & 0xFF, data[10] & 0xFF, data[11] & 0xFF, data[12] & 0xFF)); - if (data.length > 13) { - Log.d(TAG, "Checksum: " + String.format("%02X", data[13] & 0xFF)); - } + + // CRITICAL: Circuit breaker check - prevent kernel panic on REDMI 17C + // If circuit breaker tripped, refuse all writes until cooldown expires + if (fe0cConsecutiveFailures >= MAX_CONSECUTIVE_FAILURES) { + long timeSinceTripped = System.currentTimeMillis() - fe0cCircuitBreakerTrippedTimeMs; + if (timeSinceTripped < CIRCUIT_BREAKER_COOLDOWN_MS) { + // Still in cooldown period - refuse write + return false; + } else { + // Cooldown expired - reset and allow retry + Log.w(TAG, "⚠️ FE0C circuit breaker cooldown expired, resetting for retry"); + fe0cConsecutiveFailures = 0; + fe0cOutEndpoint = null; // Force endpoint re-scan } } - - // Find the bulk out endpoint - android.hardware.usb.UsbEndpoint outEndpoint = null; - for (int i = 0; i < fe0cInterface.getEndpointCount(); i++) { - android.hardware.usb.UsbEndpoint endpoint = fe0cInterface.getEndpoint(i); - if (endpoint.getType() == android.hardware.usb.UsbConstants.USB_ENDPOINT_XFER_BULK && - endpoint.getDirection() == android.hardware.usb.UsbConstants.USB_DIR_OUT) { - outEndpoint = endpoint; - break; + + // Safety: validate data before sending + if (data == null || data.length == 0) { + Log.w(TAG, "FE0C write skipped: data is null or empty"); + return false; + } + + // Safety: rate limiting to prevent USB driver overload on some devices + long now = System.currentTimeMillis(); + long elapsed = now - fe0cLastWriteTimeMs; + if (elapsed < FE0C_MIN_WRITE_INTERVAL_MS) { + // Throttle: sleep briefly to maintain minimum interval + try { Thread.sleep(FE0C_MIN_WRITE_INTERVAL_MS - elapsed); } + catch (InterruptedException ie) { Thread.currentThread().interrupt(); } + } + fe0cLastWriteTimeMs = System.currentTimeMillis(); + fe0cWriteCount++; + + // Cache the output endpoint on first use + if (fe0cOutEndpoint == null) { + for (int i = 0; i < fe0cInterface.getEndpointCount(); i++) { + UsbEndpoint endpoint = fe0cInterface.getEndpoint(i); + if (endpoint.getType() == UsbConstants.USB_ENDPOINT_XFER_BULK && + endpoint.getDirection() == UsbConstants.USB_DIR_OUT) { + fe0cOutEndpoint = endpoint; + Log.d(TAG, "✅ FE0C bulk out endpoint cached: maxPacketSize=" + endpoint.getMaxPacketSize()); + break; + } + } + if (fe0cOutEndpoint == null) { + Log.e(TAG, "❌ Could not find bulk out endpoint for FE0C"); + return false; } } - - if (outEndpoint == null) { - Log.e(TAG, "❌ Could not find bulk out endpoint for FE0C"); - return false; + + // Safety: for warmup writes (first few after connection), use shorter timeout + // to fail fast if the USB connection is unstable + int effectiveTimeout = timeout; + if (fe0cWriteCount <= FE0C_WARMUP_WRITES) { + effectiveTimeout = Math.min(timeout, 500); // 500ms max for first writes + Log.d(TAG, "FE0C warmup write #" + fe0cWriteCount + ", timeout=" + effectiveTimeout + "ms"); } - + // Send data directly via USB bulk transfer long startTime = System.currentTimeMillis(); - int result = fe0cConnection.bulkTransfer(outEndpoint, data, data.length, timeout); + int result = fe0cConnection.bulkTransfer(fe0cOutEndpoint, data, data.length, effectiveTimeout); long endTime = System.currentTimeMillis(); - + if (result >= 0) { - Log.d(TAG, "✅ FE0C direct write successful: " + result + " bytes in " + (endTime - startTime) + "ms"); + // Success - reset failure counter + fe0cConsecutiveFailures = 0; + if (fe0cWriteCount <= FE0C_WARMUP_WRITES) { + Log.d(TAG, "✅ FE0C write #" + fe0cWriteCount + ": " + result + " bytes in " + (endTime - startTime) + "ms"); + } return true; } else { - Log.e(TAG, "❌ FE0C direct write failed: " + result); + // Failure - increment failure counter + fe0cConsecutiveFailures++; + Log.e(TAG, "❌ FE0C write failed (result=" + result + "), write #" + fe0cWriteCount + + ", consecutive failures=" + fe0cConsecutiveFailures); + + // Trip circuit breaker if too many consecutive failures + if (fe0cConsecutiveFailures >= MAX_CONSECUTIVE_FAILURES) { + fe0cCircuitBreakerTrippedTimeMs = System.currentTimeMillis(); + Log.e(TAG, "🚨 FE0C circuit breaker TRIPPED! " + fe0cConsecutiveFailures + + " consecutive failures. Stopping all USB writes for " + + (CIRCUIT_BREAKER_COOLDOWN_MS / 1000) + " seconds to prevent kernel panic."); + } + + // Invalidate cached endpoint on error + fe0cOutEndpoint = null; return false; } - + } catch (Exception e) { Log.e(TAG, "❌ FE0C direct write error: " + e.getMessage(), e); + // Increment failure counter on exception + fe0cConsecutiveFailures++; + if (fe0cConsecutiveFailures >= MAX_CONSECUTIVE_FAILURES) { + fe0cCircuitBreakerTrippedTimeMs = System.currentTimeMillis(); + Log.e(TAG, "🚨 FE0C circuit breaker TRIPPED due to exception!"); + } + // Invalidate cached endpoint on error + fe0cOutEndpoint = null; return false; } } - + /** * Convert byte array to hex string for logging */ @@ -1711,6 +1796,9 @@ public void onReceive(Context context, Intent intent) { if (isOurDevice) { Log.i(TAG, "Openterface device detached — cleaning up connection state"); + // A locked keycap would otherwise keep the left button pressed on + // the host after the next reconnect. + com.openterface.AOS.target.HidManager.unlockAll(); if (onConnectionStatusListener != null) { onConnectionStatusListener.onDisconnected(); } @@ -2004,34 +2092,42 @@ private void applyDeviceSpecificConfig(UsbDevice device) { Log.d(TAG, "Applying configuration for device " + String.format("%04X:%04X", vid, pid)); - if (vid == 0x1A86) { - // CH340/CH341 based devices - if (pid == 0xFE0C) { + // Only set default baudrate if user hasn't explicitly selected one + if (!userSelectedBaudrate) { + if (vid == 0x1A86) { + // CH340/CH341 based devices + if (pid == 0xFE0C) { + preferredBaudrate = BAUDRATE_HIGHSPEED; + Log.d(TAG, "Device 1A86:FE0C - Mini-KVM v2: Default to 115200 baud"); + } else if (pid == 0x7523) { + // CH340 based devices - CH32V208 or CH9329 + // Qt auto-detects: tries 115200 first, then 9600 + // But this CH9329 firmware needs 115200 for config commands AND mouse to work + preferredBaudrate = BAUDRATE_HIGHSPEED; // 115200 + Log.d(TAG, "Device 1A86:7523 - Mini-KVM v1: Default to 115200 baud (CH9329 needs it)"); + } else { + Log.w(TAG, "Unknown 1A86 device PID: " + String.format("%04X", pid)); + preferredBaudrate = DEFAULT_BAUDRATE; + } + Log.d(TAG, "Will configure CH340 flow control after connection"); + } else if (vid == 0x345F && pid == 0x2109) { + // KVMGO-VGA (MS2109S) — composite UVC+serial device preferredBaudrate = BAUDRATE_HIGHSPEED; - Log.d(TAG, "Device 1A86:FE0C - Mini-KVM v2: Default to 115200 baud"); - } else if (pid == 0x7523) { - preferredBaudrate = BAUDRATE_LOWSPEED; - Log.d(TAG, "Device 1A86:7523 - Mini-KVM v1: Default to 9600 baud"); + Log.d(TAG, "Device 345F:2109 - KVMGO-VGA (MS2109S): Default to 115200 baud"); + } else if (vid == 0x345F && pid == 0x2132) { + // KVMGO — composite UVC+serial device + preferredBaudrate = BAUDRATE_HIGHSPEED; + Log.d(TAG, "Device 345F:2132 - KVMGO: Default to 115200 baud"); + } else if (vid == 0x534D && pid == 0x2109) { + // Mini-KVM v1 UVC interface — composite UVC+serial device + preferredBaudrate = BAUDRATE_HIGHSPEED; + Log.d(TAG, "Device 534D:2109 - Mini-KVM v1 UVC: Default to 115200 baud"); } else { - Log.w(TAG, "Unknown 1A86 device PID: " + String.format("%04X", pid)); + Log.w(TAG, "Non-standard device VID: " + String.format("%04X", vid) + " — using default baudrate"); preferredBaudrate = DEFAULT_BAUDRATE; } - Log.d(TAG, "Will configure CH340 flow control after connection"); - } else if (vid == 0x345F && pid == 0x2109) { - // KVMGO-VGA (MS2109S) — composite UVC+serial device - preferredBaudrate = BAUDRATE_HIGHSPEED; - Log.d(TAG, "Device 345F:2109 - KVMGO-VGA (MS2109S): Default to 115200 baud"); - } else if (vid == 0x345F && pid == 0x2132) { - // KVMGO — composite UVC+serial device - preferredBaudrate = BAUDRATE_HIGHSPEED; - Log.d(TAG, "Device 345F:2132 - KVMGO: Default to 115200 baud"); - } else if (vid == 0x534D && pid == 0x2109) { - // Mini-KVM v1 UVC interface — composite UVC+serial device - preferredBaudrate = BAUDRATE_HIGHSPEED; - Log.d(TAG, "Device 534D:2109 - Mini-KVM v1 UVC: Default to 115200 baud"); } else { - Log.w(TAG, "Non-standard device VID: " + String.format("%04X", vid) + " — using default baudrate"); - preferredBaudrate = DEFAULT_BAUDRATE; + Log.d(TAG, "User has selected baudrate, keeping: " + preferredBaudrate); } Log.d(TAG, "Preferred baudrate set to: " + preferredBaudrate); @@ -2175,6 +2271,7 @@ private void connectWithPermission(UsbDevice device) { if (onConnectionStatusListener != null) { onConnectionStatusListener.onConnected(preferredBaudrate); } + startReading(); return; } else { @@ -2197,7 +2294,9 @@ private void connectWithPermission(UsbDevice device) { if (onConnectionStatusListener != null) { onConnectionStatusListener.onConnected(baudrate); } + startReading(); + break; } @@ -2287,17 +2386,7 @@ private void advancedSerialRecovery(UsbDevice device) { private void closeDevice() { try { // Clean up FE0C direct connection if exists - if (fe0cConnection != null && fe0cInterface != null) { - Log.d(TAG, "Cleaning up FE0C direct connection"); - try { - fe0cConnection.releaseInterface(fe0cInterface); - Log.d(TAG, "FE0C interface released"); - } catch (Exception e) { - Log.w(TAG, "FE0C interface release warning: " + e.getMessage()); - } - fe0cConnection = null; - fe0cInterface = null; - } + releaseFe0cConnection(); // Clean up regular serial port if (port != null && port.isOpen()) { @@ -2312,9 +2401,38 @@ private void closeDevice() { isReading = false; // Stop reading loop serialConnection = null; outEndpoint = null; + inEndpoint = null; } } + /** + * Release the FE0C direct USB connection. + * + * The references must be dropped only after releaseInterface(); otherwise the interface + * stays claimed and the next claimInterface() call fails. Kept in sync with the cleanup + * used by closeDevice() — no extra close() call, to stay consistent with the FE0C path. + */ + private void releaseFe0cConnection() { + if (fe0cConnection == null) { + fe0cInterface = null; + fe0cOutEndpoint = null; + return; + } + if (fe0cInterface != null) { + try { + fe0cConnection.releaseInterface(fe0cInterface); + Log.d(TAG, "FE0C interface released"); + } catch (Exception e) { + Log.w(TAG, "FE0C interface release warning: " + e.getMessage()); + } + } + fe0cConnection = null; + fe0cInterface = null; + fe0cOutEndpoint = null; // Clear cached endpoint + fe0cConsecutiveFailures = 0; // Reset circuit breaker + fe0cCircuitBreakerTrippedTimeMs = 0; + } + public void release() { context.unregisterReceiver(usbReceiver); closeDevice(); @@ -2370,6 +2488,7 @@ private void openSerialConnection(UsbDevice serialDevice) { if (onConnectionStatusListener != null) { onConnectionStatusListener.onConnected(preferredBaudrate); } + startReading(); } else { Log.e(TAG, "Failed to establish serial connection with baudrate: " + preferredBaudrate); @@ -2434,16 +2553,25 @@ public boolean isConnected() { return true; } // For standard serial devices - return port != null && port.isOpen(); + if (port != null && port.isOpen()) { + return true; + } + // For CH9329 and other devices using USB bulk transfer (CH340 serial chip) + if (serialConnection != null && outEndpoint != null) { + return true; + } + return false; } /** * Set the preferred baudrate for serial communication + * This is called when user explicitly selects a baudrate * @param baudrate the preferred baudrate (9600 or 115200) */ public void setPreferredBaudrate(int baudrate) { this.preferredBaudrate = baudrate; - Log.d(TAG, "Preferred baudrate set to: " + baudrate); + this.userSelectedBaudrate = true; // Mark that user has made an explicit choice + Log.d(TAG, "Preferred baudrate set to: " + baudrate + " (user selected)"); } /** @@ -2455,28 +2583,73 @@ public int getPreferredBaudrate() { } /** - * Reconnect with the specified baudrate + * Reconnect with the specified baudrate. + * + * The teardown and reconnect run on the serial worker thread, so callers (usually UI + * event handlers) return immediately. connect() posts its own work to the same looper, + * so the two steps stay serialized. + * * @param baudrate the baudrate to use for reconnection - * @return true if reconnection successful, false otherwise */ - public boolean reconnectWithBaudrate(int baudrate) { - if (port != null && port.isOpen()) { + public void reconnectWithBaudrate(int baudrate) { + if (isReconnecting) { + Log.d(TAG, "reconnectWithBaudrate ignored: reconnect already in progress"); + return; + } + isReconnecting = true; + + // Camera is already open at this point, so we can connect serial immediately + setPreferredBaudrate(baudrate); + + mSerialAsyncHandler.post(() -> { try { - port.close(); - Log.d(TAG, "Closed existing connection for baudrate change"); + Log.d(TAG, "reconnectWithBaudrate: closing existing connection and clearing state..."); + + // Stop reading thread first + isReading = false; + + // Close serial port + if (port != null && port.isOpen()) { + try { + port.close(); + Log.d(TAG, "Closed serial port for baudrate change"); + } catch (Exception e) { + Log.e(TAG, "Error closing port for reconnection", e); + } + } + + // Release the FE0C interface before dropping the reference — otherwise it + // stays claimed and the next claimInterface() call fails. + releaseFe0cConnection(); + + // Clear ALL connection state so isConnected() returns false + // This is critical - otherwise connect() will skip reconnection! + port = null; + driver = null; + serialConnection = null; + outEndpoint = null; + inEndpoint = null; + isConnecting = false; + + // Notify disconnection if (onConnectionStatusListener != null) { onConnectionStatusListener.onDisconnected(); } - } catch (Exception e) { - Log.e(TAG, "Error closing port for reconnection", e); + + // Small delay to ensure device is ready for reconnection + try { + Thread.sleep(200); + } catch (InterruptedException e) { + Thread.currentThread().interrupt(); + return; + } + + Log.d(TAG, "reconnectWithBaudrate: calling connect() with new baudrate " + baudrate); + connect(); + } finally { + isReconnecting = false; } - } - - // Set the preferred baudrate and reconnect - // Camera is already open at this point, so we can connect serial immediately - setPreferredBaudrate(baudrate); - connect(); - return true; + }); } /** @@ -3122,4 +3295,5 @@ public static int getHIDModifiers(KeyEvent event) { return modifiers; } + } \ No newline at end of file diff --git a/app/src/main/java/com/openterface/AOS/serial/ZoomPanManager.java b/app/src/main/java/com/openterface/AOS/serial/ZoomPanManager.java new file mode 100644 index 0000000..8149b3a --- /dev/null +++ b/app/src/main/java/com/openterface/AOS/serial/ZoomPanManager.java @@ -0,0 +1,429 @@ +package com.openterface.AOS.serial; + +import android.graphics.Matrix; +import android.util.DisplayMetrics; +import android.util.Log; +import android.view.View; +import android.view.ViewGroup; + +import androidx.drawerlayout.widget.DrawerLayout; + +import com.openterface.AOS.activity.MainActivity; +import com.openterface.AOS.drawerLayout.ZoomLayoutDeal; +import com.serenegiant.widget.AspectRatioTextureView; + +/** + * Manages zoom and pan state for both portrait and landscape modes. + * + * Portrait mode: Uses TextureView.setTransform(Matrix) for GPU-quality scaling. + * Landscape mode: Uses DrawerLayout.setScaleX/Y for simpler scaling. + * + * Coordinates the PiP (Picture-in-Picture) indicator via ZoomLayoutDeal. + */ +public class ZoomPanManager { + private static final String TAG = "OP-ZOOM"; + + // Portrait mode state + private static float mPortraitZoomScale = 1.0f; + private static float mPortraitTranslateX = 0f; + private static float mPortraitTranslateY = 0f; + + // Scale limits + private static final float MIN_SCALE = 1.0f; + private static final float MAX_SCALE_LANDSCAPE = 2.0f; + private static final float MAX_SCALE_PORTRAIT = 5.0f; + private static final float DEFAULT_MAX_SCALE_PORTRAIT = 3.0f; + private static final float CONTAINER_HEIGHT_RATIO = 0.95f; + + // PiP state: enlargeView() grabs a bitmap and re-adds camera surfaces, so it + // must only run on the unzoomed -> zoomed transition, never per pinch frame. + private static boolean mPipVisible = false; + + // References + private static MainActivity activity; + private static DrawerLayout drawerLayout; + private static boolean isPortraitMode; + + // Callback for UI updates + private static Callbacks callbacks; + + public interface Callbacks { + void onBlackBorderOverlayUpdate(); + } + + // ===== Initialization ===== + + public static void init(MainActivity mainActivity, DrawerLayout drawer, boolean portrait, Callbacks cb) { + activity = mainActivity; + drawerLayout = drawer; + isPortraitMode = portrait; + callbacks = cb; + // A new listener means a fresh layout (startup or rotation): start unzoomed so + // the stored state always matches what is actually drawn. + reset(); + Log.d(TAG, "init: portrait=" + portrait + " drawerLayout=" + (drawer != null)); + } + + public static void setPortraitMode(boolean portrait) { + isPortraitMode = portrait; + } + + // ===== Getters ===== + + public static float getPortraitZoomScale() { + return mPortraitZoomScale; + } + + public static float getPortraitTranslateX() { + return mPortraitTranslateX; + } + + public static float getPortraitTranslateY() { + return mPortraitTranslateY; + } + + public static boolean isPortraitMode() { + return isPortraitMode; + } + + /** + * The drawer that hosts the preview in landscape; null in portrait, where the + * layout has no drawer at all. + */ + public static DrawerLayout getDrawerLayout() { + return drawerLayout; + } + + /** + * The PiP controller of the current activity. It is owned by the activity, so nothing + * in this class keeps the activity (or its views) alive through a static field. + */ + private static ZoomLayoutDeal pip() { + return activity == null ? null : activity.getZoomLayoutDeal(); + } + + /** + * Get effective zoom scale (minimum 1.0). + */ + public static float getEffectiveZoomScale() { + return Math.max(1.0f, mPortraitZoomScale); + } + + // ===== Setters ===== + + /** + * Set portrait pan translation from external source (e.g., PiP indicator drag). + */ + public static void setPortraitPan(float translateX, float translateY) { + mPortraitTranslateX = translateX; + mPortraitTranslateY = translateY; + } + + /** + * Set portrait zoom scale. + */ + public static void setPortraitZoomScale(float scale) { + mPortraitZoomScale = Math.max(MIN_SCALE, scale); + } + + // ===== Reset ===== + + /** + * Reset all zoom/pan state to default. + */ + public static void reset() { + mPortraitZoomScale = 1.0f; + mPortraitTranslateX = 0f; + mPortraitTranslateY = 0f; + mPipVisible = false; + } + + // ===== Zoom operations ===== + + /** + * Adjust zoom by a factor (e.g., 1.1 for zoom in, 0.9 for zoom out). + */ + public static void adjustZoom(float zoomFactor) { + if (isPortraitMode) { + adjustPortraitZoom(zoomFactor); + } else { + adjustLandscapeZoom(zoomFactor); + } + } + + private static void adjustPortraitZoom(float zoomFactor) { + if (activity == null || activity.mBinding == null || activity.mBinding.viewMainPreview == null) { + return; + } + + // Calculate max scale based on container size + float maxScale = calculateMaxPortraitScale(); + + // Calculate new scale + float currentScale = mPortraitZoomScale; + float newScale = currentScale * zoomFactor; + + // Clamp within bounds + newScale = Math.max(MIN_SCALE, Math.min(maxScale, newScale)); + mPortraitZoomScale = newScale; + + // If zooming out to minimum, hide PiP and reset translation + if (newScale <= MIN_SCALE) { + mPortraitTranslateX = 0f; + mPortraitTranslateY = 0f; + if (mPipVisible) { + ZoomLayoutDeal zoomLayoutDeal = pip(); + if (zoomLayoutDeal != null) { + zoomLayoutDeal.zoomOut(); + } + mPipVisible = false; + } + resetTextureViewHeight(); + } else if (!mPipVisible) { + // Only on the transition into zoom: enlargeView() captures a bitmap and + // re-adds surfaces, which is far too heavy to run on every pinch frame + // (it caused the pinch to stutter/freeze after the first step). + ZoomLayoutDeal zoomLayoutDeal = pip(); + if (zoomLayoutDeal != null) { + zoomLayoutDeal.enlargeView(); + } + mPipVisible = true; + } + + // Apply transform + applyPortraitZoomTransform(); + + // Update black border overlay + if (callbacks != null) { + callbacks.onBlackBorderOverlayUpdate(); + } + + Log.d(TAG, "Portrait zoom adjusted: newScale=" + newScale + + " translateX=" + mPortraitTranslateX + " translateY=" + mPortraitTranslateY); + } + + private static void adjustLandscapeZoom(float zoomFactor) { + if (drawerLayout == null) { + Log.d(TAG, "adjustZoom: drawerLayout not available, skipping zoom"); + return; + } + + // Get current scale + float currentScale = drawerLayout.getScaleX(); + float newScale = currentScale * zoomFactor; + + // Clamp within bounds + if (newScale < MIN_SCALE) { + newScale = MIN_SCALE; + if (mPipVisible) { + ZoomLayoutDeal zoomLayoutDeal = pip(); + if (zoomLayoutDeal != null) { + zoomLayoutDeal.zoomOut(); + } + mPipVisible = false; + } + } else if (newScale > MAX_SCALE_LANDSCAPE) { + newScale = MAX_SCALE_LANDSCAPE; + } + + // Show PiP once when zoomed in (not on every pinch frame) + if (newScale > MIN_SCALE && !mPipVisible) { + ZoomLayoutDeal zoomLayoutDeal = pip(); + if (zoomLayoutDeal != null) { + zoomLayoutDeal.enlargeView(); + } + mPipVisible = true; + } + + // Apply the new scale + drawerLayout.setScaleX(newScale); + drawerLayout.setScaleY(newScale); + + // The PiP indicator stands for the visible part of the frame, so it has to + // follow the zoom; it used to keep the size picked when the PiP was created. + ZoomLayoutDeal zoomLayoutDeal = pip(); + if (zoomLayoutDeal != null) { + zoomLayoutDeal.updateIndicatorSize(newScale); + } + + // Center pivot + drawerLayout.setPivotX(drawerLayout.getWidth() / 2f); + drawerLayout.setPivotY(drawerLayout.getHeight() / 2f); + + // Enforce clipping + drawerLayout.setClipChildren(true); + drawerLayout.setClipToPadding(true); + + Log.d(TAG, "Landscape zoom adjusted: newScale=" + newScale); + } + + /** + * Maximum portrait zoom scale. + * + * The portrait preview displays the (landscape) camera buffer at full view width, so + * the natural display height is viewWidth * bufferHeight / bufferWidth. Zoom is capped + * so the enlarged video can fill the available container height, and never above + * MAX_SCALE_PORTRAIT. + */ + private static float calculateMaxPortraitScale() { + if (activity == null || activity.getPreviewWidth() <= 0 || activity.getPreviewHeight() <= 0) { + return DEFAULT_MAX_SCALE_PORTRAIT; + } + + float viewWidth = CoordinateConverter.getTouchViewWidth(); + if (viewWidth <= 0) { + return DEFAULT_MAX_SCALE_PORTRAIT; + } + + float bufferWidth = activity.getPreviewWidth(); + float bufferHeight = activity.getPreviewHeight(); + // Height of the video as displayed at scale 1.0 (aspect preserved, full view width) + float naturalHeight = viewWidth * bufferHeight / bufferWidth; + + // Available height: the preview's container, or the screen as a fallback + float containerHeight = 0f; + if (activity.mBinding != null && activity.mBinding.viewMainPreview != null + && activity.mBinding.viewMainPreview.getParent() instanceof View) { + containerHeight = ((View) activity.mBinding.viewMainPreview.getParent()).getHeight(); + } + if (containerHeight <= 0) { + DisplayMetrics metrics = new DisplayMetrics(); + activity.getWindowManager().getDefaultDisplay().getRealMetrics(metrics); + containerHeight = metrics.heightPixels * CONTAINER_HEIGHT_RATIO; + } + + float maxScale = containerHeight / Math.max(1f, naturalHeight); + return Math.max(MIN_SCALE, Math.min(maxScale, MAX_SCALE_PORTRAIT)); + } + + // ===== Transform application ===== + + /** + * Apply the current portrait zoom transform to the TextureView. + * + * The camera buffer is landscape (e.g. 1920x1080). In portrait mode, the TextureView + * fills the screen but the video should display with correct aspect ratio. + * + * Approach: Set the TextureView's aspect ratio to match the camera buffer, + * so the view measures itself correctly (landscape aspect in portrait screen = centered with black bars). + * Then apply zoom/pan Matrix only when zoomed in (scale > 1.0). + */ + public static void applyPortraitZoomTransform() { + if (activity == null || activity.mBinding == null + || activity.mBinding.viewMainPreview == null) { + return; + } + + AspectRatioTextureView textureView = activity.mBinding.viewMainPreview; + float viewWidth = textureView.getWidth(); + float viewHeight = textureView.getHeight(); + if (viewWidth <= 0 || viewHeight <= 0) return; + + float bufferWidth = activity.getPreviewWidth(); + float bufferHeight = activity.getPreviewHeight(); + if (bufferWidth <= 0 || bufferHeight <= 0) return; + + // Set the TextureView's aspect ratio to match the camera buffer. + // This makes the view measure itself with the correct proportions: + // - In portrait screen, a landscape aspect ratio view will be centered with black bars top/bottom + // - The camera frames fill the view uniformly (no stretching) + textureView.setAspectRatio((int) bufferWidth, (int) bufferHeight); + + // Apply zoom and pan Matrix only when zoomed in + if (mPortraitZoomScale > 1.0f) { + Matrix matrix = new Matrix(); + float pivotX = viewWidth / 2f; + float pivotY = viewHeight / 2f; + matrix.postScale(mPortraitZoomScale, mPortraitZoomScale, pivotX, pivotY); + matrix.postTranslate(mPortraitTranslateX, mPortraitTranslateY); + textureView.setTransform(matrix); + } else { + // No zoom — clear any previous transform + textureView.setTransform(null); + } + textureView.invalidate(); + + // Sync PiP indicator + ZoomLayoutDeal zoomLayoutDeal = pip(); + if (zoomLayoutDeal != null) { + zoomLayoutDeal.updateIndicatorFromMainView( + mPortraitZoomScale, mPortraitTranslateX, mPortraitTranslateY); + } + + Log.d(TAG, "Portrait transform: buffer=" + bufferWidth + "x" + bufferHeight + + " view=" + viewWidth + "x" + viewHeight + " zoom=" + mPortraitZoomScale); + } + + /** + * Apply current portrait transform (called by ZoomLayoutDeal after updating pan). + */ + public static void applyCurrentPortraitTransform() { + applyPortraitZoomTransform(); + if (callbacks != null) { + callbacks.onBlackBorderOverlayUpdate(); + } + } + + // ===== TextureView reset ===== + + private static void resetTextureViewHeight() { + if (activity == null || activity.mBinding == null || activity.mBinding.viewMainPreview == null) { + return; + } + + AspectRatioTextureView textureView = activity.mBinding.viewMainPreview; + + // Portrait keeps the aspect-ratio driven height (WRAP_CONTENT) declared in the + // portrait layout, so the preview never gets resized during a pinch gesture. + // Only make sure a previous landscape resize left MATCH_PARENT behind. + ViewGroup.LayoutParams params = textureView.getLayoutParams(); + if (isPortraitMode && params != null + && params.height != ViewGroup.LayoutParams.WRAP_CONTENT) { + params.height = ViewGroup.LayoutParams.WRAP_CONTENT; + textureView.setLayoutParams(params); + } + + // Apply the current transform (identity at scale 1.0) + applyPortraitZoomTransform(); + } + + // ===== Pan adjustment (for pinch-to-zoom auto-pan) ===== + + /** + * Adjust pan to keep a point centered after zoom. + */ + public static void adjustPanForZoom(float oldScale, float newScale, float focusX, float focusY) { + // Calculate the point that was at focus before zoom + float pivotX = CoordinateConverter.getTouchViewWidth() / 2f; + float pivotY = CoordinateConverter.getTouchViewHeight() / 2f; + + // Adjust translation to keep focus point stationary + float scaleRatio = newScale / oldScale; + mPortraitTranslateX = focusX - scaleRatio * (focusX - mPortraitTranslateX - pivotX) - pivotX; + mPortraitTranslateY = focusY - scaleRatio * (focusY - mPortraitTranslateY - pivotY) - pivotY; + + // Clamp pan to valid range + clampPortraitPan(); + } + + /** + * Clamp portrait pan to valid range based on current zoom. + */ + public static void clampPortraitPan() { + if (mPortraitZoomScale <= 1.0f) { + mPortraitTranslateX = 0f; + mPortraitTranslateY = 0f; + return; + } + + float viewW = CoordinateConverter.getTouchViewWidth(); + float viewH = CoordinateConverter.getTouchViewHeight(); + + // Maximum translation: (zoom - 1) * viewSize / 2 + float maxTranslateX = (mPortraitZoomScale - 1f) * viewW / 2f; + float maxTranslateY = (mPortraitZoomScale - 1f) * viewH / 2f; + + mPortraitTranslateX = Math.max(-maxTranslateX, Math.min(maxTranslateX, mPortraitTranslateX)); + mPortraitTranslateY = Math.max(-maxTranslateY, Math.min(maxTranslateY, mPortraitTranslateY)); + } +} diff --git a/app/src/main/java/com/openterface/AOS/target/CH9329MSKBMap.java b/app/src/main/java/com/openterface/AOS/target/CH9329MSKBMap.java index c11b1ba..7545e49 100644 --- a/app/src/main/java/com/openterface/AOS/target/CH9329MSKBMap.java +++ b/app/src/main/java/com/openterface/AOS/target/CH9329MSKBMap.java @@ -96,6 +96,9 @@ public static Map MSAbsData() { MSAbsData.put("SecLeftData", "01"); MSAbsData.put("SecRightData", "02"); MSAbsData.put("SecMiddleData", "04"); + // side buttons (browser back/forward) + MSAbsData.put("SecBackData", "08"); + MSAbsData.put("SecForwardData", "10"); //three and four is x,five and six is y @@ -116,6 +119,9 @@ public static Map MSRelData() { MSRelData.put("SecLeftData", "01"); MSRelData.put("SecRightData", "02"); MSRelData.put("SecMiddleData", "04"); + // side buttons (browser back/forward) + MSRelData.put("SecBackData", "08"); + MSRelData.put("SecForwardData", "10"); //three and four is x,five and six is y @@ -213,7 +219,7 @@ public static Map getKeyCodeMap() { keyCodeMap.put("+", "2E");keyCodeMap.put("=", "2E"); keyCodeMap.put("[", "2F");keyCodeMap.put("{", "2F"); keyCodeMap.put("]", "30");keyCodeMap.put("}", "30"); - keyCodeMap.put("|", "64");keyCodeMap.put("\\", "64"); + keyCodeMap.put("|", "31");keyCodeMap.put("\\", "31"); keyCodeMap.put(";", "33");keyCodeMap.put(":", "33"); keyCodeMap.put("'", "34");keyCodeMap.put("\"", "34"); keyCodeMap.put(",", "36");keyCodeMap.put("<", "36"); @@ -269,7 +275,7 @@ public static Map getKeyCodeMap() { keyCodeMap.put("SPACE", "2C"); keyCodeMap.put("BACK", "2A"); keyCodeMap.put("ENTER", "28"); - keyCodeMap.put("DEL", "4C"); + keyCodeMap.put("DEL", "2A"); // KEYCODE_DEL is Backspace, not Delete keyCodeMap.put("Esc", "29"); keyCodeMap.put("PrtSc", "46"); keyCodeMap.put("ScrLk", "47"); @@ -291,6 +297,17 @@ public static Map getKeyCodeMap() { keyCodeMap.put("NUMPAD_MULTIPLY", "55"); keyCodeMap.put("NUMPAD_SUBTRACT", "56"); keyCodeMap.put("NUMPAD_ADD", "57"); + keyCodeMap.put("NUMPAD_ENTER", "58"); + keyCodeMap.put("NUMPAD_1", "59"); + keyCodeMap.put("NUMPAD_2", "5A"); + keyCodeMap.put("NUMPAD_3", "5B"); + keyCodeMap.put("NUMPAD_4", "5C"); + keyCodeMap.put("NUMPAD_5", "5D"); + keyCodeMap.put("NUMPAD_6", "5E"); + keyCodeMap.put("NUMPAD_7", "5F"); + keyCodeMap.put("NUMPAD_8", "60"); + keyCodeMap.put("NUMPAD_9", "61"); + keyCodeMap.put("NUMPAD_DECIMAL", "63"); //release keyboard data keyCodeMap.put("release", "57AB00020800000000000000000C"); diff --git a/app/src/main/java/com/openterface/AOS/target/HidManager.java b/app/src/main/java/com/openterface/AOS/target/HidManager.java index 8da25ec..723db29 100644 --- a/app/src/main/java/com/openterface/AOS/target/HidManager.java +++ b/app/src/main/java/com/openterface/AOS/target/HidManager.java @@ -3,6 +3,7 @@ import android.util.Log; import com.openterface.AOS.jni.KeymodJNI; +import com.openterface.AOS.serial.CH9329Function; import com.openterface.AOS.serial.UsbDeviceManager; /** @@ -210,6 +211,13 @@ public static void width_height(int width, int height) { } public static void sendHexAbsData(float x, float y) { + // Per-frame log with a stack trace: this runs on every absolute move (60~120 + // times per second), so it must stay behind the shared debug switch. + if (CH9329Function.DEBUG_SEND_LOG) { + Log.d("OP-HID", "sendHexAbsData CALLED: x=" + x + " y=" + y + + " btMouseConnected=" + com.openterface.AOS.serial.BtMouseInputHandler.isBtMouseConnected(), + new Exception("Stack trace")); + } if (useCoreImplementation && coreLibraryLoaded) { MouseManagerCore.sendHexAbsData(x, y); } else { @@ -225,11 +233,104 @@ public static void sendHexAbsButtonClickData(String mouseClick, float x, float y } } - public static void sendHexAbsDragData(float x, float y) { + /** + * Absolute move that explicitly releases any held button. Use it at the end of a + * click, a drag or a remote client click — a plain move keeps the held mask alive. + */ + public static void sendHexAbsReleaseData(float x, float y) { + if (useCoreImplementation && coreLibraryLoaded) { + MouseManagerCore.sendHexAbsReleaseData(x, y); + } else { + MouseManager.sendHexAbsReleaseData(x, y); + } + } + + /** + * Set the gesture buttons (tap, double-tap drag, BT mouse drag, remote client). + * Pass null or "SecNullData" to release them; locked keycaps stay pressed. + * Both implementations are updated so the mask survives a switch between them, + * in the same way as setMouseSpeedMultiplier(). + */ + public static void setHeldButton(String button) { + MouseManager.setHeldButton(button); + MouseManagerCore.setHeldButtons(coreButtonMask(button)); + } + + /** Add a button that stays pressed until it is unlocked (keycap long press). */ + public static void lockButton(String button) { + MouseManager.lockButton(button); + MouseManagerCore.lockButtons(coreButtonMask(button)); + } + + /** Drop one locked button (the keycap was tapped again to unlock). */ + public static void unlockButton(String button) { + MouseManager.unlockButton(button); + MouseManagerCore.unlockButtons(coreButtonMask(button)); + } + + /** Drop every gesture and locked button: disconnect, rotation, activity stop. */ + public static void unlockAll() { + MouseManager.unlockAll(); + MouseManagerCore.unlockAll(); + } + + /** Buttons that are currently kept pressed by a locked keycap. */ + public static int getLockedMask() { + return (useCoreImplementation && coreLibraryLoaded) + ? MouseManagerCore.getLockedButtons() + : MouseManager.getLockedMask(); + } + + private static int coreButtonMask(String button) { + if (button == null) return KeymodJNI.MS_BTN_NONE; + switch (button) { + case "SecLeftData": return KeymodJNI.MS_BTN_LEFT; + case "SecRightData": return KeymodJNI.MS_BTN_RIGHT; + case "SecMiddleData": return KeymodJNI.MS_BTN_MIDDLE; + case "SecBackData": return KeymodJNI.MS_BTN_BACK; + case "SecForwardData": return KeymodJNI.MS_BTN_FORWARD; + default: return KeymodJNI.MS_BTN_NONE; + } + } + + /** + * Send BT mouse absolute move with pre-converted HID coordinates. + * Bypasses Handler's view→HID scaling to avoid double-scaling. + * @param hidX HID X coordinate [0, 4095] + * @param hidY HID Y coordinate [0, 4095] + */ + public static void sendBtMouseHidAbsData(float hidX, float hidY) { + if (useCoreImplementation && coreLibraryLoaded) { + MouseManagerCore.sendBtMouseHidAbsData(hidX, hidY); + } else { + MouseManager.sendBtMouseHidAbsData(hidX, hidY); + } + } + + /** + * Send BT mouse button click with pre-converted HID coordinates. + * Bypasses Handler's view→HID scaling to avoid double-scaling. + * @param mouseClick Button key (e.g., "SecLeftData") + * @param hidX HID X coordinate [0, 4095] + * @param hidY HID Y coordinate [0, 4095] + */ + public static void sendBtMouseHidAbsButtonClickData(String mouseClick, float hidX, float hidY) { + if (useCoreImplementation && coreLibraryLoaded) { + MouseManagerCore.sendBtMouseHidAbsButtonClickData(mouseClick, hidX, hidY); + } else { + MouseManager.sendBtMouseHidAbsButtonClickData(mouseClick, hidX, hidY); + } + } + + /** + * BT mouse release with pre-converted HID coordinates [0, 4095]. Clears the held + * button, so a finished drag really ends in the host. + */ + public static void sendBtMouseHidAbsReleaseData(float hidX, float hidY) { if (useCoreImplementation && coreLibraryLoaded) { - MouseManagerCore.sendHexAbsDragData(x, y); + MouseManagerCore.sendBtMouseHidAbsReleaseData(hidX, hidY); } else { - MouseManager.sendHexAbsDragData(x, y); + MouseManager.sendBtMouseHidAbsReleaseData(hidX, hidY); } } @@ -243,7 +344,7 @@ public static void handleDoubleClickAbs(float x, float y) { public static void releaseMSAbsData(String x0, String x1, String y0, String y1) { if (useCoreImplementation && coreLibraryLoaded) { - MouseManagerCore.releaseMSAbsData(); + MouseManagerCore.releaseMSAbsData(0f, 0f); } else { MouseManager.releaseMSAbsData(x0, x1, y0, y1); } @@ -271,6 +372,11 @@ public static void handleTwoFingerPanSlideUpDown(float slideData) { } public static void sendHexRelData(String mouseClick, float sx, float sy, float lx, float ly) { + // Called on every relative move frame, so it stays behind the debug switch. + if (CH9329Function.DEBUG_SEND_LOG) { + Log.d("OP-HID", "sendHexRelData CALLED: click=" + mouseClick + + " cur=(" + sx + "," + sy + ") prev=(" + lx + "," + ly + ")"); + } if (useCoreImplementation && coreLibraryLoaded) { MouseManagerCore.sendHexRelData(mouseClick, sx, sy, lx, ly); } else { diff --git a/app/src/main/java/com/openterface/AOS/target/KeyBoardManager.java b/app/src/main/java/com/openterface/AOS/target/KeyBoardManager.java index 2337dc8..70d2a24 100644 --- a/app/src/main/java/com/openterface/AOS/target/KeyBoardManager.java +++ b/app/src/main/java/com/openterface/AOS/target/KeyBoardManager.java @@ -39,11 +39,16 @@ import com.openterface.AOS.serial.UsbDeviceManager; import java.io.IOException; -import java.util.Arrays; import java.util.Map; public class KeyBoardManager { private static final String TAG = "OP-KB"; + + /** + * Verbose per-key logging. Keyboard sends happen on every key press, so the + * trace output stays off unless someone is actively debugging the HID path. + */ + private static final boolean DEBUG_KEY_LOG = false; private static Context context; private static Map currentKeyCodeMap; private static UsbDeviceManager usbDeviceManager; // Add static reference for enhanced FE0C support @@ -77,11 +82,10 @@ public static void setKeyBoardLanguage() { resolvedLocale; if (baseLang.equals("de")) { currentKeyCodeMap = KeyMapConfig_De.getKeyCodeMap(); - Log.d(TAG, "language is de"); } else { currentKeyCodeMap = CH9329MSKBMap.getKeyCodeMap(); - Log.d(TAG, "language is us"); } + Log.d(TAG, "Keyboard layout: " + baseLang); } /** @@ -93,27 +97,30 @@ public static Map getCurrentKeyCodeMap() { } public static String getFunctionKey(KeyEvent event, int keyCode) { - boolean isCtrlPressed = (event.getMetaState() & KeyEvent.META_CTRL_ON) != 0; - boolean isShiftPressed = (event.getMetaState() & KeyEvent.META_SHIFT_ON) != 0; - boolean isAltPressed = (event.getMetaState() & KeyEvent.META_ALT_ON) != 0; - - if (isCtrlPressed) { - if (keyCode != KeyEvent.KEYCODE_CTRL_LEFT && keyCode != KeyEvent.KEYCODE_CTRL_RIGHT) { - Log.d(TAG, "click ctrl and other click"); - return "01"; - } - } else if (isShiftPressed) { - if (keyCode != KeyEvent.KEYCODE_SHIFT_LEFT && keyCode != KeyEvent.KEYCODE_SHIFT_RIGHT) { - Log.d(TAG, "click shift and other click"); - return "02"; - } - } else if (isAltPressed) { - if (keyCode != KeyEvent.KEYCODE_ALT_LEFT && keyCode != KeyEvent.KEYCODE_ALT_RIGHT) { - Log.d(TAG, "click alt and other click"); - return "04"; - } + int metaState = event.getMetaState(); + int modifier = 0; + + // Bits match the CH9329 modifier byte: Ctrl, Shift, Alt, Win. Each is + // assembled independently so that combinations such as Ctrl+Shift keep + // both bits instead of only the first one matched. + if ((metaState & KeyEvent.META_CTRL_ON) != 0 + && keyCode != KeyEvent.KEYCODE_CTRL_LEFT && keyCode != KeyEvent.KEYCODE_CTRL_RIGHT) { + modifier |= 0x01; + } + if ((metaState & KeyEvent.META_SHIFT_ON) != 0 + && keyCode != KeyEvent.KEYCODE_SHIFT_LEFT && keyCode != KeyEvent.KEYCODE_SHIFT_RIGHT) { + modifier |= 0x02; + } + if ((metaState & KeyEvent.META_ALT_ON) != 0 + && keyCode != KeyEvent.KEYCODE_ALT_LEFT && keyCode != KeyEvent.KEYCODE_ALT_RIGHT) { + modifier |= 0x04; + } + if ((metaState & KeyEvent.META_META_ON) != 0 + && keyCode != KeyEvent.KEYCODE_META_LEFT && keyCode != KeyEvent.KEYCODE_META_RIGHT) { + modifier |= 0x08; } - return "00"; + + return String.format("%02X", modifier); } public static String getKeyName(int keyCode) { @@ -126,7 +133,9 @@ public static String getKeyName(int keyCode) { public static void DetectedCharacter(String functionKey, String pressedChar, String targetChars){ if (targetChars.contains(pressedChar)) { - Log.d(TAG, "Detected special character: " + pressedChar); + if (DEBUG_KEY_LOG) { + Log.d(TAG, "Detected special character: " + pressedChar); + } sendKeyboardRequest(functionKey, pressedChar); new Handler(Looper.getMainLooper()).postDelayed(new Runnable() { @Override @@ -153,20 +162,20 @@ public static boolean sendKeyBoardDataEnhanced(UsbDeviceManager usbDeviceManager } try { - Log.d(TAG, "=== ENHANCED KEYBOARD DEBUG ==="); - Log.d(TAG, "Input - functionKey: '" + functionKey + "', keyName: '" + keyName + "'"); - Log.d(TAG, "Device Type: " + (usbDeviceManager.isFE0CDevice() ? "FE0C" : "Other")); - + if (DEBUG_KEY_LOG) { + Log.d(TAG, "Enhanced keyboard: functionKey='" + functionKey + "' keyName='" + keyName + + "' device=" + (usbDeviceManager.isFE0CDevice() ? "FE0C" : "Other")); + } + // Get the individual key code from the mapping String keyCode = currentKeyCodeMap.get(keyName); if (keyCode == null) { - Log.w(TAG, "❌ Key '" + keyName + "' not found in mapping"); - Log.d(TAG, "Available keys in map: " + currentKeyCodeMap.keySet().toString()); + Log.w(TAG, "Key '" + keyName + "' not found in mapping"); + if (DEBUG_KEY_LOG) { + Log.d(TAG, "Available keys in map: " + currentKeyCodeMap.keySet()); + } return false; } - - Log.d(TAG, "✅ Found key code for '" + keyName + "': " + keyCode); - // Build the complete keyboard command like the original method String prefix1 = CH9329MSKBMap.getKeyCodeMap().get("prefix1"); String prefix2 = CH9329MSKBMap.getKeyCodeMap().get("prefix2"); @@ -175,32 +184,25 @@ public static boolean sendKeyBoardDataEnhanced(UsbDeviceManager usbDeviceManager String dataLen = CH9329MSKBMap.DataLen().get("DataLenKB"); String dataNull = CH9329MSKBMap.DataNull().get("DataNull"); - Log.d(TAG, "Command components:"); - Log.d(TAG, " prefix1: " + prefix1); - Log.d(TAG, " prefix2: " + prefix2); - Log.d(TAG, " address: " + address); - Log.d(TAG, " cmdKB: " + cmdKB); - Log.d(TAG, " dataLen: " + dataLen); - Log.d(TAG, " functionKey: " + functionKey); - Log.d(TAG, " keyCode: " + keyCode); - Log.d(TAG, " dataNull: " + dataNull); - + // Command components are constant per key map and were logged on every + // key press before; only the assembled frame is worth a trace line. String sendKBData = prefix1 + prefix2 + address + cmdKB + dataLen + functionKey + dataNull + keyCode + dataNull + dataNull + dataNull + dataNull + dataNull; - - Log.d(TAG, "Command before checksum: " + sendKBData); - + // Add checksum sendKBData = sendKBData + CH9329Function.makeChecksum(sendKBData); - - Log.d(TAG, "✅ Final command with checksum: " + sendKBData); + + if (DEBUG_KEY_LOG) { + Log.d(TAG, "Keyboard frame: " + sendKBData); + } CH9329Function.checkSendLogData(sendKBData); - + byte[] keyboardDataBytes = CH9329Function.hexStringToByteArray(sendKBData); - Log.d(TAG, "Command as bytes: " + java.util.Arrays.toString(keyboardDataBytes)); - + boolean result = usbDeviceManager.sendKeyboardCommandFE0C(keyboardDataBytes); - Log.d(TAG, "Send result: " + (result ? "SUCCESS" : "FAILED")); + if (DEBUG_KEY_LOG) { + Log.d(TAG, "Send result: " + (result ? "SUCCESS" : "FAILED")); + } return result; } catch (Exception e) { @@ -212,7 +214,9 @@ public static boolean sendKeyBoardDataEnhanced(UsbDeviceManager usbDeviceManager public static void sendKeyBoardData(String functionKey, String keyName) { if (keyName != null) { - Log.d(TAG, "keyName: " + keyName); + if (DEBUG_KEY_LOG) { + Log.d(TAG, "sendKeyBoardData keyName=" + keyName); + } sendKeyboardRequest(functionKey, keyName); new Handler(Looper.getMainLooper()).postDelayed(new Runnable() { @Override @@ -230,19 +234,19 @@ public void run() { */ public static void sendKeyBoardPress(String functionKey, String keyName) { if (keyName == null) { - Log.w(TAG, "❌ sendKeyBoardPress: keyName is null"); + Log.w(TAG, "sendKeyBoardPress: keyName is null"); return; } - - Log.e(TAG, "🔵 ========== KEY PRESS START =========="); - Log.e(TAG, "🔵 Thread: " + Thread.currentThread().getName()); - Log.e(TAG, "🔵 functionKey: '" + functionKey + "', keyName: '" + keyName + "'"); - Log.e(TAG, "🔵 Time: " + System.currentTimeMillis()); + + if (DEBUG_KEY_LOG) { + Log.d(TAG, "Key press: functionKey='" + functionKey + "' keyName='" + keyName + + "' thread=" + Thread.currentThread().getName()); + } new Thread(() -> { try { if (usbDeviceManager == null || !usbDeviceManager.isConnected()) { - Log.e(TAG, "❌ sendKeyBoardPress USB device not connected"); + Log.e(TAG, "sendKeyBoardPress: USB device not connected"); return; } @@ -262,28 +266,25 @@ public static void sendKeyBoardPress(String functionKey, String keyName) { sendKBData = sendKBData + CH9329Function.makeChecksum(sendKBData); - Log.e(TAG, "🔵 Full command: " + sendKBData); + if (DEBUG_KEY_LOG) { + Log.d(TAG, "Key press frame: " + sendKBData); + } CH9329Function.checkSendLogData(sendKBData); byte[] sendKBDataBytes = CH9329Function.hexStringToByteArray(sendKBData); try { - long startTime = System.currentTimeMillis(); boolean result = usbDeviceManager.writeData(sendKBDataBytes); - long endTime = System.currentTimeMillis(); - if (result) { - Log.e(TAG, "✅ Key press sent successfully in " + (endTime - startTime) + "ms (no auto-release)"); - } else { - Log.e(TAG, "❌ Key press write returned false"); + if (!result) { + Log.e(TAG, "Key press write returned false"); } } catch (Exception e) { - Log.e(TAG, "❌ Error writing key press: " + e.getMessage(), e); + Log.e(TAG, "Error writing key press: " + e.getMessage(), e); } } catch (Exception e) { - Log.e(TAG, "❌ Exception in sendKeyBoardPress: " + e.getMessage(), e); + Log.e(TAG, "Exception in sendKeyBoardPress: " + e.getMessage(), e); } - Log.e(TAG, "🔵 ========== KEY PRESS END =========="); }).start(); } @@ -305,50 +306,35 @@ public static void sendKeyBoardPress(String functionKey, String keyName) { */ public static void sendKeyBoardPressAndRelease(String functionKey, String keyName) { if (keyName == null) { - Log.w(TAG, "❌ sendKeyBoardPressAndRelease: keyName is null"); + Log.w(TAG, "sendKeyBoardPressAndRelease: keyName is null"); return; } // Skip if key is already pressed (prevents duplicate from browser double-firing) if (!currentlyPressedKeys.add(keyName)) { - Log.w(TAG, "🟣 Key '" + keyName + "' already pressed, ignoring duplicate"); return; } - Log.e(TAG, "🟣 ========== KEY PRESS+RELEASE START =========="); - Log.e(TAG, "🟣 functionKey: '" + functionKey + "', keyName: '" + keyName + "'"); - keyboardExecutor.execute(() -> { try { - long startTime = System.currentTimeMillis(); - - // Step 1: Send press - Log.e(TAG, "🟣 Step 1: Sending PRESS..."); - boolean pressSent = sendKeyBoardPressSync(functionKey, keyName); - if (!pressSent) { - Log.e(TAG, "❌ Press failed, aborting release"); + if (!sendKeyBoardPressSync(functionKey, keyName)) { + Log.e(TAG, "sendKeyBoardPressAndRelease: press failed, aborting release"); currentlyPressedKeys.remove(keyName); return; } - // Step 2: Small delay to ensure target registers the press - Log.e(TAG, "🟣 Step 2: Waiting 50ms for target to register..."); + // Give the target time to register the press before releasing it. Thread.sleep(50); - // Step 3: Send release - Log.e(TAG, "🟣 Step 3: Sending RELEASE..."); sendKeyBoardReleaseSync(); - currentlyPressedKeys.remove(keyName); - - long endTime = System.currentTimeMillis(); - Log.e(TAG, "✅ Key press+release completed in " + (endTime - startTime) + "ms"); - + } catch (InterruptedException e) { + Thread.currentThread().interrupt(); + currentlyPressedKeys.remove(keyName); } catch (Exception e) { - Log.e(TAG, "❌ Exception in sendKeyBoardPressAndRelease: " + e.getMessage(), e); + Log.e(TAG, "sendKeyBoardPressAndRelease failed: " + e.getMessage(), e); currentlyPressedKeys.remove(keyName); } - Log.e(TAG, "🟣 ========== KEY PRESS+RELEASE END =========="); }); } @@ -358,29 +344,22 @@ public static void sendKeyBoardPressAndRelease(String functionKey, String keyNam */ public static void sendKeyBoardPressQueued(String functionKey, String keyName) { if (keyName == null) { - Log.w(TAG, "❌ sendKeyBoardPressQueued: keyName is null"); + Log.w(TAG, "sendKeyBoardPressQueued: keyName is null"); return; } // Skip if key is already pressed (prevents duplicate from browser double-firing) if (!currentlyPressedKeys.add(keyName)) { - Log.w(TAG, "🟣 Key '" + keyName + "' already pressed, ignoring duplicate"); return; } - Log.e(TAG, "🔵 ========== QUEUED KEY PRESS START =========="); - Log.e(TAG, "🔵 functionKey: '" + functionKey + "', keyName: '" + keyName + "'"); - keyboardExecutor.execute(() -> { try { - Log.e(TAG, "🔵 Sending PRESS..."); - boolean result = sendKeyBoardPressSync(functionKey, keyName); - Log.e(TAG, "🔵 Press result: " + (result ? "SUCCESS" : "FAILED")); + sendKeyBoardPressSync(functionKey, keyName); } catch (Exception e) { - Log.e(TAG, "❌ Exception in sendKeyBoardPressQueued: " + e.getMessage(), e); + Log.e(TAG, "sendKeyBoardPressQueued failed: " + e.getMessage(), e); currentlyPressedKeys.remove(keyName); } - Log.e(TAG, "🔵 ========== QUEUED KEY PRESS END =========="); }); } @@ -390,18 +369,13 @@ public static void sendKeyBoardPressQueued(String functionKey, String keyName) { * This ensures release always happens AFTER all queued press operations. */ public static void sendKeyBoardReleaseQueued() { - Log.e(TAG, "🔴 ========== QUEUED KEY RELEASE START =========="); - keyboardExecutor.execute(() -> { try { - Log.e(TAG, "🔴 Sending RELEASE..."); sendKeyBoardReleaseSync(); currentlyPressedKeys.clear(); - Log.e(TAG, "✅ Release sent, cleared all pressed keys"); } catch (Exception e) { - Log.e(TAG, "❌ Exception in sendKeyBoardReleaseQueued: " + e.getMessage(), e); + Log.e(TAG, "sendKeyBoardReleaseQueued failed: " + e.getMessage(), e); } - Log.e(TAG, "🔴 ========== QUEUED KEY RELEASE END =========="); }); } @@ -431,17 +405,19 @@ private static boolean sendKeyBoardPressSync(String functionKey, String keyName) sendKBData = sendKBData + CH9329Function.makeChecksum(sendKBData); - Log.e(TAG, "🟣 Sync press command: " + sendKBData); + if (DEBUG_KEY_LOG) { + Log.d(TAG, "Sync press command: " + sendKBData); + } byte[] sendKBDataBytes = CH9329Function.hexStringToByteArray(sendKBData); long writeStart = System.currentTimeMillis(); boolean result = usbDeviceManager.writeData(sendKBDataBytes); long writeEnd = System.currentTimeMillis(); - if (result) { - Log.e(TAG, "✅ Sync press sent in " + (writeEnd - writeStart) + "ms"); - } else { - Log.e(TAG, "❌ Sync press write returned false"); + if (!result) { + Log.e(TAG, "Sync press write returned false"); + } else if (DEBUG_KEY_LOG) { + Log.d(TAG, "Sync press sent in " + (writeEnd - writeStart) + "ms"); } return result; @@ -462,7 +438,9 @@ private static void sendKeyBoardReleaseSync() { return; } - Log.e(TAG, "🟣 Sync release command: " + releaseKBData); + if (DEBUG_KEY_LOG) { + Log.d(TAG, "Sync release command: " + releaseKBData); + } byte[] releaseKBDataBytes = CH9329Function.hexStringToByteArray(releaseKBData); @@ -471,10 +449,10 @@ private static void sendKeyBoardReleaseSync() { long writeStart = System.currentTimeMillis(); boolean result = usbDeviceManager.writeData(releaseKBDataBytes); long writeEnd = System.currentTimeMillis(); - if (result) { - Log.e(TAG, "✅ Sync release sent in " + (writeEnd - writeStart) + "ms"); - } else { - Log.e(TAG, "❌ Sync release write returned false"); + if (!result) { + Log.e(TAG, "Sync release write returned false"); + } else if (DEBUG_KEY_LOG) { + Log.d(TAG, "Sync release sent in " + (writeEnd - writeStart) + "ms"); } } else { Log.e(TAG, "❌ sendKeyBoardReleaseSync: USB device not connected"); @@ -488,24 +466,23 @@ private static void sendKeyBoardReleaseSync() { * Send keyboard release event (key up) - releases all keys */ public static void sendKeyBoardRelease() { - Log.e(TAG, "🔴 ========== KEY RELEASE START =========="); - Log.e(TAG, "🔴 Thread: " + Thread.currentThread().getName()); - Log.e(TAG, "🔴 Time: " + System.currentTimeMillis()); + if (DEBUG_KEY_LOG) { + Log.d(TAG, "Key release start, thread=" + Thread.currentThread().getName()); + } new Thread(() -> { // Small delay to ensure press was sent before release // This prevents release from overtaking press in the serial queue try { - Log.e(TAG, "🔴 Waiting 20ms before release..."); Thread.sleep(20); - Log.e(TAG, "🔴 Now sending release command"); } catch (InterruptedException e) { Thread.currentThread().interrupt(); - Log.e(TAG, "🔴 Release delay interrupted: " + e.getMessage()); + if (DEBUG_KEY_LOG) { + Log.d(TAG, "Release delay interrupted: " + e.getMessage()); + } } EmptyKeyboard(); - Log.e(TAG, "🔴 ========== KEY RELEASE END =========="); }).start(); } @@ -519,7 +496,9 @@ public static void sendModifierPress(String modifierKey) { return; } - Log.d(TAG, "Modifier press: " + modifierKey); + if (DEBUG_KEY_LOG) { + Log.d(TAG, "Modifier press: " + modifierKey); + } new Thread(() -> { try { @@ -550,12 +529,16 @@ public static void sendModifierPress(String modifierKey) { CH9329MSKBMap.DataNull().get("DataNull"); sendKBData = sendKBData + CH9329Function.makeChecksum(sendKBData); - Log.d(TAG, "Modifier press command: " + sendKBData); + if (DEBUG_KEY_LOG) { + Log.d(TAG, "Modifier press command: " + sendKBData); + } CH9329Function.checkSendLogData(sendKBData); byte[] sendKBDataBytes = CH9329Function.hexStringToByteArray(sendKBData); boolean result = usbDeviceManager.writeData(sendKBDataBytes); - Log.d(TAG, "Modifier press sent: " + modifierKey + " result=" + result); + if (!result) { + Log.e(TAG, "Modifier press write returned false"); + } } catch (Exception e) { Log.e(TAG, "Error sending modifier press: " + e.getMessage(), e); } @@ -566,7 +549,6 @@ public static void sendModifierPress(String modifierKey) { * Send modifier key release — sends release command to clear all modifiers. */ public static void sendModifierRelease() { - Log.d(TAG, "Modifier release"); sendKeyBoardRelease(); } @@ -576,7 +558,7 @@ public static void sendKeyBoardShortCut(String modifier, String key) { public void run() { try { if (usbDeviceManager == null || !usbDeviceManager.isConnected()) { - Log.d(TAG, "sendKeyBoardShortCut USB device not connected"); + Log.e(TAG, "sendKeyBoardShortCut USB device not connected"); return; } @@ -626,7 +608,7 @@ public static void sendKeyBoardFunctionCtrlAltDel() { public void run() { try { if (usbDeviceManager == null || !usbDeviceManager.isConnected()){ - Log.d(TAG, "sendKeyBoardFunction USB device not connected"); + Log.e(TAG, "sendKeyBoardFunction USB device not connected"); return; } @@ -675,7 +657,7 @@ public static void sendKeyBoardFunction(String FunctionKeyCtrlPress, String Func public void run() { try { if (usbDeviceManager == null || !usbDeviceManager.isConnected()){ - Log.d(TAG, "sendKeyBoardFunction USB device not connected"); + Log.e(TAG, "sendKeyBoardFunction USB device not connected"); return; } @@ -711,9 +693,6 @@ public void run() { CH9329MSKBMap.DataNull().get("DataNull") + CH9329MSKBMap.DataNull().get("DataNull") + CH9329MSKBMap.DataNull().get("DataNull"); - Log.d(TAG, "FunctionKeyPress: " + CH9329MSKBMap.KBShortCutKey().get(combinationFunctionKey)); - Log.e(TAG, "successful send keyboard data: " + keyName); - Log.e(TAG, "successful send keyboard data currentKeyCodeMap: " + currentKeyCodeMap); sendKBData = sendKBData + CH9329Function.makeChecksum(sendKBData); CH9329Function.checkSendLogData(sendKBData); @@ -723,7 +702,6 @@ public void run() { try { boolean result = usbDeviceManager.writeData(sendKBDataBytes); if (result) { - Log.e(TAG, "successful send keyboard data "); EmptyKeyboard(); } else { Log.e(TAG, "Failed to write keyboard function data"); @@ -733,7 +711,7 @@ public void run() { } } catch (Exception e) { - e.printStackTrace(); + Log.e(TAG, "sendKeyBoardFunction failed: " + e.getMessage(), e); } } }).start(); @@ -752,8 +730,8 @@ public static void sendKeyBoardFunctionPress(String FunctionKeyCtrlPress, String @Override public void run() { try { - if (usbDeviceManager == null || !usbDeviceManager.isConnected()){ - Log.d(TAG, "sendKeyBoardFunctionPress USB device not connected"); + if (usbDeviceManager == null || !usbDeviceManager.isConnected()) { + Log.e(TAG, "sendKeyBoardFunctionPress USB device not connected"); return; } @@ -790,7 +768,9 @@ public void run() { CH9329MSKBMap.DataNull().get("DataNull") + CH9329MSKBMap.DataNull().get("DataNull"); - Log.d(TAG, "=== KEY PRESS === FunctionKey: " + combinationFunctionKey + ", keyName: " + keyName); + if (DEBUG_KEY_LOG) { + Log.d(TAG, "=== KEY PRESS === FunctionKey: " + combinationFunctionKey + ", keyName: " + keyName); + } sendKBData = sendKBData + CH9329Function.makeChecksum(sendKBData); @@ -800,9 +780,7 @@ public void run() { try { boolean result = usbDeviceManager.writeData(sendKBDataBytes); - if (result) { - Log.d(TAG, "Key press sent successfully (no auto-release)"); - } else { + if (!result) { Log.e(TAG, "Failed to write keyboard function press data"); } } catch (Exception e) { @@ -822,16 +800,10 @@ public static void sendKeyboardRequest(String function_key, String keyName) { public void run() { try { if (usbDeviceManager == null || !usbDeviceManager.isConnected()) { - Log.d(TAG, "sendKeyboardRequest USB device not connected"); + Log.e(TAG, "sendKeyboardRequest USB device not connected"); return; } - Log.d(TAG, "keyName: " + keyName); - Log.d(TAG, "currentKeyCodeMap class: " + currentKeyCodeMap.getClass().getName()); - Log.d(TAG, "currentKeyCodeMap size: " + currentKeyCodeMap.size()); - String lookedUpCode = currentKeyCodeMap.get(keyName); - Log.d(TAG, "lookedUpCode for '" + keyName + "': " + lookedUpCode); - String sendKBData = ""; sendKBData = CH9329MSKBMap.getKeyCodeMap().get("prefix1") + CH9329MSKBMap.getKeyCodeMap().get("prefix2") + @@ -855,9 +827,7 @@ public void run() { try { boolean result = usbDeviceManager.writeData(sendKBDataBytes); - if (result) { - Log.d(TAG, "Key press sent successfully"); - } else { + if (!result) { Log.e(TAG, "Key press write returned false"); } @@ -875,7 +845,7 @@ public void run() { } } catch (Exception e) { - e.printStackTrace(); + Log.e(TAG, "sendKeyboardRequest failed: " + e.getMessage(), e); } } }).start(); @@ -886,13 +856,11 @@ public static void sendKeyboardMultiple(String keyName) { @Override public void run() { try { - if (usbDeviceManager == null || !usbDeviceManager.isConnected()){ - Log.d(TAG, "sendKeyboardMultiple USB device not connected"); + if (usbDeviceManager == null || !usbDeviceManager.isConnected()) { + Log.e(TAG, "sendKeyboardMultiple USB device not connected"); return; } - Log.d(TAG, "keyName: " + keyName); - String sendKBData = ""; sendKBData = CH9329MSKBMap.getKeyCodeMap().get("prefix1") + CH9329MSKBMap.getKeyCodeMap().get("prefix2") + @@ -938,40 +906,28 @@ public KeyBoardManager(Context context) { //release all keyboard button public static void EmptyKeyboard() { - Log.e(TAG, "🔴 EmptyKeyboard called - checking conditions..."); - - // Check using usbDeviceManager first (consistent with press) + // Called on every key release, so only real failures are logged. if (usbDeviceManager != null && usbDeviceManager.isConnected()) { - Log.e(TAG, "🔴 Using usbDeviceManager.writeData() for release"); - String releaseKBData = CH9329MSKBMap.getKeyCodeMap().get("release"); if (releaseKBData == null) { - Log.e(TAG, "❌ EmptyKeyboard: releaseKBData is null"); + Log.e(TAG, "EmptyKeyboard: release command missing"); return; } - Log.e(TAG, "🔴 EmptyKeyboard: Release command: " + releaseKBData); CH9329Function.ReleaseSendLogData(releaseKBData); byte[] releaseKBDataBytes = CH9329Function.hexStringToByteArray(releaseKBData); if (releaseKBDataBytes == null || releaseKBDataBytes.length == 0) { - Log.e(TAG, "❌ EmptyKeyboard: releaseKBDataBytes is null or empty"); + Log.e(TAG, "EmptyKeyboard: release bytes empty"); return; } - Log.e(TAG, "🔴 EmptyKeyboard: Writing " + releaseKBDataBytes.length + " bytes: " + Arrays.toString(releaseKBDataBytes)); - try { - long startTime = System.currentTimeMillis(); - boolean result = usbDeviceManager.writeData(releaseKBDataBytes); - long endTime = System.currentTimeMillis(); - if (result) { - Log.e(TAG, "✅ EmptyKeyboard: Key release sent successfully via usbDeviceManager in " + (endTime - startTime) + "ms"); - } else { - Log.e(TAG, "❌ EmptyKeyboard: usbDeviceManager.writeData() returned false"); + if (!usbDeviceManager.writeData(releaseKBDataBytes)) { + Log.e(TAG, "EmptyKeyboard: writeData returned false"); } } catch (Exception e) { - Log.e(TAG, "❌ EmptyKeyboard: Exception while writing via usbDeviceManager: " + e.getMessage(), e); + Log.e(TAG, "EmptyKeyboard: " + e.getMessage(), e); } return; } @@ -979,6 +935,6 @@ public static void EmptyKeyboard() { // No fallback to direct port access — UsbDeviceManager.port is a static reference // that may not reflect the actual connection state, and port.write() uses // testConnection() which fails on some CH340 chips. - Log.e(TAG, "❌ EmptyKeyboard: usbDeviceManager is null or not connected, cannot send key release"); + Log.e(TAG, "EmptyKeyboard: device not connected, cannot send key release"); } } diff --git a/app/src/main/java/com/openterface/AOS/target/MouseManager.java b/app/src/main/java/com/openterface/AOS/target/MouseManager.java index 4d06991..80a7fbf 100644 --- a/app/src/main/java/com/openterface/AOS/target/MouseManager.java +++ b/app/src/main/java/com/openterface/AOS/target/MouseManager.java @@ -54,6 +54,72 @@ public static float getMouseSpeedMultiplier() { return mouseSpeedMultiplier; } + // Buttons held by a gesture in flight (tap, double-tap drag, BT mouse drag, VNC or + // WebRTC client) and buttons held by a locked keycap. The two masks are kept apart + // on purpose: a gesture release must not drop a locked keycap, and a lock has to + // survive the finger being lifted. + // + // Every move frame carries the union as the CH9329 button byte: sending 00 in the + // middle of a drag releases the button in the host, so the drag dies after the + // first pixel of movement. + private static volatile int gestureButtons = 0; + private static volatile int lockedButtons = 0; + + /** + * Set the gesture buttons. Pass null or "SecNullData" to release them; locked + * keycaps stay pressed. + */ + public static void setHeldButton(String button) { + gestureButtons = buttonMask(button); + } + + /** Add a button that stays pressed until it is unlocked (keycap long press). */ + public static void lockButton(String button) { + lockedButtons |= buttonMask(button); + } + + /** Drop one locked button (the keycap was tapped again to unlock). */ + public static void unlockButton(String button) { + int mask = buttonMask(button); + lockedButtons &= ~mask; + gestureButtons &= ~mask; + } + + /** Drop every gesture and locked button: disconnect, rotation, activity stop. */ + public static void unlockAll() { + gestureButtons = 0; + lockedButtons = 0; + } + + /** Button byte every move frame has to carry. */ + public static int getHeldMask() { + return gestureButtons | lockedButtons; + } + + public static int getLockedMask() { + return lockedButtons; + } + + /** CH9329 button byte for a "SecXxxData" name; 0 for null or unknown names. */ + static int buttonMask(String button) { + if (button == null) return 0; + String hex = CH9329MSKBMap.MSAbsData().get(button); + if (hex == null) return 0; + try { + return Integer.parseInt(hex, 16); + } catch (NumberFormatException e) { + return 0; + } + } + + private static String heldHex() { + return String.format("%02X", gestureButtons | lockedButtons); + } + + private static String lockedHex() { + return String.format("%02X", lockedButtons); + } + // Single worker thread for all mouse events — avoids thread-per-event overhead private static HandlerThread sWorkerThread; private static MouseHandler sHandler; @@ -61,7 +127,6 @@ public static float getMouseSpeedMultiplier() { // Message types private static final int MSG_ABS_MOVE = 1; private static final int MSG_ABS_CLICK = 2; - private static final int MSG_ABS_DRAG = 3; private static final int MSG_ABS_DBL_CLICK = 4; private static final int MSG_REL_MOVE = 5; private static final int MSG_TWO_PRESS = 6; @@ -69,6 +134,11 @@ public static float getMouseSpeedMultiplier() { private static final int MSG_TWO_PAN_SLIDE = 8; private static final int MSG_REL_DBL_CLICK = 9; private static final int MSG_ABS_RELEASE = 10; + // Bluetooth mouse message types - receive pre-converted HID coords, no secondary scaling + private static final int MSG_BT_MOUSE_HID_MOVE = 11; + private static final int MSG_BT_MOUSE_HID_CLICK = 12; + private static final int MSG_ABS_RELEASE_XY = 14; + private static final int MSG_BT_MOUSE_HID_RELEASE = 15; public static void setUsbDeviceManager(UsbDeviceManager deviceManager) { usbDeviceManager = deviceManager; @@ -126,14 +196,57 @@ public static void sendHexAbsButtonClickData(String MouseClick, float x, float y sHandler.sendMessage(msg); } - public static void sendHexAbsDragData(float x, float y) { + /** + * Absolute move that also releases every mouse button. Use this at the end of a + * click, a drag or any gesture: a plain move would keep the held mask alive and + * leave the button pressed in the host. + */ + public static void sendHexAbsReleaseData(float x, float y) { if (sHandler == null) init(); - Message msg = sHandler.obtainMessage(MSG_ABS_DRAG); + Message msg = sHandler.obtainMessage(MSG_ABS_RELEASE_XY); msg.arg1 = (int) (x * 1000); msg.arg2 = (int) (y * 1000); sHandler.sendMessage(msg); } + /** + * Send BT mouse absolute move with pre-converted HID coordinates [0, 4095]. + * Bypasses Handler's view→HID scaling - sends HID coords directly. + * This avoids double-scaling when BT mouse path already converted to HID range. + */ + public static void sendBtMouseHidAbsData(float hidX, float hidY) { + if (sHandler == null) init(); + Message msg = sHandler.obtainMessage(MSG_BT_MOUSE_HID_MOVE); + msg.arg1 = (int) hidX; + msg.arg2 = (int) hidY; + sHandler.sendMessage(msg); + } + + /** + * Send BT mouse button click with pre-converted HID coordinates [0, 4095]. + * Bypasses Handler's view→HID scaling - sends HID coords directly. + */ + public static void sendBtMouseHidAbsButtonClickData(String mouseClick, float hidX, float hidY) { + if (sHandler == null) init(); + Message msg = sHandler.obtainMessage(MSG_BT_MOUSE_HID_CLICK); + msg.arg1 = (int) hidX; + msg.arg2 = (int) hidY; + msg.obj = mouseClick; + sHandler.sendMessage(msg); + } + + /** + * BT mouse release with pre-converted HID coordinates [0, 4095]. Clears the held + * button so the host sees a real button-up at the current cursor position. + */ + public static void sendBtMouseHidAbsReleaseData(float hidX, float hidY) { + if (sHandler == null) init(); + Message msg = sHandler.obtainMessage(MSG_BT_MOUSE_HID_RELEASE); + msg.arg1 = (int) hidX; + msg.arg2 = (int) hidY; + sHandler.sendMessage(msg); + } + public static void handleDoubleClickAbs(float x, float y) { if (sHandler == null) init(); Message msg = sHandler.obtainMessage(MSG_ABS_DBL_CLICK); @@ -200,28 +313,39 @@ private static class MouseHandler extends Handler { super(looper); } + /** + * Convert view coordinate to HID absolute coordinate. + * Uses 4095 (12-bit max) instead of 4096 to prevent overflow/wrapping at right/bottom edges. + * Clamps result to [0, 4095] range. + */ + private int viewToHid(float viewCoord, float screenSize) { + if (screenSize <= 0) return 0; + int hid = (int) ((viewCoord / screenSize) * 4095f); + return Math.max(0, Math.min(4095, hid)); + } + @Override public void handleMessage(Message msg) { switch (msg.what) { case MSG_ABS_MOVE: doSendHexAbsData( - (int) ((msg.arg1 / 1000f) * 4096 / screenWidth), - (int) ((msg.arg2 / 1000f) * 4096 / screenHeight)); + viewToHid(msg.arg1 / 1000f, screenWidth), + viewToHid(msg.arg2 / 1000f, screenHeight)); break; case MSG_ABS_CLICK: doSendHexAbsButtonClickData((String) msg.obj, - (int) ((msg.arg1 / 1000f) * 4096 / screenWidth), - (int) ((msg.arg2 / 1000f) * 4096 / screenHeight)); + viewToHid(msg.arg1 / 1000f, screenWidth), + viewToHid(msg.arg2 / 1000f, screenHeight)); break; - case MSG_ABS_DRAG: - doSendHexAbsData( - (int) ((msg.arg1 / 1000f) * 4096 / screenWidth), - (int) ((msg.arg2 / 1000f) * 4096 / screenHeight)); + case MSG_ABS_RELEASE_XY: + doSendHexAbsReleaseData( + viewToHid(msg.arg1 / 1000f, screenWidth), + viewToHid(msg.arg2 / 1000f, screenHeight)); break; case MSG_ABS_DBL_CLICK: doHandleDoubleClickAbs( - (int) ((msg.arg1 / 1000f) * 4096 / screenWidth), - (int) ((msg.arg2 / 1000f) * 4096 / screenHeight)); + viewToHid(msg.arg1 / 1000f, screenWidth), + viewToHid(msg.arg2 / 1000f, screenHeight)); break; case MSG_TWO_PRESS: doHandleTwoPress(); @@ -243,6 +367,23 @@ public void handleMessage(Message msg) { AbsReleaseData rd = (AbsReleaseData) msg.obj; doReleaseMSAbsData(rd.x0, rd.x1, rd.y0, rd.y1); break; + // BT mouse HID events - receive pre-converted HID coords [0,4095], send directly + // No scaling - normalizeBtMouseToHid in CustomTouchListener already did the conversion + case MSG_BT_MOUSE_HID_MOVE: + if (CH9329Function.DEBUG_SEND_LOG) { + Log.d(TAG, "BT_HID_MOVE: received arg1=" + msg.arg1 + " arg2=" + msg.arg2); + } + doSendHexAbsData(msg.arg1, msg.arg2); + break; + case MSG_BT_MOUSE_HID_CLICK: + if (CH9329Function.DEBUG_SEND_LOG) { + Log.d(TAG, "BT_HID_CLICK: received arg1=" + msg.arg1 + " arg2=" + msg.arg2 + " click=" + msg.obj); + } + doSendHexAbsButtonClickData((String) msg.obj, msg.arg1, msg.arg2); + break; + case MSG_BT_MOUSE_HID_RELEASE: + doSendHexAbsReleaseData(msg.arg1, msg.arg2); + break; } } } @@ -265,6 +406,10 @@ private static class AbsReleaseData { // ---------- Core send logic (moved from old per-thread runnables) ---------- + /** + * Absolute move that keeps whatever button is currently held. Callers that really + * want a release must use doSendHexAbsReleaseData(). + */ private static void doSendHexAbsData(int x1, int y1) { byte[] xBytes = CH9329Function.intToByteArray(x1); byte[] yBytes = CH9329Function.intToByteArray(y1); @@ -275,7 +420,7 @@ private static void doSendHexAbsData(int x1, int y1) { CH9329MSKBMap.CmdData().get("CmdMS_ABS") + CH9329MSKBMap.DataLen().get("DataLenAbsMS") + CH9329MSKBMap.MSAbsData().get("FirstData") + - CH9329MSKBMap.MSAbsData().get("SecNullData") + + heldHex() + String.format("%02X", xBytes[0]) + String.format("%02X", xBytes[1]) + String.format("%02X", yBytes[0]) + @@ -287,6 +432,8 @@ private static void doSendHexAbsData(int x1, int y1) { } private static void doSendHexAbsButtonClickData(String mouseClick, int x1, int y1) { + // Remember the button so the following move frames keep it pressed. + gestureButtons = buttonMask(mouseClick); byte[] xBytes = CH9329Function.intToByteArray(x1); byte[] yBytes = CH9329Function.intToByteArray(y1); @@ -296,7 +443,7 @@ private static void doSendHexAbsButtonClickData(String mouseClick, int x1, int y CH9329MSKBMap.CmdData().get("CmdMS_ABS") + CH9329MSKBMap.DataLen().get("DataLenAbsMS") + CH9329MSKBMap.MSAbsData().get("FirstData") + - CH9329MSKBMap.MSAbsData().get(mouseClick) + + heldHex() + String.format("%02X", xBytes[0]) + String.format("%02X", xBytes[1]) + String.format("%02X", yBytes[0]) + @@ -307,6 +454,15 @@ private static void doSendHexAbsButtonClickData(String mouseClick, int x1, int y sendToDevice(sendMSData, "sendHexAbsButtonClickData"); } + /** + * Absolute move that releases the gesture buttons (end of click / drag). A locked + * keycap keeps its button pressed. + */ + private static void doSendHexAbsReleaseData(int x1, int y1) { + gestureButtons = 0; + doSendHexAbsData(x1, y1); + } + private static void doHandleDoubleClickAbs(int x1, int y1) { byte[] xBytes = CH9329Function.intToByteArray(x1); byte[] yBytes = CH9329Function.intToByteArray(y1); @@ -348,7 +504,7 @@ private static void doHandleTwoPress() { CH9329MSKBMap.CmdData().get("CmdMS_REL") + CH9329MSKBMap.DataLen().get("DataLenRelMS") + CH9329MSKBMap.MSRelData().get("FirstData") + - CH9329MSKBMap.MSRelData().get("SecRightData") + + String.format("%02X", buttonMask("SecRightData") | gestureButtons | lockedButtons) + CH9329MSKBMap.DataNull().get("DataNull") + CH9329MSKBMap.DataNull().get("DataNull") + CH9329MSKBMap.DataNull().get("DataNull"); @@ -374,7 +530,7 @@ private static void doHandleDoubleFingerPan(float startMoveMSY, float lastMoveMS CH9329MSKBMap.CmdData().get("CmdMS_REL") + CH9329MSKBMap.DataLen().get("DataLenRelMS") + CH9329MSKBMap.MSRelData().get("FirstData") + - CH9329MSKBMap.MSRelData().get("SecMiddleData") + + String.format("%02X", buttonMask("SecMiddleData") | gestureButtons | lockedButtons) + CH9329MSKBMap.DataNull().get("DataNull") + CH9329MSKBMap.DataNull().get("DataNull") + yByte; @@ -393,7 +549,7 @@ private static void doHandleTwoFingerPanSlideUpDown(float slideData) { CH9329MSKBMap.CmdData().get("CmdMS_REL") + CH9329MSKBMap.DataLen().get("DataLenRelMS") + CH9329MSKBMap.MSRelData().get("FirstData") + - CH9329MSKBMap.MSRelData().get("SecNullData") + + heldHex() + CH9329MSKBMap.DataNull().get("DataNull") + CH9329MSKBMap.DataNull().get("DataNull") + yByte; @@ -410,7 +566,7 @@ private static void doSendHexRelData(String mouseClick, float startMoveMSX, floa int yMovement = (int) ((startMoveMSY - lastMoveMSY) * mouseSpeedMultiplier); String xByte; - if (xMovement == 0 || lastMoveMSX == 0) { + if (xMovement == 0) { xByte = "00"; } else if (xMovement > 0) { xByte = String.format("%02X", Math.min(xMovement, 0x7F)); @@ -419,7 +575,7 @@ private static void doSendHexRelData(String mouseClick, float startMoveMSX, floa } String yByte; - if (yMovement == 0 || lastMoveMSY == 0) { + if (yMovement == 0) { yByte = "00"; } else if (yMovement > 0) { yByte = String.format("%02X", Math.min(yMovement, 0x7F)); @@ -427,13 +583,20 @@ private static void doSendHexRelData(String mouseClick, float startMoveMSX, floa yByte = String.format("%02X", 0x100 + yMovement); } + // A move frame must never drop a held button: a locked keycap or an ongoing + // BT mouse drag would be released by the next movement packet. + int buttons = buttonMask(mouseClick); + if (buttons == 0) { + buttons = gestureButtons | lockedButtons; + } + String sendMSData = CH9329MSKBMap.getKeyCodeMap().get("prefix1") + CH9329MSKBMap.getKeyCodeMap().get("prefix2") + CH9329MSKBMap.getKeyCodeMap().get("address") + CH9329MSKBMap.CmdData().get("CmdMS_REL") + CH9329MSKBMap.DataLen().get("DataLenRelMS") + CH9329MSKBMap.MSRelData().get("FirstData") + - CH9329MSKBMap.MSAbsData().get(mouseClick) + + String.format("%02X", buttons) + xByte + yByte + CH9329MSKBMap.DataNull().get("DataNull"); @@ -444,13 +607,14 @@ private static void doSendHexRelData(String mouseClick, float startMoveMSX, floa private static void doHandleDoubleClickRel() { // This is the releaseMSRelData function — renamed to avoid confusion + gestureButtons = 0; String sendMSData = CH9329MSKBMap.getKeyCodeMap().get("prefix1") + CH9329MSKBMap.getKeyCodeMap().get("prefix2") + CH9329MSKBMap.getKeyCodeMap().get("address") + CH9329MSKBMap.CmdData().get("CmdMS_REL") + CH9329MSKBMap.DataLen().get("DataLenRelMS") + CH9329MSKBMap.MSRelData().get("FirstData") + - CH9329MSKBMap.MSRelData().get("SecNullData") + + lockedHex() + CH9329MSKBMap.DataNull().get("DataNull") + CH9329MSKBMap.DataNull().get("DataNull") + CH9329MSKBMap.DataNull().get("DataNull"); @@ -460,13 +624,14 @@ private static void doHandleDoubleClickRel() { } static void doReleaseMSAbsData(String xBytes0, String xBytes1, String yBytes0, String yBytes1) { + gestureButtons = 0; String sendReleaseMSData = CH9329MSKBMap.getKeyCodeMap().get("prefix1") + CH9329MSKBMap.getKeyCodeMap().get("prefix2") + CH9329MSKBMap.getKeyCodeMap().get("address") + CH9329MSKBMap.CmdData().get("CmdMS_ABS") + CH9329MSKBMap.DataLen().get("DataLenAbsMS") + CH9329MSKBMap.MSAbsData().get("FirstData") + - CH9329MSKBMap.MSAbsData().get("SecNullData") + + lockedHex() + xBytes0 + xBytes1 + yBytes0 + yBytes1 + CH9329MSKBMap.DataNull().get("DataNull"); @@ -475,13 +640,14 @@ static void doReleaseMSAbsData(String xBytes0, String xBytes1, String yBytes0, S } static void doReleaseMSRelData() { + gestureButtons = 0; String sendMSData = CH9329MSKBMap.getKeyCodeMap().get("prefix1") + CH9329MSKBMap.getKeyCodeMap().get("prefix2") + CH9329MSKBMap.getKeyCodeMap().get("address") + CH9329MSKBMap.CmdData().get("CmdMS_REL") + CH9329MSKBMap.DataLen().get("DataLenRelMS") + CH9329MSKBMap.MSRelData().get("FirstData") + - CH9329MSKBMap.MSRelData().get("SecNullData") + + lockedHex() + CH9329MSKBMap.DataNull().get("DataNull") + CH9329MSKBMap.DataNull().get("DataNull") + CH9329MSKBMap.DataNull().get("DataNull"); @@ -496,17 +662,29 @@ private static void sendToDevice(String hexData, String tag) { CH9329Function.checkSendLogData(hexData); byte[] bytes = CH9329Function.hexStringToByteArray(hexData); try { - if (usbDeviceManager != null && usbDeviceManager.isConnected()) { - boolean result = usbDeviceManager.writeData(bytes); - // Only log errors — success logging adds overhead in the hot path - if (!result) { - Log.e(TAG, "Failed to write " + tag); - } - } else { - Log.e(TAG, "USB device not connected for " + tag); + // Diagnostic logging + if (usbDeviceManager == null) { + Log.e(TAG, "USB device manager is NULL for " + tag); + return; + } + if (!usbDeviceManager.isConnected()) { + Log.e(TAG, "USB device not connected for " + tag + " - data: " + hexData); + return; + } + // 逐包 hex 日志(默认关闭,见 CH9329Function.DEBUG_SEND_LOG) + if (CH9329Function.DEBUG_SEND_LOG) { + Log.d(TAG, "Sending " + tag + ": " + hexData); + } + + boolean result = usbDeviceManager.writeData(bytes); + if (!result) { + Log.e(TAG, "Failed to write " + tag); + } else if (CH9329Function.DEBUG_SEND_LOG) { + Log.d(TAG, "Successfully wrote " + tag); } } catch (Exception e) { - Log.e(TAG, "Error writing to port: " + e.getMessage()); + // Catch all exceptions to prevent app crash on USB errors + Log.e(TAG, "Error writing to port: " + e.getMessage(), e); } } } diff --git a/app/src/main/java/com/openterface/AOS/target/MouseManagerCore.java b/app/src/main/java/com/openterface/AOS/target/MouseManagerCore.java index 531e757..2643725 100644 --- a/app/src/main/java/com/openterface/AOS/target/MouseManagerCore.java +++ b/app/src/main/java/com/openterface/AOS/target/MouseManagerCore.java @@ -50,6 +50,45 @@ public static float getMouseSpeedMultiplier() { return mouseSpeedMultiplier; } + // Buttons held by a gesture in flight and buttons held by a locked keycap. A + // gesture release must not drop a lock, and a lock has to survive the finger being + // lifted, so the two masks are kept apart. Move frames carry their union: the next + // movement packet would otherwise release the button in the host and a drag would + // stop after one pixel. + private static volatile int gestureButtons = KeymodJNI.MS_BTN_NONE; + private static volatile int lockedButtons = KeymodJNI.MS_BTN_NONE; + + /** Set the gesture buttons; 0 releases them, locked keycaps stay pressed. */ + public static void setHeldButtons(int buttons) { + gestureButtons = buttons; + } + + /** Add a button that stays pressed until it is unlocked (keycap long press). */ + public static void lockButtons(int buttons) { + lockedButtons |= buttons; + } + + /** Drop one locked button (the keycap was tapped again to unlock). */ + public static void unlockButtons(int buttons) { + lockedButtons &= ~buttons; + gestureButtons &= ~buttons; + } + + /** Drop every gesture and locked button: disconnect, rotation, activity stop. */ + public static void unlockAll() { + gestureButtons = KeymodJNI.MS_BTN_NONE; + lockedButtons = KeymodJNI.MS_BTN_NONE; + } + + /** Button mask every move frame has to carry. */ + public static int getHeldButtons() { + return gestureButtons | lockedButtons; + } + + public static int getLockedButtons() { + return lockedButtons; + } + // Single worker thread for all mouse events private static HandlerThread sWorkerThread; private static MouseHandler sHandler; @@ -57,8 +96,12 @@ public static float getMouseSpeedMultiplier() { // Message types private static final int MSG_ABS_MOVE = 1; private static final int MSG_ABS_CLICK = 2; - private static final int MSG_ABS_DRAG = 3; + private static final int MSG_ABS_RELEASE_XY = 4; private static final int MSG_REL_MOVE = 5; + // BT mouse HID events - receive pre-converted HID coords, no scaling + private static final int MSG_BT_MOUSE_HID_MOVE = 11; + private static final int MSG_BT_MOUSE_HID_CLICK = 12; + private static final int MSG_BT_MOUSE_HID_RELEASE = 15; static { // Load native library @@ -118,14 +161,54 @@ public static void sendHexAbsButtonClickData(String mouseClick, float x, float y sHandler.sendMessage(msg); } - public static void sendHexAbsDragData(float x, float y) { + /** + * Absolute move that releases the gesture buttons (end of click / drag). A locked + * keycap keeps its button pressed. + */ + public static void sendHexAbsReleaseData(float x, float y) { if (sHandler == null) init(); - Message msg = sHandler.obtainMessage(MSG_ABS_DRAG); + Message msg = sHandler.obtainMessage(MSG_ABS_RELEASE_XY); msg.arg1 = (int) (x * 1000); msg.arg2 = (int) (y * 1000); sHandler.sendMessage(msg); } + /** + * Send BT mouse absolute move with pre-converted HID coordinates [0, 4095]. + * Bypasses Handler's view→HID scaling - sends HID coords directly. + */ + public static void sendBtMouseHidAbsData(float hidX, float hidY) { + if (sHandler == null) init(); + Message msg = sHandler.obtainMessage(MSG_BT_MOUSE_HID_MOVE); + msg.arg1 = (int) hidX; + msg.arg2 = (int) hidY; + sHandler.sendMessage(msg); + } + + /** + * Send BT mouse button click with pre-converted HID coordinates [0, 4095]. + * Bypasses Handler's view→HID scaling - sends HID coords directly. + */ + public static void sendBtMouseHidAbsButtonClickData(String mouseClick, float hidX, float hidY) { + if (sHandler == null) init(); + Message msg = sHandler.obtainMessage(MSG_BT_MOUSE_HID_CLICK); + msg.arg1 = (int) hidX; + msg.arg2 = (int) hidY; + msg.obj = mouseClick; + sHandler.sendMessage(msg); + } + + /** + * BT mouse release with pre-converted HID coordinates [0, 4095]. + */ + public static void sendBtMouseHidAbsReleaseData(float hidX, float hidY) { + if (sHandler == null) init(); + Message msg = sHandler.obtainMessage(MSG_BT_MOUSE_HID_RELEASE); + msg.arg1 = (int) hidX; + msg.arg2 = (int) hidY; + sHandler.sendMessage(msg); + } + public static void sendHexRelData(String mouseClick, float sx, float sy, float lx, float ly) { if (sHandler == null) init(); RelData data = new RelData(mouseClick, sx, sy, lx, ly); @@ -140,28 +223,50 @@ private static class MouseHandler extends Handler { super(looper); } + /** + * Convert view coordinate to HID absolute coordinate. + * Uses 4095 (12-bit max) instead of 4096 to prevent overflow/wrapping at right/bottom edges. + * Clamps result to [0, 4095] range. + */ + private int viewToHid(float viewCoord, float screenSize) { + if (screenSize <= 0) return 0; + int hid = (int) ((viewCoord / screenSize) * 4095f); + return Math.max(0, Math.min(4095, hid)); + } + @Override public void handleMessage(Message msg) { switch (msg.what) { case MSG_ABS_MOVE: doSendHexAbsData( - (int) ((msg.arg1 / 1000f) * 4096 / screenWidth), - (int) ((msg.arg2 / 1000f) * 4096 / screenHeight)); + viewToHid(msg.arg1 / 1000f, screenWidth), + viewToHid(msg.arg2 / 1000f, screenHeight)); break; case MSG_ABS_CLICK: doSendHexAbsButtonClickData((String) msg.obj, - (int) ((msg.arg1 / 1000f) * 4096 / screenWidth), - (int) ((msg.arg2 / 1000f) * 4096 / screenHeight)); + viewToHid(msg.arg1 / 1000f, screenWidth), + viewToHid(msg.arg2 / 1000f, screenHeight)); break; - case MSG_ABS_DRAG: - doSendHexAbsData( - (int) ((msg.arg1 / 1000f) * 4096 / screenWidth), - (int) ((msg.arg2 / 1000f) * 4096 / screenHeight)); + case MSG_ABS_RELEASE_XY: + doSendHexAbsReleaseData( + viewToHid(msg.arg1 / 1000f, screenWidth), + viewToHid(msg.arg2 / 1000f, screenHeight)); break; case MSG_REL_MOVE: RelData d = (RelData) msg.obj; doSendHexRelData(d.click, d.sx, d.sy, d.lx, d.ly); break; + // BT mouse HID events - receive pre-converted HID coords [0,4095], send directly + // No scaling - normalizeBtMouseToHid in CustomTouchListener already did the conversion + case MSG_BT_MOUSE_HID_MOVE: + doSendHexAbsData(msg.arg1, msg.arg2); + break; + case MSG_BT_MOUSE_HID_CLICK: + doSendHexAbsButtonClickData((String) msg.obj, msg.arg1, msg.arg2); + break; + case MSG_BT_MOUSE_HID_RELEASE: + doSendHexAbsReleaseData(msg.arg1, msg.arg2); + break; } } } @@ -169,14 +274,22 @@ public void handleMessage(Message msg) { // ====== Core HID Implementation ====== private static void doSendHexAbsData(int x1, int y1) { + // Keep the buttons that are currently held (locked keycap / ongoing drag). byte[] packet = KeymodJNI.buildMouseAbsPacket( - KeymodJNI.MS_BTN_NONE, x1, y1, 0); + gestureButtons | lockedButtons, x1, y1, 0); sendToDevice(packet, "absMove"); } + private static void doSendHexAbsReleaseData(int x1, int y1) { + gestureButtons = KeymodJNI.MS_BTN_NONE; + doSendHexAbsData(x1, y1); + } + private static void doSendHexAbsButtonClickData(String mouseClick, int x1, int y1) { int buttons = parseButtonMask(mouseClick); - byte[] packet = KeymodJNI.buildMouseAbsPacket(buttons, x1, y1, 0); + gestureButtons = buttons; + byte[] packet = KeymodJNI.buildMouseAbsPacket( + buttons | lockedButtons, x1, y1, 0); sendToDevice(packet, "absClick"); } @@ -190,6 +303,10 @@ private static void doSendHexRelData(String mouseClick, float sx, float sy, floa int dy = Math.max(-128, Math.min(127, yMovement)); int buttons = parseButtonMask(mouseClick); + if (buttons == KeymodJNI.MS_BTN_NONE) { + // A move frame must not drop a held button (locked keycap / BT mouse drag). + buttons = gestureButtons | lockedButtons; + } byte[] packet = KeymodJNI.buildMouseRelPacket(buttons, dx, dy, 0); sendToDevice(packet, "relMove"); } @@ -200,6 +317,8 @@ private static int parseButtonMask(String mouseClick) { case "SecLeftData": return KeymodJNI.MS_BTN_LEFT; case "SecRightData": return KeymodJNI.MS_BTN_RIGHT; case "SecMiddleData": return KeymodJNI.MS_BTN_MIDDLE; + case "SecBackData": return KeymodJNI.MS_BTN_BACK; + case "SecForwardData": return KeymodJNI.MS_BTN_FORWARD; default: return KeymodJNI.MS_BTN_NONE; } } @@ -230,30 +349,41 @@ private static class RelData { public static void handleDoubleClickAbs(float x, float y) { sendHexAbsButtonClickData("SecLeftData", x, y); + // The press above latched the left button; send the matching release at the same + // position so the click does not leave the host button down. + if (sHandler == null) init(); + Message msg = sHandler.obtainMessage(MSG_ABS_RELEASE_XY); + msg.arg1 = (int) (x * 1000); + msg.arg2 = (int) (y * 1000); + sHandler.sendMessage(msg); } public static void handleTwoPress() { - // Right button press - byte[] packet = KeymodJNI.buildMouseRelPacket(KeymodJNI.MS_BTN_RIGHT, 0, 0, 0); + // Right button press - a locked keycap keeps its button held + byte[] packet = KeymodJNI.buildMouseRelPacket( + KeymodJNI.MS_BTN_RIGHT | gestureButtons | lockedButtons, 0, 0, 0); sendToDevice(packet, "twoPress"); } public static void handleDoubleFingerPan(float startY, float lastY) { int dy = (int) (startY - lastY); dy = Math.max(-1, Math.min(1, dy)); // Clamp to -1, 0, 1 - byte[] packet = KeymodJNI.buildMouseRelPacket(KeymodJNI.MS_BTN_MIDDLE, 0, dy, 0); + byte[] packet = KeymodJNI.buildMouseRelPacket( + KeymodJNI.MS_BTN_MIDDLE | gestureButtons | lockedButtons, 0, dy, 0); sendToDevice(packet, "pan"); } - public static void releaseMSAbsData() { - // Release by sending null button at current position - byte[] packet = KeymodJNI.buildMouseAbsPacket(KeymodJNI.MS_BTN_NONE, 0, 0, 0); + public static void releaseMSAbsData(float x, float y) { + // Release by sending null button at the given position + gestureButtons = KeymodJNI.MS_BTN_NONE; + byte[] packet = KeymodJNI.buildMouseAbsPacket(lockedButtons, (int) x, (int) y, 0); sendToDevice(packet, "releaseAbs"); } public static void releaseMSRelData() { - // Release by sending null button - byte[] packet = KeymodJNI.buildMouseRelPacket(KeymodJNI.MS_BTN_NONE, 0, 0, 0); + // Release by sending null button; a locked keycap keeps its button pressed + gestureButtons = KeymodJNI.MS_BTN_NONE; + byte[] packet = KeymodJNI.buildMouseRelPacket(lockedButtons, 0, 0, 0); sendToDevice(packet, "releaseRel"); } } diff --git a/app/src/main/java/com/openterface/AOS/target/MouseMode.java b/app/src/main/java/com/openterface/AOS/target/MouseMode.java new file mode 100644 index 0000000..a828fb3 --- /dev/null +++ b/app/src/main/java/com/openterface/AOS/target/MouseMode.java @@ -0,0 +1,12 @@ +package com.openterface.AOS.target; + +/** + * Mouse control mode. + *

+ * ABSOLUTE: Click to position cursor, long press to drag (default). + * RELATIVE: Slide to move cursor like a traditional mouse. + */ +public enum MouseMode { + ABSOLUTE, // Absolute mode: tap to position, long press to drag + RELATIVE // Relative mode: slide to move cursor +} diff --git a/app/src/main/java/com/openterface/AOS/target/MouseModeManager.java b/app/src/main/java/com/openterface/AOS/target/MouseModeManager.java new file mode 100644 index 0000000..a4db46b --- /dev/null +++ b/app/src/main/java/com/openterface/AOS/target/MouseModeManager.java @@ -0,0 +1,70 @@ +package com.openterface.AOS.target; + +import android.util.Log; + +/** + * Centralized mouse mode state management. + * Single source of truth for current mouse mode and sensitivity settings. + */ +public class MouseModeManager { + private static final String TAG = "OP-MOUSE-MGR"; + + // Core state + private static MouseMode currentMode = MouseMode.ABSOLUTE; + private static boolean trackpadSensitivity = false; // Sensitivity curve for RELATIVE mode + + // Private constructor - utility class + private MouseModeManager() {} + + /** + * Get current mouse mode. + */ + public static MouseMode getMode() { + return currentMode; + } + + /** + * Set mouse control mode. + */ + public static void setMode(MouseMode mode) { + if (mode == null) return; + if (currentMode != mode) { + currentMode = mode; + Log.d(TAG, "Mouse mode changed to: " + mode); + } + } + + /** + * Enable/disable trackpad-style sensitivity curve. + * When enabled in RELATIVE mode, sensitivity is scaled by 1/zoomScale + * for precise control when zoomed in. + */ + public static void setTrackpadSensitivity(boolean enabled) { + if (trackpadSensitivity != enabled) { + trackpadSensitivity = enabled; + Log.d(TAG, "Trackpad sensitivity curve: " + enabled); + } + } + + /** + * Check if trackpad-style sensitivity curve is currently active. + * Returns true only when mode is RELATIVE and trackpadSensitivity is enabled. + */ + public static boolean isTrackpadSensitivityEnabled() { + return trackpadSensitivity && currentMode == MouseMode.RELATIVE; + } + + /** + * Check if current mode is ABSOLUTE. + */ + public static boolean isAbsoluteMode() { + return currentMode == MouseMode.ABSOLUTE; + } + + /** + * Check if current mode is RELATIVE. + */ + public static boolean isRelativeMode() { + return currentMode == MouseMode.RELATIVE; + } +} diff --git a/app/src/main/java/com/openterface/AOS/utils/MouseModePreferences.java b/app/src/main/java/com/openterface/AOS/utils/MouseModePreferences.java new file mode 100644 index 0000000..9b4b681 --- /dev/null +++ b/app/src/main/java/com/openterface/AOS/utils/MouseModePreferences.java @@ -0,0 +1,45 @@ +package com.openterface.AOS.utils; + +import android.content.Context; +import android.content.SharedPreferences; +import com.openterface.AOS.target.MouseMode; + +/** + * Mouse mode preference persistence. + * Uses clean keys, no legacy migration. + */ +public class MouseModePreferences { + private static final String PREFS_NAME = "settings"; + private static final String KEY_MODE = "mouse_mode"; + private static final String KEY_TRACKPAD = "trackpad_sensitivity"; + + /** + * Save mouse mode and sensitivity settings. + */ + public static void save(Context context, MouseMode mode, boolean trackpadSensitivity) { + context.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE) + .edit() + .putString(KEY_MODE, mode.name().toLowerCase()) + .putBoolean(KEY_TRACKPAD, trackpadSensitivity) + .apply(); + } + + /** + * Load mouse mode from preferences. + * Returns ABSOLUTE as default if not found. + */ + public static MouseMode loadMode(Context context) { + String modeStr = context.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE) + .getString(KEY_MODE, "absolute"); + return "relative".equals(modeStr) ? MouseMode.RELATIVE : MouseMode.ABSOLUTE; + } + + /** + * Load trackpad sensitivity setting from preferences. + * Returns false as default if not found. + */ + public static boolean loadTrackpadSensitivity(Context context) { + return context.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE) + .getBoolean(KEY_TRACKPAD, false); + } +} diff --git a/app/src/main/java/com/openterface/AOS/view/BlackBorderOverlayView.java b/app/src/main/java/com/openterface/AOS/view/BlackBorderOverlayView.java new file mode 100644 index 0000000..d4cac61 --- /dev/null +++ b/app/src/main/java/com/openterface/AOS/view/BlackBorderOverlayView.java @@ -0,0 +1,56 @@ +package com.openterface.AOS.view; + +import android.content.Context; +import android.util.AttributeSet; +import android.view.View; + +import androidx.annotation.Nullable; + +/** + * Fully transparent overlay covering the whole video area. + * + * It used to draw a semi-transparent mask over the black border plus a + * "please operate inside the picture" hint. Both are gone: + * - the black border is already pure black, so a 50% black mask was invisible and + * carried no information; + * - the mask never influenced mouse/touch handling, the black border is excluded + * from HID mapping by coordinate clamping, not by this view; + * - the hint text was unwanted. + * + * What the view is still needed for: it is the only surface above the black bars, + * so it carries the transparent 1x1 PointerIcon and keeps the system mouse cursor + * from reappearing whenever it leaves the preview. + */ +public class BlackBorderOverlayView extends View { + + public BlackBorderOverlayView(Context context) { + this(context, null); + } + + public BlackBorderOverlayView(Context context, @Nullable AttributeSet attrs) { + this(context, attrs, 0); + } + + public BlackBorderOverlayView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) { + super(context, attrs, defStyleAttr); + + // Initially hidden + setVisibility(GONE); + setAlpha(0f); + } + + /** + * Show the overlay with animation. + */ + public void showOverlay() { + setVisibility(VISIBLE); + animate().alpha(1f).setDuration(200).start(); + } + + /** + * Hide the overlay with animation. + */ + public void hideOverlay() { + animate().alpha(0f).setDuration(200).withEndAction(() -> setVisibility(GONE)).start(); + } +} diff --git a/app/src/main/java/com/openterface/AOS/view/MouseControlStripView.java b/app/src/main/java/com/openterface/AOS/view/MouseControlStripView.java index 6f7b08d..3ff4b2c 100644 --- a/app/src/main/java/com/openterface/AOS/view/MouseControlStripView.java +++ b/app/src/main/java/com/openterface/AOS/view/MouseControlStripView.java @@ -68,6 +68,21 @@ public interface OnMouseClickListener { private OnMouseClickListener mouseClickListener; + /** + * Told when a keycap is locked or unlocked. A lock keeps the mouse button pressed + * in the host on its own, independently of the gestures that press and release + * buttons, so it has to be reported separately from onMouseClick/onMouseRelease. + */ + public interface OnLockChangeListener { + /** + * @param buttonMask BTN_LEFT / BTN_MIDDLE / BTN_RIGHT + * @param locked true when the long press locked it, false when a tap unlocked it + */ + void onLockChanged(int buttonMask, boolean locked); + } + + private OnLockChangeListener lockChangeListener; + public MouseControlStripView(Context context) { super(context); init(context); @@ -134,6 +149,7 @@ private void sendMouseButtonPress(int buttonMask) { int x = HidManager.getScreenWidth() / 2; int y = HidManager.getScreenHeight() / 2; String clickType = getClickTypeString(buttonMask); + HidManager.setHeldButton(clickType); HidManager.sendHexAbsButtonClickData(clickType, x, y); } } @@ -142,10 +158,17 @@ private void sendMouseRelease() { if (mouseClickListener != null) { mouseClickListener.onMouseRelease(); } else { + HidManager.setHeldButton(null); HidManager.releaseMSRelData(); } } + private void notifyLockChanged(int buttonMask, boolean locked) { + if (lockChangeListener != null) { + lockChangeListener.onLockChanged(buttonMask, locked); + } + } + private String getClickTypeString(int mask) { switch (mask) { case BTN_LEFT: return "SecLeftData"; @@ -159,6 +182,10 @@ public void setOnMouseClickListener(OnMouseClickListener listener) { this.mouseClickListener = listener; } + public void setOnLockChangeListener(OnLockChangeListener listener) { + this.lockChangeListener = listener; + } + public void setOpacity(int opacity) { currentOpacity = Math.max(0, Math.min(100, opacity)); applyOpacity(); @@ -288,6 +315,7 @@ public boolean onTouchEvent(MotionEvent event) { longPressFired = true; isLocked = true; updateLockedState(buttonMask, true); + notifyLockChanged(buttonMask, true); invalidate(); performHapticFeedback(HapticFeedbackConstants.LONG_PRESS); }; @@ -307,6 +335,7 @@ public boolean onTouchEvent(MotionEvent event) { isLocked = false; isPressed = false; updateLockedState(buttonMask, false); + notifyLockChanged(buttonMask, false); invalidate(); setButtonPressed(buttonMask, false); sendMouseRelease(); diff --git a/app/src/main/java/com/openterface/AOS/webrtc/AndroidHidInputSender.java b/app/src/main/java/com/openterface/AOS/webrtc/AndroidHidInputSender.java index 3a5ad11..ae5c208 100644 --- a/app/src/main/java/com/openterface/AOS/webrtc/AndroidHidInputSender.java +++ b/app/src/main/java/com/openterface/AOS/webrtc/AndroidHidInputSender.java @@ -28,6 +28,17 @@ public void sendAbsMove(int x, int y) { HidManager.sendHexAbsData(pixelX, pixelY); } + @Override + public void sendAbsRelease(int x, int y) { + // x,y are already normalized 0-4095 from WebRTC client + // Convert back to pixel coordinates for HidManager (which re-normalizes to 0-4095) + int width = HidManager.getScreenWidth(); + int height = HidManager.getScreenHeight(); + int pixelX = (int) (x / 4095.0f * width); + int pixelY = (int) (y / 4095.0f * height); + HidManager.sendHexAbsReleaseData(pixelX, pixelY); + } + @Override public void sendAbsButtonClick(String clickType, int x, int y) { // x,y are already normalized 0-4095 from WebRTC client diff --git a/app/src/main/java/com/openterface/AOS/webrtc/HidInputSender.java b/app/src/main/java/com/openterface/AOS/webrtc/HidInputSender.java index 781beca..35e38b6 100644 --- a/app/src/main/java/com/openterface/AOS/webrtc/HidInputSender.java +++ b/app/src/main/java/com/openterface/AOS/webrtc/HidInputSender.java @@ -17,6 +17,12 @@ public interface HidInputSender { */ void sendAbsMove(int x, int y); + /** + * Send absolute mouse movement that also releases every pressed button. + * A plain move keeps the button pressed in the host. + */ + void sendAbsRelease(int x, int y); + /** * Send absolute mouse button click. * @param clickType "SecLeftData", "SecMiddleData", "SecRightData", or "SecNullData" diff --git a/app/src/main/java/com/openterface/AOS/webrtc/WebRtcInputRouter.java b/app/src/main/java/com/openterface/AOS/webrtc/WebRtcInputRouter.java index 52627e1..9266018 100644 --- a/app/src/main/java/com/openterface/AOS/webrtc/WebRtcInputRouter.java +++ b/app/src/main/java/com/openterface/AOS/webrtc/WebRtcInputRouter.java @@ -73,8 +73,8 @@ public void onMouseEvent(int buttonMask, int x, int y, boolean pressed) { else if ((buttonMask & 0x04) != 0) mouseClick = "SecRightData"; hidSender.sendAbsButtonClick(mouseClick, x, y); } else { - // Button release - hidSender.sendAbsMove(x, y); + // Button release - a plain move would keep it pressed in the host + hidSender.sendAbsRelease(x, y); } lastButtonMask = buttonMask; } else if (buttonMask == 0) { diff --git a/app/src/main/res/drawable/settings_button_selector.xml b/app/src/main/res/drawable/settings_button_selector.xml new file mode 100644 index 0000000..3d7e964 --- /dev/null +++ b/app/src/main/res/drawable/settings_button_selector.xml @@ -0,0 +1,25 @@ + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/app/src/main/res/layout-port/activity_main.xml b/app/src/main/res/layout-port/activity_main.xml index 5196d48..23df2be 100644 --- a/app/src/main/res/layout-port/activity_main.xml +++ b/app/src/main/res/layout-port/activity_main.xml @@ -4,277 +4,290 @@ Zone 2: Video Stream (center, fills remaining space) Zone 3: Module Drawer (hidden by default, overlays bottom of video area) Zone 4: Module Selector Bar (bottom) --> - + android:layout_width="match_parent" + android:layout_height="match_parent" + tools:context="com.openterface.AOS.activity.MainActivity"> - - - - - - + android:layout_height="match_parent" + android:orientation="vertical" + android:background="@android:color/black"> - + + + + + + - - - - + android:layout_weight="1" + android:scrollbars="none"> - - - - - + + + + + + + + android:layout_height="0dp" + android:layout_weight="1" + android:clipChildren="true" + android:clipToPadding="true" + android:background="@color/black"> - + + - - - - + + android:layout_height="match_parent" + android:layout_centerInParent="true" /> + + + + + + + - + + + + + + + + android:layout_alignParentTop="true" + android:layout_alignParentEnd="true" + android:layout_marginTop="12dp" + android:layout_marginEnd="12dp" + android:background="@drawable/shortcut_button_background" + android:src="@drawable/ic_lock_open_24" + android:scaleType="centerInside" + android:padding="8dp" + android:contentDescription="@string/lock_screen_rotation" + android:elevation="5dp" /> - - - - - - - - - - - - + - - + android:layout_height="wrap_content" + android:orientation="vertical" + android:background="@color/background_light" + android:visibility="gone"> - - + + + + + + android:id="@+id/module_selector_bar" + android:layout_width="match_parent" + android:layout_height="56dp" + android:orientation="horizontal" + android:background="@color/card_background" + android:gravity="center_vertical" + android:paddingHorizontal="4dp"> - + - - + - + + - + - - + - + + - + - - + - + + - + + + + + + - - + diff --git a/app/src/main/res/layout/activity_main.xml b/app/src/main/res/layout/activity_main.xml index fc81a66..644cc70 100644 --- a/app/src/main/res/layout/activity_main.xml +++ b/app/src/main/res/layout/activity_main.xml @@ -38,6 +38,13 @@ android:layout_centerInParent="true" android:visibility="visible" /> + + + - - - - - - - - diff --git a/app/src/main/res/layout/module_portrait_settings.xml b/app/src/main/res/layout/module_portrait_settings.xml index 716c642..4e27c55 100644 --- a/app/src/main/res/layout/module_portrait_settings.xml +++ b/app/src/main/res/layout/module_portrait_settings.xml @@ -1,7 +1,13 @@ - + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + diff --git a/app/src/main/res/layout/module_portrait_settings_inner.xml b/app/src/main/res/layout/module_portrait_settings_inner.xml index d6fb60c..d42ab88 100644 --- a/app/src/main/res/layout/module_portrait_settings_inner.xml +++ b/app/src/main/res/layout/module_portrait_settings_inner.xml @@ -636,63 +636,41 @@ android:textColor="@color/text_primary" android:layout_marginBottom="8dp" /> - - + - - - - + android:text="@string/settings_mouse_control_mode" + android:textSize="12sp" + android:textColor="@color/text_secondary" + android:layout_marginBottom="4dp" /> - - - - + + android:orientation="horizontal"> - - - + android:layout_weight="1" + android:layout_marginEnd="4dp" + android:text="@string/settings_mouse_mode_absolute" + android:textSize="11sp" + android:background="@drawable/settings_button_selector" /> - + android:background="@drawable/settings_button_selector" /> diff --git a/app/src/main/res/values-zh-rCN/strings.xml b/app/src/main/res/values-zh-rCN/strings.xml index 17c8887..f2805ad 100644 --- a/app/src/main/res/values-zh-rCN/strings.xml +++ b/app/src/main/res/values-zh-rCN/strings.xml @@ -149,11 +149,10 @@ 主机 - 鼠标控制方式 - 触摸\n模式 - 拖动\n模式 - 拖动 - 指针\n模式 + 鼠标模式 + 绝对模式 + 相对模式 + 拖动中 设备连接 @@ -216,6 +215,10 @@ 鼠标设置 滚轮速度 中等 + 鼠标控制模式 + 相对模式 + 绝对模式 + 输入设置 发送前验证文本 自动保存草稿 diff --git a/app/src/main/res/values/colors.xml b/app/src/main/res/values/colors.xml index ebf23e0..060975d 100644 --- a/app/src/main/res/values/colors.xml +++ b/app/src/main/res/values/colors.xml @@ -58,4 +58,8 @@ #FFD0D0D0 #FFBDBDBD #FFFFE0B2 + + + #FFFFFFFF + #FFBDBDBD \ No newline at end of file diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 4f76b09..db6b94d 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -221,12 +221,10 @@ Host - Mouse Ctrl Way - Touch\nMode - Drag\nMode + Mouse Mode + Absolute + Relative Dragging - Pointer\nMode - Trackpad\nMode Select Baudrate @@ -234,6 +232,7 @@ 115200 bps (Fast) Cancel Current: %s bps + Current: Not connected TouchPad @@ -488,6 +487,10 @@ Mouse Settings Scroll Speed Medium + Mouse Control Mode + Relative + Absolute + Input Settings Pre-send Validation Auto-save Draft diff --git a/app/src/test/java/com/openterface/AOS/webrtc/WebRtcInputRouterTest.java b/app/src/test/java/com/openterface/AOS/webrtc/WebRtcInputRouterTest.java index 7cc89f0..c5a1c11 100644 --- a/app/src/test/java/com/openterface/AOS/webrtc/WebRtcInputRouterTest.java +++ b/app/src/test/java/com/openterface/AOS/webrtc/WebRtcInputRouterTest.java @@ -69,14 +69,15 @@ public void mouseRightButtonPressSendsRightClick() { } @Test - public void mouseButtonReleaseSendsMove() { + public void mouseButtonReleaseSendsRelease() { // First press a button router.onMouseEvent(1, 100, 200, true); assertEquals("sendAbsButtonClick", mockHidSender.lastAction); - // Then release (buttonMask=0) — should send move + // Then release (buttonMask=0) — the button has to be released explicitly, + // a plain move would keep it pressed in the host router.onMouseEvent(0, 100, 200, false); - assertEquals("sendAbsMove", mockHidSender.lastAction); + assertEquals("sendAbsRelease", mockHidSender.lastAction); } @Test @@ -94,6 +95,7 @@ public void mouseMovementOnlyUpdatesPosition() { // the actual mouse action (click/move) should NOT happen. assertNotEquals("sendAbsButtonClick", mockHidSender.lastAction); assertNotEquals("sendAbsMove", mockHidSender.lastAction); + assertNotEquals("sendAbsRelease", mockHidSender.lastAction); } // ====== Keyboard routing tests ====== @@ -393,14 +395,14 @@ public void mouseButtons_simultaneousMiddleAndRight_prioritizesMiddle() { } @Test - public void mouseButtonRelease_fromLeftClick_sendsMove() { + public void mouseButtonRelease_fromLeftClick_sendsRelease() { // Press left button router.onMouseEvent(0x01, 100, 200, true); assertEquals("sendAbsButtonClick", mockHidSender.lastAction); // Release all buttons router.onMouseEvent(0x00, 100, 200, false); - assertEquals("sendAbsMove", mockHidSender.lastAction); + assertEquals("sendAbsRelease", mockHidSender.lastAction); } // ====== Unknown keysyms ====== @@ -453,7 +455,7 @@ public void mixedMouseAndKeyboardEvents() { // Release mouse router.onMouseEvent(0, 100, 200, false); - assertEquals("sendAbsMove", mockHidSender.lastAction); + assertEquals("sendAbsRelease", mockHidSender.lastAction); } // ====== Mock implementations ====== @@ -490,6 +492,13 @@ public void sendAbsMove(int x, int y) { lastY = y; } + @Override + public void sendAbsRelease(int x, int y) { + lastAction = "sendAbsRelease"; + lastX = x; + lastY = y; + } + @Override public void sendAbsButtonClick(String clickType, int x, int y) { lastAction = "sendAbsButtonClick";