forked from indyblue/android_remote_control
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
277 lines (250 loc) · 7.77 KB
/
index.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
<!doctype html>
<html>
<head>
<title>Android Remote Session</title>
<meta charset="UTF-8" />
<style>
.fc {
display: flex;
flex-direction: column;
align-items: stretch;
}
.fc>*,
.fr>* {
flex: 1;
}
.f0 {
flex: initial !important;
flex-shrink: 1;
}
.fr {
display: flex;
flex-direction: row;
align-items: stretch;
}
.bb {
border: 1px solid black;
}
#canvas {
display: inline-block;
}
.off {
background-color: red;
}
</style>
</head>
<body class='fr'>
<div class='f0'>
<canvas class='bb' tabindex="0" id="canvas"></canvas>
</div>
<div class='fc' id='controls'>
<div class='fr f0 btnr'>
<button tabindex="2" id="power">Power</button>
<button tabindex="3" id="get-clip">Get Clipboard</button>
<button tabindex="4" id="set-clip">Set Clipboard</button>
</div>
<textarea tabindex="5" id="txt-clip" cols='10'></textarea>
<div class='fr f0 btnr'>
<button tabindex="6" id="list-ports">Ports</button>
<input tabindex='7' id='port-num' value='19000,19001,7007' size='1' style='flex:5;' />
<button tabindex="8" id="fwd-port">Fwd</button>
<button tabindex="9" id="rev-port">Rev</button>
</div>
<div class='bb' style='white-space: pre-wrap;' id='port-list'> </div>
<div class='fr f0 btnr'>
<button class='off' tabindex="10" id="minicap">Minicap</button>
</div>
</div>
<script>
'option strict';
var debug = false;
function setdebug() {
debug = !debug;
wsJson({ event: 'debug', val: debug });
}
/*jshint browser:true*/
var BLANK_IMG =
'data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw=='
var canvas = document.getElementById('canvas')
, g = canvas.getContext('2d')
, elClts = document.getElementById('controls')
, resM = 25
, fcr = (row) => {
for (var e of document.querySelectorAll('.btnr')) {
e.classList.remove(row ? 'fc' : 'fr');
e.classList.add(!row ? 'fc' : 'fr');
}
}
, minW = 0
, fnRes = () => {
fcr(elClts.scrollWidth > 310);
canvas.style.maxHeight = (window.innerHeight - resM) + 'px'
canvas.style.maxWidth = (window.innerWidth - resM) + 'px'
}
, minicap = document.getElementById('minicap')
, landscape = false;
fnRes();
window.onresize = fnRes;
function wsOnMessage(message) {
if (typeof message.data === 'string') {
let j = null;
try { j = JSON.parse(message.data); } catch (e) { }
if (j) {
if (j.type == 'GetClipboardResponse') getClipResponse(j);
if (j.type == 'ports') getPortsResponse(j);
if (j.type == 'minicapStatus') {
if (!j.state) blankScreen();
console.log(message.data);
}
}
} else {
var blob = new Blob([message.data], { type: 'image/jpeg' })
var URL = window.URL || window.webkitURL
var img = new Image()
img.onload = function () {
console.log(img.width, img.height)
canvas.width = img.width;
canvas.height = img.height;
fnOrientation(img.width, img.height);
g.drawImage(img, 0, 0);
img.onload = null;
img.src = BLANK_IMG;
img = null;
u = null;
blob = null;
fnRes();
minicap.classList.remove('off');
}
var u = URL.createObjectURL(blob)
img.src = u
}
}
var ws;
function createWs() {
ws = new WebSocket('ws://' + window.location.host, 'minicap')
ws.binaryType = 'blob'
ws.onclose = function () {
blankScreen();
console.log('ws close...try reopen in 2 seconds');
setTimeout(createWs, 2000);
}
ws.onopen = function () { console.log('ws open'); };
ws.onerror = function () { console.log('ws error', arguments); };
ws.onmessage = wsOnMessage;
}
function wsJson(obj) {
if (ws.readyState === ws.OPEN) {
var json = JSON.stringify(obj);
if (debug) console.log(json);
ws.send(json);
} else console.log('websocket is closed');
}
createWs();
function blankScreen() {
g.fillStyle = "#a00";
g.fillRect(0, 0, canvas.width, canvas.height);
minicap.classList.add('off');
}
function fnOrientation(w, h) {
landscape = w / h > 1;
document.body.classList.remove(!landscape ? 'fc' : 'fr');
document.body.classList.add(landscape ? 'fc' : 'fr');
}
canvas.mdown = false;
canvas.debounce = 0;
function mouseHandler(t, e) {
var cx = e.clientX || (e.touches[0] || e.changedTouches[0] || {}).clientX,
cy = e.clientY || (e.touches[0] || e.changedTouches[0] || {}).clientY;
var x = cx / canvas.scrollWidth, y = cy / canvas.scrollHeight;
if (landscape) { var tmp = x; x = 1 - y; y = tmp; }
wsJson({ event: 'mouse', t: t, x: x, y: y });
if (debug) console.log(e.type, t, canvas.mdown, cx, cy, canvas.scrollWidth, canvas.scrollHeight);
}
var fnmm = function (e) {
if (!canvas.mdown) return;
if (!canvas.debounce) {
canvas.debounce++;
mouseHandler('m', e);
setTimeout(() => {
canvas.debounce = 0;
}, 100);
}
};
canvas.addEventListener('mousemove', fnmm);
canvas.addEventListener('touchmove', fnmm);
var fnmd = function (e) {
if (canvas.mdown) return;
canvas.mdown = true;
mouseHandler('d', e);
e.preventDefault();
canvas.focus();
};
canvas.addEventListener('mousedown', fnmd);
canvas.addEventListener('touchstart', fnmd);
var fnmu = function (e) {
if (!canvas.mdown) return;
canvas.mdown = false;
mouseHandler('u', e);
e.preventDefault();
};
canvas.addEventListener('mouseup', fnmu);
canvas.addEventListener('touchend', fnmu);
function keyHandler(e, isDown) {
var cl = e.getModifierState('CapsLock');
var obj = {
event: 'key', key: e.keyCode,
mods: [e.shiftKey, e.ctrlKey, e.altKey, e.metaKey, cl], isDown
}
wsJson(obj);
}
canvas.addEventListener('keydown', function (e) {
keyHandler(e, true);
e.preventDefault();
});
canvas.addEventListener('keyup', function (e) {
keyHandler(e, false);
e.preventDefault();
});
var bPower = document.getElementById('power');
bPower.onclick = function (e) {
wsJson({ event: 'power' });
};
var getClip = document.getElementById('get-clip');
var txtClip = document.getElementById('txt-clip');
var setClip = document.getElementById('set-clip');
getClip.onclick = function (e) {
txtClip.allowUpdate = true;
wsJson({ event: 'getclip' });
};
setClip.onclick = function (e) {
wsJson({ event: 'setclip', text: txtClip.value });
};
function getClipResponse(j) {
if (txtClip.allowUpdate) {
txtClip.value = j.message.text;
txtClip.allowUpdate = false;
}
}
var lstPorts = document.getElementById('list-ports');
var portList = document.getElementById('port-list');
var fwdPort = document.getElementById('fwd-port');
var revPort = document.getElementById('rev-port');
var portNum = document.getElementById('port-num');
lstPorts.onclick = function () {
wsJson({ event: 'listports' });
};
function getPortsResponse(j) {
portList.innerHTML = j.data;
}
fwdPort.onclick = function () {
wsJson({ event: 'addport', rev: false, port: portNum.value });
};
revPort.onclick = function () {
wsJson({ event: 'addport', rev: true, port: portNum.value });
};
minicap.onclick = function () {
wsJson({ event: 'minicap' });
};
</script>
</body>
</html>