feat(ui): add game shortcuts to the Ports menu

Closed
AL
1mo ago
+1079-4

Based on feat/geforce-now like #8, and independent of it: the two touch different parts of handheld_ui.zig and merge cleanly. Branch name and commits follow the feat/<kebab-case> and conventional-commit patterns of #2-#4. The fix for a bug found during device testing is a separate commit, as #3 did.

What does this change?

Adds game shortcuts: a game from the library gets its own Ports entry that opens straight into that game. The idea comes from the shortcut feature in Kasumi (a GeForce NOW client for the 3DS). No Kasumi code is used, since it is GPL-3.0.

  • In the library, Select on a title writes <Title>.sh next to GreenOvercast.sh, and Select again removes it. The prompt bar shows SELECT PORTS when shortcuts are available, and a short notice confirms each action in place of the second prompt row.
  • The shortcut sets GREENOVERCAST_SERVICE and GREENOVERCAST_AUTOSTART=1, then execs the real launcher with the title's product id as $1. exec keeps $0 pointing at the real launcher, which PortMaster's control.txt derives GAMEDIR from.
  • On startup, autostart starts that title once if it is in the catalog. The existing argv[1] cursor handling is reused through a new exact-match library.findTitle. If the title is no longer in the catalog, the library opens as usual. After the session ends, the library is shown, as for any other launch.
  • muOS: the title's cached artwork is converted to a PNG that fits inside 320x240 and written to the Ports catalogue box/ folder. The folder and the "fit inside 320x240, never enlarge" rule are the ones PortMaster's muos/image_smash.txt uses for installed ports. The launcher exports GREENOVERCAST_PORT_ARTWORK_DIR only when that folder exists. Other CFWs get the entry without artwork; their frontends store artwork differently and I can't test them.
  • Safety:
    • Every generated script carries a marker line, and only files with that marker are ever replaced or removed. A real port with the same name is left alone ("A PORT WITH THIS NAME ALREADY EXISTS").
    • Titles are reduced to a conservative ASCII file name.
    • The launcher path and product id are validated before they are single-quoted into the script.
    • Select acts on release and is disarmed by Start, so the Select + Start exit chord never creates a shortcut.

New code:

  • src/ui/port_shortcut.zig: file names, script generation, add and remove.
  • src/ui/cover_image.zig: box-filter fit and a small PNG encoder built on std.compress.zlib. The FFmpeg build has no PNG encoder.
  • Loader.cachedCoverPng in artwork_loader.zig.
  • The launcher exports GREENOVERCAST_PORT_LAUNCHER and, on muOS, GREENOVERCAST_PORT_ARTWORK_DIR.

How did you test it?

  • tools/zig.sh build test and tools/zig.sh build fmt-check pass. New tests cover:
    • file name sanitising
    • exact script contents
    • quoting rejection (launcher with a quote, relative launcher, product id with shell syntax)
    • add and remove without touching a same-named foreign port, including the 0755 mode
    • image fitting and box filtering
    • PNG header and IEND CRC, plus a zlib round-trip of the image data
  • tools/zig.sh build release passes, and nm -D --undefined-only webrtc_stream | grep -c __aarch64_ is 0.
  • On an Anbernic RG35XX-H (H700, muOS, kernel 4.9.170), with GeForce NOW and CONTROL:
    1. Select added CONTROL Resonant.sh to Ports, along with box/CONTROL Resonant.png (170x240 from the 256-wide portrait art). The entry showed its cover in the muOS Ports list.
    2. Launching the entry went straight into a session (Starting shortcut title), skipping the library.
    3. Select removed the script and its artwork.
    4. The shortcut also works when GreenOvercast itself is started from muOS Applications through a wrapper that execs the port launcher. On muOS, realpath "$0" resolves the launcher to /mnt/union/ROMS/PORTS/..., and the shortcut works from that path.
  • Bug found and fixed on device: the first build saved shortcuts without artwork on muOS. Dir.readFileAlloc sizes its buffer with File.stat(), which uses statx, and that fails on kernel 4.9 (the limitation stat_compat.zig documents). The second commit reads the cache with readToEndAlloc instead.
  • Not tested: Xbox shortcuts (I only use GFN; the Xbox path uses the same product id matching), and other CFWs.
  • Unrelated observation: in one shortcut-launched session the client exited when I pressed A in CONTROL's launcher. A relaunch worked, and I couldn't reproduce it. The launcher truncates its log on every start, so that run's log was lost. The shortcut code is not involved once the session starts, but I'm mentioning it in case you've seen it.
  • Related upstream note, not changed here: video_pipeline.zig:154 calls file.getEndPos(), which also goes through File.stat()/statx. On 4.9 kernels that probably fails and returns 0, so the H.264 bootstrap file may never be read. I haven't verified this; happy to look at it separately.

