-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdisplayutil_resolution.m
More file actions
312 lines (256 loc) · 9.15 KB
/
displayutil_resolution.m
File metadata and controls
312 lines (256 loc) · 9.15 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
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
/*
displayutil - displayutil_resolution.m
set the resolution for a display
History:
v. 1.0.0 (05/06/2022) - Initial working version
v. 1.0.1 (06/03/2022) - clean up usage message
v. 1.0.2 (11/12/2022) - update usage message
Copyright (c) 2022 Sriranga R. Veeraraghavan <ranga@calalum.org>
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:
The above copyright notice and this permission notice shall be included
in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#import <stdio.h>
#import <ApplicationServices/ApplicationServices.h>
#import "displayutil_display.h"
#import "displayutil_argutils.h"
#import "displayutil_resolution.h"
/* strings to select the resolution mode */
const char *gStrModeResolutionLong = "resolution";
const char *gStrModeResolutionShort = "rs";
/* informational / error messages */
static const char *gStrErrGetDisplayModes =
"cannot get display mode information";
static const char *gStrErrGetResolutionUnavailable =
"resolution not found/available";
static const char *gStrErrCantConfigure =
"cannot configure display";
static const char *gStrErrCantComplete =
"cannot complete display configuration";
/* private functions */
/* public functions */
/* printResolutionUsage - print usage message for resolution mode */
void printResolutionUsage(void)
{
fprintf(stderr,
"%s [%s|%s] [%s|%s] [[width] [height] [pts]]\n",
gPgmName,
gStrModeResolutionShort,
gStrModeResolutionLong,
gStrMain,
gStrDisp);
}
/* setResolutionForDisplay - sets the resolution for the specified
display */
bool setResolutionForMainDisplay(size_t width,
size_t height,
bool inPts,
bool searchAll)
{
return setResolutionForDisplay(CGMainDisplayID(),
width,
height,
inPts,
searchAll);
}
/* setResolutionForDisplay - sets the resolution for the specified display */
bool setResolutionForDisplay(unsigned long display,
size_t width,
size_t height,
bool inPts,
bool searchAll)
{
CGDisplayErr err;
CGDisplayCount onlineDisplayCnt, i;
CGDirectDisplayID displays[MAXDISPLAYS];
CGDisplayModeRef mode = NULL;
CGDisplayConfigRef config;
CFArrayRef allModes = NULL;
CFDictionaryRef options = NULL;
const CFStringRef dictkeys[] =
{kCGDisplayShowDuplicateLowResolutionModes};
const CFBooleanRef dictvalues[] =
{kCFBooleanTrue};
long numModes = 0, j = 0;
size_t modeWidth = 0, modeHeight = 0;
bool foundDisplay = false, foundMode = false;
bool ret = false;
/* make sure a non-zero width and height are specified */
if (width <= 0 || height <= 0)
{
return ret;
}
/* get a list of the available displays */
err = CGGetOnlineDisplayList(gMaxDisplays,
displays,
&onlineDisplayCnt);
if (err != kCGErrorSuccess)
{
fprintf(stderr,
"error: %s: %s\n",
gStrModeResolutionLong,
gStrErrGetDisplays);
return ret;
}
/* see if the specified display is available */
for (i = 0; i < onlineDisplayCnt; i++)
{
if (displays[i] != display)
{
continue;
}
/* the specified display is available */
foundDisplay = true;
break;
}
if (foundDisplay == false)
{
fprintf(stderr,
"error: %s: %s: '%lu'\n",
gStrModeResolutionLong,
gStrErrNoSuchDisplay,
display);
return ret;
}
/* check if the requested resolution is available */
/*
if searchAll is true, then look through all available
resolutions, otherwise, just look at supported
resolutions
*/
if (searchAll == true)
{
options = CFDictionaryCreate(NULL,
(const void **)dictkeys,
(const void **)dictvalues,
1,
&kCFCopyStringDictionaryKeyCallBacks,
&kCFTypeDictionaryValueCallBacks);
}
/* get the available display modes for this display */
allModes = CGDisplayCopyAllDisplayModes((CGDirectDisplayID)display,
options);
if (options != NULL)
{
CFRelease(options);
}
if (allModes == NULL)
{
fprintf(stderr,
"error: %s: %s\n",
gStrModeResolutionLong,
gStrErrGetDisplayModes);
return ret;
}
/* get the number of available modes that were found */
numModes = CFArrayGetCount(allModes);
if (numModes <= 0)
{
CFRelease(allModes);
fprintf(stderr,
"error: %s: %s\n",
gStrModeResolutionLong,
gStrErrGetDisplayModes);
return ret;
}
for (j = 0; j < numModes; j++)
{
mode = (CGDisplayModeRef)CFArrayGetValueAtIndex(allModes, j);
if (mode == NULL)
{
continue;
}
/*
if the requested mode is a retina mode (i.e. inPts
is true), then look at the resolution in points,
otherwise, use the pixel resolution
*/
if (inPts)
{
modeWidth = CGDisplayModeGetWidth(mode);
modeHeight = CGDisplayModeGetHeight(mode);
}
else
{
modeWidth = CGDisplayModeGetPixelWidth(mode);
modeHeight = CGDisplayModeGetPixelHeight(mode);
}
/* we found a matching mode, break */
if (width == modeWidth && height == modeHeight)
{
foundMode = true;
break;
}
}
/* the requested resolution isn't available */
if (foundMode != true)
{
CFRelease(allModes);
fprintf(stderr,
"error: %s: %s: %lu x %lu\n",
gStrModeResolutionLong,
gStrErrGetResolutionUnavailable,
width,
height);
return ret;
}
/*
A matching resolution is available; try to set the
display's resolution to the requested resolution
Based on:
https://gist.github.com/bellbind/27220148ea752335259dfce63f43bd5d#file-displaymode-c
https://developer.apple.com/documentation/coregraphics/1455235-cgbegindisplayconfiguration?language=objc
https://developer.apple.com/documentation/coregraphics/1454273-cgconfiguredisplaywithdisplaymod?language=objc
https://developer.apple.com/documentation/coregraphics/1454760-cgdisplaysetdisplaymode?language=objc
https://developer.apple.com/documentation/coregraphics/1454488-cgcompletedisplayconfiguration?language=objc
*/
if (CGBeginDisplayConfiguration(&config) != kCGErrorSuccess)
{
CFRelease(allModes);
fprintf(stderr,
"error: %s: %s\n",
gStrModeResolutionLong,
gStrErrCantConfigure);
return ret;
}
if (CGConfigureDisplayWithDisplayMode(config,
(CGDirectDisplayID)display,
mode,
NULL) != kCGErrorSuccess)
{
CFRelease(allModes);
fprintf(stderr,
"error: %s: %s %lu to %lu x %lu\n",
gStrModeResolutionLong,
gStrErrCantConfigure,
display,
modeWidth,
modeHeight);
return ret;
}
if (CGCompleteDisplayConfiguration(config,
kCGConfigurePermanently)
!= kCGErrorSuccess)
{
CFRelease(allModes);
fprintf(stderr,
"error: %s: %s",
gStrModeResolutionLong,
gStrErrCantComplete);
return ret;
}
return true;
}