Skip to content

Commit

Permalink
Rewrite in TypeScript
Browse files Browse the repository at this point in the history
  • Loading branch information
drauggres committed May 14, 2019
1 parent 7a53b39 commit ef3a99a
Show file tree
Hide file tree
Showing 14 changed files with 586 additions and 201 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
/node_modules
/dist
/build
/.idea
8 changes: 6 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,19 @@
"description": "ws client for scrcpy",
"main": "index.js",
"scripts": {
"build": "./node_modules/.bin/browserify src/index.js -o dist/bundle.js",
"compile": "./node_modules/.bin/tsc -p .",
"build": "npm run compile && ./node_modules/.bin/browserify build/generated/index.js -o dist/bundle.js && cp src/index.html dist",
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "Sergey Volkov <s.volkov@netris.ru>",
"license": "MIT",
"dependencies": {
"buffer": "^5.2.1",
"h264-converter": "^0.1.0"
},
"devDependencies": {
"browserify": "^16.2.3"
"browserify": "^16.2.3",
"tslib": "^1.9.3",
"typescript": "^3.4.5"
}
}
173 changes: 173 additions & 0 deletions src/ControlEvent.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,173 @@
import Position from "./Position";
import {Buffer} from "buffer";

export class ControlEvent {
public static TYPE_KEYCODE = 0;
public static TYPE_TEXT = 1;
public static TYPE_MOUSE = 2;
public static TYPE_SCROLL = 3;
public static TYPE_COMMAND = 4;

public static KEYCODE_PAYLOAD_LENGTH = 9;
public static MOUSE_PAYLOAD_LENGTH = 17;
public static SCROLL_PAYLOAD_LENGTH = 20;
public static COMMAND_PAYLOAD_LENGTH = 1;

constructor(readonly type: number) {
this.type = type;
}

public toBuffer(): Buffer {
throw Error('Not implemented');
}

static createKeycodeControlEvent(action: number, keycode: number, metaState: number) {
return new KeyCodeControlEvent(action, keycode, metaState);
}

static createTextControlEvent(text: string) {
return new TextControlEvent(text);
}

static createMotionControlEvent(action: number, buttons: number, position: Position) {
return new MotionControlEvent(action, buttons, position);
}

static createScrollControlEvent(position: Position, hScroll: number, vScroll: number) {
return new ScrollControlEvent(position, hScroll, vScroll);
}

static createCommandControlEvent(action: number) {
return new CommandControlEvent(action);
}
}

export class CommandControlEvent extends ControlEvent{
public static COMMAND_BACK_OR_SCREEN_ON = 0;
public static COMMAND_EXPAND_NOTIFICATION_PANEL = 1;
public static COMMAND_COLLAPSE_NOTIFICATION_PANEL = 2;

constructor(readonly action: number) {
super(ControlEvent.TYPE_COMMAND);
this.action = action;
}

/**
* @override
*/
public toBuffer(): Buffer {
const buffer = new Buffer(ControlEvent.COMMAND_PAYLOAD_LENGTH + 1);
buffer.writeUInt8(this.type, 0);
buffer.writeUInt8(this.action, 1);
return buffer;
}

public toString(): string {
return `KeyCodeControlEvent{action=${this.action}}`;
}
}

export class KeyCodeControlEvent extends ControlEvent{
constructor(readonly action: number, readonly keycode: number, readonly metaState: number) {
super(ControlEvent.TYPE_KEYCODE);
this.action = action;
this.keycode = keycode;
this.metaState = metaState;
}

/**
* @override
*/
public toBuffer(): Buffer {
const buffer = new Buffer(ControlEvent.KEYCODE_PAYLOAD_LENGTH + 1);
buffer.writeUInt8(this.type, 0);
buffer.writeUInt8(this.action, 1);
buffer.writeUInt32BE(this.keycode, 2);
buffer.writeUInt32BE(this.metaState, 6);
return buffer;
}

public toString(): string {
return `KeyCodeControlEvent{action=${this.action}, keycode=${this.keycode}, metaState=${this.metaState}}`;
}
}

export class MotionControlEvent extends ControlEvent{
constructor(readonly action: number, readonly buttons: number, readonly position: Position) {
super(ControlEvent.TYPE_MOUSE);
this.action = action;
this.buttons = buttons;
this.position = position;
}

/**
* @override
*/
public toBuffer(): Buffer {
const buffer: Buffer = new Buffer(ControlEvent.MOUSE_PAYLOAD_LENGTH + 1);
buffer.writeUInt8(this.type, 0);
buffer.writeUInt8(this.action, 1);
buffer.writeUInt32BE(this.buttons, 2);
buffer.writeUInt32BE(this.position.point.x, 6);
buffer.writeUInt32BE(this.position.point.y, 10);
buffer.writeUInt16BE(this.position.screenSize.width, 14);
buffer.writeUInt16BE(this.position.screenSize.height, 16);
return buffer;
}

public toString(): string {
return `MotionControlEvent{action=${this.action}, buttons=${this.buttons}, position=${this.position}}`;
}
}

