-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: improved ui and power commands
- Loading branch information
Showing
14 changed files
with
321 additions
and
180 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import log from './log' | ||
import { | ||
getBreakBytes, | ||
getMakeBytes, | ||
} from 'keycodes-to-ps2-scan-codes' | ||
import { CommandFrame } from 'common' | ||
|
||
export function startControls() { | ||
log.info('Connecting to websocket...') | ||
const wsUrl = window.location.href.replace(/^http/, 'ws') | ||
const ws = new WebSocket(wsUrl) | ||
|
||
ws.addEventListener('error', (err) => { | ||
log.error('Error connecting to control websocket:', err) | ||
}) | ||
|
||
ws.addEventListener('close', () => { | ||
log.error('WebSocket closed. Commands can not be sent.') | ||
}) | ||
|
||
ws.addEventListener('open', () => { | ||
log.info('WebSocket connected.') | ||
|
||
const keyEventHandler = KeyEventHandler(ws) | ||
|
||
document.addEventListener('keydown', keyEventHandler) | ||
document.addEventListener('keyup', keyEventHandler) | ||
|
||
;['Power', 'WakeUp', 'Sleep'].map(code => { | ||
const el = document.querySelector(`button#${code.toLowerCase()}`) | ||
el.addEventListener('mousedown', () => document.dispatchEvent(new KeyboardEvent('keydown', { code }))) | ||
el.addEventListener('mouseup', () => document.dispatchEvent(new KeyboardEvent('keyup', { code }))) | ||
}) | ||
}) | ||
} | ||
|
||
function KeyEventHandler (ws: WebSocket) { | ||
return function handleKeyEvent (e: KeyboardEvent) { | ||
const getBytes = e.type === 'keyup' ? getBreakBytes : getMakeBytes | ||
let ps2Command | ||
|
||
try { | ||
ps2Command = getBytes(e.code) | ||
} catch (err) { | ||
log.warn('Error getting scancode for key', e.code, err) | ||
return | ||
} | ||
|
||
const frame: CommandFrame = { | ||
type: 'ps2-command', | ||
ps2Command | ||
} | ||
|
||
ws.send(JSON.stringify(frame)) | ||
|
||
e.preventDefault() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
$muted: #a9a9a9; | ||
$text: #d5d5d5; | ||
$page-bg: #222222; | ||
$accent: #3b5249; | ||
|
||
* { | ||
padding: 0; | ||
margin: 0; | ||
box-sizing: border-box; | ||
} | ||
|
||
a, a:visited { | ||
color: darken($text, 10); | ||
} | ||
|
||
body, html { | ||
width: 100%; | ||
height: 100%; | ||
margin: 0; | ||
padding: 0; | ||
} | ||
|
||
body { | ||
display: flex; | ||
flex-direction: row; | ||
// flex-wrap: wrap-reverse; | ||
background: $page-bg; | ||
color: $text; | ||
font-family: sans-serif; | ||
} | ||
|
||
#video-container { | ||
min-width: auto; | ||
min-height: auto; | ||
background-color: black; | ||
text-align: center; | ||
|
||
#video { | ||
margin: auto; | ||
max-width: 100%; | ||
max-height: 100%; | ||
} | ||
} | ||
|
||
#interface { | ||
display: flex; | ||
flex-grow: 1; | ||
flex-direction: column; | ||
min-width: 500px; | ||
min-height: 500px; | ||
padding: 0; | ||
margin: 0; | ||
|
||
overflow-y: auto; | ||
overflow-x: hidden; | ||
|
||
#logo { | ||
padding: 10px; | ||
font-size: 30px; | ||
|
||
#controls { | ||
float: right; | ||
|
||
button { | ||
background-color: $accent; | ||
color: $muted; | ||
border-radius: 5px; | ||
padding: 5px; | ||
border: 0; | ||
} | ||
|
||
button:hover { | ||
cursor: pointer; | ||
background-color: lighten($accent, 10); | ||
} | ||
|
||
button:active { | ||
background-color: darken($accent, 10); | ||
} | ||
} | ||
} | ||
|
||
#log { | ||
width: 100%; | ||
overflow-y: auto; | ||
overflow-x: hidden; | ||
|
||
li { | ||
padding: 2px 8px; | ||
width: 100%; | ||
list-style-type: none; | ||
|
||
.icon { | ||
width: 1em; | ||
display: inline-block; | ||
text-align: center; | ||
} | ||
|
||
time { | ||
opacity: .5; | ||
} | ||
|
||
&.warn > .icon { | ||
color: yellow; | ||
} | ||
|
||
&.error > .icon { | ||
color: red; | ||
} | ||
|
||
&.info > .icon { | ||
color: lighten($accent, 50) | ||
} | ||
} | ||
|
||
li:nth-child(even) { | ||
background-color: transparentize($color: $accent, $amount: .5); | ||
} | ||
|
||
li:nth-child(odd) { | ||
background-color: transparentize($color: $accent, $amount: .8); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,49 +1,11 @@ | ||
import { keysToScanCodes } from './keys-to-scan-codes' | ||
import { CommandFrame } from 'common' | ||
import { lockKeyboard } from './lock-keyboard' | ||
import { startVideo } from './video' | ||
import { startControls } from './controls' | ||
import { version, repository } from '../../package.json' | ||
import log from './log' | ||
|
||
const videoImg = document.querySelector('#video') | ||
log.info(`Welcome to Pilo v${version}! Please report any bugs on the <a href="${repository}">GitHub repo</a>.`) | ||
|
||
videoImg.addEventListener('error', (err) => { | ||
console.error('Error connecting to MJPEG stream:', err) | ||
}) | ||
|
||
videoImg.setAttribute('src', 'video.mjpeg') | ||
|
||
const wsUrl = window.location.href.replace(/^http/, 'ws') | ||
const ws = new WebSocket(wsUrl) | ||
|
||
|
||
const handleKeyEvent = (e) => { | ||
const scanCode = keysToScanCodes[e.code] | ||
|
||
if (!scanCode) { | ||
console.warn(`Unmapped keypress: ${e.code}`, { e, keysToScanCodes }) | ||
return | ||
} | ||
|
||
const ps2Command = [scanCode] | ||
|
||
if (e.type === 'keyup') { | ||
ps2Command.unshift(0xF0) // break keypress | ||
} | ||
|
||
const frame: CommandFrame = { | ||
type: 'ps2-command', | ||
ps2Command | ||
} | ||
|
||
ws.send(JSON.stringify(frame)) | ||
|
||
e.preventDefault() | ||
} | ||
|
||
document.addEventListener('keydown', handleKeyEvent) | ||
document.addEventListener('keyup', handleKeyEvent) | ||
|
||
ws.addEventListener('error', (err) => { | ||
console.error('Error connecting to control websocket:', err) | ||
}) | ||
|
||
ws.addEventListener('open', () => { | ||
console.log('WS connected.') | ||
}) | ||
lockKeyboard() | ||
startVideo() | ||
startControls() |
Oops, something went wrong.