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
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
|
/************************************************************************************
Filename : OVR_OSX_Display.cpp
Content : OSX-specific Display declarations
Created : July 2, 2014
Authors : James Hughes
Copyright : Copyright 2014 Oculus VR, LLC All Rights reserved.
Licensed under the Oculus VR Rift SDK License Version 3.2 (the "License");
you may not use the Oculus VR Rift SDK except in compliance with the License,
which is provided at the time of installation or download, or which
otherwise accompanies this software in either electronic or hard copy form.
You may obtain a copy of the License at
http://www.oculusvr.com/licenses/LICENSE-3.2
Unless required by applicable law or agreed to in writing, the Oculus VR SDK
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.
*************************************************************************************/
#include "OVR_OSX_Display.h"
#include "Kernel/OVR_Log.h"
#include <ApplicationServices/ApplicationServices.h>
#include <CoreFoundation/CoreFoundation.h>
#include <CoreFoundation/CFString.h>
#include <IOKit/graphics/IOGraphicsLib.h>
//-------------------------------------------------------------------------------------
// ***** Display enumeration Helpers
namespace OVR {
// FIXME Code duplication with windows.
#define EDID_LENGTH 0x80
#define EDID_HEADER 0x00
#define EDID_HEADER_END 0x07
#define ID_MANUFACTURER_NAME 0x08
#define ID_MANUFACTURER_NAME_END 0x09
#define EDID_STRUCT_VERSION 0x12
#define EDID_STRUCT_REVISION 0x13
#define ESTABLISHED_TIMING_1 0x23
#define ESTABLISHED_TIMING_2 0x24
#define MANUFACTURERS_TIMINGS 0x25
#define DETAILED_TIMING_DESCRIPTIONS_START 0x36
#define DETAILED_TIMING_DESCRIPTION_SIZE 18
#define NO_DETAILED_TIMING_DESCRIPTIONS 4
#define DETAILED_TIMING_DESCRIPTION_1 0x36
#define DETAILED_TIMING_DESCRIPTION_2 0x48
#define DETAILED_TIMING_DESCRIPTION_3 0x5a
#define DETAILED_TIMING_DESCRIPTION_4 0x6c
#define MONITOR_NAME 0xfc
#define MONITOR_LIMITS 0xfd
#define MONITOR_SERIAL 0xff
#define UNKNOWN_DESCRIPTOR -1
#define DETAILED_TIMING_BLOCK -2
#define DESCRIPTOR_DATA 5
const UByte edid_v1_header[] = { 0x00, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0x00 };
const UByte edid_v1_descriptor_flag[] = { 0x00, 0x00 };
// FIXME Code duplication with windows. Refactor.
static int blockType( UByte* block )
{
if ( !strncmp( (const char*)edid_v1_descriptor_flag, (const char*)block, 2 ) )
{
// descriptor
if ( block[ 2 ] != 0 )
return UNKNOWN_DESCRIPTOR;
return block[ 3 ];
}
else
{
return DETAILED_TIMING_BLOCK;
}
}
static char* getMonitorName( UByte const* block )
{
static char name[ 13 ];
unsigned i;
UByte const* ptr = block + DESCRIPTOR_DATA;
for( i = 0; i < 13; i++, ptr++ )
{
if ( *ptr == 0xa )
{
name[ i ] = 0;
return name;
}
name[ i ] = *ptr;
}
return name;
}
// FIXME Code duplication with windows. Refactor.
static bool parseEdid( UByte* edid, OVR::OSX::DisplayEDID& edidResult )
{
unsigned i;
UByte* block;
const char* monitor_name = "Unknown";
UByte checksum = 0;
for( i = 0; i < EDID_LENGTH; i++ )
checksum += edid[ i ];
// Bad checksum, fail EDID
if ( checksum != 0 )
return false;
if ( strncmp( (const char*)edid+EDID_HEADER, (const char*)edid_v1_header, EDID_HEADER_END+1 ) )
{
// First bytes don't match EDID version 1 header
return false;
}
// OVR_DEBUG_LOG_TEXT(( "\n# EDID version %d revision %d\n",
// (int)edid[EDID_STRUCT_VERSION],(int)edid[EDID_STRUCT_REVISION] ));
// Monitor name and timings
char serialNumber[14];
memset( serialNumber, 0, 14 );
block = edid + DETAILED_TIMING_DESCRIPTIONS_START;
for( i = 0; i < NO_DETAILED_TIMING_DESCRIPTIONS; i++,
block += DETAILED_TIMING_DESCRIPTION_SIZE )
{
if ( blockType( block ) == MONITOR_NAME )
{
monitor_name = getMonitorName( block );
}
if( blockType( block ) == MONITOR_SERIAL )
{
memcpy( serialNumber, block + 5, 13 );
break;
}
}
UByte vendorString[4] = {0};
vendorString[0] = (edid[8] >> 2 & 31) + 64;
vendorString[1] = ((edid[8] & 3) << 3) | (edid[9] >> 5) + 64;
vendorString[2] = (edid[9] & 31) + 64;
edidResult.ModelNumber = *(UInt16*)&edid[10];
edidResult.MonitorName = OVR::String(monitor_name);
edidResult.VendorName = OVR::String((const char*)vendorString);
edidResult.SerialNumber = OVR::String(serialNumber);
// printf( "\tIdentifier \"%s\"\n", monitor_name );
// printf( "\tVendorName \"%s\"\n", vendorString );
// printf( "\tModelName \"%s\"\n", monitor_name );
// printf( "\tModelNumber %d\n", edidResult.ModelNumber );
// printf( "\tSerialNumber \"%s\"\n", edidResult.SerialNumber.ToCStr() );
// FIXME: Get timings as well, though they aren't very useful here
// except for the vertical refresh rate, presumably
return true;
}
static int discoverExtendedRifts(OVR::OSX::DisplayDesc* descriptorArray, int inputArraySize, bool edidInfo)
{
OVR_UNUSED(edidInfo);
int result = 0;
static bool reportDiscovery = true;
OVR_UNUSED(reportDiscovery);
CGDirectDisplayID Displays[32];
uint32_t NDisplays = 0;
CGGetOnlineDisplayList(32, Displays, &NDisplays);
for (unsigned int i = 0; i < NDisplays; i++)
{
CGDirectDisplayID dispId = Displays[i];
io_service_t port = CGDisplayIOServicePort(dispId);
CFDictionaryRef DispInfo = IODisplayCreateInfoDictionary(port, kNilOptions);
// Display[i]
uint32_t vendor = CGDisplayVendorNumber(dispId);
uint32_t product = CGDisplayModelNumber(dispId);
CGRect desktop = CGDisplayBounds(dispId);
Vector2i desktopOffset(desktop.origin.x, desktop.origin.y);
if (vendor == 16082 && ( (product == 1)||(product == 2)||(product == 3) ) ) // 7" or HD
{
if( result >= inputArraySize )
{
CFRelease(DispInfo);
return result;
}
int width = static_cast<int>(CGDisplayPixelsWide(dispId));
int height = static_cast<int>(CGDisplayPixelsHigh(dispId));
Sizei monitorResolution(width, height);
// Obtain and parse EDID data.
CFDataRef data =
(CFDataRef)CFDictionaryGetValue(DispInfo, CFSTR(kIODisplayEDIDKey));
if (!data)
{
CFRelease(DispInfo);
OVR::LogError("[OSX Display] Unable to obtain EDID for Oculus product %d", product);
continue;
}
UByte* edid = (UByte*)CFDataGetBytePtr(data);
OSX::DisplayEDID edidResult;
parseEdid( edid, edidResult );
OVR::OSX::DisplayDesc& desc = descriptorArray[result++];
desc.DisplayID = dispId;
desc.ModelName = edidResult.MonitorName; // User friendly string.
desc.EdidSerialNumber = edidResult.SerialNumber;
desc.LogicalResolutionInPixels = monitorResolution;
desc.NativeResolutionInPixels = monitorResolution;
desc.DesktopDisplayOffset = desktopOffset;
desc.Rotation = 0;
auto roughEqual = [](double a, double b) -> bool
{
return fabs(a - b) < 1.0;
};
switch (product)
{
case 3: desc.DeviceTypeGuess = HmdType_DK2; break;
case 2: desc.DeviceTypeGuess = HmdType_DKHDProto; break;
case 1: desc.DeviceTypeGuess = HmdType_DK1; break;
default:
case 0: desc.DeviceTypeGuess = HmdType_Unknown; break;
}
bool portraitDevice = (desc.DeviceTypeGuess == HmdType_DK2);
double rotation = fabs(CGDisplayRotation(dispId));
if (roughEqual(rotation, 0))
{
desc.Rotation = portraitDevice ? 270 : 0;
}
else if (roughEqual(rotation, 90))
{
desc.Rotation = portraitDevice ? 0 : 90;
}
else if (roughEqual(rotation, 180))
{
desc.Rotation = portraitDevice ? 90 : 180;
}
else if (roughEqual(rotation, 270))
{
desc.Rotation = portraitDevice ? 180 : 270;
}
// Hard-coded defaults in case the device doesn't have the data itself.
// DK2 prototypes (0003) or DK HD Prototypes (0002)
if (product == 3 || product == 2)
{
desc.LogicalResolutionInPixels = Sizei(1920, 1080);
desc.NativeResolutionInPixels = Sizei(1080, 1920);
}
else
{
desc.LogicalResolutionInPixels = monitorResolution;
desc.NativeResolutionInPixels = monitorResolution;
}
//OVR_DEBUG_LOG_TEXT(("Display Found %x:%x\n", vendor, product));
}
CFRelease(DispInfo);
}
return result;
}
//-------------------------------------------------------------------------------------
// ***** Display
bool Display::Initialize()
{
// Nothing to initialize. OS X only supports compatibility mode.
return true;
}
void Display::Shutdown()
{
}
bool Display::GetDriverMode(bool& driverInstalled, bool& compatMode, bool& hideDK1Mode)
{
driverInstalled = false;
compatMode = true;
hideDK1Mode = false;
return true;
}
bool Display::SetDriverMode(bool /*compatMode*/, bool /*hideDK1Mode*/)
{
return false;
}
DisplaySearchHandle* Display::GetDisplaySearchHandle()
{
return new OSX::OSXDisplaySearchHandle();
}
bool Display::InCompatibilityMode( bool displaySearch )
{
OVR_UNUSED( displaySearch );
return true;
}
int Display::GetDisplayCount( DisplaySearchHandle* handle, bool extended, bool applicationOnly, bool edidInfo )
{
OVR_UNUSED(applicationOnly);
static int extendedCount = -1;
OSX::OSXDisplaySearchHandle* localHandle = (OSX::OSXDisplaySearchHandle*)handle;
if (localHandle == NULL)
{
OVR::LogError("[OSX Display] No search handle passed into GetDisplayCount. Return 0 rifts.");
return 0;
}
if (extendedCount == -1 || extended)
{
extendedCount = discoverExtendedRifts(localHandle->cachedDescriptorArray, OSX::OSXDisplaySearchHandle::DescArraySize, edidInfo);
}
localHandle->extended = true;
localHandle->extendedDisplayCount = extendedCount;
int totalCount = extendedCount;
/// FIXME: Implement application mode for OS X.
localHandle->application = false;
localHandle->applicationDisplayCount = 0;
localHandle->displayCount = totalCount;
return totalCount;
}
Ptr<Display> Display::GetDisplay( int index, DisplaySearchHandle* handle )
{
Ptr<Display> result = NULL;
if (index < 0)
{
OVR::LogError("[OSX Display] Invalid index given to GetDisplay.");
return NULL;
}
OSX::OSXDisplaySearchHandle* localHandle = (OSX::OSXDisplaySearchHandle*)handle;
if (localHandle == NULL)
{
OVR::LogError("[OSX Display] No search handle passed into GetDisplay. Return 0 rifts.");
return NULL;
}
if (localHandle->extended)
{
if (index >= 0 && index < (int)localHandle->extendedDisplayCount)
{
return *new OSX::OSXDisplayGeneric(localHandle->cachedDescriptorArray[index]);
}
// index -= localHandle->extendedDisplayCount;
}
if (localHandle->application)
{
OVR::LogError("[OSX Display] Mac does not support application displays.");
}
return result;
}
} // namespace OVR
|