2020-08-17 17:30:09 +02:00
|
|
|
//
|
|
|
|
|
// main.swift
|
|
|
|
|
// GPU
|
|
|
|
|
//
|
|
|
|
|
// Created by Serhiy Mytrovtsiy on 17/08/2020.
|
|
|
|
|
// Using Swift 5.0.
|
|
|
|
|
// Running on macOS 10.15.
|
|
|
|
|
//
|
|
|
|
|
// Copyright © 2020 Serhiy Mytrovtsiy. All rights reserved.
|
|
|
|
|
//
|
|
|
|
|
|
|
|
|
|
import Cocoa
|
2021-06-04 19:37:29 +02:00
|
|
|
import Kit
|
2024-07-17 19:34:51 +02:00
|
|
|
import WidgetKit
|
2020-08-17 17:30:09 +02:00
|
|
|
|
2020-12-19 19:38:39 +01:00
|
|
|
public typealias GPU_type = String
|
|
|
|
|
public enum GPU_types: GPU_type {
|
|
|
|
|
case unknown = ""
|
|
|
|
|
|
|
|
|
|
case integrated = "i"
|
|
|
|
|
case external = "e"
|
|
|
|
|
case discrete = "d"
|
|
|
|
|
}
|
|
|
|
|
|
2023-06-27 17:09:38 +02:00
|
|
|
public struct GPU_Info: Codable {
|
2021-01-09 12:37:15 +01:00
|
|
|
public let id: String
|
2020-12-19 19:38:39 +01:00
|
|
|
public let type: GPU_type
|
2021-01-09 12:37:15 +01:00
|
|
|
|
|
|
|
|
public let IOClass: String
|
|
|
|
|
public var vendor: String? = nil
|
|
|
|
|
public let model: String
|
2022-01-14 17:26:06 +01:00
|
|
|
public var cores: Int? = nil
|
2021-01-09 12:37:15 +01:00
|
|
|
|
2020-12-31 11:57:25 +01:00
|
|
|
public var state: Bool = true
|
2020-08-17 20:37:20 +02:00
|
|
|
|
2021-01-09 12:37:15 +01:00
|
|
|
public var fanSpeed: Int? = nil
|
|
|
|
|
public var coreClock: Int? = nil
|
|
|
|
|
public var memoryClock: Int? = nil
|
|
|
|
|
public var temperature: Double? = nil
|
|
|
|
|
public var utilization: Double? = nil
|
2022-01-14 17:26:06 +01:00
|
|
|
public var renderUtilization: Double? = nil
|
|
|
|
|
public var tilerUtilization: Double? = nil
|
2021-01-09 12:37:15 +01:00
|
|
|
|
2024-07-17 19:34:51 +02:00
|
|
|
init(id: String, type: GPU_type, IOClass: String, vendor: String? = nil, model: String, cores: Int?, utilization: Double? = nil, render: Double? = nil, tiler: Double? = nil) {
|
2021-02-20 18:54:02 +01:00
|
|
|
self.id = id
|
2021-01-09 12:37:15 +01:00
|
|
|
self.type = type
|
|
|
|
|
self.IOClass = IOClass
|
|
|
|
|
self.vendor = vendor
|
|
|
|
|
self.model = model
|
2022-01-14 17:26:06 +01:00
|
|
|
self.cores = cores
|
2024-07-17 19:34:51 +02:00
|
|
|
self.utilization = utilization
|
|
|
|
|
self.renderUtilization = render
|
|
|
|
|
self.tilerUtilization = tiler
|
2021-01-09 12:37:15 +01:00
|
|
|
}
|
2025-04-05 22:54:01 +02:00
|
|
|
|
|
|
|
|
public func remote() -> String {
|
|
|
|
|
var id = self.id
|
|
|
|
|
if self.id.isEmpty {
|
|
|
|
|
id = "0"
|
|
|
|
|
}
|
|
|
|
|
return "\(id),1,\(self.utilization ?? 0),\(self.renderUtilization ?? 0),\(self.tilerUtilization ?? 0),,"
|
|
|
|
|
}
|
2020-08-17 20:37:20 +02:00
|
|
|
}
|
|
|
|
|
|
2025-04-05 22:54:01 +02:00
|
|
|
public class GPUs: Codable, RemoteType {
|
2025-03-04 20:49:52 +01:00
|
|
|
private var queue: DispatchQueue = DispatchQueue(label: "eu.exelban.Stats.GPU.SynchronizedArray")
|
|
|
|
|
|
|
|
|
|
private var _list: [GPU_Info] = []
|
|
|
|
|
public var list: [GPU_Info] {
|
|
|
|
|
get { self.queue.sync { self._list } }
|
|
|
|
|
set { self.queue.sync { self._list = newValue } }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
enum CodingKeys: String, CodingKey {
|
|
|
|
|
case list
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
required public init(from decoder: Decoder) throws {
|
|
|
|
|
let container = try decoder.container(keyedBy: CodingKeys.self)
|
|
|
|
|
self.list = try container.decode(Array<GPU_Info>.self, forKey: CodingKeys.list)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public func encode(to encoder: Encoder) throws {
|
|
|
|
|
var container = encoder.container(keyedBy: CodingKeys.self)
|
|
|
|
|
try container.encode(list, forKey: .list)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
init() {}
|
2020-08-17 20:37:20 +02:00
|
|
|
|
|
|
|
|
internal func active() -> [GPU_Info] {
|
2021-01-09 12:37:15 +01:00
|
|
|
return self.list.filter{ $0.state && $0.utilization != nil }.sorted{ $0.utilization ?? 0 > $1.utilization ?? 0 }
|
2020-08-17 20:37:20 +02:00
|
|
|
}
|
2025-04-05 22:54:01 +02:00
|
|
|
|
|
|
|
|
public func remote() -> Data? {
|
|
|
|
|
var string = "\(self.list.count),"
|
|
|
|
|
for (i, v) in self.list.enumerated() {
|
|
|
|
|
string += v.remote()
|
|
|
|
|
if i != self.list.count {
|
|
|
|
|
string += ","
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
string += "$"
|
|
|
|
|
return string.data(using: .utf8)
|
|
|
|
|
}
|
2020-08-17 20:37:20 +02:00
|
|
|
}
|
2020-08-17 17:30:09 +02:00
|
|
|
|
|
|
|
|
public class GPU: Module {
|
2023-02-25 11:46:59 +01:00
|
|
|
private let popupView: Popup
|
|
|
|
|
private let settingsView: Settings
|
|
|
|
|
private let portalView: Portal
|
2023-12-12 20:43:37 +01:00
|
|
|
private let notificationsView: Notifications
|
2023-02-25 11:46:59 +01:00
|
|
|
|
2020-08-17 20:37:20 +02:00
|
|
|
private var infoReader: InfoReader? = nil
|
|
|
|
|
|
|
|
|
|
private var selectedGPU: String = ""
|
2022-07-05 20:22:50 +02:00
|
|
|
private var notificationLevelState: Bool = false
|
|
|
|
|
private var notificationID: String? = nil
|
2020-08-17 17:30:09 +02:00
|
|
|
|
2020-12-19 19:38:39 +01:00
|
|
|
private var showType: Bool {
|
2025-12-03 19:02:40 +01:00
|
|
|
Store.shared.bool(key: "\(self.config.name)_showType", defaultValue: false)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private var systemWidgetsUpdatesState: Bool {
|
2026-02-14 18:39:49 +01:00
|
|
|
self.userDefaults?.bool(forKey: "systemWidgetsUpdates_state") ?? false
|
2020-12-19 19:38:39 +01:00
|
|
|
}
|
|
|
|
|
|
2021-03-25 22:14:20 +01:00
|
|
|
public init() {
|
2023-02-25 11:46:59 +01:00
|
|
|
self.popupView = Popup()
|
2024-02-10 17:03:47 +01:00
|
|
|
self.settingsView = Settings(.GPU)
|
2024-01-23 19:12:14 +01:00
|
|
|
self.portalView = Portal(.GPU)
|
2023-12-12 20:43:37 +01:00
|
|
|
self.notificationsView = Notifications(.GPU)
|
2020-08-17 17:30:09 +02:00
|
|
|
|
|
|
|
|
super.init(
|
2024-12-13 12:49:30 +01:00
|
|
|
moduleType: .GPU,
|
2020-08-17 20:37:20 +02:00
|
|
|
popup: self.popupView,
|
2023-02-25 11:46:59 +01:00
|
|
|
settings: self.settingsView,
|
2023-12-12 20:43:37 +01:00
|
|
|
portal: self.portalView,
|
|
|
|
|
notifications: self.notificationsView
|
2020-08-17 17:30:09 +02:00
|
|
|
)
|
|
|
|
|
guard self.available else { return }
|
|
|
|
|
|
2024-02-10 17:03:47 +01:00
|
|
|
self.infoReader = InfoReader(.GPU) { [weak self] value in
|
2023-08-13 13:15:27 +02:00
|
|
|
self?.infoCallback(value)
|
2020-08-17 20:37:20 +02:00
|
|
|
}
|
2024-02-10 17:03:47 +01:00
|
|
|
self.selectedGPU = Store.shared.string(key: "\(self.config.name)_gpu", defaultValue: self.selectedGPU)
|
2020-08-17 20:37:20 +02:00
|
|
|
|
2023-08-13 13:15:27 +02:00
|
|
|
self.settingsView.selectedGPUHandler = { [weak self] value in
|
|
|
|
|
self?.selectedGPU = value
|
|
|
|
|
self?.infoReader?.read()
|
2020-08-17 20:37:20 +02:00
|
|
|
}
|
2023-08-13 13:15:27 +02:00
|
|
|
self.settingsView.setInterval = { [weak self] value in
|
|
|
|
|
self?.infoReader?.setInterval(value)
|
2020-08-17 20:37:20 +02:00
|
|
|
}
|
2023-08-13 13:15:27 +02:00
|
|
|
self.settingsView.callback = { [weak self] in
|
|
|
|
|
self?.infoReader?.read()
|
2020-12-19 19:38:39 +01:00
|
|
|
}
|
2020-08-17 17:30:09 +02:00
|
|
|
|
2024-02-10 17:03:47 +01:00
|
|
|
self.setReaders([self.infoReader])
|
2020-08-17 17:30:09 +02:00
|
|
|
}
|
2020-08-17 20:37:20 +02:00
|
|
|
|
2020-12-19 17:25:11 +01:00
|
|
|
private func infoCallback(_ raw: GPUs?) {
|
2023-08-13 13:15:27 +02:00
|
|
|
guard raw != nil && !raw!.list.isEmpty, let value = raw, self.enabled else { return }
|
2020-08-17 20:37:20 +02:00
|
|
|
|
2020-11-26 15:35:30 +01:00
|
|
|
DispatchQueue.main.async(execute: {
|
2020-12-19 17:25:11 +01:00
|
|
|
self.popupView.infoCallback(value)
|
2020-11-26 15:35:30 +01:00
|
|
|
})
|
2020-12-19 17:25:11 +01:00
|
|
|
self.settingsView.setList(value)
|
2020-08-17 20:37:20 +02:00
|
|
|
|
2020-12-19 17:25:11 +01:00
|
|
|
let activeGPUs = value.active()
|
2020-12-19 19:38:39 +01:00
|
|
|
guard let activeGPU = activeGPUs.first(where: { $0.state }) ?? activeGPUs.first else {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
let selectedGPU: GPU_Info = activeGPUs.first{ $0.model == self.selectedGPU } ?? activeGPU
|
2021-01-09 12:37:15 +01:00
|
|
|
guard let utilization = selectedGPU.utilization else {
|
|
|
|
|
return
|
|
|
|
|
}
|
2020-08-17 20:37:20 +02:00
|
|
|
|
2024-01-23 19:12:14 +01:00
|
|
|
self.portalView.callback(selectedGPU)
|
2023-12-12 20:43:37 +01:00
|
|
|
self.notificationsView.usageCallback(utilization)
|
2022-07-05 20:22:50 +02:00
|
|
|
|
2024-07-04 19:52:52 +02:00
|
|
|
self.menuBar.widgets.filter{ $0.isActive }.forEach { (w: SWidget) in
|
2021-02-13 16:39:11 +01:00
|
|
|
switch w.item {
|
|
|
|
|
case let widget as Mini:
|
|
|
|
|
widget.setValue(utilization)
|
|
|
|
|
widget.setTitle(self.showType ? "\(selectedGPU.type)GPU" : nil)
|
|
|
|
|
case let widget as LineChart: widget.setValue(utilization)
|
2021-08-02 20:11:48 +02:00
|
|
|
case let widget as BarChart: widget.setValue([[ColorValue(utilization)]])
|
2021-10-13 18:27:31 +02:00
|
|
|
case let widget as Tachometer:
|
|
|
|
|
widget.setValue([
|
|
|
|
|
circle_segment(value: utilization, color: NSColor.systemBlue)
|
|
|
|
|
])
|
2021-02-13 16:39:11 +01:00
|
|
|
default: break
|
|
|
|
|
}
|
2020-08-17 20:37:20 +02:00
|
|
|
}
|
2024-07-17 19:34:51 +02:00
|
|
|
|
2025-12-03 19:02:40 +01:00
|
|
|
if self.systemWidgetsUpdatesState {
|
2026-02-22 15:52:36 +01:00
|
|
|
if isWidgetActive(self.userDefaults, [GPU_entry.kind, "UnitedWidget"]), let blobData = try? JSONEncoder().encode(selectedGPU) {
|
|
|
|
|
self.userDefaults?.set(blobData, forKey: "GPU@InfoReader")
|
2025-11-14 11:12:06 +01:00
|
|
|
}
|
2026-02-22 15:52:36 +01:00
|
|
|
WidgetCenter.shared.reloadTimelines(ofKind: GPU_entry.kind)
|
|
|
|
|
WidgetCenter.shared.reloadTimelines(ofKind: "UnitedWidget")
|
2024-07-17 19:34:51 +02:00
|
|
|
}
|
2020-08-17 20:37:20 +02:00
|
|
|
}
|
2020-08-17 17:30:09 +02:00
|
|
|
}
|