diff --git a/palm-client/Makefile b/palm-client/Makefile index 181a498..1f7d6c2 100644 --- a/palm-client/Makefile +++ b/palm-client/Makefile @@ -3,6 +3,7 @@ # make build build/visormesh.prc (in Docker if the toolchain isn't installed) # make test run the host-side protocol tests # make install install the .prc over the USB cradle (press HotSync when asked) +# make shots fetch screenshots taken on the Visor into build/shots/ (HotSync) # make editor copy the SDK headers to .sdk/ and write compile_flags.txt for clangd # make clean @@ -10,7 +11,7 @@ APP = visormesh NAME = VisorMesh CREATOR = VMsh -SRCS = src/main.c src/app.c src/home_form.c src/convo_form.c src/node_form.c src/channel_form.c src/convos.c src/protocol.c src/sha256.c src/transport_srm.c src/transport_sb.c src/sb_spi.c src/session.c src/settings.c src/presets.c src/contacts.c src/contacts_form.c src/contact_form.c src/heard.c src/remote_fmt.c src/remote.c src/remote_form.c src/messages.c src/channels.c src/preview_form.c src/ui/draw.c src/ui/text.c src/ui/rowlist.c src/ui/fontmetrics.c src/ports.c src/sounds.c src/mutes.c src/scopes.c src/scope_proto.c src/map_geo.c src/map_form.c src/ui/emoji.c src/bgcheck.c src/pins.c +SRCS = src/main.c src/app.c src/home_form.c src/convo_form.c src/node_form.c src/channel_form.c src/convos.c src/protocol.c src/sha256.c src/transport_srm.c src/transport_sb.c src/sb_spi.c src/session.c src/settings.c src/presets.c src/contacts.c src/contacts_form.c src/contact_form.c src/heard.c src/remote_fmt.c src/remote.c src/remote_form.c src/messages.c src/channels.c src/preview_form.c src/ui/draw.c src/ui/text.c src/ui/rowlist.c src/ui/fontmetrics.c src/ports.c src/sounds.c src/mutes.c src/scopes.c src/scope_proto.c src/map_geo.c src/map_form.c src/ui/emoji.c src/bgcheck.c src/pins.c src/shots.c OBJS = $(SRCS:src/%.c=build/%.o) CC = m68k-palmos-gcc @@ -93,6 +94,16 @@ install: all @echo "Press the HotSync button on the Visor cradle..." $(PILOT_XFER) -p $(PORT) -i build/$(APP).prc +# Screenshots taken on the Visor (Options > Screenshot) into build/shots/ as PNGs. +# Press HotSync when asked; the database stays on the Visor (converted ones are skipped). +shots: | build + @if pgrep -f tools/visor_bridge.py >/dev/null; then \ + echo "Stopping visor_bridge.py (it would grab the Visor during HotSync)"; \ + pkill -f tools/visor_bridge.py; sleep 1; fi + @echo "Press the HotSync button on the Visor cradle..." + cd build && $(PILOT_XFER) -p $(PORT) -f VisorMeshShots + python3 tools/shots.py build/VisorMeshShots.pdb build/shots + # The basemap (tools/make_map.py; OSM data cached in tools/map-cache). `make map` # downloads on the first run; MAP_ARGS passes options, e.g. MAP_ARGS="--max-zoom 12". map: | build @@ -118,4 +129,4 @@ editor: image -Wno-ignored-attributes -Wno-pragma-once-outside-header > compile_flags.txt find .sdk/sdk-3.5 -type d | sed 's|^|-I|' >> compile_flags.txt -.PHONY: all test image install clean editor map install-map +.PHONY: all test image install clean editor map install-map shots diff --git a/palm-client/resources/visormesh.rcp b/palm-client/resources/visormesh.rcp index 1c75466..3419c2d 100644 --- a/palm-client/resources/visormesh.rcp +++ b/palm-client/resources/visormesh.rcp @@ -33,6 +33,8 @@ BEGIN MENUITEM "-" AUTOID MENUITEM "Serial Ports..." ID MainMenuPorts "P" MENUITEM "Font Preview" ID MainMenuFontPreview "F" + MENUITEM "Screenshot" ID MainMenuShot "S" + MENUITEM "Screenshot in 5 s" ID MainMenuShotLater "D" END END @@ -56,6 +58,8 @@ BEGIN MENUITEM "-" AUTOID MENUITEM "Serial Ports..." ID MainMenuPorts "P" MENUITEM "Font Preview" ID MainMenuFontPreview "F" + MENUITEM "Screenshot" ID MainMenuShot "S" + MENUITEM "Screenshot in 5 s" ID MainMenuShotLater "D" END END @@ -83,6 +87,8 @@ BEGIN MENUITEM "-" AUTOID MENUITEM "Serial Ports..." ID MainMenuPorts "P" MENUITEM "Font Preview" ID MainMenuFontPreview "F" + MENUITEM "Screenshot" ID MainMenuShot "S" + MENUITEM "Screenshot in 5 s" ID MainMenuShotLater "D" END END diff --git a/palm-client/src/main.c b/palm-client/src/main.c index 250575b..edc2128 100644 --- a/palm-client/src/main.c +++ b/palm-client/src/main.c @@ -22,6 +22,7 @@ #include "preview_form.h" #include "channel_form.h" #include "bgcheck.h" +#include "shots.h" #include "ui/draw.h" #define VM_MIN_ROM_VERSION sysMakeROMVersion(3, 5, 0, sysROMStageRelease, 0) @@ -57,6 +58,8 @@ static Boolean HandleMenu(UInt16 itemID) case MainMenuMuteChannel: ChannelMuteToggleCurrent(); return true; case MainMenuChannelScope: ChannelScopeChooseCurrent(); return true; case MainMenuScopes: ScopesFormOpen(FrmGetActiveFormID()); return true; + case MainMenuShot: ShotCapture(); return true; /* the menu has gone by now */ + case MainMenuShotLater: ShotLater(5); return true; } return false; } @@ -207,6 +210,7 @@ UInt32 PilotMain(UInt16 cmd, MemPtr cmdPBP, UInt16 launchFlags) ChannelsClose(); MessagesClose(); UiFontRelease(); + ShotCancel(); BgSetAppOpen(false); BgScheduleIfIdle(); } diff --git a/palm-client/src/resources.h b/palm-client/src/resources.h index 086ab31..3f2251c 100644 --- a/palm-client/src/resources.h +++ b/palm-client/src/resources.h @@ -28,6 +28,8 @@ #define MainMenuScopes 1011 #define MainMenuMap 1012 #define HomeMenuBar 1013 /* Go, Channel (Add), Options */ +#define MainMenuShot 1015 +#define MainMenuShotLater 1016 #define ConvoChanMenuBar 1014 /* Go, Channel (Add, Leave, Mute, Scope), Options */ #define MainLinkGroup 1 diff --git a/palm-client/src/shots.c b/palm-client/src/shots.c new file mode 100644 index 0000000..35c4841 --- /dev/null +++ b/palm-client/src/shots.c @@ -0,0 +1,64 @@ +#include +#include "app.h" +#include "shots.h" + +#define SHOTS_DB_TYPE 'shot' + +static void ShotAlarm(UInt16 cmd, SysAlarmTriggeredParamType *p) +{ + if (cmd == almProcCmdTriggered) + ShotCapture(); +} + +void ShotCapture(void) +{ + BitmapType *bmp = WinGetBitmap(WinGetDisplayWindow()); + ShotHeader hdr; + UInt32 bits; + UInt16 index = dmMaxRecordIndex; + LocalID id; + DmOpenRef db; + MemHandle h; + void *p; + + if (!bmp) + return; + hdr.width = (UInt16)bmp->width; + hdr.height = (UInt16)bmp->height; + hdr.rowBytes = bmp->rowBytes; + hdr.pixelSize = bmp->pixelSize; + hdr.reserved = 0; + hdr.time = TimGetSeconds(); + bits = (UInt32)hdr.rowBytes * hdr.height; + + id = DmFindDatabase(0, "VisorMeshShots"); + if (!id && DmCreateDatabase(0, "VisorMeshShots", VM_CREATOR, SHOTS_DB_TYPE, false) == errNone) + id = DmFindDatabase(0, "VisorMeshShots"); + db = id ? DmOpenDatabase(0, id, dmModeReadWrite) : 0; + if (!db) { + SndPlaySystemSound(sndError); + return; + } + h = DmNewRecord(db, &index, sizeof(hdr) + bits); + if (h) { + p = MemHandleLock(h); + DmWrite(p, 0, &hdr, sizeof(hdr)); + DmWrite(p, sizeof(hdr), BmpGetBits(bmp), bits); + MemHandleUnlock(h); + DmReleaseRecord(db, index, true); + SndPlaySystemSound(sndClick); + } else { + SndPlaySystemSound(sndError); + } + DmCloseDatabase(db); +} + +void ShotLater(UInt16 seconds) +{ + AlmSetProcAlarm(ShotAlarm, 0, TimGetSeconds() + seconds); +} + +void ShotCancel(void) +{ + AlmSetProcAlarm(ShotAlarm, 0, 0); +} diff --git a/palm-client/src/shots.h b/palm-client/src/shots.h new file mode 100644 index 0000000..fd5bd86 --- /dev/null +++ b/palm-client/src/shots.h @@ -0,0 +1,31 @@ +/* + * Screenshots (Options > Screenshot): copies the screen into a record of the + * VisorMeshShots database. `make shots` fetches it over HotSync and + * tools/shots.py turns the records into PNGs. A click means it was saved. + * + * All of this is in the first segment and uses no globals: the delayed + * capture runs from a procedure alarm, inside whatever event loop is running + * then (an open menu, a popup list), which is the point of it. + */ +#ifndef VM_SHOTS_H +#define VM_SHOTS_H + +#include + +/* Record layout: this header (68k byte order), then rowBytes * height bytes of screen bits. */ +typedef struct { + UInt16 width; + UInt16 height; + UInt16 rowBytes; + UInt8 pixelSize; /* bits per pixel: 4 on the Neo (0 = white, 15 = black) */ + UInt8 reserved; + UInt32 time; /* TimGetSeconds: local time, seconds since 1904 */ +} ShotHeader; + +void ShotCapture(void); +/* Captures in about 'seconds' (the Alarm Manager counts whole seconds). */ +void ShotLater(UInt16 seconds); +/* Cancels a pending ShotLater: the alarm calls into the app's code, so before the app exits. */ +void ShotCancel(void); + +#endif diff --git a/palm-client/tools/shots.py b/palm-client/tools/shots.py new file mode 100755 index 0000000..cadc473 --- /dev/null +++ b/palm-client/tools/shots.py @@ -0,0 +1,81 @@ +#!/usr/bin/env python3 +"""Turns the Visor's VisorMeshShots database into PNGs. + + tools/shots.py build/VisorMeshShots.pdb [out_dir] (default: build/shots) + +`make shots` fetches the database over HotSync and runs this. Each record is a +ShotHeader (src/shots.h, big-endian) followed by the screen bits, rows of +rowBytes, pixels packed most significant bits first, 0 = white. Files are +named by the capture time (the Visor's local clock); existing ones are kept. +""" +import datetime +import os +import struct +import sys +import zlib + +HEADER = struct.Struct(">HHHBBI") +PALM_EPOCH = datetime.datetime(1904, 1, 1) + + +def records(pdb): + count = struct.unpack_from(">H", pdb, 76)[0] + offsets = [struct.unpack_from(">I", pdb, 78 + 8 * i)[0] for i in range(count)] + for i, start in enumerate(offsets): + end = offsets[i + 1] if i + 1 < count else len(pdb) + yield pdb[start:end] + + +def gray_rows(rec): + width, height, row_bytes, depth, _, when = HEADER.unpack_from(rec) + if depth not in (1, 2, 4, 8): + raise ValueError(f"unsupported depth {depth}") + bits = rec[HEADER.size:] + top = (1 << depth) - 1 + rows = [] + for y in range(height): + row = bits[y * row_bytes:(y + 1) * row_bytes] + out = bytearray() + for x in range(width): + bit = x * depth + v = (row[bit // 8] >> (8 - depth - bit % 8)) & top + out.append(255 - v * 255 // top) # 8-bit is palette-indexed: this is only an approximation + rows.append(bytes(out)) + return width, height, when, rows + + +def png(width, height, rows): + def chunk(kind, data): + body = kind + data + return struct.pack(">I", len(data)) + body + struct.pack(">I", zlib.crc32(body)) + raw = b"".join(b"\0" + r for r in rows) + return (b"\x89PNG\r\n\x1a\n" + chunk(b"IHDR", struct.pack(">IIBBBBB", width, height, 8, 0, 0, 0, 0)) + + chunk(b"IDAT", zlib.compress(raw, 9)) + chunk(b"IEND", b"")) + + +def main(): + if len(sys.argv) not in (2, 3): + sys.exit(__doc__) + out_dir = sys.argv[2] if len(sys.argv) == 3 else "build/shots" + os.makedirs(out_dir, exist_ok=True) + with open(sys.argv[1], "rb") as f: + pdb = f.read() + written = 0 + seen = {} + for rec in records(pdb): + width, height, when, rows = gray_rows(rec) + stamp = (PALM_EPOCH + datetime.timedelta(seconds=when)).strftime("%Y%m%d-%H%M%S") + n = seen.get(stamp, 0) # two in one second: -1, -2, ... in record order + seen[stamp] = n + 1 + name = os.path.join(out_dir, f"visormesh-{stamp}{f'-{n}' if n else ''}.png") + if os.path.exists(name): + continue # converted on an earlier run + with open(name, "wb") as f: + f.write(png(width, height, rows)) + written += 1 + print(name) + print(f"{written} screenshot(s) in {out_dir}") + + +if __name__ == "__main__": + main()