mirror of
https://github.com/morgan9e/macos-stats
synced 2026-04-15 00:34:08 +09:00
feat: moved from UnsafePointer<SMCService> to sharable instance (SMC.shared)
This commit is contained in:
@@ -67,8 +67,9 @@ open class Reader<T>: ReaderInternal_p {
|
||||
|
||||
private var history: [T]? = []
|
||||
|
||||
public init() {
|
||||
public init(popup: Bool = false) {
|
||||
self.log = OSLog(subsystem: Bundle.main.bundleIdentifier!, category: "\(T.self)")
|
||||
self.popup = popup
|
||||
|
||||
self.setup()
|
||||
|
||||
|
||||
@@ -40,7 +40,7 @@ public class CPU: Module {
|
||||
}
|
||||
}
|
||||
|
||||
public init(_ smc: UnsafePointer<SMCService>) {
|
||||
public init() {
|
||||
self.settingsView = Settings("CPU")
|
||||
self.popupView = Popup("CPU")
|
||||
|
||||
@@ -52,10 +52,10 @@ public class CPU: Module {
|
||||
|
||||
self.loadReader = LoadReader()
|
||||
self.processReader = ProcessReader()
|
||||
self.temperatureReader = TemperatureReader(smc: smc)
|
||||
self.temperatureReader = TemperatureReader(popup: true)
|
||||
|
||||
#if arch(x86_64)
|
||||
self.frequencyReader = FrequencyReader()
|
||||
self.frequencyReader = FrequencyReader(popup: true)
|
||||
#endif
|
||||
|
||||
self.settingsView.callback = { [unowned self] in
|
||||
|
||||
@@ -226,26 +226,18 @@ public class ProcessReader: Reader<[TopProcess]> {
|
||||
}
|
||||
|
||||
public class TemperatureReader: Reader<Double> {
|
||||
private let smc: UnsafePointer<SMCService>
|
||||
|
||||
init(smc: UnsafePointer<SMCService>) {
|
||||
self.smc = smc
|
||||
super.init()
|
||||
self.popup = true
|
||||
}
|
||||
|
||||
public override func read() {
|
||||
var temperature: Double? = nil
|
||||
|
||||
if let value = self.smc.pointee.getValue("TC0D"), value < 110 {
|
||||
if let value = SMC.shared.getValue("TC0D"), value < 110 {
|
||||
temperature = value
|
||||
} else if let value = self.smc.pointee.getValue("TC0E"), value < 110 {
|
||||
} else if let value = SMC.shared.getValue("TC0E"), value < 110 {
|
||||
temperature = value
|
||||
} else if let value = self.smc.pointee.getValue("TC0F"), value < 110 {
|
||||
} else if let value = SMC.shared.getValue("TC0F"), value < 110 {
|
||||
temperature = value
|
||||
} else if let value = self.smc.pointee.getValue("TC0P"), value < 110 {
|
||||
} else if let value = SMC.shared.getValue("TC0P"), value < 110 {
|
||||
temperature = value
|
||||
} else if let value = self.smc.pointee.getValue("TC0H"), value < 110 {
|
||||
} else if let value = SMC.shared.getValue("TC0H"), value < 110 {
|
||||
temperature = value
|
||||
}
|
||||
|
||||
@@ -280,11 +272,6 @@ public class FrequencyReader: Reader<Double> {
|
||||
}
|
||||
}
|
||||
|
||||
override init() {
|
||||
super.init()
|
||||
self.popup = true
|
||||
}
|
||||
|
||||
public override func setup() {
|
||||
guard self.isEnabled else { return }
|
||||
|
||||
|
||||
@@ -35,17 +35,14 @@ public struct Fan {
|
||||
}
|
||||
|
||||
public class Fans: Module {
|
||||
private var smc: UnsafePointer<SMCService>
|
||||
|
||||
private var fansReader: FansReader
|
||||
private var settingsView: Settings
|
||||
private let popupView: Popup
|
||||
|
||||
public init(_ smc: UnsafePointer<SMCService>) {
|
||||
self.smc = smc
|
||||
self.fansReader = FansReader(smc)
|
||||
public init() {
|
||||
self.fansReader = FansReader()
|
||||
self.settingsView = Settings("Fans", list: &self.fansReader.list)
|
||||
self.popupView = Popup(smc)
|
||||
self.popupView = Popup()
|
||||
|
||||
super.init(
|
||||
popup: self.popupView,
|
||||
@@ -75,7 +72,7 @@ public class Fans: Module {
|
||||
}
|
||||
|
||||
public override func isAvailable() -> Bool {
|
||||
return smc.pointee.getValue("FNum") != nil && smc.pointee.getValue("FNum") != 0 && !self.fansReader.list.isEmpty
|
||||
return SMC.shared.getValue("FNum") != nil && SMC.shared.getValue("FNum") != 0 && !self.fansReader.list.isEmpty
|
||||
}
|
||||
|
||||
private func checkIfNoSensorsEnabled() {
|
||||
|
||||
@@ -16,12 +16,9 @@ import StatsKit
|
||||
internal class Popup: NSStackView, Popup_p {
|
||||
public var sizeCallback: ((NSSize) -> Void)? = nil
|
||||
|
||||
private var smc: UnsafePointer<SMCService>
|
||||
private var list: [Int: FanView] = [:]
|
||||
|
||||
public init(_ smc: UnsafePointer<SMCService>) {
|
||||
self.smc = smc
|
||||
|
||||
public init() {
|
||||
super.init(frame: NSRect(x: 0, y: 0, width: Constants.Popup.width, height: 0))
|
||||
|
||||
self.orientation = .vertical
|
||||
@@ -34,7 +31,7 @@ internal class Popup: NSStackView, Popup_p {
|
||||
|
||||
internal func setup(_ values: [Fan]) {
|
||||
values.forEach { (f: Fan) in
|
||||
let view = FanView(f, smc: self.smc, width: self.frame.width, callback: self.recalculateHeight)
|
||||
let view = FanView(f, width: self.frame.width, callback: self.recalculateHeight)
|
||||
self.list[f.id] = view
|
||||
self.addArrangedSubview(view)
|
||||
}
|
||||
@@ -66,7 +63,6 @@ internal class Popup: NSStackView, Popup_p {
|
||||
internal class FanView: NSStackView {
|
||||
public var sizeCallback: (() -> Void)
|
||||
|
||||
private var smc: UnsafePointer<SMCService>
|
||||
private var fan: Fan
|
||||
private var ready: Bool = false
|
||||
|
||||
@@ -78,9 +74,8 @@ internal class FanView: NSStackView {
|
||||
private var controlView: NSView? = nil
|
||||
private var debouncer: DispatchWorkItem? = nil
|
||||
|
||||
public init(_ fan: Fan, smc: UnsafePointer<SMCService>, width: CGFloat, callback: @escaping (() -> Void)) {
|
||||
public init(_ fan: Fan, width: CGFloat, callback: @escaping (() -> Void)) {
|
||||
self.fan = fan
|
||||
self.smc = smc
|
||||
self.sizeCallback = callback
|
||||
|
||||
let inset: CGFloat = 5
|
||||
@@ -204,7 +199,7 @@ internal class FanView: NSStackView {
|
||||
buttons.callback = { [weak self] (mode: FanMode) in
|
||||
self?.fan.mode = mode
|
||||
if let fan = self?.fan {
|
||||
self?.smc.pointee.setFanMode(fan.id, mode: mode)
|
||||
SMC.shared.setFanMode(fan.id, mode: mode)
|
||||
}
|
||||
self?.toggleMode()
|
||||
}
|
||||
@@ -295,7 +290,7 @@ internal class FanView: NSStackView {
|
||||
let task = DispatchWorkItem { [weak self] in
|
||||
DispatchQueue.global(qos: .userInteractive).async { [weak self] in
|
||||
if let id = self?.fan.id {
|
||||
self?.smc.pointee.setFanSpeed(id, speed: Int(value))
|
||||
SMC.shared.setFanSpeed(id, speed: Int(value))
|
||||
}
|
||||
DispatchQueue.main.async {
|
||||
field.textColor = .systemBlue
|
||||
|
||||
@@ -15,14 +15,12 @@ import StatsKit
|
||||
import os.log
|
||||
|
||||
internal class FansReader: Reader<[Fan]> {
|
||||
private var smc: UnsafePointer<SMCService>
|
||||
internal var list: [Fan] = []
|
||||
|
||||
init(_ smc: UnsafePointer<SMCService>) {
|
||||
self.smc = smc
|
||||
init() {
|
||||
super.init()
|
||||
|
||||
guard let count = smc.pointee.getValue("FNum") else {
|
||||
guard let count = SMC.shared.getValue("FNum") else {
|
||||
return
|
||||
}
|
||||
os_log(.debug, log: self.log, "Found %.0f fans", count)
|
||||
@@ -30,10 +28,10 @@ internal class FansReader: Reader<[Fan]> {
|
||||
for i in 0..<Int(count) {
|
||||
self.list.append(Fan(
|
||||
id: i,
|
||||
name: smc.pointee.getStringValue("F\(i)ID") ?? "Fan #\(i)",
|
||||
minSpeed: smc.pointee.getValue("F\(i)Mn") ?? 1,
|
||||
maxSpeed: smc.pointee.getValue("F\(i)Mx") ?? 1,
|
||||
value: smc.pointee.getValue("F\(i)Ac") ?? 0,
|
||||
name: SMC.shared.getStringValue("F\(i)ID") ?? "Fan #\(i)",
|
||||
minSpeed: SMC.shared.getValue("F\(i)Mn") ?? 1,
|
||||
maxSpeed: SMC.shared.getValue("F\(i)Mx") ?? 1,
|
||||
value: SMC.shared.getValue("F\(i)Ac") ?? 0,
|
||||
mode: self.getFanMode(i)
|
||||
))
|
||||
}
|
||||
@@ -41,13 +39,13 @@ internal class FansReader: Reader<[Fan]> {
|
||||
|
||||
public override func read() {
|
||||
for i in 0..<self.list.count {
|
||||
self.list[i].value = smc.pointee.getValue("F\(self.list[i].id)Ac") ?? 0
|
||||
self.list[i].value = SMC.shared.getValue("F\(self.list[i].id)Ac") ?? 0
|
||||
}
|
||||
self.callback(self.list)
|
||||
}
|
||||
|
||||
private func getFanMode(_ id: Int) -> FanMode {
|
||||
let fansMode: Int = Int(self.smc.pointee.getValue("FS! ") ?? 0)
|
||||
let fansMode: Int = Int(SMC.shared.getValue("FS! ") ?? 0)
|
||||
var mode: FanMode = .automatic
|
||||
|
||||
if fansMode == 0 {
|
||||
|
||||
@@ -62,8 +62,6 @@ public struct GPUs: value_t {
|
||||
}
|
||||
|
||||
public class GPU: Module {
|
||||
private let smc: UnsafePointer<SMCService>?
|
||||
|
||||
private var infoReader: InfoReader? = nil
|
||||
private var settingsView: Settings
|
||||
private var popupView: Popup = Popup()
|
||||
@@ -76,8 +74,7 @@ public class GPU: Module {
|
||||
}
|
||||
}
|
||||
|
||||
public init(_ smc: UnsafePointer<SMCService>) {
|
||||
self.smc = smc
|
||||
public init() {
|
||||
self.settingsView = Settings("GPU")
|
||||
|
||||
super.init(
|
||||
@@ -87,7 +84,6 @@ public class GPU: Module {
|
||||
guard self.available else { return }
|
||||
|
||||
self.infoReader = InfoReader()
|
||||
self.infoReader?.smc = smc
|
||||
self.selectedGPU = Store.shared.string(key: "\(self.config.name)_gpu", defaultValue: self.selectedGPU)
|
||||
|
||||
self.infoReader?.callbackHandler = { [unowned self] value in
|
||||
|
||||
@@ -27,8 +27,6 @@ let vendors: [Data: String] = [
|
||||
]
|
||||
|
||||
internal class InfoReader: Reader<GPUs> {
|
||||
internal var smc: UnsafePointer<SMCService>? = nil
|
||||
|
||||
private var gpus: GPUs = GPUs()
|
||||
private var devices: [device] = []
|
||||
|
||||
@@ -115,7 +113,7 @@ internal class InfoReader: Reader<GPUs> {
|
||||
type = .discrete
|
||||
|
||||
if temperature == nil || temperature == 0 {
|
||||
if let tmp = self.smc?.pointee.getValue("TGDD"), tmp != 128 {
|
||||
if let tmp = SMC.shared.getValue("TGDD"), tmp != 128 {
|
||||
temperature = Int(tmp)
|
||||
}
|
||||
}
|
||||
@@ -124,7 +122,7 @@ internal class InfoReader: Reader<GPUs> {
|
||||
type = .integrated
|
||||
|
||||
if temperature == nil || temperature == 0 {
|
||||
if let tmp = self.smc?.pointee.getValue("TCGC"), tmp != 128 {
|
||||
if let tmp = SMC.shared.getValue("TCGC"), tmp != 128 {
|
||||
temperature = Int(tmp)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -18,8 +18,8 @@ public class Sensors: Module {
|
||||
private let popupView: Popup = Popup()
|
||||
private var settingsView: Settings
|
||||
|
||||
public init(_ smc: UnsafePointer<SMCService>) {
|
||||
self.sensorsReader = SensorsReader(smc)
|
||||
public init() {
|
||||
self.sensorsReader = SensorsReader()
|
||||
self.settingsView = Settings("Sensors", list: &self.sensorsReader.list)
|
||||
|
||||
super.init(
|
||||
|
||||
@@ -16,14 +16,11 @@ import os.log
|
||||
|
||||
internal class SensorsReader: Reader<[Sensor_t]> {
|
||||
internal var list: [Sensor_t] = []
|
||||
private var smc: UnsafePointer<SMCService>
|
||||
|
||||
init(_ smc: UnsafePointer<SMCService>) {
|
||||
self.smc = smc
|
||||
|
||||
init() {
|
||||
super.init()
|
||||
|
||||
var available: [String] = self.smc.pointee.getAllKeys()
|
||||
var available: [String] = SMC.shared.getAllKeys()
|
||||
var list: [Sensor_t] = []
|
||||
|
||||
available = available.filter({ (key: String) -> Bool in
|
||||
@@ -56,7 +53,7 @@ internal class SensorsReader: Reader<[Sensor_t]> {
|
||||
}
|
||||
|
||||
for (index, sensor) in list.enumerated().reversed() {
|
||||
if let newValue = self.smc.pointee.getValue(sensor.key) {
|
||||
if let newValue = SMC.shared.getValue(sensor.key) {
|
||||
// Remove the temperature sensor, if SMC report more that 110 C degree.
|
||||
if sensor.type == SensorType.Temperature.rawValue && newValue > 110 {
|
||||
list.remove(at: index)
|
||||
@@ -74,7 +71,7 @@ internal class SensorsReader: Reader<[Sensor_t]> {
|
||||
|
||||
public override func read() {
|
||||
for i in 0..<self.list.count {
|
||||
if let newValue = self.smc.pointee.getValue(self.list[i].key) {
|
||||
if let newValue = SMC.shared.getValue(self.list[i].key) {
|
||||
self.list[i].value = newValue
|
||||
}
|
||||
}
|
||||
|
||||
@@ -8,8 +8,10 @@
|
||||
|
||||
import Cocoa
|
||||
import os.log
|
||||
|
||||
import StatsKit
|
||||
import ModuleKit
|
||||
|
||||
import CPU
|
||||
import RAM
|
||||
import Disk
|
||||
@@ -20,17 +22,16 @@ import GPU
|
||||
import Fans
|
||||
|
||||
let updater = macAppUpdater(user: "exelban", repo: "stats")
|
||||
var smc: SMCService = SMCService()
|
||||
var modules: [Module] = [
|
||||
Battery(),
|
||||
Network(),
|
||||
Fans(&smc),
|
||||
Sensors(&smc),
|
||||
Disk(),
|
||||
CPU(),
|
||||
GPU(),
|
||||
RAM(),
|
||||
GPU(&smc),
|
||||
CPU(&smc),
|
||||
].reversed()
|
||||
Disk(),
|
||||
Sensors(),
|
||||
Fans(),
|
||||
Network(),
|
||||
Battery(),
|
||||
]
|
||||
var log = OSLog(subsystem: Bundle.main.bundleIdentifier!, category: "Stats")
|
||||
|
||||
class AppDelegate: NSObject, NSApplicationDelegate, NSUserNotificationCenterDelegate {
|
||||
@@ -41,7 +42,6 @@ class AppDelegate: NSObject, NSApplicationDelegate, NSUserNotificationCenterDele
|
||||
|
||||
func applicationDidFinishLaunching(_ aNotification: Notification) {
|
||||
let startingPoint = Date()
|
||||
// print("------------", startingPoint, "------------", to: &Log.log)
|
||||
|
||||
self.parseArguments()
|
||||
self.parseVersion()
|
||||
@@ -60,7 +60,6 @@ class AppDelegate: NSObject, NSApplicationDelegate, NSUserNotificationCenterDele
|
||||
|
||||
func applicationWillTerminate(_ aNotification: Notification) {
|
||||
modules.forEach{ $0.terminate() }
|
||||
_ = smc.close()
|
||||
NotificationCenter.default.removeObserver(self)
|
||||
}
|
||||
|
||||
|
||||
@@ -104,7 +104,9 @@ internal struct SMCVal_t {
|
||||
}
|
||||
}
|
||||
|
||||
public class SMCService {
|
||||
public class SMC {
|
||||
public static let shared = SMC()
|
||||
|
||||
private var conn: io_connect_t = 0
|
||||
|
||||
public init() {
|
||||
@@ -134,6 +136,13 @@ public class SMCService {
|
||||
}
|
||||
}
|
||||
|
||||
deinit {
|
||||
let result = self.close()
|
||||
if (result != kIOReturnSuccess) {
|
||||
print("error close smc connection: " + (String(cString: mach_error_string(result), encoding: String.Encoding.ascii) ?? "unknown error"))
|
||||
}
|
||||
}
|
||||
|
||||
public func close() -> kern_return_t{
|
||||
return IOServiceClose(conn)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user