From 86b55c3df99918603553cfb7804320b28b1bdee8 Mon Sep 17 00:00:00 2001 From: Xtr126 Date: Wed, 30 Aug 2023 19:50:35 +0530 Subject: [PATCH] handle mouse events from wayland client --- .../xtr/keymapper/server/InputService.java | 23 ++++++++++++++++++ .../xtr/keymapper/server/RemoteService.java | 24 +++++++++++-------- .../touchpointer/KeyEventHandler.java | 8 +++---- .../touchpointer/MouseEventHandler.java | 12 ++++++++++ 4 files changed, 53 insertions(+), 14 deletions(-) diff --git a/app/src/main/java/xtr/keymapper/server/InputService.java b/app/src/main/java/xtr/keymapper/server/InputService.java index 778a3ac8..1bc1961e 100644 --- a/app/src/main/java/xtr/keymapper/server/InputService.java +++ b/app/src/main/java/xtr/keymapper/server/InputService.java @@ -1,5 +1,7 @@ package xtr.keymapper.server; +import static xtr.keymapper.InputEventCodes.*; + import android.os.RemoteException; import android.view.MotionEvent; @@ -115,6 +117,27 @@ public void stop() { public native void setMouseLock(boolean lock); + public void sendWaylandMouseEvent(String line) { + String[] input_event = line.split("\\s+"); + int value = Integer.parseInt(input_event[2]); + switch (input_event[1]) { + case "ABS_X": + mouseEventHandler.evAbsX(value); + break; + case "ABS_Y": + mouseEventHandler.evAbsY(value); + break; + case "REL_WHEEL": + mouseEventHandler.handleEvent(REL_WHEEL, value); + break; + case "BTN_LEFT": + mouseEventHandler.handleEvent(BTN_MOUSE, value); + break; + case "BTN_RIGHT": + mouseEventHandler.handleEvent(BTN_RIGHT, value); + break; + } + } /* * Called from native code to send mouse event to client */ diff --git a/app/src/main/java/xtr/keymapper/server/RemoteService.java b/app/src/main/java/xtr/keymapper/server/RemoteService.java index f9c54fbe..df75ec45 100644 --- a/app/src/main/java/xtr/keymapper/server/RemoteService.java +++ b/app/src/main/java/xtr/keymapper/server/RemoteService.java @@ -59,9 +59,13 @@ private void start_getevent() { String line; boolean stopEvents = false; while ((line = getevent.readLine()) != null) { - if (addNewDevices(line)) if (!stopEvents) { - if (inputService != null) - inputService.getKeyEventHandler().handleEvent(line); + String[] data = line.split(":"); // split a string like "/dev/input/event2: EV_REL REL_X ffffffff" + if (addNewDevices(data)) if (!stopEvents) { + if (inputService != null) { + inputService.getKeyEventHandler().handleEvent(data[1]); + if (isWaylandClient && data[0].contains("wl_pointer")) + inputService.sendWaylandMouseEvent(data[1]); + } if (mOnKeyEventListener != null) mOnKeyEventListener.onKeyEvent(line); } } @@ -71,12 +75,10 @@ private void start_getevent() { }).start(); } - private boolean addNewDevices(String line) { - String[] input_event, data; - String evdev; - data = line.split(":"); // split a string like "/dev/input/event2: EV_REL REL_X ffffffff" + private boolean addNewDevices(String[] data) { + String[] input_event; if (data.length != 2) return false; - evdev = data[0]; + String evdev = data[0]; input_event = data[1].split("\\s+"); if (isWaylandClient) return true; @@ -101,8 +103,10 @@ public boolean isRoot() { @Override public void startServer(KeymapProfile profile, KeymapConfig keymapConfig, IRemoteServiceCallback cb, int screenWidth, int screenHeight) { inputService = new InputService(profile, keymapConfig, cb, screenWidth, screenHeight); - inputService.setMouseLock(true); - inputService.openDevice(currentDevice); + if (!isWaylandClient) { + inputService.setMouseLock(true); + inputService.openDevice(currentDevice); + } } @Override diff --git a/app/src/main/java/xtr/keymapper/touchpointer/KeyEventHandler.java b/app/src/main/java/xtr/keymapper/touchpointer/KeyEventHandler.java index 7abb4079..c9ff3724 100644 --- a/app/src/main/java/xtr/keymapper/touchpointer/KeyEventHandler.java +++ b/app/src/main/java/xtr/keymapper/touchpointer/KeyEventHandler.java @@ -82,17 +82,17 @@ public static class KeyEvent { } public void handleEvent(String line) throws RemoteException { - // line: /dev/input/event3: EV_KEY KEY_X DOWN + // line: EV_KEY KEY_X DOWN String[] input_event = line.split("\\s+"); - if (!input_event[1].equals("EV_KEY")) return; + if (!input_event[0].equals("EV_KEY")) return; KeyEvent event = new KeyEvent(); - event.code = input_event[2]; + event.code = input_event[1]; if (!event.code.contains("KEY_")) return; KeymapConfig keymapConfig = mInput.getKeymapConfig(); - switch (input_event[3]) { + switch (input_event[2]) { case "UP": event.action = UP; break; diff --git a/app/src/main/java/xtr/keymapper/touchpointer/MouseEventHandler.java b/app/src/main/java/xtr/keymapper/touchpointer/MouseEventHandler.java index a04d787e..07e515bf 100644 --- a/app/src/main/java/xtr/keymapper/touchpointer/MouseEventHandler.java +++ b/app/src/main/java/xtr/keymapper/touchpointer/MouseEventHandler.java @@ -138,6 +138,18 @@ public void handleEvent(int code, int value) { if (code == REL_X || code == REL_Y) movePointer(); } + public void evAbsY(int y) { + this.y1 = y; + if (pointer_down) mInput.injectEvent(x1, y1, MOVE, pointerId1); + movePointer(); + } + + public void evAbsX(int x) { + this.x1 = x; + if (pointer_down) mInput.injectEvent(x1, y1, MOVE, pointerId1); + movePointer(); + } + public void stop() { mouseAimHandler = null; scrollZoomHandler = null;