diff options
author | Sven Gothel <[email protected]> | 2015-03-28 01:43:35 +0100 |
---|---|---|
committer | Sven Gothel <[email protected]> | 2015-03-28 01:43:35 +0100 |
commit | 4207f9c279e832e3afcb3f5fc6cd8d84cb4cfe4c (patch) | |
tree | cf3671058d55b47ab6cb6f36f369928606137628 /LibOVR/Src/Displays | |
parent | c29cd1a2fbff6282bab956ad61661ac9d48c4e6e (diff) |
Bump OculusVR RIFT SDK to 0.5.0.1vanilla_0.5.0.1
Diffstat (limited to 'LibOVR/Src/Displays')
21 files changed, 3329 insertions, 3977 deletions
diff --git a/LibOVR/Src/Displays/OVR_Display.cpp b/LibOVR/Src/Displays/OVR_Display.cpp index 55eb8a9..7c74eee 100644..100755 --- a/LibOVR/Src/Displays/OVR_Display.cpp +++ b/LibOVR/Src/Displays/OVR_Display.cpp @@ -27,10 +27,262 @@ limitations under the License. #include "OVR_Display.h" + namespace OVR { // Place platform-independent code here +static bool DirectDisplayInitialized = false; + +bool Display::GetDirectDisplayInitialized() +{ + return DirectDisplayInitialized; +} + +void Display::SetDirectDisplayInitialized(bool initialized) +{ + DirectDisplayInitialized = initialized; +} + + +//----------------------------------------------------------------------------- +// EDID Parsing + +// FIXME: This can be done much more compactly without the bitfields. +#pragma pack(push, 1) + +#if defined(_MSC_VER) +#pragma warning(disable: 4201) // Nameless struct/union +#endif + +// All of our EDIDs use the Detailed timing descriptors, and not the +// older Standard timing info in the EDID. Conforming EDID v1.3+ displays +// always put their preferred resolution, refresh, and timing info into the +// first Detailed timing descriptor. + +#ifndef byte_t +typedef unsigned char byte_t; +#endif + +//static const uint32_t EDIDv13Size = 128; // Standard EDID v1.3 is 128 bytes. +static const uint32_t FirstDetailedTimingOffset = 54; // Detailed timing table starts here. +static const uint32_t DetailedTimingDescriptorCount = 4; +static const byte_t MonitorSerialNumberType = 0xFF; +static const byte_t MonitorNameType = 0xFC; + +// Expected signature of EDID +static const byte_t EDIDSignature[] = { 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00 }; + +struct EDID_Header +{ + byte_t Signature[8]; + + byte_t VendorIDHigh; + byte_t VendorIDLow; + + uint16_t ProductCode; + uint32_t SerialNumber; + + // We don't currently use anything farther into the header + byte_t Unused[112]; +}; + +struct EDID_Detailed_Timing_Descriptor +{ + uint16_t PixelClock; // In 10Khz units + + byte_t HActivePixelsLSB; + byte_t HBlankingPixelsLSB; + + union + { + struct + { + byte_t HBlankingPixelsMSB : 4; + byte_t HActivePixelsMSB : 4; + } Values; + byte_t Value_; + } HSizeMSB; + + byte_t VActivePixelsLSB; + byte_t VBlankingPixelsLSB; + union + { + struct + { + byte_t VBlankingPixelsMSB : 4; + byte_t VActivePixelsMSB : 4; + } Values; + byte_t Value_; + } VSizeMSB; + + byte_t HSyncOffsetPixelsLSB; + byte_t HSyncPulseWidthPixelsLSB; + + union + { + struct + { + byte_t VSyncPulseWidthLSB : 4; + byte_t VSyncOffsetPixelsLSB : 4; + } Values; + byte_t Value_; + } VSync; + + union + { + struct + { + byte_t VSyncPulseWidthMSB : 2; + byte_t VSyncOffsetMSB : 2; + byte_t HSyncPulseWidthMSB : 2; + byte_t HSyncOffsetMSB : 2; + } Values; + byte_t Value_; + } SyncMSB; + + byte_t Unused[6]; // We don't use anything else in the header +}; + +struct EDID_Other_Descriptor +{ + byte_t Reserved[3]; + byte_t Type; + byte_t Reserved1; + char Data[13]; +}; + +#pragma pack(pop) + + +static void StripTrailingWhitespace(char* str) +{ + // Get initial string length + int mlen = (int)strlen(str); + + // While removing trailing characters, + while (mlen > 0) + { + // If trailing character should be stripped, + char trailing = str[mlen - 1]; + if (trailing == '\n' || trailing == ' ') + { + // Strip it and reduce string length. + str[mlen - 1] = '\0'; + --mlen; + } + else + { + // Stop here. + break; + } + } +} + +bool DisplayEDID::Parse(const unsigned char* edid) +{ + const EDID_Header* header = (const EDID_Header*)edid; + + if (memcmp(header->Signature, EDIDSignature, sizeof(EDIDSignature)) != 0) + { + OVR_ASSERT(false); + return false; + } + + memset(VendorName, 0, sizeof(VendorName)); + memset(MonitorName, 0, sizeof(MonitorName)); + memset(SerialNumber, 0, sizeof(SerialNumber)); + + // Extract the 5-bit chars for the Vendor ID (PNP code) + uint16_t char1 = (header->VendorIDHigh >> 2) & 0x1F; + uint16_t char2 = ((header->VendorIDHigh & 0x2) << 3) | (header->VendorIDLow >> 5); + uint16_t char3 = header->VendorIDLow & 0x1F; + VendorName[0] = (char)char1 - 1 + 'A'; + VendorName[1] = (char)char2 - 1 + 'A'; + VendorName[2] = (char)char3 - 1 + 'A'; + VendorName[3] = '\0'; + ModelNumber = header->ProductCode; + + const EDID_Detailed_Timing_Descriptor* detailedTiming = (const EDID_Detailed_Timing_Descriptor*)&edid[FirstDetailedTimingOffset]; + + // First Detailed timing info is always preferred mode: + uint32_t hActive = (detailedTiming->HSizeMSB.Values.HActivePixelsMSB << 8) | detailedTiming->HActivePixelsLSB; + uint32_t vActive = (detailedTiming->VSizeMSB.Values.VActivePixelsMSB << 8) | detailedTiming->VActivePixelsLSB; + uint32_t hBlanking = (detailedTiming->HSizeMSB.Values.HBlankingPixelsMSB << 8) | detailedTiming->HBlankingPixelsLSB; + uint32_t vBlanking = (detailedTiming->VSizeMSB.Values.VBlankingPixelsMSB << 8) | detailedTiming->VBlankingPixelsLSB; + + // Need to scale up the values, since they're in 10Khz, and we're using integer math without fractions + uint32_t denom = 1000; + uint32_t totalPixels = (hActive + hBlanking) * (vActive + vBlanking); + uint32_t vSyncNumerator = (uint32_t)(((uint64_t)detailedTiming->PixelClock * 10000 * denom) / totalPixels); + + Width = hActive; + Height = vActive; + + RefreshNumerator = vSyncNumerator; + RefreshDenominator = denom; + + // The remaining ones can hold extra info. Look for monitor name & serial number strings. + ++detailedTiming; + for (int i = 1; i < (int)DetailedTimingDescriptorCount; ++i, ++detailedTiming) + { + if (detailedTiming->PixelClock == 0) + { + // Not a timing info, use OtherDescriptor instead + const EDID_Other_Descriptor* other = (const EDID_Other_Descriptor*)detailedTiming; + switch (other->Type) + { + case MonitorNameType: + static_assert(sizeof(MonitorName) == sizeof(other->Data) + 1, "serial number field size is off"); + memcpy(MonitorName, other->Data, sizeof(MonitorName)); + MonitorName[sizeof(MonitorName) - 1] = '\0'; + StripTrailingWhitespace(MonitorName); + break; + + case MonitorSerialNumberType: + static_assert(sizeof(SerialNumber) == sizeof(other->Data) + 1, "serial number field size is off"); + memcpy(SerialNumber, other->Data, sizeof(SerialNumber)); + SerialNumber[sizeof(SerialNumber) - 1] = '\0'; + StripTrailingWhitespace(SerialNumber); + break; + + default: + break; + } + } + } + + return true; +} + +HmdTypeEnum HmdTypeFromModelNumber(int modelNumber) +{ + HmdTypeEnum deviceTypeGuess = HmdType_Unknown; + switch (modelNumber) + { + case 3: deviceTypeGuess = HmdType_DK2; break; + case 2: deviceTypeGuess = HmdType_DKHDProto; break; + case 1: deviceTypeGuess = HmdType_DK1; break; + default: break; + } + return deviceTypeGuess; +} + +bool Display::MatchDisplay(const Display* other) +{ + // Note this is not checking the DeviceName, which corresponds to which monitor the device is. + // This allows matching to match a display that has changed how it is plugged in. + // The rotation must match, which allows us to react properly by regenerating the HMD info. + bool displayMatch = + (DisplayID == other->DisplayID) && + (EdidSerialNumber == other->EdidSerialNumber) && + (NativeResolutionInPixels == other->NativeResolutionInPixels) && + (Rotation == other->Rotation) && + (ApplicationExclusive == other->ApplicationExclusive); + + return displayMatch; +} + } // namespace OVR diff --git a/LibOVR/Src/Displays/OVR_Display.h b/LibOVR/Src/Displays/OVR_Display.h index 46e9f13..7b4f1db 100644 --- a/LibOVR/Src/Displays/OVR_Display.h +++ b/LibOVR/Src/Displays/OVR_Display.h @@ -28,19 +28,63 @@ limitations under the License. #ifndef OVR_Display_h #define OVR_Display_h -#include "../Sensors/OVR_DeviceConstants.h" // Required for HmdTypeEnum +#include "Sensors/OVR_DeviceConstants.h" // Required for HmdTypeEnum -#include "../Kernel/OVR_Types.h" -#include "../Kernel/OVR_Atomic.h" -#include "../Kernel/OVR_RefCount.h" -#include "../Kernel/OVR_Array.h" -#include "../Kernel/OVR_String.h" -#include "../Kernel/OVR_Math.h" +#include "Kernel/OVR_Types.h" +#include "Kernel/OVR_Atomic.h" +#include "Kernel/OVR_RefCount.h" +#include "Kernel/OVR_Array.h" +#include "Kernel/OVR_String.h" +#include "Extras/OVR_Math.h" +#include <stdint.h> // uint32_t namespace OVR { -class DisplaySearchHandle : virtual public RefCountBaseV<DisplaySearchHandle> + +//------------------------------------------------------------------------------------- +// DisplayEDID +// +// Parses binary EDID information for the pieces we need +struct DisplayEDID +{ + char VendorName[4]; + char MonitorName[14]; + char SerialNumber[14]; + uint16_t ModelNumber; + + uint32_t Width; + uint32_t Height; + + uint32_t RefreshNumerator; + uint32_t RefreshDenominator; + + bool Parse(const unsigned char* edid); +}; + +HmdTypeEnum HmdTypeFromModelNumber(int modelNumber); + + +//------------------------------------------------------------------------------------- +// DisplayDesc + +// Display information that is enumerable +struct DisplayDesc +{ + HmdTypeEnum DeviceTypeGuess; // This is a guess about what type of HMD it is connected to + char DisplayID[64]; // This is the device identifier string from MONITORINFO (for app usage) + char ModelName[14]; // This is a "DK2" type string + char EdidSerialNumber[14]; + Sizei ResolutionInPixels; + Vector2i DesktopDisplayOffset; + int Rotation; +}; + + +//----------------------------------------------------------------------------- +// Display Search Handle +// +class DisplaySearchHandle { public: DisplaySearchHandle() {} @@ -116,7 +160,8 @@ public: // any necessary shimming and function hooks. This should be one // of the very first things your application does when it // initializes LibOVR - static bool Initialize(); + static bool Initialize(); + static void Shutdown(); // Returns a count of the detected displays. These are Rift displays // attached directly to an active display port @@ -127,16 +172,7 @@ public: // Returns true if we are referencing the same display; useful for matching display // objects with the ones already detected. - bool MatchDisplay(const Display* other) - { - // Note this is not checking the DeviceName, which corresponds to which monitor the device is. - // This allows matching to match a display that has changed how it is plugged in. - return (DisplayID == other->DisplayID) && - (EdidSerialNumber == other->EdidSerialNumber) && - (NativeResolutionInPixels == other->NativeResolutionInPixels) && - (DesktopDisplayOffset == other->DesktopDisplayOffset) && - (ApplicationExclusive == other->ApplicationExclusive); - } + bool MatchDisplay(const Display* other); // ----- Device independent instance based Display functionality ----- @@ -187,9 +223,18 @@ public: return false; } - // Check if right now the current rendering application should be in compatibility mode + // Check if right now the current rendering application should be in monitor-extended mode. + // If displaySearch is true then this function attempts to discover extended mode devices. Otherwise this + // function modifies no data. static bool InCompatibilityMode( bool displaySearch = true ); + // Polls the computer's displays to see if any of them are extended mode Rift devices. + static bool ExtendedModeDevicesExist(); + + // Tracks the initialization state of direct mode. + static bool GetDirectDisplayInitialized(); + static void SetDirectDisplayInitialized(bool initialized); + // Get/set the mode for all applications static bool GetDriverMode(bool& driverInstalled, bool& compatMode, bool& hideDK1Mode); static bool SetDriverMode(bool compatMode, bool hideDK1Mode); diff --git a/LibOVR/Src/Displays/OVR_Linux_Display.cpp b/LibOVR/Src/Displays/OVR_Linux_Display.cpp index 4b840dd..f18dfac 100644..100755 --- a/LibOVR/Src/Displays/OVR_Linux_Display.cpp +++ b/LibOVR/Src/Displays/OVR_Linux_Display.cpp @@ -7,16 +7,16 @@ 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 +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 +http://www.oculusvr.com/licenses/LICENSE-3.2 -Unless required by applicable law or agreed to in writing, the Oculus VR SDK +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 @@ -25,7 +25,7 @@ limitations under the License. *************************************************************************************/ #include "OVR_Linux_Display.h" -#include "../Kernel/OVR_Log.h" +#include "Kernel/OVR_Log.h" #include "../../../3rdParty/EDID/edid.h" @@ -34,10 +34,11 @@ limitations under the License. #include <X11/extensions/Xrandr.h> #include <X11/Xatom.h> + //------------------------------------------------------------------------------------- // ***** Display enumeration Helpers -namespace OVR { +namespace OVR { static const uint8_t edid_v1_header[] = { 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00 }; @@ -185,7 +186,7 @@ static int parseEdid(uint8_t* edid, Linux::DisplayEDID& edidResult) // data OUT This pointer is modified to point to the output from // XRRGetOutputProperty. You *must* call XFree on this pointer. // dataLen OUT The length of the data returned in 'data'. -static int getXRRProperty(struct _XDisplay* display, RROutput output, Atom atom, +static int getXRRProperty(struct _XDisplay* X11Display, RROutput output, Atom atom, uint8_t** data, int* dataLen) { unsigned long nitems; @@ -193,7 +194,7 @@ static int getXRRProperty(struct _XDisplay* display, RROutput output, Atom atom, int actualFormat; Atom actualType; - int ret = XRRGetOutputProperty(display, output, atom, 0, 100, + int ret = XRRGetOutputProperty(X11Display, output, atom, 0, 100, False, False, AnyPropertyType, &actualType, &actualFormat, &nitems, &bytesAfter, data); @@ -222,28 +223,30 @@ static XRRModeInfo* findModeByXID(XRRScreenResources* screen, RRMode xid) return NULL; } -static int discoverExtendedRifts(OVR::Linux::DisplayDesc* descriptorArray, int inputArraySize, bool /*edidInfo*/) +static struct _XDisplay* X11Display = NULL; +static int BaseRREvent = 0; +static int BaseRRError = 0; + +static int discoverExtendedRifts(OVR::DisplayDesc* descriptorArray, int inputArraySize, bool /*edidInfo*/) { int result = 0; - struct _XDisplay* display = XOpenDisplay(NULL); - - if (display == NULL) + if (X11Display == NULL) { OVR::LogError("[Linux Display] Unable to open X Display!"); return 0; } - Atom EDIDAtom = XInternAtom(display, RR_PROPERTY_RANDR_EDID, False); - int numScreens = XScreenCount(display); + Atom EDIDAtom = XInternAtom(X11Display, RR_PROPERTY_RANDR_EDID, False); + int numScreens = XScreenCount(X11Display); for (int i = 0; i < numScreens; ++i) { - Window sr = XRootWindow(display, i); - XRRScreenResources* screen = XRRGetScreenResources(display, sr); + Window sr = XRootWindow(X11Display, i); + XRRScreenResources* screen = XRRGetScreenResources(X11Display, sr); for (int ii = 0; ii < screen->ncrtc; ++ii) { - XRRCrtcInfo* crtcInfo = XRRGetCrtcInfo(display, screen, screen->crtcs[ii]); + XRRCrtcInfo* crtcInfo = XRRGetCrtcInfo(X11Display, screen, screen->crtcs[ii]); if (0 == crtcInfo->noutput) { @@ -256,7 +259,7 @@ static int discoverExtendedRifts(OVR::Linux::DisplayDesc* descriptorArray, int i for (int k = 0; k < crtcInfo->noutput; ++k) { XRROutputInfo* outputInfo = - XRRGetOutputInfo(display, screen, crtcInfo->outputs[k]); + XRRGetOutputInfo(X11Display, screen, crtcInfo->outputs[k]); for (int kk = 0; kk < outputInfo->nmode; ++kk) { @@ -280,7 +283,7 @@ static int discoverExtendedRifts(OVR::Linux::DisplayDesc* descriptorArray, int i continue; } - XRROutputInfo* outputInfo = XRRGetOutputInfo(display, screen, output); + XRROutputInfo* outputInfo = XRRGetOutputInfo(X11Display, screen, output); if (RR_Connected != outputInfo->connection) { XRRFreeOutputInfo(outputInfo); @@ -291,7 +294,7 @@ static int discoverExtendedRifts(OVR::Linux::DisplayDesc* descriptorArray, int i // Read EDID associated with crtc. uint8_t* data = NULL; int dataLen = 0; - if (getXRRProperty(display, output, EDIDAtom, &data, &dataLen) != 0) + if (getXRRProperty(X11Display, output, EDIDAtom, &data, &dataLen) != 0) { // Identify rifts based on EDID. Linux::DisplayEDID edid; @@ -302,7 +305,7 @@ static int discoverExtendedRifts(OVR::Linux::DisplayDesc* descriptorArray, int i // TODO: Remove either this 3rdParty call to read EDID data // or remove our own parsing of the EDID. Probably opt // to remove our parsing. - MonitorInfo* mi = read_edid_data(display, output); + MonitorInfo* mi = read_edid_data(X11Display, output); if (mi == NULL) { XRRFreeOutputInfo(outputInfo); @@ -325,13 +328,6 @@ static int discoverExtendedRifts(OVR::Linux::DisplayDesc* descriptorArray, int i int width = modeInfo->width; int height = modeInfo->height; - if ( crtcInfo->rotation == RR_Rotate_90 - || crtcInfo->rotation == RR_Rotate_270 ) - { - width = modeInfo->height; - height = modeInfo->width; - } - int x = crtcInfo->x; int y = crtcInfo->y; @@ -341,12 +337,32 @@ static int discoverExtendedRifts(OVR::Linux::DisplayDesc* descriptorArray, int i mi->manufacturer_code, mi->product_code, screen->crtcs[ii]); - OVR::Linux::DisplayDesc& desc = descriptorArray[result++]; - desc.DisplayID = device_id; - desc.ModelName = edid.MonitorName; - desc.EdidSerialNumber = edid.SerialNumber; - desc.LogicalResolutionInPixels = Sizei(width, height); + OVR::DisplayDesc& desc = descriptorArray[result++]; + desc.ResolutionInPixels = Sizei(width, height); desc.DesktopDisplayOffset = Vector2i(x, y); + strncpy(desc.DisplayID, device_id, sizeof(desc.DisplayID)-1); + desc.DisplayID[sizeof(desc.DisplayID)-1] = 0; + strncpy(desc.ModelName, edid.MonitorName, sizeof(desc.ModelName)-1); + desc.ModelName[sizeof(desc.ModelName)-1] = 0; + strncpy(desc.EdidSerialNumber, edid.SerialNumber, sizeof(desc.EdidSerialNumber)-1); + desc.EdidSerialNumber[sizeof(desc.EdidSerialNumber)-1] = 0; + + bool tallScreen = (height > width); + switch (crtcInfo->rotation) + { + default: + desc.Rotation = tallScreen ? 270 : 0; + break; + case RR_Rotate_90: + desc.Rotation = tallScreen ? 0 : 90; + break; + case RR_Rotate_180: + desc.Rotation = tallScreen ? 90 : 180; + break; + case RR_Rotate_270: + desc.Rotation = tallScreen ? 180 : 270; + break; + } switch (mi->product_code) { @@ -363,19 +379,21 @@ static int discoverExtendedRifts(OVR::Linux::DisplayDesc* descriptorArray, int i if ( desc.DeviceTypeGuess == HmdType_DK2 || desc.DeviceTypeGuess == HmdType_DKHDProto) { - desc.LogicalResolutionInPixels = Sizei(1920, 1080); - desc.NativeResolutionInPixels = Sizei(1080, 1920); + desc.ResolutionInPixels = Sizei(1920, 1080); } else { - desc.LogicalResolutionInPixels = Sizei(width, height); - desc.NativeResolutionInPixels = Sizei(width, height); + desc.ResolutionInPixels = Sizei(width, height); } } delete mi; mi = NULL; } + else + { + XFree(data); + } XRRFreeOutputInfo(outputInfo); XRRFreeCrtcInfo(crtcInfo); @@ -384,21 +402,44 @@ static int discoverExtendedRifts(OVR::Linux::DisplayDesc* descriptorArray, int i XRRFreeScreenResources(screen); } - XCloseDisplay(display); - return result; } //------------------------------------------------------------------------------------- -// ***** Display +// ***** Display bool Display::Initialize() { - // Nothing to initialize. OS X only supports compatibility mode. + if (X11Display == NULL) + { + X11Display = XOpenDisplay(NULL); + } + + if (X11Display == NULL) + { + OVR::LogError("[Linux Display] Unable to open X display!"); + return false; + } + + if (!XRRQueryExtension(X11Display, &BaseRREvent, &BaseRRError)) + { + OVR::LogError("[Linux Display] Unable to query XRandR extension!"); + return false; + } + return true; } +void Display::Shutdown() +{ + if (X11Display != NULL) + { + XCloseDisplay(X11Display); + X11Display = NULL; + } +} + bool Display::GetDriverMode(bool& driverInstalled, bool& compatMode, bool& hideDK1Mode) { driverInstalled = false; @@ -417,9 +458,9 @@ DisplaySearchHandle* Display::GetDisplaySearchHandle() return new Linux::LinuxDisplaySearchHandle(); } -bool Display::InCompatibilityMode( bool displaySearch ) +bool Display::InCompatibilityMode( bool X11DisplaySearch ) { - OVR_UNUSED( displaySearch ); + OVR_UNUSED( X11DisplaySearch ); return true; } @@ -427,15 +468,42 @@ int Display::GetDisplayCount(DisplaySearchHandle* handle, bool extended, bool ap { OVR_UNUSED4(handle, extended, applicationOnly, edidInfo); - static int extendedCount = -1; + static int extendedCount = -1; + static int numScreens = -1; - Linux::LinuxDisplaySearchHandle* localHandle = (Linux::LinuxDisplaySearchHandle*)handle; + Linux::LinuxDisplaySearchHandle* localHandle = (Linux::LinuxDisplaySearchHandle*)handle; if (localHandle == NULL) { OVR::LogError("[Linux Display] No search handle passed into GetDisplayCount. Return 0 rifts."); return 0; } + if (X11Display == NULL) + { + OVR::LogError("[Linux Display] Unable to open X Display!"); + return 0; + } + + int screen_count = XScreenCount(X11Display); + if (screen_count != numScreens) + { + numScreens = screen_count; + extended = true; + for (int screen = 0; screen < numScreens; ++screen) + { + // Be sure we're subscribed to config changes on all screens. + XRRSelectInput(X11Display, XRootWindow(X11Display, screen), + RRScreenChangeNotifyMask | RRCrtcChangeNotifyMask | + RROutputChangeNotifyMask | RROutputPropertyNotifyMask); + } + } + + XEvent event_return = XEvent(); + if (XCheckTypedEvent(X11Display, BaseRREvent + RRScreenChangeNotify, &event_return)) + { + extended = true; + } + if (extendedCount == -1 || extended) { extendedCount = discoverExtendedRifts(localHandle->cachedDescriptorArray, Linux::LinuxDisplaySearchHandle::DescArraySize, edidInfo); @@ -445,7 +513,6 @@ int Display::GetDisplayCount(DisplaySearchHandle* handle, bool extended, bool ap localHandle->extendedDisplayCount = extendedCount; int totalCount = extendedCount; - /// FIXME: Implement application mode for OS X. localHandle->application = false; localHandle->applicationDisplayCount = 0; @@ -484,7 +551,7 @@ Ptr<Display> Display::GetDisplay( int index, DisplaySearchHandle* handle ) if (localHandle->application) { - OVR::LogError("[Linux Display] Mac does not support application displays."); + OVR::LogError("[Linux Display] Mac does not support application X11Displays."); } return result; diff --git a/LibOVR/Src/Displays/OVR_Linux_Display.h b/LibOVR/Src/Displays/OVR_Linux_Display.h index f685e6b..1ef1dab 100644 --- a/LibOVR/Src/Displays/OVR_Linux_Display.h +++ b/LibOVR/Src/Displays/OVR_Linux_Display.h @@ -33,23 +33,6 @@ namespace OVR { namespace Linux { //------------------------------------------------------------------------------------- -// DisplayDesc - -// Display information enumerable through OS . -// TBD: Should we just move this to public header, so it's a const member of Display? -struct DisplayDesc -{ - HmdTypeEnum DeviceTypeGuess; - String DisplayID; // This is the device identifier string from MONITORINFO (for app usage) - String ModelName; // This is a "DK2" type string - String EdidSerialNumber; - Sizei LogicalResolutionInPixels; - Sizei NativeResolutionInPixels; - Vector2i DesktopDisplayOffset; -}; - - -//------------------------------------------------------------------------------------- // DisplayEDID // Describes EDID information as reported from our display driver. @@ -82,7 +65,7 @@ public: static const int DescArraySize = 16; - Linux::DisplayDesc cachedDescriptorArray[DescArraySize]; + DisplayDesc cachedDescriptorArray[DescArraySize]; bool extended; bool application; int extendedDisplayCount; @@ -102,12 +85,12 @@ public: dd.DisplayID, dd.ModelName, dd.EdidSerialNumber, - dd.LogicalResolutionInPixels, - dd.NativeResolutionInPixels, + dd.ResolutionInPixels, + dd.ResolutionInPixels, dd.DesktopDisplayOffset, 0, - 0, - false) + dd.Rotation, + false) { } diff --git a/LibOVR/Src/Displays/OVR_Linux_SDKWindow.cpp b/LibOVR/Src/Displays/OVR_Linux_SDKWindow.cpp index 3d76f69..aa9acfc 100644 --- a/LibOVR/Src/Displays/OVR_Linux_SDKWindow.cpp +++ b/LibOVR/Src/Displays/OVR_Linux_SDKWindow.cpp @@ -25,8 +25,8 @@ limitations under the License. *******************************************************************************/ #include "OVR_Linux_SDKWindow.h" -#include "../Kernel/OVR_Log.h" -#include "../Kernel/OVR_Log.h" +#include "Kernel/OVR_Log.h" +#include "Kernel/OVR_Log.h" #include "../../../3rdParty/EDID/edid.h" namespace OVR { @@ -257,6 +257,7 @@ bool SDKWindow::getVisualFromDrawable(GLXDrawable drawable, XVisualInfo* vinfoOu XVisualInfo* chosen = glXGetVisualFromFBConfig(display, *config); *vinfoOut = *chosen; XFree(config); + XFree(chosen); return true; } return false; diff --git a/LibOVR/Src/Displays/OVR_Linux_SDKWindow.h b/LibOVR/Src/Displays/OVR_Linux_SDKWindow.h index 70b95dd..81c83b3 100644 --- a/LibOVR/Src/Displays/OVR_Linux_SDKWindow.h +++ b/LibOVR/Src/Displays/OVR_Linux_SDKWindow.h @@ -27,8 +27,8 @@ limitations under the License. #ifndef OVR_Linux_SDKWindow_h #define OVR_Linux_SDKWindow_h -#include "../OVR_CAPI.h" -#include "../CAPI/GL/CAPI_GL_Util.h" +#include "OVR_CAPI.h" +#include "CAPI/GL/CAPI_GL_Util.h" #include <X11/Xlib.h> #include <X11/Xatom.h> diff --git a/LibOVR/Src/Displays/OVR_OSX_Display.cpp b/LibOVR/Src/Displays/OVR_OSX_Display.cpp index 67df51d..5b88c49 100644 --- a/LibOVR/Src/Displays/OVR_OSX_Display.cpp +++ b/LibOVR/Src/Displays/OVR_OSX_Display.cpp @@ -25,7 +25,7 @@ limitations under the License. *************************************************************************************/ #include "OVR_OSX_Display.h" -#include "../Kernel/OVR_Log.h" +#include "Kernel/OVR_Log.h" #include <ApplicationServices/ApplicationServices.h> #include <CoreFoundation/CoreFoundation.h> @@ -197,15 +197,17 @@ static int discoverExtendedRifts(OVR::OSX::DisplayDesc* descriptorArray, int inp for (unsigned int i = 0; i < NDisplays; i++) { - io_service_t port = CGDisplayIOServicePort(Displays[i]); + CGDirectDisplayID dispId = Displays[i]; + + io_service_t port = CGDisplayIOServicePort(dispId); CFDictionaryRef DispInfo = IODisplayCreateInfoDictionary(port, kNilOptions); // Display[i] - uint32_t vendor = CGDisplayVendorNumber(Displays[i]); - uint32_t product = CGDisplayModelNumber(Displays[i]); + uint32_t vendor = CGDisplayVendorNumber(dispId); + uint32_t product = CGDisplayModelNumber(dispId); - CGRect desktop = CGDisplayBounds(Displays[i]); + CGRect desktop = CGDisplayBounds(dispId); Vector2i desktopOffset(desktop.origin.x, desktop.origin.y); if (vendor == 16082 && ( (product == 1)||(product == 2)||(product == 3) ) ) // 7" or HD @@ -216,7 +218,9 @@ static int discoverExtendedRifts(OVR::OSX::DisplayDesc* descriptorArray, int inp return result; } - Sizei monitorResolution(1280, 800); + 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 = @@ -232,20 +236,46 @@ static int discoverExtendedRifts(OVR::OSX::DisplayDesc* descriptorArray, int inp parseEdid( edid, edidResult ); OVR::OSX::DisplayDesc& desc = descriptorArray[result++]; - desc.DisplayID = Displays[i]; - desc.ModelName = edidResult.MonitorName; // User friendly string. - desc.EdidSerialNumber = edidResult.SerialNumber; + 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; + 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; + 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. @@ -279,6 +309,10 @@ bool Display::Initialize() return true; } +void Display::Shutdown() +{ +} + bool Display::GetDriverMode(bool& driverInstalled, bool& compatMode, bool& hideDK1Mode) { diff --git a/LibOVR/Src/Displays/OVR_OSX_Display.h b/LibOVR/Src/Displays/OVR_OSX_Display.h index a69c955..d76e8db 100644 --- a/LibOVR/Src/Displays/OVR_OSX_Display.h +++ b/LibOVR/Src/Displays/OVR_OSX_Display.h @@ -53,6 +53,7 @@ struct DisplayDesc Sizei LogicalResolutionInPixels; Sizei NativeResolutionInPixels; Vector2i DesktopDisplayOffset; + int Rotation; }; @@ -112,8 +113,8 @@ public: dd.NativeResolutionInPixels, dd.DesktopDisplayOffset, 0, - 0, - false) + dd.Rotation, + false) { } diff --git a/LibOVR/Src/Displays/OVR_OSX_FocusObserver.h b/LibOVR/Src/Displays/OVR_OSX_FocusObserver.h deleted file mode 100644 index e2f28e3..0000000 --- a/LibOVR/Src/Displays/OVR_OSX_FocusObserver.h +++ /dev/null @@ -1,75 +0,0 @@ -/************************************************************************************ - -Filename : OVR_OSX_FocusObserver.h -Content : Observer for app focus on OSX -Created : August 5, 2014 -Authors : Jordan Tritell - -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. - -*************************************************************************************/ - -OVR_PRIVATE_FILE - -#ifndef OVR_OSX_FocusObserver_h -#define OVR_OSX_FocusObserver_h - -#include "../Kernel/OVR_Threads.h" -#include "../Kernel/OVR_System.h" -#include "../Kernel/OVR_Lockless.h" - -#include "../Service/Service_NetServer.h" - -namespace OVR { namespace OSX{ - - struct FocusNotifierImpl; - -class AppFocusObserver : public SystemSingletonBase<AppFocusObserver> -{ - OVR_DECLARE_SINGLETON(AppFocusObserver); - -public: - Lock ListLock; - Array<pid_t> AppList; - Service::NetServerListener *listener; - FocusNotifierImpl* impl; - - void OnProcessFocus(pid_t pid); - void SetListener(Service::NetServerListener *_listener); - - pid_t LastProcessId; - pid_t ActiveProcessId; - void AddProcess(pid_t pid); - void nextProcess(); - void RemoveProcess(pid_t pid); - - -protected: - void onAppFocus(pid_t pid); - - pid_t LastAppFocus; - -}; - - - -}} // namespace OVR, OSX - - -#endif /* OVR_OSX_FocusObserver_h */ - diff --git a/LibOVR/Src/Displays/OVR_OSX_FocusObserver.mm b/LibOVR/Src/Displays/OVR_OSX_FocusObserver.mm deleted file mode 100644 index 3dd77a8..0000000 --- a/LibOVR/Src/Displays/OVR_OSX_FocusObserver.mm +++ /dev/null @@ -1,245 +0,0 @@ -/************************************************************************************
-
-Filename : OVR_OSX_FocusObserver.mm
-Content : Observer for app focus on OSX
-Created : August 5, 2014
-Authors : Jordan Tritell
-
-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.
-
-*************************************************************************************/
-
-OVR_PRIVATE_FILE
-
-#include "OVR_OSX_FocusObserver.h"
-
-#include "../Service/Service_NetServer.h"
-#include "OVR_DisplayEnumerator.h"
-
-#include <Cocoa/Cocoa.h>
-
-OVR_DEFINE_SINGLETON(OVR::OSX::AppFocusObserver);
-
-extern bool ServiceRunningFlag;
-
-@interface FocusNotifier : NSObject <NSApplicationDelegate>{
- NSWindow *window;
-}
-
-- (void)start;
-
-@property (assign) IBOutlet NSWindow *window;
-
-@end
-
-
-@implementation FocusNotifier
-
-@synthesize window;
-
-- (void) addActivationObserver
-{
- //subscribe to focus notifications from the workspace
- [[[NSWorkspace sharedWorkspace] notificationCenter] addObserver:self
- selector:@selector(activated:)
- name:NSWorkspaceDidActivateApplicationNotification
- object:nil];
-
- //subscribe to termination notifications from the workspace
- [[[NSWorkspace sharedWorkspace] notificationCenter] addObserver:self
- selector:@selector(terminated:)
- name:NSWorkspaceDidTerminateApplicationNotification
- object:nil];
-}
-
--(void) activated:(NSNotification *)notification
-{
- NSRunningApplication *app = [[notification userInfo] objectForKey:@"NSWorkspaceApplicationKey"];
- pid_t pid = [app processIdentifier];
- OVR::OSX::AppFocusObserver::GetInstance()->OnProcessFocus(pid);
- // NSLog(@"Activated: %@", [activatedApp bundleIdentifier]);
-}
-
--(void) terminated:(NSNotification *)notification
-{
- NSRunningApplication *app = [[notification userInfo] objectForKey:@"NSWorkspaceApplicationKey"];
- pid_t pid = [app processIdentifier];
- OVR::OSX::AppFocusObserver::GetInstance()->RemoveProcess(pid);
- // NSLog(@"Activated: %@", [activatedApp bundleIdentifier]);
-}
-
-- (void)start
-{
- //initialize with relevant variables
- [self addActivationObserver];
-}
-
-@end
-
-
-namespace OVR { namespace OSX{
-
-struct FocusNotifierImpl
-{
- FocusNotifier* wrapped;
-};
-
-AppFocusObserver::AppFocusObserver():
- impl(new FocusNotifierImpl),
- listener(NULL)
-{
- //initialize with correct values
- impl->wrapped = [[FocusNotifier alloc] init];
- [impl->wrapped start];
- ActiveProcessId = 0;
-}
-
-AppFocusObserver::~AppFocusObserver()
-{
- [impl->wrapped dealloc];
- delete impl;
-}
-
-void AppFocusObserver::SetListener(Service::NetServerListener *_listener)
-{
- listener = _listener;
-}
-
-void AppFocusObserver::OnSystemDestroy()
-{
-}
-
-void AppFocusObserver::OnProcessFocus(pid_t pid)
-{
- // If the process changed,
- if (pid != LastProcessId)
- {
- LastProcessId = pid;
-
- Lock::Locker locker(&ListLock);
-
- // Find the process id in the list
- const int count = AppList.GetSizeI();
- for (int i = 0; i < count; ++i)
- {
- // If it is a rift process,
- if (AppList[i] == pid)
- {
- onAppFocus(pid);
- OVR_DEBUG_LOG(("[AppFocusObserver] Oculus Process getting focus: pid=%d", pid));
- return;
- }
- }
-
- OVR_DEBUG_LOG(("[AppFocusObserver] Focus change: %d (non-Oculus process)", pid));
- }
-}
-
-void AppFocusObserver::AddProcess(pid_t pid)
-{
- Lock::Locker locker(&ListLock);
-
- // If it already exists in the array,
- const int count = AppList.GetSizeI();
- for (int i = 0; i < count; ++i)
- {
- // If we found it,
- if (AppList[i] == pid)
- {
- return;
- }
- }
-
- // If the process being added is already in focus,
- if (pid == LastProcessId)
- {
- // Set the active process
- OVR_DEBUG_LOG(("[AppFocusObserver] AddProcess: Recognizing the newly added process as in-focus pid=%d", pid));
- }
-
- // Add it to the list
- AppList.PushBack(pid);
-}
-
-void AppFocusObserver::nextProcess()
-{
- Lock::Locker locker(&ListLock);
-
- int count = AppList.GetSizeI();
-
- // Pick the next available rift process
- if (count > 0)
- {
- ActiveProcessId = AppList[0];
- OVR_DEBUG_LOG(("[AppFocusObserver] NextProcess: Switching active rift process to pid=%d", ActiveProcessId));
- onAppFocus(ActiveProcessId);
- return;
- }
-
- // No process to use
- onAppFocus(ActiveProcessId);
- OVR_DEBUG_LOG(("[AppFocusObserver] NextProcess: No remaining rift processes"));
-}
-
-
-void AppFocusObserver::onAppFocus(pid_t pid)
-{
- // Note: This is not necessarily the same as the FocusState->ActiveProcessId.
-
- ActiveProcessId = pid;
-
- if (ActiveProcessId == 0) return;
-
- if (pid != LastAppFocus)
- {
- LastAppFocus = pid;
- if(listener){
- listener->onFocusChange(pid);
- }
-// FocusSubject->Call(pid);
- }
-}
-
-void AppFocusObserver::RemoveProcess(pid_t pid)
-{
- Lock::Locker locker(&ListLock);
-
- // Find the pid in the app list:
- const int count = AppList.GetSizeI();
- for (int i = 0; i < count; ++i)
- {
- // If the app was found,
- if (AppList[i] == pid)
- {
- // Remove from list here
- AppList.RemoveAtUnordered(i);
-
- // If the removed process is the active one,
- if (ActiveProcessId == pid)
- {
- OVR_DEBUG_LOG(("[AppFocusObserver] RemoveProcess: Active process going away"));
- // Find a new active process
- nextProcess();
- }
-
- break;
- }
- }
-}
-}} //namespace OVR::OSX
-
diff --git a/LibOVR/Src/Displays/OVR_OSX_FocusReader.h b/LibOVR/Src/Displays/OVR_OSX_FocusReader.h deleted file mode 100644 index dbfe9f6..0000000 --- a/LibOVR/Src/Displays/OVR_OSX_FocusReader.h +++ /dev/null @@ -1,45 +0,0 @@ -/************************************************************************************ - -Filename : OVR_OSX_FocusReader.h -Content : Reader for current app with focus on OSX -Created : August 5, 2014 -Authors : Jordan Tritell - -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. - -*************************************************************************************/ - -OVR_PRIVATE_FILE - -#ifndef OVR_OSX_FocusReader_h -#define OVR_OSX_FocusReader_h - -#import <Cocoa/Cocoa.h> - -@interface FocusReader : NSObject <NSApplicationDelegate>{ - NSWindow *window; -} - -- (void)start; - -@property (assign) IBOutlet NSWindow *window; - -@end - -#endif /* OVR_OSX_FocusReader_h */ - diff --git a/LibOVR/Src/Displays/OVR_OSX_FocusReader.mm b/LibOVR/Src/Displays/OVR_OSX_FocusReader.mm deleted file mode 100644 index bed95d6..0000000 --- a/LibOVR/Src/Displays/OVR_OSX_FocusReader.mm +++ /dev/null @@ -1,75 +0,0 @@ -/************************************************************************************
-
-Filename : OVR_OSX_FocusReader.mm
-Content : Reader for current app with focus on OSX
-Created : August 5, 2014
-Authors : Jordan Tritell
-
-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.
-
-*************************************************************************************/
-
-OVR_PRIVATE_FILE
-
-#import "OVR_OSX_FocusReader.h"
-#import "OVR_OSX_FocusObserver.h"
-
-@implementation FocusReader
-
-@synthesize window;
-
-- (void) addActivationObserver
-{
- //subscribe to focus notifications from the workspace
- [[[NSWorkspace sharedWorkspace] notificationCenter] addObserver:self
- selector:@selector(activated:)
- name:NSWorkspaceDidActivateApplicationNotification
- object:nil];
-
- //subscribe to focus notifications from the workspace
- [[[NSWorkspace sharedWorkspace] notificationCenter] addObserver:self
- selector:@selector(terminated:)
- name:NSWorkspaceDidTerminateApplicationNotification
- object:nil];
-}
-
--(void) activated:(NSNotification *)notification
-{
- NSRunningApplication *app = [[notification userInfo] objectForKey:@"NSWorkspaceApplicationKey"];
- pid_t pid = [app processIdentifier];
- OVR::OSX::AppFocusObserver::GetInstance()->OnProcessFocus(pid);
- // NSLog(@"Activated: %@", [activatedApp bundleIdentifier]);
-}
-
--(void) terminated:(NSNotification *)notification
-{
- NSRunningApplication *app = [[notification userInfo] objectForKey:@"NSWorkspaceApplicationKey"];
- pid_t pid = [app processIdentifier];
- OVR::OSX::AppFocusObserver::GetInstance()->RemoveProcess(pid);
- // NSLog(@"Activated: %@", [activatedApp bundleIdentifier]);
-}
-
-- (void)start
-{
- //initialize with relevant variables
- [self addActivationObserver];
-}
-
-@end
-
-
diff --git a/LibOVR/Src/Displays/OVR_Win32_Display.cpp b/LibOVR/Src/Displays/OVR_Win32_Display.cpp index 677c619..f45dacc 100644 --- a/LibOVR/Src/Displays/OVR_Win32_Display.cpp +++ b/LibOVR/Src/Displays/OVR_Win32_Display.cpp @@ -1,1323 +1,720 @@ -/************************************************************************************
-
-Filename : OVR_Win32_Display.cpp
-Content : Win32 Display implementation
-Created : May 6, 2014
-Authors : Dean Beeler
-
-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 <Windows.h>
-
-#include "OVR_Win32_Display.h"
-#include "OVR_Win32_Dxgi_Display.h"
-
-#include <stdio.h>
-#include <tchar.h>
-#include <string.h>
-#include <stdlib.h>
-#include <winioctl.h>
-#include <SetupAPI.h>
-#include <Mmsystem.h>
-#include <conio.h>
-
-#ifdef OVR_COMPAT_EDID_VIA_WMI
-# pragma comment(lib, "wbemuuid.lib")
-#include <wbemidl.h>
-#else // OVR_COMPAT_EDID_VIA_WMI
-#include <setupapi.h>
-#include <initguid.h>
-#endif // OVR_COMPAT_EDID_VIA_WMI
-
-// WIN32_LEAN_AND_MEAN included in OVR_Atomic.h may break 'byte' declaration.
-#ifdef WIN32_LEAN_AND_MEAN
- typedef unsigned char byte;
-#endif
-
-#include "../Kernel/OVR_String.h"
-#include "../Kernel/OVR_Log.h"
-
-typedef struct
-{
- HANDLE hDevice;
- UINT ExpectedWidth;
- UINT ExpectedHeight;
- HWND hWindow;
- bool CompatibilityMode;
- bool HideDK1Mode;
-} ContextStruct;
-
-static ContextStruct GlobalDisplayContext = {0};
-
-
-//-------------------------------------------------------------------------------------
-// ***** Display enumeration Helpers
-
-// THere are two ways to enumerate display: through our driver (DeviceIoControl)
-// and through Win32 EnumDisplayMonitors (compatibility mode).
-
-
-namespace OVR {
-
-ULONG getRiftCount( HANDLE hDevice )
-{
- ULONG riftCount = 0;
- DWORD bytesReturned = 0;
-
- BOOL result = DeviceIoControl( hDevice, IOCTL_RIFTMGR_GET_RIFT_COUNT, NULL, 0,
- &riftCount, sizeof( ULONG ), &bytesReturned, NULL );
-
- if( result )
- return riftCount;
- else
- return 0;
-}
-
-ULONG getRift( HANDLE hDevice, int index )
-{
- ULONG riftCount = getRiftCount( hDevice );
- DWORD bytesReturned = 0;
- BOOL result;
-
- if( riftCount >= (ULONG)index )
- {
- RIFT_STATUS riftStatus[16] = {0};
-
- result = DeviceIoControl( hDevice, IOCTL_RIFTMGR_GET_RIFT_ARRAY, riftStatus,
- riftCount * sizeof( RIFT_STATUS ), &riftCount,
- sizeof( ULONG ), &bytesReturned, NULL );
- if( result )
- {
- PRIFT_STATUS tmpRift;
- unsigned int i;
- for( i = 0, tmpRift = riftStatus; i < riftCount; ++i, ++tmpRift )
- {
- if( i == (unsigned int)index )
- return tmpRift->childUid;
- }
- }
- else
- {
- printf("Failed to get array of rift devices\n");
- }
- }
-
- return 0;
-}
-
-#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 byte edid_v1_header[] = { 0x00, 0xff, 0xff, 0xff,
- 0xff, 0xff, 0xff, 0x00 };
-
-const byte edid_v1_descriptor_flag[] = { 0x00, 0x00 };
-
-
-static int blockType( byte* 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( byte const* block )
-{
- static char name[ 13 ];
- unsigned i;
- byte 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;
-}
-
-
-static bool parseEdid( byte* edid, OVR::Win32::DisplayEDID& edidResult )
-{
- unsigned i;
- byte* block;
- char* monitor_name = "Unknown";
- byte 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;
- }
-
- //printf( "\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;
- }
- }
-
- BYTE 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);
-
-#if 0
- printf( "\tIdentifier \"%s\"\n", monitor_name );
- printf( "\tVendorName \"%s\"\n", vendorString );
- printf( "\tModelName \"%s\"\n", monitor_name );
- printf( "\tModelNumber %d\n", modelNumber );
- printf( "\tSerialNumber \"%x\"\n", *serialPointer );
-#endif
-
- // FIXME: Get timings as well, though they aren't very useful here
- // except for the vertical refresh rate, presumably
-
- return true;
-}
-
-static bool getEdid(HANDLE hDevice, ULONG uid, OVR::Win32::DisplayEDID& edidResult)
-{
- ULONG riftCount = 0;
- DWORD bytesReturned = 0;
- RIFT_STATUS riftStatus[16] = {0};
-
- BOOL result = DeviceIoControl( hDevice, IOCTL_RIFTMGR_GET_RIFT_COUNT, NULL, 0,
- &riftCount, sizeof( ULONG ), &bytesReturned, NULL );
-
- if (!result)
- {
- return false;
- }
-
- result = DeviceIoControl( hDevice, IOCTL_RIFTMGR_GET_RIFT_ARRAY, &riftStatus,
- riftCount * sizeof( RIFT_STATUS ), &riftCount, sizeof(ULONG),
- &bytesReturned, NULL );
- if (!result)
- {
- return false;
- }
-
- for (ULONG i = 0; i < riftCount; ++i)
- {
- ULONG riftUid = riftStatus[i].childUid;
- if (riftUid == uid)
- {
- char edidBuffer[512];
-
- result = DeviceIoControl(hDevice, IOCTL_RIFTMGR_GETEDID, &riftUid, sizeof(ULONG),
- edidBuffer, 512, &bytesReturned, NULL);
-
- if (result)
- {
- if (parseEdid((byte*)edidBuffer, edidResult))
- {
- return true;
- }
- else
- {
- OVR_DEBUG_LOG(("[Win32Display] WARNING: The driver was not able to return EDID for a display"));
- }
- }
-
- break;
- }
- }
-
- return false;
-}
-
-
-// Used to capture all the active monitor handles
-struct MonitorSet
-{
- enum { MaxMonitors = 8 };
- HMONITOR Monitors[MaxMonitors];
- int MonitorCount;
- int PrimaryCount;
-};
-
-static BOOL CALLBACK MonitorEnumProc(HMONITOR hMonitor, HDC, LPRECT, LPARAM dwData)
-{
- MonitorSet* monitorSet = (MonitorSet*)dwData;
- if (monitorSet->MonitorCount >= MonitorSet::MaxMonitors)
- return FALSE;
-
- monitorSet->Monitors[monitorSet->MonitorCount] = hMonitor;
- monitorSet->MonitorCount++;
- return TRUE;
-};
-
-#ifdef OVR_COMPAT_EDID_VIA_WMI
-
-static bool getCompatDisplayEDID( WCHAR* displayName, String& serialNumberStr, String& userFriendlyNameStr )
-{
- IWbemLocator *pLoc = NULL;
- IWbemServices *pSvc = NULL;
- HRESULT hres;
-
- static bool initialized = false;
- static bool selfInitialized = true;
- if (!initialized)
- {
- hres = CoInitializeEx(0, COINIT_MULTITHREADED);
- if (FAILED(hres))
- {
-
- LogError("{ERR-082} [Display] WARNING: Failed to initialize COM library. Error code = 0x%x", hres);
- OVR_ASSERT(false);
- return false;
- }
-
- hres = CoInitializeSecurity(
- NULL,
- -1, // COM authentication
- NULL, // Authentication services
- NULL, // Reserved
- RPC_C_AUTHN_LEVEL_DEFAULT, // Default authentication
- RPC_C_IMP_LEVEL_IMPERSONATE, // Default Impersonation
- NULL, // Authentication info
- EOAC_NONE, // Additional capabilities
- NULL // Reserved
- );
-
- if (FAILED(hres))
- {
- LogError("{ERR-083} [Display] WARNING: Failed to initialize security. Error code = 0x%x", hres);
- OVR_ASSERT(false);
- selfInitialized = false;
- }
-
- initialized = true;
- }
-
- hres = CoCreateInstance(
- CLSID_WbemLocator,
- 0,
- CLSCTX_INPROC_SERVER,
- IID_IWbemLocator, (LPVOID *)&pLoc);
-
- if (FAILED(hres))
- {
- LogError("{ERR-084} [Display] WARNING: Failed to create IWbemLocator object.Err code = 0x%x", hres);
- OVR_ASSERT(false);
- return false;
- }
-
- BSTR AbackB = SysAllocString(L"root\\WMI");
- // Connect to the root\cimv2 namespace with
- // the current user and obtain pointer pSvc
- // to make IWbemServices calls.
- hres = pLoc->ConnectServer(
- AbackB, // Object path of WMI namespace
- NULL, // User name. NULL = current user
- NULL, // User password. NULL = current
- 0, // Locale. NULL indicates current
- NULL, // Security flags.
- 0, // Authority (e.g. Kerberos)
- 0, // Context object
- &pSvc // pointer to IWbemServices proxy
- );
- SysFreeString(AbackB);
-
- if (FAILED(hres))
- {
- LogError("{ERR-085} [Display] WARNING: Could not connect to root\\WMI. Error code = 0x%x", hres);
- OVR_ASSERT(false);
- pLoc->Release();
- return false;
- }
-
- hres = CoSetProxyBlanket(
- pSvc, // Indicates the proxy to set
- RPC_C_AUTHN_WINNT, // RPC_C_AUTHN_xxx
- RPC_C_AUTHZ_NONE, // RPC_C_AUTHZ_xxx
- NULL, // Server principal name
- RPC_C_AUTHN_LEVEL_CALL, // RPC_C_AUTHN_LEVEL_xxx
- RPC_C_IMP_LEVEL_IMPERSONATE, // RPC_C_IMP_LEVEL_xxx
- NULL, // client identity
- EOAC_NONE // proxy capabilities
- );
-
- if (FAILED(hres))
- {
- LogError("{ERR-086} [Display] WARNING: Could not set proxy blanket. Error code = 0x%x", hres);
- OVR_ASSERT(false);
- pSvc->Release();
- pLoc->Release();
- return false;
- }
-
-
- BSTR wql = SysAllocString(L"WQL");
- BSTR select = SysAllocString(L"SELECT * FROM WmiMonitorID");
- IEnumWbemClassObject* pEnumerator = NULL;
- hres = pSvc->ExecQuery(
- wql,
- select,
- WBEM_FLAG_FORWARD_ONLY | WBEM_FLAG_RETURN_IMMEDIATELY,
- NULL,
- &pEnumerator);
- SysFreeString(wql);
- SysFreeString(select);
-
- if (FAILED(hres))
- {
- LogError("{ERR-087} [Display] WARNING: Query for operating system name failed. Error code = 0x%x", hres);
- OVR_ASSERT(false);
- pSvc->Release();
- pLoc->Release();
- return false;
- }
-
- int enumeratedCount = 0;
- bool found = false;
-
- IWbemClassObject *pclsObj = 0;
- while (pEnumerator)
- {
- ULONG uReturn = 0;
- HRESULT hr = pEnumerator->Next(WBEM_INFINITE, 1, &pclsObj, &uReturn);
-
- if (FAILED(hr) || !uReturn)
- {
- break;
- }
-
- ++enumeratedCount;
-
- VARIANT vtProp;
- hr = pclsObj->Get(L"InstanceName", 0, &vtProp, 0, 0);
-
- WCHAR* instanceName = vtProp.bstrVal;
- WCHAR* nextToken = NULL;
- if (SUCCEEDED(hr) &&
- wcstok_s(instanceName, L"\\", &nextToken) != NULL)
- {
- WCHAR* aToken = wcstok_s(NULL, L"\\", &nextToken);
-
- if (aToken != NULL)
- {
- VariantClear(&vtProp);
-
- if (wcscmp(aToken, displayName) != 0)
- {
- pclsObj->Release();
- continue;
- }
-
- // Read serial
-
- hr = pclsObj->Get(L"SerialNumberID", 0, &vtProp, 0, 0);
-
- if (SUCCEEDED(hr))
- {
- if (vtProp.vt != VT_NULL && vtProp.parray != NULL)
- {
- static const int MaxSerialBytes = 14;
- char serialNumber[MaxSerialBytes] = { 0 };
-
- UINT32* serialArray = (UINT32*)vtProp.parray->pvData;
- for (int i = 0; i < MaxSerialBytes; ++i)
- {
- serialNumber[i] = (BYTE)(serialArray[i] & 0xff);
- }
-
- serialNumber[sizeof(serialNumber)-1] = '\0';
- serialNumberStr = serialNumber;
- }
- else
- {
- LogError("{ERR-088} [Display] WARNING: Wrong data format for SerialNumberID");
- }
-
- VariantClear(&vtProp);
- }
- else
- {
- LogError("{ERR-089} [Display] WARNING: Failure getting display SerialNumberID: %d", (int)hr);
- }
-
- // Read length of name
-
- int userFriendlyNameLen = 0;
-
- hr = pclsObj->Get(L"UserFriendlyNameLength", 0, &vtProp, 0, 0);
-
- if (SUCCEEDED(hr))
- {
- if (vtProp.vt != VT_NULL)
- {
- userFriendlyNameLen = vtProp.iVal;
-
- if (userFriendlyNameLen <= 0)
- {
- userFriendlyNameLen = 0;
-
- LogError("{ERR-090} [Display] WARNING: UserFriendlyNameLength = 0");
- }
- }
- else
- {
- LogError("{ERR-091} [Display] WARNING: Wrong data format for UserFriendlyNameLength");
- }
-
- VariantClear(&vtProp);
- }
- else
- {
- LogError("{ERR-092} [Display] WARNING: Failure getting display UserFriendlyNameLength: %d", (int)hr);
- }
-
- // Read name
-
- hr = pclsObj->Get(L"UserFriendlyName", 0, &vtProp, 0, 0);
-
- if (SUCCEEDED(hr) && userFriendlyNameLen > 0)
- {
- if (vtProp.vt != VT_NULL && vtProp.parray != NULL)
- {
- static const int MaxNameBytes = 64;
- char userFriendlyName[MaxNameBytes] = { 0 };
-
- UINT32* nameArray = (UINT32*)vtProp.parray->pvData;
- for (int i = 0; i < MaxNameBytes && i < userFriendlyNameLen; ++i)
- {
- userFriendlyName[i] = (BYTE)(nameArray[i] & 0xff);
- }
-
- userFriendlyName[sizeof(userFriendlyName)-1] = '\0';
- userFriendlyNameStr = userFriendlyName;
- }
- else
- {
- // See: https://developer.oculusvr.com/forums/viewtopic.php?f=34&t=10961
- // This can happen if someone has an EDID override in the registry.
- LogError("{ERR-093} [Display] WARNING: Wrong data format for UserFriendlyName");
- }
-
- VariantClear(&vtProp);
- }
- else
- {
- LogError("{ERR-094} [Display] WARNING: Failure getting display UserFriendlyName: %d", (int)hr);
- }
- }
-
- found = true;
- pclsObj->Release();
- break;
- }
-
- pclsObj->Release();
- }
-
- HMODULE hModule = GetModuleHandleA("wbemuuid");
- if (hModule)
- {
- DisableThreadLibraryCalls(hModule);
- }
-
- pSvc->Release();
- pLoc->Release();
- pEnumerator->Release();
-
- if (!found)
- {
- LogError("{ERR-095} [Display] WARNING: Unable to enumerate the rift via WMI (found %d monitors). This is not normally an issue. Running as a user with Administrator privileges has fixed this problem in the past.", enumeratedCount);
- OVR_ASSERT(false);
- }
-
- return found;
-}
-
-#else // OVR_COMPAT_EDID_VIA_WMI
-
-#define NAME_SIZE 128
-
-DEFINE_GUID(GUID_CLASS_MONITOR,
- 0x4d36e96e, 0xe325, 0x11ce, 0xbf, 0xc1, 0x08, 0x00, 0x2b, 0xe1,
- 0x03, 0x18);
-
-static void truncateNonalphanum(char* str, int len)
-{
- for (int i = 0; i < len; ++i)
- {
- char ch = str[i];
-
- if ((ch < 'A' || ch > 'Z') &&
- (ch < 'a' || ch > 'z') &&
- (ch < '0' || ch > '9') &&
- ch != ' ')
- {
- str[i] = '\0';
- break;
- }
- }
-}
-
-static bool AccessDeviceRegistry(IN HDEVINFO devInfo, IN PSP_DEVINFO_DATA devInfoData,
- const WCHAR* displayName, String& serialNumberStr, String& userFriendlyNameStr)
-{
- // Match hardware id to display name
- WCHAR hardwareId[128];
- if (!SetupDiGetDeviceRegistryProperty(
- devInfo,
- devInfoData,
- SPDRP_HARDWAREID,
- NULL,
- (PBYTE)hardwareId,
- sizeof(hardwareId),
- NULL))
- {
- LogError("{ERR-096} [Display] WARNING: SetupDiGetDeviceRegistryProperty for SPDRP_HARDWAREID failed. LastErr=%d", GetLastError());
- OVR_ASSERT(false);
- return false;
- }
- hardwareId[127] = 0;
-
- // If the hardware id did not match,
- if (!wcsstr(hardwareId, displayName))
- {
- // Stop here
- return false;
- }
-
- // Grab hardware info registry key
- HKEY hDevRegKey = SetupDiOpenDevRegKey(
- devInfo,
- devInfoData,
- DICS_FLAG_GLOBAL,
- 0,
- DIREG_DEV,
- KEY_READ); // Only read permissions so it can be run without Administrator privs
-
- if (hDevRegKey == INVALID_HANDLE_VALUE)
- {
- LogError("{ERR-097} [Display] WARNING: SetupDiOpenDevRegKey failed. LastErr=%d", GetLastError());
- OVR_ASSERT(false);
- return false;
- }
-
- // Enumerate keys in registry
- bool found = false;
-
- // For each key,
- for (int i = 0;; i++)
- {
- BYTE EDIDdata[1024];
- DWORD edidsize = sizeof(EDIDdata);
-
- DWORD dwType, ActualValueNameLength = NAME_SIZE;
- CHAR valueName[NAME_SIZE];
-
- // Read the key value data
- LSTATUS retValue = RegEnumValueA(
- hDevRegKey,
- i,
- &valueName[0],
- &ActualValueNameLength,
- NULL,
- &dwType,
- EDIDdata,
- &edidsize);
-
- // If no more items in the enumeration,
- if (retValue == ERROR_NO_MORE_ITEMS)
- {
- // Stop here
- break;
- }
-
- // If the request failed,
- if (FAILED(retValue))
- {
- LogError("{ERR-098} [Display] WARNING: RegEnumValueA failed to read a key. LastErr=%d", retValue);
- OVR_ASSERT(false);
- }
- else if (0 == strcmp(valueName, "EDID")) // Value is EDID:
- {
- // Tested working for DK1 and DK2:
-
- char friendlyString[9];
- memcpy(friendlyString, EDIDdata + 77, 8);
- truncateNonalphanum(friendlyString, 8);
- friendlyString[8] = '\0';
-
- char edidString[14];
- memcpy(edidString, EDIDdata + 95, 13);
- truncateNonalphanum(edidString, 13);
- edidString[13] = '\0';
-
- serialNumberStr = edidString;
- userFriendlyNameStr = friendlyString;
-
- found = true;
- break;
- }
- }
-
- RegCloseKey(hDevRegKey);
-
- return found;
-}
-
-static bool getCompatDisplayEDID(const WCHAR* displayName, String& serialNumberStr, String& userFriendlyNameStr)
-{
- HDEVINFO devInfo = NULL;
-
- devInfo = SetupDiGetClassDevsEx(
- &GUID_CLASS_MONITOR, //class GUID
- NULL, //enumerator
- NULL, //HWND
- DIGCF_PRESENT, // Flags //DIGCF_ALLCLASSES|
- NULL, // device info, create a new one.
- NULL, // machine name, local machine
- NULL);// reserved
-
- if (NULL == devInfo)
- {
- return false;
- }
-
- DWORD lastError = 0;
-
- // For each display,
- for (int i = 0;; i++)
- {
- SP_DEVINFO_DATA devInfoData = {};
- devInfoData.cbSize = sizeof(devInfoData);
-
- // Grab device info
- if (SetupDiEnumDeviceInfo(devInfo, i, &devInfoData))
- {
- // Access device info from registry
- if (AccessDeviceRegistry(devInfo, &devInfoData, displayName, serialNumberStr, userFriendlyNameStr))
- {
- return true;
- }
- }
- else
- {
- lastError = GetLastError();
-
- // If no more items found,
- if (lastError != ERROR_NO_MORE_ITEMS)
- {
- LogError("{ERR-099} [Display] WARNING: SetupDiEnumDeviceInfo failed. LastErr=%d", lastError);
- OVR_ASSERT(false);
- }
-
- break;
- }
- }
-
- LogError("{ERR-100} [Display] WARNING: SetupDiEnumDeviceInfo did not return the rift display. LastErr=%d", lastError);
- OVR_ASSERT(false);
-
- return false;
-}
-
-#endif // OVR_COMPAT_EDID_VIA_WMI
-
-// This is function that's used
-bool anyRiftsInExtendedMode()
-{
- bool result = false;
-
- MonitorSet monitors;
- monitors.MonitorCount = 0;
- // Get all the monitor handles
- EnumDisplayMonitors(NULL, NULL, MonitorEnumProc, (LPARAM)&monitors);
-
- DISPLAY_DEVICE dd, ddm;
- UINT i, j;
-
- for( i = 0;
- (ZeroMemory(&dd, sizeof(dd)), dd.cb = sizeof(dd),
- EnumDisplayDevices(0, i, &dd, 0)) != 0; i++ )
- {
- for( j = 0;
- (ZeroMemory(&ddm, sizeof(ddm)), ddm.cb = sizeof(ddm),
- EnumDisplayDevices(dd.DeviceName, j, &ddm, 0)) != 0; j++ )
- {
- // Our monitor hardware has string "RTD2205" in it
- // Nate's device "CVT0003"
- if( wcsstr(ddm.DeviceID, L"RTD2205") ||
- wcsstr(ddm.DeviceID, L"CVT0003") ||
- wcsstr(ddm.DeviceID, L"MST0030") ||
- wcsstr(ddm.DeviceID, L"OVR00") ) // Part of Oculus EDID.
- {
- result = true;
- }
- }
- }
-
- return result;
-}
-
-static int discoverExtendedRifts(OVR::Win32::DisplayDesc* descriptorArray, int inputArraySize, bool includeEDID)
-{
- static bool reportDiscovery = true;
-
- int result = 0;
-
- MonitorSet monitors;
- monitors.MonitorCount = 0;
- // Get all the monitor handles
- EnumDisplayMonitors(NULL, NULL, MonitorEnumProc, (LPARAM)&monitors);
-
- DISPLAY_DEVICE dd, ddm;
- UINT i, j;
-
- for (i = 0;
- (ZeroMemory(&dd, sizeof(dd)), dd.cb = sizeof(dd),
- EnumDisplayDevices(0, i, &dd, 0)) != 0; i++)
- {
- for (j = 0;
- (ZeroMemory(&ddm, sizeof(ddm)), ddm.cb = sizeof(ddm),
- EnumDisplayDevices(dd.DeviceName, j, &ddm, 0)) != 0; j++)
- {
- if( result >= inputArraySize )
- return result;
-
- // Our monitor hardware has string "RTD2205" in it
- // Nate's device "CVT0003"
- if (wcsstr(ddm.DeviceID, L"RTD2205") ||
- wcsstr(ddm.DeviceID, L"CVT0003") ||
- wcsstr(ddm.DeviceID, L"MST0030") ||
- wcsstr(ddm.DeviceID, L"OVR00") ) // Part of Oculus EDID.
- {
- String deviceId(ddm.DeviceID);
- String displayDeviceName(ddm.DeviceName);
- Vector2i desktopOffset(0, 0);
- Sizei monitorResolution(1280, 800);
-
- // Make a device type guess
- HmdTypeEnum deviceTypeGuess = HmdType_Unknown;
- if (wcsstr(ddm.DeviceID, L"OVR0003"))
- {
- // DK2 prototypes and variants
- deviceTypeGuess = HmdType_DK2;
- // Could also be:
- // HmdType_CrystalCoveProto
- }
- else if (wcsstr(ddm.DeviceID, L"OVR0002"))
- { // HD Prototypes
- deviceTypeGuess = HmdType_DKHDProto;
- // Could also be:
- // HmdType_DKHD2Proto
- // HmdType_DKHDProto566Mi
- }
- else if (wcsstr(ddm.DeviceID, L"OVR0001"))
- { // DK1
- deviceTypeGuess = HmdType_DK1;
- }
- else if (wcsstr(ddm.DeviceID, L"OVR00"))
- { // Future Oculus HMD devices
- deviceTypeGuess = HmdType_Unknown;
- }
- else
- {
- deviceTypeGuess = HmdType_DKProto;
- }
-
- // Find the matching MONITORINFOEX for this device so we can get the
- // screen coordinates
- MONITORINFOEX info;
- for (int m=0; m < monitors.MonitorCount; m++)
- {
- info.cbSize = sizeof(MONITORINFOEX);
- GetMonitorInfo(monitors.Monitors[m], &info);
- if (_tcsstr(ddm.DeviceName, info.szDevice) == ddm.DeviceName)
- { // If the device name starts with the monitor name
- // then we found the matching DISPLAY_DEVICE and MONITORINFO
- // so we can gather the monitor coordinates
- desktopOffset = Vector2i(info.rcMonitor.left, info.rcMonitor.top);
- break;
- }
- }
-
- WCHAR* instanceBuffer = (WCHAR*)calloc(wcslen(ddm.DeviceID) + 1, sizeof(WCHAR));
- wcscpy_s(instanceBuffer, wcslen(ddm.DeviceID) + 1, ddm.DeviceID);
- WCHAR* instanceName = instanceBuffer;
- WCHAR* nextToken = NULL;
- if (wcstok_s(instanceName, L"\\", &nextToken))
- {
- WCHAR* aToken = wcstok_s(NULL, L"\\", &nextToken);
-
- if (aToken)
- {
- String serialNumberStr, userFriendlyNameStr;
- if (!includeEDID || getCompatDisplayEDID(aToken, serialNumberStr, userFriendlyNameStr))
- {
- // Set descriptor
- OVR::Win32::DisplayDesc& desc = descriptorArray[result++];
-
- // If not including EDID,
- if (!includeEDID)
- {
- // If DK2 id,
- if (wcsstr(ddm.DeviceID, L"OVR0003"))
- {
- userFriendlyNameStr = "Rift DK2";
- }
- else // Assume DK1 for now:
- {
- userFriendlyNameStr = "Rift DK1";
- }
- }
-
- desc.DeviceTypeGuess = deviceTypeGuess;
- desc.DisplayID = displayDeviceName;
- desc.ModelName = userFriendlyNameStr;
- desc.EdidSerialNumber = serialNumberStr;
- desc.LogicalResolutionInPixels = monitorResolution;
- desc.DesktopDisplayOffset = desktopOffset;
-
- // Hard-coded defaults in case the device doesn't have the data itself.
- // DK2 prototypes (0003) or DK HD Prototypes (0002)
- if (wcsstr(ddm.DeviceID, L"OVR0003") || wcsstr(ddm.DeviceID, L"OVR0002"))
- {
- desc.LogicalResolutionInPixels = Sizei(1920, 1080);
- desc.NativeResolutionInPixels = Sizei(1080, 1920);
- }
- else
- {
- desc.LogicalResolutionInPixels = monitorResolution;
- desc.NativeResolutionInPixels = monitorResolution;
- }
- }
- }
- }
-
- if (reportDiscovery)
- {
- // Only report once per run
- OVR_DEBUG_LOG_TEXT(("Display Found %s - %s\n",
- deviceId.ToCStr(), displayDeviceName.ToCStr()));
- reportDiscovery = false;
- }
-
- free(instanceBuffer);
-
- break;
- }
- }
- }
-
- return result;
-}
-
-
-//-------------------------------------------------------------------------------------
-// ***** Display
-
-bool Display::InCompatibilityMode( bool displaySearch )
-{
- bool result = false;
- if( displaySearch )
- {
- OVR::Win32::DisplayDesc displayArray[8];
-
- int extendedRiftCount = discoverExtendedRifts(displayArray, 8, false);
- if( extendedRiftCount )
- {
- result = true;
- }
- else
- {
- result = GlobalDisplayContext.CompatibilityMode;
- }
- }
- else
- {
- result = GlobalDisplayContext.CompatibilityMode;
- }
-
- return result;
-}
-
-#define OVR_FLAG_COMPATIBILITY_MODE 1
-#define OVR_FLAG_HIDE_DK1 2
-
-bool Display::Initialize()
-{
- HANDLE hDevice = INVALID_HANDLE_VALUE;
-
- if (GlobalDisplayContext.hDevice == 0)
- {
- hDevice = CreateFile( L"\\\\.\\ovr_video" ,
- GENERIC_READ | GENERIC_WRITE, NULL,
- NULL, OPEN_EXISTING, NULL, NULL);
- }
- else
- { // The device has already been created
- hDevice = GlobalDisplayContext.hDevice;
- }
-
- if (hDevice != NULL && hDevice != INVALID_HANDLE_VALUE)
- {
- GlobalDisplayContext.hDevice = hDevice;
- GlobalDisplayContext.CompatibilityMode = false;
-
- DWORD bytesReturned = 0;
- LONG compatiblityResult = OVR_STATUS_SUCCESS;
-
- BOOL result = DeviceIoControl( hDevice, IOCTL_RIFTMGR_GETCOMPATIBILITYMODE, NULL, 0,
- &compatiblityResult, sizeof( LONG ), &bytesReturned, NULL );
- if (result)
- {
- GlobalDisplayContext.CompatibilityMode = (compatiblityResult & OVR_FLAG_COMPATIBILITY_MODE) != 0;
- GlobalDisplayContext.HideDK1Mode = (compatiblityResult & OVR_FLAG_HIDE_DK1) != 0;
- }
- else
- {
- // If calling our driver fails in any way, assume compatibility mode as well
- GlobalDisplayContext.CompatibilityMode = true;
- }
-
- if (!GlobalDisplayContext.CompatibilityMode)
- {
- Ptr<DisplaySearchHandle> searchHandle = *Display::GetDisplaySearchHandle();
-
- // If a display is actually connected, bring up the shim layers so we can actually use it
- if (GetDisplayCount(searchHandle) > 0)
- {
- // FIXME: Initializing DX9 with landscape numbers rather than portrait
- GlobalDisplayContext.ExpectedWidth = 1080;
- GlobalDisplayContext.ExpectedHeight = 1920;
- }
- else
- {
- GlobalDisplayContext.CompatibilityMode = true;
- }
-
- }
- }
- else
- {
- GlobalDisplayContext.CompatibilityMode = true;
- }
-
- return true;
-}
-
-bool Display::GetDriverMode(bool& driverInstalled, bool& compatMode, bool& hideDK1Mode)
-{
- if (GlobalDisplayContext.hDevice == NULL)
- {
- driverInstalled = false;
- compatMode = true;
- hideDK1Mode = false;
- }
- else
- {
- driverInstalled = true;
- compatMode = GlobalDisplayContext.CompatibilityMode;
- hideDK1Mode = GlobalDisplayContext.HideDK1Mode;
- }
-
- return true;
-}
-
-bool Display::SetDriverMode(bool compatMode, bool hideDK1Mode)
-{
- // If device is not initialized,
- if (GlobalDisplayContext.hDevice == NULL)
- {
- OVR_ASSERT(false);
- return false;
- }
-
- // If no change,
- if ((compatMode == GlobalDisplayContext.CompatibilityMode) &&
- (hideDK1Mode == GlobalDisplayContext.HideDK1Mode))
- {
- return true;
- }
-
- LONG mode_flags = 0;
- if (compatMode)
- {
- mode_flags |= OVR_FLAG_COMPATIBILITY_MODE;
- }
- if (hideDK1Mode)
- {
- mode_flags |= OVR_FLAG_HIDE_DK1;
- }
-
- DWORD bytesReturned = 0;
- LONG err = 1;
-
- if (!DeviceIoControl(GlobalDisplayContext.hDevice,
- IOCTL_RIFTMGR_SETCOMPATIBILITYMODE,
- &mode_flags,
- sizeof(LONG),
- &err,
- sizeof(LONG),
- &bytesReturned,
- NULL) ||
- (err != 0 && err != -3))
- {
- LogError("{ERR-001w} [Win32Display] Unable to set device mode to (compat=%d dk1hide=%d): err=%d",
- (int)compatMode, (int)hideDK1Mode, (int)err);
- return false;
- }
-
- OVR_DEBUG_LOG(("[Win32Display] Set device mode to (compat=%d dk1hide=%d)",
- (int)compatMode, (int)hideDK1Mode));
-
- GlobalDisplayContext.HideDK1Mode = hideDK1Mode;
- GlobalDisplayContext.CompatibilityMode = compatMode;
- return true;
-}
-
-DisplaySearchHandle* Display::GetDisplaySearchHandle()
-{
- return new Win32::Win32DisplaySearchHandle();
-}
-
-// FIXME: The handle parameter will be used to unify GetDisplayCount and GetDisplay calls
-// The handle will be written to the 64-bit value pointed and will store the enumerated
-// display list. This will allow the indexes to be meaningful between obtaining
-// the count. With a single handle the count should be stable
-int Display::GetDisplayCount(DisplaySearchHandle* handle, bool extended, bool applicationOnly, bool extendedEDIDSerials)
-{
- static int extendedCount = -1;
- static int applicationCount = -1;
-
- Win32::Win32DisplaySearchHandle* localHandle = (Win32::Win32DisplaySearchHandle*)handle;
-
- if( localHandle == NULL )
- return 0;
-
- if( extendedCount == -1 || extended )
- {
- extendedCount = discoverExtendedRifts(localHandle->cachedDescriptorArray, 16, extendedEDIDSerials);
- }
-
- localHandle->extended = true;
- localHandle->extendedDisplayCount = extendedCount;
- int totalCount = extendedCount;
-
- if( applicationCount == -1 || applicationOnly )
- {
- applicationCount = getRiftCount(GlobalDisplayContext.hDevice);
- localHandle->application = true;
- }
-
- totalCount += applicationCount;
- localHandle->applicationDisplayCount = applicationCount;
- localHandle->displayCount = totalCount;
-
- return totalCount;
-}
-
-Ptr<Display> Display::GetDisplay(int index, DisplaySearchHandle* handle)
-{
- Ptr<Display> result;
-
- if( index < 0 )
- return result;
-
- Win32::Win32DisplaySearchHandle* localHandle = (Win32::Win32DisplaySearchHandle*)handle;
-
- if( localHandle == NULL )
- return NULL;
-
- if (localHandle->extended)
- {
- if (index >= 0 && index < (int)localHandle->extendedDisplayCount)
- {
- return *new Win32::Win32DisplayGeneric(localHandle->cachedDescriptorArray[index]);
- }
-
- index -= localHandle->extendedDisplayCount;
- }
-
- if(localHandle->application)
- {
- if (index >= 0 && index < (int)getRiftCount(GlobalDisplayContext.hDevice))
- {
- ULONG riftChildId = getRift(GlobalDisplayContext.hDevice, index);
- Win32::DisplayEDID dEdid;
-
- if (!getEdid(GlobalDisplayContext.hDevice, riftChildId, dEdid))
- {
- return NULL;
- }
-
- // FIXME: We have the EDID. Let's just use that instead.
- uint32_t nativeWidth = 1080, nativeHeight = 1920;
- uint32_t logicalWidth = 1920, logicalHeight = 1080;
- uint32_t rotation = 0;
-
- switch (dEdid.ModelNumber)
- {
- case 0:
- case 1:
- nativeWidth = 1280;
- nativeHeight = 800;
- logicalWidth = nativeWidth;
- logicalHeight = nativeHeight;
- break;
- case 2:
- case 3:
- default:
- rotation = 90;
- break;
- }
-
- HmdTypeEnum deviceTypeGuess = HmdType_Unknown;
- switch (dEdid.ModelNumber)
- {
- case 3: deviceTypeGuess = HmdType_DK2; break;
- case 2: deviceTypeGuess = HmdType_DKHDProto; break;
- case 1: deviceTypeGuess = HmdType_DK1; break;
- default: break;
- }
-
- result = *new Win32::Win32DisplayDriver(
- deviceTypeGuess,
- "",
- dEdid.MonitorName,
- dEdid.SerialNumber,
- Sizei(logicalWidth, logicalHeight),
- Sizei(nativeWidth, nativeHeight),
- Vector2i(0),
- dEdid,
- GlobalDisplayContext.hDevice,
- riftChildId,
- rotation);
- }
- }
- return result;
-}
-
-Display::MirrorMode Win32::Win32DisplayDriver::SetMirrorMode( Display::MirrorMode newMode )
-{
- return newMode;
-}
-
-static bool SetDisplayPower(HANDLE hDevice, ULONG childId, int mode)
-{
- ULONG_PTR longArray[2];
-
- longArray[0] = childId;
- longArray[1] = mode;
-
- ULONG localResult = 0;
- DWORD bytesReturned = 0;
-
- BOOL result = DeviceIoControl(hDevice,
- IOCTL_RIFTMGR_DISPLAYPOWER,
- longArray,
- 2 * sizeof(ULONG_PTR),
- &localResult,
- sizeof(ULONG),
- &bytesReturned,
- NULL);
-
- // Note: bytesReturned does not seem to be set
- return result != FALSE /* && bytesReturned == sizeof(ULONG) */ && mode == (int)localResult;
-}
-
-bool Win32::Win32DisplayDriver::SetDisplaySleep(bool sleep)
-{
- return SetDisplayPower(hDevice, ChildId, sleep ? 2 : 1);
-}
-
-
-} // namespace OVR
+/************************************************************************************ + +Filename : OVR_Win32_Display.cpp +Content : Win32 Display implementation +Created : May 6, 2014 +Authors : Dean Beeler + +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 "Kernel/OVR_Win32_IncludeWindows.h" + +#include "OVR_Win32_Display.h" +#include "OVR_Win32_Dxgi_Display.h" +#include "OVR_Win32_ShimFunctions.h" +#include "Util/Util_Direct3D.h" + +#include <stdio.h> +#include <tchar.h> +#include <string.h> +#include <stdlib.h> +#include <winioctl.h> +#include <SetupAPI.h> +#include <Mmsystem.h> +#include <conio.h> + +#include <setupapi.h> +#include <initguid.h> +#pragma comment(lib, "setupapi.lib") // SetupDiGetDeviceRegistryProperty et al +#pragma comment(lib, "dxgi.lib") // D3D11 adapter/output enumeration + +// WIN32_LEAN_AND_MEAN included in OVR_Atomic.h may break 'byte' declaration. +#ifdef WIN32_LEAN_AND_MEAN + typedef unsigned char byte; +#endif + +#include "Kernel/OVR_String.h" +#include "Kernel/OVR_Log.h" + +typedef struct +{ + HANDLE hDevice; + UINT ExpectedWidth; + UINT ExpectedHeight; + HWND hWindow; + bool CompatibilityMode; + bool HideDK1Mode; +} ContextStruct; + +static ContextStruct GlobalDisplayContext = {}; + + +//------------------------------------------------------------------------------------- +// ***** Display enumeration Helpers + +// THere are two ways to enumerate display: through our driver (DeviceIoControl) +// and through Win32 EnumDisplayMonitors (compatibility mode). + + +namespace OVR { + + +//----------------------------------------------------------------------------- +// Direct Mode + +ULONG getRiftCount( HANDLE hDevice ) +{ + ULONG riftCount = 0; + DWORD bytesReturned = 0; + + BOOL result = DeviceIoControl( hDevice, IOCTL_RIFTMGR_GET_RIFT_COUNT, nullptr, 0, + &riftCount, sizeof( ULONG ), &bytesReturned, nullptr ); + + if( result ) + return riftCount; + else + return 0; +} + +ULONG getRift( HANDLE hDevice, int index ) +{ + ULONG riftCount = getRiftCount( hDevice ); + DWORD bytesReturned = 0; + BOOL result; + + if( riftCount >= (ULONG)index ) + { + RIFT_STATUS riftStatus[16] = {0}; + + result = DeviceIoControl( hDevice, IOCTL_RIFTMGR_GET_RIFT_ARRAY, riftStatus, + riftCount * sizeof( RIFT_STATUS ), &riftCount, + sizeof( ULONG ), &bytesReturned, nullptr ); + if( result ) + { + PRIFT_STATUS tmpRift; + unsigned int i; + for( i = 0, tmpRift = riftStatus; i < riftCount; ++i, ++tmpRift ) + { + if( i == (unsigned int)index ) + return tmpRift->childUid; + } + } + } + + return 0; +} + +static bool getEdid(HANDLE hDevice, ULONG uid, DisplayEDID& edid) +{ + ULONG riftCount = 0; + DWORD bytesReturned = 0; + RIFT_STATUS riftStatus[16] = {0}; + + BOOL result = DeviceIoControl( hDevice, IOCTL_RIFTMGR_GET_RIFT_COUNT, nullptr, 0, + &riftCount, sizeof( ULONG ), &bytesReturned, nullptr ); + + if (!result) + { + return false; + } + + result = DeviceIoControl( hDevice, IOCTL_RIFTMGR_GET_RIFT_ARRAY, &riftStatus, + riftCount * sizeof( RIFT_STATUS ), &riftCount, sizeof(ULONG), + &bytesReturned, nullptr ); + if (!result) + { + return false; + } + + for (ULONG i = 0; i < riftCount; ++i) + { + ULONG riftUid = riftStatus[i].childUid; + if (riftUid == uid) + { + char edidBuffer[512]; + + result = DeviceIoControl(hDevice, IOCTL_RIFTMGR_GETEDID, &riftUid, sizeof(ULONG), + edidBuffer, 512, &bytesReturned, nullptr); + + if (result) + { + if (edid.Parse((unsigned char*)edidBuffer)) + { + return true; + } + else + { + LogError(("[Win32Display] WARNING: The driver was not able to return EDID for a display")); + } + } + + break; + } + } + + return false; +} + + +//----------------------------------------------------------------------------- +// Rift Monitors + +static bool GetMonitorEDID(const wchar_t* deviceID, DisplayEDID& edid) +{ + // Find second slash and split there + const wchar_t* pSlash1 = wcschr(deviceID, L'\\'); + if (!pSlash1) + return false; + + // And the second one + const wchar_t* pSlash2 = wcschr(pSlash1 + 1, L'\\'); + if (!pSlash2) + return false; + + wchar_t hardwareID[128] = {}; + wcsncpy_s(hardwareID, pSlash1 + 1, (pSlash2 - pSlash1 - 1)); + + wchar_t driverID[128] = {}; + wcscpy_s(driverID, pSlash2 + 1); + + ScopedHKEY displayKey; + if (RegOpenKey(HKEY_LOCAL_MACHINE, L"SYSTEM\\CurrentControlSet\\Enum\\DISPLAY", + &displayKey.GetRawRef()) == 0) + { + wchar_t keyName[MAX_PATH] = {}; + DWORD iKey = 0; + while (RegEnumKey(displayKey.Get(), iKey++, keyName, _countof(keyName)) == 0) + { + if (_wcsicmp(keyName, hardwareID) == 0) + { + ScopedHKEY devKey; + if (RegOpenKey(displayKey.Get(), keyName, &devKey.GetRawRef()) == 0) + { + wchar_t nodeName[MAX_PATH] = {}; + DWORD iNode = 0; + while (RegEnumKey(devKey.Get(), iNode++, nodeName, _countof(nodeName)) == 0) + { + wchar_t devDriverID[MAX_PATH] = {}; + DWORD cbDriverID = sizeof(devDriverID); + if (RegGetValue(devKey.Get(), nodeName, L"Driver", RRF_RT_REG_SZ, nullptr, + devDriverID, &cbDriverID) == 0) + { + // Check if DriverID matches + if (_wcsicmp(devDriverID, driverID) == 0) + { + ScopedHKEY nodeKey; + if (RegOpenKey(devKey.Get(), nodeName, &nodeKey.GetRawRef()) == 0) + { + BYTE edidBytes[512] = {}; + DWORD cbEDID = sizeof(edidBytes); + if (RegGetValue(nodeKey.Get(), L"Device Parameters", L"EDID", RRF_RT_REG_BINARY, + nullptr, edidBytes, &cbEDID) == 0) + { + return edid.Parse(edidBytes); + } + } + } + } + } + } + } + } + } + + return true; +} + +static bool FillDisplayDesc(DXGI_OUTPUT_DESC const& outputDesc, DISPLAY_DEVICEW& dispDev, DisplayEDID const& edid, DisplayDesc& result) +{ + // EdidSerialNumber + static_assert(sizeof(result.EdidSerialNumber) == sizeof(edid.SerialNumber), "sizes need to match"); + memcpy(result.EdidSerialNumber, edid.SerialNumber, sizeof(result.EdidSerialNumber)); + + // DisplayID + size_t converted = 0; + if (0 != wcstombs_s(&converted, result.DisplayID, dispDev.DeviceName, _TRUNCATE)) + return false; + + // DesktopDisplayOffset + result.DesktopDisplayOffset = Vector2i(outputDesc.DesktopCoordinates.left, + outputDesc.DesktopCoordinates.top); + + // ModelName + static_assert(sizeof(result.ModelName) == sizeof(edid.MonitorName), "sizes need to match"); + memcpy(result.ModelName, edid.MonitorName, sizeof(result.ModelName)); + + // Resolution + result.ResolutionInPixels.w = edid.Width; + result.ResolutionInPixels.h = edid.Height; + + // DK2 Landscape = DXGI_MODE_ROTATION_IDENTITY -> DXGI_MODE_ROTATION_ROTATE90 + // DK2 Portrait = DXGI_MODE_ROTATION_ROTATE90 -> DXGI_MODE_ROTATION_IDENTITY + // DK2 Landscape(Flipped) = DXGI_MODE_ROTATION_ROTATE180 -> DXGI_MODE_ROTATION_ROTATE270 + // DK2 Portrait(Flipped) = DXGI_MODE_ROTATION_ROTATE270 -> DXGI_MODE_ROTATION_ROTATE180 + bool tallScreen = result.ResolutionInPixels.w < result.ResolutionInPixels.h; + + // Rotation + // This is the rotation from portrait mode, and the DK2 (for example) is naturally landscape, + // so we need to adjust the rotation value based on the HMD type. + switch (outputDesc.Rotation) + { + case DXGI_MODE_ROTATION_IDENTITY: + default: + result.Rotation = tallScreen ? 270 : 0; + break; + case DXGI_MODE_ROTATION_ROTATE90: + result.Rotation = tallScreen ? 0 : 90; + break; + case DXGI_MODE_ROTATION_ROTATE180: + result.Rotation = tallScreen ? 90 : 180; + break; + case DXGI_MODE_ROTATION_ROTATE270: + result.Rotation = tallScreen ? 180 : 270; + break; + } + + // DeviceTypeGuess + result.DeviceTypeGuess = HmdTypeFromModelNumber(edid.ModelNumber); + + return true; +} + +// Pass in 0 if only an existence check is requested. +static int DiscoverRiftMonitors(DisplayDesc* descriptorArray = nullptr, int inputArraySize = 0) +{ + int outputArraySize = 0; + + Ptr<IDXGIFactory1> factory; + HRESULT hr = ::CreateDXGIFactory1(IID_PPV_ARGS(&factory.GetRawRef())); + OVR_D3D_CHECK_RET_FALSE(hr); + + Ptr<IDXGIAdapter> adapter; + uint32_t iAdapter = 0; + while (adapter = nullptr, + SUCCEEDED(factory->EnumAdapters(iAdapter++, &adapter.GetRawRef()))) + { + Ptr<IDXGIAdapter1> adapter1; + hr = adapter->QueryInterface(IID_PPV_ARGS(&adapter1.GetRawRef())); + if (!OVR_D3D_CHECK(hr)) + continue; + + DXGI_ADAPTER_DESC1 adapterDesc = {}; + adapter1->GetDesc1(&adapterDesc); + + if (adapterDesc.Flags & DXGI_ADAPTER_FLAG_SOFTWARE) + { + // Don't bother with software adapters (WARP, BasicRender, etc...) + continue; + } + + Ptr<IDXGIOutput> output; + uint32_t iOutput = 0; + while (output = nullptr, + SUCCEEDED(adapter->EnumOutputs(iOutput++, &output.GetRawRef()))) + { + // Get DeviceName for each output + DXGI_OUTPUT_DESC outputDesc; + hr = output->GetDesc(&outputDesc); + if (!OVR_D3D_CHECK(hr)) + continue; + + // Get DeviceID from DeviceName + DISPLAY_DEVICEW dispDev = {}; + dispDev.cb = sizeof(dispDev); + if (!::EnumDisplayDevicesW(outputDesc.DeviceName, 0, &dispDev, 0)) + continue; + + // If it is a Rift monitor, + if (wcsstr(dispDev.DeviceID, L"RTD2205") || + wcsstr(dispDev.DeviceID, L"CVT0003") || + wcsstr(dispDev.DeviceID, L"MST0030") || + wcsstr(dispDev.DeviceID, L"OVR00")) // Part of Oculus EDID. + { + DisplayEDID edid = {}; + + // If monitor existence is the goal, + if (!descriptorArray || inputArraySize <= 0) + return 1; + + if (outputArraySize < inputArraySize) + { + if (GetMonitorEDID(dispDev.DeviceID, edid) && + FillDisplayDesc(outputDesc, dispDev, edid, descriptorArray[outputArraySize])) + { + outputArraySize++; + } + } + } + } + } + + return outputArraySize; +} + +bool Display::ExtendedModeDevicesExist() +{ + return DiscoverRiftMonitors() > 0; +} + + +//------------------------------------------------------------------------------------- +// ***** Display + +bool Display::InCompatibilityMode(bool displaySearch) +{ + return (displaySearch && ExtendedModeDevicesExist()) || + GlobalDisplayContext.CompatibilityMode; +} + +#define OVR_FLAG_COMPATIBILITY_MODE 1 +#define OVR_FLAG_HIDE_DK1 2 + + +void Display::Shutdown() +{ + Win32::DisplayShim::GetInstance().Shutdown(); + OVR::Display::SetDirectDisplayInitialized(false); +} + +bool Display::Initialize() +{ + // This function is re-entrant because it may be called again to + // patch-up comatibility mode by the Config Util (Hack in HMDView::SetupGraphics). + // For this reason, the GetDirectDisplayInitialized() check is only done for + // shim initialization below. + + HANDLE hDevice = INVALID_HANDLE_VALUE; + + if (GlobalDisplayContext.hDevice == 0) + { + hDevice = CreateFile(L"\\\\.\\ovr_video" , + GENERIC_READ | GENERIC_WRITE, 0, + nullptr, OPEN_EXISTING, 0, nullptr); + } + else + { // The device has already been created + hDevice = GlobalDisplayContext.hDevice; + } + + if (hDevice != nullptr && hDevice != INVALID_HANDLE_VALUE) + { + GlobalDisplayContext.hDevice = hDevice; + GlobalDisplayContext.CompatibilityMode = false; + + DWORD bytesReturned = 0; + LONG compatiblityResult = OVR_STATUS_SUCCESS; + + BOOL result = DeviceIoControl( hDevice, IOCTL_RIFTMGR_GETCOMPATIBILITYMODE, nullptr, 0, + &compatiblityResult, sizeof( LONG ), &bytesReturned, nullptr ); + if (result) + { + GlobalDisplayContext.CompatibilityMode = (compatiblityResult & OVR_FLAG_COMPATIBILITY_MODE) != 0; + GlobalDisplayContext.HideDK1Mode = (compatiblityResult & OVR_FLAG_HIDE_DK1) != 0; + } + else + { + // If calling our driver fails in any way, assume compatibility mode as well + GlobalDisplayContext.CompatibilityMode = true; + } + + if (!GlobalDisplayContext.CompatibilityMode) + { + // If a display is actually connected, bring up the shim layers so we can actually use it + // TBD: Do we care about extended mode displays? -cat + if (OVR::Display::ExtendedModeDevicesExist() || + getRiftCount(GlobalDisplayContext.hDevice) > 0) + { + // FIXME: Initializing DX9 with landscape numbers rather than portrait + GlobalDisplayContext.ExpectedWidth = 1080; + GlobalDisplayContext.ExpectedHeight = 1920; + } + else + { + GlobalDisplayContext.CompatibilityMode = true; + } + } + } + else + { + GlobalDisplayContext.CompatibilityMode = true; + } + + + + // Set up display code for Windows + Win32::DisplayShim::GetInstance(); + + // This code will look for the first display. If it's a display + // that's extending the desktop, the code will assume we're in + // compatibility mode. Compatibility mode prevents shim loading + // and renders only to extended Rifts. + // If we find a display and it's application exclusive, + // we load the shim so we can render to it. + // If no display is available, we revert to whatever the + // driver tells us we're in + + bool anyExtendedRifts = OVR::Display::ExtendedModeDevicesExist() || + GlobalDisplayContext.CompatibilityMode; + + if (!OVR::Display::GetDirectDisplayInitialized()) + { + OVR::Display::SetDirectDisplayInitialized( + Win32::DisplayShim::GetInstance().Initialize(anyExtendedRifts)); + } + + return true; +} + +bool Display::GetDriverMode(bool& driverInstalled, bool& compatMode, bool& hideDK1Mode) +{ + if (GlobalDisplayContext.hDevice == nullptr) + { + driverInstalled = false; + compatMode = true; + hideDK1Mode = false; + } + else + { + driverInstalled = true; + compatMode = GlobalDisplayContext.CompatibilityMode; + hideDK1Mode = GlobalDisplayContext.HideDK1Mode; + } + + return true; +} + +bool Display::SetDriverMode(bool compatMode, bool hideDK1Mode) +{ + // If device is not initialized, + if (GlobalDisplayContext.hDevice == nullptr) + { + OVR_ASSERT(false); + return false; + } + + // If no change, + if ((compatMode == GlobalDisplayContext.CompatibilityMode) && + (hideDK1Mode == GlobalDisplayContext.HideDK1Mode)) + { + return true; + } + + LONG mode_flags = 0; + if (compatMode) + { + mode_flags |= OVR_FLAG_COMPATIBILITY_MODE; + } + if (hideDK1Mode) + { + mode_flags |= OVR_FLAG_HIDE_DK1; + } + + DWORD bytesReturned = 0; + LONG err = 1; + + if (!DeviceIoControl(GlobalDisplayContext.hDevice, + IOCTL_RIFTMGR_SETCOMPATIBILITYMODE, + &mode_flags, + sizeof(LONG), + &err, + sizeof(LONG), + &bytesReturned, + nullptr) || + (err != 0 && err != -3)) + { + LogError("{ERR-001w} [Win32Display] Unable to set device mode to (compat=%d dk1hide=%d): err=%d", + (int)compatMode, (int)hideDK1Mode, (int)err); + return false; + } + + OVR_DEBUG_LOG(("[Win32Display] Set device mode to (compat=%d dk1hide=%d)", + (int)compatMode, (int)hideDK1Mode)); + + GlobalDisplayContext.HideDK1Mode = hideDK1Mode; + GlobalDisplayContext.CompatibilityMode = compatMode; + return true; +} + +DisplaySearchHandle* Display::GetDisplaySearchHandle() +{ + return new Win32::Win32DisplaySearchHandle(); +} + +// FIXME: The handle parameter will be used to unify GetDisplayCount and GetDisplay calls +// The handle will be written to the 64-bit value pointed and will store the enumerated +// display list. This will allow the indexes to be meaningful between obtaining +// the count. With a single handle the count should be stable +int Display::GetDisplayCount(DisplaySearchHandle* handle, bool extended, bool applicationOnly, bool extendedEDIDSerials) +{ + OVR_UNUSED(extendedEDIDSerials); + static int extendedCount = -1; + static int applicationCount = -1; + + Win32::Win32DisplaySearchHandle* localHandle = (Win32::Win32DisplaySearchHandle*)handle; + + if( localHandle == nullptr ) + return 0; + + if( extendedCount == -1 || extended ) + { + extendedCount = DiscoverRiftMonitors(localHandle->cachedDescriptorArray, 16); + } + + localHandle->extended = true; + localHandle->extendedDisplayCount = extendedCount; + int totalCount = extendedCount; + + if( applicationCount == -1 || applicationOnly ) + { + applicationCount = getRiftCount(GlobalDisplayContext.hDevice); + localHandle->application = true; + } + + totalCount += applicationCount; + localHandle->applicationDisplayCount = applicationCount; + localHandle->displayCount = totalCount; + + return totalCount; +} + +Ptr<Display> Display::GetDisplay(int index, DisplaySearchHandle* handle) +{ + Ptr<Display> result; + + if( index < 0 ) + return result; + + Win32::Win32DisplaySearchHandle* localHandle = (Win32::Win32DisplaySearchHandle*)handle; + + if( localHandle == nullptr ) + return nullptr; + + if (localHandle->extended) + { + if (index >= 0 && index < (int)localHandle->extendedDisplayCount) + { + return *new Win32::Win32DisplayGeneric(localHandle->cachedDescriptorArray[index]); + } + + index -= localHandle->extendedDisplayCount; + } + + if(localHandle->application) + { + if (index >= 0 && index < (int)getRiftCount(GlobalDisplayContext.hDevice)) + { + ULONG riftChildId = getRift(GlobalDisplayContext.hDevice, index); + DisplayEDID dEdid; + + if (!getEdid(GlobalDisplayContext.hDevice, riftChildId, dEdid)) + { + return nullptr; + } + + uint32_t nativeWidth = dEdid.Width, nativeHeight = dEdid.Height; + uint32_t rotation = (dEdid.ModelNumber == 2 || + dEdid.ModelNumber == 3) ? 90 : 0; + + uint32_t logicalWidth, logicalHeight; + if (rotation == 0) + { + logicalWidth = nativeWidth; + logicalHeight = nativeHeight; + } + else + { + logicalWidth = nativeHeight; + logicalHeight = nativeWidth; + } + + result = *new Win32::Win32DisplayDriver( + HmdTypeFromModelNumber(dEdid.ModelNumber), + "", + dEdid.MonitorName, + dEdid.SerialNumber, + Sizei(logicalWidth, logicalHeight), + Sizei(nativeWidth, nativeHeight), + Vector2i(0), + dEdid, + GlobalDisplayContext.hDevice, + riftChildId, + rotation); + } + } + return result; +} + +Display::MirrorMode Win32::Win32DisplayDriver::SetMirrorMode( Display::MirrorMode newMode ) +{ + return newMode; +} + +static bool SetDisplayPower(HANDLE hDevice, ULONG childId, int mode) +{ +#ifdef OVR_OS_WIN64 + BOOL is64BitOS = TRUE; +#else + BOOL is64BitOS = FALSE; + BOOL res = IsWow64Process(GetCurrentProcess(), &is64BitOS); + OVR_ASSERT_AND_UNUSED(res == TRUE,res); +#endif + ULONG localResult = 0; + DWORD bytesReturned = 0; + BOOL result; + if (is64BitOS) + { + ULONG64 longArray[2]; + longArray[0] = childId; + longArray[1] = mode; + result = DeviceIoControl(hDevice, + IOCTL_RIFTMGR_DISPLAYPOWER, + longArray, + 2 * sizeof(ULONG64), + &localResult, + sizeof(ULONG), + &bytesReturned, + nullptr); + } + else + { + ULONG32 longArray[2]; + longArray[0] = childId; + longArray[1] = mode; + result = DeviceIoControl(hDevice, + IOCTL_RIFTMGR_DISPLAYPOWER, + longArray, + 2 * sizeof(ULONG32), + &localResult, + sizeof(ULONG), + &bytesReturned, + nullptr); + } + + // Note: bytesReturned does not seem to be set + return result != FALSE /* && bytesReturned == sizeof(ULONG) */ && mode == (int)localResult; +} + +bool Win32::Win32DisplayDriver::SetDisplaySleep(bool sleep) +{ + return SetDisplayPower(hDevice, ChildId, sleep ? 2 : 1); +} + + +} // namespace OVR diff --git a/LibOVR/Src/Displays/OVR_Win32_Display.h b/LibOVR/Src/Displays/OVR_Win32_Display.h index 4517d44..5b30482 100644 --- a/LibOVR/Src/Displays/OVR_Win32_Display.h +++ b/LibOVR/Src/Displays/OVR_Win32_Display.h @@ -1,182 +1,153 @@ -/************************************************************************************
-
-Filename : OVR_Win32_Display.h
-Content : Win32-specific Display declarations
-Created : May 6, 2014
-Authors : Dean Beeler
-
-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.
-
-*************************************************************************************/
-
-#ifndef OVR_Win32_Display_h
-#define OVR_Win32_Display_h
-
-#include "OVR_Display.h"
-
-OVR_DISABLE_MSVC_WARNING(4351) // new behavior: elements of array will be default initialized
-
-namespace OVR { namespace Win32 {
-
-
-//-------------------------------------------------------------------------------------
-// DisplayDesc
-
-// Display information enumerable through Win32.
-// TBD: Should we just move this to public header, so it's a const member of Display?
-struct DisplayDesc
-{
- HmdTypeEnum DeviceTypeGuess; // This is a guess about what type of HMD it is connected to
- String DisplayID; // This is the device identifier string from MONITORINFO (for app usage)
- String ModelName; // This is a "DK2" type string
- String EdidSerialNumber;
- Sizei LogicalResolutionInPixels;
- Sizei NativeResolutionInPixels;
- Vector2i DesktopDisplayOffset;
-};
-
-
-//-------------------------------------------------------------------------------------
-// DisplayEDID
-
-// Describes EDID information as reported from our display driver.
-struct DisplayEDID
-{
- String MonitorName;
- UINT16 ModelNumber;
- String VendorName;
- String SerialNumber;
-};
-
-class Win32DisplaySearchHandle : public DisplaySearchHandle
-{
-public:
- static const int ArraySize = 16;
-
- Win32::DisplayDesc cachedDescriptorArray[ArraySize];
- bool extended;
- bool application;
- int extendedDisplayCount;
- int applicationDisplayCount;
- int displayCount;
-
- Win32DisplaySearchHandle()
- : cachedDescriptorArray(),
- extended(),
- application(false),
- extendedDisplayCount(0),
- applicationDisplayCount(0),
- displayCount(0)
- {
- }
-
- virtual ~Win32DisplaySearchHandle()
- {
- }
-};
-
-//-------------------------------------------------------------------------------------
-// Win32DisplayGeneric
-
-// Describes Win32 display in Compatibility mode, containing basic data
-class Win32DisplayGeneric : public Display
-{
-public:
- Win32DisplayGeneric( const DisplayDesc& dd ) :
- Display(dd.DeviceTypeGuess,
- dd.DisplayID,
- dd.ModelName,
- dd.EdidSerialNumber,
- dd.LogicalResolutionInPixels,
- dd.NativeResolutionInPixels,
- dd.DesktopDisplayOffset,
- 0,
- 0,
- false)
- {
- }
-
- virtual ~Win32DisplayGeneric()
- {
- }
-
- // Generic displays are not capable of mirroring
- virtual MirrorMode SetMirrorMode( MirrorMode newMode )
- {
- OVR_UNUSED( newMode );
- return MirrorDisabled;
- }
-};
-
-
-//-------------------------------------------------------------------------------------
-// Win32DisplayDriver
-
-// Oculus driver based display object.
-class Win32DisplayDriver : public Display
-{
- HANDLE hDevice;
- ULONG ChildId;
- DisplayEDID Edid;
-
-public:
- Win32DisplayDriver(const HmdTypeEnum deviceTypeGuess,
- const String& displayID,
- const String& modelName,
- const String& edidSerial,
- const Sizei& logicalRes,
- const Sizei& nativeRes,
- const Vector2i& displayOffset,
- const DisplayEDID& edid,
- HANDLE hdevice,
- ULONG child,
- uint32_t rotation) :
- Display(deviceTypeGuess,
- displayID,
- modelName,
- edidSerial,
- logicalRes,
- nativeRes,
- displayOffset,
- child,
- rotation,
- true),
- hDevice(hdevice),
- ChildId(child),
- Edid(edid)
- {
- }
-
- virtual ~Win32DisplayDriver()
- {
- }
-
- virtual MirrorMode SetMirrorMode( MirrorMode newMode );
-
- // Support sleep/wake
- virtual bool SetDisplaySleep(bool off);
-};
-
-
-}} // namespace OVR::Win32
-
-
-OVR_RESTORE_MSVC_WARNING()
-
-
-#endif // OVR_Win32_Display_h
+/************************************************************************************ + +Filename : OVR_Win32_Display.h +Content : Win32-specific Display declarations +Created : May 6, 2014 +Authors : Dean Beeler + +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. + +*************************************************************************************/ + +#ifndef OVR_Win32_Display_h +#define OVR_Win32_Display_h + +#include "OVR_Display.h" + +OVR_DISABLE_MSVC_WARNING(4351) // new behavior: elements of array will be default initialized + +namespace OVR { namespace Win32 { + + +class Win32DisplaySearchHandle : public DisplaySearchHandle +{ +public: + static const int ArraySize = 16; + + DisplayDesc cachedDescriptorArray[ArraySize]; + bool extended; + bool application; + int extendedDisplayCount; + int applicationDisplayCount; + int displayCount; + + Win32DisplaySearchHandle() : + cachedDescriptorArray(), + extended(), + application(false), + extendedDisplayCount(0), + applicationDisplayCount(0), + displayCount(0) + { + } + + virtual ~Win32DisplaySearchHandle() + { + } +}; + +//------------------------------------------------------------------------------------- +// Win32DisplayGeneric + +// Describes Win32 display in Compatibility mode, containing basic data +class Win32DisplayGeneric : public Display +{ +public: + Win32DisplayGeneric( const DisplayDesc& dd ) : + Display(dd.DeviceTypeGuess, + dd.DisplayID, + dd.ModelName, + dd.EdidSerialNumber, + dd.ResolutionInPixels, + dd.ResolutionInPixels, + dd.DesktopDisplayOffset, + 0, + dd.Rotation, + false) + { + } + + virtual ~Win32DisplayGeneric() + { + } + + // Generic displays are not capable of mirroring + virtual MirrorMode SetMirrorMode( MirrorMode newMode ) + { + OVR_UNUSED( newMode ); + return MirrorDisabled; + } +}; + + +//------------------------------------------------------------------------------------- +// Win32DisplayDriver + +// Oculus driver based display object. +class Win32DisplayDriver : public Display +{ + HANDLE hDevice; + ULONG ChildId; + DisplayEDID Edid; + +public: + Win32DisplayDriver(const HmdTypeEnum deviceTypeGuess, + const String& displayID, + const String& modelName, + const String& edidSerial, + const Sizei& logicalRes, + const Sizei& nativeRes, + const Vector2i& displayOffset, + const DisplayEDID& edid, + HANDLE hdevice, + ULONG child, + uint32_t rotation) : + Display(deviceTypeGuess, + displayID, + modelName, + edidSerial, + logicalRes, + nativeRes, + displayOffset, + child, + rotation, + true), + hDevice(hdevice), + ChildId(child), + Edid(edid) + { + } + + virtual ~Win32DisplayDriver() + { + } + + virtual MirrorMode SetMirrorMode( MirrorMode newMode ); + + // Support sleep/wake + virtual bool SetDisplaySleep(bool off); +}; + + +}} // namespace OVR::Win32 + + +OVR_RESTORE_MSVC_WARNING() + + +#endif // OVR_Win32_Display_h diff --git a/LibOVR/Src/Displays/OVR_Win32_Dxgi_Display.h b/LibOVR/Src/Displays/OVR_Win32_Dxgi_Display.h index e801f4a..4ae110b 100644 --- a/LibOVR/Src/Displays/OVR_Win32_Dxgi_Display.h +++ b/LibOVR/Src/Displays/OVR_Win32_Dxgi_Display.h @@ -1,429 +1,444 @@ -/************************************************************************************
-
-PublicHeader: None
-Filename : dxgi_ovr_filter.h
-Content : Shared usermode/kernel mode definitions for IOCTL functionality.
- Also used from LibOVR to access the driver.
-Created : January 27, 2014
-Authors : Dean Beeler
-
-Copyright : Copyright 2013 Oculus, LLC. All Rights reserved.
-
-Use of this software is subject to the terms of the Oculus LLC license
-agreement provided at the time of installation or download, or which
-otherwise accompanies this software in either electronic or hard copy form.
-
-/************************************************************************************/
-
-#ifndef OVR_dxgi_ovr_filter_h
-#define OVR_dxgi_ovr_filter_h
-
-#include <Windows.h>
-#include <InitGuid.h>
-
-#define USERMODE_TEST_ROTATION 1
-
-#if USERMODE_TEST_ROTATION // Used only by the um test application
-#define USERMODE_SURFACE_WIDTH 1920
-#define USERMODE_SURFACE_HEIGHT 1080
-#else
-#define USERMODE_SURFACE_WIDTH 1080
-#define USERMODE_SURFACE_HEIGHT 1920
-#endif
-
-#define TEST_ROTATION 0 // Kernel-mode parameters
-
-#if TEST_ROTATION
-#define SURFACE_WIDTH 1920
-#define SURFACE_HEIGHT 1080
-#else
-#define SURFACE_WIDTH 1080
-#define SURFACE_HEIGHT 1920
-#endif
-
-// {46231713-49FD-4922-84E3-9FF907C06803}
-DEFINE_GUID(GUID_DEVINTERFACE_OVRRIFTMGR,
- 0x46231713, 0x49fd, 0x4922, 0x84, 0xe3, 0x9f, 0xf9, 0x7, 0xc0, 0x68, 0x3);
-
-#define QUERYADAPTER_MAGICSIZE 17836
-#define QUERYADAPTER_MAGICHEADER 0x4f565246 // OVRF
-#define QUERYADAPTER_MAXPATH 2048
-
-#define FUNCTION_INDEX 0xb800
-
-#pragma pack(push,1)
-
-#define OVR_RIFT_MODE_OFF 0 // Disabled
-#define OVR_RIFT_MODE_ENABLED 1 // Enabled
-#define OVR_RIFT_MODE_EXTEND 2 // Extending 2D displays. Without this flag
- // 2D displays are disabled when the Rift
- // is active
-#define OVR_RIFT_MODE_FRONTBUFFER 4 // Enable front buffer only for Rift
-#define OVR_RIFT_MODE_LOCKMOUSE 8 // Prevent mouse from entering bounds
-
-#define OVR_ESCAPE_TYPE_HANDLE 1 // Escape to notify driver of our collected handles
-
-#define OVR_FlipImmediate 0x2
-#define OVR_FlipOnNextVSync 0x4
-
-//-----------------------------------------------------------------------------------
-// Structures for application to UM driver
-
-// Kernel32.dll functionality
-typedef HMODULE (WINAPI *WinLoadLibraryA) ( LPCSTR );
-typedef HMODULE (WINAPI *WinLoadLibraryW) ( LPCWSTR );
-typedef HMODULE (WINAPI *WinLoadLibraryExA) ( LPCSTR, HANDLE, DWORD );
-typedef HMODULE (WINAPI *WinLoadLibraryExW) ( LPCWSTR, HANDLE, DWORD );
-typedef BOOL (WINAPI *WinGetModuleHandleExA)( DWORD, LPCSTR, HMODULE* );
-typedef BOOL (WINAPI *WinGetModuleHandleExW)( DWORD, LPCWSTR, HMODULE* );
-
-// Overridden DirectX 9 entry points
-typedef void* (WINAPI *WinDirect3DCreate9)(UINT SDKVersion);
-typedef HRESULT (WINAPI *WinDirect3DCreate9Ex)(UINT SDKVersion, void** aDevice);
-
-// Overridden DXGI entry points
-typedef HRESULT (WINAPI *WinCreateDXGIFactory)(
- __in REFIID riid,
- __out void **ppFactory
- );
-
-typedef HRESULT (WINAPI *WinCreateDXGIFactory1)(
- __in REFIID riid,
- __out void **ppFactory
- );
-
-typedef HRESULT (WINAPI *WinCreateDXGIFactory2)(
- __in UINT flags,
- __in const IID &riid,
- __out void **ppFactory
- );
-
-// Application usermode callbacks from usermode driver. These
-// functions are all provided by the calling application that uses
-// the filter mode driver
-
-// IsInitializingDisplay is used at runtime to validate that
-// the created resource (RT or bind_present) matches the resolution
-// of our expected backbuffer. If the application returns true,
-// our usermode driver will convert this to a primary
-typedef BOOL (WINAPI *IsInitializingDisplay) ( PVOID, UINT, UINT );
-// RiftForContext is a function that will return the Rift device of
-// the concerned context. This is for targeting a particular
-// device instance with a particular Rift for rendering
-typedef ULONG (WINAPI *RiftForContext)( PVOID, HANDLE );
-// CloseRiftForContext is a function that informs the application
-// the created device is shutting down and the context
-// can freedly disassociate with the particular
-typedef BOOL (WINAPI *CloseRiftForContext)( PVOID, HANDLE, ULONG );
-typedef BOOL (WINAPI *WindowDisplayResolution)( PVOID, UINT*, UINT*, UINT*, UINT*, BOOL* );
-// IsCreatingBackBuffer is a function directed at the runtime shim
-// to confirm that the runtime is actively creating the additional
-// swapchain for rotation and display out to the rift.
-// When creating the original swapchain this function should return false
-// so the orignal swapchain isn't inadvertantly coopted.
-typedef BOOL (WINAPI *IsCreatingBackBuffer)( PVOID );
-// Callback from the usermode driver to obtain the desire to see debug statements from
-// the usermode drivers on the output console. Only called one per usermode driver shim
-// and usermode runtime.
-typedef BOOL (WINAPI *ShouldEnableDebug)( VOID );
-// Callback from the usermode driver to the runtime obtain the vsync status
-typedef BOOL (WINAPI *ShouldVSync)( VOID );
-// Callback from usermode mode and runtime driver to obtain expected native width,
-// height and degrees rotation of the rift
-typedef BOOL (WINAPI *ExpectedResolution)( PVOID, UINT*, UINT*, UINT* );
-// Usermode callback that reports whether or not mirroring is enabled
-typedef BOOL (WINAPI *MirroringEnabled)( PVOID );
-// Callback from the shim for Unity and other plugins used to
-// report the swapchain that was created by the application
-typedef void* (WINAPI *GetDX11SwapChain)( PVOID );
-// Callback to report the HWND associated with this context
-typedef HWND (WINAPI* GetWindowForContext)( PVOID );
-// Should present Rift on context
-typedef BOOL (WINAPI* PresentRiftOnContext)( PVOID );
-// Used by a pre-loaded shim (d3d9, dxgi, opengl32) to
-// identify which api version we loaded
-// 1 = OpenGL
-// 9 = DirectX 9
-// 10 = DirectX 1X
-typedef int (WINAPI* ActiveAPIVersion)( PVOID );
-
-// Get the version of the runtime filter.
-typedef ULONG (WINAPI* GetRTFilterVersion)();
-
-#pragma warning(push)
-#pragma warning(disable: 4201)
-
-typedef struct _LINK_APPLICATION_DRIVER
-{
- UINT32 version;
- PVOID context;
-
- union
- {
- struct
- {
- IsInitializingDisplay pfnInitializingDisplay;
- RiftForContext pfnRiftForContext;
- CloseRiftForContext pfnCloseRiftForContext;
- WindowDisplayResolution pfnWindowDisplayResolution;
- IsCreatingBackBuffer pfnIsCreatingBackBuffer;
- ShouldEnableDebug pfnShouldEnableDebug;
- ShouldVSync pfnShouldVSync;
- ExpectedResolution pfnExpectedResolution;
- MirroringEnabled pfnMirroringEnabled;
- GetDX11SwapChain pfnGetDX11SwapChain;
- GetWindowForContext pfnGetWindowForContext;
- PresentRiftOnContext pfnPresentRiftOnContext;
- ActiveAPIVersion pfnActiveAPIVersion;
- };
-
- PROC placeholders[128];
- };
-
-
- // Used by Runtime filter for linking with original libraries
- WinDirect3DCreate9 pfnDirect3DCreate9;
- WinDirect3DCreate9Ex pfnDirect3DCreate9Ex;
- WinCreateDXGIFactory pfnCreateDXGIFactory;
- WinCreateDXGIFactory1 pfnCreateDXGIFactory1;
- WinCreateDXGIFactory2 pfnCreateDXGIFactory2;
-} LINK_APPLICATION_DRIVER, *PLINK_APPLICATION_DRIVER;
-
-#pragma warning(pop)
-
-
-// OVRDisplay.dll functionality
-typedef HRESULT (WINAPI *PreloadLibraryFn) ( WinLoadLibraryA , LPCSTR, PLINK_APPLICATION_DRIVER appDriver );
-typedef HRESULT (WINAPI *PreloadLibraryRTFn) ( PLINK_APPLICATION_DRIVER appDriver );
-
-//-----------------------------------------------------------------------------------
-// Structures for UM driver to KM driver
-
-typedef struct _QUERY_KM_DRIVER
-{
- UINT32 magic; // Friend or foe identifier for our filter driver
- // See: QUERYADAPTER_MAGICHEADER
- UINT32 maxVidPnSources; // Returns the maximum number of video present network sources
-} QUERY_KM_DRIVER, *PQUERY_KM_DRIVER;
-
-#ifndef _D3DUKMDT_H_
-typedef UINT D3DKMT_HANDLE;
-#endif
-
-typedef struct _HandleNotepad
-{
- // These are assigned around CreateResource
- HANDLE hUsermodeInResource;
- HANDLE hUsermodeOutResource;
-
- // These are assigned within the kernel with
- // DxgkDdiCreateAllocation and
- // DxgkDdiOpenAllocation
- D3DKMT_HANDLE hAllocation;
- PVOID hDeviceSpecificHandle;
- PVOID hKernelDriverHandle;
-
- // These are assigned around pfnAllocateCb
- HANDLE hUsermodeSharedResource;
- D3DKMT_HANDLE hKernelModeSharedResource;
-
- ULONG childUid;
-
- UINT pitch;
-
-} HandleNotepad, *PHandleNotepad;
-
-
-typedef struct _ALLOC_PRIVATE_STRUCTURE
-{
- UINT32 magic; // Friend or foe identifier for our filter driver
-
- PVOID originalPrivataDataPtr; // Location in usermode of the original private data structure
- UINT originalPrivateSize; // Size of private data structure at the end of this header
-
- PVOID hAllocationHandle; // User-mode-assigned allocation handle for CreateAllocation
- PVOID hDeviceSpecificHandle; // Assigned in kernal OpenAllocation
- PVOID hInternalHandle; // Assigned in kernal CreateAllocation
- UINT pitch; // Hinted surface pitch
-
- BYTE originalPrivateData[1]; // Variable length
-
-
-} ALLOC_PRIVATE_STRUCTURE, *PALLOC_PRIVATE_STRUCTURE;
-
-typedef struct _ESCAPE_STRUCTURE
-{
- UINT32 magic; // Friend or foe identifier for our filter driver
-
- UINT32 escapeType; // Specifier for individual type of escape message
- // Type 1 for notepad
- union {
- HandleNotepad notepad;
- };
-} ESCAPE_STRUCTURE, *PESCAPE_STRUCTURE;
-
-// Structures for internal operation of KM driver
-
-typedef struct _RIFT_SYNC
-{
- ULONG childUid; // ChildUid as reported by RIFT_STATUS
- ULONG vsync; // 1 for vsync, 0 for immediate
-} RIFT_SYNC, *PRIFT_SYNC;
-
-typedef struct _RIFT_MODE
-{
- ULONG childUid; // ChildUid as reported by RIFT_STATUS
- ULONG mode; // Bitmap of mode values, defined by OVR_RIFT_HOME_*
- HANDLE userModeHandle; // Handle of render target created in user mode
- // that's usable as a primary
-} RIFT_MODE, *PRIFT_MODE;
-
-typedef struct _RIFT_STATUS
-{
- ULONG childUid; // Display driver assigned Uid for this display
- ULONG mode; // Active rift mode, see OVR_RIFT_MODE_*
- ULONG serialNumber; // Serial number as reported in the Rift's EDID
- ULONG textureHandle; // Handle of shared render resource -- NULL if not shared
-} RIFT_STATUS, *PRIFT_STATUS;
-
-typedef struct _RIFT_STATUS_ARRAY
-{
- ULONG arraySize; // Size of pre-allocated RIFT_STATUS structures.
- RIFT_STATUS status[1]; // Array of status blocks containing connection information on each Rift
-} RIFT_STATUS_ARRAY, *PRIFT_STATUS_ARRAY;
-
-#pragma pack(pop)
-
-// IOCTL for UM application to KM driver
-
-#define OVR_STATUS_SUCCESS 0
-#define OVR_STATUS_FAIL -1
-#define OVR_STATUS_DRIVER_IN_USE -2
-#define OVR_STATUS_MODE_ALREADY_ACTIVE -3
-#define OVR_STATUS_RIFT_NOT_PRESENT -4
-
-//
-// Returns the number of Rift displays attached to the video adapter
-// If 0, no Rift displays have been connected.
-// If greater than 0, use this size to pre-allocate space for an array
-// of rift statuses
-//
-// Input Buffer: Nothing
-// Output Buffer: LONG - count of Rift displays attached to video adapter
-//
-#define IOCTL_RIFTMGR_GET_RIFT_COUNT CTL_CODE(FILE_DEVICE_VIDEO, \
- FUNCTION_INDEX, METHOD_BUFFERED, FILE_ANY_ACCESS)
-
-//
-// Fills out a pre-allocated array with information on the individually attached
-// screens.
-//
-// On Input, specify the arraySize as the size of the allocation.
-//
-// On Output, the arraySize will be updated with the actual number of Rifts
-// reported. Use IOCTL_RIFTMGR_GET_RIFT_COUNT to query the number of Rifts.
-// If the count changes (added or removed) between calls, the function will either fail
-// due to the buffer being too small, or the arraySize count will be updated
-// with a new count of devices along with their respective parameters.
-//
-// Input Buffer: PRIFT_STATUS - Pointer to allocated status array
-// Output Buffer: LONG - Count of Rift displays reported in the structure. -1 if out of
-// memory
-//
-#define IOCTL_RIFTMGR_GET_RIFT_ARRAY CTL_CODE(FILE_DEVICE_VIDEO, \
- FUNCTION_INDEX + 1, METHOD_NEITHER, FILE_ANY_ACCESS)
-
-// Changes the mode of an attached Rift (DEPRECATED)
-// Input Buffer: PRIFT_MODE - Pointer to a mode structure specifying the childUid and
-// mode for a particular Rift
-// Output Buffer: LONG - Non-zero on error, 0 on successful mode change
-//
-#define IOCTL_RIFTMGR_SET_RIFT_MODE CTL_CODE(FILE_DEVICE_VIDEO, \
- FUNCTION_INDEX + 2, METHOD_NEITHER, FILE_ANY_ACCESS)
-
-// Lock the primary of the rift and obtain an address
-// Input Buffer: ULONG - ChildUid of a Rift as previously discovered
-// Output Buffer: ULONG_PTR - Pointer to a usermode mapped address of the primary
-#define IOCTL_RIFTMGR_GET_RIFT_PRIMARY CTL_CODE(FILE_DEVICE_VIDEO, \
- FUNCTION_INDEX + 3, METHOD_NEITHER, FILE_ANY_ACCESS)
-
-// Release Rift primary
-// Input Buffer: PULONG_PTR - ChildUid of a Rift as previously discovered and virtual pointer
-// Output Buffer: NOTHING
-#define IOCTL_RIFTMGR_RELEASE_RIFT_PRIMARY CTL_CODE(FILE_DEVICE_VIDEO, \
- FUNCTION_INDEX + 4, METHOD_NEITHER, FILE_ANY_ACCESS)
-
-
-// Point the rift to another render target
-// Input Buffer: PHANDLE - Array of handles, rift and the render target resource
-// Output Buffer: NOTHING
-#define IOCTL_RIFTMGR_SETRIFTBUFFER CTL_CODE(FILE_DEVICE_VIDEO, \
- FUNCTION_INDEX + 5, METHOD_NEITHER, FILE_ANY_ACCESS)
-
-// Enable or disable vsync on Rift present
-// Input Buffer: PRIFT_SYNC - Pointer to a mode structure specifying the childUid and
-// and sync
-// Output Buffer: NOTHING
-#define IOCTL_RIFTMGR_SETVSYNCMODE CTL_CODE(FILE_DEVICE_VIDEO, \
- FUNCTION_INDEX + 6, METHOD_NEITHER, FILE_ANY_ACCESS)
-
-// Get scan line
-// Input Buffer: ULONG - ChildUid of a Rift as previously discovered
-// Output Buffer: ULONG - 31st bit is set if in vertical blank, high 15 bits has per second
-// frame number (0-74), low 16 bits has scanline (0-1919)
-#define IOCTL_RIFTMGR_GETSCANLINE CTL_CODE(FILE_DEVICE_VIDEO, \
- FUNCTION_INDEX + 7, METHOD_NEITHER, FILE_ANY_ACCESS)
-
-// Enable or disable compatibility mode. Entering compatibility mode will fail if
-// the Rift is already actively scanning out a surface
-// Input Buffer: LONG - Bit assignments:
-// LSB (bit 0) is a flag for compatibility mode itself.
-// 1 means compatibility mode.
-// 0 means application direct mode.
-// Bit 1 means "Hide DK1's".
-// 1 means operate DK1's in synchronous with the compatibility mode exactly.
-// 0 means operate in DK1 legacy mode.
-// Output Buffer: LONG - Result value (see OVR statuses)
-// 0 = success
-// -1 = general failure
-// -2 = failure, rift scanning out
-// -3 = already active
-// -4 = rift not present
-#define IOCTL_RIFTMGR_SETCOMPATIBILITYMODE CTL_CODE(FILE_DEVICE_VIDEO, \
- FUNCTION_INDEX + 8, METHOD_NEITHER, FILE_ANY_ACCESS)
-
-// Call to obtain the current status of compatibility mode
-// Input Buffer: NOTHING
-// Output Buffer: LONG - Bit assignments:
-// LSB (bit 0) is a flag for compatibility mode itself.
-// 1 means compatibility mode.
-// 0 means application direct mode.
-// Bit 1 means "Hide DK1's".
-// 1 means operate DK1's in synchronous with the compatibility mode exactly.
-// 0 means operate in DK1 legacy mode.
-#define IOCTL_RIFTMGR_GETCOMPATIBILITYMODE CTL_CODE(FILE_DEVICE_VIDEO, \
- FUNCTION_INDEX + 9, METHOD_NEITHER, FILE_ANY_ACCESS)
-
-// Call to set the power mode of a particular Rift
-// Input Buffer: PULONG_PTR - ChildUid of a Rift as previously discovered and ULONG value
-// second ULONG has value of
-// 0 to simply obtain the power status of the display
-// 1 to set the display into a full power state (needs a primary to fully scan out)
-// 2 to set the display into sleep mode
-// 3 to set the display into full power off mode (WARNING: Will potentially trash primary)
-// Output Buffer: LONG - Result value
-// 0 = Failure to obtain power status
-// 1 = Full power
-// 2 = Sleep
-// 3 = Power off
-#define IOCTL_RIFTMGR_DISPLAYPOWER CTL_CODE(FILE_DEVICE_VIDEO, \
- FUNCTION_INDEX + 10, METHOD_NEITHER, FILE_ANY_ACCESS)
-
-// Return the EDID of the display in the output buffer. The driver
-// will copy as many bytes as possible to fill the buffer.
-// Input Buffer: ULONG - ChildUid of a Rift as previously discovered
-// Output Buffer: PCHAR - Preallocated buffer of a variable size to store the EDID from the display
-#define IOCTL_RIFTMGR_GETEDID CTL_CODE(FILE_DEVICE_VIDEO, \
- FUNCTION_INDEX + 11, METHOD_NEITHER, FILE_ANY_ACCESS)
-
-#endif
+/************************************************************************************ + +PublicHeader: None +Filename : dxgi_ovr_filter.h +Content : Shared usermode/kernel mode definitions for IOCTL functionality. + Also used from LibOVR to access the driver. +Created : January 27, 2014 +Authors : Dean Beeler + +Copyright : Copyright 2013 Oculus, LLC. All Rights reserved. + +Use of this software is subject to the terms of the Oculus LLC license +agreement provided at the time of installation or download, or which +otherwise accompanies this software in either electronic or hard copy form. + +/************************************************************************************/ + +#ifndef OVR_dxgi_ovr_filter_h +#define OVR_dxgi_ovr_filter_h + +#include <Kernel/OVR_Win32_IncludeWindows.h> +#include <InitGuid.h> + +#define USERMODE_TEST_ROTATION 1 + +#if USERMODE_TEST_ROTATION // Used only by the um test application +#define USERMODE_SURFACE_WIDTH 1920 +#define USERMODE_SURFACE_HEIGHT 1080 +#else +#define USERMODE_SURFACE_WIDTH 1080 +#define USERMODE_SURFACE_HEIGHT 1920 +#endif + +#define TEST_ROTATION 0 // Kernel-mode parameters + +#if TEST_ROTATION +#define SURFACE_WIDTH 1920 +#define SURFACE_HEIGHT 1080 +#else +#define SURFACE_WIDTH 1080 +#define SURFACE_HEIGHT 1920 +#endif + +// {46231713-49FD-4922-84E3-9FF907C06803} +DEFINE_GUID(GUID_DEVINTERFACE_OVRRIFTMGR, + 0x46231713, 0x49fd, 0x4922, 0x84, 0xe3, 0x9f, 0xf9, 0x7, 0xc0, 0x68, 0x3); + +#define QUERYADAPTER_MAGICSIZE 17836 +#define QUERYADAPTER_MAGICHEADER 0x4f565246 // OVRF +#define QUERYADAPTER_MAXPATH 2048 + +#define FUNCTION_INDEX 0xb800 + +#pragma pack(push,1) + +#define OVR_RIFT_MODE_OFF 0 // Disabled +#define OVR_RIFT_MODE_ENABLED 1 // Enabled +#define OVR_RIFT_MODE_EXTEND 2 // Extending 2D displays. Without this flag + // 2D displays are disabled when the Rift + // is active +#define OVR_RIFT_MODE_FRONTBUFFER 4 // Enable front buffer only for Rift +#define OVR_RIFT_MODE_LOCKMOUSE 8 // Prevent mouse from entering bounds + +#define OVR_ESCAPE_TYPE_HANDLE 1 // Escape to notify driver of our collected handles + +#define OVR_FlipImmediate 0x2 +#define OVR_FlipOnNextVSync 0x4 + +//----------------------------------------------------------------------------------- +// Structures for application to UM driver + +// Kernel32.dll functionality +typedef HMODULE (WINAPI *WinLoadLibraryA) ( LPCSTR ); +typedef HMODULE (WINAPI *WinLoadLibraryW) ( LPCWSTR ); +typedef HMODULE (WINAPI *WinLoadLibraryExA) ( LPCSTR, HANDLE, DWORD ); +typedef HMODULE (WINAPI *WinLoadLibraryExW) ( LPCWSTR, HANDLE, DWORD ); +typedef BOOL (WINAPI *WinGetModuleHandleExA)( DWORD, LPCSTR, HMODULE* ); +typedef BOOL (WINAPI *WinGetModuleHandleExW)( DWORD, LPCWSTR, HMODULE* ); + +// Overridden DirectX 9 entry points +typedef void* (WINAPI *WinDirect3DCreate9)(UINT SDKVersion); +typedef HRESULT (WINAPI *WinDirect3DCreate9Ex)(UINT SDKVersion, void** aDevice); + +// Overridden DXGI entry points +typedef HRESULT (WINAPI *WinCreateDXGIFactory)( + __in REFIID riid, + __out void **ppFactory + ); + +typedef HRESULT (WINAPI *WinCreateDXGIFactory1)( + __in REFIID riid, + __out void **ppFactory + ); + +typedef HRESULT (WINAPI *WinCreateDXGIFactory2)( + __in UINT flags, + __in const IID &riid, + __out void **ppFactory + ); + +// Application usermode callbacks from usermode driver. These +// functions are all provided by the calling application that uses +// the filter mode driver + +// IsInitializingDisplay is used at runtime to validate that +// the created resource (RT or bind_present) matches the resolution +// of our expected backbuffer. If the application returns true, +// our usermode driver will convert this to a primary +typedef BOOL (WINAPI *IsInitializingDisplay) ( PVOID, UINT, UINT ); +// RiftForContext is a function that will return the Rift device of +// the concerned context. This is for targeting a particular +// device instance with a particular Rift for rendering +typedef ULONG (WINAPI *RiftForContext)( PVOID, HANDLE ); +// CloseRiftForContext is a function that informs the application +// the created device is shutting down and the context +// can freedly disassociate with the particular +typedef BOOL (WINAPI *CloseRiftForContext)( PVOID, HANDLE, ULONG ); +typedef BOOL (WINAPI *WindowDisplayResolution)( PVOID, UINT*, UINT*, UINT*, UINT*, BOOL* ); +// IsCreatingBackBuffer is a function directed at the runtime shim +// to confirm that the runtime is actively creating the additional +// swapchain for rotation and display out to the rift. +// When creating the original swapchain this function should return false +// so the orignal swapchain isn't inadvertantly coopted. +typedef BOOL (WINAPI *IsCreatingBackBuffer)( PVOID ); +// Callback from the usermode driver to obtain the desire to see debug statements from +// the usermode drivers on the output console. Only called one per usermode driver shim +// and usermode runtime. +typedef BOOL (WINAPI *ShouldEnableDebug)( VOID ); +// Callback from the usermode driver to the runtime obtain the vsync status +typedef BOOL (WINAPI *ShouldVSync)( VOID ); +// Callback from usermode mode and runtime driver to obtain expected native width, +// height and degrees rotation of the rift +typedef BOOL (WINAPI *ExpectedResolution)( PVOID, UINT*, UINT*, UINT* ); +// Usermode callback that reports whether or not mirroring is enabled +typedef BOOL (WINAPI *MirroringEnabled)( PVOID ); +// Callback from the shim for Unity and other plugins used to +// report the swapchain that was created by the application +typedef void* (WINAPI *GetDX11SwapChain)( PVOID ); +// Callback to report the HWND associated with this context +typedef HWND (WINAPI* GetWindowForContext)( PVOID ); +// Should present Rift on context +typedef BOOL (WINAPI* PresentRiftOnContext)( PVOID ); +// Used by a pre-loaded shim (d3d9, dxgi, opengl32) to +// identify which api version we loaded +// 1 = OpenGL +// 9 = DirectX 9 +// 10 = DirectX 1X +typedef int (WINAPI* ActiveAPIVersion)( PVOID ); + +// Get the version of the runtime filter. +typedef ULONG (WINAPI* GetRTFilterVersion)(); + +#pragma warning(push) +#pragma warning(disable: 4201) + +typedef struct _LINK_APPLICATION_DRIVER +{ + UINT32 version; + PVOID context; + + union + { + struct + { + IsInitializingDisplay pfnInitializingDisplay; + RiftForContext pfnRiftForContext; + CloseRiftForContext pfnCloseRiftForContext; + WindowDisplayResolution pfnWindowDisplayResolution; + IsCreatingBackBuffer pfnIsCreatingBackBuffer; + ShouldEnableDebug pfnShouldEnableDebug; + ShouldVSync pfnShouldVSync; + ExpectedResolution pfnExpectedResolution; + MirroringEnabled pfnMirroringEnabled; + GetDX11SwapChain pfnGetDX11SwapChain; + GetWindowForContext pfnGetWindowForContext; + PresentRiftOnContext pfnPresentRiftOnContext; + ActiveAPIVersion pfnActiveAPIVersion; + }; + + PROC placeholders[128]; + }; + + + // Used by Runtime filter for linking with original libraries + WinDirect3DCreate9 pfnDirect3DCreate9; + WinDirect3DCreate9Ex pfnDirect3DCreate9Ex; + WinCreateDXGIFactory pfnCreateDXGIFactory; + WinCreateDXGIFactory1 pfnCreateDXGIFactory1; + WinCreateDXGIFactory2 pfnCreateDXGIFactory2; +} LINK_APPLICATION_DRIVER, *PLINK_APPLICATION_DRIVER; + +#pragma warning(pop) + + +// OVRDisplay.dll functionality +typedef HRESULT (WINAPI *PreloadLibraryFn) ( WinLoadLibraryA , LPCSTR, PLINK_APPLICATION_DRIVER appDriver ); +typedef HRESULT (WINAPI *PreloadLibraryRTFn) ( PLINK_APPLICATION_DRIVER appDriver ); + +//----------------------------------------------------------------------------------- +// Structures for UM driver to KM driver + +typedef struct _QUERY_KM_DRIVER +{ + UINT32 magic; // Friend or foe identifier for our filter driver + // See: QUERYADAPTER_MAGICHEADER + UINT32 maxVidPnSources; // Returns the maximum number of video present network sources +} QUERY_KM_DRIVER, *PQUERY_KM_DRIVER; + +#ifndef _D3DUKMDT_H_ +typedef UINT D3DKMT_HANDLE; +#endif + +typedef struct _HandleNotepad +{ + // These are assigned around CreateResource + HANDLE hUsermodeInResource; + HANDLE hUsermodeOutResource; + + // These are assigned within the kernel with + // DxgkDdiCreateAllocation and + // DxgkDdiOpenAllocation + D3DKMT_HANDLE hAllocation; + PVOID hDeviceSpecificHandle; + PVOID hKernelDriverHandle; + + // These are assigned around pfnAllocateCb + HANDLE hUsermodeSharedResource; + D3DKMT_HANDLE hKernelModeSharedResource; + + ULONG childUid; + + UINT pitch; + +} HandleNotepad, *PHandleNotepad; + + +typedef struct _ALLOC_PRIVATE_STRUCTURE +{ + UINT32 magic; // Friend or foe identifier for our filter driver + + PVOID originalPrivataDataPtr; // Location in usermode of the original private data structure + UINT originalPrivateSize; // Size of private data structure at the end of this header + + PVOID hAllocationHandle; // User-mode-assigned allocation handle for CreateAllocation + PVOID hDeviceSpecificHandle; // Assigned in kernal OpenAllocation + PVOID hInternalHandle; // Assigned in kernal CreateAllocation + UINT pitch; // Hinted surface pitch + + BYTE originalPrivateData[1]; // Variable length + + +} ALLOC_PRIVATE_STRUCTURE, *PALLOC_PRIVATE_STRUCTURE; + +typedef struct _ESCAPE_STRUCTURE +{ + UINT32 magic; // Friend or foe identifier for our filter driver + + UINT32 escapeType; // Specifier for individual type of escape message + // Type 1 for notepad + union { + HandleNotepad notepad; + }; +} ESCAPE_STRUCTURE, *PESCAPE_STRUCTURE; + +// Structures for internal operation of KM driver + +typedef struct _RIFT_SYNC +{ + ULONG childUid; // ChildUid as reported by RIFT_STATUS + ULONG vsync; // 1 for vsync, 0 for immediate +} RIFT_SYNC, *PRIFT_SYNC; + +typedef struct _RIFT_MODE +{ + ULONG childUid; // ChildUid as reported by RIFT_STATUS + ULONG mode; // Bitmap of mode values, defined by OVR_RIFT_HOME_* + HANDLE userModeHandle; // Handle of render target created in user mode + // that's usable as a primary +} RIFT_MODE, *PRIFT_MODE; + +typedef struct _RIFT_STATUS +{ + ULONG childUid; // Display driver assigned Uid for this display + ULONG mode; // Active rift mode, see OVR_RIFT_MODE_* + ULONG serialNumber; // Serial number as reported in the Rift's EDID + ULONG textureHandle; // Handle of shared render resource -- NULL if not shared +} RIFT_STATUS, *PRIFT_STATUS; + +typedef struct _RIFT_STATUS_ARRAY +{ + ULONG arraySize; // Size of pre-allocated RIFT_STATUS structures. + RIFT_STATUS status[1]; // Array of status blocks containing connection information on each Rift +} RIFT_STATUS_ARRAY, *PRIFT_STATUS_ARRAY; + +#pragma pack(pop) + +// IOCTL for UM application to KM driver + +#define OVR_STATUS_SUCCESS 0 +#define OVR_STATUS_FAIL -1 +#define OVR_STATUS_DRIVER_IN_USE -2 +#define OVR_STATUS_MODE_ALREADY_ACTIVE -3 +#define OVR_STATUS_RIFT_NOT_PRESENT -4 + +// +// Returns the number of Rift displays attached to the video adapter +// If 0, no Rift displays have been connected. +// If greater than 0, use this size to pre-allocate space for an array +// of rift statuses +// +// Input Buffer: Nothing +// Output Buffer: LONG - count of Rift displays attached to video adapter +// +#define IOCTL_RIFTMGR_GET_RIFT_COUNT CTL_CODE(FILE_DEVICE_VIDEO, \ + FUNCTION_INDEX, METHOD_BUFFERED, FILE_ANY_ACCESS) + +// +// Fills out a pre-allocated array with information on the individually attached +// screens. +// +// On Input, specify the arraySize as the size of the allocation. +// +// On Output, the arraySize will be updated with the actual number of Rifts +// reported. Use IOCTL_RIFTMGR_GET_RIFT_COUNT to query the number of Rifts. +// If the count changes (added or removed) between calls, the function will either fail +// due to the buffer being too small, or the arraySize count will be updated +// with a new count of devices along with their respective parameters. +// +// Input Buffer: PRIFT_STATUS - Pointer to allocated status array +// Output Buffer: LONG - Count of Rift displays reported in the structure. -1 if out of +// memory +// +#define IOCTL_RIFTMGR_GET_RIFT_ARRAY CTL_CODE(FILE_DEVICE_VIDEO, \ + FUNCTION_INDEX + 1, METHOD_NEITHER, FILE_ANY_ACCESS) + +// Changes the mode of an attached Rift (DEPRECATED) +// Input Buffer: PRIFT_MODE - Pointer to a mode structure specifying the childUid and +// mode for a particular Rift +// Output Buffer: LONG - Non-zero on error, 0 on successful mode change +// +#define IOCTL_RIFTMGR_SET_RIFT_MODE CTL_CODE(FILE_DEVICE_VIDEO, \ + FUNCTION_INDEX + 2, METHOD_NEITHER, FILE_ANY_ACCESS) + +// Lock the primary of the rift and obtain an address +// Input Buffer: ULONG - ChildUid of a Rift as previously discovered +// Output Buffer: ULONG_PTR - Pointer to a usermode mapped address of the primary +#define IOCTL_RIFTMGR_GET_RIFT_PRIMARY CTL_CODE(FILE_DEVICE_VIDEO, \ + FUNCTION_INDEX + 3, METHOD_NEITHER, FILE_ANY_ACCESS) + +// Release Rift primary +// Input Buffer: PULONG_PTR - ChildUid of a Rift as previously discovered and virtual pointer +// Output Buffer: NOTHING +#define IOCTL_RIFTMGR_RELEASE_RIFT_PRIMARY CTL_CODE(FILE_DEVICE_VIDEO, \ + FUNCTION_INDEX + 4, METHOD_NEITHER, FILE_ANY_ACCESS) + + +// Point the rift to another render target +// Input Buffer: PHANDLE - Array of handles, rift and the render target resource +// Output Buffer: NOTHING +#define IOCTL_RIFTMGR_SETRIFTBUFFER CTL_CODE(FILE_DEVICE_VIDEO, \ + FUNCTION_INDEX + 5, METHOD_NEITHER, FILE_ANY_ACCESS) + +// Enable or disable vsync on Rift present +// Input Buffer: PRIFT_SYNC - Pointer to a mode structure specifying the childUid and +// and sync +// Output Buffer: NOTHING +#define IOCTL_RIFTMGR_SETVSYNCMODE CTL_CODE(FILE_DEVICE_VIDEO, \ + FUNCTION_INDEX + 6, METHOD_NEITHER, FILE_ANY_ACCESS) + +// Get scan line +// Input Buffer: ULONG - ChildUid of a Rift as previously discovered +// Output Buffer: ULONG - 31st bit is set if in vertical blank, high 15 bits has per second +// frame number (0-74), low 16 bits has scanline (0-1919) +#define IOCTL_RIFTMGR_GETSCANLINE CTL_CODE(FILE_DEVICE_VIDEO, \ + FUNCTION_INDEX + 7, METHOD_NEITHER, FILE_ANY_ACCESS) + +// Enable or disable compatibility mode. Entering compatibility mode will fail if +// the Rift is already actively scanning out a surface +// Input Buffer: LONG - Bit assignments: +// LSB (bit 0) is a flag for compatibility mode itself. +// 1 means compatibility mode. +// 0 means application direct mode. +// Bit 1 means "Hide DK1's". +// 1 means operate DK1's in synchronous with the compatibility mode exactly. +// 0 means operate in DK1 legacy mode. +// Output Buffer: LONG - Result value (see OVR statuses) +// 0 = success +// -1 = general failure +// -2 = failure, rift scanning out +// -3 = already active +// -4 = rift not present +#define IOCTL_RIFTMGR_SETCOMPATIBILITYMODE CTL_CODE(FILE_DEVICE_VIDEO, \ + FUNCTION_INDEX + 8, METHOD_NEITHER, FILE_ANY_ACCESS) + +// Call to obtain the current status of compatibility mode +// Input Buffer: NOTHING +// Output Buffer: LONG - Bit assignments: +// LSB (bit 0) is a flag for compatibility mode itself. +// 1 means compatibility mode. +// 0 means application direct mode. +// Bit 1 means "Hide DK1's". +// 1 means operate DK1's in synchronous with the compatibility mode exactly. +// 0 means operate in DK1 legacy mode. +#define IOCTL_RIFTMGR_GETCOMPATIBILITYMODE CTL_CODE(FILE_DEVICE_VIDEO, \ + FUNCTION_INDEX + 9, METHOD_NEITHER, FILE_ANY_ACCESS) + +// Call to set the power mode of a particular Rift +// Input Buffer: PULONG_PTR - ChildUid of a Rift as previously discovered and ULONG value +// second ULONG has value of +// 0 to simply obtain the power status of the display +// 1 to set the display into a full power state (needs a primary to fully scan out) +// 2 to set the display into sleep mode +// 3 to set the display into full power off mode (WARNING: Will potentially trash primary) +// Output Buffer: LONG - Result value +// 0 = Failure to obtain power status +// 1 = Full power +// 2 = Sleep +// 3 = Power off +#define IOCTL_RIFTMGR_DISPLAYPOWER CTL_CODE(FILE_DEVICE_VIDEO, \ + FUNCTION_INDEX + 10, METHOD_NEITHER, FILE_ANY_ACCESS) + +// Return the EDID of the display in the output buffer. The driver +// will copy as many bytes as possible to fill the buffer. +// Input Buffer: ULONG - ChildUid of a Rift as previously discovered +// Output Buffer: PCHAR - Preallocated buffer of a variable size to store the EDID from the display +#define IOCTL_RIFTMGR_GETEDID CTL_CODE(FILE_DEVICE_VIDEO, \ + FUNCTION_INDEX + 11, METHOD_NEITHER, FILE_ANY_ACCESS) + +#define IOCTL_RIFTMGR_WAITFORVSYNC CTL_CODE(FILE_DEVICE_VIDEO, \ + FUNCTION_INDEX + 12, METHOD_NEITHER, FILE_ANY_ACCESS) + +// Return information about the currently scanned out frame to the rift +// Input Buffer: ULONG - ChildUid of a Rift as previously discovered +// Output Buffer: UINT64[2] - Preallocated buffer of 2 UINT64s to hold CurrentFrameIndex & QPC time of current scan out start +#define IOCTL_RIFTMGR_GETCURRENTFRAMEINFO CTL_CODE(FILE_DEVICE_VIDEO, \ + FUNCTION_INDEX + 13, METHOD_NEITHER, FILE_ANY_ACCESS) + +// Return the kernel index of the adapter that the rift is connected to +// Input Buffer: ULONG - ChildUid of a Rift as previously discovered +// Output Buffer: ULONG - The kernel adapter index +#define IOCTL_RIFTMGR_GETRIFTADAPTERID CTL_CODE(FILE_DEVICE_VIDEO, \ + FUNCTION_INDEX + 14, METHOD_NEITHER, FILE_ANY_ACCESS) + +#endif diff --git a/LibOVR/Src/Displays/OVR_Win32_FocusReader.cpp b/LibOVR/Src/Displays/OVR_Win32_FocusReader.cpp index 7c3c7a3..e551657 100644 --- a/LibOVR/Src/Displays/OVR_Win32_FocusReader.cpp +++ b/LibOVR/Src/Displays/OVR_Win32_FocusReader.cpp @@ -1,79 +1,79 @@ -/************************************************************************************
-
-Filename : OVR_Win32_FocusReader.cpp
-Content : Reader for current app with focus on Windows
-Created : July 2, 2014
-Authors : Chris Taylor
-
-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_Win32_FocusReader.h"
-#include "../Kernel/OVR_Log.h"
-#include "../Service/Service_NetClient.h"
-
-OVR_DEFINE_SINGLETON(OVR::Win32::RenderFocusReader);
-
-namespace OVR { namespace Win32 {
-
-
-HWND RenderFocusReader::ReadActiveWindow()
-{
- const LocklessFocusState* focusState = Reader.Get();
-
- if (!focusState || NoSharedMemory)
- {
- if (!Reader.Open(OVR_FOCUS_OBSERVER_SHARE_NAME))
- {
- OVR_DEBUG_LOG(("[Win32ShimFunctions] Unable to open the shared memory space"));
- // Note: This should only warn and not assert because it is normal behavior when the server is not running.
- NoSharedMemory = true;
- return 0;
- }
-
- focusState = Reader.Get();
- if (!focusState)
- {
- OVR_DEBUG_LOG(("[Win32ShimFunctions] Unable to get the shared memory space"));
- NoSharedMemory = true;
- return 0;
- }
- }
-
- return (HWND)Ptr64ToPtr(focusState->ActiveWindowHandle);
-}
-
-RenderFocusReader::RenderFocusReader() :
- NoSharedMemory(false)
-{
- // Must be at end of function
- PushDestroyCallbacks();
-}
-
-RenderFocusReader::~RenderFocusReader()
-{
-}
-
-void RenderFocusReader::OnSystemDestroy()
-{
- delete this;
-}
-
-
-}} // namespace OVR::Win32
+/************************************************************************************ + +Filename : OVR_Win32_FocusReader.cpp +Content : Reader for current app with focus on Windows +Created : July 2, 2014 +Authors : Chris Taylor + +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_Win32_FocusReader.h" +#include "Kernel/OVR_Log.h" +#include "../Service/Service_NetClient.h" + +OVR_DEFINE_SINGLETON(OVR::Win32::RenderFocusReader); + +namespace OVR { namespace Win32 { + + +HWND RenderFocusReader::ReadActiveWindow() +{ + const LocklessFocusState* focusState = Reader.Get(); + + if (!focusState || NoSharedMemory) + { + if (!Reader.Open(OVR_FOCUS_OBSERVER_SHARE_NAME)) + { + OVR_DEBUG_LOG(("[Win32ShimFunctions] Unable to open the shared memory space")); + // Note: This should only warn and not assert because it is normal behavior when the server is not running. + NoSharedMemory = true; + return 0; + } + + focusState = Reader.Get(); + if (!focusState) + { + OVR_DEBUG_LOG(("[Win32ShimFunctions] Unable to get the shared memory space")); + NoSharedMemory = true; + return 0; + } + } + + return (HWND)Ptr64ToPtr(focusState->ActiveWindowHandle); +} + +RenderFocusReader::RenderFocusReader() : + NoSharedMemory(false) +{ + // Must be at end of function + PushDestroyCallbacks(); +} + +RenderFocusReader::~RenderFocusReader() +{ +} + +void RenderFocusReader::OnSystemDestroy() +{ + delete this; +} + + +}} // namespace OVR::Win32 diff --git a/LibOVR/Src/Displays/OVR_Win32_FocusReader.h b/LibOVR/Src/Displays/OVR_Win32_FocusReader.h index 358ab38..3805b1d 100644 --- a/LibOVR/Src/Displays/OVR_Win32_FocusReader.h +++ b/LibOVR/Src/Displays/OVR_Win32_FocusReader.h @@ -1,81 +1,81 @@ -/************************************************************************************
-
-Filename : OVR_Win32_FocusReader.h
-Content : Reader for current app with focus on Windows
-Created : July 2, 2014
-Authors : Chris Taylor
-
-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.
-
-*************************************************************************************/
-
-#ifndef OVR_Win32_FocusReader_h
-#define OVR_Win32_FocusReader_h
-
-#include "../Kernel/OVR_System.h"
-#include "../Kernel/OVR_Lockless.h"
-#include "../Kernel/OVR_Array.h"
-#include "../Kernel/OVR_SharedMemory.h"
-
-namespace OVR { namespace Win32 {
-
-
-#define OVR_FOCUS_OBSERVER_SHARE_NAME "OVRAppFocus"
-
-//-----------------------------------------------------------------------------
-// LocklessFocusState
-
-#pragma pack(push, 8)
-
-// Focus state data
-struct LocklessFocusState
-{
- LocklessFocusState(DWORD pid = 0) :
- ActiveProcessId(pid),
- ActiveWindowHandle(NULL)
- {
- }
-
- DWORD ActiveProcessId;
- void * POINTER_64 ActiveWindowHandle;
-};
-
-#pragma pack(pop)
-
-typedef SharedObjectWriter< LocklessFocusState > SharedFocusWriter;
-typedef SharedObjectReader< LocklessFocusState > SharedFocusReader;
-
-
-//-----------------------------------------------------------------------------
-// RenderFocusReader
-
-class RenderFocusReader : public OVR::SystemSingletonBase<RenderFocusReader>, public NewOverrideBase
-{
- OVR_DECLARE_SINGLETON(RenderFocusReader);
-
- SharedFocusReader Reader; // Shared memory reader
- bool NoSharedMemory; // Flag reporting that no shared memory has been detected;
-
-public:
- HWND ReadActiveWindow();
-};
-
-
-}} // namespace OVR::Win32
-
-#endif // OVR_Win32_FocusReader_h
+/************************************************************************************ + +Filename : OVR_Win32_FocusReader.h +Content : Reader for current app with focus on Windows +Created : July 2, 2014 +Authors : Chris Taylor + +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. + +*************************************************************************************/ + +#ifndef OVR_Win32_FocusReader_h +#define OVR_Win32_FocusReader_h + +#include "Kernel/OVR_System.h" +#include "Kernel/OVR_Lockless.h" +#include "Kernel/OVR_Array.h" +#include "Kernel/OVR_SharedMemory.h" + +namespace OVR { namespace Win32 { + + +#define OVR_FOCUS_OBSERVER_SHARE_NAME "OVRAppFocus" + +//----------------------------------------------------------------------------- +// LocklessFocusState + +#pragma pack(push, 8) + +// Focus state data +struct LocklessFocusState +{ + LocklessFocusState(DWORD pid = 0) : + ActiveProcessId(pid), + ActiveWindowHandle(NULL) + { + } + + DWORD ActiveProcessId; + void * POINTER_64 ActiveWindowHandle; +}; + +#pragma pack(pop) + +typedef SharedObjectWriter< LocklessFocusState > SharedFocusWriter; +typedef SharedObjectReader< LocklessFocusState > SharedFocusReader; + + +//----------------------------------------------------------------------------- +// RenderFocusReader + +class RenderFocusReader : public OVR::SystemSingletonBase<RenderFocusReader>, public NewOverrideBase +{ + OVR_DECLARE_SINGLETON(RenderFocusReader); + + SharedFocusReader Reader; // Shared memory reader + bool NoSharedMemory; // Flag reporting that no shared memory has been detected; + +public: + HWND ReadActiveWindow(); +}; + + +}} // namespace OVR::Win32 + +#endif // OVR_Win32_FocusReader_h diff --git a/LibOVR/Src/Displays/OVR_Win32_RenderShim.cpp b/LibOVR/Src/Displays/OVR_Win32_RenderShim.cpp index a74ab75..48bcf2b 100644 --- a/LibOVR/Src/Displays/OVR_Win32_RenderShim.cpp +++ b/LibOVR/Src/Displays/OVR_Win32_RenderShim.cpp @@ -1,974 +1,991 @@ -/************************************************************************************
-
-Filename : OVR_Win32_DisplayShim.cpp
-Content : Shared static functions for inclusion that allow for an application
- to inject the usermode driver into an application
-Created : March 21, 2014
-Authors : Dean Beeler
-
-Copyright : Copyright 2014 Oculus VR, LLC All Rights reserved.
-
-Use of this software is subject to the terms of the Oculus Inc license
-agreement provided at the time of installation or download, or which
-otherwise accompanies this software in either electronic or hard copy form.
-
-************************************************************************************/
-
-#include "../../Include/OVR_Version.h"
-
-#ifndef AVOID_LIB_OVR
-#include "../Kernel/OVR_Types.h" // Without this we can get warnings (due to VC++ bugs) about _malloca being redefined, and miss LibOVR overrides.
-#endif
-
-#include <windows.h>
-#include <DbgHelp.h>
-#include <malloc.h>
-
-#include "OVR_Win32_Dxgi_Display.h"
-#include "OVR_Win32_ShimVersion.h"
-
-#if AVOID_LIB_OVR
-#define IN_COMPATIBILITY_MODE() (0)
-#else
-#include "OVR_Win32_Display.h"
-#define IN_COMPATIBILITY_MODE() OVR::Display::InCompatibilityMode()
-#endif
-
-#pragma comment(lib, "DbgHelp.lib")
-
-#define WIDE_TO_MB(wideString) \
- int wideString ## _slen = (int)wcslen(wideString); \
- char* wideString ## _cstr = (char*)alloca(wideString ## _slen * 2); \
- int count = WideCharToMultiByte(GetACP(), 0, wideString, -1, wideString ## _cstr, wideString ## _slen * 2, NULL, NULL); \
- wideString ## _cstr[count] = '\0';
-
-// Forward declarations
-// These functions are implemented in OVR_Win32_DisplayDevice.cpp.
-
-BOOL WINAPI OVRIsInitializingDisplay( PVOID context, UINT width, UINT height );
-BOOL WINAPI OVRIsCreatingBackBuffer( PVOID context );
-BOOL WINAPI OVRShouldVSync( );
-ULONG WINAPI OVRRiftForContext( PVOID context, HANDLE driverHandle );
-BOOL WINAPI OVRCloseRiftForContext( PVOID context, HANDLE driverHandle, ULONG rift );
-BOOL WINAPI OVRWindowDisplayResolution( PVOID context, UINT* width, UINT* height,
- UINT* titleHeight, UINT* borderWidth,
- BOOL* vsyncEnabled );
-BOOL WINAPI OVRExpectedResolution( PVOID context, UINT* width, UINT* height, UINT* rotationInDegrees );
-BOOL WINAPI OVRShouldEnableDebug();
-BOOL WINAPI OVRMirroringEnabled( PVOID context );
-HWND WINAPI OVRGetWindowForContext(PVOID context);
-BOOL WINAPI OVRShouldPresentOnContext(PVOID context);
-
-static const char* GFX_DRIVER_KEY_FMT = "SYSTEM\\CurrentControlSet\\Control\\Class\\{4d36e968-e325-11ce-bfc1-08002be10318}\\%04d";
-#ifdef _WIN64
-static const char* RTFilter = "OVRDisplayRT64.dll";
-static const char* UMFilter = "OVRDisplay64.dll";
-#else
-static const char* RTFilter = "OVRDisplayRT32.dll";
-static const char* UMFilter = "OVRDisplay32.dll";
-#endif
-static const char* OptimusDrivers = "nvumdshimx.dll nvumdshim.dll";
-
-typedef enum OVRTargetAPI
-{
- DirectX,
- OpenGL
-};
-
-static PVOID lastContext = NULL;
-LINK_APPLICATION_DRIVER appDriver = {0};
-static INT apiVersion = 10;
-
-static CHAR* ReadRegStr(HKEY keySub, const char* keyName, const char* valName)
-{
- CHAR *val = NULL;
- REGSAM access = KEY_READ;
- HKEY hKey;
-
-TryAgainWOW64:
- NTSTATUS res = RegOpenKeyExA( keySub, keyName, 0, access, &hKey );
- if ( res == ERROR_SUCCESS )
- {
- DWORD valLen;
- res = RegQueryValueExA( hKey, valName, NULL, NULL, NULL, &valLen );
- if( res == ERROR_SUCCESS ) {
- val = (CHAR*)calloc( valLen + 1, sizeof(CHAR) );
- res = RegQueryValueExA( hKey, valName, NULL, NULL, (LPBYTE)val, &valLen );
-
- if( res == ERROR_SUCCESS )
- {
- CHAR* byte = val;
- for( DWORD j = 0; j < valLen; ++j )
- {
- if( byte[j] == 0 )
- byte[j] = ' ';
- }
- }
- else
- {
- free( val );
- val = NULL;
- }
- }
- RegCloseKey( hKey );
- }
-
- if( res == ERROR_FILE_NOT_FOUND && keySub == HKEY_LOCAL_MACHINE && access == KEY_READ ) {
-#ifdef _WIN64
- access = KEY_READ | KEY_WOW64_32KEY;
-#else
- access = KEY_READ | KEY_WOW64_64KEY;
-#endif
- goto TryAgainWOW64;
- }
- return val;
-}
-
-#define OLD_DATA_BACKUP_SIZE 16
-
-static WinLoadLibraryA oldProcA = NULL; // Note: This is used to indicate that the shim is in place
-static WinLoadLibraryExA oldProcExA = NULL;
-static WinLoadLibraryW oldProcW = NULL;
-static WinLoadLibraryExW oldProcExW = NULL;
-static WinGetModuleHandleExA oldProcModExA = NULL;
-static WinGetModuleHandleExW oldProcModExW = NULL;
-static WinDirect3DCreate9 oldDirectX9Create = NULL;
-static BYTE oldDirectX9CreateData[OLD_DATA_BACKUP_SIZE];
-static WinDirect3DCreate9Ex oldDirectX9ExCreate = NULL;
-static BYTE oldDirectX9ExCreateData[OLD_DATA_BACKUP_SIZE];
-static WinCreateDXGIFactory oldCreateDXGIFactory = NULL;
-static BYTE oldCreateDXGIFactoryData[OLD_DATA_BACKUP_SIZE];
-static WinCreateDXGIFactory1 oldCreateDXGIFactory1 = NULL;
-static BYTE oldCreateDXGIFactory1Data[OLD_DATA_BACKUP_SIZE];
-static WinCreateDXGIFactory2 oldCreateDXGIFactory2 = NULL;
-static BYTE oldCreateDXGIFactory2Data[OLD_DATA_BACKUP_SIZE];
-
-#define NUM_LOADER_LIBS 4
-
-static const char* loaderLibraryList[NUM_LOADER_LIBS] = {
- "kernel32.dll",
- "api-ms-win-core-libraryloader-l1-2-0.dll",
- "api-ms-win-core-libraryloader-l1-1-0.dll",
- "api-ms-win-core-libraryloader-l1-1-1.dll"
-};
-
-enum ShimedLibraries
-{
- ShimLibDXGI = 0,
- ShimLibD3D9 = 1,
- ShimLibD3D11 = 2,
- ShimLibDXGIDebug = 3,
- ShimLibD3D10Core = 4,
- ShimLibD3D10 = 5,
- ShimLibGL = 6,
- ShimCountMax = 7
-};
-
-static const char* dllList[ShimCountMax] = {
- "dxgi.dll",
- "d3d9.dll",
- "d3d11.dll",
- "dxgidebug.dll",
- "d3d10core.dll",
- "d3d10.dll",
- "opengl32.dll"
-};
-
-static HINSTANCE oldLoaderInstances[ShimCountMax] = { NULL };
-static PROC oldLoaderProcA[ShimCountMax][NUM_LOADER_LIBS] = { { NULL } };
-static PROC oldLoaderProcW[ShimCountMax][NUM_LOADER_LIBS] = { { NULL } };
-static PROC oldLoaderProcExA[ShimCountMax][NUM_LOADER_LIBS] = { { NULL } };
-static PROC oldLoaderProcExW[ShimCountMax][NUM_LOADER_LIBS] = { { NULL } };
-static PROC oldLoaderProcModExA[ShimCountMax][NUM_LOADER_LIBS] = { { NULL } };
-static PROC oldLoaderProcModExW[ShimCountMax][NUM_LOADER_LIBS] = { { NULL } };
-
-static HMODULE rtFilterModule = NULL;
-
-static bool checkForOverride( LPCSTR libFileName, OVRTargetAPI& targetApi )
-{
- for (int i=0; ; i++)
- {
- CHAR keyString[256] = {0};
-
- sprintf_s( keyString, 256, GFX_DRIVER_KEY_FMT, i );
-
- CHAR* infSection = ReadRegStr( HKEY_LOCAL_MACHINE, keyString, "InfSection" );
-
- // No provider name means we're out of display enumerations
- if( infSection == NULL )
- break;
-
- free(infSection);
-
- // Check 64-bit driver names followed by 32-bit driver names
- const char* driverKeys[] = {"UserModeDriverName", "UserModeDriverNameWoW", "OpenGLDriverName", "OpenGLDriverNameWoW", "InstalledDisplayDrivers" };
- for( int j = 0; j < 6; ++j )
- {
- CHAR userModeList[4096] = {0};
-
- switch(j)
- {
- case 5:
- strcpy_s( userModeList, 4095, OptimusDrivers );
- break;
- default:
- {
- CHAR* regString = ReadRegStr( HKEY_LOCAL_MACHINE, keyString, driverKeys[j] );
- if( regString )
- {
- strcpy_s( userModeList, 4095, regString );
- free( regString );
- }
-
- }
- break;
- }
-
- char *nextToken = NULL;
-
- if( userModeList )
- {
- char* first = strtok_s( userModeList, " ", &nextToken );
- while( first )
- {
- if( strstr( libFileName, first ) != 0 )
- {
- if( j < 2 )
- targetApi = DirectX;
- else
- targetApi = OpenGL;
-
- return true;
- }
- first = strtok_s( NULL, " ", &nextToken );
- }
- }
- }
- }
-
- return false;
-}
-
-static HMODULE createShim( LPCSTR lpLibFileName, OVRTargetAPI targetAPI )
-{
- //Sleep(10000);
- if( IN_COMPATIBILITY_MODE() )
- {
- return (*oldProcA)( lpLibFileName );
- }
-
- UNREFERENCED_PARAMETER( targetAPI );
-
- HMODULE result = NULL;
-
- result = (*oldProcA)( UMFilter );
-
- if( result )
- {
- PreloadLibraryFn loadFunc = (PreloadLibraryFn)GetProcAddress( result, "PreloadLibrary" );
- if( loadFunc )
- {
- HRESULT localRes = (*loadFunc)( oldProcA, lpLibFileName, &appDriver );
- if( localRes != S_OK )
- result = NULL;
- }
- }
-
- if( !result )
- {
- MessageBox(nullptr, L"Unable to find the Rift Display Driver.", L"Configuration Error", MB_OK | MB_ICONERROR);
- result = (*oldProcA)( lpLibFileName );
- }
- return result;
-}
-
-static HMODULE
- WINAPI
- OVRLoadLibraryA(
- __in LPCSTR lpLibFileName
- )
-{
- OVRTargetAPI targetAPI = DirectX;
- bool needShim = checkForOverride( lpLibFileName, targetAPI );
- if( !needShim )
- return (*oldProcA)( lpLibFileName );
-
- return createShim( lpLibFileName, targetAPI );
-}
-
-static HMODULE
- WINAPI
- OVRLoadLibraryW(
- __in LPCWSTR lpLibFileName
- )
-{
- WIDE_TO_MB(lpLibFileName); // Convert lpLibFileName -> lpLibFileName_cstr
-
- OVRTargetAPI targetAPI = DirectX;
-
- bool needShim = checkForOverride( lpLibFileName_cstr, targetAPI );
- if( !needShim )
- return (*oldProcW)( lpLibFileName );
-
- return createShim( lpLibFileName_cstr, targetAPI );
-}
-
-static HMODULE
- WINAPI
- OVRLoadLibraryExA(
- __in LPCSTR lpLibFileName,
- __reserved HANDLE hFile,
- __in DWORD dwFlags
-
- )
-{
- OVRTargetAPI targetAPI = DirectX;
-
- bool needShim = checkForOverride( lpLibFileName, targetAPI );
- if( !needShim )
- return (*oldProcExA)( lpLibFileName, hFile, dwFlags );
-
- // FIXME: Don't throw away the flags parameter
- return createShim( lpLibFileName, targetAPI );
-}
-
-static HMODULE
- WINAPI
- OVRLoadLibraryExW(
- __in LPCWSTR lpLibFileName,
- __reserved HANDLE hFile,
- __in DWORD dwFlags
- )
-{
- WIDE_TO_MB(lpLibFileName); // Convert lpLibFileName -> lpLibFileName_cstr
-
- OVRTargetAPI targetAPI = DirectX;
-
- bool needShim = checkForOverride( lpLibFileName_cstr, targetAPI );
- if( !needShim )
- return (*oldProcExW)( lpLibFileName, hFile, dwFlags );
-
- // FIXME: Don't throw away the flags parameter
- return createShim( lpLibFileName_cstr, targetAPI );
-}
-
-static BOOL WINAPI OVRGetModuleHandleExA(
- __in DWORD dwFlags,
- __in_opt LPCSTR lpModuleName,
- __out HMODULE *phModule
- )
-{
- OVRTargetAPI targetAPI = DirectX;
-
- bool needShim = checkForOverride( lpModuleName, targetAPI );
- if( !needShim )
- {
- return (*oldProcModExA)( dwFlags, lpModuleName, phModule );
- }
-
- *phModule = createShim( lpModuleName, targetAPI );
-
- return TRUE;
-}
-
-static BOOL WINAPI OVRGetModuleHandleExW(
- __in DWORD dwFlags,
- __in_opt LPCWSTR lpModuleName,
- __out HMODULE *phModule
- )
-{
- WIDE_TO_MB(lpModuleName); // Convert lpModuleName -> lpModuleName_cstr
-
- OVRTargetAPI targetAPI = DirectX;
-
- bool needShim = checkForOverride( lpModuleName_cstr, targetAPI );
- if( !needShim )
- {
- return (*oldProcModExW)( dwFlags, lpModuleName, phModule );
- }
-
- *phModule = createShim( lpModuleName_cstr, targetAPI );
-
- return TRUE;
-}
-
-#ifdef _AMD64_
-static void restoreFunction( PROC pfnHookAPIAddr, PBYTE oldData )
-{
- static const LONGLONG addressSize = sizeof(PROC);
- static const LONGLONG jmpSize = addressSize + 6;
-
- DWORD oldProtect;
- VirtualProtect((LPVOID)pfnHookAPIAddr, OLD_DATA_BACKUP_SIZE,
- PAGE_EXECUTE_READWRITE, &oldProtect);
-
- memcpy(pfnHookAPIAddr, oldData, OLD_DATA_BACKUP_SIZE);
-
- VirtualProtect((LPVOID)pfnHookAPIAddr, OLD_DATA_BACKUP_SIZE, oldProtect, NULL);
-}
-
-static void setFunction( PROC pfnHookAPIAddr, PROC replacementFunction, PBYTE oldData )
-{
- static const LONGLONG addressSize = sizeof(PROC);
- static const LONGLONG jmpSize = addressSize + 6;
-
- INT_PTR jumpOffset = (INT_PTR)replacementFunction;
-
- DWORD oldProtect;
- VirtualProtect((LPVOID)pfnHookAPIAddr, OLD_DATA_BACKUP_SIZE,
- PAGE_EXECUTE_READWRITE, &oldProtect);
-
- memcpy(oldData, pfnHookAPIAddr, OLD_DATA_BACKUP_SIZE);
-
- PBYTE functionData = (PBYTE)pfnHookAPIAddr;
- functionData[0] = 0xff; // JMP [RIP+0]
- functionData[1] = 0x25; //
- functionData[2] = 0x00; //
- functionData[3] = 0x00; //
- functionData[4] = 0x00; //
- functionData[5] = 0x00; //
- memcpy( functionData + 6, &jumpOffset, sizeof( INT_PTR ) );
-
- VirtualProtect((LPVOID)pfnHookAPIAddr, OLD_DATA_BACKUP_SIZE, oldProtect, NULL);
-}
-#else
-static void restoreFunction( PROC pfnHookAPIAddr, PBYTE oldData )
-{
- static const LONGLONG addressSize = sizeof(PROC);
- static const LONGLONG jmpSize = addressSize + 1;
-
- DWORD oldProtect;
- VirtualProtect((LPVOID)pfnHookAPIAddr, jmpSize,
- PAGE_EXECUTE_READWRITE, &oldProtect);
-
- memcpy(pfnHookAPIAddr, oldData, jmpSize);
-
- VirtualProtect((LPVOID)pfnHookAPIAddr, jmpSize, oldProtect, NULL);
-}
-
-static void setFunction( PROC pfnHookAPIAddr, PROC replacementFunction, PBYTE oldData )
-{
- static const LONGLONG addressSize = sizeof(PROC);
- static const LONGLONG jmpSize = addressSize + 1;
-
- INT_PTR jumpOffset = (INT_PTR)replacementFunction - (INT_PTR)pfnHookAPIAddr - jmpSize;
-
- DWORD oldProtect;
- VirtualProtect((LPVOID)pfnHookAPIAddr, jmpSize,
- PAGE_EXECUTE_READWRITE, &oldProtect);
-
- memcpy(oldData, pfnHookAPIAddr, jmpSize);
-
- PBYTE functionData = (PBYTE)pfnHookAPIAddr;
- memcpy( oldData, functionData, jmpSize );
- functionData[0] = 0xe9;
- memcpy( functionData + 1, &jumpOffset, sizeof( INT_PTR ) );
-
- VirtualProtect((LPVOID)pfnHookAPIAddr, jmpSize, oldProtect, NULL);
-}
-#endif
-
-static BOOL WINAPI OVRLocalIsInitializingDisplay( PVOID context, UINT width, UINT height )
-{
- UINT expectedWidth, expectedHeight, rotation;
-
- OVRExpectedResolution( context, &expectedWidth, &expectedHeight, &rotation );
-
- if( appDriver.pfnActiveAPIVersion )
- apiVersion = (*appDriver.pfnActiveAPIVersion)( context );
-
- switch( apiVersion )
- {
- case 1: // OpenGL
- case 10: // DirectX 1X
- if( width == expectedWidth && height == expectedHeight )
- return TRUE;
- break;
- case 9: // DirectX 9
- if( rotation == 90 || rotation == 270 )
- {
- if( width == expectedHeight && height == expectedWidth )
- return TRUE;
- }
- else
- {
- if( width == expectedWidth && height == expectedHeight )
- return TRUE;
- }
- break;
- default:
- break;
- }
-
- return FALSE;
-}
-
-
-HRESULT APIENTRY OVRDirect3DCreate9Ex(UINT SDKVersion, void** aDevice)
-{
- apiVersion = 9;
-
- HRESULT result = S_OK;
-
- restoreFunction( (PROC)oldDirectX9ExCreate, oldDirectX9ExCreateData );
-
- if (IN_COMPATIBILITY_MODE())
- {
- result = (*oldDirectX9ExCreate)(SDKVersion, aDevice);
- }
- else
- {
- WinDirect3DCreate9Ex createFunction = (WinDirect3DCreate9Ex)GetProcAddress(rtFilterModule, "Direct3DCreate9Ex");
- result = (*createFunction)(SDKVersion, aDevice);
- }
-
- setFunction( (PROC)oldDirectX9ExCreate, (PROC)OVRDirect3DCreate9Ex, oldDirectX9ExCreateData );
-
- printf("%s result 0x%x\n", __FUNCTION__, result);
-
- return result;
-}
-
-void* APIENTRY OVRDirect3DCreate9(UINT SDKVersion)
-{
- void* result = NULL;
-
- OVRDirect3DCreate9Ex( SDKVersion, &result );
-
- return result;
-}
-
-HRESULT APIENTRY OVRCreateDXGIFactory(
- __in REFIID riid,
- __out void **ppFactory
- )
-{
- HRESULT result = E_FAIL;
-
- restoreFunction( (PROC)oldCreateDXGIFactory, oldCreateDXGIFactoryData );
-
- if (IN_COMPATIBILITY_MODE())
- {
- result = (*oldCreateDXGIFactory)(riid, ppFactory);
- }
- else
- {
- WinCreateDXGIFactory createFunction = (WinCreateDXGIFactory)GetProcAddress(rtFilterModule, "CreateDXGIFactory");
- result = (*createFunction)(riid, ppFactory);
- }
-
- setFunction( (PROC)oldCreateDXGIFactory, (PROC)OVRCreateDXGIFactory, oldCreateDXGIFactoryData );
-
- printf("%s result 0x%x\n", __FUNCTION__, result);
-
- return result;
-}
-
-HRESULT APIENTRY OVRCreateDXGIFactory1(
- __in REFIID riid,
- __out void **ppFactory
- )
-{
- HRESULT result = E_FAIL;
-
- restoreFunction( (PROC)oldCreateDXGIFactory1, oldCreateDXGIFactory1Data );
-
- if (IN_COMPATIBILITY_MODE())
- {
- result = (*oldCreateDXGIFactory1)(riid, ppFactory);
- }
- else
- {
- WinCreateDXGIFactory1 createFunction = (WinCreateDXGIFactory1)GetProcAddress(rtFilterModule, "CreateDXGIFactory1");
- result = (*createFunction)(riid, ppFactory);
- }
-
- setFunction( (PROC)oldCreateDXGIFactory1, (PROC)OVRCreateDXGIFactory1, oldCreateDXGIFactory1Data );
-
- printf("%s result 0x%x\n", __FUNCTION__, result);
-
- return result;
-}
-
-HRESULT APIENTRY OVRCreateDXGIFactory2(
- __in UINT flags,
- __in const IID &riid,
- __out void **ppFactory
- )
-{
- HRESULT result = E_FAIL;
-
- restoreFunction( (PROC)oldCreateDXGIFactory2, oldCreateDXGIFactory2Data );
-
- if (IN_COMPATIBILITY_MODE())
- {
- result = (*oldCreateDXGIFactory2)(flags, riid, ppFactory);
- }
- else
- {
- WinCreateDXGIFactory2 createFunction = (WinCreateDXGIFactory2)GetProcAddress(rtFilterModule, "CreateDXGIFactory2");
- result = (*createFunction)(flags, riid, ppFactory);
- }
-
- setFunction( (PROC)oldCreateDXGIFactory2, (PROC)OVRCreateDXGIFactory2, oldCreateDXGIFactory2Data );
-
- printf("%s result 0x%x\n", __FUNCTION__, result);
-
- return result;
-}
-
-static PROC SetProcAddressDirect(
- __in HINSTANCE hInstance,
- __in LPCSTR lpProcName,
- __in PROC newFunction,
- __inout BYTE* oldData
- )
-{
- static const LONGLONG addressSize = sizeof(PROC);
- static const LONGLONG jmpSize = addressSize + 1;
-
- PROC result = NULL;
-
- PROC pfnHookAPIAddr = GetProcAddress( hInstance, lpProcName );
-
- if( pfnHookAPIAddr )
- {
- result = pfnHookAPIAddr;
-
- setFunction( pfnHookAPIAddr, newFunction, oldData );
- }
-
- return result;
-}
-
-static PROC SetProcAddressA(
- __in HINSTANCE targetModule,
- __in LPCSTR lpLibFileName,
- __in LPCSTR lpProcName,
- __in PROC newFunction
- )
-{
- HMODULE hModule = LoadLibraryA( lpLibFileName );
- if(hModule == NULL)
- return NULL;
-
- // To do: call FreeLibrary(hModule) at the appropriate time.
- PROC pfnHookAPIAddr = GetProcAddress(hModule, lpProcName );
-
- HINSTANCE hInstance = targetModule;
-
- ULONG ulSize;
- PIMAGE_IMPORT_DESCRIPTOR pImportDesc =
- (PIMAGE_IMPORT_DESCRIPTOR)ImageDirectoryEntryToData(
- hInstance,
- TRUE,
- IMAGE_DIRECTORY_ENTRY_IMPORT,
- &ulSize
- );
-
- while (pImportDesc->Name)
- {
- PSTR pszModName = (PSTR)((PBYTE) hInstance + pImportDesc->Name);
- if (_stricmp(pszModName, lpLibFileName) == 0)
- break;
- pImportDesc++;
- }
-
- PIMAGE_THUNK_DATA pThunk =
- (PIMAGE_THUNK_DATA)((PBYTE) hInstance + pImportDesc->FirstThunk);
-
- while (pThunk->u1.Function)
- {
- PROC* ppfn = (PROC*) &pThunk->u1.Function;
- BOOL bFound = (*ppfn == pfnHookAPIAddr);
-
- if (bFound)
- {
- MEMORY_BASIC_INFORMATION mbi;
- VirtualQuery(
- ppfn,
- &mbi,
- sizeof(MEMORY_BASIC_INFORMATION)
- );
- VirtualProtect(
- mbi.BaseAddress,
- mbi.RegionSize,
- PAGE_READWRITE,
- &mbi.Protect);
-
- *ppfn = *newFunction;
-
- DWORD dwOldProtect;
- VirtualProtect(
- mbi.BaseAddress,
- mbi.RegionSize,
- mbi.Protect,
- &dwOldProtect
- );
- break;
- }
- pThunk++;
- }
-
- return pfnHookAPIAddr;
-}
-
-
-bool checkUMDriverOverrides(void* context)
-{
- lastContext = context;
- if (oldProcA != NULL)
- return true;
-
- PreloadLibraryRTFn loadFunc = NULL;
- for( int i = 0; i < ShimCountMax; ++i )
- {
- HINSTANCE hInst = NULL;
- try
- {
- hInst = LoadLibraryA(dllList[i]);
- }
- catch(...)
- {
- }
-
- oldLoaderInstances[i] = hInst;
-
- if (hInst == NULL)
- continue;
-
- ShimedLibraries libCount = (ShimedLibraries)i;
- switch( libCount )
- {
- case ShimLibDXGI:
- oldCreateDXGIFactory = (WinCreateDXGIFactory)SetProcAddressDirect( hInst, "CreateDXGIFactory", (PROC)OVRCreateDXGIFactory, oldCreateDXGIFactoryData );
- oldCreateDXGIFactory1 = (WinCreateDXGIFactory1)SetProcAddressDirect( hInst, "CreateDXGIFactory1", (PROC)OVRCreateDXGIFactory1, oldCreateDXGIFactory1Data );
- oldCreateDXGIFactory2 = (WinCreateDXGIFactory2)SetProcAddressDirect( hInst, "CreateDXGIFactory2", (PROC)OVRCreateDXGIFactory2, oldCreateDXGIFactory2Data );
- break;
- case ShimLibD3D9:
- oldDirectX9Create = (WinDirect3DCreate9)SetProcAddressDirect( hInst, "Direct3DCreate9", (PROC)OVRDirect3DCreate9, oldDirectX9CreateData );
- oldDirectX9ExCreate = (WinDirect3DCreate9Ex)SetProcAddressDirect( hInst, "Direct3DCreate9Ex", (PROC)OVRDirect3DCreate9Ex, oldDirectX9ExCreateData );
- break;
- default:
- break;
- }
-
- for (int j = 0; j < NUM_LOADER_LIBS; ++j)
- {
- const char* loaderLibrary = loaderLibraryList[j];
-
- PROC temp = NULL;
- temp = SetProcAddressA(hInst, loaderLibrary, "LoadLibraryA", (PROC)OVRLoadLibraryA);
- if (!oldProcA)
- {
- oldProcA = (WinLoadLibraryA)temp;
- }
- oldLoaderProcA[i][j] = temp;
-
- temp = SetProcAddressA(hInst, loaderLibrary, "LoadLibraryW", (PROC)OVRLoadLibraryW);
- if (!oldProcW)
- {
- oldProcW = (WinLoadLibraryW)temp;
- }
- oldLoaderProcW[i][j] = temp;
-
- temp = SetProcAddressA(hInst, loaderLibrary, "LoadLibraryExA", (PROC)OVRLoadLibraryExA);
- if (!oldProcExA)
- {
- oldProcExA = (WinLoadLibraryExA)temp;
- }
- oldLoaderProcExA[i][j] = temp;
-
- temp = SetProcAddressA(hInst, loaderLibrary, "LoadLibraryExW", (PROC)OVRLoadLibraryExW);
- if (!oldProcExW)
- {
- oldProcExW = (WinLoadLibraryExW)temp;
- }
- oldLoaderProcExW[i][j] = temp;
-
- temp = SetProcAddressA(hInst, loaderLibrary, "GetModuleHandleExA", (PROC)OVRGetModuleHandleExA);
- if (!oldProcModExA)
- {
- oldProcModExA = (WinGetModuleHandleExA)temp;
- }
- oldLoaderProcModExA[i][j] = temp;
-
- temp = SetProcAddressA(hInst, loaderLibrary, "GetModuleHandleExW", (PROC)OVRGetModuleHandleExW);
- if (!oldProcModExW)
- {
- oldProcModExW = (WinGetModuleHandleExW)temp;
- }
- oldLoaderProcModExW[i][j] = temp;
- }
-
- if (loadFunc == NULL)
- {
- loadFunc = (PreloadLibraryRTFn)GetProcAddress(hInst, "PreloadLibraryRT");
- }
- }
-
- rtFilterModule = oldProcA ? (*oldProcA)(RTFilter) : NULL;
-
- IsCreatingBackBuffer backBufferFunc = NULL;
- ShouldVSync shouldVSyncFunc = NULL;
- GetRTFilterVersion getRTFilterVersionFunc = NULL;
-
- if (rtFilterModule != NULL)
- {
- loadFunc = (PreloadLibraryRTFn)GetProcAddress(rtFilterModule, "PreloadLibraryRT");
- backBufferFunc = (IsCreatingBackBuffer)GetProcAddress(rtFilterModule, "OVRIsCreatingBackBuffer");
- shouldVSyncFunc = (ShouldVSync)GetProcAddress(rtFilterModule, "OVRShouldVSync");
- getRTFilterVersionFunc = (GetRTFilterVersion)GetProcAddress(rtFilterModule, "OVRGetRTFilterVersion");
- }
-
- if (loadFunc == NULL)
- {
- MessageBox(nullptr, L"Unable to load the Oculus Display Driver. Please reinstall the Oculus Runtime.", L"Configuration Error", MB_OK | MB_ICONERROR);
- return false;
- }
-
- if (getRTFilterVersionFunc == NULL)
- {
- WCHAR message[1000] = {};
- swprintf_s(message, L"This app requires the %d.%d.%d version of the Oculus Runtime.", OVR_MAJOR_VERSION, OVR_MINOR_VERSION, OVR_BUILD_VERSION);
- MessageBox(nullptr, message, L"Configuration Error", MB_OK | MB_ICONERROR);
- return false;
- }
-
- // Verify that we are running with the appropriate display driver
- {
- const ULONG rtFilterVersion = (*getRTFilterVersionFunc)();
- const ULONG rtFilterMajor = OVR_GET_VERSION_MAJOR(rtFilterVersion);
- const ULONG rtFilterMinor = OVR_GET_VERSION_MINOR(rtFilterVersion);
-
- if ((rtFilterMajor != OVR_RTFILTER_VERSION_MAJOR) || (rtFilterMinor < OVR_RTFILTER_VERSION_MINOR))
- {
- WCHAR message[1000] = {};
- swprintf_s(message, L"This app requires the %d.%d.%d version of the Oculus Runtime.", OVR_MAJOR_VERSION, OVR_MINOR_VERSION, OVR_BUILD_VERSION);
- MessageBox(nullptr, message, L"Configuration Error", MB_OK | MB_ICONERROR);
- return false;
- }
- }
-
- appDriver.version = OVR_RENDER_SHIM_VERSION_MAJOR;
- appDriver.context = lastContext;
-
-// appDriver.pfnInitializingDisplay = OVRIsInitializingDisplay;
- appDriver.pfnInitializingDisplay = OVRLocalIsInitializingDisplay;
- appDriver.pfnRiftForContext = OVRRiftForContext;
- appDriver.pfnCloseRiftForContext = OVRCloseRiftForContext;
- appDriver.pfnWindowDisplayResolution = OVRWindowDisplayResolution;
- appDriver.pfnShouldEnableDebug = OVRShouldEnableDebug;
- appDriver.pfnIsCreatingBackBuffer = (backBufferFunc == NULL) ? OVRIsCreatingBackBuffer : backBufferFunc;
- appDriver.pfnShouldVSync = (shouldVSyncFunc == NULL) ? OVRShouldVSync : shouldVSyncFunc;
- appDriver.pfnExpectedResolution = OVRExpectedResolution;
- appDriver.pfnMirroringEnabled = OVRMirroringEnabled;
- appDriver.pfnGetWindowForContext = OVRGetWindowForContext;
- appDriver.pfnPresentRiftOnContext = OVRShouldPresentOnContext;
-
- appDriver.pfnDirect3DCreate9 = oldDirectX9Create;
- appDriver.pfnDirect3DCreate9Ex = oldDirectX9ExCreate;
- appDriver.pfnCreateDXGIFactory = oldCreateDXGIFactory;
- appDriver.pfnCreateDXGIFactory1 = oldCreateDXGIFactory1;
- appDriver.pfnCreateDXGIFactory2 = oldCreateDXGIFactory2;
-
- (*loadFunc)( &appDriver );
-
- return true;
-}
-
-void clearUMDriverOverrides()
-{
- if (oldProcA != NULL)
- {
- // Unpatch all the things.
-
- if (oldCreateDXGIFactory)
- {
- restoreFunction((PROC)oldCreateDXGIFactory, oldCreateDXGIFactoryData);
- }
- if (oldCreateDXGIFactory1)
- {
- restoreFunction((PROC)oldCreateDXGIFactory1, oldCreateDXGIFactory1Data);
- }
- if (oldCreateDXGIFactory2)
- {
- restoreFunction((PROC)oldCreateDXGIFactory2, oldCreateDXGIFactory2Data);
- }
- if (oldDirectX9Create)
- {
- restoreFunction((PROC)oldDirectX9Create, oldDirectX9CreateData);
- }
- if (oldDirectX9ExCreate)
- {
- restoreFunction((PROC)oldDirectX9ExCreate, oldDirectX9ExCreateData);
- }
- if (oldCreateDXGIFactory2)
- {
- restoreFunction((PROC)oldCreateDXGIFactory2, oldCreateDXGIFactory2Data);
- }
-
- for (int i = 0; i < ShimCountMax; ++i)
- {
- HINSTANCE hInst = oldLoaderInstances[i];
-
- if (hInst != NULL)
- {
- for (int j = 0; j < NUM_LOADER_LIBS; ++j)
- {
- const char* loaderLibrary = loaderLibraryList[j];
-
- if (oldLoaderProcA[j])
- {
- SetProcAddressA(hInst, loaderLibrary, "LoadLibraryA", oldLoaderProcA[i][j]);
- }
- if (oldLoaderProcW[j])
- {
- SetProcAddressA(hInst, loaderLibrary, "LoadLibraryW", oldLoaderProcW[i][j]);
- }
- if (oldLoaderProcExA[j])
- {
- SetProcAddressA(hInst, loaderLibrary, "LoadLibraryExA", oldLoaderProcExA[i][j]);
- }
- if (oldLoaderProcExW[j])
- {
- SetProcAddressA(hInst, loaderLibrary, "LoadLibraryExW", oldLoaderProcExW[i][j]);
- }
- if (oldLoaderProcModExA[j])
- {
- SetProcAddressA(hInst, loaderLibrary, "GetModuleHandleExA", oldLoaderProcModExA[i][j]);
- }
- if (oldLoaderProcModExW[j])
- {
- SetProcAddressA(hInst, loaderLibrary, "GetModuleHandleExW", oldLoaderProcModExW[i][j]);
- }
- }
-
- FreeLibrary(hInst);
- }
- }
-
- if (rtFilterModule != NULL)
- {
- LPFNCANUNLOADNOW pfnCanUnloadNow = (LPFNCANUNLOADNOW)GetProcAddress(rtFilterModule, "DllCanUnloadNow");
- if (pfnCanUnloadNow && pfnCanUnloadNow() == S_OK)
- {
- FreeLibrary(rtFilterModule);
- rtFilterModule = NULL;
- }
- }
-
- oldProcA = NULL;
- oldProcExA = NULL;
- oldProcW = NULL;
- oldProcExW = NULL;
- oldProcModExA = NULL;
- oldProcModExW = NULL;
- oldDirectX9Create = NULL;
- oldDirectX9ExCreate = NULL;
- oldCreateDXGIFactory = NULL;
- oldCreateDXGIFactory1 = NULL;
- oldCreateDXGIFactory2 = NULL;
- lastContext = NULL;
- }
-}
+/************************************************************************************ + +Filename : OVR_Win32_DisplayShim.cpp +Content : Shared static functions for inclusion that allow for an application + to inject the usermode driver into an application +Created : March 21, 2014 +Authors : Dean Beeler + +Copyright : Copyright 2014 Oculus VR, LLC All Rights reserved. + +Use of this software is subject to the terms of the Oculus Inc license +agreement provided at the time of installation or download, or which +otherwise accompanies this software in either electronic or hard copy form. + +************************************************************************************/ + +#include "../../Include/OVR_Version.h" + +#ifndef AVOID_LIB_OVR +#include "Kernel/OVR_Types.h" // Without this we can get warnings (due to VC++ bugs) about _malloca being redefined, and miss LibOVR overrides. +#include "Kernel/OVR_Win32_IncludeWindows.h" +#else +#include <windows.h> +#ifndef NTSTATUS +#define NTSTATUS DWORD +#endif +#endif + +#include <ObjBase.h> + +#include <DbgHelp.h> +#include "OVR_Win32_Dxgi_Display.h" +#include "OVR_Win32_ShimVersion.h" + +#if AVOID_LIB_OVR +#define IN_COMPATIBILITY_MODE() (0) +#else +#include "OVR_Win32_Display.h" +#define IN_COMPATIBILITY_MODE() OVR::Display::InCompatibilityMode() +#endif + +#pragma comment(lib, "DbgHelp.lib") + +#ifndef alloca +#include <malloc.h> // alloca +#endif + +#define WIDE_TO_MB(wideString) \ + int wideString ## _slen = (int)wcslen(wideString); \ + char* wideString ## _cstr = (char*)alloca(wideString ## _slen * 2); \ + int count = WideCharToMultiByte(GetACP(), 0, wideString, -1, wideString ## _cstr, wideString ## _slen * 2, NULL, NULL); \ + wideString ## _cstr[count] = '\0'; + +// Forward declarations +// These functions are implemented in OVR_Win32_DisplayDevice.cpp. + +BOOL WINAPI OVRIsInitializingDisplay( PVOID context, UINT width, UINT height ); +BOOL WINAPI OVRIsCreatingBackBuffer( PVOID context ); +BOOL WINAPI OVRShouldVSync( ); +ULONG WINAPI OVRRiftForContext( PVOID context, HANDLE driverHandle ); +BOOL WINAPI OVRCloseRiftForContext( PVOID context, HANDLE driverHandle, ULONG rift ); +BOOL WINAPI OVRWindowDisplayResolution( PVOID context, UINT* width, UINT* height, + UINT* titleHeight, UINT* borderWidth, + BOOL* vsyncEnabled ); +BOOL WINAPI OVRExpectedResolution( PVOID context, UINT* width, UINT* height, UINT* rotationInDegrees ); +BOOL WINAPI OVRShouldEnableDebug(); +BOOL WINAPI OVRMirroringEnabled( PVOID context ); +HWND WINAPI OVRGetWindowForContext(PVOID context); +BOOL WINAPI OVRShouldPresentOnContext(PVOID context); + +static const char* GFX_DRIVER_KEY_FMT = "SYSTEM\\CurrentControlSet\\Control\\Class\\{4d36e968-e325-11ce-bfc1-08002be10318}\\%04d"; +#ifdef _WIN64 +static const char* RTFilter = "OVRDisplayRT64.dll"; +static const char* UMFilter = "OVRDisplay64.dll"; +#else +static const char* RTFilter = "OVRDisplayRT32.dll"; +static const char* UMFilter = "OVRDisplay32.dll"; +#endif +static const char* OptimusDrivers = "nvumdshimx.dll nvumdshim.dll"; + +typedef enum OVRTargetAPI +{ + DirectX, + OpenGL +}; + +static PVOID lastContext = NULL; +LINK_APPLICATION_DRIVER appDriver = {0}; +static INT apiVersion = 10; + +static CHAR* ReadRegStr(HKEY keySub, const char* keyName, const char* valName) +{ + CHAR *val = NULL; + REGSAM access = KEY_READ; + HKEY hKey; + +TryAgainWOW64: + NTSTATUS res = RegOpenKeyExA( keySub, keyName, 0, access, &hKey ); + if ( res == ERROR_SUCCESS ) + { + DWORD valLen; + res = RegQueryValueExA( hKey, valName, NULL, NULL, NULL, &valLen ); + if( res == ERROR_SUCCESS ) { + val = (CHAR*)calloc( valLen + 1, sizeof(CHAR) ); + res = RegQueryValueExA( hKey, valName, NULL, NULL, (LPBYTE)val, &valLen ); + + if( res == ERROR_SUCCESS ) + { + CHAR* byte = val; + for( DWORD j = 0; j < valLen; ++j ) + { + if( byte[j] == 0 ) + byte[j] = ' '; + } + } + else + { + free( val ); + val = NULL; + } + } + RegCloseKey( hKey ); + } + + if( res == ERROR_FILE_NOT_FOUND && keySub == HKEY_LOCAL_MACHINE && access == KEY_READ ) { +#ifdef _WIN64 + access = KEY_READ | KEY_WOW64_32KEY; +#else + access = KEY_READ | KEY_WOW64_64KEY; +#endif + goto TryAgainWOW64; + } + return val; +} + +#define OLD_DATA_BACKUP_SIZE 16 + +static WinLoadLibraryA oldProcA = NULL; // Note: This is used to indicate that the shim is in place +static WinLoadLibraryExA oldProcExA = NULL; +static WinLoadLibraryW oldProcW = NULL; +static WinLoadLibraryExW oldProcExW = NULL; +static WinGetModuleHandleExA oldProcModExA = NULL; +static WinGetModuleHandleExW oldProcModExW = NULL; +static WinDirect3DCreate9 oldDirectX9Create = NULL; +static BYTE oldDirectX9CreateData[OLD_DATA_BACKUP_SIZE]; +static WinDirect3DCreate9Ex oldDirectX9ExCreate = NULL; +static BYTE oldDirectX9ExCreateData[OLD_DATA_BACKUP_SIZE]; +static WinCreateDXGIFactory oldCreateDXGIFactory = NULL; +static BYTE oldCreateDXGIFactoryData[OLD_DATA_BACKUP_SIZE]; +static WinCreateDXGIFactory1 oldCreateDXGIFactory1 = NULL; +static BYTE oldCreateDXGIFactory1Data[OLD_DATA_BACKUP_SIZE]; +static WinCreateDXGIFactory2 oldCreateDXGIFactory2 = NULL; +static BYTE oldCreateDXGIFactory2Data[OLD_DATA_BACKUP_SIZE]; + +#define NUM_LOADER_LIBS 4 + +static const char* loaderLibraryList[NUM_LOADER_LIBS] = { + "kernel32.dll", + "api-ms-win-core-libraryloader-l1-2-0.dll", + "api-ms-win-core-libraryloader-l1-1-0.dll", + "api-ms-win-core-libraryloader-l1-1-1.dll" +}; + +enum ShimedLibraries +{ + ShimLibDXGI = 0, + ShimLibD3D9 = 1, + ShimLibD3D11 = 2, + ShimLibDXGIDebug = 3, + ShimLibD3D10Core = 4, + ShimLibD3D10 = 5, + ShimLibGL = 6, + ShimCountMax = 7 +}; + +static const char* dllList[ShimCountMax] = { + "dxgi.dll", + "d3d9.dll", + "d3d11.dll", + "dxgidebug.dll", + "d3d10core.dll", + "d3d10.dll", + "opengl32.dll" +}; + +static HINSTANCE oldLoaderInstances[ShimCountMax] = { NULL }; +static PROC oldLoaderProcA[ShimCountMax][NUM_LOADER_LIBS] = { { NULL } }; +static PROC oldLoaderProcW[ShimCountMax][NUM_LOADER_LIBS] = { { NULL } }; +static PROC oldLoaderProcExA[ShimCountMax][NUM_LOADER_LIBS] = { { NULL } }; +static PROC oldLoaderProcExW[ShimCountMax][NUM_LOADER_LIBS] = { { NULL } }; +static PROC oldLoaderProcModExA[ShimCountMax][NUM_LOADER_LIBS] = { { NULL } }; +static PROC oldLoaderProcModExW[ShimCountMax][NUM_LOADER_LIBS] = { { NULL } }; + +static HMODULE rtFilterModule = NULL; + +static bool checkForOverride( LPCSTR libFileName, OVRTargetAPI& targetApi ) +{ + for (int i=0; ; i++) + { + CHAR keyString[256] = {0}; + + sprintf_s( keyString, 256, GFX_DRIVER_KEY_FMT, i ); + + CHAR* infSection = ReadRegStr( HKEY_LOCAL_MACHINE, keyString, "InfSection" ); + + // No provider name means we're out of display enumerations + if( infSection == NULL ) + break; + + free(infSection); + + // Check 64-bit driver names followed by 32-bit driver names + const char* driverKeys[] = {"UserModeDriverName", "UserModeDriverNameWoW", "OpenGLDriverName", "OpenGLDriverNameWoW", "InstalledDisplayDrivers" }; + for( int j = 0; j < 6; ++j ) + { + CHAR userModeList[4096] = {0}; + + switch(j) + { + case 5: + strcpy_s( userModeList, 4095, OptimusDrivers ); + break; + default: + { + CHAR* regString = ReadRegStr( HKEY_LOCAL_MACHINE, keyString, driverKeys[j] ); + if( regString ) + { + strcpy_s( userModeList, 4095, regString ); + free( regString ); + } + + } + break; + } + + char *nextToken = NULL; + + if( userModeList ) + { + char* first = strtok_s( userModeList, " ", &nextToken ); + while( first ) + { + if( strstr( libFileName, first ) != 0 ) + { + if( j < 2 ) + targetApi = DirectX; + else + targetApi = OpenGL; + + return true; + } + first = strtok_s( NULL, " ", &nextToken ); + } + } + } + } + + return false; +} + +static HMODULE createShim( LPCSTR lpLibFileName, OVRTargetAPI targetAPI ) +{ + //Sleep(10000); + if( IN_COMPATIBILITY_MODE() ) + { + return (*oldProcA)( lpLibFileName ); + } + + UNREFERENCED_PARAMETER( targetAPI ); + + HMODULE result = NULL; + + result = (*oldProcA)( UMFilter ); + + if( result ) + { + PreloadLibraryFn loadFunc = (PreloadLibraryFn)GetProcAddress( result, "PreloadLibrary" ); + if( loadFunc ) + { + HRESULT localRes = (*loadFunc)( oldProcA, lpLibFileName, &appDriver ); + if( localRes != S_OK ) + result = NULL; + } + } + + if( !result ) + { + MessageBox(nullptr, L"Unable to find the Rift Display Driver.", L"Configuration Error", MB_OK | MB_ICONERROR); + result = (*oldProcA)( lpLibFileName ); + } + return result; +} + +static HMODULE + WINAPI + OVRLoadLibraryA( + __in LPCSTR lpLibFileName + ) +{ + OVRTargetAPI targetAPI = DirectX; + bool needShim = checkForOverride( lpLibFileName, targetAPI ); + if( !needShim ) + return (*oldProcA)( lpLibFileName ); + + return createShim( lpLibFileName, targetAPI ); +} + +static HMODULE + WINAPI + OVRLoadLibraryW( + __in LPCWSTR lpLibFileName + ) +{ + WIDE_TO_MB(lpLibFileName); // Convert lpLibFileName -> lpLibFileName_cstr + + OVRTargetAPI targetAPI = DirectX; + + bool needShim = checkForOverride( lpLibFileName_cstr, targetAPI ); + if( !needShim ) + return (*oldProcW)( lpLibFileName ); + + return createShim( lpLibFileName_cstr, targetAPI ); +} + +static HMODULE + WINAPI + OVRLoadLibraryExA( + __in LPCSTR lpLibFileName, + __reserved HANDLE hFile, + __in DWORD dwFlags + + ) +{ + OVRTargetAPI targetAPI = DirectX; + + bool needShim = checkForOverride( lpLibFileName, targetAPI ); + if( !needShim ) + return (*oldProcExA)( lpLibFileName, hFile, dwFlags ); + + // FIXME: Don't throw away the flags parameter + return createShim( lpLibFileName, targetAPI ); +} + +static HMODULE + WINAPI + OVRLoadLibraryExW( + __in LPCWSTR lpLibFileName, + __reserved HANDLE hFile, + __in DWORD dwFlags + ) +{ + WIDE_TO_MB(lpLibFileName); // Convert lpLibFileName -> lpLibFileName_cstr + + OVRTargetAPI targetAPI = DirectX; + + bool needShim = checkForOverride( lpLibFileName_cstr, targetAPI ); + if( !needShim ) + return (*oldProcExW)( lpLibFileName, hFile, dwFlags ); + + // FIXME: Don't throw away the flags parameter + return createShim( lpLibFileName_cstr, targetAPI ); +} + +static BOOL WINAPI OVRGetModuleHandleExA( + __in DWORD dwFlags, + __in_opt LPCSTR lpModuleName, + __out HMODULE *phModule + ) +{ + OVRTargetAPI targetAPI = DirectX; + + bool needShim = checkForOverride( lpModuleName, targetAPI ); + if( !needShim ) + { + return (*oldProcModExA)( dwFlags, lpModuleName, phModule ); + } + + *phModule = createShim( lpModuleName, targetAPI ); + + return TRUE; +} + +static BOOL WINAPI OVRGetModuleHandleExW( + __in DWORD dwFlags, + __in_opt LPCWSTR lpModuleName, + __out HMODULE *phModule + ) +{ + WIDE_TO_MB(lpModuleName); // Convert lpModuleName -> lpModuleName_cstr + + OVRTargetAPI targetAPI = DirectX; + + bool needShim = checkForOverride( lpModuleName_cstr, targetAPI ); + if( !needShim ) + { + return (*oldProcModExW)( dwFlags, lpModuleName, phModule ); + } + + *phModule = createShim( lpModuleName_cstr, targetAPI ); + + return TRUE; +} + +#ifdef _AMD64_ +static void restoreFunction( PROC pfnHookAPIAddr, PBYTE oldData ) +{ + static const LONGLONG addressSize = sizeof(PROC); + static const LONGLONG jmpSize = addressSize + 6; + + DWORD oldProtect; + VirtualProtect((LPVOID)pfnHookAPIAddr, OLD_DATA_BACKUP_SIZE, + PAGE_EXECUTE_READWRITE, &oldProtect); + + memcpy(pfnHookAPIAddr, oldData, OLD_DATA_BACKUP_SIZE); + + VirtualProtect((LPVOID)pfnHookAPIAddr, OLD_DATA_BACKUP_SIZE, oldProtect, NULL); +} + +static void setFunction( PROC pfnHookAPIAddr, PROC replacementFunction, PBYTE oldData ) +{ + static const LONGLONG addressSize = sizeof(PROC); + static const LONGLONG jmpSize = addressSize + 6; + + INT_PTR jumpOffset = (INT_PTR)replacementFunction; + + DWORD oldProtect; + VirtualProtect((LPVOID)pfnHookAPIAddr, OLD_DATA_BACKUP_SIZE, + PAGE_EXECUTE_READWRITE, &oldProtect); + + memcpy(oldData, pfnHookAPIAddr, OLD_DATA_BACKUP_SIZE); + + PBYTE functionData = (PBYTE)pfnHookAPIAddr; + functionData[0] = 0xff; // JMP [RIP+0] + functionData[1] = 0x25; // + functionData[2] = 0x00; // + functionData[3] = 0x00; // + functionData[4] = 0x00; // + functionData[5] = 0x00; // + memcpy( functionData + 6, &jumpOffset, sizeof( INT_PTR ) ); + + VirtualProtect((LPVOID)pfnHookAPIAddr, OLD_DATA_BACKUP_SIZE, oldProtect, NULL); +} +#else +static void restoreFunction( PROC pfnHookAPIAddr, PBYTE oldData ) +{ + static const LONGLONG addressSize = sizeof(PROC); + static const LONGLONG jmpSize = addressSize + 1; + + DWORD oldProtect; + VirtualProtect((LPVOID)pfnHookAPIAddr, jmpSize, + PAGE_EXECUTE_READWRITE, &oldProtect); + + memcpy(pfnHookAPIAddr, oldData, jmpSize); + + VirtualProtect((LPVOID)pfnHookAPIAddr, jmpSize, oldProtect, NULL); +} + +static void setFunction( PROC pfnHookAPIAddr, PROC replacementFunction, PBYTE oldData ) +{ + static const LONGLONG addressSize = sizeof(PROC); + static const LONGLONG jmpSize = addressSize + 1; + + INT_PTR jumpOffset = (INT_PTR)replacementFunction - (INT_PTR)pfnHookAPIAddr - jmpSize; + + DWORD oldProtect; + VirtualProtect((LPVOID)pfnHookAPIAddr, jmpSize, + PAGE_EXECUTE_READWRITE, &oldProtect); + + memcpy(oldData, pfnHookAPIAddr, jmpSize); + + PBYTE functionData = (PBYTE)pfnHookAPIAddr; + memcpy( oldData, functionData, jmpSize ); + functionData[0] = 0xe9; + memcpy( functionData + 1, &jumpOffset, sizeof( INT_PTR ) ); + + VirtualProtect((LPVOID)pfnHookAPIAddr, jmpSize, oldProtect, NULL); +} +#endif + +static BOOL WINAPI OVRLocalIsInitializingDisplay( PVOID context, UINT width, UINT height ) +{ + UINT expectedWidth, expectedHeight, rotation; + + OVRExpectedResolution( context, &expectedWidth, &expectedHeight, &rotation ); + + if( appDriver.pfnActiveAPIVersion ) + apiVersion = (*appDriver.pfnActiveAPIVersion)( context ); + + switch( apiVersion ) + { + case 1: // OpenGL + case 10: // DirectX 1X + if( width == expectedWidth && height == expectedHeight ) + return TRUE; + break; + case 9: // DirectX 9 + if( rotation == 90 || rotation == 270 ) + { + if( width == expectedHeight && height == expectedWidth ) + return TRUE; + } + else + { + if( width == expectedWidth && height == expectedHeight ) + return TRUE; + } + break; + default: + break; + } + + return FALSE; +} + + +HRESULT APIENTRY OVRDirect3DCreate9Ex(UINT SDKVersion, void** aDevice) +{ + apiVersion = 9; + + HRESULT result = S_OK; + + restoreFunction( (PROC)oldDirectX9ExCreate, oldDirectX9ExCreateData ); + + if (IN_COMPATIBILITY_MODE()) + { + result = (*oldDirectX9ExCreate)(SDKVersion, aDevice); + } + else + { + WinDirect3DCreate9Ex createFunction = (WinDirect3DCreate9Ex)GetProcAddress(rtFilterModule, "Direct3DCreate9Ex"); + result = (*createFunction)(SDKVersion, aDevice); + } + + setFunction( (PROC)oldDirectX9ExCreate, (PROC)OVRDirect3DCreate9Ex, oldDirectX9ExCreateData ); + + printf("%s result 0x%x\n", __FUNCTION__, result); + + return result; +} + +void* APIENTRY OVRDirect3DCreate9(UINT SDKVersion) +{ + void* result = NULL; + + OVRDirect3DCreate9Ex( SDKVersion, &result ); + + return result; +} + +HRESULT APIENTRY OVRCreateDXGIFactory( + __in REFIID riid, + __out void **ppFactory + ) +{ + HRESULT result = E_FAIL; + + restoreFunction( (PROC)oldCreateDXGIFactory, oldCreateDXGIFactoryData ); + + if (IN_COMPATIBILITY_MODE()) + { + result = (*oldCreateDXGIFactory)(riid, ppFactory); + } + else + { + WinCreateDXGIFactory createFunction = (WinCreateDXGIFactory)GetProcAddress(rtFilterModule, "CreateDXGIFactory"); + result = (*createFunction)(riid, ppFactory); + } + + setFunction( (PROC)oldCreateDXGIFactory, (PROC)OVRCreateDXGIFactory, oldCreateDXGIFactoryData ); + + printf("%s result 0x%x\n", __FUNCTION__, result); + + return result; +} + +HRESULT APIENTRY OVRCreateDXGIFactory1( + __in REFIID riid, + __out void **ppFactory + ) +{ + HRESULT result = E_FAIL; + + restoreFunction( (PROC)oldCreateDXGIFactory1, oldCreateDXGIFactory1Data ); + + if (IN_COMPATIBILITY_MODE()) + { + result = (*oldCreateDXGIFactory1)(riid, ppFactory); + } + else + { + WinCreateDXGIFactory1 createFunction = (WinCreateDXGIFactory1)GetProcAddress(rtFilterModule, "CreateDXGIFactory1"); + result = (*createFunction)(riid, ppFactory); + } + + setFunction( (PROC)oldCreateDXGIFactory1, (PROC)OVRCreateDXGIFactory1, oldCreateDXGIFactory1Data ); + + printf("%s result 0x%x\n", __FUNCTION__, result); + + return result; +} + +HRESULT APIENTRY OVRCreateDXGIFactory2( + __in UINT flags, + __in const IID &riid, + __out void **ppFactory + ) +{ + HRESULT result = E_FAIL; + + restoreFunction( (PROC)oldCreateDXGIFactory2, oldCreateDXGIFactory2Data ); + + if (IN_COMPATIBILITY_MODE()) + { + result = (*oldCreateDXGIFactory2)(flags, riid, ppFactory); + } + else + { + WinCreateDXGIFactory2 createFunction = (WinCreateDXGIFactory2)GetProcAddress(rtFilterModule, "CreateDXGIFactory2"); + result = (*createFunction)(flags, riid, ppFactory); + } + + setFunction( (PROC)oldCreateDXGIFactory2, (PROC)OVRCreateDXGIFactory2, oldCreateDXGIFactory2Data ); + + printf("%s result 0x%x\n", __FUNCTION__, result); + + return result; +} + +static PROC SetProcAddressDirect( + __in HINSTANCE hInstance, + __in LPCSTR lpProcName, + __in PROC newFunction, + __inout BYTE* oldData + ) +{ + static const LONGLONG addressSize = sizeof(PROC); + static const LONGLONG jmpSize = addressSize + 1; + + PROC result = NULL; + + PROC pfnHookAPIAddr = GetProcAddress( hInstance, lpProcName ); + + if( pfnHookAPIAddr ) + { + result = pfnHookAPIAddr; + + setFunction( pfnHookAPIAddr, newFunction, oldData ); + } + + return result; +} + +static PROC SetProcAddressA( + __in HINSTANCE targetModule, + __in LPCSTR lpLibFileName, + __in LPCSTR lpProcName, + __in PROC newFunction, + __in PROC origFunction = NULL + ) +{ + HMODULE hModule = LoadLibraryA( lpLibFileName ); + if(hModule == NULL) + return NULL; + + // To do: call FreeLibrary(hModule) at the appropriate time. + PROC pfnHookAPIAddr = (origFunction != NULL) ? origFunction : GetProcAddress(hModule, lpProcName ); + + HINSTANCE hInstance = targetModule; + + ULONG ulSize; + PIMAGE_IMPORT_DESCRIPTOR pImportDesc = + (PIMAGE_IMPORT_DESCRIPTOR)ImageDirectoryEntryToData( + hInstance, + TRUE, + IMAGE_DIRECTORY_ENTRY_IMPORT, + &ulSize + ); + + while (pImportDesc->Name) + { + PSTR pszModName = (PSTR)((PBYTE) hInstance + pImportDesc->Name); + if (_stricmp(pszModName, lpLibFileName) == 0) + break; + pImportDesc++; + } + + PIMAGE_THUNK_DATA pThunk = + (PIMAGE_THUNK_DATA)((PBYTE) hInstance + pImportDesc->FirstThunk); + + while (pThunk->u1.Function) + { + PROC* ppfn = (PROC*) &pThunk->u1.Function; + BOOL bFound = (*ppfn == pfnHookAPIAddr); + + if (bFound) + { + MEMORY_BASIC_INFORMATION mbi; + VirtualQuery( + ppfn, + &mbi, + sizeof(MEMORY_BASIC_INFORMATION) + ); + VirtualProtect( + mbi.BaseAddress, + mbi.RegionSize, + PAGE_READWRITE, + &mbi.Protect); + + *ppfn = *newFunction; + + DWORD dwOldProtect; + VirtualProtect( + mbi.BaseAddress, + mbi.RegionSize, + mbi.Protect, + &dwOldProtect + ); + break; + } + pThunk++; + } + + return pfnHookAPIAddr; +} + +void clearUMDriverOverrides() +{ + if (lastContext != NULL) + { + // Unpatch all the things. + + if (oldCreateDXGIFactory) + { + restoreFunction((PROC)oldCreateDXGIFactory, oldCreateDXGIFactoryData); + } + if (oldCreateDXGIFactory1) + { + restoreFunction((PROC)oldCreateDXGIFactory1, oldCreateDXGIFactory1Data); + } + if (oldCreateDXGIFactory2) + { + restoreFunction((PROC)oldCreateDXGIFactory2, oldCreateDXGIFactory2Data); + } + if (oldDirectX9Create) + { + restoreFunction((PROC)oldDirectX9Create, oldDirectX9CreateData); + } + if (oldDirectX9ExCreate) + { + restoreFunction((PROC)oldDirectX9ExCreate, oldDirectX9ExCreateData); + } + if (oldCreateDXGIFactory2) + { + restoreFunction((PROC)oldCreateDXGIFactory2, oldCreateDXGIFactory2Data); + } + + for (int i = 0; i < ShimCountMax; ++i) + { + HINSTANCE hInst = oldLoaderInstances[i]; + + if (hInst != NULL) + { + for (int j = 0; j < NUM_LOADER_LIBS; ++j) + { + const char* loaderLibrary = loaderLibraryList[j]; + + if (oldLoaderProcA[j]) + { + SetProcAddressA(hInst, loaderLibrary, "LoadLibraryA", oldLoaderProcA[i][j], (PROC)OVRLoadLibraryA); + } + if (oldLoaderProcW[j]) + { + SetProcAddressA(hInst, loaderLibrary, "LoadLibraryW", oldLoaderProcW[i][j], (PROC)OVRLoadLibraryW); + } + if (oldLoaderProcExA[j]) + { + SetProcAddressA(hInst, loaderLibrary, "LoadLibraryExA", oldLoaderProcExA[i][j], (PROC)OVRLoadLibraryExA); + } + if (oldLoaderProcExW[j]) + { + SetProcAddressA(hInst, loaderLibrary, "LoadLibraryExW", oldLoaderProcExW[i][j], (PROC)OVRLoadLibraryExW); + } + if (oldLoaderProcModExA[j]) + { + SetProcAddressA(hInst, loaderLibrary, "GetModuleHandleExA", oldLoaderProcModExA[i][j], (PROC)OVRGetModuleHandleExA); + } + if (oldLoaderProcModExW[j]) + { + SetProcAddressA(hInst, loaderLibrary, "GetModuleHandleExW", oldLoaderProcModExW[i][j], (PROC)OVRGetModuleHandleExW); + } + } + + FreeLibrary(hInst); + } + } + + if (rtFilterModule != NULL) + { + LPFNCANUNLOADNOW pfnCanUnloadNow = (LPFNCANUNLOADNOW)GetProcAddress(rtFilterModule, "DllCanUnloadNow"); + if (pfnCanUnloadNow && pfnCanUnloadNow() == S_OK) + { + FreeLibrary(rtFilterModule); + rtFilterModule = NULL; + } + } + + // Do not set the following to NULL, because it's possible that we may be called after this function is executed. + // This is due to quirks in how third party DLLs implement their linking to these functions. In any case + // this module will be going away within a month or so of this writing. + //oldProcA = NULL; + //oldProcExA = NULL; + //oldProcW = NULL; + //oldProcExW = NULL; + //oldProcModExA = NULL; + //oldProcModExW = NULL; + + oldDirectX9Create = NULL; + oldDirectX9ExCreate = NULL; + oldCreateDXGIFactory = NULL; + oldCreateDXGIFactory1 = NULL; + oldCreateDXGIFactory2 = NULL; + + lastContext = NULL; + } +} + +bool checkUMDriverOverrides(void* context) +{ + lastContext = context; + if (oldCreateDXGIFactory != NULL) + return true; + + PreloadLibraryRTFn loadFunc = NULL; + for( int i = 0; i < ShimCountMax; ++i ) + { + HINSTANCE hInst = NULL; + try + { + hInst = LoadLibraryA(dllList[i]); + } + catch(...) + { + } + + oldLoaderInstances[i] = hInst; + + if (hInst == NULL) + continue; + + ShimedLibraries libCount = (ShimedLibraries)i; + switch( libCount ) + { + case ShimLibDXGI: + oldCreateDXGIFactory = (WinCreateDXGIFactory)SetProcAddressDirect( hInst, "CreateDXGIFactory", (PROC)OVRCreateDXGIFactory, oldCreateDXGIFactoryData ); + oldCreateDXGIFactory1 = (WinCreateDXGIFactory1)SetProcAddressDirect( hInst, "CreateDXGIFactory1", (PROC)OVRCreateDXGIFactory1, oldCreateDXGIFactory1Data ); + oldCreateDXGIFactory2 = (WinCreateDXGIFactory2)SetProcAddressDirect( hInst, "CreateDXGIFactory2", (PROC)OVRCreateDXGIFactory2, oldCreateDXGIFactory2Data ); + break; + case ShimLibD3D9: + oldDirectX9Create = (WinDirect3DCreate9)SetProcAddressDirect( hInst, "Direct3DCreate9", (PROC)OVRDirect3DCreate9, oldDirectX9CreateData ); + oldDirectX9ExCreate = (WinDirect3DCreate9Ex)SetProcAddressDirect( hInst, "Direct3DCreate9Ex", (PROC)OVRDirect3DCreate9Ex, oldDirectX9ExCreateData ); + break; + default: + break; + } + + for (int j = 0; j < NUM_LOADER_LIBS; ++j) + { + const char* loaderLibrary = loaderLibraryList[j]; + + PROC temp = NULL; + temp = SetProcAddressA(hInst, loaderLibrary, "LoadLibraryA", (PROC)OVRLoadLibraryA); + if (!oldProcA) + { + oldProcA = (WinLoadLibraryA)temp; + } + oldLoaderProcA[i][j] = temp; + + temp = SetProcAddressA(hInst, loaderLibrary, "LoadLibraryW", (PROC)OVRLoadLibraryW); + if (!oldProcW) + { + oldProcW = (WinLoadLibraryW)temp; + } + oldLoaderProcW[i][j] = temp; + + temp = SetProcAddressA(hInst, loaderLibrary, "LoadLibraryExA", (PROC)OVRLoadLibraryExA); + if (!oldProcExA) + { + oldProcExA = (WinLoadLibraryExA)temp; + } + oldLoaderProcExA[i][j] = temp; + + temp = SetProcAddressA(hInst, loaderLibrary, "LoadLibraryExW", (PROC)OVRLoadLibraryExW); + if (!oldProcExW) + { + oldProcExW = (WinLoadLibraryExW)temp; + } + oldLoaderProcExW[i][j] = temp; + + temp = SetProcAddressA(hInst, loaderLibrary, "GetModuleHandleExA", (PROC)OVRGetModuleHandleExA); + if (!oldProcModExA) + { + oldProcModExA = (WinGetModuleHandleExA)temp; + } + oldLoaderProcModExA[i][j] = temp; + + temp = SetProcAddressA(hInst, loaderLibrary, "GetModuleHandleExW", (PROC)OVRGetModuleHandleExW); + if (!oldProcModExW) + { + oldProcModExW = (WinGetModuleHandleExW)temp; + } + oldLoaderProcModExW[i][j] = temp; + } + + if (loadFunc == NULL) + { + loadFunc = (PreloadLibraryRTFn)GetProcAddress(hInst, "PreloadLibraryRT"); + } + } + + rtFilterModule = oldProcA ? (*oldProcA)(RTFilter) : NULL; + + IsCreatingBackBuffer backBufferFunc = NULL; + ShouldVSync shouldVSyncFunc = NULL; + GetRTFilterVersion getRTFilterVersionFunc = NULL; + + if (rtFilterModule != NULL) + { + loadFunc = (PreloadLibraryRTFn)GetProcAddress(rtFilterModule, "PreloadLibraryRT"); + backBufferFunc = (IsCreatingBackBuffer)GetProcAddress(rtFilterModule, "OVRIsCreatingBackBuffer"); + shouldVSyncFunc = (ShouldVSync)GetProcAddress(rtFilterModule, "OVRShouldVSync"); + getRTFilterVersionFunc = (GetRTFilterVersion)GetProcAddress(rtFilterModule, "OVRGetRTFilterVersion"); + } + + if (loadFunc == NULL) + { + MessageBox(nullptr, L"Unable to load the Oculus Display Driver. Please reinstall the Oculus Runtime.", L"Configuration Error", MB_OK | MB_ICONERROR); + clearUMDriverOverrides(); + return false; + } + + if (getRTFilterVersionFunc == NULL) + { + WCHAR message[1000] = {}; + swprintf_s(message, L"This app requires the %d.%d.%d version of the Oculus Runtime.", OVR_MAJOR_VERSION, OVR_MINOR_VERSION, OVR_BUILD_NUMBER); + MessageBox(nullptr, message, L"Configuration Error", MB_OK | MB_ICONERROR); + clearUMDriverOverrides(); + return false; + } + + // Verify that we are running with the appropriate display driver + { + const ULONG rtFilterVersion = (*getRTFilterVersionFunc)(); + const ULONG rtFilterMajor = OVR_GET_VERSION_MAJOR(rtFilterVersion); + const ULONG rtFilterMinor = OVR_GET_VERSION_MINOR(rtFilterVersion); + + if ((rtFilterMajor != OVR_RTFILTER_VERSION_MAJOR) || (rtFilterMinor < OVR_RTFILTER_VERSION_MINOR)) + { + WCHAR message[1000] = {}; + swprintf_s(message, L"This app requires the %d.%d.%d version of the Oculus Runtime.", OVR_MAJOR_VERSION, OVR_MINOR_VERSION, OVR_BUILD_NUMBER); + MessageBox(nullptr, message, L"Configuration Error", MB_OK | MB_ICONERROR); + clearUMDriverOverrides(); + return false; + } + } + + appDriver.version = OVR_RENDER_SHIM_VERSION_MAJOR; + appDriver.context = lastContext; + +// appDriver.pfnInitializingDisplay = OVRIsInitializingDisplay; + appDriver.pfnInitializingDisplay = OVRLocalIsInitializingDisplay; + appDriver.pfnRiftForContext = OVRRiftForContext; + appDriver.pfnCloseRiftForContext = OVRCloseRiftForContext; + appDriver.pfnWindowDisplayResolution = OVRWindowDisplayResolution; + appDriver.pfnShouldEnableDebug = OVRShouldEnableDebug; + appDriver.pfnIsCreatingBackBuffer = (backBufferFunc == NULL) ? OVRIsCreatingBackBuffer : backBufferFunc; + appDriver.pfnShouldVSync = (shouldVSyncFunc == NULL) ? OVRShouldVSync : shouldVSyncFunc; + appDriver.pfnExpectedResolution = OVRExpectedResolution; + appDriver.pfnMirroringEnabled = OVRMirroringEnabled; + appDriver.pfnGetWindowForContext = OVRGetWindowForContext; + appDriver.pfnPresentRiftOnContext = OVRShouldPresentOnContext; + + appDriver.pfnDirect3DCreate9 = oldDirectX9Create; + appDriver.pfnDirect3DCreate9Ex = oldDirectX9ExCreate; + appDriver.pfnCreateDXGIFactory = oldCreateDXGIFactory; + appDriver.pfnCreateDXGIFactory1 = oldCreateDXGIFactory1; + appDriver.pfnCreateDXGIFactory2 = oldCreateDXGIFactory2; + + (*loadFunc)( &appDriver ); + + return true; +} diff --git a/LibOVR/Src/Displays/OVR_Win32_ShimFunctions.cpp b/LibOVR/Src/Displays/OVR_Win32_ShimFunctions.cpp index 602df98..e399863 100644 --- a/LibOVR/Src/Displays/OVR_Win32_ShimFunctions.cpp +++ b/LibOVR/Src/Displays/OVR_Win32_ShimFunctions.cpp @@ -1,234 +1,243 @@ -/************************************************************************************
-
-Filename : OVR_Win32_ShimFunctions.cpp
-Content : Client-side shim callbacks for usermode/rt hooks
-Created : May 6, 2014
-Authors : Dean Beeler
-
-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_Win32_Display.h"
-#include "OVR_Win32_ShimFunctions.h"
-#include "OVR_Win32_Dxgi_Display.h"
-#include "../OVR_Stereo.h"
-#include "OVR_Win32_FocusReader.h"
-
-// Exported
-extern bool checkUMDriverOverrides(void* context);
-extern void clearUMDriverOverrides();
-
-#include <stdio.h>
-#include <tchar.h>
-#include <string.h>
-#include <stdlib.h>
-#include <WinSock2.h>
-#include <WS2tcpip.h>
-#define WIN32_LEAN_AND_MEAN
-#include <Windows.h>
-#include <winioctl.h>
-#include <SetupAPI.h>
-#include <Mmsystem.h>
-#include <conio.h>
-
-
-//-------------------------------------------------------------------------------------
-// ***** User-mode Callbacks
-//
-// See IsInitializingDisplay, etc in Dxgi_Display.h for details
-//
-
-extern LINK_APPLICATION_DRIVER appDriver;
-
-BOOL WINAPI OVRIsInitializingDisplay(PVOID context, UINT width, UINT height)
-{
- OVR::Win32::DisplayShim* con = (OVR::Win32::DisplayShim*)context;
- if (con->ExpectedWidth == (int)width && con->ExpectedHeight == (int)height)
- return TRUE;
-
- return FALSE;
-}
-
-BOOL WINAPI OVRExpectedResolution( PVOID context, UINT* width, UINT* height, UINT* rotationInDegrees )
-{
- OVR::Win32::DisplayShim* con = (OVR::Win32::DisplayShim*)context;
-
- *width = con->ExpectedWidth;
- *height = con->ExpectedHeight;
- *rotationInDegrees = con->Rotation;
- return TRUE;
-}
-
-BOOL WINAPI OVRIsCreatingBackBuffer(PVOID context)
-{
- OVR::Win32::DisplayShim* con = (OVR::Win32::DisplayShim*)context;
- if( con->ExpectedWidth != -1 && con->ExpectedHeight != -1 )
- return TRUE;
-
- return FALSE;
-}
-
-BOOL WINAPI OVRShouldVSync( )
-{
- return FALSE;
-}
-
-
-ULONG WINAPI OVRRiftForContext(PVOID context, HANDLE driverHandle)
-{
- OVR_UNUSED( driverHandle );
- OVR::Win32::DisplayShim* con = (OVR::Win32::DisplayShim*)context;
-
- return con->ChildUid;
-}
-
-HWND WINAPI OVRGetWindowForContext(PVOID context)
-{
- OVR::Win32::DisplayShim* con = (OVR::Win32::DisplayShim*)context;
-
- if( con->Active )
- {
- return con->hWindow;
- }
- else
- {
- return 0;
- }
-}
-
-BOOL WINAPI OVRShouldPresentOnContext(PVOID context)
-{
- OVR::Win32::DisplayShim* con = (OVR::Win32::DisplayShim*)context;
-
- return con->Active && ( con->hWindow == OVR::Win32::RenderFocusReader::GetInstance()->ReadActiveWindow() );
-}
-
-BOOL WINAPI OVRCloseRiftForContext( PVOID context, HANDLE driverHandle, ULONG rift )
-{
- OVR_UNUSED( context ); OVR_UNUSED( driverHandle ); OVR_UNUSED( rift );
- // TODO
- return TRUE;
-}
-
-BOOL WINAPI OVRWindowDisplayResolution( PVOID context, UINT* width, UINT* height,
- UINT* titleHeight, UINT* borderWidth,
- BOOL* vsyncEnabled )
-{
- OVR::Win32::DisplayShim* con = (OVR::Win32::DisplayShim*)context;
- void* handle = con->hWindow;
-
- if( handle )
- {
- RECT winRect = { 0 };
- GetWindowRect( (HWND)handle, &winRect );
-
- RECT rect = {0};
- if( GetClientRect( (HWND)handle, &rect ) )
- {
- LONG barHeight = (winRect.bottom - winRect.top) - (rect.bottom - rect.top);
- LONG borderSize = (winRect.right - winRect.left) - (rect.right - rect.left);
-
- *titleHeight = barHeight - borderSize + (borderSize / 2 );
- *borderWidth = borderSize / 2;
- *width = rect.right - rect.left + (borderSize / 2);
- *height = rect.bottom - rect.top + *titleHeight;
- }
- else
- {
- return FALSE;
- }
- }
- else
- {
- return FALSE;
- }
-
- *vsyncEnabled = TRUE;
-
- return TRUE;
-}
-
-BOOL WINAPI OVRShouldEnableDebug()
-{
- return FALSE;
-}
-
-BOOL WINAPI OVRMirroringEnabled( PVOID context )
-{
- OVR::Win32::DisplayShim* con = (OVR::Win32::DisplayShim*)context;
-
- return con->UseMirroring;
-}
-
-namespace OVR { namespace Win32 {
-
-DisplayShim::DisplayShim() :
- ChildUid( 0 ),
- ExpectedWidth( 1280 ),
- ExpectedHeight( 800 ),
- Rotation( 0 ),
- hWindow( 0 ),
- UseMirroring( true ),
- Active( false )
-{
-
-}
-
-DisplayShim::~DisplayShim()
-{
-
-}
-
-bool DisplayShim::Initialize( bool inCompatibility )
-{
- if (inCompatibility)
- return false;
-
- return checkUMDriverOverrides( this );
-}
-
-bool DisplayShim::Shutdown()
-{
- clearUMDriverOverrides();
-
- return true;
-}
-
-bool DisplayShim::Update(Win32ShimInfo* shimInfo)
-{
- ChildUid = shimInfo->DeviceNumber;
- ExpectedWidth = shimInfo->NativeWidth;
- ExpectedHeight = shimInfo->NativeHeight;
- Rotation = shimInfo->Rotation;
- UseMirroring = shimInfo->UseMirroring != 0;
- return true;
-}
-
-void* DisplayShim::GetDX11SwapChain()
-{
- if( appDriver.pfnGetDX11SwapChain )
- {
- return (*appDriver.pfnGetDX11SwapChain)(this);
- }
-
- return NULL;
-}
-
-
-} } // OVR::Win32
+/************************************************************************************ + +Filename : OVR_Win32_ShimFunctions.cpp +Content : Client-side shim callbacks for usermode/rt hooks +Created : May 6, 2014 +Authors : Dean Beeler + +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 <Kernel/OVR_Win32_IncludeWindows.h> +#include "OVR_Win32_Display.h" +#include "OVR_Win32_ShimFunctions.h" +#include "OVR_Win32_Dxgi_Display.h" +#include "../OVR_Stereo.h" +#include "OVR_Win32_FocusReader.h" + +// Exported +extern bool checkUMDriverOverrides(void* context); +extern void clearUMDriverOverrides(); + +#include <stdio.h> +#include <tchar.h> +#include <string.h> +#include <stdlib.h> +#include <winioctl.h> +#include <SetupAPI.h> +#include <Mmsystem.h> +#include <conio.h> + + +//------------------------------------------------------------------------------------- +// ***** User-mode Callbacks +// +// See IsInitializingDisplay, etc in Dxgi_Display.h for details +// + +extern LINK_APPLICATION_DRIVER appDriver; + +BOOL WINAPI OVRIsInitializingDisplay(PVOID context, UINT width, UINT height) +{ + OVR::Win32::DisplayShim* con = (OVR::Win32::DisplayShim*)context; + if (con->ExpectedWidth == (int)width && con->ExpectedHeight == (int)height) + return TRUE; + + return FALSE; +} + +BOOL WINAPI OVRExpectedResolution( PVOID context, UINT* width, UINT* height, UINT* rotationInDegrees ) +{ + OVR::Win32::DisplayShim* con = (OVR::Win32::DisplayShim*)context; + + *width = con->ExpectedWidth; + *height = con->ExpectedHeight; + *rotationInDegrees = con->Rotation; + return TRUE; +} + +BOOL WINAPI OVRIsCreatingBackBuffer(PVOID context) +{ + OVR::Win32::DisplayShim* con = (OVR::Win32::DisplayShim*)context; + if( con->ExpectedWidth != -1 && con->ExpectedHeight != -1 ) + return TRUE; + + return FALSE; +} + +BOOL WINAPI OVRShouldVSync( ) +{ + return FALSE; +} + + +ULONG WINAPI OVRRiftForContext(PVOID context, HANDLE driverHandle) +{ + OVR_UNUSED( driverHandle ); + OVR::Win32::DisplayShim* con = (OVR::Win32::DisplayShim*)context; + + return con->ChildUid; +} + +HWND WINAPI OVRGetWindowForContext(PVOID context) +{ + OVR::Win32::DisplayShim* con = (OVR::Win32::DisplayShim*)context; + + if( con->Active ) + { + return con->hWindow; + } + else + { + return 0; + } +} + +BOOL WINAPI OVRShouldPresentOnContext(PVOID context) +{ + OVR::Win32::DisplayShim* con = (OVR::Win32::DisplayShim*)context; + + return con->Active && ( con->hWindow == OVR::Win32::RenderFocusReader::GetInstance()->ReadActiveWindow() ); +} + +BOOL WINAPI OVRCloseRiftForContext( PVOID context, HANDLE driverHandle, ULONG rift ) +{ + OVR_UNUSED( context ); OVR_UNUSED( driverHandle ); OVR_UNUSED( rift ); + // TODO + return TRUE; +} + +BOOL WINAPI OVRWindowDisplayResolution( PVOID context, UINT* width, UINT* height, + UINT* titleHeight, UINT* borderWidth, + BOOL* vsyncEnabled ) +{ + OVR::Win32::DisplayShim* con = (OVR::Win32::DisplayShim*)context; + void* handle = con->hWindow; + + if( handle ) + { + RECT winRect = { 0 }; + GetWindowRect( (HWND)handle, &winRect ); + + RECT rect = {0}; + if( GetClientRect( (HWND)handle, &rect ) ) + { + LONG barHeight = (winRect.bottom - winRect.top) - (rect.bottom - rect.top); + LONG borderSize = (winRect.right - winRect.left) - (rect.right - rect.left); + + *titleHeight = barHeight - borderSize + (borderSize / 2 ); + *borderWidth = borderSize / 2; + *width = rect.right - rect.left + (borderSize / 2); + *height = rect.bottom - rect.top + *titleHeight; + } + else + { + return FALSE; + } + } + else + { + return FALSE; + } + + *vsyncEnabled = TRUE; + + return TRUE; +} + +BOOL WINAPI OVRShouldEnableDebug() +{ + return FALSE; +} + +BOOL WINAPI OVRMirroringEnabled( PVOID context ) +{ + OVR::Win32::DisplayShim* con = (OVR::Win32::DisplayShim*)context; + + return con->UseMirroring; +} + + +// This is a temporarily exported function for the purpose of aiding in the DLL transition for the Unity plugin. +// It is unsupported and will be removed in a future release. +extern "C" +{ + OVR_PUBLIC_FUNCTION(void*) ovr_GetDX11SwapChain() + { + return OVR::Win32::DisplayShim::GetInstance().GetDX11SwapChain(); + } +} + + +namespace OVR { namespace Win32 { + +DisplayShim::DisplayShim() : + ChildUid( 0 ), + ExpectedWidth( 1280 ), + ExpectedHeight( 800 ), + Rotation( 0 ), + hWindow( 0 ), + UseMirroring( true ), + Active( false ) +{ + +} + +DisplayShim::~DisplayShim() +{ + +} + +bool DisplayShim::Initialize( bool inCompatibility ) +{ + if (inCompatibility) + return false; + + return checkUMDriverOverrides( this ); +} + +bool DisplayShim::Shutdown() +{ + clearUMDriverOverrides(); + + return true; +} + +bool DisplayShim::Update(ExtraMonitorInfo* shimInfo) +{ + ChildUid = shimInfo->DeviceNumber; + ExpectedWidth = shimInfo->NativeWidth; + ExpectedHeight = shimInfo->NativeHeight; + Rotation = shimInfo->Rotation; + UseMirroring = shimInfo->UseMirroring != 0; + return true; +} + +void* DisplayShim::GetDX11SwapChain() +{ + if( appDriver.pfnGetDX11SwapChain ) + { + return (*appDriver.pfnGetDX11SwapChain)(this); + } + + return NULL; +} + + +} } // OVR::Win32 diff --git a/LibOVR/Src/Displays/OVR_Win32_ShimFunctions.h b/LibOVR/Src/Displays/OVR_Win32_ShimFunctions.h index 1339de4..08e384d 100644 --- a/LibOVR/Src/Displays/OVR_Win32_ShimFunctions.h +++ b/LibOVR/Src/Displays/OVR_Win32_ShimFunctions.h @@ -1,79 +1,79 @@ -/************************************************************************************
-
-Filename : OVR_Win32_ShimFunctions.h
-Content : Client-side shim callbacks for usermode/rt hooks
-Created : May 6, 2014
-Authors : Dean Beeler
-
-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.
-
-*************************************************************************************/
-
-#ifndef OVR_Win32_ShimFunctions_h
-#define OVR_Win32_ShimFunctions_h
-
-#include "OVR_Win32_Display.h"
-
-namespace OVR {
-
-struct Win32ShimInfo;
-
-namespace Win32 {
-
-
-class DisplayShim
-{
-public:
-
-public:
- static DisplayShim& GetInstance()
- {
- static DisplayShim instance;
- return instance;
- }
-
- bool Initialize( bool inCompatibility );
- bool Shutdown();
-
- bool Update( Win32ShimInfo* shimInfo );
-
- void* GetDX11SwapChain();
-
- ULONG ChildUid;
- int ExpectedWidth;
- int ExpectedHeight;
- int Rotation;
- HWND hWindow;
- bool UseMirroring;
- bool Active;
-
-private:
-
- DisplayShim();
-
- virtual ~DisplayShim();
-
- DisplayShim(DisplayShim const&); // Don't Implement
- void operator=(DisplayShim const&); // Don't implement
-
-};
-
-
-}} // namespace OVR::Win32
-
-#endif
+/************************************************************************************ + +Filename : OVR_Win32_ShimFunctions.h +Content : Client-side shim callbacks for usermode/rt hooks +Created : May 6, 2014 +Authors : Dean Beeler + +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. + +*************************************************************************************/ + +#ifndef OVR_Win32_ShimFunctions_h +#define OVR_Win32_ShimFunctions_h + +#include "OVR_Win32_Display.h" + +namespace OVR { + +struct ExtraMonitorInfo; + +namespace Win32 { + + +class DisplayShim +{ +public: + +public: + static DisplayShim& GetInstance() + { + static DisplayShim instance; + return instance; + } + + bool Initialize( bool inCompatibility ); + bool Shutdown(); + + bool Update( ExtraMonitorInfo* shimInfo ); + + void* GetDX11SwapChain(); + + ULONG ChildUid; + int ExpectedWidth; + int ExpectedHeight; + int Rotation; + HWND hWindow; + bool UseMirroring; + bool Active; + +private: + + DisplayShim(); + + virtual ~DisplayShim(); + + DisplayShim(DisplayShim const&); // Don't Implement + void operator=(DisplayShim const&); // Don't implement + +}; + + +}} // namespace OVR::Win32 + +#endif diff --git a/LibOVR/Src/Displays/OVR_Win32_ShimVersion.h b/LibOVR/Src/Displays/OVR_Win32_ShimVersion.h index f5eaa7c..2561ee5 100644 --- a/LibOVR/Src/Displays/OVR_Win32_ShimVersion.h +++ b/LibOVR/Src/Displays/OVR_Win32_ShimVersion.h @@ -1,47 +1,47 @@ -/************************************************************************************
-
-Filename : OVR_Win32_ShimVersion.h
-Content : Versioning info for our display shim
-Created : Nov 4, 2014
-Authors : Scott Bassett
-
-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.
-
-*************************************************************************************/
-
-#define STRINGIZE_(x) #x
-#define STRINGIZE(x) STRINGIZE_(x)
-
-#define OVR_MAKE_VERSION(major, minor, patch) (ULONG)(((major) << 24) | ((minor) << 16) | patch)
-#define OVR_GET_VERSION_MAJOR(x) (ULONG)(((x) >> 24) & 0x000000FF)
-#define OVR_GET_VERSION_MINOR(x) (ULONG)(((x) >> 16) & 0x000000FF)
-#define OVR_GET_VERSION_PATCH(x) (ULONG)((x) & 0x0000FFFF)
-
-#define OVR_RENDER_SHIM_VERSION_MAJOR 1
-#define OVR_RENDER_SHIM_VERSION_MINOR 0
-#define OVR_RENDER_SHIM_VERSION_PATCH 0
-#define OVR_RENDER_SHIM_VERSION OVR_MAKE_VERSION(OVR_RENDER_SHIM_VERSION_MAJOR, OVR_RENDER_SHIM_VERSION_MINOR, OVR_RENDER_SHIM_VERSION_PATCH)
-#define OVR_RENDER_SHIM_VERSION_STRING (STRINGIZE(OVR_RENDER_SHIM_VERSION_MAJOR) "." STRINGIZE(OVR_RENDER_SHIM_VERSION_MINOR) "." STRINGIZE(OVR_RENDER_SHIM_VERSION_PATCH))
-
-// IF YOU CHANGE ANY OF THESE NUMBERS YOU MUST UPDATE MULTIPLE FILES.
-// PLEASE LOOK AT CHANGELIST 31947 TO SEE THE FULL LIST.
-#define OVR_RTFILTER_VERSION_MAJOR 1
-#define OVR_RTFILTER_VERSION_MINOR 2
-#define OVR_RTFILTER_VERSION_PATCH 2
-#define OVR_RTFILTER_VERSION OVR_MAKE_VERSION(OVR_RTFILTER_VERSION_MAJOR, OVR_RTFILTER_VERSION_MINOR, OVR_RTFILTER_VERSION_PATCH)
-#define OVR_RTFILTER_VERSION_STRING (STRINGIZE(OVR_RTFILTER_VERSION_MAJOR) "." STRINGIZE(OVR_RTFILTER_VERSION_MINOR) "." STRINGIZE(OVR_RTFILTER_VERSION_PATCH))
+/************************************************************************************ + +Filename : OVR_Win32_ShimVersion.h +Content : Versioning info for our display shim +Created : Nov 4, 2014 +Authors : Scott Bassett + +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. + +*************************************************************************************/ + +#define STRINGIZE_(x) #x +#define STRINGIZE(x) STRINGIZE_(x) + +#define OVR_MAKE_VERSION(major, minor, patch) (ULONG)(((major) << 24) | ((minor) << 16) | patch) +#define OVR_GET_VERSION_MAJOR(x) (ULONG)(((x) >> 24) & 0x000000FF) +#define OVR_GET_VERSION_MINOR(x) (ULONG)(((x) >> 16) & 0x000000FF) +#define OVR_GET_VERSION_PATCH(x) (ULONG)((x) & 0x0000FFFF) + +#define OVR_RENDER_SHIM_VERSION_MAJOR 1 +#define OVR_RENDER_SHIM_VERSION_MINOR 0 +#define OVR_RENDER_SHIM_VERSION_PATCH 0 +#define OVR_RENDER_SHIM_VERSION OVR_MAKE_VERSION(OVR_RENDER_SHIM_VERSION_MAJOR, OVR_RENDER_SHIM_VERSION_MINOR, OVR_RENDER_SHIM_VERSION_PATCH) +#define OVR_RENDER_SHIM_VERSION_STRING (STRINGIZE(OVR_RENDER_SHIM_VERSION_MAJOR) "." STRINGIZE(OVR_RENDER_SHIM_VERSION_MINOR) "." STRINGIZE(OVR_RENDER_SHIM_VERSION_PATCH)) + +// IF YOU CHANGE ANY OF THESE NUMBERS YOU MUST UPDATE MULTIPLE FILES. +// PLEASE LOOK AT CHANGELIST 31947 TO SEE THE FULL LIST. +#define OVR_RTFILTER_VERSION_MAJOR 1 +#define OVR_RTFILTER_VERSION_MINOR 2 +#define OVR_RTFILTER_VERSION_PATCH 4 +#define OVR_RTFILTER_VERSION OVR_MAKE_VERSION(OVR_RTFILTER_VERSION_MAJOR, OVR_RTFILTER_VERSION_MINOR, OVR_RTFILTER_VERSION_PATCH) +#define OVR_RTFILTER_VERSION_STRING (STRINGIZE(OVR_RTFILTER_VERSION_MAJOR) "." STRINGIZE(OVR_RTFILTER_VERSION_MINOR) "." STRINGIZE(OVR_RTFILTER_VERSION_PATCH)) |