mirror of
https://github.com/redphx/better-xcloud.git
synced 2025-08-05 12:56:42 +02:00
Support emulated MKB in Android app
commit ad365d4ee854971122f0e8cb9157ed44b3aac0d8 Author: redphx <96280+redphx@users.noreply.github.com> Date: Wed May 29 17:19:57 2024 +0700 Fix not able to reconnect to WebSocket server when switching game commit ca9369318d4cbb831650e8ca631e7997dc7706cb Author: redphx <96280+redphx@users.noreply.github.com> Date: Wed May 29 17:19:23 2024 +0700 Stop emulated MKB when losing pointer capture commit 8cca1a0554c46b8f61455e79d5b16f1dff9a8014 Author: redphx <96280+redphx@users.noreply.github.com> Date: Wed May 29 17:17:42 2024 +0700 Allow fine-tuning maximum video bitrate commit 763d414d560d9d2aa6710fd60e3f80bf43a534d6 Author: redphx <96280+redphx@users.noreply.github.com> Date: Wed May 29 08:13:56 2024 +0700 Update mouse settings commit d65c5ab4e4a33ed8ad13acf0a15c4bb5ace870eb Author: redphx <96280+redphx@users.noreply.github.com> Date: Wed May 29 08:10:49 2024 +0700 Increase MKB dialog's bg opacity commit 3e72f2ad2700737c8148ef47629528954a606578 Author: redphx <96280+redphx@users.noreply.github.com> Date: Wed May 29 08:02:57 2024 +0700 Show/hide MKB dialog properly commit e7786f36508e3aa843604d9886861930bada5d60 Author: redphx <96280+redphx@users.noreply.github.com> Date: Wed May 29 07:47:21 2024 +0700 Fix connecting to WebSocket server when it's not ready commit 512d8c227a057e5c0399bf128bc1c52a88fcf853 Author: redphx <96280+redphx@users.noreply.github.com> Date: Wed May 29 07:18:06 2024 +0700 Fix arrow keys not working in Android app commit 0ce90f47f37d057d5a4fab0003e2bec8960d1eee Author: redphx <96280+redphx@users.noreply.github.com> Date: Tue May 28 17:36:56 2024 +0700 Set mouse's default sensitivities to 50 commit 16eb48660dd44497e16ca22343a880d9a2e53a30 Author: redphx <96280+redphx@users.noreply.github.com> Date: Tue May 28 17:33:37 2024 +0700 Allow emulated MKB feature in Android app commit c3d0e64f8502e19cd4f167fea4cdbdfc2e14b65e Author: redphx <96280+redphx@users.noreply.github.com> Date: Tue May 28 17:32:49 2024 +0700 Remove stick decay settings commit d289d2a0dea61a440c1bc6b9392920b8e6ab6298 Author: redphx <96280+redphx@users.noreply.github.com> Date: Tue May 28 17:21:39 2024 +0700 Remove stick decaying feature commit 76bd001d98bac53f757f4ae793b2850aad055007 Author: redphx <96280+redphx@users.noreply.github.com> Date: Tue May 28 17:21:14 2024 +0700 Update data structure commit c5d3c87da9e6624ebefb288f6d7c8d06dc00916b Author: redphx <96280+redphx@users.noreply.github.com> Date: Tue May 28 08:14:27 2024 +0700 Fix not toggling the MKB feature correctly commit 9615535cf0e4d4372e201aefb6f1231ddbc22536 Author: redphx <96280+redphx@users.noreply.github.com> Date: Mon May 27 20:51:57 2024 +0700 Handle mouse data from the app
This commit is contained in:
152
src/modules/mkb/pointer-client.ts
Normal file
152
src/modules/mkb/pointer-client.ts
Normal file
@@ -0,0 +1,152 @@
|
||||
import { BxLogger } from "@/utils/bx-logger";
|
||||
import type { MkbHandler } from "./mkb-handler";
|
||||
import { KeyHelper } from "./key-helper";
|
||||
import { WheelCode } from "./definitions";
|
||||
import { Toast } from "@/utils/toast";
|
||||
|
||||
const LOG_TAG = 'PointerClient';
|
||||
|
||||
enum PointerAction {
|
||||
MOVE = 1,
|
||||
BUTTON_PRESS = 2,
|
||||
BUTTON_RELEASE = 3,
|
||||
SCROLL = 4,
|
||||
POINTER_CAPTURE_CHANGED = 5,
|
||||
}
|
||||
|
||||
const FixedMouseIndex = {
|
||||
1: 0,
|
||||
2: 2,
|
||||
4: 1,
|
||||
}
|
||||
|
||||
export class PointerClient {
|
||||
static #PORT = 9269;
|
||||
|
||||
private static instance: PointerClient;
|
||||
public static getInstance(): PointerClient {
|
||||
if (!PointerClient.instance) {
|
||||
PointerClient.instance = new PointerClient();
|
||||
}
|
||||
|
||||
return PointerClient.instance;
|
||||
}
|
||||
|
||||
#socket: WebSocket | undefined | null;
|
||||
#mkbHandler: MkbHandler | undefined;
|
||||
|
||||
start(mkbHandler: MkbHandler) {
|
||||
this.#mkbHandler = mkbHandler;
|
||||
|
||||
// Create WebSocket connection.
|
||||
this.#socket = new WebSocket(`ws://localhost:${PointerClient.#PORT}`);
|
||||
this.#socket.binaryType = 'arraybuffer';
|
||||
|
||||
// Connection opened
|
||||
this.#socket.addEventListener('open', (event) => {
|
||||
BxLogger.info(LOG_TAG, 'connected')
|
||||
});
|
||||
|
||||
// Error
|
||||
this.#socket.addEventListener('error', (event) => {
|
||||
BxLogger.error(LOG_TAG, event);
|
||||
Toast.show('Cannot setup mouse');
|
||||
});
|
||||
|
||||
this.#socket.addEventListener('close', (event) => {
|
||||
this.#socket = null;
|
||||
});
|
||||
|
||||
// Listen for messages
|
||||
this.#socket.addEventListener('message', (event) => {
|
||||
const dataView = new DataView(event.data);
|
||||
|
||||
let messageType = dataView.getInt8(0);
|
||||
let offset = Int8Array.BYTES_PER_ELEMENT;
|
||||
switch (messageType) {
|
||||
case PointerAction.MOVE:
|
||||
this.onMove(dataView, offset);
|
||||
break;
|
||||
|
||||
case PointerAction.BUTTON_PRESS:
|
||||
case PointerAction.BUTTON_RELEASE:
|
||||
this.onPress(messageType, dataView, offset);
|
||||
break;
|
||||
|
||||
case PointerAction.SCROLL:
|
||||
this.onScroll(dataView, offset);
|
||||
break;
|
||||
|
||||
case PointerAction.POINTER_CAPTURE_CHANGED:
|
||||
this.onPointerCaptureChanged(dataView, offset);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
onMove(dataView: DataView, offset: number) {
|
||||
// [X, Y]
|
||||
const x = dataView.getInt16(offset);
|
||||
offset += Int16Array.BYTES_PER_ELEMENT;
|
||||
const y = dataView.getInt16(offset);
|
||||
|
||||
this.#mkbHandler?.handleMouseMove({
|
||||
movementX: x,
|
||||
movementY: y,
|
||||
});
|
||||
// BxLogger.info(LOG_TAG, 'move', x, y);
|
||||
}
|
||||
|
||||
onPress(messageType: PointerAction, dataView: DataView, offset: number) {
|
||||
const buttonIndex = dataView.getInt8(offset);
|
||||
const fixedIndex = FixedMouseIndex[buttonIndex as keyof typeof FixedMouseIndex];
|
||||
const keyCode = 'Mouse' + fixedIndex;
|
||||
|
||||
this.#mkbHandler?.handleMouseClick({
|
||||
key: {
|
||||
code: keyCode,
|
||||
name: KeyHelper.codeToKeyName(keyCode),
|
||||
},
|
||||
pressed: messageType === PointerAction.BUTTON_PRESS,
|
||||
});
|
||||
|
||||
// BxLogger.info(LOG_TAG, 'press', buttonIndex);
|
||||
}
|
||||
|
||||
onScroll(dataView: DataView, offset: number) {
|
||||
// [V_SCROLL, H_SCROLL]
|
||||
const vScroll = dataView.getInt8(offset);
|
||||
offset += Int8Array.BYTES_PER_ELEMENT;
|
||||
const hScroll = dataView.getInt8(offset);
|
||||
|
||||
let code = '';
|
||||
if (vScroll < 0) {
|
||||
code = WheelCode.SCROLL_UP;
|
||||
} else if (vScroll > 0) {
|
||||
code = WheelCode.SCROLL_DOWN;
|
||||
} else if (hScroll < 0) {
|
||||
code = WheelCode.SCROLL_LEFT;
|
||||
} else if (hScroll > 0) {
|
||||
code = WheelCode.SCROLL_RIGHT;
|
||||
}
|
||||
|
||||
code && this.#mkbHandler?.handleMouseWheel({
|
||||
key: {
|
||||
code: code,
|
||||
name: KeyHelper.codeToKeyName(code),
|
||||
},
|
||||
});
|
||||
|
||||
// BxLogger.info(LOG_TAG, 'scroll', vScroll, hScroll);
|
||||
}
|
||||
|
||||
onPointerCaptureChanged(dataView: DataView, offset: number) {
|
||||
const hasCapture = dataView.getInt8(offset) === 1;
|
||||
!hasCapture && this.#mkbHandler?.stop();
|
||||
}
|
||||
|
||||
stop() {
|
||||
try {
|
||||
this.#socket?.close();
|
||||
} catch (e) {}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user