# The Nugget Linux desktop Nano-X (Microwindows) on the Mac Plus screen, on the two-disk set (`./make-floppy.sh plus set` → `floppy/linux-plus-set.dsk` and `floppy/linux-plus-set-2.dsk`), with networking. At the shell on the Mac's own console, type `desktop`; Quit Desktop (in the menu bar's triangle menu) comes back to the shell. - `microwindows/`: Microwindows, cloned from https://github.com/ghaerr/microwindows at 3b20527696f29ddd6726f291400895a034fbec0a, with our changes in place. `microwindows.patch` records them (`git -C microwindows diff`). They're all under `#if NUGGET` (or `MAC_MONO01`): - 1 bpp framebuffer only, with the palette swapped (on the Mac, 1 = black) - Lisa-style window frames (white caption; the active window's title white in a black box between vertical bars; on a window too narrow for the bars, the whole caption is black, the close box an opening in it; a plain square close box at the left), a grow strip with a grow box under resizable windows (the terminal and NugEdit: the other apps have fixed layouts), and a 50% grey desktop drawn straight into the screen - requests of at most 2 KB: the server keeps one on its 16 KB stack - nxterm on legacy ptys (/dev/ptypN) with vfork; it scrolls by several lines at once when output comes faster than it can draw (it waits for the server to finish drawing before reading more) - NugEdit (`nugedit.c`, our own; it replaced Microwindows' nxedit): one document a window, File and Edit menus in a strip in the window, Undo and Redo, Cut/Copy/Paste through /tmp/.clipboard, Find, Open and Save As dialogs (the RAM disk, Mac floppies, and the Nugget disk to open from). Floppies go through nxfiles (a FIFO, /tmp/.nxfiles: only it uses libhfs). Fixed-width text wrapped at the window's edge; after a change only the rows that differ are drawn. NUGEDIT_KEYS types keys, for tests in MAME (which has no Mac keyboard) - FloppySaver (Apps; `/hfs/fsave.c`): a full low-level format, bad sectors set aside in the volume's bad-block file, 800K or 400K. It's nxfiles run under that name (/bin/FloppySaver links to it): the code runs in place, so it costs no RAM until it runs, and the desktop goes on meanwhile (nxfiles lets go of the drive: U and M requests) - Quit Desktop, Restart and Shut Down ask each NugEdit to close first (it asks about unsaved changes; its Cancel cancels the quit) - Shift, Command and Option with keys and clicks: the server asks the console for its shift state (TIOCLINUX 6; tty bytes don't carry it), per key and per button change, not per mouse move - the server scrolls a buffered window within its buffer, not from the screen. (It ran at nice -5 for a while: that made it preempt an app at every request, which cost more than it gained.) - a new window is framed by the window manager before it's drawn, so it's drawn once (it was drawn bare on the desktop first, then again) - the pointer: a black arrow with a white halo (crisp on the grey), drawn by the kernel at each vertical blank, as the Mac's ROM did (`asm/macplus_cursor.h`, shared through /dev/kcursor): it moves while the server is busy, and the server is woken for motion only 15 times a second (each move in one pass down the screen, so never a moment without a pointer). The server marks itself busy around each request and hides the pointer only where it draws over it - save-unders: the screen under a window with `GR_WM_PROPS_SAVEUNDER` (nxfiles' menus, alerts and Wait box) is kept, and put back when it goes, with no redrawing by the windows under it (unless something drew there meanwhile) - the window manager's events (child updates it selects for every client) are dealt with before a client is woken, not sent on as empty events - text: whole strings a row at a time; the client works out text sizes from the font's widths, and keeps the system colours: fewer round trips, each about 80 ms (most of it the kernel's per-message cost) - no socket: requests, replies and events go by two FIFOs per client, named after it; it tells the server its pid by the server's FIFO, /tmp/.nano-X, and the names go once both ends are open. That works with a kernel without networking; and a round trip by pipe is about 20 ms, by a Unix socket 44 (MAME) - a timed wait for events (GrGetNextEventTimeout) leaves at most one request out: they used to pile up until client and server deadlocked - dragging or resizing a window shows only an outline until the button is let go (OUTLINE_MOVE), drawn as on the Mac through the desktop's grey pattern: solid over the desktop, dotted over windows (a plain inverted line is invisible on a 50% grey) - minimizing, as NeXTSTEP's miniwindows (`nanox/wmtile.c`): double-click a window's title bar and it's hidden, a 52x52 tile at the bottom left in its place (the program's icon, its name); double-click the tile and it's back. The program runs on meanwhile. The icons are drawn by `mkwmicons.py`, or by hand in `../icons/NAME-tile.png` (32x32, black and white), which take the place of its own; it writes `nanox/wmicons.h` (`--show` prints them). The file manager shows the same icons for the programs in Apps. One click on a tile brings the window back (it's inverted while held). A tile's label is the program's name, or the window's title when that isn't the program's (a document, a picture). Live tiles (`GR_WM_PROPS_LIVETILE`: NugMon's graph, Eyes' eyes): the program is sent an update event, `GR_UPDATE_TILE` (subwid the tile, x, y on the screen), draws in the tile (it's its window, and its exposures go on to it), and `GR_UPDATE_UNTILE` when it's back. - `GR_WM_PROPS_GROWINSIDE`: no grow strip under the window; the window manager puts an input-only 16x16 grow box over the client's own bottom-right corner, and the client draws it there (FrogSurf, at the end of its status line) - the window manager's own XOR outlines are never taken for a client's (`nx_in_wm`): the end of a drag is often handled after the button's up, and its last outlines held every other client for ten seconds - a new window title repaints its frame's caption at once (an activation repaints one only when it goes active or inactive) - error messages to stderr - `nxfiles.c`: the file manager (our own; see the comment at its top): disk icons on the desktop (Nugget, the RAM disk /tmp, Mac floppies, the Trash), folder windows, double-click to open or run, drag to copy, move, delete or eject, Command-O/E/W. Mac floppies through libhfs (../hfs). Its icons are drawn from shapes in the code. Slow work shows a Lisa-style Wait box; starting a program shows the wristwatch until its window is up. - About (the triangle menu): `about.c`, a program of its own in nxapps (the desktop and menu bar go on while it's open; it moves and minimizes; its tile's icon `../icons/about-tile.png`): the Action Retro logo (`../icons/actionretro-logo.png`, 64x64; `mkwmicons.py` writes it into `aboutlogo.h`) at the left of what this Mac is and has. - Copy and Paste: `clip.h`, the clipboard NugEdit has always used (/tmp/.clipboard, a text file). Command-C, Command-V in Calculator (the number), Network's fields, FrogSurf (text dragged across on a page, or Command-A for all of it; else a link's address or the page's; Paste into the address bar or a form field) and its Save dialog's name. The Terminal: drag to select, Command-Shift-C and Command-Shift-V (Command is Control there: Control-C must stay an interrupt). A window asks for the button's release from the start, so that a press holds the mouse for it (moves and release coming to it). The menu bar's Edit (Copy, Paste) sends them to the program in front -- the window that had the keys as the menu opened -- as Command-Shift-C and -V (GrInjectKeyboardEvent, built in for Nugget though MW_FEATURE_TINY), as control characters, as the keyboard sends them (`cmdkey.h`: a Command key's letter, whichever way it comes), and gives it the keys back. - The server waits with epoll (`CONFIG_EPOLL` in the networking kernel), not select(): a key reached the screen ~15-20% sooner in MAME (`typekey`, a TOOLS=1 test tool, with MACPLUS_SHOTS_FAST), for 12 KB of RAM. - The menu bar (in nxfiles: the desktop shell): the triangle menu (About, Settings, Restart, Shut Down, Quit Desktop), Apps (the apps folder), Disk (eject, initialize or erase, a drive at a time), the network icon (an Ethernet socket, grey when neither eth0 nor sl0 is up; checked every 2 s while idle; a click opens Network) and the time, from the Plus's clock chip (read by the kernel at boot). nanowm keeps windows below it (WM_TOPY). Shut Down halts: the kernel then shows "You can now switch off your Macintosh." Restart jumps back into the ROM (as before; MAME doesn't come back from it, the real Plus is untested). When a Mac disk with network settings (`NugPrefs:Network`) is mounted and nothing's connected, nxfiles runs `network -auto` (once a session). nxfiles has a 16 KB stack: writing a new file onto a floppy (libhfs's B-tree inserts) overflowed 12 KB into the client library's request buffer; its icons and window table are allocated, not static, so it still starts from 32 KB. - FrogSurf's pictures: each in a window of its own (up to four), with a File menu (Save..., Close; Command-S, Command-W). Save writes MacPaint (576x720, PackBits) onto a Mac disk, typed PNTG/MPNT (the file manager's P request takes a fifth field, type and creator), or onto the RAM disk as NAME.pntg (the file manager types a .pntg copied to a Mac disk the same). Over a link, a button or a picture, the pointer's a hand (FrogSurf asks for mouse moves, at most every 100 ms). `FROGSURF_TEST_PICTURE` opens a test picture, for MAME without FrogFind. - `network.c`: Network (the menu bar's network icon, Settings): Ethernet (eth-up) or the serial port (SLIP: net-up), an address, the router (or the other end) and a name server, SLIP's speed; Connect, Disconnect, Save onto a Mac floppy (`NugPrefs:Network`, key=value lines, through nxfiles: `fmclient.c`). What's connected is read from the interfaces, the routing table and /tmp/resolv.conf. No DHCP (it needs CONFIG_PACKET, ~20 KB of kernel). - `settings.c`: Settings (the triangle menu): the date and time (double-click one to type it; set into the clock chip through the kernel's /proc/macplus_rtc, which reads it back too) and Network... - `nuginstall.c`: NuggetInstall (in Apps; `/bin/nuginstall`, as the shell's installer is `nuggetinstall`): copies Nugget Linux onto blank floppies, as the shell's does, in a window that shows both drives and says what goes where. It works with what's already in them (disks 1 and 2 in the two drives, a blank in one), copies drive to drive, or with one drive a part at a time with swaps, each swap checked (the disk copied is known by its first 4 KB, and told from the new copy by its last). It copies through what memory the desktop leaves (often under 100 KB: in parts), and has the file manager leave the drives alone meanwhile (U and M, as FloppySaver). It makes the two-disk set or the single (shell) disk, chosen at the start (a disk 1 already in a drive chooses). With one drive and too little memory (dozens of swaps), Close Desktop runs the shell's nuggetinstall in the desktop's place, with its memory: the file manager runs /tmp/.after-desktop once it's quit, then the desktop again. - `wallpaper.c`: Wallpaper (Settings' Wallpaper...): the desktop's pattern -- 32 to pick from, fat bits to draw in, a preview; Set Desktop, and Save ("NugPrefs:Desktop" on a Mac disk, put back by the file manager whenever that disk goes in, as Network's are). The file manager sets it (its K request): the server paints the root with it (GrSetDesktopPattern, a request of ours) and the file manager the desktop icons' backgrounds, lined up with the screen. - `eyes.c`: Eyes (our own, after xeyes; in Apps, and `xeyes`): two eyes that follow the pointer, drawn again only when a pupil moves, at most a few times a second (it took Clock's place in Apps). - `nxdoubles.c`: Doubles (our own sliding-tile game): equal tiles merge into their sum; arrow keys or mouse drags; in Apps. - `quitdesktop`: Quit Desktop: tells nxfiles to finish (it writes out the floppies' changes), after which `desktop` stops the server. - Fonts: the system font (menus, titles, dialogs) is Chicago 12, the Mac Plus ROM's own -- its only font, FONT 12 in the ROM's resources (the map 4 bytes past where the ROM header's offset at 0x1A points). The server copies its printable ASCII glyphs out of the ROM when the first font is made (microwindows drivers/genfont.c, nx_chicago_load): ~3 KB, in place of FreeSansSerif (6 KB in the server, 6 KB more in the RAM-held root). No such font in the ROM: the fixed font (X6x13, the Terminal's) instead. - Cursors from the ROM too (`romcursor.h`: its CURS 1-4, read where they are): the I-beam over text (NugEdit, the Terminal, Network's fields, FrogSurf's fields and address box) and the wristwatch while a program starts. Each program keeps a drawn one for a ROM without them. - `Makefile.nugget`: the build (after Microwindows' Makefile.elks): the server `nano-X` with the nanowm window manager built in, the file manager `nxfiles`, and `nxapps`: the other programs (nxterm, eyes, nxcalc, nugedit, nugmon, nxdoubles, FrogSurf; Landmine without networking) in one file, as BusyBox is (`nxapps.c` runs the one it's run as; /bin/nxterm and the rest link to it). They were a file each, each with its own C library and Nano-X client library: the root is held in RAM, so that cost ~40 KB a program, ~230 KB in all. Each program is linked on its own first, its names made local, so they can't clash; fmclient.o is linked once for them all. None keeps arrays in static data (every program's would be in each process, inside the 32 KB it starts in): they're in a struct allocated at the start, `V`, its fields #defined to the old names. Static data and bss are ~17 KB with an 8 KB stack, ~6.5 KB spare. All bFLT, linked against BusyBox's thread-free uClibc. /apps on the desktop disk links them under their own names (Terminal, Clock ...). - `mwconfig.nugget`: the Microwindows feature settings. - `m68kmem.S`: a 68000 `memcpy`, linked into every program ahead of uClibc's (whose generic one merges unaligned longwords with shifts). - `build-desktop.sh`: run by `userland/build-userland.sh` for the set; builds in `~/work/nanox` in the build container and installs into the desktop root. - `desktop`: the script that starts it all. Memory is what limits it, and the root filesystem is held in RAM, so every program on the disk costs its size whether it runs or not. With the server and the file manager running (a floppy mounted), about 410 KB is free in MAME (180 KB before nxapps; the kernel keeps only 48 KB in reserve, its tty buffers are 1 KB and TCP's metrics hash 16 slots; see the kernel patch). The kernel needs `CONFIG_UNIX` (the apps connect to the server at /tmp/.nano-X), `CONFIG_FB_DEVICE` (/dev/fb0, mapped without an MMU through macplusfb's own mmap) and `CONFIG_INPUT_MOUSEDEV` (/dev/input/mice, which adds up movement while the server is busy, where evdev would drop events): all in `macplus_net.config`, the set's kernel. Testing in MAME: `mame/guitest.sh` (boots image.bin, plays a mouse script, takes screenshots) and `mame/settest.sh` (boots the floppies). For timing, `mame/lastchange.py` says when the screenshots stopped changing; for profiles, build with `PROF=1`, dump /proc/pcprof to the serial port and summarise it with `mame/profsum.py` (it needs `System.map-set` and the programs' `.syms`, from nm). The profiler can't see work done in interrupts (the mouse, the pointer, the timer tick): `/proc/stat` and MAME's debugger (cycles between breakpoints, as `mame/tickcyc.sh`) can. `userland/tools/pingpong.c` (`TOOLS=1`) times round trips between two processes: about 20 ms over a pipe and 44 over a Unix socket (in MAME), which is what a Nano-X request with a reply mostly costs. (On the Mac Plus a pipe keeps no pages of its own, to save memory; all pipes share one spare.) Each event a client is sent costs one too, which is why nxfiles asks only for size updates (`GR_EVENT_MASK_UPDATE_SIZE`, a Nugget mask) and why the window manager's events are dealt with in the server. `userland/tools/selbench.c` (`TOOLS=1`) times a select() that doesn't wait: about 2.7 ms on a pipe, 5 ms with a tty too (a getppid() is 0.24 ms), which is what each of the server's waits costs. `NXFILES_BENCH=N desktop` opens and closes the Nugget window N times and prints how long each took to draw (about 0.48 s in MAME; `NXFILES_BENCH_DIR=/bin` for another folder; the clock it reads moves in 40 ms steps); with a `PROF=1` build, `MACPLUS_PROFDUMP_AT` (mame/profwin.lua) reads the kernel profiler's samples, gathered only while the windows drew, straight out of emulated memory.