From 644a80bce1aea4faec3a3f83f92da0728cef8a25 Mon Sep 17 00:00:00 2001 From: Noa Himesaka Date: Sun, 31 May 2026 19:09:04 +0900 Subject: [PATCH] fix: appload quirk workaround (#15475) AppLoad's quirk of removing framebuffer when nothing's holding framebuffer even though app is running prevented crash handling and restarting KOReader to fail, so introduce a keep-alive process (which was LLM assisted, and it's very cursed, but it works!) to work around this issue. It wasn't a problem for shimmed version since shim loaded even for sh handling shell script too, which acted as keep-alive. --- .ci/check.sh | 2 +- make/remarkable.mk | 1 + platform/remarkable/koreader.sh | 13 +++++++ platform/remarkable/qtfb_keep_alive.lua | 45 +++++++++++++++++++++++++ 4 files changed, 60 insertions(+), 1 deletion(-) create mode 100644 platform/remarkable/qtfb_keep_alive.lua diff --git a/.ci/check.sh b/.ci/check.sh index 4895a02db..ce11fc6c8 100755 --- a/.ci/check.sh +++ b/.ci/check.sh @@ -39,6 +39,6 @@ if [ "${untagged_todo}" ]; then fi echo -e "\n${ANSI_GREEN}Luacheck results${ANSI_RESET}" -luacheck ${PARALLEL_JOBS:+-j "${PARALLEL_JOBS}"} -q {reader,setupkoenv,datastorage}.lua frontend plugins spec || exit_code=1 +luacheck ${PARALLEL_JOBS:+-j "${PARALLEL_JOBS}"} -q {reader,setupkoenv,datastorage}.lua frontend plugins spec platform/remarkable/qtfb_keep_alive.lua || exit_code=1 exit ${exit_code} diff --git a/make/remarkable.mk b/make/remarkable.mk index 369998f24..cd78d9fd7 100644 --- a/make/remarkable.mk +++ b/make/remarkable.mk @@ -14,6 +14,7 @@ update-prepare: all # Remarkable scripts $(SYMLINK) $(COMMON_DIR)/spinning_zsync $(INSTALL_DIR)/koreader/ $(SYMLINK) $(REMARKABLE_DIR)/koreader.sh $(INSTALL_DIR)/koreader/ + $(SYMLINK) $(REMARKABLE_DIR)/qtfb_keep_alive.lua $(INSTALL_DIR)/koreader/ $(SYMLINK) resources/koreader.png $(INSTALL_DIR)/koreader/icon.png $(SYMLINK) $(REMARKABLE_DIR)/external.manifest.json $(INSTALL_DIR)/koreader/ $(SYMLINK) $(REMARKABLE_DIR)/external.manifest.shim.json $(INSTALL_DIR)/koreader/ diff --git a/platform/remarkable/koreader.sh b/platform/remarkable/koreader.sh index fa42ddeb6..c41f16f21 100755 --- a/platform/remarkable/koreader.sh +++ b/platform/remarkable/koreader.sh @@ -166,6 +166,14 @@ if [ -e crash.log ]; then mv -f crash.log.new crash.log fi +# since AppLoad unloads framebuffer when first launched app using QTFB exits, run a keep-alive process to prevent this from happening. +BG_KEEP_ALIVE_PID="" +if [ -n "${KO_USE_QTFB}" ] && [ -n "${QTFB_KEY}" ]; then + echo "Starting QTFB connection keep-alive..." >>crash.log 2>&1 + ./luajit qtfb_keep_alive.lua >/dev/null 2>&1 & + BG_KEEP_ALIVE_PID=$! +fi + CRASH_COUNT=0 CRASH_TS=0 CRASH_PREV_TS=0 @@ -277,4 +285,9 @@ if [ -z "${KO_DONT_SET_DEPTH}" ] && [ -z "${KO_USE_QTFB}" ]; then fi fi +if [ -n "${BG_KEEP_ALIVE_PID}" ]; then + echo "Stopping QTFB connection keep-alive..." >>crash.log 2>&1 + kill "${BG_KEEP_ALIVE_PID}" >>crash.log 2>&1 +fi + exit ${RETURN_VALUE} diff --git a/platform/remarkable/qtfb_keep_alive.lua b/platform/remarkable/qtfb_keep_alive.lua new file mode 100644 index 000000000..232f9b042 --- /dev/null +++ b/platform/remarkable/qtfb_keep_alive.lua @@ -0,0 +1,45 @@ +-- qtfb_keep_alive.lua +-- Background process to keep the QTFB socket connection active across KOReader restarts. +-- This prevents the rm-appload launcher from destroying the QML window canvas. + +local ffi = require("ffi") +require("ffi/posix_h") +local qtfb = require("ffi/qtfb") +local C = ffi.C + +local key_str = os.getenv("QTFB_KEY") +local key = key_str and tonumber(key_str) or 245209899 -- QTFB_DEFAULT_FRAMEBUFFER + +local shmType = 0 -- FBFMT_RM2FB as default +if qtfb.is_rmpp then + shmType = 3 -- FBFMT_RMPP_RGB565 +elseif qtfb.is_rmppm then + shmType = 6 -- FBFMT_RMPPM_RGB565 +end + +-- Create UNIX domain socket +local sock = C.socket(C.AF_UNIX, C.SOCK_SEQPACKET, 0) +assert(sock >= 0, "Failed to create UNIX socket") + +local addr = ffi.new("struct sockaddr_un", C.AF_UNIX, "/tmp/qtfb.sock") + +-- Retry loop to wait for the QTFB server (xochitl/rm-appload) to start listening +while C.connect(sock, ffi.cast("const struct sockaddr *", addr), ffi.sizeof(addr)) ~= 0 do + C.sleep(1) +end + +-- Send MESSAGE_INITIALIZE (0) +local initMsg = ffi.new("struct ClientMessage") +initMsg.type = qtfb.MESSAGE_INITIALIZE +initMsg.init.framebufferKey = key +initMsg.init.framebufferType = shmType + +local bytes_sent = C.send(sock, initMsg, ffi.sizeof(initMsg), 0) +assert(bytes_sent >= 0, "Failed to send init message to QTFB server") + +-- Keep the socket open indefinitely until killed by the parent process. +-- pause() blocks the process indefinitely until a signal is received. +ffi.cdef[[int pause(void);]] +while true do + C.pause() +end