File: wgfx.cpp

package info (click to toggle)
0ad 0.0.23.1-5
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 78,412 kB
  • sloc: cpp: 245,162; ansic: 200,249; javascript: 19,244; python: 13,754; sh: 6,104; perl: 4,620; makefile: 977; xml: 810; java: 533; ruby: 229; erlang: 46; pascal: 30; sql: 21; tcl: 4
file content (187 lines) | stat: -rw-r--r-- 6,219 bytes parent folder | download | duplicates (2)
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
/* Copyright (C) 2017 Wildfire Games.
 *
 * Permission is hereby granted, free of charge, to any person obtaining
 * a copy of this software and associated documentation files (the
 * "Software"), to deal in the Software without restriction, including
 * without limitation the rights to use, copy, modify, merge, publish,
 * distribute, sublicense, and/or sell copies of the Software, and to
 * permit persons to whom the Software is furnished to do so, subject to
 * the following conditions:
 *
 * The above copyright notice and this permission notice shall be included
 * in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
 * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */

/*
 * graphics card detection on Windows.
 */

#include "precompiled.h"
#include "lib/sysdep/os/win/wgfx.h"

#include "lib/sysdep/gfx.h"
#include "lib/sysdep/os/win/wdll_ver.h"
#include "lib/sysdep/os/win/wutil.h"

#if MSC_VERSION
#pragma comment(lib, "advapi32.lib")	// registry
#endif


// note: this implementation doesn't require OpenGL to be initialized.
static Status AppendDriverVersionsFromRegistry(VersionList& versionList)
{
	// rationale:
	// - we could easily determine the 2d driver via EnumDisplaySettings,
	//   but we want to query the actual OpenGL driver. see
	//   http://www.opengl.org/discussion_boards/ubb/Forum3/HTML/009679.html ;
	//   in short, we need the exact OpenGL driver version because some
	//   driver packs (e.g. Omega) mix and match DLLs.
	// - an alternative implementation would be to enumerate all
	//   DLLs loaded into the process, and check for a glBegin export.
	//   that requires toolhelp/PSAPI (a bit complicated) and telling
	//   ICD/opengl32.dll apart (not future-proof).
	// - therefore, we stick with the OpenGLDrivers approach. since there is
	//   no good way to determine which of the subkeys (e.g. nvoglnt) is
	//   active (several may exist due to previously removed drivers),
	//   we just display all of them. it is obvious from looking at
	//   gfx_card which one is correct; we thus avoid driver-specific
	//   name checks and reporting incorrectly.

	wchar_t dllName[MAX_PATH+1];

	HKEY hkDrivers;
	const wchar_t* key = L"SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\OpenGLDrivers";
	// (we've received word of this failing on one WinXP system, but
	// AppendDriverVersionsFromKnownFiles might still work.)
	if(RegOpenKeyExW(HKEY_LOCAL_MACHINE, key, 0, KEY_READ, &hkDrivers) != 0)
		return ERR::FAIL;	// NOWARN (see above)

	// for each subkey (i.e. installed OpenGL driver):
	for(DWORD i = 0; ; i++)
	{
		wchar_t driverName[32];
		DWORD driverNameLength = ARRAY_SIZE(driverName);
		const LONG err = RegEnumKeyExW(hkDrivers, i, driverName, &driverNameLength, 0, 0, 0, 0);
		if(err == ERROR_NO_MORE_ITEMS)
		{
			if(i == 0)
			{
				RegCloseKey(hkDrivers);
				return ERR::NOT_SUPPORTED;	// NOWARN (ATI and NVidia don't create sub-keys on Windows 7)
			}
			break;
		}
		ENSURE(err == ERROR_SUCCESS);

		HKEY hkDriver;
		if(RegOpenKeyExW(hkDrivers, driverName, 0, KEY_QUERY_VALUE, &hkDriver) == 0)
		{
			DWORD dllNameLength = ARRAY_SIZE(dllName)-5;	// for ".dll"
			if(RegQueryValueExW(hkDriver, L"Dll", 0, 0, (LPBYTE)dllName, &dllNameLength) == 0)
				wdll_ver_Append(dllName, versionList);

			RegCloseKey(hkDriver);
		}
	}

	// for each value:
	// (some old drivers, e.g. S3 Super Savage, store their ICD name in a
	// single REG_SZ value. we therefore include those as well.)
	for(DWORD i = 0; ; i++)
	{
		wchar_t name[100];	// we don't need this, but RegEnumValue fails otherwise.
		DWORD nameLength = ARRAY_SIZE(name);
		DWORD type;
		DWORD dllNameLength = ARRAY_SIZE(dllName)-5;	// for ".dll"
		const DWORD err = RegEnumValueW(hkDrivers, i, name, &nameLength, 0, &type, (LPBYTE)dllName, &dllNameLength);
		if(err == ERROR_NO_MORE_ITEMS)
			break;
		ENSURE(err == ERROR_SUCCESS);
		if(type == REG_SZ)
			wdll_ver_Append(dllName, versionList);
	}

	RegCloseKey(hkDrivers);

	return INFO::OK;
}


static void AppendDriverVersionsFromKnownFiles(VersionList& versionList)
{
	// (check all known file names regardless of gfx_card, which may change and
	// defeat our parsing. this takes about 5..10 ms)

	// NVidia
	wdll_ver_Append(L"nvoglv64.dll", versionList);
	wdll_ver_Append(L"nvoglv32.dll", versionList);
	wdll_ver_Append(L"nvoglnt.dll", versionList);

	// ATI
	wdll_ver_Append(L"atioglxx.dll", versionList);

	// Intel
	wdll_ver_Append(L"ig4icd32.dll", versionList);
	wdll_ver_Append(L"ig4icd64.dll", versionList);
	wdll_ver_Append(L"iglicd32.dll", versionList);
}


std::wstring wgfx_DriverInfo()
{
	VersionList versionList;
	if(AppendDriverVersionsFromRegistry(versionList) != INFO::OK)	// (fails on Windows 7)
		AppendDriverVersionsFromKnownFiles(versionList);
	return versionList;
}


//-----------------------------------------------------------------------------
// direct implementations of some gfx functions

namespace gfx {

Status GetVideoMode(int* xres, int* yres, int* bpp, int* freq)
{
	DEVMODE dm = { sizeof(dm) };

	if(!EnumDisplaySettings(0, ENUM_CURRENT_SETTINGS, &dm))
		WARN_RETURN(ERR::FAIL);

	// EnumDisplaySettings is documented to set the values of the following:
	const DWORD expectedFlags = DM_PELSWIDTH|DM_PELSHEIGHT|DM_BITSPERPEL|DM_DISPLAYFREQUENCY|DM_DISPLAYFLAGS;
	ENSURE((dm.dmFields & expectedFlags) == expectedFlags);

	if(xres)
		*xres = (int)dm.dmPelsWidth;
	if(yres)
		*yres = (int)dm.dmPelsHeight;
	if(bpp)
		*bpp  = (int)dm.dmBitsPerPel;
	if(freq)
		*freq = (int)dm.dmDisplayFrequency;

	return INFO::OK;
}


Status GetMonitorSize(int& width_mm, int& height_mm)
{
	// (DC for the primary monitor's entire screen)
	const HDC hDC = GetDC(0);
	width_mm = GetDeviceCaps(hDC, HORZSIZE);
	height_mm = GetDeviceCaps(hDC, VERTSIZE);
	ReleaseDC(0, hDC);
	return INFO::OK;
}

}	// namespace gfx