export class ScrollControlEvent extends ControlEvent{
constructor(readonly position: Position, readonly hScroll: number, readonly vScroll: number) {
super(ControlEvent.TYPE_SCROLL);
}

/**
* @override
*/
public toBuffer(): Buffer {
const buffer = new Buffer(ControlEvent.SCROLL_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);
return buffer;
}

public toString(): string {
return `ScrollControlEvent{hScroll=${this.hScroll}, vScroll=${this.vScroll}, position=${this.position}}`;
}
}

export class TextControlEvent extends ControlEvent{
constructor(readonly text: string) {
super(ControlEvent.TYPE_TEXT);
this.text = text;
}

public getText(): string {
return this.text;
}

/**
* @override
*/
public toBuffer(): Buffer {
const length = this.text.length;
const buffer = new Buffer(length + 1 + 2);
buffer.writeUInt8(this.type, 0);
buffer.writeUInt16BE(length, 1);
buffer.write(this.text, 3);
return buffer;
}

public toString(): string {
return `TextControlEvent{text=${this.text}}`;
}
}
19 changes: 19 additions & 0 deletions src/MotionEvent.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
export default class MotionEvent {
public static ACTION_DOWN = 0;
public static ACTION_UP = 1;
public static ACTION_MOVE = 2;
/**
* Button constant: Primary button (left mouse button).
*/
public static BUTTON_PRIMARY = 1 << 0;

/**
* Button constant: Secondary button (right mouse button).
*/
public static BUTTON_SECONDARY = 1 << 1;

/**
* Button constant: Tertiary button (middle mouse button).
*/
public static BUTTON_TERTIARY = 1 << 2
};
20 changes: 20 additions & 0 deletions src/Point.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
export default class Point {
constructor(readonly x: number, readonly y: number) {
this.x = Math.round(x);
this.y = Math.round(y);
}

public equals(o: Point): boolean {
if (this === o) {
return true;
}
if (o == null) {
return false;
}
return this.x === o.x && this.y === o.y;
}

public toString(): string {
return `Point{x=${this.x}, y=${this.y}}`;
}
}
24 changes: 24 additions & 0 deletions src/Position.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import Point from "./Point";
import Size from "./Size";

export default class Position {
public constructor(readonly point: Point, readonly screenSize: Size) {
this.point = point;
this.screenSize = screenSize;
}

public equals(o: Position): boolean {
if (this === o) {
return true;
}
if (o == null) {
return false;
}

return this.point.equals(o.point) && this.screenSize.equals(o.screenSize);
}

public toString(): string {
return `Position{point=${this.point}, screenSize=${this.screenSize}}`;
}
}
8 changes: 8 additions & 0 deletions src/Rect.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
export default class Rect {
constructor(readonly left: number, readonly top: number, readonly right: number, readonly bottom: number) {
this.left = left;
this.top = top;
this.right = right;
this.bottom = bottom;
}
}
30 changes: 30 additions & 0 deletions src/Size.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import Rect from "./Rect";

export default class Size {
constructor(readonly width: number, readonly height: number) {
this.width = width;
this.height = height;
}

public rotate(): Size {
return new Size(this.height, this.width);
}

public toRect(): Rect {
return new Rect(0, 0, this.width, this.height);
}

public equals(o: Size): boolean {
if (this === o) {
return true;
}
if (o == null) {
return false;
}
return this.width == o.width && this.height == o.height;
}

public toString(): string {
return `Size{width=${this.width}, height=${this.height}}`;
}
}
42 changes: 42 additions & 0 deletions src/StreamInfo.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
interface StreamInfoInterface {
"width": number
"height": number
"frameRate": number
"bitRate": number
"type": string
}

export class StreamInfo {
readonly width: number;
readonly height: number;
readonly frameRate: number;
readonly bitRate: number;
constructor(data: StreamInfoInterface) {
if (typeof data.width !== 'number') {
throw TypeError('Wrong width type');
}
if (typeof data.height !== 'number') {
throw TypeError('Wrong height type');
}
if (typeof data.frameRate !== 'number') {
throw TypeError('Wrong frameRate type');
}
if (typeof data.bitRate !== 'number') {
throw TypeError('Wrong bitRate type');
}
this.width = data.width;
this.height = data.height;
this.frameRate = data.frameRate;
this.bitRate = data.bitRate;
}

public equals(streamInfo: StreamInfo): boolean {
if (streamInfo === null) {
return false;
}
return this.width === streamInfo.width &&
this.height === streamInfo.height &&
this.frameRate === streamInfo.frameRate &&
this.bitRate === streamInfo.bitRate;
}
}
32 changes: 0 additions & 32 deletions src/const.js

This file was deleted.

2 changes: 1 addition & 1 deletion src/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
background-color: black;
}
</style>
<script src="../dist/bundle.js" type="application/javascript"></script>
<script src="bundle.js" type="application/javascript"></script>
<script type="application/javascript">

</script>
Expand Down
Loading

0 comments on commit ef3a99a

Please sign in to comment.