From 357a7c256ff74bb145fa495a1dbd974839bddd61 Mon Sep 17 00:00:00 2001
From: Louis Capitanchik <contact@louiscap.co>
Date: Sun, 21 Aug 2022 14:33:51 +0100
Subject: [PATCH] Add build configs for Picade

---
 .gitignore                       |   7 ++-
 Makefile                         |  11 ++++
 build/cabinet/asound.conf        |  41 ++++++++++++
 build/cabinet/autoboot.service   |  14 +++++
 build/cabinet/builder.dockerfile |  21 +++++++
 build/cabinet/cabinetos.json     | 105 +++++++++++++++++++++++++++++++
 build/cabinet/config.txt         |  54 ++++++++++++++++
 build/cabinet/game_core.desktop  |   0
 build/cabinet/picade.udev.rules  |   1 +
 build/cabinet/userconf.txt       |   1 +
 10 files changed, 254 insertions(+), 1 deletion(-)
 create mode 100644 build/cabinet/asound.conf
 create mode 100644 build/cabinet/autoboot.service
 create mode 100644 build/cabinet/builder.dockerfile
 create mode 100644 build/cabinet/cabinetos.json
 create mode 100644 build/cabinet/config.txt
 create mode 100644 build/cabinet/game_core.desktop
 create mode 100644 build/cabinet/picade.udev.rules
 create mode 100644 build/cabinet/userconf.txt

diff --git a/.gitignore b/.gitignore
index 3ad33f1..7432d45 100644
--- a/.gitignore
+++ b/.gitignore
@@ -7,4 +7,9 @@
 
 .idea/
 .vscode/
-dist/
\ No newline at end of file
+dist/
+
+*.img
+.packer_cache
+*.dtbo
+wpa_supplicant.conf
\ No newline at end of file
diff --git a/Makefile b/Makefile
index 2dc4e11..7750f0b 100644
--- a/Makefile
+++ b/Makefile
@@ -53,6 +53,17 @@ build-web: top_tail
 clean_dist:
 	rm -rf ./dist
 
+cabinet-builder:
+	docker build -t mhrf/cabinet-builder -f build/cabinet/builder.dockerfile build/cabinet
+
+cabinet:
+	docker run --rm --privileged \
+		-v "$(CURRENT_DIRECTORY)/.packer_cache":/root/.cache/packer \
+		-v /dev:/dev \
+		-v $(CURRENT_DIRECTORY):/build \
+		-e DTBO_DEST=/build/build/cabinet \
+		mhrf/cabinet-builder build build/cabinet/cabinetos.json
+
 top_tail:
 	@echo "================================================"
 	@echo "               Building ${PROJECT_NAME}"
