From cdb7abe6841c77da085453a73c213ca129c28f16 Mon Sep 17 00:00:00 2001 From: Xtr126 Date: Sat, 5 Oct 2024 11:34:46 +0530 Subject: [PATCH] fix: cursor randomly invisible --- .../xtr/keymapper/server/InputService.java | 1 + .../xtr/keymapper/server/RemoteService.java | 71 ++++++++++--------- 2 files changed, 40 insertions(+), 32 deletions(-) diff --git a/app/src/main/java/xtr/keymapper/server/InputService.java b/app/src/main/java/xtr/keymapper/server/InputService.java index a5429a73..ccc3c8f3 100644 --- a/app/src/main/java/xtr/keymapper/server/InputService.java +++ b/app/src/main/java/xtr/keymapper/server/InputService.java @@ -171,6 +171,7 @@ public void showCursor() { throw new RuntimeException(e); } } + } public void reloadKeymap() { diff --git a/app/src/main/java/xtr/keymapper/server/RemoteService.java b/app/src/main/java/xtr/keymapper/server/RemoteService.java index ce305564..a2bab5ef 100644 --- a/app/src/main/java/xtr/keymapper/server/RemoteService.java +++ b/app/src/main/java/xtr/keymapper/server/RemoteService.java @@ -107,30 +107,30 @@ public void destroy() { } private void addCursorView() { - if (cursorView != null) mHandler.post(() -> { - if(cursorView.isAttachedToWindow()) { - cursorView.setVisibility(View.VISIBLE); - } else { - WindowManager.LayoutParams params = new WindowManager.LayoutParams( - WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.MATCH_PARENT, - TYPE_SECURE_SYSTEM_OVERLAY, - // Don't let the cursor grab the input focus - WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE | - WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE | - WindowManager.LayoutParams.FLAG_FULLSCREEN | - WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN | - WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED, - // Make the underlying application window visible - // through the cursor - PixelFormat.TRANSLUCENT); - try { - windowManager.addView(cursorView, params); - } catch (IllegalStateException e) { // A14 QPR3 https://gist.github.com/RikkaW/be3fe4178903702c54ec73b2fc1187fe - Log.e(TAG, e.getMessage(), e); - cursorView = null; - } + if (cursorView == null) return; + + if(cursorView.isAttachedToWindow()) { + cursorView.setVisibility(View.VISIBLE); + } else { + WindowManager.LayoutParams params = new WindowManager.LayoutParams( + WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.MATCH_PARENT, + TYPE_SECURE_SYSTEM_OVERLAY, + // Don't let the cursor grab the input focus + WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE | + WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE | + WindowManager.LayoutParams.FLAG_FULLSCREEN | + WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN | + WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED, + // Make the underlying application window visible + // through the cursor + PixelFormat.TRANSLUCENT); + try { + windowManager.addView(cursorView, params); + } catch (IllegalStateException e) { // A14 QPR3 issue https://gist.github.com/RikkaW/be3fe4178903702c54ec73b2fc1187fe + cursorView = null; + Log.e(TAG, e.getMessage(), e); } - }); + } } public void prepareCursorOverlayWindow() throws NoSuchMethodException, NoSuchFieldException, IllegalAccessException, InvocationTargetException { @@ -214,17 +214,24 @@ private boolean addNewDevices(String[] data) { */ @Override public void startServer(KeymapProfile profile, KeymapConfig keymapConfig, IRemoteServiceCallback cb, int screenWidth, int screenHeight) throws RemoteException { - if (inputService != null) stopServer(); if (cb != null) cb.asBinder().linkToDeath(this::stopServer, 0); - if (keymapConfig.pointerMode != KeymapConfig.POINTER_SYSTEM) { - addCursorView(); - } - inputService = new InputService(profile, keymapConfig, cb, screenWidth, screenHeight, cursorView, isWaylandClient); - if (!isWaylandClient) { - inputService.setMouseLock(true); - inputService.openDevice(currentDevice); - } + mHandler.post(() -> { + if (keymapConfig.pointerMode != KeymapConfig.POINTER_SYSTEM) { + addCursorView(); + } else { + cursorView = null; + } + try { + inputService = new InputService(profile, keymapConfig, cb, screenWidth, screenHeight, cursorView, isWaylandClient); + if (!isWaylandClient) { + inputService.setMouseLock(true); + inputService.openDevice(currentDevice); + } + } catch (RemoteException e) { + throw new RuntimeException(e); + } + }); } @Override