aboutsummaryrefslogtreecommitdiffstats
path: root/LibOVR/Src/Displays/OVR_Win32_ShimFunctions.cpp
blob: e399863dc41d2574d4099b053a21013f2d004c74 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
/************************************************************************************

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