From 506ae347deb7943602fa317417510e324a3cff63 Mon Sep 17 00:00:00 2001 From: Udo Thiel Date: Thu, 19 Oct 2023 12:53:31 +0200 Subject: [PATCH 1/4] use SMAppService if macOS >= 13 --- Hot/Classes/NSApplication+LaunchServices.m | 25 ++++++++++++++++++++-- 1 file changed, 23 insertions(+), 2 deletions(-) diff --git a/Hot/Classes/NSApplication+LaunchServices.m b/Hot/Classes/NSApplication+LaunchServices.m index 1f9010c..c089725 100644 --- a/Hot/Classes/NSApplication+LaunchServices.m +++ b/Hot/Classes/NSApplication+LaunchServices.m @@ -23,6 +23,7 @@ ******************************************************************************/ #import "NSApplication+LaunchServices.h" +#import /* * LaunchServices functions for managing login items are deprecated, @@ -205,12 +206,32 @@ + ( void )disableLoginItemForBundleURL: ( NSURL * )url - ( BOOL )isLoginItemEnabled { - return [ NSApplication isLoginItemEnabledForBundleURL: [ NSURL fileURLWithPath: [ [ NSBundle mainBundle ] bundlePath ] ] ]; + if (@available(macOS 13.0, *)) { + + SMAppServiceStatus status = SMAppService.mainAppService.status; + NSLog(@"SMAppServiceStatus=%li", status); + return status == SMAppServiceStatusEnabled; + } else { + return [ NSApplication isLoginItemEnabledForBundleURL: [ NSURL fileURLWithPath: [ [ NSBundle mainBundle ] bundlePath ] ] ]; + } } - ( void )setLoginItemEnabled: ( BOOL )enabled { - [ NSApplication setLoginItemEnabled: enabled forBundleURL: [ NSURL fileURLWithPath: [ [ NSBundle mainBundle ] bundlePath ] ] ]; + if (@available(macOS 13.0, *)) { + + NSError *error; + + if (enabled) { + [SMAppService.mainAppService registerAndReturnError:&error]; + NSLog(@"SMAppService Registered with error: %@",error); + } else { + NSLog(@"SMAppService Unregistered with error: %@",error); + [SMAppService.mainAppService unregisterAndReturnError:&error]; + } + } else { + [ NSApplication setLoginItemEnabled: enabled forBundleURL: [ NSURL fileURLWithPath: [ [ NSBundle mainBundle ] bundlePath ] ] ]; + } } - ( void )enableLoginItem From d446093e126a78e29565f201731dc0b381520016 Mon Sep 17 00:00:00 2001 From: Udo Thiel Date: Thu, 19 Oct 2023 13:44:57 +0200 Subject: [PATCH 2/4] use IOKIT to read thermal stats --- Hot/Classes/ThermalLog.swift | 76 +++++++----------------------------- 1 file changed, 15 insertions(+), 61 deletions(-) diff --git a/Hot/Classes/ThermalLog.swift b/Hot/Classes/ThermalLog.swift index 06cb820..2af3725 100644 --- a/Hot/Classes/ThermalLog.swift +++ b/Hot/Classes/ThermalLog.swift @@ -25,6 +25,7 @@ import Foundation import IOHIDKit import SMCKit +import IOKit.pwr_mgt public class ThermalLog: NSObject { @@ -185,67 +186,20 @@ public class ThermalLog: NSObject { self.temperature = NSNumber( value: temp ) } - - let pipe = Pipe() - let task = Process() - task.launchPath = "/usr/bin/pmset" - task.arguments = [ "-g", "therm" ] - task.standardOutput = pipe - - task.launch() - task.waitUntilExit() - - if task.terminationStatus != 0 - { - self.refreshing = false - - completion() - - return - } - - let data = pipe.fileHandleForReading.readDataToEndOfFile() - - guard let str = String( data: data, encoding: .utf8 ), str.count > 0 - else - { - self.refreshing = false - - completion() - - return - } - - let lines = str.replacingOccurrences( of: " ", with: "" ).replacingOccurrences( of: "\t", with: "" ).split( separator: "\n" ) - - for line in lines - { - let p = line.split( separator: "=" ) - - if p.count < 2 - { - continue - } - - guard let n = UInt( p[ 1 ] ) - else - { - continue - } - - if p[ 0 ] == "CPU_Scheduler_Limit" - { - self.schedulerLimit = NSNumber( value: n ) - } - else if p[ 0 ] == "CPU_Available_CPUs" - { - self.availableCPUs = NSNumber( value: n ) - } - else if p[ 0 ] == "CPU_Speed_Limit" - { - self.speedLimit = NSNumber( value: n ) - } - } + + let status = UnsafeMutablePointer?>.allocate(capacity: 3) + let result = IOPMCopyCPUPowerStatus(status) + + if result == kIOReturnSuccess, + let data = status.move()?.takeUnretainedValue() { + let dataMap = data as NSDictionary + + self.speedLimit = (dataMap[kIOPMCPUPowerLimitProcessorSpeedKey]! as! Double) as NSNumber + self.availableCPUs = (dataMap[kIOPMCPUPowerLimitProcessorCountKey]! as! Int) as NSNumber + self.schedulerLimit = (dataMap[kIOPMCPUPowerLimitSchedulerTimeKey]! as! Double) as NSNumber + } + + status.deallocate() self.refreshing = false From f2fde4b2d9a4eaf6b0747f920dc8f7be96b0bca6 Mon Sep 17 00:00:00 2001 From: Udo Thiel Date: Sat, 21 Oct 2023 18:01:09 +0200 Subject: [PATCH 3/4] small performance improvement --- Hot/Classes/ThermalLog.swift | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Hot/Classes/ThermalLog.swift b/Hot/Classes/ThermalLog.swift index 2af3725..c983b4b 100644 --- a/Hot/Classes/ThermalLog.swift +++ b/Hot/Classes/ThermalLog.swift @@ -194,9 +194,9 @@ public class ThermalLog: NSObject let data = status.move()?.takeUnretainedValue() { let dataMap = data as NSDictionary - self.speedLimit = (dataMap[kIOPMCPUPowerLimitProcessorSpeedKey]! as! Double) as NSNumber - self.availableCPUs = (dataMap[kIOPMCPUPowerLimitProcessorCountKey]! as! Int) as NSNumber - self.schedulerLimit = (dataMap[kIOPMCPUPowerLimitSchedulerTimeKey]! as! Double) as NSNumber + self.speedLimit = dataMap[kIOPMCPUPowerLimitProcessorSpeedKey] as? NSNumber + self.availableCPUs = dataMap[kIOPMCPUPowerLimitProcessorCountKey] as? NSNumber + self.schedulerLimit = dataMap[kIOPMCPUPowerLimitSchedulerTimeKey] as? NSNumber } status.deallocate() From 4537943468a03cc0fa0ce649187db66020d3e326 Mon Sep 17 00:00:00 2001 From: Udo Thiel Date: Sat, 21 Oct 2023 23:03:10 +0200 Subject: [PATCH 4/4] disable call to IOPMCopyCPUPowerStatus on arm as it doesn't return anything anyway --- Hot/Classes/ThermalLog.swift | 2 ++ 1 file changed, 2 insertions(+) diff --git a/Hot/Classes/ThermalLog.swift b/Hot/Classes/ThermalLog.swift index c983b4b..bbaf7de 100644 --- a/Hot/Classes/ThermalLog.swift +++ b/Hot/Classes/ThermalLog.swift @@ -187,6 +187,7 @@ public class ThermalLog: NSObject self.temperature = NSNumber( value: temp ) } +#if arch(x86_64) let status = UnsafeMutablePointer?>.allocate(capacity: 3) let result = IOPMCopyCPUPowerStatus(status) @@ -200,6 +201,7 @@ public class ThermalLog: NSObject } status.deallocate() +#endif self.refreshing = false