forked from mirror/smcFanControl
For Apple Silicon use the temperature sensors from IOKIT
This commit is contained in:
@ -25,6 +25,7 @@
|
||||
#import "NSFileManager+DirectoryLocations.h"
|
||||
#import "smc.h"
|
||||
#import "smcWrapper.h"
|
||||
#import "IOKitSensor.h"
|
||||
#import "MachineDefaults.h"
|
||||
|
||||
#import "Power.h"
|
||||
|
||||
@ -150,7 +150,11 @@ NSUserDefaults *defaults;
|
||||
@0, PREF_AC_SELECTION,
|
||||
@0, PREF_CHARGING_SELECTION,
|
||||
@0, PREF_MENU_DISPLAYMODE,
|
||||
[MachineDefaults isAppleSilicon] ? @"Tp0D" : @"TC0D", PREF_TEMPERATURE_SENSOR,
|
||||
#if TARGET_CPU_ARM64
|
||||
@"",PREF_TEMPERATURE_SENSOR,
|
||||
#else
|
||||
@"TC0D",PREF_TEMPERATURE_SENSOR,
|
||||
#endif
|
||||
@0, PREF_NUMBEROF_LAUNCHES,
|
||||
@NO,PREF_DONATIONMESSAGE_DISPLAY,
|
||||
[NSArchiver archivedDataWithRootObject:[NSColor blackColor]],PREF_MENU_TEXTCOLOR,
|
||||
@ -399,7 +403,11 @@ NSUserDefaults *defaults;
|
||||
|
||||
if (bNeedTemp == true) {
|
||||
// Read current temperature and format text for the menubar.
|
||||
#if TARGET_CPU_ARM64
|
||||
c_temp = [IOKitSensor getSOCTemperature];
|
||||
#else
|
||||
c_temp = [smcWrapper get_maintemp];
|
||||
#endif
|
||||
|
||||
if ([[defaults objectForKey:PREF_TEMP_UNIT] intValue]==0) {
|
||||
temp = [NSString stringWithFormat:@"%@%CC",@(c_temp),(unsigned short)0xb0];
|
||||
@ -741,10 +749,10 @@ NSUserDefaults *defaults;
|
||||
#pragma mark **Power Watchdog-Methods**
|
||||
|
||||
- (void)systemWillSleep:(id)sender{
|
||||
if ([MachineDefaults isAppleSilicon]) {
|
||||
[FanControl setRights];
|
||||
[self setFansToAuto:true];
|
||||
}
|
||||
#if TARGET_CPU_ARM64
|
||||
[FanControl setRights];
|
||||
[self setFansToAuto:true];
|
||||
#endif
|
||||
}
|
||||
|
||||
- (void)systemDidWakeFromSleep:(id)sender{
|
||||
|
||||
57
Classes/IOKitSensor.h
Normal file
57
Classes/IOKitSensor.h
Normal file
@ -0,0 +1,57 @@
|
||||
/*
|
||||
* FanControl
|
||||
*
|
||||
* Copyright (c) 2006-2012 Hendrik Holtmann
|
||||
*
|
||||
* Sensor.h - MacBook(Pro) FanControl application
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation; either version 2 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program; if not, write to the Free Software
|
||||
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||
*/
|
||||
|
||||
#import <Foundation/Foundation.h>
|
||||
|
||||
#import <IOKit/hidsystem/IOHIDEventSystemClient.h>
|
||||
#import <IOKit/hidsystem/IOHIDServiceClient.h>
|
||||
|
||||
// https://opensource.apple.com/source/IOHIDFamily/IOHIDFamily-1035.70.7/IOHIDFamily/AppleHIDUsageTables.h.auto.html
|
||||
#define kHIDPage_AppleVendor 0xff00
|
||||
#define kHIDUsage_AppleVendor_TemperatureSensor 0x0005
|
||||
|
||||
// https://opensource.apple.com/source/IOHIDFamily/IOHIDFamily-1035.70.7/IOHIDFamily/IOHIDEventTypes.h.auto.html
|
||||
#ifdef __LP64__
|
||||
typedef double IOHIDFloat;
|
||||
#else
|
||||
typedef float IOHIDFloat;
|
||||
#endif
|
||||
|
||||
// https://opensource.apple.com/source/IOHIDFamily/IOHIDFamily-1035.70.7/IOHIDFamily/IOHIDEventTypes.h.auto.html
|
||||
#define IOHIDEventFieldBase(type) (type << 16)
|
||||
#define kIOHIDEventTypeTemperature 15
|
||||
|
||||
|
||||
typedef struct __IOHIDEvent *IOHIDEventRef;
|
||||
typedef struct __IOHIDServiceClient *IOHIDServiceClientRef;
|
||||
|
||||
IOHIDEventSystemClientRef IOHIDEventSystemClientCreate(CFAllocatorRef);
|
||||
void IOHIDEventSystemClientSetMatching(IOHIDEventSystemClientRef, CFDictionaryRef);
|
||||
IOHIDEventRef IOHIDServiceClientCopyEvent(IOHIDServiceClientRef, int64_t, int32_t, int64_t);
|
||||
IOHIDFloat IOHIDEventGetFloatValue(IOHIDEventRef event, uint32_t field);
|
||||
|
||||
@interface IOKitSensor : NSObject {
|
||||
}
|
||||
|
||||
+ (float) getSOCTemperature;
|
||||
|
||||
@end
|
||||
72
Classes/IOKitSensor.m
Normal file
72
Classes/IOKitSensor.m
Normal file
@ -0,0 +1,72 @@
|
||||
/*
|
||||
* FanControl
|
||||
*
|
||||
* Copyright (c) 2006-2012 Hendrik Holtmann
|
||||
*
|
||||
* Sensor.m - MacBook(Pro) FanControl application
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation; either version 2 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program; if not, write to the Free Software
|
||||
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||
*/
|
||||
|
||||
#import "IOKitSensor.h"
|
||||
|
||||
@implementation IOKitSensor
|
||||
|
||||
static BOOL isSOCSensor(CFStringRef sensorName) {
|
||||
return CFStringHasPrefix(sensorName, CFSTR("PMU")) &&
|
||||
!CFStringHasSuffix(sensorName, CFSTR("tcal")); // Ignore "PMU tcal" as it seems static
|
||||
}
|
||||
|
||||
static float toOneDecimalPlace(float value) {
|
||||
return roundf(10.0f * value) / 10.0f;
|
||||
}
|
||||
|
||||
+ (float) getSOCTemperature {
|
||||
|
||||
IOHIDEventSystemClientRef eventSystemClient = IOHIDEventSystemClientCreate(kCFAllocatorDefault);
|
||||
CFArrayRef services = IOHIDEventSystemClientCopyServices(eventSystemClient);
|
||||
if (services) {
|
||||
|
||||
float socSensorSum = 0.0f;
|
||||
int socSensorCount = 0;
|
||||
|
||||
for (int i = 0; i < CFArrayGetCount(services); i++) {
|
||||
IOHIDServiceClientRef serviceClientRef = (IOHIDServiceClientRef)CFArrayGetValueAtIndex(services, i);
|
||||
CFStringRef sensorName = IOHIDServiceClientCopyProperty(serviceClientRef, CFSTR("Product"));
|
||||
if (sensorName) {
|
||||
if (isSOCSensor(sensorName)) {
|
||||
IOHIDEventRef event = IOHIDServiceClientCopyEvent(serviceClientRef, kIOHIDEventTypeTemperature, 0, 0);
|
||||
if (event) {
|
||||
IOHIDFloat sensorTemperature = IOHIDEventGetFloatValue(event, IOHIDEventFieldBase(kIOHIDEventTypeTemperature));
|
||||
CFRelease(event);
|
||||
socSensorSum += sensorTemperature;
|
||||
socSensorCount++;
|
||||
}
|
||||
}
|
||||
CFRelease(sensorName);
|
||||
}
|
||||
}
|
||||
|
||||
CFRelease(services);
|
||||
CFRelease(eventSystemClient);
|
||||
|
||||
float avgSOCTemp = socSensorCount > 0 ? socSensorSum / socSensorCount: 0.0f;
|
||||
return toOneDecimalPlace(avgSOCTemp);
|
||||
}
|
||||
|
||||
return 0.0f;
|
||||
}
|
||||
|
||||
@end
|
||||
@ -30,7 +30,6 @@
|
||||
int machine_num;
|
||||
}
|
||||
|
||||
+ (BOOL)isAppleSilicon;
|
||||
+ (NSString *)computerModel;
|
||||
- (instancetype)init:(NSString*)p_machine ;
|
||||
|
||||
|
||||
@ -22,9 +22,6 @@
|
||||
|
||||
#import "MachineDefaults.h"
|
||||
#import "NSFileManager+DirectoryLocations.h"
|
||||
#include <sys/types.h>
|
||||
#include <sys/sysctl.h>
|
||||
#include <mach/machine.h>
|
||||
|
||||
@interface MachineDefaults ()
|
||||
{
|
||||
@ -126,14 +123,6 @@
|
||||
return defaultsDict;
|
||||
}
|
||||
|
||||
+ (BOOL)isAppleSilicon
|
||||
{
|
||||
cpu_type_t cpu_type;
|
||||
size_t size = sizeof(cpu_type);
|
||||
sysctlbyname("hw.cputype", &cpu_type, &size, NULL, 0);
|
||||
return cpu_type == CPU_TYPE_ARM64;
|
||||
}
|
||||
|
||||
+ (NSString *)computerModel
|
||||
{
|
||||
static NSString *computerModel = nil;
|
||||
|
||||
@ -70,11 +70,7 @@ NSArray *allSensors;
|
||||
NSString *sensor = [[NSUserDefaults standardUserDefaults] objectForKey:PREF_TEMPERATURE_SENSOR];
|
||||
SMCReadKey2((char*)[sensor UTF8String], &val,conn);
|
||||
retValue = [self convertToNumber:val];
|
||||
if ([MachineDefaults isAppleSilicon]) {
|
||||
allSensors = [NSArray arrayWithObjects:@"Tp0D",@"Tp0P",nil];
|
||||
} else {
|
||||
allSensors = [NSArray arrayWithObjects:@"TC0D",@"TC0P",@"TCAD",@"TC0H",@"TC0F",@"TCAH",@"TCBH",nil];
|
||||
}
|
||||
allSensors = [NSArray arrayWithObjects:@"TC0D",@"TC0P",@"TCAD",@"TC0H",@"TC0F",@"TCAH",@"TCBH",nil];
|
||||
if (retValue<=0 || floor(retValue) == 129 ) { //workaround for some iMac Models
|
||||
for (NSString *sensor in allSensors) {
|
||||
SMCReadKey2((char*)[sensor UTF8String], &val,conn);
|
||||
|
||||
Reference in New Issue
Block a user