diff --git a/build/cabinet/asound.conf b/build/cabinet/asound.conf
new file mode 100644
index 0000000..da2e867
--- /dev/null
+++ b/build/cabinet/asound.conf
@@ -0,0 +1,41 @@
+pcm.real {
+  type hw
+  card 0
+  device 0
+}
+
+pcm.dmixer {
+  type dmix
+  ipc_key 1024
+  ipc_perm 0666
+  slave.pcm "real"
+  slave {
+    period_time 0
+    period_size 1024
+    buffer_size 8192
+    rate 44100
+  }
+  bindings {
+    0 0
+    1 1
+  }
+}
+
+ctl.dmixer {
+  type hw
+  card 0
+}
+
+pcm.softvol {
+  type softvol
+  slave.pcm "dmixer"
+  control {
+    name "PCM" # Masquerade as the default "PCM" sound device on Pi
+    card 0
+   }
+}
+
+pcm.!default {
+   type plug
+   slave.pcm "softvol"
+}
\ No newline at end of file
diff --git a/build/cabinet/autoboot.service b/build/cabinet/autoboot.service
new file mode 100644
index 0000000..640ad92
--- /dev/null
+++ b/build/cabinet/autoboot.service
@@ -0,0 +1,14 @@
+[Unit]
+Description=Autostart Realm Fantastique On Boot
+
+[Service]
+Environment=DISPLAY=:0
+ExecStart=/var/game/game_core
+WorkingDirectory=/var/game/game_core
+Restart=always
+RestartSec=10s
+KillMode=process
+TimeoutSec=infinity
+
+[Install]
+WantedBy=graphical.target
\ No newline at end of file
diff --git a/build/cabinet/builder.dockerfile b/build/cabinet/builder.dockerfile
new file mode 100644
index 0000000..55237e9
--- /dev/null
+++ b/build/cabinet/builder.dockerfile
@@ -0,0 +1,21 @@
+FROM mkaczanowski/packer-builder-arm
+
+RUN apt-get -yqq update && \
+    apt-get install -yqq git device-tree-compiler build-essential make && \
+    rm -rf /var/lib/apt/lists/*
+
+WORKDIR /deps
+
+RUN git clone --depth=1 https://github.com/pimoroni/picade-hat.git . && \
+    make
+
+WORKDIR /build
+
+# ENV DTBO_DEST "/build"
+RUN touch "/shim.sh" && \
+    chmod u+x "/shim.sh" && \
+    echo '#!/usr/bin/env sh' >> "/shim.sh" && \
+    echo 'cp /deps/picade.dtbo $DTBO_DEST/picade.dtbo' >> "/shim.sh" && \
+    echo 'bash /entrypoint.sh $@' >> "/shim.sh"
+
+ENTRYPOINT ["/shim.sh"]
\ No newline at end of file
diff --git a/build/cabinet/cabinetos.json b/build/cabinet/cabinetos.json
new file mode 100644
index 0000000..c321505
--- /dev/null
+++ b/build/cabinet/cabinetos.json
@@ -0,0 +1,105 @@
+{
+  "variables": {},
+  "builders": [{
+	"type": "arm",
+	"file_urls" : ["https://downloads.raspberrypi.org/raspios_arm64/images/raspios_arm64-2022-04-07/2022-04-04-raspios-bullseye-arm64.img.xz"],
+	"file_checksum_url": "https://downloads.raspberrypi.org/raspios_arm64/images/raspios_arm64-2022-04-07/2022-04-04-raspios-bullseye-arm64.img.xz.sha256",
+	"file_checksum_type": "sha256",
+	"file_target_extension": "xz",
+	"file_unarchive_cmd": ["xz", "-d", "$ARCHIVE_PATH"],
+	"image_build_method": "resize",
+	"image_path": "cabinetos.img",
+	"image_size": "4G",
+	"image_type": "dos",
+	"image_partitions": [
+	  {
+		"name": "boot",
+		"type": "c",
+		"start_sector": "8192",
+		"filesystem": "vfat",
+		"size": "256M",
+		"mountpoint": "/boot"
+	  },
+	  {
+		"name": "root",
+		"type": "83",
+		"start_sector": "532480",
+		"filesystem": "ext4",
+		"size": "0",
+		"mountpoint": "/"
+	  }
+	],
+	"image_chroot_env": ["PATH=/usr/local/bin:/usr/local/sbin:/usr/bin:/usr/sbin:/bin:/sbin"],
+	"qemu_binary_source_path": "/usr/bin/qemu-arm-static",
+	"qemu_binary_destination_path": "/usr/bin/qemu-arm-static"
+  }],
+  "provisioners": [
+	{
+	  "type": "shell",
+	  "inline": [
+		"touch /boot/ssh"
+	  ]
+	},
+	{
+	  "type": "shell",
+	  "inline": [
+		"apt-get -qq update",
+		"apt-get -y upgrade",
+		"apt-get install -y mesa-vulkan-drivers libvulkan-dev libvulkan1 vulkan-tools"
+	  ]
+	},
+	{
+	  "type": "file",
+	  "source": "build/cabinet/userconf.txt",
+	  "destination": "/boot/userconf.txt"
+	},
+	{
+	  "type": "file",
+	  "source": "build/cabinet/config.txt",
+	  "destination": "/boot/config.txt"
+	},
+	{
+	  "type": "file",
+	  "source": "build/cabinet/picade.dtbo",
+	  "destination": "/boot/overlays/picade.dtbo"
+	},
+	{
+	  "type": "file",
+	  "source": "build/cabinet/asound.conf",
+	  "destination": "/etc/asound.conf"
+	},
+	{
+	  "type": "file",
+	  "source": "build/cabinet/picade.udev.rules",
+	  "destination": "/etc/udev/rules.d/10-picade.rules"
+	},
+	{
+	  "type": "file",
+	  "source": "build/cabinet/autoboot.service",
+	  "destination": "/etc/systemd/system/game_core.service"
+	},
+	{
+	  "type": "file",
+	  "source": "build/cabinet/wpa_supplicant.conf",
+	  "destination": "/etc/wpa_supplicant.conf"
+	},
+	{
+	  "type": "shell",
+	  "inline": [
+		"cd /var",
+		"wget -O game.zip https://lab.lcr.gr/microhacks/fantastqiue/-/jobs/artifacts/trunk/raw/dist/linux.arm64.zip?job=package-all",
+		"mkdir game",
+		"unzip game.zip -d game",
+		"mv game/game_core.arm64 game/game_core"
+	  ]
+	},
+	{
+	  "type": "shell",
+	  "inline": [
+		"rm /etc/xdg/autostart/piwiz.desktop",
+		"rm /etc/xdg/autostart/pprompt.desktop"
+	  ]
+	}
+
+  ]
+}
diff --git a/build/cabinet/config.txt b/build/cabinet/config.txt
new file mode 100644
index 0000000..b79d245
--- /dev/null
+++ b/build/cabinet/config.txt
@@ -0,0 +1,54 @@
+hdmi_force_hotplug=1
+
+# Additional overlays and parameters are documented /boot/overlays/README
+
+# Configure cabinet buttons
+dtoverlay=picade
+dtparam=up=17    #  W
+dtparam=down=31  #  S
+dtparam=left=30  #  A
+dtparam=right=32 #  D
+dtparam=button1=22  #  U
+dtparam=button2=23  #  I
+dtparam=button3=24  #  O
+dtparam=button4=36  #  J
+dtparam=button5=37  #  K
+dtparam=button6=38  #  L
+dtparam=enter=28  #  Enter Key
+dtparam=escape=1  #  Escape Key
+dtparam=coin=42   #  Left Shift
+dtparam=start=29  #  Left Control
+
+
+# Enable audio (loads snd_bcm2835)
+dtparam=audio=on
+
+# Automatically load overlays for detected cameras
+camera_auto_detect=1
+
+# Automatically load overlays for detected DSI displays
+display_auto_detect=1
+
+# Enable DRM VC4 V3D driver
+dtoverlay=vc4-kms-v3d
+max_framebuffers=2
+
+# Run in 64-bit mode
+arm_64bit=1
+
+# Disable compensation for displays with overscan
+disable_overscan=1
+
+[cm4]
+# Enable host mode on the 2711 built-in XHCI USB controller.
+# This line should be removed if the legacy DWC2 controller is required
+# (e.g. for USB device mode) or if USB support is not required.
+otg_mode=1
+
+[all]
+
+[pi4]
+# Run as fast as firmware / board allows
+arm_boost=1
+
+[all]
\ No newline at end of file
diff --git a/build/cabinet/game_core.desktop b/build/cabinet/game_core.desktop
new file mode 100644
index 0000000..e69de29
diff --git a/build/cabinet/picade.udev.rules b/build/cabinet/picade.udev.rules
new file mode 100644
index 0000000..818dd1b
--- /dev/null
+++ b/build/cabinet/picade.udev.rules
@@ -0,0 +1 @@
+SUBSYSTEM=="input", ATTRS{name}=="gpio_keys", ENV{ID_INPUT_KEYBOARD}="1"
\ No newline at end of file
diff --git a/build/cabinet/userconf.txt b/build/cabinet/userconf.txt
new file mode 100644
index 0000000..5e30f1c
--- /dev/null
+++ b/build/cabinet/userconf.txt
@@ -0,0 +1 @@
+pi:$6$bevy$gFy/K.UcTH6QpahcMQMOCfBuKUnX2lSwVqYg9CSkCDtWBmmcKt4BaFESMzJFTStGfjrCxeU5sgWfxt5KRfACZ/
\ No newline at end of file
-- 
GitLab