forked from jhford/screenresolution
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcg_utils.c
137 lines (131 loc) · 5.2 KB
/
cg_utils.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
// vim: ts=4:sw=4
/*
* screenresolution sets the screen resolution on Mac computers.
* Copyright (C) 2011 John Ford <[email protected]>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* version 2 as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
* 02110-1301, USA.
*/
#include "cg_utils.h"
size_t bitDepth(CGDisplayModeRef mode) {
size_t depth = 0;
CFStringRef pixelEncoding = CGDisplayModeCopyPixelEncoding(mode);
// my numerical representation for kIO16BitFloatPixels and kIO32bitFloatPixels
// are made up and possibly non-sensical
if (kCFCompareEqualTo == CFStringCompare(pixelEncoding, CFSTR(kIO32BitFloatPixels), kCFCompareCaseInsensitive)) {
depth = 96;
} else if (kCFCompareEqualTo == CFStringCompare(pixelEncoding, CFSTR(kIO64BitDirectPixels), kCFCompareCaseInsensitive)) {
depth = 64;
} else if (kCFCompareEqualTo == CFStringCompare(pixelEncoding, CFSTR(kIO16BitFloatPixels), kCFCompareCaseInsensitive)) {
depth = 48;
} else if (kCFCompareEqualTo == CFStringCompare(pixelEncoding, CFSTR(IO32BitDirectPixels), kCFCompareCaseInsensitive)) {
depth = 32;
} else if (kCFCompareEqualTo == CFStringCompare(pixelEncoding, CFSTR(kIO30BitDirectPixels), kCFCompareCaseInsensitive)) {
depth = 30;
} else if (kCFCompareEqualTo == CFStringCompare(pixelEncoding, CFSTR(IO16BitDirectPixels), kCFCompareCaseInsensitive)) {
depth = 16;
} else if (kCFCompareEqualTo == CFStringCompare(pixelEncoding, CFSTR(IO8BitIndexedPixels), kCFCompareCaseInsensitive)) {
depth = 8;
}
CFRelease(pixelEncoding);
return depth;
}
unsigned int configureDisplay(CGDirectDisplayID display, struct config *config, int displayNum) {
unsigned int returncode = 1;
CFArrayRef allModes = CGDisplayCopyAllDisplayModes(display, NULL);
if (allModes == NULL) {
NSLog(CFSTR("Error: failed trying to look up modes for display %u"), displayNum);
}
CGDisplayModeRef newMode = NULL;
CGDisplayModeRef possibleMode;
size_t pw; // possible width.
size_t ph; // possible height.
size_t pd; // possible depth.
double pr; // possible refresh rate
int looking = 1; // used to decide whether to continue looking for modes.
int i;
for (i = 0 ; i < CFArrayGetCount(allModes) && looking; i++) {
possibleMode = (CGDisplayModeRef)CFArrayGetValueAtIndex(allModes, i);
pw = CGDisplayModeGetWidth(possibleMode);
ph = CGDisplayModeGetHeight(possibleMode);
pd = bitDepth(possibleMode);
pr = CGDisplayModeGetRefreshRate(possibleMode);
if (pw == config->w &&
ph == config->h &&
pd == config->d &&
pr == config->r) {
looking = 0; // Stop looking for more modes!
newMode = possibleMode;
}
}
CFRelease(allModes);
if (newMode != NULL) {
NSLog(CFSTR("set mode on display %u to %ux%ux%u@%.0f"), displayNum, pw, ph, pd, pr);
setDisplayToMode(display,newMode);
} else {
NSLog(CFSTR("Error: mode %ux%ux%u@%f not available on display %u"),
config->w, config->h, config->d, config->r, displayNum);
returncode = 0;
}
return returncode;
}
unsigned int setDisplayToMode(CGDirectDisplayID display, CGDisplayModeRef mode) {
CGError rc;
CGDisplayConfigRef config;
rc = CGBeginDisplayConfiguration(&config);
if (rc != kCGErrorSuccess) {
NSLog(CFSTR("Error: failed CGBeginDisplayConfiguration err(%u)"), rc);
return 0;
}
rc = CGConfigureDisplayWithDisplayMode(config, display, mode, NULL);
if (rc != kCGErrorSuccess) {
NSLog(CFSTR("Error: failed CGConfigureDisplayWithDisplayMode err(%u)"), rc);
return 0;
}
rc = CGCompleteDisplayConfiguration(config, kCGConfigureForSession);
if (rc != kCGErrorSuccess) {
NSLog(CFSTR("Error: failed CGCompleteDisplayConfiguration err(%u)"), rc);
return 0;
}
return 1;
}
unsigned int parseStringConfig(const char *string, struct config *out) {
unsigned int rc;
size_t w;
size_t h;
size_t d;
double r;
int numConverted = sscanf(string, "%lux%lux%lu@%lf", &w, &h, &d, &r);
if (numConverted != 4) {
numConverted = sscanf(string, "%lux%lux%lu", &w, &h, &d);
if (numConverted != 3) {
rc = 0;
NSLog(CFSTR("Error: the mode '%s' couldn't be parsed"), string);
} else {
out->w = w;
out->h = h;
out->d = d;
r=60.0;
rc = 1;
NSLog(CFSTR("Warning: no refresh rate specified, assuming %.0lfHz"), r);
}
} else {
out->w = w;
out->h = h;
out->d = d;
out->r = r;
rc = 1;
}
return rc;
}