forked from p00ya/displaymode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
displaymode.c
342 lines (303 loc) · 10.7 KB
/
displaymode.c
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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
// displaymode - a utility for changing the display resolution on Mac OS X.
//
// Copyright 2019, 2020 Dean Scarff.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// Compilation:
// clang -std=c11 -framework CoreFoundation -framework CoreGraphics -o displaymode displaymode.c
//
// Usage (to change the resolution to 1440x900):
// displaymode t 1440 900
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <CoreFoundation/CoreFoundation.h>
#include <CoreGraphics/CoreGraphics.h>
#include <MacTypes.h>
// Name and version to display with "v" option.
const char kProgramVersion[] = "displaymode 1.3.1";
// States for the main invocation "option".
//
// The enum value of the alphabetical options matches the letter that should
// be used on the command line.
enum Option {
kOptionMissing = 0,
kOptionInvalid = 1,
kOptionInvalidMode = 2,
kOptionSupportedModes = 'd',
kOptionHelp = 'h',
kOptionConfigureMode = 't',
kOptionVersion = 'v',
};
// Positions in argv of various expected parameters.
enum {
kArgvOptionIndex = 1,
kArgvWidthIndex = 2,
kArgvHeightIndex = 3,
kArgvDisplayIndex = 4,
};
const uint32_t kMaxDisplays = 32;
// Represents the command-line arguments after parsing.
struct ParsedArgs {
enum Option option;
const char * literal_option;
unsigned long width;
unsigned long height;
uint32_t display_index;
};
// Parses the "width height [display]" mode specification.
void ParseMode(const int argc, const char * argv[],
struct ParsedArgs * parsed_args) {
if (argc <= kArgvHeightIndex) {
parsed_args->option = kOptionInvalidMode;
return;
}
errno = 0;
const unsigned long width =
strtoul(argv[kArgvWidthIndex], NULL, 10);
if (errno != 0) {
fprintf(stderr, "Error parsing width \"%s\": %s\n",
argv[kArgvWidthIndex], strerror(errno));
errno = 0;
parsed_args->option = kOptionInvalidMode;
}
const unsigned long height =
strtoul(argv[kArgvHeightIndex], NULL, 10);
if (errno != 0) {
fprintf(stderr, "Error parsing height \"%s\": %s\n",
argv[kArgvHeightIndex], strerror(errno));
errno = 0;
parsed_args->option = kOptionInvalidMode;
}
if (kArgvDisplayIndex < argc) {
parsed_args->display_index =
(uint32_t) strtoul(argv[kArgvDisplayIndex], NULL, 10);
if (errno != 0) {
fprintf(stderr, "Error parsing display \"%s\": %s\n",
argv[kArgvDisplayIndex], strerror(errno));
errno = 0;
parsed_args->option = kOptionInvalidMode;
}
}
if (0 < width && 0 < height) {
parsed_args->width = width;
parsed_args->height = height;
} else {
parsed_args->option = kOptionInvalidMode;
}
}
// Parses the command-line arguments and returns them.
struct ParsedArgs ParseArgs(int argc, const char * argv[]) {
struct ParsedArgs parsed_args = { 0 };
if (argc <= 1) {
return parsed_args;
}
if (1 != strlen(argv[kArgvOptionIndex])) {
return parsed_args;
}
// Validate the command.
parsed_args.literal_option = argv[kArgvOptionIndex];
// All options are single-letter.
const char option = argv[kArgvOptionIndex][0];
switch (option) {
case kOptionSupportedModes:
case kOptionHelp:
case kOptionConfigureMode:
case kOptionVersion:
parsed_args.option = option;
break;
}
if (option == kOptionConfigureMode) {
ParseMode(argc, argv, &parsed_args);
}
return parsed_args;
}
const char kUsage[] =
"Usage:\n\n"
" displaymode [options...]\n\n"
"Options:\n"
" t <width> <height> [display]\n"
" sets the display's resolution to width x height\n\n"
" d\n"
" prints available resolutions for each display\n\n"
" h\n"
" prints this message\n\n"
" v\n"
" prints version and copyright notice\n";
// Prints a message describing how to invoke the tool on the command line.
void ShowUsage() {
puts(kUsage);
}
// Prints the resolution and refresh rate for a display mode.
void PrintMode(CGDisplayModeRef mode) {
const size_t width = CGDisplayModeGetWidth(mode);
const size_t height = CGDisplayModeGetHeight(mode);
const double refresh_rate = CGDisplayModeGetRefreshRate(mode);
const bool usable_for_desktop =
CGDisplayModeIsUsableForDesktopGUI(mode);
printf("%zu x %zu @%.1fHz%s", width, height, refresh_rate,
usable_for_desktop ? "" : " !");
}
// Prints all display modes for the main display. Returns 0 on success.
int PrintModes(CGDirectDisplayID display) {
CGDisplayModeRef current_mode = CGDisplayCopyDisplayMode(display);
CFArrayRef modes = CGDisplayCopyAllDisplayModes(display, NULL);
const CFIndex count = CFArrayGetCount(modes);
Boolean has_current = 0;
for (CFIndex i = 0; i < count; ++i) {
CGDisplayModeRef mode =
(CGDisplayModeRef) CFArrayGetValueAtIndex(modes, i);
PrintMode(mode);
if (CFEqual(mode, current_mode)) {
has_current = 1;
puts(" *");
} else {
puts("");
}
}
if (!has_current) {
PrintMode(current_mode);
puts(" *");
}
CFRelease(modes);
CGDisplayModeRelease(current_mode);
return EXIT_SUCCESS;
}
int PrintModesForAllDisplays() {
CGDirectDisplayID displays[kMaxDisplays];
uint32_t num_displays;
CGError e =
CGGetActiveDisplayList(kMaxDisplays, &displays[0], &num_displays);
if (e) {
fprintf(stderr, "CGGetActiveDisplayList CGError: %d\n", e);
return e;
}
for (uint32_t i = 0; i < num_displays; ++i) {
printf("%sDisplay %u%s:\n", i == 0 ? "" : "\n", i, i == 0 ? " (MAIN)" : "");
PrintModes(displays[i]);
}
return EXIT_SUCCESS;
}
// Returns the display ID (arbitrary integers) corresponding to the given
// display index (0-indexed).
CGError GetDisplayID(uint32_t display_index, CGDirectDisplayID * display) {
CGDirectDisplayID displays[kMaxDisplays];
uint32_t num_displays;
CGError e =
CGGetActiveDisplayList(kMaxDisplays, &displays[0], &num_displays);
if (e) {
fprintf(stderr, "CGGetActiveDisplayList CGError: %d\n", e);
return e;
}
if (num_displays <= display_index) {
fprintf(stderr, "Display %u not supported; display must be < %u\n",
display_index, num_displays);
return kCGErrorRangeCheck;
}
*display = displays[display_index];
return kCGErrorSuccess;
}
// Returns the first mode whose resolution matches the width and height
// specified in `parsed_args'. Returns NULL if no modes matched.
// The caller owns the returned mode.
CGDisplayModeRef GetModeMatching(const struct ParsedArgs * parsed_args,
const CGDirectDisplayID display) {
CFArrayRef modes = CGDisplayCopyAllDisplayModes(display, NULL);
const CFIndex count = CFArrayGetCount(modes);
CGDisplayModeRef matched_mode = NULL;
// Set matched_mode to the first display mode matching the requested
// resolution.
for (CFIndex i = 0; i < count; ++i) {
CGDisplayModeRef const mode =
(CGDisplayModeRef) CFArrayGetValueAtIndex(modes, i);
const size_t width = CGDisplayModeGetWidth(mode);
const size_t height = CGDisplayModeGetHeight(mode);
if (width == parsed_args->width &&
height == parsed_args->height) {
matched_mode = CGDisplayModeRetain(mode);
break;
}
}
CFRelease(modes);
return matched_mode;
}
// Changes the resolution permanently for the user.
int ConfigureMode(const struct ParsedArgs * parsed_args) {
CGDirectDisplayID display;
CGError e;
if ((e = GetDisplayID(parsed_args->display_index, &display))) {
return e;
}
CGDisplayModeRef mode = GetModeMatching(parsed_args, display);
if (NULL == mode) {
fprintf(stderr, "Could not find a mode for resolution %lux%lu\n",
parsed_args->width, parsed_args->height);
return -1;
}
// Save the original resolution.
CGDisplayModeRef original_mode = CGDisplayCopyDisplayMode(display);
const size_t original_width = CGDisplayModeGetWidth(original_mode);
const size_t original_height = CGDisplayModeGetHeight(original_mode);
CGDisplayModeRelease(original_mode);
// Change the resolution.
CGDisplayConfigRef config;
if ((e = CGBeginDisplayConfiguration(&config))) {
fprintf(stderr, "CGBeginDisplayConfiguration CGError: %d\n", e);
return e;
}
if ((e = CGConfigureDisplayWithDisplayMode(config, display, mode, NULL))) {
fprintf(stderr, "CGConfigureDisplayWithDisplayMode CGError: %d\n", e);
return e;
}
if ((e = CGCompleteDisplayConfiguration(config, kCGConfigurePermanently))) {
fprintf(stderr, "CGCompleteDisplayConfiguration CGError: %d\n", e);
return e;
}
CGDisplayModeRelease(mode);
printf("Changed display resolution from %zux%zu to %lux%lu\n",
original_width, original_height,
parsed_args->width, parsed_args->height);
return EXIT_SUCCESS;
}
int main(int argc, const char * argv[]) {
const struct ParsedArgs parsed_args = ParseArgs(argc, argv);
switch (parsed_args.option) {
case kOptionMissing:
fputs("Missing option; server mode is not supported\n\n", stderr);
ShowUsage();
break;
case kOptionInvalid:
fprintf(stderr, "Invalid option: '%s'\n\n",
parsed_args.literal_option);
ShowUsage();
break;
case kOptionInvalidMode:
fputs("Invalid mode\n", stderr);
break;
case kOptionConfigureMode:
return ConfigureMode(&parsed_args);
case kOptionHelp:
ShowUsage();
return EXIT_SUCCESS;
case kOptionSupportedModes:
return PrintModesForAllDisplays();
case kOptionVersion:
printf("%s\nCopyright 2019-2020 Dean Scarff\n", kProgramVersion);
return EXIT_SUCCESS;
default:
break;
}
return EXIT_FAILURE;
}