Display, Input and Sound

A framebuffer, evdev input and ALSA sound for guest programs.

import { createKernel, domCodeToLinux, pcmToFloat32 } from "unkernel";

const vm = await createKernel({
  display: { width: 800, height: 600 }, // the guest gets /dev/fb0
  onDisplay: (mode) => console.log(mode), // a program opened it (its mode) or closed it (null)
  sound: { onChunk: ({ data, format }) => play(pcmToFloat32(data, format)) },
});

const frame = vm.frame(); // RGBA, or null while nothing draws
if (frame) ctx.putImageData(new ImageData(frame.data, frame.width, frame.height), 0, 0);

addEventListener("keydown", (e) => {
  const key = domCodeToLinux(e.code);
  if (key && vm.evdev.keyboardOpen) vm.evdev.key(key, true);
});

#Input

evdev also has button(), move(), moveTo() and wheel() for a mouse or tablet. onInput tells you when a program opens the keyboard and mouse; until then, send keys to the console. readFrame() waits for a fresh frame.

#Sound in browsers

createAudioOut() plays the guest's sound with Web Audio:

import { createAudioOut, createKernel } from "unkernel/web";

button.hidden = true;
const audio = createAudioOut({
  onChange: (state) => {
    button.hidden = state !== "waiting"; // "click to play"
  },
});
const vm = await createKernel({ sound: audio });
button.onclick = () => audio.resume(); // or any other click or key press on the page

Browsers start sound only after a click, tap or key press on the page. Until then the latest holdSeconds (10) of what the guest plays are held: a clip that has ended by then plays whole, a stream joins live. audio.muted = true silences it.

One sink serves one kernel at a time, and outlives it: pass it to the next kernel, or close() it.