Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
216 changes: 2 additions & 214 deletions Core-Monitor/CoreMonitorShareKit.swift
Original file line number Diff line number Diff line change
@@ -1,221 +1,9 @@
import Foundation

struct CoreMonitorShareSnapshotContext: Equatable {
let generatedAt: Date
let appVersion: String
let macOSVersion: String
let hostModelIdentifier: String
let hostModelName: String
let chipName: String
let cpuUsagePercent: Double
let performanceCoreUsagePercent: Double?
let efficiencyCoreUsagePercent: Double?
let memoryUsagePercent: Double
let memoryUsedGB: Double
let totalMemoryGB: Double
let cpuTemperature: Double?
let gpuTemperature: Double?
let ssdTemperature: Double?
let fanSpeeds: [Int]
let fanModeTitle: String
let helperStateTitle: String
let helperInstalled: Bool
let batteryChargePercent: Int?
let batteryPowerWatts: Double?
let totalSystemWatts: Double?
let thermalStateTitle: String
let hasSMCAccess: Bool
let smcError: String?
}

/// Canonical outbound links for the app. Kept in one place so the About tab
/// and the Help menu cannot drift apart.
enum CoreMonitorShareKit {
static let websiteURL = URL(string: "https://offyotto.github.io/Core-Monitor/")!
static let repositoryURL = URL(string: "https://github.com/offyotto/Core-Monitor")!
static let latestReleaseURL = URL(string: "https://github.com/offyotto/Core-Monitor/releases/latest")!
static let appStoreURL = URL(string: "https://apps.apple.com/us/app/core-monitor/id6762558526?mt=12")!

static func productPitch() -> String {
"""
Core-Monitor is a free, open-source Apple Silicon system monitor and optional fan-control app for macOS.

It tracks thermals, power, battery, CPU, GPU, memory, menu bar status, alerts, Touch Bar widgets, and helper-backed fan control locally on your Mac. Monitoring works without elevated access; the helper is only needed for fan writes.

Website: \(websiteURL.absoluteString)
Download: \(latestReleaseURL.absoluteString)
Mac App Store edition: \(appStoreURL.absoluteString)
Source: \(repositoryURL.absoluteString)
"""
}

static func launchPost() -> String {
"""
Core-Monitor is a free, open-source Apple Silicon monitor for macOS: thermals, watts, battery, fans, menu bar status, alerts, Touch Bar widgets, and optional fan control with no account or telemetry.

Download: \(latestReleaseURL.absoluteString)
Source: \(repositoryURL.absoluteString)
"""
}

@MainActor
static func makeSupportSnapshot(
systemMonitor: SystemMonitor,
fanController: FanController,
helperManager: SMCHelperManager = .shared,
generatedAt: Date = Date()
) -> String {
let snapshot = systemMonitor.snapshot
let modelIdentifier = SystemMonitor.hostModelIdentifier()
let context = CoreMonitorShareSnapshotContext(
generatedAt: generatedAt,
appVersion: AppVersion.current,
macOSVersion: ProcessInfo.processInfo.operatingSystemVersionString,
hostModelIdentifier: modelIdentifier,
hostModelName: MacModelRegistry.displayName(for: modelIdentifier),
chipName: SystemMonitor.chipName(),
cpuUsagePercent: snapshot.cpuUsagePercent,
performanceCoreUsagePercent: snapshot.performanceCoreUsagePercent,
efficiencyCoreUsagePercent: snapshot.efficiencyCoreUsagePercent,
memoryUsagePercent: snapshot.memoryUsagePercent,
memoryUsedGB: snapshot.memoryUsedGB,
totalMemoryGB: snapshot.totalMemoryGB,
cpuTemperature: snapshot.cpuTemperature,
gpuTemperature: snapshot.gpuTemperature,
ssdTemperature: snapshot.ssdTemperature,
fanSpeeds: snapshot.fanSpeeds,
fanModeTitle: fanModeTitle(fanController.mode),
helperStateTitle: helperStateTitle(helperManager.connectionState),
helperInstalled: helperManager.isInstalled,
batteryChargePercent: snapshot.batteryInfo.chargePercent,
batteryPowerWatts: snapshot.batteryInfo.powerWatts,
totalSystemWatts: snapshot.totalSystemWatts,
thermalStateTitle: thermalStateTitle(snapshot.thermalState),
hasSMCAccess: snapshot.hasSMCAccess,
smcError: snapshot.lastError
)
return supportSnapshotMarkdown(from: context)
}

static func supportSnapshotMarkdown(from context: CoreMonitorShareSnapshotContext) -> String {
var lines: [String] = [
"# Core-Monitor Support Snapshot",
"",
"- Generated: \(iso8601String(context.generatedAt))",
"- App: Core Monitor \(context.appVersion)",
"- macOS: \(context.macOSVersion)",
"- Mac: \(context.hostModelName) (\(context.hostModelIdentifier))",
"- Chip: \(context.chipName)",
"",
"## Monitoring",
"",
"- CPU: \(percentString(context.cpuUsagePercent))"
]

if let performanceCoreUsagePercent = context.performanceCoreUsagePercent {
lines.append("- P-cores: \(percentString(performanceCoreUsagePercent))")
}

if let efficiencyCoreUsagePercent = context.efficiencyCoreUsagePercent {
lines.append("- E-cores: \(percentString(efficiencyCoreUsagePercent))")
}

lines.append("- Memory: \(gbString(context.memoryUsedGB)) of \(gbString(context.totalMemoryGB)) (\(percentString(context.memoryUsagePercent)))")
lines.append("- Thermal pressure: \(context.thermalStateTitle)")
lines.append("- CPU temperature: \(temperatureString(context.cpuTemperature))")
lines.append("- GPU temperature: \(temperatureString(context.gpuTemperature))")
lines.append("- SSD temperature: \(temperatureString(context.ssdTemperature))")
lines.append("- System power: \(wattsString(context.totalSystemWatts))")
lines.append("- Battery: \(batteryString(chargePercent: context.batteryChargePercent, watts: context.batteryPowerWatts))")
lines.append("- Fans: \(fanSpeedsString(context.fanSpeeds))")
lines.append("- SMC access: \(context.hasSMCAccess ? "Available" : "Unavailable")")

if let smcError = context.smcError?.trimmingCharacters(in: .whitespacesAndNewlines), smcError.isEmpty == false {
lines.append("- SMC note: \(smcError)")
}

lines.append(contentsOf: [
"",
"## Cooling",
"",
"- Mode: \(context.fanModeTitle)",
"- Helper: \(context.helperStateTitle) (installed: \(context.helperInstalled ? "yes" : "no"))",
"",
"Core-Monitor: \(websiteURL.absoluteString)",
"Source: \(repositoryURL.absoluteString)"
])

return lines.joined(separator: "\n")
}

private static func iso8601String(_ date: Date) -> String {
let formatter = ISO8601DateFormatter()
formatter.formatOptions = [.withInternetDateTime]
return formatter.string(from: date)
}

private static func percentString(_ value: Double) -> String {
"\(Int(value.rounded()))%"
}

private static func gbString(_ value: Double) -> String {
guard value > 0 else { return "0 GB" }
if value >= 10 {
return String(format: "%.0f GB", value)
}
return String(format: "%.1f GB", value)
}

private static func temperatureString(_ value: Double?) -> String {
guard let value else { return "Unavailable" }
return "\(Int(value.rounded())) C"
}

private static func wattsString(_ value: Double?) -> String {
guard let value else { return "Unavailable" }
return String(format: "%.1f W", value)
}

private static func batteryString(chargePercent: Int?, watts: Double?) -> String {
let charge = chargePercent.map { "\($0)%" } ?? "Unavailable"
guard let watts else { return charge }
return "\(charge), \(wattsString(watts))"
}

private static func fanSpeedsString(_ fanSpeeds: [Int]) -> String {
guard fanSpeeds.isEmpty == false else { return "Unavailable" }
// A negative value is the failed-read sentinel, not a real RPM.
return fanSpeeds.map { $0 < 0 ? "Unavailable" : "\($0) RPM" }.joined(separator: ", ")
}

private static func fanModeTitle(_ mode: FanControlMode) -> String {
switch mode {
case .smart: return "Smart"
case .silent: return "System"
case .balanced: return "Balanced"
case .performance: return "Performance"
case .max: return "Maximum"
case .manual: return "Manual"
case .custom: return "Custom"
case .automatic: return "System Automatic"
}
}

private static func helperStateTitle(_ state: SMCHelperManager.ConnectionState) -> String {
switch state {
case .missing: return "Missing"
case .unknown: return "Unknown"
case .checking: return "Checking"
case .reachable: return "Reachable"
case .unreachable: return "Unavailable"
}
}

private static func thermalStateTitle(_ state: ProcessInfo.ThermalState) -> String {
switch state {
case .nominal: return "Nominal"
case .fair: return "Fair"
case .serious: return "Serious"
case .critical: return "Critical"
@unknown default: return "Unknown"
}
}
}
33 changes: 23 additions & 10 deletions Core-Monitor/FanController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -488,7 +488,9 @@ final class FanController: ObservableObject {
func setAutoMaxSpeed(_ speed: Int) {
autoMaxSpeed = max(minSpeed, min(maxSpeed, speed))
saveSettings()
if mode == .smart || mode == .automatic {
// The ceiling only feeds the smart profile. Re-running the control loop
// in a system-owned mode would just churn the status line.
if mode == .smart {
lastAppliedSpeed = 0
updateManagedControl()
}
Expand Down Expand Up @@ -567,8 +569,6 @@ final class FanController: ObservableObject {
if mode == .custom {
lastAppliedSpeed = 0
applyCurrentMode(force: true)
}
if mode == .custom {
statusMessage = "Custom preset \"\(preset.name)\" applied."
}
return .success(customPresetStatus)
Expand Down Expand Up @@ -653,11 +653,20 @@ final class FanController: ObservableObject {
allSuccess = false
}
}
if allSuccess {
}
statusMessage = allSuccess ? "System automatic control restored" : "Failed to restore automatic control"
}

/// Hands every fan back to the firmware curve without ever prompting for a
/// helper install. Choosing a system-owned mode must never escalate
/// privileges, so with no helper present we only report the passive state.
private func requestSystemAutomaticHandoff() {
guard helperManager.isInstalled else {
statusMessage = passiveStatusMessage(for: mode)
return
}
resetToSystemAutomatic()

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Keep system handoff on the non-installing helper path

When the helper executable exists but its connection state is .unreachable, this call enters resetToSystemAutomatic(), whose ensureHelperInstalledIfNeeded() invokes attemptPrivilegedInstall(forceReinstall: true) and can display an administrator authorization prompt. This contradicts the new non-escalating System-mode contract and also occurs during automatic handoffs after wake; use the existing executeIfInstalled path so selecting a system-owned mode never initiates installation or repair.

Useful? React with 👍 / 👎.

}

func calibrateFanControl() {
guard !isCalibrating else { return }
guard ensureHelperInstalledIfNeeded() else {
Expand Down Expand Up @@ -728,8 +737,12 @@ final class FanController: ObservableObject {

switch mode {
case .automatic, .silent:
if Self.shouldRequestSystemAutomaticHandoff(lastAppliedSpeed: lastAppliedSpeed) {
resetToSystemAutomatic()
// `force` means the user just picked this mode, so always hand the
// fans back. lastAppliedSpeed only tracks writes made by this
// process: after a relaunch, a crash, or an earlier handoff it
// reads 0 or -1 while the fans may still be pinned from before.
if force || Self.shouldRequestSystemAutomaticHandoff(lastAppliedSpeed: lastAppliedSpeed) {
requestSystemAutomaticHandoff()
} else {
statusMessage = passiveStatusMessage(for: mode)
}
Expand All @@ -745,7 +758,7 @@ final class FanController: ObservableObject {
}

private func updateManagedControl() {
guard let _ = systemMonitor else { return }
guard systemMonitor != nil else { return }

switch mode {
case .manual:
Expand All @@ -757,9 +770,9 @@ final class FanController: ObservableObject {

case .automatic, .silent:
if Self.shouldRequestSystemAutomaticHandoff(lastAppliedSpeed: lastAppliedSpeed) {
resetToSystemAutomatic()
requestSystemAutomaticHandoff()
} else {
statusMessage = "System automatic mode is active"
statusMessage = passiveStatusMessage(for: mode)
}
lastAppliedSpeed = -1

Expand Down
Loading