forked from Informatic/webos-ssap-web
-
Notifications
You must be signed in to change notification settings - Fork 3
/
demo.js
520 lines (422 loc) · 13.9 KB
/
demo.js
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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
/*
Copyright 2022-2023 throwaway96 (https://github.com/throwaway96)
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
/* Copyright 2021 Piotr Dobrowolski <informatic at hackerspace dot pl> */
function wait(ms) {
return new Promise((resolve, reject) => {
setTimeout(() => resolve(), ms);
});
}
function log(msg) {
const l = document.querySelector('#log');
l.innerText = '[' + new Date() + '] ' + msg + '\n' + l.innerText;
}
let client = null;
let lastImageUri = null;
let inputSocket = null;
document.addEventListener('keydown', (evt) => {
const keys = {
'Enter': 'ENTER',
'ArrowLeft': 'LEFT',
'ArrowRight': 'RIGHT',
'ArrowUp': 'UP',
'ArrowDown': 'DOWN',
'Escape': 'BACK',
'Home': 'HOME',
'0': '0',
'1': '1',
'2': '2',
'3': '3',
'4': '4',
'5': '5',
'6': '6',
'7': '7',
'8': '8',
'9': '9',
'Delete': 'EXIT',
'ContextMenu': 'MENU',
'e': 'EZ_ADJUST',
'h': 'MYAPPS',
'i': 'IN_START',
'm': 'MUTE',
};
if (evt.key in keys) {
/* XXX: is there a better way to do this? */
/* stop scrolling */
if ((evt.key === 'ArrowDown') || (evt.key === 'ArrowUp')) {
evt.preventDefault();
}
sendKey(keys[evt.key]);
} else {
console.log("unrecognized key: '" + evt.key + "'");
}
});
function sendKey(keyName) {
if (!isInputSocketOpen()) {
console.log("would send key: " + keyName);
return;
}
console.log("sending key: " + keyName);
inputSocket.send(Object.entries({type: 'button', name: keyName}).map(([k, v]) => `${k}:${v}`).join('\n') + '\n\n');
}
/* prevent menu key from opening context menu */
document.addEventListener('keyup', (evt) => {
if (evt.key == 'ContextMenu') {
evt.preventDefault();
}
});
const keypadButtons = document.querySelectorAll('button.keypad');
const keypadClickHandler = (evt) => {
if ((typeof(evt) !== 'object') || !(evt instanceof Event)) {
console.error('keypadHandler: weird event (evt not Event object): ' + evt);
return;
}
const target = evt.target;
if ((typeof(target) !== 'object') || !(target instanceof HTMLElement)) {
console.error('keypadHandler: weird event (evt.target not HTMLElement object): ' + evt);
return;
}
let dataset = target.dataset;
if (!('key' in dataset)) {
/* hack for <span>s on remote */
dataset = target.parentElement.dataset;
if (!('key' in dataset)) {
console.error('keypadHandler: key not set: ' + evt);
return;
}
}
sendKey(dataset.key);
};
for (let button of keypadButtons) {
button.addEventListener('click', keypadClickHandler);
}
const reqDiv = document.getElementById('request');
/* stop keydown events in request form from bubbling and being sent via ssap */
reqDiv.addEventListener('keydown', (evt) => {
evt.stopPropagation();
});
/* "pause preview" checkbox */
const pausePreview = document.getElementById('pause_preview');
/* controls whether preview image should be updated */
let previewEnabled = false;
/* event fired when previewEnabled goes from false to true */
const previewEnabledEvent = new Event('previewEnabled');
/*
* check the the pausePreview checkbox and dispatch event if it the state has
* changed to enabled
*/
function updatePreviewEnabled() {
const oldPreviewEnabled = previewEnabled;
previewEnabled = !pausePreview.checked;
/* only fire on change from disabled to enabled */
if (previewEnabled && !oldPreviewEnabled) {
pausePreview.dispatchEvent(previewEnabledEvent);
}
}
pausePreview.addEventListener('change', (evt) => updatePreviewEnabled());
updatePreviewEnabled();
async function waitForPreviewEnable() {
console.log(`waitForPreviewEnable starting (${previewEnabled})`);
while (!previewEnabled) {
await new Promise((resolve) => {
const listener = () => {
pausePreview.removeEventListener('previewEnabled', listener);
console.log(`waitForPreviewEnable listener (${previewEnabled})`);
resolve();
}
pausePreview.addEventListener('previewEnabled', listener);
});
}
console.log(`waitForPreviewEnable returning (${previewEnabled})`);
}
const reqList = document.getElementById('reqs');
const reqSend = document.getElementById('req_send');
const reqUri = document.getElementById('req_uri');
const reqPrivate = document.getElementById('req_private');
const reqPayload = document.getElementById('req_payload');
const respStatus = document.getElementById('resp_status');
const respPayload = document.getElementById('resp_payload');
function reqListHandler(evt) {
const sel = reqList.options[reqList.selectedIndex];
let disableEdit = true;
if (sel.value === 'custom') {
disableEdit = false;
} else {
reqUri.value = sel.dataset['endpoint'];
reqPayload.value = sel.dataset['payload'];
reqPrivate.checked = (sel.dataset['private'] !== undefined);
}
reqUri.disabled = disableEdit;
reqPayload.disabled = disableEdit;
reqPrivate.disabled = disableEdit;
}
reqList.addEventListener('change', reqListHandler);
reqListHandler();
function setResponseStatus(message) {
if (typeof message !== 'string') {
throw new Error(`setResponseStatus: message must be a string (got ${typeof message})`);
}
respStatus.value = message;
}
function sendRequest(uri, payload) {
if (!isSSAPConnected()) {
console.warn('syncRequest: SSAP not connected');
return;
}
setResponseStatus('sending...');
client.request({
uri: uri,
payload: payload,
}).then((res) => {
let statusMsg = '???';
if (res.type === 'error') {
statusMsg = 'error: ' + res.error;
} else if (res.type === 'response') {
statusMsg = 'response';
} else {
const msg = `unknown response type: ${res.type}`;
setResponseStatus(msg);
throw new Error(`sendRequest: ${msg}`);
}
setResponseStatus(statusMsg);
respPayload.value = JSON.stringify(res.payload);
}).catch((error) => {
throw new Error(`sendRequest: error: ${error}`);
});
}
async function sendPrivateRequest(uri, payload, manual = false) {
if (!isSSAPConnected()) {
console.warn('syncRequest: SSAP not connected');
return;
}
const createReq = {
"message": "hi lol",
"type": "confirm",
"modal": false,
"isSysReq": true,
"onclose": {
"uri": uri,
"params": payload,
},
"onfail": {
"uri": uri,
"params": payload,
},
"buttons": [
{
"label": "hax",
"focus": true,
"buttonType": "ok",
"onClick": uri,
"params": payload,
},
],
};
setResponseStatus('sending createAlert...');
const alertId = await client.request({
uri: 'ssap://system.notifications/createAlert',
payload: createReq,
}).then((res) => {
let statusMsg = 'createAlert ???';
if (res.type === 'error') {
const msg = `createAlert error: ${res.error}`;
setResponseStatus(msg);
respPayload.value = JSON.stringify(res.payload)
throw new Error(`sendPrivateRequest: ${msg}`);
} else if (res.type === 'response') {
statusMsg = 'createAlert response';
} else {
const msg = `createAlert unknown response type: ${res.type}`;
setResponseStatus(msg);
respPayload.value = JSON.stringify(res.payload)
throw new Error(`sendPrivateRequest: ${msg}`);
}
setResponseStatus(statusMsg);
const payload = res.payload;
/* returnValue should always be true, but double check anyway */
if (typeof payload.returnValue !== 'boolean') {
respPayload.value = JSON.stringify(res.payload);
throw new Error('createAlert: returnValue missing/wrong type');
} else if (!payload.returnValue) {
respPayload.value = JSON.stringify(res.payload);
throw new Error('createAlert: returnValue false');
}
const alertId = payload.alertId;
if (typeof alertId !== 'string') {
throw new Error('createAlert: alertId missing/wrong type');
}
return alertId;
}).catch((error) => {
throw new Error('sendPrivateRequest: createAlert error: ' + error);
});
const closeReq = {
"alertId": alertId
};
setResponseStatus('sending closeAlert...');
client.request({
uri: 'ssap://system.notifications/closeAlert',
payload: closeReq,
}).then((res) => {
let statusMsg = 'closeAlert ???';
if (res.type === 'error') {
statusMsg = 'closeAlert error: ' + res.error;
} else if (res.type === 'response') {
statusMsg = 'closeAlert response';
} else {
const msg = `closeAlert unknown response type: ${res.type}`;
setResponseStatus(msg);
throw new Error(`sendPrivateRequest: ${msg}`);
}
setResponseStatus(statusMsg);
respPayload.value = JSON.stringify(res.payload);
}).catch((error) => {
throw new Error('sendPrivateRequest: closeAlert error: ' + error);
});
}
reqSend.addEventListener('click', (evt) => {
const uri = reqUri.value;
const payload = JSON.parse(reqPayload.value);
if (reqPrivate.checked) {
sendPrivateRequest(uri, payload);
} else {
sendRequest(uri, payload);
}
});
const ipInput = document.getElementById("ip");
const connectButton = document.getElementById("connect");
const discButton = document.getElementById("disconnect");
/* connect on enter in ip input */
ipInput.addEventListener('keypress', (evt) => {
if ((typeof(evt) === 'object') && ( evt instanceof KeyboardEvent) && (evt.key === 'Enter')) {
connectButton.click();
}
});
/* TODO: move this into SSAPClient */
let ssapConnected = false;
function isSSAPConnected() {
return (client !== null) && ssapConnected;
}
function onSSAPConnect() {
log('connected');
console.log('SSAP connected');
ssapConnected = true;
setControlState(true);
}
function onSSAPDisconnect() {
ssapConnected = false;
setControlState(false);
log('disconnected');
console.log('SSAP disconnected');
}
function setControlState(connected) {
reqSend.disabled = !connected;
connectButton.disabled = connected;
discButton.disabled = !connected;
}
function isInputSocketOpen() {
return (inputSocket instanceof WebSocket) && (inputSocket.readyState === 1);
}
function onInputSocketOpen() {
log('input open');
console.log('input socket opened');
}
function onInputSocketClose() {
log('input closed');
console.log('input socket closed');
}
createRemote(document.getElementById('remote'), keypadClickHandler);
discButton.addEventListener('click', async () => {
if (client === null) {
console.warn("tried to disconnect while client is null");
return;
}
onSSAPDisconnect();
client.close();
client = null;
});
connectButton.addEventListener('click', async () => {
if (client !== null) {
console.warn("tried to connect while client is not null");
return;
}
connectButton.disabled = true;
const target = document.querySelector('#ip').value;
if ((typeof target !== 'string') || (target.length < 1)) {
alert("target empty");
return;
}
const useTLS = document.querySelector('#ssl').checked;
try {
client = new SSAPClient(target, window.localStorage['client-key-' + target], useTLS);
log('connecting...');
await client.connect();
log('registering...');
let manifest = defaultAppManifest;
try {
manifest = JSON.parse(window.localStorage['ssap-app-manifest']);
log("using custom manifest");
} catch (err) {}
window.localStorage['client-key-' + target] = await client.register(manifest);
onSSAPConnect();
if (document.querySelector(':focus')) document.querySelector(':focus').blur();
(async () => {
const sock = await client.request({
uri: 'ssap://com.webos.service.networkinput/getPointerInputSocket'
});
inputSocket = new WebSocket(sock.payload.socketPath);
inputSocket.onopen = onInputSocketOpen;
inputSocket.onerror = (err) => log('input err ' + err.msg);
inputSocket.onclose = onInputSocketClose;
})();
while (true) {
if (client === null) {
/* disconnected */
break;
}
await waitForPreviewEnable();
const res = await client.request({
uri: 'ssap://tv/executeOneShot',
payload: {},
});
lastImageUri = res.payload.imageUri;
const oldb = document.querySelector('.capture .back');
if (oldb) {
oldb.remove();
}
const oldf = document.querySelector('.capture .front');
if (oldf) {
oldf.classList.remove('front');
oldf.classList.add('back');
}
// This is bad. We do some """double-buffering""" of dynamically-generated
// SVG objects in order to bust Chrome's cache for preview image. `imageUri`
// here is static for all responses, and doesn't accept any extra query
// arguments. This hack seems to solve it on Chrome. (Firefox seems to
// update the <img> if hash-part of an URL changes)
const newf = document.createElement('object');
newf.classList.add('front');
newf.setAttribute('type', 'image/svg+xml');
newf.setAttribute('data', 'data:image/svg+xml;base64,' + btoa(`<svg preserveAspectRatio="xMinYMin meet" viewBox="0 0 1920 1080" width="1920" height="1080" xmlns="http://www.w3.org/2000/svg"><image width="1920" height="1080" href="${res.payload.imageUri}#${Date.now()}" /></svg>`)); // res.payload.imageUri + '#' + Date.now());
document.querySelector('.capture').appendChild(newf);
await wait(100);
}
} catch (err) {
log('error: ' + err); console.info(err);
onSSAPDisconnect();
client.close();
client = null;
}
});
function getLastImageUri() {
return lastImageUri;
}