File: XnOSWin32.cpp

package info (click to toggle)
openni 1.5.4.0%2Bdfsg-4
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 44,844 kB
  • sloc: cpp: 116,706; ansic: 58,777; sh: 10,287; cs: 7,698; java: 7,402; python: 1,541; makefile: 492; xml: 167
file content (282 lines) | stat: -rw-r--r-- 9,011 bytes parent folder | download | duplicates (7)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
/****************************************************************************
*                                                                           *
*  OpenNI 1.x Alpha                                                         *
*  Copyright (C) 2011 PrimeSense Ltd.                                       *
*                                                                           *
*  This file is part of OpenNI.                                             *
*                                                                           *
*  OpenNI is free software: you can redistribute it and/or modify           *
*  it under the terms of the GNU Lesser General Public License as published *
*  by the Free Software Foundation, either version 3 of the License, or     *
*  (at your option) any later version.                                      *
*                                                                           *
*  OpenNI is distributed in the hope that it will be useful,                *
*  but WITHOUT ANY WARRANTY; without even the implied warranty of           *
*  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the             *
*  GNU Lesser General Public License for more details.                      *
*                                                                           *
*  You should have received a copy of the GNU Lesser General Public License *
*  along with OpenNI. If not, see <http://www.gnu.org/licenses/>.           *
*                                                                           *
****************************************************************************/
//---------------------------------------------------------------------------
// Includes
//---------------------------------------------------------------------------
#include <XnOS.h>
#include <XnLog.h>

#ifdef __INTEL_COMPILER
	#include <ia32intrin.h>
#else
	#include <intrin.h>
#endif

//---------------------------------------------------------------------------
// Code
//---------------------------------------------------------------------------
static const XnChar* GetOSName(OSVERSIONINFOEX& osVersionInfo)
{
	if (osVersionInfo.dwMajorVersion == 4)
	{
		// Windows NT
		switch (osVersionInfo.wProductType)
		{
		case VER_NT_WORKSTATION:
			return "Windows NT 4.0 Workstation";
		case VER_NT_SERVER:
		case VER_NT_DOMAIN_CONTROLLER:
			return "Windows NT 4.0 Server";
		default:
			return "Unknown Windows NT 4.0 version";
		}
	}
	else if (osVersionInfo.dwMajorVersion == 5 && osVersionInfo.dwMinorVersion == 0)
	{
		// Windows 2000
		switch (osVersionInfo.wProductType)
		{
		case VER_NT_WORKSTATION:
			return "Windows 2000 Professional";
		case VER_NT_SERVER:
		case VER_NT_DOMAIN_CONTROLLER:
			if ((osVersionInfo.wSuiteMask & VER_SUITE_DATACENTER) == VER_SUITE_DATACENTER)
				return "Windows 2000 Datacenter Server";
			else if ((osVersionInfo.wSuiteMask & VER_SUITE_ENTERPRISE) == VER_SUITE_ENTERPRISE)
				return "Windows 2000 Advanced Server";
			else
				return "Windows 2000 Server";
		default:
			return "Unknown Windows 2000 version";
		}
	}
	else if (osVersionInfo.dwMajorVersion == 5 && osVersionInfo.dwMinorVersion == 1)
	{
		// Windows XP.
		if (0 != GetSystemMetrics(SM_TABLETPC))
			return "Windows XP Tablet PC Edition";
		else if (0 != GetSystemMetrics(SM_STARTER))
			return "Windows XP Starter Edition";
		else if (0 != GetSystemMetrics(SM_MEDIACENTER))
			return "Windows XP Media Center Edition";
		else if ((osVersionInfo.wSuiteMask & VER_SUITE_PERSONAL) == VER_SUITE_PERSONAL)
			return "Windows XP Home Edition";
		else
			return "Windows XP Professional";
	}
	else if (osVersionInfo.dwMajorVersion == 5 && osVersionInfo.dwMinorVersion == 2)
	{
		// Windows Server 2003
		if((osVersionInfo.wSuiteMask & VER_SUITE_DATACENTER) == VER_SUITE_DATACENTER)
			return "Windows Server 2003 Datacenter Edition";
		else if((osVersionInfo.wSuiteMask & VER_SUITE_ENTERPRISE) == VER_SUITE_ENTERPRISE)
			return "Windows Server 2003 Enterprise Edition";
		else if((osVersionInfo.wSuiteMask & VER_SUITE_BLADE) == VER_SUITE_BLADE)
			return "Windows Server 2003 Web Edition";
		else 
			return "Windows Server 2003 Standard Edition";
	}
	else if (osVersionInfo.dwMajorVersion == 6 && osVersionInfo.dwMinorVersion == 0)
	{
		// Windows Vista / Server 2008
		switch (osVersionInfo.wProductType)
		{
		case VER_NT_WORKSTATION:
			// Vista
			return "Windows Vista";
		case VER_NT_SERVER:
		case VER_NT_DOMAIN_CONTROLLER:
			return "Windows Server 2008";
		default:
			return "Unknown Windows 6.0 version";
		}
	}

	return "Unknown Windows Version";
}

