-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathToggleHDR.swift
171 lines (143 loc) · 5.78 KB
/
ToggleHDR.swift
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
import Cocoa
import ColorSync
import Foundation
let userDefaultsSuiteName = "com.alin23.ToggleHDR"
func toggleHDR(display: MPDisplay, enabled: Bool? = nil, try60Hz: Bool = false) {
let id = display.displayID
let name = display.displayName ?? ""
let hdrIsCurrentlyEnabled = display.preferHDRModes()
let newHDRState = enabled ?? !hdrIsCurrentlyEnabled
guard newHDRState != hdrIsCurrentlyEnabled else {
print("HDR is already \(newHDRState ? "enabled" : "disabled") for \(name) [ID: \(id)]")
return
}
if try60Hz, newHDRState, !display.hasHDRModes, let scanRate = display.currentMode.scanRate,
scanRate.intValue > 60, forceEnableHDR60Hertz(display: display)
{
display.setPreferHDRModes(true)
return
}
guard display.hasHDRModes else {
print("The display does not support HDR control: \(name) [ID: \(id)]")
return
}
updatePreferHDR(display: display, enabled: newHDRState)
}
func printDisplays(_ displays: [MPDisplay]) {
print("ID\tUUID \tSupports HDR\tHDR Enabled\tName")
for panel in displays {
print(
"\(panel.displayID)\t\(panel.uuid?.uuidString ?? "")\t\(panel.hasHDRModes) \t\(panel.preferHDRModes()) \t\(panel.displayName ?? "Unknown name")"
)
}
}
func bool(_ arg: String) -> Bool? {
if ["on", "true", "yes"].contains(arg) || arg.starts(with: "enable") {
return true
}
if ["off", "false", "no"].contains(arg) || arg.starts(with: "disable") {
return false
}
return nil
}
func updatePreferHDR(display: MPDisplay, enabled: Bool) {
let id = display.displayID
let name = display.displayName ?? ""
print("\(enabled ? "Enabling" : "Disabling") HDR for \(name) [ID: \(id)]")
if !enabled, let ud = UserDefaults(suiteName: userDefaultsSuiteName) {
// We are currently in HDR mode. Check to see if we have a stored SDR
// mode number for the current display, and if that mode number can
// successfully be used to create an MPDisplayMode.
let uuid = display.uuid?.uuidString ?? name
let udKey = "SDRModeNumber-\(uuid)"
let sdrModeNumber = ud.integer(forKey: udKey).i32
if sdrModeNumber > 0, let newMode = display.mode(withNumber: sdrModeNumber) {
display.setMode(newMode)
ud.removeObject(forKey: udKey)
return
}
}
display.setPreferHDRModes(enabled)
}
func forceEnableHDR60Hertz(display: MPDisplay) -> Bool {
// macOS doesn't think this display supports HDR; see if it supports HDR when set to 60 Hertz
guard let ud = UserDefaults(suiteName: userDefaultsSuiteName),
let scanRates = display.scanRates, scanRates.contains(where: { $0 == 60 }),
let newMode = display.mode(matchingResolutionOf: display.currentMode, withScanRate: 60)
else {
return false
}
// We found a 60 Hertz version of the current display mode. Before
// we make any changes, store the CURRENT display mode in User
// Defaults so that we can restore it later.
let modeNumber = display.currentMode.modeNumber
let uuid = display.uuid?.uuidString ?? display.displayName ?? ""
ud.setValue(modeNumber, forKey: "SDRModeNumber-\(uuid)")
// Store the current mode so that if the 60 Hertz mode doesn't support HDR we can switch back.
let originalMode = display.currentMode
// Now switch to the 60 Hertz version of the current display mode.
display.setMode(newMode)
guard display.hasHDRModes else {
// Even at 60 Hertz, HDR isn't supported; reset back to the original mode.
display.setMode(originalMode)
return false
}
return true
}
func printHelp() {
print("""
Usage: \(CommandLine.arguments[0]) [id/uuid/name/all] [on/off] [--try-60-hz]
Options:
• id/uuid/name: The display to toggle HDR on/off for.
• all: Toggle HDR on/off for all displays that support it.
• on/off: Enable or disable HDR. If not provided, the opposite of the current state is used.
• --try-60-hz: If the display doesn't support HDR at the current refresh rate, it will try switching to 60 Hertz.
""")
}
func main() {
guard let mgr = MPDisplayMgr(), let displays = mgr.displays else { return }
let try60Hz = CommandLine.arguments.contains("--try-60-hz")
let args = CommandLine.arguments.filter { arg in !["--try-60-hz"].contains(arg) }
guard args.count >= 2 else {
if displays.count == 1, displays[0].hasHDRModes || try60Hz {
// If there is only one display, toggle the HDR on that display.
toggleHDR(display: displays[0], try60Hz: try60Hz)
return
}
printHelp()
printDisplays(displays)
return
}
if args.contains("--help") || args.contains("-h") {
printHelp()
printDisplays(displays)
return
}
defer {
print("")
printDisplays(displays)
}
let arg = args[1].lowercased()
// Example: `ToggleHDR on` or `ToggleHDR off`
if let enabled = bool(arg) {
for display in displays.filter({ $0.hasHDRModes || try60Hz }) {
toggleHDR(display: display, enabled: enabled, try60Hz: try60Hz)
}
return
}
let enabled = args.count >= 3 ? bool(args[2].lowercased()) : nil
// Example: `ToggleHDR all` or `ToggleHDR all on`
if arg == "all" {
for display in displays.filter({ $0.hasHDRModes || try60Hz }) {
toggleHDR(display: display, enabled: enabled, try60Hz: try60Hz)
}
return
}
// Example: `ToggleHDR DELL` or `ToggleHDR DELL on`
guard let display = mgr.matchDisplay(filter: arg) else {
print("No display found for query: \(arg)")
return
}
toggleHDR(display: display, enabled: enabled, try60Hz: try60Hz)
}
main()