Skip to content

Commit

Permalink
feat(android): generate scroll event
Browse files Browse the repository at this point in the history
fix #133
  • Loading branch information
drauggres committed Oct 4, 2021
1 parent a5087b6 commit 961ee59
Show file tree
Hide file tree
Showing 6 changed files with 107 additions and 76 deletions.
6 changes: 3 additions & 3 deletions src/app/applDevice/client/StreamClientQVHack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { StreamReceiver } from '../../client/StreamReceiver';
import Position from '../../Position';
import { MsePlayerForQVHack } from '../../player/MsePlayerForQVHack';
import { BasePlayer, PlayerClass } from '../../player/BasePlayer';
import { SimpleTouchHandler, TouchHandlerListener } from '../../touchHandler/SimpleTouchHandler';
import { SimpleInteractionHandler, TouchHandlerListener } from '../../interactionHandler/SimpleInteractionHandler';
import { ACTION } from '../../../common/Action';
import { ParsedUrlQuery } from 'querystring';
import Util from '../../Util';
Expand All @@ -29,7 +29,7 @@ export class StreamClientQVHack extends BaseClient<ParamsStreamQVHack, never> im
private waitForWda?: boolean;
private readonly streamReceiver: StreamReceiver<ParamsStreamQVHack>;
private videoWrapper?: HTMLElement;
private touchHandler?: SimpleTouchHandler;
private touchHandler?: SimpleInteractionHandler;
private readonly udid: string;

public static registerPlayer(playerClass: PlayerClass): void {
Expand Down Expand Up @@ -210,7 +210,7 @@ export class StreamClientQVHack extends BaseClient<ParamsStreamQVHack, never> im
if (this.touchHandler) {
return;
}
this.touchHandler = new SimpleTouchHandler(player, this);
this.touchHandler = new SimpleInteractionHandler(player, this);
}

