forked from th507/screen-resolution-switcher
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscres.swift
executable file
·288 lines (240 loc) · 8.75 KB
/
scres.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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
#!/usr/bin/env xcrun -sdk macosx swift
//
// x2.swift
//
//
// Created by john on 20/1/2020.
//
import Foundation
import ApplicationServices
import CoreVideo
import OSAKit
import IOKit
class DisplayManager {
let displayID:CGDirectDisplayID, displayInfo:[DisplayInfo], modes:[CGDisplayMode], modeIndex:Int
init(_ _displayID:CGDirectDisplayID) {
displayID = _displayID
var modesArray:[CGDisplayMode]?
if let modeList = CGDisplayCopyAllDisplayModes(displayID, [kCGDisplayShowDuplicateLowResolutionModes:kCFBooleanTrue] as CFDictionary) {
modesArray = (modeList as! Array).filter { ($0 as CGDisplayMode).isUsableForDesktopGUI() }
} else {
print("Unable to get display modes")
}
modes = modesArray!
displayInfo = modes.map { DisplayInfo(displayID:_displayID, mode:$0) }
let mode = CGDisplayCopyDisplayMode(displayID)!
modeIndex = modes.firstIndex(of:mode)!
}
private func _format(_ di:DisplayInfo, leadingString:String, trailingString:String) -> String {
// We assume that 5 digits are enough to hold dimensions.
// 100K monitor users will just have to live with a bit of formatting misalignment.
return String(
format:" %@ %5d x %4d @ %dx @ %dHz%@",
leadingString,
di.width, di.height,
di.scale, di.frequency,
trailingString
)
}
func printForOneDisplay(_ leadingString:String) {
print(_format(displayInfo[modeIndex], leadingString:leadingString, trailingString:""))
}
func printFormatForAllModes() {
var i = 0
displayInfo.forEach { di in
let bool = i == modeIndex
print(_format(di, leadingString: bool ? "\u{001B}[0;33m⮕" : " ", trailingString: bool ? "\u{001B}[0;49m" : ""))
i += 1
}
}
private func _set(_ mi:Int) {
let mode:CGDisplayMode = modes[mi]
print("Setting display mode")
var config:CGDisplayConfigRef?
let error:CGError = CGBeginDisplayConfiguration(&config)
if error == .success {
CGConfigureDisplayWithDisplayMode(config, displayID, mode, nil)
let afterCheck = CGCompleteDisplayConfiguration(config, CGConfigureOption.permanently)
if afterCheck != .success { CGCancelDisplayConfiguration(config) }
}
}
func set(with setting: DisplayUserSetting) {
if let mi = displayInfo.firstIndex(where: { setting ~= $0 }) {
if mi != modeIndex { _set(mi) }
} else {
print("This mode is unavailable")
}
}
}
// return width, height and frequency info for corresponding displayID
struct DisplayInfo {
static let MAX_SCALE = 10
var width, height, scale, frequency:Int
init(displayID:CGDirectDisplayID, mode:CGDisplayMode) {
width = mode.width
height = mode.height
scale = mode.pixelWidth / mode.width;
frequency = Int( mode.refreshRate )
if frequency == 0 {
var link:CVDisplayLink?
CVDisplayLinkCreateWithCGDisplay(displayID, &link)
let time = CVDisplayLinkGetNominalOutputVideoRefreshPeriod(link!)
// timeValue is in fact already in Int64
let timeScale = Int64(time.timeScale) + time.timeValue / 2
frequency = Int( timeScale / time.timeValue )
}
}
static func ~= (lhs: Self, rhs: DisplayUserSetting) -> Bool {
return rhs ~= lhs
}
}
// Supported command calls:
// 1 width => 2
// 2 id, width
// 3 width, scale => 6
// 4 width, height => 5
// 5 id, width, height
// 6 id, width, scale
// 7 id, width, height, scale
struct DisplayUserSetting {
var displayIndex = 0, width = 0
var height, scale:Int?
init(_ arr:[String]) {
var args = arr.compactMap { Int($0) }
if args.count < 1 { return }
if args[0] > Screens.MAX_DISPLAYS { args.insert(0 /* displayIndex */, at:0) }
if args.count < 2 { return }
displayIndex = args[0]
width = args[1]
if args.count == 2 { return }
if args[2] > DisplayInfo.MAX_SCALE {
height = args[2]
if args.count > 3 { scale = args[3] }
}
else {
scale = args[2]
if args.count > 3 { height = args[3] }
}
}
// override a lesser-used operator to simplify diplay mode checks
static func ~= (lhs: Self, rhs: DisplayInfo) -> Bool {
var bool = lhs.width == rhs.width
if lhs.height != nil { bool = bool && lhs.height == rhs.height }
if lhs.scale != nil { bool = bool && lhs.scale == rhs.scale }
return bool
}
}
class Screens {
// assume at most 8 display connected
static let MAX_DISPLAYS = 8
var maxDisplays = MAX_DISPLAYS
// actual number of display
var displayCount:Int = 0
var dm = [DisplayManager]()
init() {
// actual number of display
var displayCount32:UInt32 = 0
var displayIDs = [CGDirectDisplayID](arrayLiteral: 0)
guard CGGetOnlineDisplayList(UInt32(maxDisplays), &displayIDs, &displayCount32) == .success else {
print("Error on getting online display List.")
return
}
displayCount = Int( displayCount32 )
dm = displayIDs.map { DisplayManager($0) }
}
// print a list of all displays
// used by -l
func listDisplays() {
for (i, m) in dm.enumerated() {
m.printForOneDisplay("Display \(i):")
}
}
func listModes(_ displayIndex:Int) {
dm[displayIndex].printFormatForAllModes()
}
func set(with setting:DisplayUserSetting) {
dm[setting.displayIndex].set(with:setting)
}
}
// darkMode toggle code with JXA ;-)
// Method from Stackoverflow User: bacongravy
// https://stackoverflow.com/questions/44209057
struct DarkMode {
static let scriptString = """
pref = Application(\"System Events\").appearancePreferences
pref.darkMode = !pref.darkMode()
"""
let script = OSAScript.init(source: scriptString, language: OSALanguage.init(forName: "JavaScript"))
init() {
var compileError: NSDictionary?
script.compileAndReturnError(&compileError)
}
func toggle() {
var scriptError: NSDictionary?
if let result = script.executeAndReturnError(&scriptError)?.stringValue { print("Dark Mode:", result) }
}
}
func sleepDisplay() {
let r = IORegistryEntryFromPath(kIOMasterPortDefault, strdup("IOService:/IOResources/IODisplayWrangler"))
IORegistryEntrySetCFProperty(r, ("IORequestIdle" as CFString), kCFBooleanTrue)
IOObjectRelease(r)
}
func seeHelp() {
print("""
Usage:
screen-resolution-switcher [-h|--help] [-l|--list|list] [-m|--mode|mode displayIndex]
[-s|--set|set displayIndex width scale] [-r|--set-retina|retina displayIndex width],
Here are some examples:
-h get help
-l list displays
-m 0 list all mode from a certain display
-m shorthand for -m 0
-s 0 800 600 1 set resolution of display 0 to 800 x 600 @ 1x [@ 60Hz]
-s 0 800 600 set resolution of display 0 to 800 x 600 @(highest scale factor)
-s 0 800 1 set resolution of display 0 to 800 [x 600] @ 1x [@ 60Hz]
-s 0 800 shorthand for -s 0 800 2 (highest scale factor)
-s 800 shorthand for -s 0 800 2 (highest scale factor)
-r 0 800 shorthand for -s 0 800 2
-r 800 shorthand for -s 0 800 2
-d toggle macOS Dark Mode
-sl sleep display
""")
}
func main() {
let screens = Screens()
let arguments = CommandLine.arguments
let count = arguments.count
guard count >= 2 else {
seeHelp()
return
}
switch arguments[1] {
case "-l", "--list", "list":
screens.listDisplays()
case "-m", "--mode", "mode":
var displayIndex = 0
if count > 2, let index = Int(arguments[2]) {
displayIndex = index
}
if displayIndex < screens.displayCount {
print("Supported Modes for Display \(displayIndex):")
screens.listModes(displayIndex)
} else {
print("Display index not found. List all available displays by:\n screen-resolution-switcher -l")
}
case "-s", "--set", "set", "-r", "--set-retina", "retina":
screens.set(with:DisplayUserSetting( arguments ))
case "-d", "--toggle-dark-mode":
DarkMode().toggle()
case "-sl", "--sleep", "sleep":
sleepDisplay()
default:
seeHelp()
}
}
#if os(macOS)
// run it
main()
#else
print("This script currently only runs on macOS")
#endif