git clone https://garage44.eu/jeroen/pinnacle.git.. /xtask/start.sh
#!/bin/sh
# Starts Pinnacle in QEMU, in a window of its own: the launcher of the
# folder `cargo xtask bundle` makes (ARM, `Image`) or `cargo xtask bundle
# pc` (x86-64, `kernel`). Needs only QEMU (`brew install qemu` on a Mac; on
# Linux qemu-system-aarch64 or qemu-system-x86 and their GUI and audio
# modules). Where the host's processor is the system's, it runs on the
# processor itself: Hypervisor.framework on an Apple silicon Mac with ARM's,
# KVM on Linux (an x86-64 laptop with the PC's); elsewhere QEMU emulates it.
#
# The disks are kept in this folder between runs: system.img, and data.img,
# the person's data, made blank at the first start (the system asks for a
# password then). Delete data.img to start over.
#
# ./start a phone held upright, 720x1440; the PC
# a laptop's screen, 1280x800
# PINNACLE_SCREEN=1280x800 ./start any size to start at; the system takes
# the window's size when it is resized
# PINNACLE_ACCEL=tcg ./start emulate, even where it could run natively
# PINNACLE_AUDIO=wav ./start sound to sound.wav, not the speakers
#
# Sound plays through the host's speakers and microphone: Core Audio on a
# Mac; on Linux PipeWire, PulseAudio or ALSA, whichever is there; none on a
# machine without any (a server). PINNACLE_AUDIO names a QEMU audio driver.
#
# The Store's package source is the `store` folder, served to the guest at
# http://10.0.2.2:8080/ by this same script, which QEMU starts once per
# connection (`./start serve`): nothing listens on the host.
set -eu
here=$(cd "$(dirname "$0")" && pwd)
if [ "${1:-}" = serve ]; then
# One HTTP/1.0 request on stdin, its answer on stdout.
read -r _ path _ || exit 0
while read -r line; do
[ -z "$(printf '%s' "$line" | tr -d '\r')" ] && break
done
path=${path#/}
case $path in *..*) path= ;; esac
file=$here/store/$path
if [ -n "$path" ] && [ -f "$file" ]; then
printf 'HTTP/1.0 200 OK\r\nContent-Length: %s\r\n\r\n' "$(wc -c <"$file" | tr -d ' ')"
cat "$file"
else
printf 'HTTP/1.0 404 Not Found\r\nContent-Length: 0\r\n\r\n'
fi
exit 0
fi
# Which system this folder holds: a PC's kernel is an ELF named `kernel`.
if [ -f "$here/kernel" ]; then
pc=yes
qemu=qemu-system-x86_64
package=qemu-system-x86
else
pc=
qemu=qemu-system-aarch64
package=qemu-system-aarch64
fi
if ! command -v "$qemu" >/dev/null; then
echo "Pinnacle needs QEMU: brew install qemu (on Linux: the $package package)" >&2
exit 1
fi
if [ -n "$pc" ]; then
screen=${PINNACLE_SCREEN:-1280x800}
else
screen=${PINNACLE_SCREEN:-720x1440}
fi
width=${screen%x*}
height=${screen#*x}
# Native where the host's processor is the system's, with a hypervisor
# QEMU can use.
accel=${PINNACLE_ACCEL:-}
if [ -z "$accel" ]; then
case "$(uname -s)/$(uname -m)/$pc" in
Darwin/arm64/) accel=hvf ;;
Linux/aarch64/ | Linux/x86_64/yes) [ -w /dev/kvm ] && accel=kvm || accel=tcg ;;
*) accel=tcg ;;
esac
fi
case $accel/$pc in
hvf/* | kvm/*) cpu=host ;;
*/yes) cpu=max ;;
*) cpu=cortex-a72 ;;
esac
# Pinnacle draws no pointer of its own (it is made for touch), and QEMU hides
# the host's over the window unless told not to.
case "$(uname -s)" in
Darwin)
display=${PINNACLE_DISPLAY:-cocoa,zoom-to-fit=on,show-cursor=on}
audio=${PINNACLE_AUDIO:-coreaudio}
;;
*)
display=${PINNACLE_DISPLAY:-gtk,zoom-to-fit=on,show-cursor=on}
audio=${PINNACLE_AUDIO:-}
;;
esac
# How many of the monitor's pixels to each of the window's: QEMU's window
# reports its size in the desktop's logical pixels, and on a HiDPI desktop
# the system takes a screen this many times larger, drawn one to one. KDE's
# outputs say their scale; elsewhere `PINNACLE_SCALE=2 ./start`.
scale=${PINNACLE_SCALE:-}
if [ -z "$scale" ] && command -v kscreen-doctor >/dev/null; then
# The largest scale of the enabled outputs, rounded up.
scale=$(kscreen-doctor -o 2>/dev/null | sed 's/\x1b\[[0-9;]*m//g' |
awk '/^Output:/ { on = ($0 !~ /disabled/) } /Scale:/ && on { s = $2 + 0; if (s > max) max = s }
END { m = int(max); if (max > m) m++; print (m >= 1 ? m : 1) }')
fi
case "$scale" in [1-4]) ;; *) scale=1 ;; esac
# On Linux, the sound server the desktop runs, if this QEMU has a driver for
# it (distributions ship them as separate modules), else the sound card
# itself, else none: a server has neither, and the system runs silent there.
if [ -z "$audio" ]; then
drivers=$("$qemu" -audiodev help 2>/dev/null || true)
has() { printf '%s\n' "$drivers" | grep -qx "$1"; }
run=${XDG_RUNTIME_DIR:-/run/user/$(id -u)}
if [ -S "$run/pipewire-0" ] && has pipewire; then
audio=pipewire
elif [ -S "$run/pulse/native" ] && has pa; then
audio=pa
elif ls /dev/snd/pcmC* >/dev/null 2>&1 && has alsa; then
audio=alsa
else
audio=none
fi
fi
# `PINNACLE_AUDIO=wav` writes what the system plays to sound.wav here.
[ "$audio" = wav ] && audio="wav,path=$(printf '%s' "$here/sound.wav" | sed 's/,/,,/g')"
if [ ! -f "$here/data.img" ]; then
dd if=/dev/zero of="$here/data.img" bs=1048576 count=0 seek=32 2>/dev/null
fi
# QEMU's option parser takes ",," for a comma within a value.
serve_cmd=$(printf "/bin/sh '%s/start' serve" "$here" | sed 's/,/,,/g')
system=$(printf '%s' "$here/system.img" | sed 's/,/,,/g')
data=$(printf '%s' "$here/data.img" | sed 's/,/,,/g')
# The machine, and the bus its devices are on: ARM's virt with virtio over
# MMIO, or a PC (q35) with every device on PCI and the virtio-gpu its only
# screen. A PC runs on no more cores than the host has: a waiting core
# spins, and spinning on a core the host has not got stalls the others.
if [ -n "$pc" ]; then
cores=$(nproc 2>/dev/null || sysctl -n hw.ncpu 2>/dev/null || echo 4)
[ "$cores" -gt 6 ] && cores=6
set -- -M q35 -vga none -device isa-debug-exit,iobase=0xf4,iosize=0x04
bus=pci
kernel=$here/kernel
else
cores=6
set -- -M virt,gic-version=3 -semihosting-config enable=on,target=native \
-global virtio-mmio.force-legacy=false
bus=device
kernel=$here/Image
fi
echo "Pinnacle: ${width}x${height} at scale $scale, $accel, sound ${audio%%,*}; F12 turns the screen. The kernel's log follows." >&2
# The same machine `cargo xtask run` (ARM) or `cargo xtask pc` boots
# (xtask/src/main.rs, qemu_guest and qemu_pc), with a window and sound
# instead of none.
# shellcheck disable=SC2086
exec "$qemu" "$@" \
-fw_cfg "name=opt/pinnacle/screen,string=${width}x${height}" \
-fw_cfg "name=opt/pinnacle/scale,string=$scale" \
-accel "$accel" -cpu "$cpu" -m 256M -smp "$cores" \
-nic none -serial stdio -display "$display" \
-device "virtio-gpu-$bus,xres=$width,yres=$height" \
-device "virtio-tablet-$bus" -device "virtio-keyboard-$bus" \
-netdev "user,id=net0,guestfwd=tcp:10.0.2.2:8080-cmd:$serve_cmd" \
-device "virtio-net-$bus,netdev=net0" \
-device "virtio-rng-$bus" \
-audiodev "$audio,id=snd0" -device "virtio-sound-$bus,audiodev=snd0" \
-drive "if=none,id=system,format=raw,file=$system" -device "virtio-blk-$bus,drive=system" \
-drive "if=none,id=data,format=raw,file=$data" -device "virtio-blk-$bus,drive=data" \
-kernel "$kernel" -append "" \
${PINNACLE_QEMU_ARGS:-}