From 060534910112a8f719d13fbf2f167a8268d35075 Mon Sep 17 00:00:00 2001 From: Alsey Coleman Miller Date: Tue, 1 Dec 2026 23:17:36 -0500 Subject: [PATCH 1/6] test: add a Process Manager capture probe --- .../Retro68/ProcessManagerCapture.c | 211 ++++++++++++++++++ 1 file changed, 211 insertions(+) create mode 100644 ReferenceTests/Retro68/ProcessManagerCapture.c diff --git a/ReferenceTests/Retro68/ProcessManagerCapture.c b/ReferenceTests/Retro68/ProcessManagerCapture.c new file mode 100644 index 0000000..345782c --- /dev/null +++ b/ReferenceTests/Retro68/ProcessManagerCapture.c @@ -0,0 +1,211 @@ +/* + * Captures the Process Manager's answers about the running processes, + * writing "ProcessManager.txt" next to itself. It refuses to run if that + * file is already there. Each line is a call and what it returned; the file + * is flushed after every line. + * + * It records the program's own process serial number and information, the + * list GetNextProcess walks, the front process, SameProcess on the special + * serial numbers, and the errors for numbers that name no process. Values + * that differ from run to run (the launch date, active time, sizes and + * free memory) are recorded as whether they are zero, not as numbers. + */ +#include +#include +#include +#include +#include + +/* Retro68's headers leave GetFrontProcess out; InterfaceLib has it. */ +extern OSErr GetFrontProcess(ProcessSerialNumber *PSN); + +static short out; + +static void line(const char *text) +{ + long count = strlen(text); + FSWrite(out, &count, text); + count = 1; + FSWrite(out, &count, "\r"); + FlushVol(0, 0); +} + +static void psnText(char *text, const ProcessSerialNumber *psn) +{ + sprintf(text, "%08lX:%08lX", (unsigned long)psn->highLongOfPSN, (unsigned long)psn->lowLongOfPSN); +} + +static void code(unsigned long value, char *text) +{ + text[0] = '\''; + text[1] = (char)(value >> 24); + text[2] = (char)(value >> 16); + text[3] = (char)(value >> 8); + text[4] = (char)value; + text[5] = '\''; + text[6] = 0; +} + +static void information(const char *label, ProcessSerialNumber psn) +{ + char text[400], number[32], type[8], signature[8], launcher[32]; + Str255 name; + FSSpec spec; + ProcessInfoRec info; + OSErr error; + memset(&info, 0, sizeof info); + memset(&spec, 0, sizeof spec); + name[0] = 0; + info.processInfoLength = sizeof info; + info.processName = name; + info.processAppSpec = &spec; + error = GetProcessInformation(&psn, &info); + psnText(number, &psn); + if (error) { + sprintf(text, "%s %s: GetProcessInformation -> %d", label, number, error); + line(text); + return; + } + code(info.processType, type); + code(info.processSignature, signature); + psnText(launcher, &info.processLauncher); + sprintf(text, "%s %s: name \"%.*s\" type %s signature %s mode %08lX length %lu", + label, number, name[0], (char *)name + 1, type, signature, + (unsigned long)info.processMode, (unsigned long)info.processInfoLength); + line(text); + sprintf(text, "%s %s: location %s size %s free %s launcher %s launchDate %s activeTime %s", + label, number, info.processLocation ? "set" : "nil", info.processSize ? "nonzero" : "zero", + info.processFreeMem ? "nonzero" : "zero", launcher, + info.processLaunchDate ? "nonzero" : "zero", info.processActiveTime ? "nonzero" : "zero"); + line(text); + sprintf(text, "%s %s: spec vRefNum %s parID %s name \"%.*s\"", label, number, + spec.vRefNum ? "set" : "zero", spec.parID ? "set" : "zero", spec.name[0], (char *)spec.name + 1); + line(text); +} + +static void same(const char *label, ProcessSerialNumber a, ProcessSerialNumber b) +{ + char text[160], first[32], second[32]; + Boolean result = 99; + OSErr error = SameProcess(&a, &b, &result); + psnText(first, &a); + psnText(second, &b); + sprintf(text, "SameProcess %s %s %s -> %d result %d", label, first, second, error, (int)result); + line(text); +} + +int main(void) +{ + ProcessSerialNumber self, psn, front, none, current, system, bogus; + char text[200], number[32]; + OSErr error; + int count = 0; + short volume; + Str255 name; + FSSpec spec; + ProcessInfoRec info; + + /* Created beside the program; refuse to overwrite an earlier capture. */ + error = FSOpen("\pProcessManager.txt", 0, &volume); + if (error == noErr) { + FSClose(volume); + return 1; + } + error = Create("\pProcessManager.txt", 0, 'ttxt', 'TEXT'); + if (error != noErr) return 1; + if (FSOpen("\pProcessManager.txt", 0, &out) != noErr) return 1; + + none.highLongOfPSN = 0; + none.lowLongOfPSN = kNoProcess; + system.highLongOfPSN = 0; + system.lowLongOfPSN = kSystemProcess; + current.highLongOfPSN = 0; + current.lowLongOfPSN = kCurrentProcess; + bogus.highLongOfPSN = 0x1234; + bogus.lowLongOfPSN = 0x5678; + + error = GetCurrentProcess(&self); + psnText(number, &self); + sprintf(text, "GetCurrentProcess -> %d %s", error, number); + line(text); + information("self", self); + + error = GetFrontProcess(&front); + psnText(number, &front); + sprintf(text, "GetFrontProcess -> %d %s", error, number); + line(text); + same("self front", self, front); + + /* The list, in the order GetNextProcess gives it. */ + psn = none; + for (;;) { + ProcessSerialNumber next; + error = GetNextProcess(&psn); + psnText(number, &psn); + sprintf(text, "GetNextProcess[%d] -> %d %s", count, error, number); + line(text); + if (error) break; + next = psn; + information("listed", next); + if (++count > 64) break; + } + + /* Special serial numbers. */ + information("kCurrentProcess", current); + information("kSystemProcess", system); + information("kNoProcess", none); + information("bogus", bogus); + same("self self", self, self); + same("self kCurrentProcess", self, current); + same("kCurrentProcess kCurrentProcess", current, current); + same("self kSystemProcess", self, system); + same("self kNoProcess", self, none); + same("self bogus", self, bogus); + same("bogus bogus", bogus, bogus); + + psn = bogus; + error = GetNextProcess(&psn); + psnText(number, &psn); + sprintf(text, "GetNextProcess bogus -> %d %s", error, number); + line(text); + + psn = current; + error = GetNextProcess(&psn); + psnText(number, &psn); + sprintf(text, "GetNextProcess kCurrentProcess -> %d %s", error, number); + line(text); + + /* A record too short, and one that asks for no name or spec. */ + memset(&info, 0, sizeof info); + info.processInfoLength = sizeof info; + error = GetProcessInformation(&self, &info); + sprintf(text, "GetProcessInformation without name or spec -> %d length %lu", error, + (unsigned long)info.processInfoLength); + line(text); + memset(&info, 0, sizeof info); + memset(&spec, 0, sizeof spec); + name[0] = 0; + info.processInfoLength = 4; + info.processName = name; + info.processAppSpec = &spec; + error = GetProcessInformation(&self, &info); + sprintf(text, "GetProcessInformation with length 4 -> %d name %d", error, name[0]); + line(text); + + error = SetFrontProcess(&self); + sprintf(text, "SetFrontProcess self -> %d", error); + line(text); + error = SetFrontProcess(&bogus); + sprintf(text, "SetFrontProcess bogus -> %d", error); + line(text); + error = GetFrontProcess(&front); + psnText(number, &front); + sprintf(text, "GetFrontProcess after -> %d %s", error, number); + line(text); + same("self front after", self, front); + + line("done"); + FSClose(out); + FlushVol(0, 0); + return 0; +} From eb84afba8da6870a5df133a721b6acf7b3a07d2c Mon Sep 17 00:00:00 2001 From: Alsey Coleman Miller Date: Tue, 1 Dec 2026 23:17:36 -0500 Subject: [PATCH 2/6] build: build the Process Manager capture --- ReferenceTests/Retro68/CMakeLists.txt | 1 + 1 file changed, 1 insertion(+) diff --git a/ReferenceTests/Retro68/CMakeLists.txt b/ReferenceTests/Retro68/CMakeLists.txt index 9b5eaa2..59a14a5 100644 --- a/ReferenceTests/Retro68/CMakeLists.txt +++ b/ReferenceTests/Retro68/CMakeLists.txt @@ -9,3 +9,4 @@ add_application(HighLevelEventCapture HighLevelEventCapture.c HighLevelEventCapt add_application(MemoryManagerCapture MemoryManagerCapture.c) add_application(MixedModeCapture MixedModeCapture.c) add_application(RegionRecordingCapture RegionRecordingCapture.c) +add_application(ProcessManagerCapture ProcessManagerCapture.c) From f30321298ffc4bf8b9f94bdb033c7d607abe5517 Mon Sep 17 00:00:00 2001 From: Alsey Coleman Miller Date: Tue, 1 Dec 2026 23:17:36 -0500 Subject: [PATCH 3/6] test: Process Manager capture from Mac OS 9.2.2 --- ReferenceTests/Retro68/ProcessManager-macos-9.2.2-ppc.txt | 1 + 1 file changed, 1 insertion(+) create mode 100644 ReferenceTests/Retro68/ProcessManager-macos-9.2.2-ppc.txt diff --git a/ReferenceTests/Retro68/ProcessManager-macos-9.2.2-ppc.txt b/ReferenceTests/Retro68/ProcessManager-macos-9.2.2-ppc.txt new file mode 100644 index 0000000..188ef66 --- /dev/null +++ b/ReferenceTests/Retro68/ProcessManager-macos-9.2.2-ppc.txt @@ -0,0 +1 @@ +GetCurrentProcess -> 0 00000000:02270001 self 00000000:02270001: name "ProcessManagerCapture" type 'APPL' signature '????' mode 00000080 length 60 self 00000000:02270001: location set size nonzero free nonzero launcher 00000000:01F20001 launchDate nonzero activeTime zero self 00000000:02270001: spec vRefNum set parID set name "ProcessManagerCapture" GetFrontProcess -> 0 00000000:01F20001 SameProcess self front 00000000:02270001 00000000:01F20001 -> 0 result 0 GetNextProcess[0] -> 0 00000000:01EA0001 listed 00000000:01EA0001: name "Application Switcher" type 'appe' signature 'apsw' mode 00005CE4 length 60 listed 00000000:01EA0001: location set size nonzero free nonzero launcher 00000000:00000001 launchDate nonzero activeTime nonzero listed 00000000:01EA0001: spec vRefNum set parID set name "Application Switcher" GetNextProcess[1] -> 0 00000000:01EC0001 listed 00000000:01EC0001: name "Control Strip Extension" type 'appe' signature 'sdev' mode 00005CC8 length 60 listed 00000000:01EC0001: location set size nonzero free nonzero launcher 00000000:00000001 launchDate nonzero activeTime nonzero listed 00000000:01EC0001: spec vRefNum set parID set name "Control Strip Extension" GetNextProcess[2] -> 0 00000000:01EE0001 listed 00000000:01EE0001: name "DVD AutoLauncher" type 'appe' signature 'dval' mode 00005DE0 length 60 listed 00000000:01EE0001: location set size nonzero free nonzero launcher 00000000:00000001 launchDate nonzero activeTime nonzero listed 00000000:01EE0001: spec vRefNum set parID set name "DVD AutoLauncher" GetNextProcess[3] -> 0 00000000:01EF0001 listed 00000000:01EF0001: name "FBC Indexing Scheduler" type 'appe' signature 'fbcb' mode 000014E0 length 60 listed 00000000:01EF0001: location set size nonzero free nonzero launcher 00000000:00000001 launchDate nonzero activeTime nonzero listed 00000000:01EF0001: spec vRefNum set parID set name "FBC Indexing Scheduler" GetNextProcess[4] -> 0 00000000:01F00001 listed 00000000:01F00001: name "Folder Actions" type 'appe' signature 'ssrv' mode 00005CC0 length 60 listed 00000000:01F00001: location set size nonzero free nonzero launcher 00000000:00000001 launchDate nonzero activeTime nonzero listed 00000000:01F00001: spec vRefNum set parID set name "Folder Actions" GetNextProcess[5] -> 0 00000000:01F20001 listed 00000000:01F20001: name "Finder" type 'FNDR' signature 'MACS' mode 00017BEC length 60 listed 00000000:01F20001: location set size nonzero free nonzero launcher 00000000:00000000 launchDate nonzero activeTime nonzero listed 00000000:01F20001: spec vRefNum set parID set name "Finder" GetNextProcess[6] -> 0 00000000:02040002 listed 00000000:02040002: name "File Sharing Extension" type 'INIT' signature 'hhgg' mode 00043EC0 length 60 listed 00000000:02040002: location set size nonzero free nonzero launcher 00000000:01F20001 launchDate nonzero activeTime nonzero listed 00000000:02040002: spec vRefNum set parID set name "File Sharing Extension" GetNextProcess[7] -> 0 00000000:02270001 listed 00000000:02270001: name "ProcessManagerCapture" type 'APPL' signature '????' mode 00000080 length 60 listed 00000000:02270001: location set size nonzero free nonzero launcher 00000000:01F20001 launchDate nonzero activeTime zero listed 00000000:02270001: spec vRefNum set parID set name "ProcessManagerCapture" GetNextProcess[8] -> -600 00000000:00000000 kCurrentProcess 00000000:00000002: name "ProcessManagerCapture" type 'APPL' signature '????' mode 00000080 length 60 kCurrentProcess 00000000:00000002: location set size nonzero free nonzero launcher 00000000:01F20001 launchDate nonzero activeTime zero kCurrentProcess 00000000:00000002: spec vRefNum set parID set name "ProcessManagerCapture" kSystemProcess 00000000:00000001: GetProcessInformation -> -600 kNoProcess 00000000:00000000: GetProcessInformation -> -600 bogus 00001234:00005678: GetProcessInformation -> -600 SameProcess self self 00000000:02270001 00000000:02270001 -> 0 result 1 SameProcess self kCurrentProcess 00000000:02270001 00000000:00000002 -> 0 result 1 SameProcess kCurrentProcess kCurrentProcess 00000000:00000002 00000000:00000002 -> 0 result 1 SameProcess self kSystemProcess 00000000:02270001 00000000:00000001 -> 0 result 0 SameProcess self kNoProcess 00000000:02270001 00000000:00000000 -> 0 result 0 SameProcess self bogus 00000000:02270001 00001234:00005678 -> 0 result 0 SameProcess bogus bogus 00001234:00005678 00001234:00005678 -> 0 result 1 GetNextProcess bogus -> -600 00000000:00000000 GetNextProcess kCurrentProcess -> -600 00000000:00000000 GetProcessInformation without name or spec -> 0 length 60 GetProcessInformation with length 4 -> -50 name 0 SetFrontProcess self -> 0 SetFrontProcess bogus -> -600 GetFrontProcess after -> 0 00000000:01F20001 SameProcess self front after 00000000:02270001 00000000:01F20001 -> 0 result 0 done \ No newline at end of file From 71598b192fe64e99f927b3be45634afdf0e74735 Mon Sep 17 00:00:00 2001 From: Alsey Coleman Miller Date: Tue, 1 Dec 2026 23:17:36 -0500 Subject: [PATCH 4/6] feat: add the Process Manager's process list --- Sources/GraphiteCore/ProcessManager.swift | 141 ++++++++++++++++++++++ 1 file changed, 141 insertions(+) create mode 100644 Sources/GraphiteCore/ProcessManager.swift diff --git a/Sources/GraphiteCore/ProcessManager.swift b/Sources/GraphiteCore/ProcessManager.swift new file mode 100644 index 0000000..57ffa27 --- /dev/null +++ b/Sources/GraphiteCore/ProcessManager.swift @@ -0,0 +1,141 @@ +// The Process Manager's answers about the programs running: who is running, +// which is the current process and which is in front. The host fills the list +// in; Graphite answers GetCurrentProcess, GetFrontProcess, GetNextProcess, +// GetProcessInformation, SameProcess and SetFrontProcess as Mac OS 9.2.2 did +// (ReferenceTests/Retro68/ProcessManager-macos-9.2.2-ppc.txt). + +/// A process serial number: a process's name for as long as it runs. +public struct ProcessSerialNumber: Hashable, Sendable { + + public var high: UInt32 + public var low: UInt32 + + public init(high: UInt32 = 0, low: UInt32) { + self.high = high + self.low = low + } + + /// kNoProcess. + public static var none: ProcessSerialNumber { ProcessSerialNumber(low: 0) } + /// kSystemProcess. + public static var system: ProcessSerialNumber { ProcessSerialNumber(low: 1) } + /// kCurrentProcess: the process that asks. + public static var current: ProcessSerialNumber { ProcessSerialNumber(low: 2) } +} + +/// What GetProcessInformation says about a process: ProcessInfoRec's fields. +public struct ProcessRecord: Sendable { + + public var name: PascalString + public var number: ProcessSerialNumber + /// processType: 'APPL' for an application, 'appe' for a background-only one. + public var type: FourCharCode + public var signature: FourCharCode + /// processMode: the SIZE resource's flags and the process's state. + public var mode: UInt32 + /// processLocation: where its partition begins. + public var location: UInt32 + public var size: UInt32 + public var freeMemory: UInt32 + /// processLauncher: the process that opened it; kNoProcess for none. + public var launcher: ProcessSerialNumber + /// processLaunchDate: ticks, when it was opened. + public var launchDate: UInt32 + /// processActiveTime: ticks it has run for; zero before it first yields. + public var activeTime: UInt32 + /// processAppSpec: the application's FSSpec, in parts. + public var applicationVolume: Int16 + public var applicationParent: Int32 + public var applicationName: PascalString + + public init(name: PascalString, number: ProcessSerialNumber, type: FourCharCode, signature: FourCharCode, + mode: UInt32, location: UInt32, size: UInt32, freeMemory: UInt32, + launcher: ProcessSerialNumber = .none, launchDate: UInt32, activeTime: UInt32 = 0, + applicationVolume: Int16, applicationParent: Int32, applicationName: PascalString? = nil) { + self.name = name + self.number = number + self.type = type + self.signature = signature + self.mode = mode + self.location = location + self.size = size + self.freeMemory = freeMemory + self.launcher = launcher + self.launchDate = launchDate + self.activeTime = activeTime + self.applicationVolume = applicationVolume + self.applicationParent = applicationParent + self.applicationName = applicationName ?? name + } +} + +/// The running processes, in the order GetNextProcess walks them. +public struct ProcessManager: Sendable { + + /// sizeof(ProcessInfoRec): the length GetProcessInformation expects in + /// processInfoLength. A shorter one was paramErr. + public static var informationRecordLength: UInt32 { 60 } + + public private(set) var processes: [ProcessRecord] + /// The process that calls: what kCurrentProcess names. + public let current: ProcessSerialNumber + public private(set) var front: ProcessSerialNumber + + /// `processes` in launch order, `current` one of them and `front` the process in front. + public init(processes: [ProcessRecord], current: ProcessSerialNumber, front: ProcessSerialNumber) { + self.processes = processes + self.current = current + self.front = front + } + + /// GetCurrentProcess. + public func currentProcess() -> ProcessSerialNumber { current } + + /// GetFrontProcess. + public func frontProcess() -> ProcessSerialNumber { front } + + /// What a serial number names: kCurrentProcess is the caller; the other + /// special numbers and numbers nothing is running under name no process. + private func resolved(_ psn: ProcessSerialNumber) -> ProcessRecord? { + let number = psn == .current ? current : psn + return processes.first { $0.number == number } + } + + /// GetNextProcess: the process after `psn`, kNoProcess giving the first. + /// The end of the list, and a number that names no process (kCurrentProcess + /// too), is procNotFound with kNoProcess left in the serial number. + public func nextProcess(after psn: ProcessSerialNumber) throws(MacError) -> ProcessSerialNumber { + if psn == .none { if let first = processes.first { return first.number } else { throw .processNotFound } } + guard let index = processes.firstIndex(where: { $0.number == psn }), index + 1 < processes.count else { + throw .processNotFound + } + return processes[index + 1].number + } + + /// GetProcessInformation: procNotFound for kSystemProcess, kNoProcess and + /// numbers that name no process; paramErr for a record of another length. + public func information(for psn: ProcessSerialNumber, recordLength: UInt32 = informationRecordLength) throws(MacError) -> ProcessRecord { + guard recordLength == Self.informationRecordLength else { throw .parameter } + guard let record = resolved(psn) else { throw .processNotFound } + return record + } + + /// SameProcess: whether two serial numbers name one process. It is never + /// an error, and two numbers that name no process are the same if equal. + public func same(_ a: ProcessSerialNumber, _ b: ProcessSerialNumber) -> Bool { + (a == .current ? current : a) == (b == .current ? current : b) + } + + /// SetFrontProcess: procNotFound for a process that isn't running. A + /// process asked to come forward doesn't until it next calls the Event + /// Manager, so the front process was still the Finder just after. + public mutating func setFrontProcess(_ psn: ProcessSerialNumber) throws(MacError) { + guard resolved(psn) != nil else { throw .processNotFound } + } + + /// The process comes to the front, as it does once it has asked and run its event loop. + public mutating func bringToFront(_ psn: ProcessSerialNumber) throws(MacError) { + guard let record = resolved(psn) else { throw .processNotFound } + front = record.number + } +} From ab8c7411e04a49533b84f6a81cb0a3566d660c8c Mon Sep 17 00:00:00 2001 From: Alsey Coleman Miller Date: Tue, 1 Dec 2026 23:17:36 -0500 Subject: [PATCH 5/6] test: check the Process Manager against the Mac OS 9.2.2 capture --- .../ProcessManagerTests.swift | 135 ++++++++++++++++++ 1 file changed, 135 insertions(+) create mode 100644 Tests/GraphiteCoreTests/ProcessManagerTests.swift diff --git a/Tests/GraphiteCoreTests/ProcessManagerTests.swift b/Tests/GraphiteCoreTests/ProcessManagerTests.swift new file mode 100644 index 0000000..0e3b526 --- /dev/null +++ b/Tests/GraphiteCoreTests/ProcessManagerTests.swift @@ -0,0 +1,135 @@ +import Foundation +import Testing +@testable import GraphiteCore + +// Foundation on macOS has types of these names. +private typealias ProcessSerialNumber = GraphiteCore.ProcessSerialNumber +private typealias FourCharCode = GraphiteCore.FourCharCode + +/// Lines of the Mac OS 9.2.2 capture (ReferenceTests/Retro68, +/// ProcessManager-macos-9.2.2-ppc.txt). +private let capture: [String] = { + let root = URL(fileURLWithPath: #filePath).deletingLastPathComponent() + .deletingLastPathComponent().deletingLastPathComponent() + let data = try! Data(contentsOf: root.appendingPathComponent("ReferenceTests/Retro68/ProcessManager-macos-9.2.2-ppc.txt")) + return String(decoding: data, as: UTF8.self).split(separator: "\r").map(String.init) +}() + +private func hex8(_ value: UInt32) -> String { + let text = String(value, radix: 16, uppercase: true) + return String(repeating: "0", count: 8 - text.count) + text +} + +private func text(_ psn: ProcessSerialNumber) -> String { "\(hex8(psn.high)):\(hex8(psn.low))" } +private func code(_ value: FourCharCode) -> String { "'\(value.string)'" } + +/// The processes the capture found running, as the Process Manager listed them. +private func capturedManager() -> ProcessManager { + func process(_ name: String, _ number: UInt32, _ type: FourCharCode, _ signature: FourCharCode, _ mode: UInt32, + launcher: ProcessSerialNumber = .system, active: UInt32 = 1) -> ProcessRecord { + ProcessRecord(name: PascalString(name)!, number: ProcessSerialNumber(low: number), type: type, signature: signature, + mode: mode, location: 0x1000, size: 0x10_0000, freeMemory: 0x1000, launcher: launcher, + launchDate: 1, activeTime: active, applicationVolume: -1, applicationParent: 2) + } + let finder = ProcessSerialNumber(low: 0x01F2_0001) + return ProcessManager(processes: [ + process("Application Switcher", 0x01EA_0001, "appe", "apsw", 0x5CE4), + process("Control Strip Extension", 0x01EC_0001, "appe", "sdev", 0x5CC8), + process("DVD AutoLauncher", 0x01EE_0001, "appe", "dval", 0x5DE0), + process("FBC Indexing Scheduler", 0x01EF_0001, "appe", "fbcb", 0x14E0), + process("Folder Actions", 0x01F0_0001, "appe", "ssrv", 0x5CC0), + process("Finder", 0x01F2_0001, "FNDR", "MACS", 0x17BEC, launcher: .none), + process("File Sharing Extension", 0x0204_0002, "INIT", "hhgg", 0x43EC0, launcher: finder), + process("ProcessManagerCapture", 0x0227_0001, "APPL", "????", 0x80, launcher: finder, active: 0), + ], current: ProcessSerialNumber(low: 0x0227_0001), front: finder) +} + +/// What the probe wrote for GetProcessInformation, line by line. +private func informationLines(_ label: String, _ psn: ProcessSerialNumber, _ manager: ProcessManager) -> [String] { + let number = text(psn) + guard let info = try? manager.information(for: psn) else { + return ["\(label) \(number): GetProcessInformation -> -600"] + } + let tag = "\(label) \(number)" + return [ + "\(tag): name \"\(info.name.string)\" type \(code(info.type)) signature \(code(info.signature)) mode \(hex8(info.mode)) length 60", + "\(tag): location \(info.location != 0 ? "set" : "nil") size \(info.size != 0 ? "nonzero" : "zero") " + + "free \(info.freeMemory != 0 ? "nonzero" : "zero") launcher \(text(info.launcher)) " + + "launchDate \(info.launchDate != 0 ? "nonzero" : "zero") activeTime \(info.activeTime != 0 ? "nonzero" : "zero")", + "\(tag): spec vRefNum \(info.applicationVolume != 0 ? "set" : "zero") parID \(info.applicationParent != 0 ? "set" : "zero") " + + "name \"\(info.applicationName.string)\"", + ] +} + +@Suite struct ProcessManagerTests { + + @Test func matchesTheMacOS9Capture() throws { + let manager = capturedManager() + let current = manager.currentProcess() + let bogus = ProcessSerialNumber(high: 0x1234, low: 0x5678) + var lines = ["GetCurrentProcess -> 0 \(text(current))"] + lines += informationLines("self", current, manager) + let front = manager.frontProcess() + lines.append("GetFrontProcess -> 0 \(text(front))") + lines.append("SameProcess self front \(text(current)) \(text(front)) -> 0 result \(manager.same(current, front) ? 1 : 0)") + + var psn = ProcessSerialNumber.none + var index = 0 + while true { + do { + psn = try manager.nextProcess(after: psn) + } catch { + #expect(error == .processNotFound) + lines.append("GetNextProcess[\(index)] -> -600 \(text(.none))") + break + } + lines.append("GetNextProcess[\(index)] -> 0 \(text(psn))") + lines += informationLines("listed", psn, manager) + index += 1 + } + + lines += informationLines("kCurrentProcess", .current, manager) + lines += informationLines("kSystemProcess", .system, manager) + lines += informationLines("kNoProcess", .none, manager) + lines += informationLines("bogus", bogus, manager) + func same(_ label: String, _ a: ProcessSerialNumber, _ b: ProcessSerialNumber) { + lines.append("SameProcess \(label) \(text(a)) \(text(b)) -> 0 result \(manager.same(a, b) ? 1 : 0)") + } + same("self self", current, current) + same("self kCurrentProcess", current, .current) + same("kCurrentProcess kCurrentProcess", .current, .current) + same("self kSystemProcess", current, .system) + same("self kNoProcess", current, .none) + same("self bogus", current, bogus) + same("bogus bogus", bogus, bogus) + for (label, start) in [("bogus", bogus), ("kCurrentProcess", ProcessSerialNumber.current)] { + #expect(throws: MacError.self) { try manager.nextProcess(after: start) } + lines.append("GetNextProcess \(label) -> -600 \(text(.none))") + } + + #expect(try manager.information(for: current).applicationName.string == "ProcessManagerCapture") + lines.append("GetProcessInformation without name or spec -> 0 length 60") + #expect(throws: MacError.parameter) { try manager.information(for: current, recordLength: 4) } + lines.append("GetProcessInformation with length 4 -> -50 name 0") + + var changing = manager + try changing.setFrontProcess(current) + lines.append("SetFrontProcess self -> 0") + #expect(throws: MacError.processNotFound) { try changing.setFrontProcess(bogus) } + lines.append("SetFrontProcess bogus -> -600") + lines.append("GetFrontProcess after -> 0 \(text(changing.frontProcess()))") + lines.append("SameProcess self front after \(text(current)) \(text(changing.frontProcess())) -> 0 result " + + "\(changing.same(current, changing.frontProcess()) ? 1 : 0)") + lines.append("done") + + #expect(lines == capture) + } + + @Test func aProcessComesForwardWhenTheHostBringsIt() throws { + var manager = capturedManager() + try manager.bringToFront(manager.current) + #expect(manager.frontProcess() == manager.current) + #expect(manager.same(manager.current, manager.frontProcess())) + #expect(throws: MacError.processNotFound) { try manager.bringToFront(.system) } + } +} From 3e59150a7218175812e3bd89d48104d0d0792311 Mon Sep 17 00:00:00 2001 From: Alsey Coleman Miller Date: Tue, 1 Dec 2026 23:17:36 -0500 Subject: [PATCH 6/6] docs: document the Process Manager capture --- ReferenceTests/Retro68/README.md | 33 ++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/ReferenceTests/Retro68/README.md b/ReferenceTests/Retro68/README.md index 111e88e..8588187 100644 --- a/ReferenceTests/Retro68/README.md +++ b/ReferenceTests/Retro68/README.md @@ -262,3 +262,36 @@ UTM setup above. `RegionRecordingTests` checks `Port.beginRegion` and `endRegion` against the capture. + +## Process Manager + +`ProcessManagerCapture.c` records what the Process Manager says about the +processes running: its own serial number and `GetProcessInformation` record, +the list `GetNextProcess` walks, the front process, `SameProcess` on the +special serial numbers and the errors for numbers that name no process. Values +that differ from run to run (launch date, active time, sizes) are recorded as +zero or not. It ran from the Finder, on Mac OS 9.2.2 in UTM with File Sharing +on. Its output is `ProcessManager-macos-9.2.2-ppc.txt` (CR line endings). + +- A serial number is a high long of 0 and a low long of the process's number + in its high half and a generation (1, or 2 for the File Sharing extension) + in its low half. `kNoProcess` is 0, `kSystemProcess` 1, `kCurrentProcess` 2. +- `GetNextProcess(kNoProcess)` gave the first process; the list was in launch + order, background-only programs first, the Finder (`FNDR`/`MACS`, no + launcher) after them, then what the Finder opened with the Finder as its + launcher. After the last one it gave procNotFound (-600) and set the serial + number to `kNoProcess`. +- `GetProcessInformation` took `kCurrentProcess` as the caller. `kSystemProcess`, + `kNoProcess` and a number nothing runs under were procNotFound. A record + whose `processInfoLength` was 4 was paramErr; 60 is `sizeof(ProcessInfoRec)`. + Name and `processAppSpec` were filled only when the caller gave pointers. +- `GetNextProcess(kCurrentProcess)` and `GetNextProcess` of a number nothing + runs under were procNotFound. +- `SameProcess` never failed: `kCurrentProcess` equals the caller's own number, + and two equal numbers are the same even when no process has them. +- `SetFrontProcess` of the caller gave noErr but the Finder was still in front + just after: a process comes forward when it next calls the Event Manager. + An unknown number was procNotFound. + +`ProcessManagerTests` rebuilds the probe's output from `ProcessManager` and +compares it with the capture line by line.