Relay browser touches through the harness into the real touchscreen
This commit is contained in:
+33
-1
@@ -22,7 +22,7 @@ const page = `<!doctype html>
|
||||
<meta name="viewport" content="width=device-width">
|
||||
<title>cabletest</title>
|
||||
<body style="margin:0;background:#000;min-height:100vh;display:grid;place-items:center">
|
||||
<canvas style="max-width:100vw;max-height:100vh"></canvas>
|
||||
<canvas style="max-width:100vw;max-height:100vh;touch-action:none"></canvas>
|
||||
<script>
|
||||
(async () => {
|
||||
const cv = document.querySelector('canvas'), g = cv.getContext('2d');
|
||||
@@ -59,6 +59,34 @@ addEventListener('keydown', e => {
|
||||
fetch('keys', {method: 'POST', body: e.key});
|
||||
}
|
||||
});
|
||||
(() => {
|
||||
const cv = document.querySelector('canvas');
|
||||
let q = Promise.resolve(), held = -1, lastMove = 0;
|
||||
const send = b => q = q.then(() => fetch('touch', {method: 'POST', body: b}).catch(() => {}));
|
||||
const at = e => {
|
||||
const r = cv.getBoundingClientRect();
|
||||
return Math.round((e.clientX - r.left) * cv.width / r.width) + ' ' +
|
||||
Math.round((e.clientY - r.top) * cv.height / r.height);
|
||||
};
|
||||
cv.addEventListener('pointerdown', e => {
|
||||
e.preventDefault();
|
||||
held = e.pointerId;
|
||||
cv.setPointerCapture(held);
|
||||
send('down ' + at(e));
|
||||
});
|
||||
cv.addEventListener('pointermove', e => {
|
||||
if (e.pointerId !== held || e.timeStamp - lastMove < 50) return;
|
||||
lastMove = e.timeStamp;
|
||||
send('move ' + at(e));
|
||||
});
|
||||
const lift = e => {
|
||||
if (e.pointerId !== held) return;
|
||||
held = -1;
|
||||
send('up');
|
||||
};
|
||||
cv.addEventListener('pointerup', lift);
|
||||
cv.addEventListener('pointercancel', lift);
|
||||
})();
|
||||
</script>
|
||||
`
|
||||
|
||||
@@ -87,6 +115,9 @@ func serveStream() error {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if touchscreen, err = openTouchscreen(); err != nil {
|
||||
return err
|
||||
}
|
||||
ln, err := net.Listen("tcp", streamAddr)
|
||||
if err != nil {
|
||||
return err
|
||||
@@ -104,6 +135,7 @@ func serveStream() error {
|
||||
})
|
||||
mux.HandleFunc("/frames", frames)
|
||||
mux.HandleFunc("/keys", keys)
|
||||
mux.HandleFunc("/touch", touch)
|
||||
go http.Serve(ln, mux)
|
||||
return nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user