Subject: [PATCH] community/kodi: allow running kodi-gbm at boot as it's own user

From: Bart Ribbers <[email-masked]>
Date: Tue, 20 Aug 2019 15:07:06 +0200

If kodi-gbm gets installed, it creates the "kodi" system user.
Then using the service file Kodi can be started on boot to run in KMS
mode (kodi-gbm) as the kodi user. In combination with a polkit rule in
kodi-polkit the user can then shut down the system from within Kodi.

With this setup Alpine Linux with Kodi becomes viable for HTPC devices
and smart TV's.
---
 community/kodi/60-kodi.rules        | 12 ++++++++
 community/kodi/APKBUILD             | 21 +++++++++++--
 community/kodi/kodi-gbm.pre-install | 10 +++++++
 community/kodi/kodi.initd           | 12 ++++++++
 community/kodi/no-snesapu.patch     | 46 -----------------------------
 5 files changed, 52 insertions(+), 49 deletions(-)
 create mode 100644 community/kodi/60-kodi.rules
 create mode 100644 community/kodi/kodi-gbm.pre-install
 create mode 100644 community/kodi/kodi.initd
 delete mode 100644 community/kodi/no-snesapu.patch

diff --git a/community/kodi/60-kodi.rules b/community/kodi/60-kodi.rules
new file mode 100644
index 0000000000..7023ff73a1
--- /dev/null
+++ b/community/kodi/60-kodi.rules
@@ -0,0 +1,12 @@
+// DO NOT EDIT THIS FILE, it will be overwritten on update
+//
+// Allows shutting down the system when using Kodi with a dedicated user
+
+polkit.addRule(function(action, subject) {
+        if (( (action.id.indexOf("org.freedesktop.udisks.") == 0) ||
+                (action.id.indexOf("org.freedesktop.upower.") == 0) ||
+                (action.id.indexOf("org.freedesktop.login1.") == 0) )  &&
+                subject.user == "kodi") {
+                return polkit.Result.YES;
+        }
+});
diff --git a/community/kodi/APKBUILD b/community/kodi/APKBUILD
index 2231b91820..df0ee6752d 100644
--- a/community/kodi/APKBUILD
+++ b/community/kodi/APKBUILD
@@ -3,7 +3,7 @@
 pkgname=kodi
 pkgver=18.3
 _realname=Leia
-pkgrel=0
+pkgrel=1
 _libdvdcss_pkgver="1.4.2-Leia-Beta-5"
 _libdvdread_pkgver="6.0.0-Leia-Alpha-3"
 _libdvdnav_pkgver="6.0.0-Leia-Alpha-3"
@@ -87,7 +87,7 @@ makedepends="$depends_dev
 	tar
 	openjdk8-jre-base
 	wayland-protocols"
-subpackages="$pkgname-dev $pkgname-doc $pkgname-dbg $pkgname-bin $pkgname-wayland $pkgname-gbm"
+subpackages="$pkgname-dev $pkgname-doc $pkgname-openrc $pkgname-dbg $pkgname-bin $pkgname-wayland $pkgname-gbm $pkgname-polkit::noarch"
 replaces="xbmc"
 options="ldpath-recursive !check" # No testsuite
 source="https://github.com/xbmc/xbmc/archive/$pkgver-$_realname.tar.gz
@@ -97,6 +97,8 @@ source="https://github.com/xbmc/xbmc/archive/$pkgver-$_realname.tar.gz
 	crossguid-$_crossguid_hash.tar.gz::https://mirrors.kodi.tv/build-deps/sources/crossguid-$_crossguid_hash.tar.gz
 	set-default-stacksize.patch
 	fix-musl-incompability.patch
+	kodi.initd
+	60-kodi.rules
 	"
 
 builddir="$srcdir/xbmc-$pkgver-$_realname"
