Repository navigation
Expand file tree
/
Copy pathOSDUtils.swift
More file actions
146 lines (130 loc) · 5.02 KB
/
Copy pathOSDUtils.swift
File metadata and controls
146 lines (130 loc) · 5.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
// Copyright © MonitorControl. @victorchabbert, @JoniVR, @theOneyouseek, @waydabber and others
import Cocoa
import os.log
class OSDUtils: NSObject {
enum OSDImage: Int64 {
case brightness = 1
case audioSpeaker = 3
case audioSpeakerMuted = 4
case contrast = 0
}
static func getOSDImageByCommand(command: Command, value: Float = 1) -> OSDImage {
var osdImage: OSDImage
switch command {
case .audioSpeakerVolume: osdImage = value > 0 ? .audioSpeaker : .audioSpeakerMuted
case .audioMuteScreenBlank: osdImage = .audioSpeakerMuted
case .contrast: osdImage = .contrast
default: osdImage = .brightness
}
return osdImage
}
static func showOsd(displayID: CGDirectDisplayID, command: Command, value: Float, maxValue: Float = 1, roundChiclet: Bool = false, lock: Bool = false) {
self.withOSD { manager in
let osdImage = self.getOSDImageByCommand(command: command, value: value)
let filledChiclets: Int
let totalChiclets: Int
if roundChiclet {
let osdChiclet = OSDUtils.chiclet(fromValue: value, maxValue: maxValue)
filledChiclets = Int(round(osdChiclet))
totalChiclets = 16
} else {
filledChiclets = Int(value * 100)
totalChiclets = Int(maxValue * 100)
}
manager.showImage(osdImage.rawValue, onDisplayID: displayID, priority: 0x1F4, msecUntilFade: 1000, filledChiclets: UInt32(filledChiclets), totalChiclets: UInt32(totalChiclets), locked: lock)
}
}
static func showOsdVolumeDisabled(displayID: CGDirectDisplayID) {
self.withOSD { manager in
manager.showImage(22, onDisplayID: displayID, priority: 0x1F4, msecUntilFade: 1000)
}
}
static func showOsdMuteDisabled(displayID: CGDirectDisplayID) {
self.withOSD { manager in
manager.showImage(21, onDisplayID: displayID, priority: 0x1F4, msecUntilFade: 1000)
}
}
static func popEmptyOsd(displayID: CGDirectDisplayID, command: Command) {
self.withOSD { manager in
let osdImage = self.getOSDImageByCommand(command: command)
manager.showImage(osdImage.rawValue, onDisplayID: displayID, priority: 0x1F4, msecUntilFade: 0)
}
}
private static func withOSD(_ show: @escaping (OSDUIHelperProtocol) -> Void) {
if #available(macOS 26.0, *), ProcessInfo.processInfo.operatingSystemVersion.majorVersion <= 27 {
TraditionalOSD.show(show)
} else if let manager = OSDManager.sharedManager() as? OSDManager {
show(manager)
}
}
static let chicletCount: Float = 16
static func chiclet(fromValue value: Float, maxValue: Float, half: Bool = false) -> Float {
(value * self.chicletCount * (half ? 2 : 1)) / maxValue
}
static func value(fromChiclet chiclet: Float, maxValue: Float, half: Bool = false) -> Float {
(chiclet * maxValue) / (self.chicletCount * (half ? 2 : 1))
}
static func getDistance(fromNearestChiclet chiclet: Float) -> Float {
abs(chiclet.rounded(.towardZero) - chiclet)
}
}
@available(macOS 26.0, *)
private enum TraditionalOSD {
private static var connection: NSXPCConnection?
static func show(_ show: @escaping (OSDUIHelperProtocol) -> Void) {
guard Thread.isMainThread else {
DispatchQueue.main.async {
self.show(show)
}
return
}
guard let helper = self.helper() else { return }
show(helper)
}
private static func helper() -> OSDUIHelperProtocol? {
let connection: NSXPCConnection
if let existingConnection = self.connection {
connection = existingConnection
} else {
connection = NSXPCConnection(machServiceName: "com.apple.OSDUIHelper", options: [])
connection.remoteObjectInterface = NSXPCInterface(with: OSDUIHelperProtocol.self)
connection.interruptionHandler = { [weak connection] in
os_log("Traditional OSD XPC connection interrupted", type: .error)
guard let connection = connection else { return }
DispatchQueue.main.async {
self.reset(connection)
}
}
connection.invalidationHandler = { [weak connection] in
guard let connection = connection else { return }
DispatchQueue.main.async {
if self.connection === connection {
self.connection = nil
}
}
}
self.connection = connection
connection.resume()
}
let proxy = connection.remoteObjectProxyWithErrorHandler { [weak connection] error in
os_log("Traditional OSD XPC proxy error: %{public}@", type: .error, error.localizedDescription)
guard let connection = connection else { return }
DispatchQueue.main.async {
self.reset(connection)
}
}
guard let helper = proxy as? OSDUIHelperProtocol else {
os_log("Unable to obtain Traditional OSD XPC proxy", type: .error)
self.reset(connection)
return nil
}
return helper
}
private static func reset(_ connection: NSXPCConnection) {
guard self.connection === connection else { return }
connection.interruptionHandler = nil
connection.invalidationHandler = nil
connection.invalidate()
self.connection = nil
}
}