-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathosxDisplayDeviceManager.cpp
More file actions
191 lines (167 loc) · 6.47 KB
/
osxDisplayDeviceManager.cpp
File metadata and controls
191 lines (167 loc) · 6.47 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
/* -*- mode: c++ -*-
*
* pointing/output/osx/osxDisplayDeviceManager.cpp --
*
* Initial software
* Authors: Izzatbek Mukhanov
* Copyright © Inria
*
* http://libpointing.org/
*
* This software may be used and distributed according to the terms of
* the GNU General Public License version 2 or any later version.
*
*/
#include <pointing/output/osx/osxDisplayDeviceManager.h>
#include <IOKit/graphics/IOGraphicsLib.h>
#include <stdexcept>
extern "C" bool NSApplicationLoad(void) ;
// https://developer.apple.com/library/mac/documentation/GraphicsImaging/Reference/Quartz_Services_Ref/index.html#//apple_ref/doc/c_ref/CGDisplayChangeSummaryFlags
namespace pointing
{
void osxDisplayDeviceManager::MyDisplayReconfigurationCallBack(CGDirectDisplayID display, CGDisplayChangeSummaryFlags flags, void *userInfo) {
osxDisplayDeviceManager *self = (osxDisplayDeviceManager *)userInfo;
if (flags & kCGDisplayAddFlag)
self->addDisplay(display);
if (flags & kCGDisplayRemoveFlag)
self->removeDisplay(display);
}
std::string CFStringRefToStdString(CFStringRef aString) {
if (aString == NULL) {
return "Unknown";
}
CFIndex length = CFStringGetLength(aString);
CFIndex maxSize =
CFStringGetMaximumSizeForEncoding(length,
kCFStringEncodingUTF8);
char *buffer = (char *)malloc(maxSize);
if (CFStringGetCString(aString, buffer, maxSize,
kCFStringEncodingUTF8)) {
std::string result(buffer);
free(buffer);
return result;
}
return "Unknown";
}
// Returns the io_service_t corresponding to a CG display ID, or 0 on failure.
// The io_service_t should be released with IOObjectRelease when not needed.
//
static io_service_t IOServicePortFromCGDisplayID(CGDirectDisplayID displayID)
{
io_iterator_t iter;
io_service_t serv, servicePort = 0;
CFMutableDictionaryRef matching = IOServiceMatching("IODisplayConnect");
// releases matching for us
kern_return_t err = IOServiceGetMatchingServices(kIOMasterPortDefault,
matching,
&iter);
if (err)
return 0;
while ((serv = IOIteratorNext(iter)) != 0)
{
CFDictionaryRef info;
CFIndex vendorID, productID, serialNumber;
CFNumberRef vendorIDRef, productIDRef, serialNumberRef;
Boolean success;
info = IODisplayCreateInfoDictionary(serv,
kIODisplayOnlyPreferredName);
vendorIDRef = static_cast<CFNumberRef>(CFDictionaryGetValue(info,
CFSTR(kDisplayVendorID)));
productIDRef = static_cast<CFNumberRef>(CFDictionaryGetValue(info,
CFSTR(kDisplayProductID)));
serialNumberRef = static_cast<CFNumberRef>(CFDictionaryGetValue(info,
CFSTR(kDisplaySerialNumber)));
success = CFNumberGetValue(vendorIDRef, kCFNumberCFIndexType,
&vendorID);
success &= CFNumberGetValue(productIDRef, kCFNumberCFIndexType,
&productID);
if (serialNumberRef != 0)
success &= CFNumberGetValue(serialNumberRef, kCFNumberCFIndexType,
&serialNumber);
if (!success)
{
CFRelease(info);
continue;
}
// If the vendor and product id along with the serial don't match
// then we are not looking at the correct monitor.
// NOTE: The serial number is important in cases where two monitors
// are the exact same.
if (CGDisplayVendorNumber(displayID) != vendorID ||
CGDisplayModelNumber(displayID) != productID ||
CGDisplaySerialNumber(displayID) != serialNumber)
{
CFRelease(info);
continue;
}
// The VendorID, Product ID, and the Serial Number all Match Up!
// Therefore we have found the appropriate display io_service
servicePort = serv;
CFRelease(info);
break;
}
IOObjectRelease(iter);
return servicePort;
}
std::string getDisplayName(CGDirectDisplayID did)
{
std::string result;
CFDictionaryRef deviceInfo = IODisplayCreateInfoDictionary(IOServicePortFromCGDisplayID(did), kIODisplayOnlyPreferredName);
CFTypeRef value = NULL;
value = CFDictionaryGetValue(deviceInfo, CFSTR(kDisplayProductName));
if (value)
{
CFDictionaryRef valueDict = (CFDictionaryRef)value;
CFIndex number = CFDictionaryGetCount(valueDict);
if (number > 0)
{
CFStringRef *key = (CFStringRef *)malloc(sizeof(CFStringRef)*number);
CFDictionaryGetKeysAndValues(valueDict, NULL, (const void **)key);
result = CFStringRefToStdString(key[0]);
free(key);
}
}
else
result = "Unknown Display Name";
CFRelease(deviceInfo);
return result;
}
DisplayDeviceDescriptor osxDisplayDeviceManager::convertDevice(CGDirectDisplayID did)
{
std::stringstream uri;
uri << "osxdisplay:/" << did;
DisplayDeviceDescriptor desc;
desc.devURI = uri.str();
desc.name = getDisplayName(did);
CGDisplayModeRef dispMode = CGDisplayCopyDisplayMode(did);
CGRect rect = CGDisplayBounds(did);
desc.width = rect.size.width;
desc.height = rect.size.height;
CGDisplayModeRelease(dispMode);
return desc;
}
void osxDisplayDeviceManager::addDisplay(CGDirectDisplayID did)
{
DisplayDeviceDescriptor desc = convertDevice(did);
addDevice(desc);
}
void osxDisplayDeviceManager::removeDisplay(CGDirectDisplayID did)
{
DisplayDeviceDescriptor desc = convertDevice(did);
removeDevice(desc);
}
osxDisplayDeviceManager::osxDisplayDeviceManager()
{
CGDisplayCount dspyCnt = 0 ;
CGDisplayErr err = CGGetActiveDisplayList(0, 0, &dspyCnt) ;
CGDirectDisplayID *activeDspys = new CGDirectDisplayID [dspyCnt] ;
err = CGGetActiveDisplayList(dspyCnt, activeDspys, &dspyCnt) ;
for (unsigned int i = 0; i < dspyCnt; ++i) {
DisplayDeviceDescriptor desc = convertDevice(activeDspys[i]) ;
addDevice(desc);
}
delete [] activeDspys;
NSApplicationLoad() ;
CGDisplayRegisterReconfigurationCallback(MyDisplayReconfigurationCallBack, this);
}
}