static void GetCPUName(XnChar* csName)
{
	int CPUInfo[4] = {-1};
    xnOSMemSet(csName, 0, XN_MAX_OS_NAME_LENGTH);

	// calling cpuid with 0x80000000 retrives the number of extended properties
	__cpuid(CPUInfo, 0x80000000);
	unsigned int nExIds = CPUInfo[0];

	if (nExIds >= 0x80000002)
	{
		__cpuid(CPUInfo, 0x80000002);
		xnOSMemCopy(csName, CPUInfo, sizeof(CPUInfo));	
	}

	if (nExIds >= 0x80000003)
	{
		__cpuid(CPUInfo, 0x80000003);
		xnOSMemCopy(csName + 16, CPUInfo, sizeof(CPUInfo));	
	}

	if (nExIds >= 0x80000004)
	{
		__cpuid(CPUInfo, 0x80000004);
		xnOSMemCopy(csName + 32, CPUInfo, sizeof(CPUInfo));	
	}

	if (nExIds > 0x80000002)
		return;

	// else, we need to build the name ourselves
	xnOSMemSet(CPUInfo, 0xFF, sizeof(CPUInfo));

	__cpuid(CPUInfo, 0);
	int nIds = CPUInfo[0];

    *((int*)csName) = CPUInfo[1];
    *((int*)(csName+4)) = CPUInfo[3];
    *((int*)(csName+8)) = CPUInfo[2];

	if (nIds >= 1)
	{
		__cpuid(CPUInfo, 1);
        int nSteppingID = CPUInfo[0] & 0xf;
        int nModel = (CPUInfo[0] >> 4) & 0xf;
        int nFamily = (CPUInfo[0] >> 8) & 0xf;
        //int nProcessorType = (CPUInfo[0] >> 12) & 0x3;
        //int nExtendedmodel = (CPUInfo[0] >> 16) & 0xf;
        //int nExtendedfamily = (CPUInfo[0] >> 20) & 0xff;
        //int nBrandIndex = CPUInfo[1] & 0xff;

		sprintf(csName+strlen(csName), " Model: %d Family: %d Stepping: %d", nModel, nFamily, nSteppingID);
	}
}

XN_C_API XnStatus xnOSGetInfo(xnOSInfo* pOSInfo)
{
	// Validate input/output arguments
	XN_VALIDATE_OUTPUT_PTR(pOSInfo);

	// Get OS Info
	OSVERSIONINFOEX osVersionInfo;
	osVersionInfo.dwOSVersionInfoSize = sizeof(OSVERSIONINFOEX);
	if (0 == GetVersionEx((LPOSVERSIONINFO)&osVersionInfo))
	{
		DWORD nErr = GetLastError();
		xnLogWarning(XN_MASK_OS, "Failed getting OS version information. Error code: %d", nErr);
		return XN_STATUS_ERROR;
	}

	sprintf(pOSInfo->csOSName, "%s", GetOSName(osVersionInfo));
	if (osVersionInfo.szCSDVersion[0] != '\0')
	{
		strcat(pOSInfo->csOSName, " ");
		strcat(pOSInfo->csOSName, osVersionInfo.szCSDVersion);
	}

	// Get CPU Info
	GetCPUName(pOSInfo->csCPUName);

	SYSTEM_INFO systemInfo;
	GetSystemInfo(&systemInfo);
	pOSInfo->nProcessorsCount = systemInfo.dwNumberOfProcessors;

	// Get Memory Info
	MEMORYSTATUSEX memoryStatus;
	memoryStatus.dwLength = sizeof(memoryStatus);
	if (0 == GlobalMemoryStatusEx(&memoryStatus))
	{
		DWORD nErr = GetLastError();
		xnLogWarning(XN_MASK_OS, "Failed getting Memory information. Error code: %d", nErr);
		return XN_STATUS_ERROR;
	}

	pOSInfo->nTotalMemory = memoryStatus.ullTotalPhys;

	return (XN_STATUS_OK);
}

XnStatus XnWin32CreateKernelObjectName(XnChar* strDest, const XnUInt32 nDestLength, const XnChar* strSource, XnBool bAllowOtherUsers)
{
	XnChar* pDest = strDest;
	XnChar* pDestEnd = strDest + nDestLength;

	if (bAllowOtherUsers)
	{
		static const XnChar strGlobal[] = "Global\\";
		strcpy(strDest, strGlobal);
		pDest = strDest + sizeof(strGlobal) - 1;
	}

	const XnChar* pSource = strSource;

	while (pDest < pDestEnd && *pSource != '\0')
	{
		*pDest = *pSource == '\\' ? '_' : *pSource;
		++pDest;
		++pSource;
	}

	if (pDest == pDestEnd)
	{
		xnLogWarning(XN_MASK_OS, "Event name is too long!");
		return XN_STATUS_ERROR;
	}

	*pDest = '\0';

	return (XN_STATUS_OK);
}

XnStatus XnWin32GetSecurityAttributes(XnBool bAllowOtherUsers, SECURITY_ATTRIBUTES** ppSecurityAttributes)
{
	*ppSecurityAttributes = NULL;

	static SECURITY_DESCRIPTOR securityDesc;
	static SECURITY_ATTRIBUTES securityAttributes;
	static SECURITY_ATTRIBUTES* pSecurityAttributes = NULL;

	if (pSecurityAttributes == NULL)
	{
		// initialize it now
		if (FALSE == InitializeSecurityDescriptor(&securityDesc, SECURITY_DESCRIPTOR_REVISION))
		{
			xnLogError(XN_MASK_OS, "Failed to initialize security descriptor (%d)", GetLastError());
			return XN_STATUS_ERROR;
		}

		if (FALSE == SetSecurityDescriptorDacl(&securityDesc, TRUE, NULL, FALSE))
		{
			xnLogError(XN_MASK_OS, "Failed to set security descriptor DACL (%d)", GetLastError());
			return XN_STATUS_ERROR;
		}

		securityAttributes.nLength = sizeof(SECURITY_ATTRIBUTES);
		securityAttributes.lpSecurityDescriptor = &securityDesc;
		securityAttributes.bInheritHandle = FALSE;

		// initialization complete. Store a pointer
		pSecurityAttributes = &securityAttributes;
	}

	*ppSecurityAttributes = bAllowOtherUsers ? pSecurityAttributes : NULL;

	return (XN_STATUS_OK);
}