Review follow-up (cubic)

I fixed five of cubic's six findings in two focused commits. The sixth is documented rather than changed; my reasons are in its thread.

  • b3bbf8e fix(ui):
    • a marked script only counts as this title's shortcut when it launches the same product id, so same-named titles stay apart
    • writes go through std.fs.AtomicFile (random name, exclusive create)
    • removal deletes the artwork first, best effort, and the script last
    • a cache miss no longer logs
    • a comment explains why cover generation stays on the caller's thread
  • 3cb0f78 fix(portmaster): the launcher resolves itself through BASH_SOURCE and logs when shortcuts are disabled.

Re-verified on the RG35XX-H after these commits: remove and re-add worked, the cover was rewritten, the script is mode 755, and no temporary files were left behind.

Review round 2 (maintainer)

Both requested changes are fixed, one commit each:

  • d4842b5 fix(ui): shortcut artwork is created only when no image exists, and its CRC-32 and size are recorded in the shortcut's own script. It is replaced or removed only while that record still matches the file, so other ports' images in muOS's shared catalogue are never overwritten or deleted. Regression tests cover an existing PNG with and without a cached cover, owned artwork, and artwork replaced after creation.
  • 5c17dc0 fix(portmaster): the launcher path is resolved before PortMaster's helpers run or the directory changes, and exported only when it is an existing file, so bash ./GreenOvercast.sh from the Ports folder works.

Re-verified: build test, fmt-check and build release (0 __aarch64_ imports). ShellCheck is unchanged from the base. Your P2 and P3 reproductions now pass, the P3 mock covering absolute, relative and sourced starts. On the RG35XX-H, add → remove → add created the cover, recorded it, deleted it and recreated it, with no temporary files left behind.

Review round 3

  • 3e77e68 fix(ui): addresses cubic's two findings. add() now builds the script before touching any file, creates new artwork exclusively (O_EXCL), and rolls the artwork back if saving the script fails.

While skimming the full change, I also found and fixed a few more issues:

  • 94550d0: only absolute shortcut paths are accepted from the environment, because openDirAbsolute() asserts. Autostart requires GREENOVERCAST_AUTOSTART=1.
  • ad94ad9: a pending Select is cancelled when the search keyboard or settings open.
  • 5d97941: a shortcut whose game is gone shows "SHORTCUT GAME NOT FOUND", and shortcut problems are shown in red.
  • 8823480: a shortcut's script is kept while its artwork can't be removed, so no image is orphaned. Names can't imitate the artwork record.
  • 80ef3ee: the rollback tests now prove the rollback ran, and the root check works on macOS hosts.

Each commit passes build test and fmt-check on its own. build release passes with 0 __aarch64_ imports. Re-tested on the RG35XX-H: add and remove with artwork, the missing-game notice, and Select with search open.

AI disclosure

Per CONTRIBUTING: this was written with AI assistance (Claude). I reviewed the diff, built it, and tested it on hardware myself. I'm happy to change the button, the naming, or how other CFWs should be handled.

🤖 Generated with Claude Code


Summary by cubic

Adds game shortcuts to the Ports menu: pressing Select on a library title creates a launcher next to GreenOvercast.sh that starts that game directly, and pressing Select again removes it. The launcher re-runs the port launcher with the title's product id and autostart, so launching the shortcut skips the library. On muOS, the title's cached artwork is also written to the Ports catalogue as a PNG so the entry shows a cover.

Only scripts carrying the shortcut marker are ever replaced or removed, and only when they launch the same product id, so existing ports with the same name are never touched. Writes go through atomic files, the launcher path is resolved from BASH_SOURCE before PortMaster's helpers change directories, and launcher paths are capped at 512 bytes so every generated script fits in the recognition buffer.

Bug Fixes

  • Shortcuts were saved without artwork on muOS because the artwork read used File.stat(), which needs statx and fails on the 4.9 kernels these handhelds ship; it now reads with readToEndAlloc.
  • Artwork in muOS's shared catalogue is only replaced or removed while the CRC-32 and size recorded in the shortcut's own script still match the file on disk.
  • A failed script save rolls the artwork back, a failed artwork removal keeps the shortcut and its record so a later attempt can clean up, and new artwork is created exclusively so another writer's file is never replaced.
  • Invalid launcher or artwork environment paths now disable shortcuts instead of ending the app on Select, and a pending Select press is cancelled when the search keyboard or settings opens.
  • A shortcut whose game is no longer in the catalog shows SHORTCUT GAME NOT FOUND in red instead of opening the library without explanation.