public performClick(position: Position): void {
Expand Down
15 changes: 8 additions & 7 deletions src/app/controlMessage/ScrollControlMessage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,14 @@ export class ScrollControlMessage extends ControlMessage {
*/
public toBuffer(): Buffer {
const buffer = new Buffer(ScrollControlMessage.PAYLOAD_LENGTH + 1);
buffer.writeUInt8(this.type, 0);
buffer.writeUInt32BE(this.position.point.x, 1);
buffer.writeUInt32BE(this.position.point.y, 5);
buffer.writeUInt16BE(this.position.screenSize.width, 9);
buffer.writeUInt16BE(this.position.screenSize.height, 11);
buffer.writeUInt32BE(this.hScroll, 13);
buffer.writeUInt32BE(this.vScroll, 17);
let offset = 0;
offset = buffer.writeUInt8(this.type, offset);
offset = buffer.writeUInt32BE(this.position.point.x, offset);
offset = buffer.writeUInt32BE(this.position.point.y, offset);
offset = buffer.writeUInt16BE(this.position.screenSize.width, offset);
offset = buffer.writeUInt16BE(this.position.screenSize.height, offset);
offset = buffer.writeInt32BE(this.hScroll, offset);
buffer.writeInt32BE(this.vScroll, offset);
return buffer;
}

Expand Down
8 changes: 4 additions & 4 deletions src/app/googDevice/client/StreamClientScrcpy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import { ConfigureScrcpy } from './ConfigureScrcpy';
import { DeviceTracker } from './DeviceTracker';
import { ControlCenterCommand } from '../../../common/ControlCenterCommand';
import { html } from '../../ui/HtmlTag';
import { FeaturedTouchHandler, TouchHandlerListener } from '../../touchHandler/FeaturedTouchHandler';
import { FeaturedInteractionHandler, InteractionHandlerListener } from '../../interactionHandler/FeaturedInteractionHandler';
import DeviceMessage from '../DeviceMessage';
import { DisplayInfo } from '../../DisplayInfo';
import { Attribute } from '../../Attribute';
Expand All @@ -41,7 +41,7 @@ const TAG = '[StreamClientScrcpy]';

export class StreamClientScrcpy
extends BaseClient<ParamsStreamScrcpy, never>
implements KeyEventListener, TouchHandlerListener {
implements KeyEventListener, InteractionHandlerListener {
public static ACTION = 'stream';
private static players: Map<string, PlayerClass> = new Map<string, PlayerClass>();

Expand All @@ -51,7 +51,7 @@ export class StreamClientScrcpy
private clientsCount = -1;
private joinedStream = false;
private requestedVideoSettings?: VideoSettings;
private touchHandler?: FeaturedTouchHandler;
private touchHandler?: FeaturedInteractionHandler;
private droidMoreBox?: DroidMoreBox;
private player?: BasePlayer;
private filePushHandler?: FilePushHandler;
Expand Down Expand Up @@ -386,7 +386,7 @@ export class StreamClientScrcpy
if (this.touchHandler) {
return;
}
this.touchHandler = new FeaturedTouchHandler(player, this);
this.touchHandler = new FeaturedInteractionHandler(player, this);
}

private applyNewVideoSettings(videoSettings: VideoSettings, saveToStorage: boolean): void {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,47 +1,77 @@
import { KeyEventNames, TouchEventNames, TouchHandler } from './TouchHandler';
import { InteractionEvents, KeyEventNames, InteractionHandler } from './InteractionHandler';
import { BasePlayer } from '../player/BasePlayer';
import { ControlMessage } from '../controlMessage/ControlMessage';
import { TouchControlMessage } from '../controlMessage/TouchControlMessage';
import MotionEvent from '../MotionEvent';
import ScreenInfo from '../ScreenInfo';
import { ScrollControlMessage } from '../controlMessage/ScrollControlMessage';

const TAG = '[FeaturedTouchHandler]';

export interface TouchHandlerListener {
sendMessage: (messages: TouchControlMessage) => void;
export interface InteractionHandlerListener {
sendMessage: (message: ControlMessage) => void;
}

export class FeaturedTouchHandler extends TouchHandler {
private readonly storedFromMouseEvent = new Map<number, TouchControlMessage>();
private readonly storedFromTouchEvent = new Map<number, TouchControlMessage>();
private static readonly touchEventsNames: TouchEventNames[] = [
export class FeaturedInteractionHandler extends InteractionHandler {
private static readonly touchEventsNames: InteractionEvents[] = [
'touchstart',
'touchend',
'touchmove',
'touchcancel',
'mousedown',
'mouseup',
'mousemove',
'wheel',
];
private static readonly keyEventsNames: KeyEventNames[] = ['keydown', 'keyup'];
public static SCROLL_EVENT_THROTTLING_TIME = 30; // one event per 50ms
private readonly storedFromMouseEvent = new Map<number, TouchControlMessage>();
private readonly storedFromTouchEvent = new Map<number, TouchControlMessage>();
private lastScrollEvent?: { time: number; hScroll: number; vScroll: number };

constructor(player: BasePlayer, public readonly listener: TouchHandlerListener) {
super(player, FeaturedTouchHandler.touchEventsNames, FeaturedTouchHandler.keyEventsNames);
constructor(player: BasePlayer, public readonly listener: InteractionHandlerListener) {
super(player, FeaturedInteractionHandler.touchEventsNames, FeaturedInteractionHandler.keyEventsNames);
this.tag.addEventListener('mouseleave', this.onMouseLeave);
this.tag.addEventListener('mouseenter', this.onMouseEnter);
}

protected onTouchEvent(e: MouseEvent | TouchEvent): void {
public buildScrollEvent(e: WheelEvent, screenInfo: ScreenInfo): ScrollControlMessage[] {
const messages: ScrollControlMessage[] = [];
const touchOnClient = InteractionHandler.buildTouchOnClient(e, screenInfo);
if (touchOnClient) {
const hScroll = e.deltaX > 0 ? -1 : e.deltaX < -0 ? 1 : 0;
const vScroll = e.deltaY > 0 ? -1 : e.deltaY < -0 ? 1 : 0;
const time = Date.now();
if (
!this.lastScrollEvent ||
time - this.lastScrollEvent.time > FeaturedInteractionHandler.SCROLL_EVENT_THROTTLING_TIME ||
this.lastScrollEvent.vScroll !== vScroll ||
this.lastScrollEvent.hScroll !== hScroll
) {
this.lastScrollEvent = { time, hScroll, vScroll };
messages.push(new ScrollControlMessage(touchOnClient.touch.position, hScroll, vScroll));
}
}
return messages;
}

protected onInteraction(e: MouseEvent | TouchEvent): void {
const screenInfo = this.player.getScreenInfo();
if (!screenInfo) {
return;
}
let messages: TouchControlMessage[];
let messages: ControlMessage[];
let storage: Map<number, TouchControlMessage>;
if (e instanceof MouseEvent) {
if (e.target !== this.tag) {
return;
}
storage = this.storedFromMouseEvent;
messages = this.buildTouchEvent(e, screenInfo, storage);
if (window['WheelEvent'] && e instanceof WheelEvent) {
messages = this.buildScrollEvent(e, screenInfo);
} else {
storage = this.storedFromMouseEvent;
messages = this.buildTouchEvent(e, screenInfo, storage);
}
if (this.over) {
this.lastPosition = e;
}
Expand Down Expand Up @@ -75,7 +105,7 @@ export class FeaturedTouchHandler extends TouchHandler {
}
const { ctrlKey, shiftKey } = e;
const { target, button, buttons, clientY, clientX } = this.lastPosition;
const type = TouchHandler.SIMULATE_MULTI_TOUCH;
const type = InteractionHandler.SIMULATE_MULTI_TOUCH;
const event = { ctrlKey, shiftKey, type, target, button, buttons, clientX, clientY };
this.buildTouchEvent(event, screenInfo, new Map());
}
Expand All @@ -87,7 +117,7 @@ export class FeaturedTouchHandler extends TouchHandler {
this.lastPosition = undefined;
this.over = false;
this.storedFromMouseEvent.forEach((message) => {
this.listener.sendMessage(TouchHandler.createEmulatedMessage(MotionEvent.ACTION_UP, message));
this.listener.sendMessage(InteractionHandler.createEmulatedMessage(MotionEvent.ACTION_UP, message));
});
this.storedFromMouseEvent.clear();
this.clearCanvas();
Expand All @@ -98,6 +128,5 @@ export class FeaturedTouchHandler extends TouchHandler {
this.tag.removeEventListener('mouseleave', this.onMouseLeave);
this.tag.removeEventListener('mouseenter', this.onMouseEnter);
this.storedFromMouseEvent.clear();
this.storedFromMouseEvent.clear();
}
}
Loading

0 comments on commit 961ee59

Please sign in to comment.