A rootless Android app that boots Alpine Linux: run containers (Podman/Docker/LXC) and GUI desktop apps.
fix: terminal sizing, arrow keys, and MOTD polish
ExTvbb0e8f599228
diff --git a/app/src/main/java/com/excp/podroid/ui/screens/terminal/TerminalViewModel.kt b/app/src/main/java/com/excp/podroid/ui/screens/terminal/TerminalViewModel.ktindex eb3edae..9d5ba52 100644--- a/app/src/main/java/com/excp/podroid/ui/screens/terminal/TerminalViewModel.kt+++ b/app/src/main/java/com/excp/podroid/ui/screens/terminal/TerminalViewModel.kt@@ -272,8 +272,12 @@ class TerminalViewModel @Inject constructor(// 6=ctrl+shift, 7=ctrl+alt, 8=all. Used for "ESC [1;<m><final>".val mod = 1 + (if (shift) 1 else 0) + (if (alt) 2 else 0) + (if (ctrl) 4 else 0)fun arrow(final: Char): ByteArray =- if (mod == 1) "\u001b[$final".toByteArray()- else "\u001b[1;$mod$final".toByteArray()+ if (mod == 1) {+ if (isDecsetSet(session?.emulator, 1)) "\u001bO$final".toByteArray()+ else "\u001b[$final".toByteArray()+ } else {+ "\u001b[1;$mod$final".toByteArray()+ }val bytes = when (keyCode) {KeyEvent.KEYCODE_ENTER -> byteArrayOf(13)@@ -409,24 +413,29 @@ class TerminalViewModel @Inject constructor(fun sendFocusEvent(focused: Boolean) {val sess = session ?: returnval emu = sess.emulator ?: return- if (!isFocusReportingEnabled(emu)) return+ if (!isDecsetSet(emu, 1004)) returnval seq = if (focused) "\u001b[I".toByteArray() else "\u001b[O".toByteArray()sess.write(seq, 0, seq.size)}- private fun isFocusReportingEnabled(emu: TerminalEmulator): Boolean = try {- val cls = TerminalEmulator::class.java- val flagsField = cls.getDeclaredField("mCurrentDecSetFlags").apply { isAccessible = true }- // Termux maps the public DECSET code 1004 via mapDecSetBitToInternalBit — we can't- // call that (package-private), so we derive the bit by setting 1004 through- // the public doDecSetOrReset path and observing the delta. Cheaper: invoke the- // package-private mapper reflectively.- val mapper = cls.getDeclaredMethod("mapDecSetBitToInternalBit", Int::class.javaPrimitiveType)- .apply { isAccessible = true }- val bit = mapper.invoke(null, 1004) as Int- val flags = flagsField.getInt(emu)- (flags and bit) != 0- } catch (_: Throwable) { false }+ /**+ * Check if a DECSET mode is enabled in the emulator (e.g. 1 for DECCKM,+ * 1004 for focus reporting). Uses reflection to access private fields+ * and mapper methods in the Termux AAR.+ */+ private fun isDecsetSet(emu: TerminalEmulator?, bit: Int): Boolean {+ if (emu == null) return false+ return try {+ val cls = TerminalEmulator::class.java+ val mapper = cls.getDeclaredMethod("mapDecSetBitToInternalBit", Int::class.javaPrimitiveType)+ .apply { isAccessible = true }+ val internalBit = mapper.invoke(null, bit) as Int+ val flagsField = cls.getDeclaredField("mCurrentDecSetFlags")+ .apply { isAccessible = true }+ val flags = flagsField.getInt(emu)+ (flags and internalBit) != 0+ } catch (_: Throwable) { false }+ }fun sendExtraKey(key: String) {when (key) {@@ -436,12 +445,12 @@ class TerminalViewModel @Inject constructor(val bytes = when (key) {"ESC" -> byteArrayOf(27)"TAB" -> byteArrayOf(9)- "UP" -> "\u001b[A".toByteArray()- "DOWN" -> "\u001b[B".toByteArray()- "LEFT" -> "\u001b[D".toByteArray()- "RIGHT"-> "\u001b[C".toByteArray()- "HOME" -> "\u001b[H".toByteArray()- "END" -> "\u001b[F".toByteArray()+ "UP" -> if (isDecsetSet(session?.emulator, 1)) "\u001bOA".toByteArray() else "\u001b[A".toByteArray()+ "DOWN" -> if (isDecsetSet(session?.emulator, 1)) "\u001bOB".toByteArray() else "\u001b[B".toByteArray()+ "LEFT" -> if (isDecsetSet(session?.emulator, 1)) "\u001bOD".toByteArray() else "\u001b[D".toByteArray()+ "RIGHT"-> if (isDecsetSet(session?.emulator, 1)) "\u001bOC".toByteArray() else "\u001b[C".toByteArray()+ "HOME" -> if (isDecsetSet(session?.emulator, 1)) "\u001bOH".toByteArray() else "\u001b[H".toByteArray()+ "END" -> if (isDecsetSet(session?.emulator, 1)) "\u001bOF".toByteArray() else "\u001b[F".toByteArray()"PGUP" -> "\u001b[5~".toByteArray()"PGDN" -> "\u001b[6~".toByteArray()"F1" -> "\u001bOP".toByteArray()diff --git a/init-podroid b/init-podroidindex df5469f..3ec323f 100755--- a/init-podroid+++ b/init-podroid@@ -81,7 +81,7 @@ fiif [ -b /dev/zram0 ]; then_mem_kb=$(awk '/^MemTotal:/{print $2}' /proc/meminfo)echo lz4 > /sys/block/zram0/comp_algorithm 2>/dev/null || \- echo lzo-rle > /sys/block/zram0/comp_algorithm 2>/dev/null+ echo lzo-rle > /sys/block/zram0/comp_algorithm 2>/dev/nullecho $(( _mem_kb * 512 )) > /sys/block/zram0/disksize 2>/dev/nullmkswap /dev/zram0 >/dev/null 2>&1 && swapon -p 100 /dev/zram0 2>/dev/nullfi@@ -194,7 +194,7 @@ trap 'podman stop -a -t 5 2>/dev/null; podman network prune -f 2>/dev/null; sync[ -c /dev/hvc1 ] || exit 0while IFS= read -r _line; docase "$_line" in- RESIZE\ *) set -- $_line; stty rows "$2" cols "$3" </dev/hvc0 2>/dev/null ;;+ RESIZE\ *) set -- $_line; stty rows "$2" cols "$3" </dev/hvc0 2>/dev/null; printf '%s %s\n' "$2" "$3" > /run/term_size 2>/dev/null ;;esacdone </dev/hvc1) &@@ -202,7 +202,8 @@ trap 'podman stop -a -t 5 2>/dev/null; podman network prune -f 2>/dev/null; sync# ── MOTD + profile ────────────────────────────────────────────────────────────boot_stage "Almost ready..."-_kver_s=$(uname -r | cut -d- -f1)+_alpine=$(cat /etc/alpine-release 2>/dev/null || echo "?")+_kver=$(uname -r | cut -d- -f1)_mem_mb=$(awk '/^MemTotal:/{printf "%.0f",$2/1024}' /proc/meminfo)_mem_free=$(awk '/^MemAvailable:/{printf "%.0f",$2/1024}' /proc/meminfo)_cpus=$(nproc)@@ -210,34 +211,41 @@ _uptime=$(( $(cut -d. -f1 /proc/uptime) / 60 ))_disk_used=$(df -BG / 2>/dev/null | awk 'NR==2{gsub(/G/,"",$3);print $3}')_disk_total=$(df -BG / 2>/dev/null | awk 'NR==2{gsub(/G/,"",$2);print $2}')-if [ "$SSH_ENABLED" = "1" ] && [ -n "$ANDROID_IP" ]; then- _ssh_block="-┌────────────────────────────────────────────────────────────┐-│ SSH: ssh root@${ANDROID_IP} -p 9922 pass: podroid │-└────────────────────────────────────────────────────────────┘"+# Probe internet synchronously (max 2s) so the *first* MOTD already shows the+# real status. The async-sed pattern was racing with first login → user always+# saw "checking..." until they exited the shell once.+if wget -q -O /dev/null --timeout=2 http://dl-cdn.alpinelinux.org/alpine/MIRRORS.txt 2>/dev/null; then+ _net="✓ online"else- _ssh_block=""+ _net="✗ offline"fi-# __NET__ is replaced asynchronously once the internet check completes-cat > /etc/motd <<MOTD-- ╔══════════════════════════════════════════════════════════╗- ║ Podroid · Alpine Linux Container Runtime ║- ╚══════════════════════════════════════════════════════════╝-- 📦 Alpine ${_kver_s} · ${_cpus} CPU · ${_mem_free}/${_mem_mb} MB RAM · ${_disk_used}/${_disk_total} GB · Up ${_uptime}m- Net: __NET__-${_ssh_block}- 🐳 podman run -it --rm alpine # interactive shell- podman run -d -p 8080:80 nginx # background service- podman ps -a # list containers--MOTD+if [ "$SSH_ENABLED" = "1" ] && [ -n "$ANDROID_IP" ]; then+ _ssh_line="SSH: ssh root@${ANDROID_IP} -p 9922 (pass: podroid)"+else+ _ssh_line=""+fi-{ wget -q -O /dev/null --timeout=2 http://dl-cdn.alpinelinux.org/alpine/MIRRORS.txt 2>/dev/null \- && sed -i 's/__NET__/✓ internet/' /etc/motd \- || sed -i 's/__NET__/✗ offline/' /etc/motd; } &+# ── MOTD box ──────────────────────────────────────────────────────────────────+# Bash counts ${#var} as characters (not bytes) under LC_ALL=C.UTF-8, which is+# already exported above — so multi-byte content like · / ✓ / ✗ pads correctly.+_W=58+_pad() {+ local _s="$1" _p+ _p=$((_W - ${#_s})); [ $_p -lt 0 ] && _p=0+ printf '│ %s%*s │\n' "$_s" "$_p" ''+}+_BAR='────────────────────────────────────────────────────────────'+{+ printf '┌%s┐\n' "$_BAR"+ _pad ""+ _pad "Podroid · Alpine ${_alpine} · Linux ${_kver}"+ _pad "${_cpus} CPU · ${_mem_free}/${_mem_mb} MB · ${_disk_used}/${_disk_total} GB · Up ${_uptime}m · Net: ${_net}"+ [ -n "$_ssh_line" ] && _pad "$_ssh_line"+ _pad "Quickstart: podman run -it --rm alpine"+ _pad ""+ printf '└%s┘\n' "$_BAR"+} > /etc/motdcat > /etc/profile <<'PROF'export HOME=/root SHELL=/bin/bash@@ -259,13 +267,26 @@ alias dstop='podman stop' drm='podman rm' drmi='podman rmi' dstats='podman stPS1='\[\e[1;32m\]➜\[\e[0m\] \[\e[1;34m\]\w\[\e[0m\] \[\e[1;31m\]#\[\e[0m\] 'cd /root 2>/dev/null++# Auto-source the user's ~/.bashrc if present. /root is on the persistent+# overlay upper, so this survives reboots — drop a .bashrc there to layer+# custom aliases / PS1 / env on top of the defaults above.+[ -r "$HOME/.bashrc" ] && . "$HOME/.bashrc"PROF# ── Login wrapper ─────────────────────────────────────────────────────────────cat > /usr/local/bin/podroid-login <<'LOGIN'#!/bin/bash+# virtio-console resets winsize to 0x0 each time the line is reopened, so+# every getty respawn (after `exit` / Ctrl-D) starts a shell that TUI apps+# can't draw into until the next host SIGWINCH. Restore the last-known size+# the resize daemon recorded in /run/term_size before exec'ing bash.+if [ -r /run/term_size ]; then+ read _r _c < /run/term_size+ [ -n "$_r" ] && [ -n "$_c" ] && stty rows "$_r" cols "$_c" 2>/dev/null+ficat /etc/motd 2>/dev/null-exec /usr/bin/script -q -c 'exec /bin/bash --login' /dev/null+exec /bin/bash --loginLOGINchmod +x /usr/local/bin/podroid-logingrep -q '^root:' /etc/passwd 2>/dev/null || \