@@ -184,6 +186,8 @@ package() {
 	cd "$builddir"/build-x11
 	make DESTDIR="$pkgdir" install
 
+	install -Dm755 "$srcdir"/kodi.initd "$pkgdir"/etc/init.d/kodi
+
 	rm \
 		"$pkgdir"/usr/lib/kodi/kodi-x11 \
 		"$pkgdir"/usr/lib/kodi/kodi-xrandr
@@ -208,15 +212,26 @@ wayland() {
 gbm() {
 	pkgdesc="GBM Kodi binary"
 	provides="$pkgname-bin"
+	depends="$pkgname-openrc"
+	install="$subpkgname.pre-install"
 
 	cd "$builddir"/build-gbm
 	install -Dm755 kodi-gbm "$subpkgdir"/usr/lib/kodi/kodi-gbm
 }
 
+polkit() {
+	pkgdesc="Polkit rules for Kodi in combination with GBM"
+	install_if="$pkgname-gbm polkit"
+
+	install -Dm644 "$srcdir"/60-kodi.rules "$subpkgdir"/etc/polkit-1/rules.d/60-kodi.rules
+}
+
 sha512sums="4e898e28238dcbc67e106c5a0ef88bd6a82f8c0ad00e77e3ddbe2da6181b37d9ca1ba6bda57206223cfa119c5291edb4410d3e773d97b5ab67660e49929437ec  18.3-Leia.tar.gz
 5185dbdbeb1bd13ea9d8723f1f4ab599d6f3102f5ba1096cd085aa1cda252c045f327c719227bba8e1b742352ade5e335106c8d0c1637a5a6b93ce661620dd7e  libdvdcss-1.4.2-Leia-Beta-5.tar.gz
 b3419ba0a1a2dd70f1bb6236afdfe1c6e88c9ad4264198b289e3bba9375e077cecf7f89848c7b09debaa445327f3507101f3d157e692f7a7163b2bb52643e1e7  libdvdread-6.0.0-Leia-Alpha-3.tar.gz
 11c93eaacd156f8fd7dec7c43d366438b201f31ad55b2870463a9e286912b6ada08882319a021fb7992190f87b909a49f2b83e0321cc17aedc29f7fe5898fa72  libdvdnav-6.0.0-Leia-Alpha-3.tar.gz
 2682d63609d3dcdfcd8136be632e45df26ad88ce93b9c49745cf728bbd2e6254a7b05c8b059ab581d532372e504206a525a52564b64d076dfdae9c965a09fd16  crossguid-8f399e8bd4.tar.gz
 3fd97e9fa22e59b3cd30fdad19474e177a9d062598765fa8ba4955f1da4e10581ff1463dd0794d3ee3cefebf753007a4d445b2bc47a257574d3927c1408b8876  set-default-stacksize.patch
-65b78a96bef33cec975b371f1a930d551d7b224955d6075b7edc7e51ec30fec9c77d34f622abbaf827c60df6029206b72c02ee2d2f138d276c1b843336fd14ab  fix-musl-incompability.patch"
+65b78a96bef33cec975b371f1a930d551d7b224955d6075b7edc7e51ec30fec9c77d34f622abbaf827c60df6029206b72c02ee2d2f138d276c1b843336fd14ab  fix-musl-incompability.patch
+7e2a2289da3098e0007e461ad538dcd6435e908283caf1719788248fe736615890175e11392ca0cd68002872217bbf7b70cf909f228307828aa82467d5502ed4  kodi.initd
+465a2079d598bf909a458d8e9d0ddd2b2a41dff83ba736542cffec7811c044295d5af092928f20aaa6cd3bc8b13bb03ede5c7ded1783680f890464e103392787  60-kodi.rules"
diff --git a/community/kodi/kodi-gbm.pre-install b/community/kodi/kodi-gbm.pre-install
new file mode 100644
index 0000000000..03b2e52770
--- /dev/null
+++ b/community/kodi/kodi-gbm.pre-install
@@ -0,0 +1,10 @@
+#!/bin/sh
+
+addgroup -S kodi 2>/dev/null
+adduser -S -D -h /var/lib/kodi -s /sbin/nologin -G kodi kodi 2>/dev/null
+
+for group in video audio input; do
+	adduser kodi $group 2>/dev/null
+done
+
+exit 0
diff --git a/community/kodi/kodi.initd b/community/kodi/kodi.initd
new file mode 100644
index 0000000000..4fedd315b9
--- /dev/null
+++ b/community/kodi/kodi.initd
@@ -0,0 +1,12 @@
+#!/sbin/openrc-run
+
+supervisor=supervise-daemon
+
+name="Kodi"
+description="A software media player and entertainment hub for digital media"
+
+command=/usr/lib/kodi/kodi-gbm
+command_args="--standalone"
+command_background=true
+pidfile="/var/run/kodi.pid"
+command_user="kodi:kodi"
diff --git a/community/kodi/no-snesapu.patch b/community/kodi/no-snesapu.patch
deleted file mode 100644
index 0ea55a8dd9..0000000000
--- a/community/kodi/no-snesapu.patch
+++ /dev/null
@@ -1,46 +0,0 @@
-diff --git a/Makefile.in b/Makefile.in
-index 31826b1..2f468e7 100644
---- a/Makefile.in
-+++ b/Makefile.in
-@@ -168,7 +168,6 @@ PAPCODECS_DIRS= \
- 	lib/timidity \
- 	lib/libsidplay2 \
- 	lib/stsound/StSoundLibrary \
--	lib/snesapu/SNES/SNESAPU \
- 	lib/vgmstream
- 
- ifeq (@USE_ASAP_CODEC@,1)
-@@ -373,9 +372,6 @@ ifeq (@USE_ASAP_CODEC@,1)
-   endif
- endif
- 	$(MAKE) -C lib/stsound/StSoundLibrary
--ifeq ($(or $(findstring powerpc,@ARCH@),$(findstring x86_64-linux,@ARCH@),$(findstring arm, @ARCH@),$(findstring freebsd,@ARCH@)),)
--	$(MAKE) -C lib/snesapu/SNES/SNESAPU
--endif
- imagelib: dllloader
- 	$(MAKE) -C lib/cximage-6.0
- 
-diff --git a/configure.in b/configure.in
-index 538d9af..3f04545 100644
---- a/configure.ac
-+++ b/configure.ac
-@@ -2348,7 +2348,6 @@ OUTPUT_FILES="Makefile \
-     lib/nosefart/Makefile \
-     lib/libsidplay2/Makefile \
-     lib/vgmstream/Makefile \
--    lib/snesapu/SNES/SNESAPU/Makefile \
-     lib/stsound/StSoundLibrary/Makefile \
-     xbmc/cores/playercorefactory/Makefile \
-     xbmc/music/karaoke/Makefile \
-diff --git a/xbmc/cores/paplayer/Makefile.in b/xbmc/cores/paplayer/Makefile.in
-index 31cc8bd..a433e82 100644
---- a/xbmc/cores/paplayer/Makefile.in
-+++ b/xbmc/cores/paplayer/Makefile.in
-@@ -31,7 +31,6 @@ SRCS += ASAPCodec.cpp
- endif
- 
- ifneq ($(ARCH), arm)
--CXXFLAGS += -DHAS_SPC_CODEC
- SRCS += SPCCodec.cpp
- endif
- 
-- 
2.21.0