Skip to content

Architecture

fosterbarnes edited this page Oct 30, 2026 · 7 revisions

How FTB's code fits together. FTB is C (gnu11) on libctru and citro3d, with the FTP server in C++ (gnu++20). Read this before changing shared code; Safety Rules and Code Style cover the rules.

Folders

Folder What lives there
source/core Engine: screen and textures (screen.c), UI (ui/), tasks and data ops (task/), data formats (data/: CIA, SMDH, banner, ticket, TMD, language), FS helpers (fs.c), HTTP (http.c), SPI for DS cards (spi.c), linked list, clipboard, string helpers
source/app FTB's screens, actions (action/), and list tasks (task/). screens.h declares every screen's open function.
source/app/legacy FBI-only screens: Titles, Tickets, Ext and System Save Data, Pending Titles, Dump NAND, Receive URLs over the network (receiveurls.c)
source/ftpd The FTP server (ftpd, C++) and its C bridge ftpd.{h,cpp}
source/app/webtransfer 3DS Web Server: the screen (webtransfer.c), its network worker and API (webserver.c), the HTTPS redirect (webtls.c), and HTTP and path rules that build on a PC too (webproto.c)
source/libs quirc (QR decoding), qrcodegen (QR encoding), and stb_image (PNG only): bug fixes and version updates only
romfs/ Built-in theme art and textcolor.cfg (see Theming), and the 3DS Web Server page webtransfer.html
meta/ HOME Menu icon, 3D banner, banner sound, launch splash
.scripts/ Build, release, and asset scripts
servefiles/ FBI's servefiles.py and sendurls.py for Other FBI Legacy > Receive URLs over the network, shipped in FTB.zip

The build finds sources recursively, so a new folder needs no Makefile change. Includes are relative to the including file.

Startup and exit

main.c: init() mounts RomFS, starts the system services from the services table (failing with "FTB needs Luma3DS" when am:net is not reachable), then starts the screen, UI, and task systems. mainmenu_open() loads the theme, settings, and CIA folders and pushes the main menu. The main loop runs ui_update() until the view stack is empty, the console asks to close, or app_restart() was called.

cleanup() stops the FTP server first (a HOME Menu exit skips the FTP screen's own B handler), then task_exit() waits up to 5 seconds for every worker before the services close.

app_restart() relaunches FTB after an update: the CIA build through libctru's chainloader, the 3DSX build by pointing Luma3DS's hb:ldr at its own path first.

UI views

source/core/ui/ui.{c,h}. The UI is a stack of ui_views; ui_push shows a view and ui_pop removes the top one. Each frame ui_update calls only the top view's update, then draws its drawTop and drawBottom with the screen bars, the view's name (bottom screen's top bar), and info (key hints in the bottom bar).

Building blocks on top of views:

Module Use
list.c Scrolling lists (most screens), see below
prompt.c Notices and yes/no prompts. prompt_wait_yes_no blocks a worker thread until the user answers.
info.c Progress screens; info_set_data_op draws item and batch bars, speed, and time left from a data op
error.c Error screens with the decoded result code
kbd.c The system keyboard

Touch buttons: set view->buttons and buttonCount (UI_BUTTON_SIZE 26 px). ui_update hit-tests them before update; a button fires when the touch lifts on it and update sees its index in buttonActivated for that frame.

Lists

source/core/ui/list.{c,h}. list_display(name, info, data, update, drawTop) shows a linked_list of list_items (name, color, data). The update callback adds items and reacts to the selection.

  • Layouts: list_set_layout(view, style, rowsOnTop, itemIcon, itemDetail). Styles are LIST_STYLE_TEXT (FBI's rows, the default), LIST_STYLE_ICON_LIST, and LIST_STYLE_GRID. Rows sit on the bottom screen by default; rowsOnTop swaps them with drawTop. Every screen but the Title Manager uses the default, which must stay identical to FBI's list.
  • Zoom: per style (list_zoom_count, list_set_zoom): grid 2 levels, icon list 3.
  • Sections: list_set_sections(view, label, headers) starts a section wherever label changes; headers draws headers over them. list_set_section_keys(view, true) makes L/R jump sections.
  • Loading: list_set_loading(view, true) replaces the rows with loading dots and ignores input.
  • Spacers: list_item.spacer is a blank row that can never be selected (text style only).

Tasks

source/core/task/task.{c,h}. Every background job embeds a task_state and starts with task_start(&state, func, arg, priority, core); never call threadCreate directly.

Side Rules
Before the first start Set state.finished = true, so "not running" reads as finished
UI thread Read finished only through task_is_finished; after that, state.result and the worker's writes are safe to read. task_cancel asks a worker to stop; task_cancel_and_wait before leaving or reloading a list.
Worker Poll task_is_cancelled (it also waits while the console sleeps, and is true when FTB quits)
Sleep task_sleep_block / task_sleep_unblock count holders; never call aptSetSleepAllowed from app code

The linked list has no lock: one worker may append while the UI walks it, but never sort or remove while a populate task may still insert. Screens whose actions remove rows open them only after their populate task finishes.

Data ops

source/core/task/dataop.{c,h}. Copies, downloads, and deletes go through task_data_op with a data_op_data: the caller fills callbacks (openSrc, readSrc, openDst, writeDst, delete, error, and so on) and the engine runs the loop, the retry prompts, and the progress fields. Set op.totalBytes when sizes are known (else batch progress counts items) and op.stage around slow steps that move no bytes. Data ops block sleep while they run.

CIA installs from the SD card (action/installcias.c) and from URLs (action/installurl.c) share action/ciainstall.{c,h}: change install rules there, never in one caller. Ticket installs share action/ticketinstall.{c,h} the same way (lock, backup of the old tickets, restore on failure).

Title Manager and the title cache

source/app/titlemanager.c is the screen; source/app/task/listtitles.c loads titles. The cache is one file per pass in sdmc:/ftb/cache/ (sdTitles.bin, nandTitles.bin, twlTitles.bin; game cards are never cached): a header (magic FTBT, format, record size, system language) and title_records sorted by title ID. AM's live title list decides which titles exist; a record is reused only while its title ID, version, and installed size match. Cached titles show first, then misses load; a file is rewritten only after a complete pass that changed something. Bump TITLE_CACHE_FORMAT when title_record changes meaning without changing size.

source/app/legacy/titles.c is FBI's frozen Titles screen. Both share the title actions (titles_action_open).

Settings and theme

  • source/app/settings.c stores sdmc:/ftb/settings.cfg. Add an on/off setting with a field in ftb_settings and a row in settings_bools; a choice with a row in settings_choices (single digit, below its count). settings_set() replaces and saves the whole struct. Saves go through fs_write_file_safe (write <path>.tmp, then replace); loads go through fs_open_file_safe, which uses a leftover <path>.tmp when <path> is missing.
  • source/app/resources.c loads colors and textures from the resources_colors and resources_textures tables; add new keys and art there under new names.
  • SD paths live in source/app/paths.h.

FTP server

source/ftpd is ftpd 3.2.2, treated as FTB code (no upstream syncing; older changes are marked FTB:). platform.cpp is the 3DS layer, ftpd.{h,cpp} the C bridge used by source/app/ftp.c. The UI thread never touches the server's sockets: settings set a restart flag and the server thread reopens. Do not include C task headers from source/ftpd (dataop.h has a member named delete); declare the few C functions it needs with extern "C".

Clone this wiki locally