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
|
/*
================================================================================================
Description : Basic system info.
Author : J.M.P. van Waveren
Date : 12/10/2016
Language : C99
Format : Real tabs with the tab size equal to 4 spaces.
Copyright : Copyright (c) 2016 Oculus VR, LLC. All Rights reserved.
LICENSE
=======
Copyright (c) 2016 Oculus VR, LLC.
SPDX-License-Identifier: Apache-2.0
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
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.
================================================================================================
*/
#if !defined( KSSYSINFO_H )
#define KSSYSINFO_H
#if defined( WIN32 ) || defined( _WIN32 ) || defined( WIN64 ) || defined( _WIN64 )
#if !defined( OS_WINDOWS )
#define OS_WINDOWS
#endif
#elif defined( __ANDROID__ )
#if !defined( OS_ANDROID )
#define OS_ANDROID
#endif
#elif defined( __hexagon__ ) || defined( __qdsp6__ )
#if !defined( OS_HEXAGON )
#define OS_HEXAGON
#endif
#elif defined( __APPLE__ )
#if !defined( OS_APPLE )
#define OS_APPLE
#endif
#include <Availability.h>
#if __IPHONE_OS_VERSION_MAX_ALLOWED && !defined( OS_APPLE_IOS )
#define OS_APPLE_IOS
#elif __MAC_OS_X_VERSION_MAX_ALLOWED && !defined( OS_APPLE_MACOS )
#define OS_APPLE_MACOS
#endif
#elif defined( __linux__ )
#if !defined( OS_LINUX )
#define OS_LINUX
#endif
#else
#error "unknown platform"
#endif
#if defined( OS_WINDOWS )
#include <windows.h>
#elif defined( OS_APPLE )
#include <Foundation/NSString.h>
#include <Foundation/NSProcessInfo.h>
#elif defined( OS_ANDROID )
#include <dlfcn.h> // for dlopen
#endif
#include <stdio.h>
static const char * GetOSVersion()
{
#if defined( OS_WINDOWS )
HKEY hKey = 0;
if ( RegOpenKeyA( HKEY_LOCAL_MACHINE, "SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion", &hKey ) == ERROR_SUCCESS )
{
static char version[1024];
DWORD version_length = sizeof( version );
DWORD dwType = REG_SZ;
DWORD dwRet = RegQueryValueExA( hKey, "ProductName", NULL, &dwType, (LPBYTE)&version, &version_length );
RegCloseKey( hKey );
if ( dwRet == ERROR_SUCCESS )
{
return version;
}
}
return "Microsoft Windows";
#elif defined( OS_LINUX )
static char buffer[1024];
FILE * os_release = fopen( "/etc/os-release", "r" );
if ( os_release != NULL )
{
while ( fgets( buffer, sizeof( buffer ), os_release ) )
{
if ( strncmp( buffer, "PRETTY_NAME=", 12 ) == 0 )
{
char * pretty_name = buffer + 12;
// remove newline and enclosing quotes
while( pretty_name[0] == ' ' ||
pretty_name[0] == '\t' ||
pretty_name[0] == ':' ||
pretty_name[0] == '\'' ||
pretty_name[0] == '\"' )
{
pretty_name++;
}
int last = strlen( pretty_name ) - 1;
while( last >= 0 && (
pretty_name[last] == '\n' ||
pretty_name[last] == '\'' ||
pretty_name[last] == '\"' ) )
{
pretty_name[last--] = '\0';
}
return pretty_name;
}
}
fclose( os_release );
}
return "Linux";
#elif defined( OS_APPLE_MACOS )
return [NSString stringWithFormat: @"Apple macOS %@", NSProcessInfo.processInfo.operatingSystemVersionString].UTF8String;
#elif defined( OS_APPLE_IOS )
return [NSString stringWithFormat: @"Apple iOS %@", NSProcessInfo.processInfo.operatingSystemVersionString].UTF8String;
#elif defined( OS_ANDROID )
static char version[1024];
#define PROP_NAME_MAX 32
#define PROP_VALUE_MAX 92
char release[PROP_VALUE_MAX] = { 0 };
char build[PROP_VALUE_MAX] = { 0 };
void * handle = dlopen( "libc.so", RTLD_NOLOAD );
if ( handle != NULL )
{
typedef int (*PFN_SYSTEM_PROP_GET)(const char *, char *);
PFN_SYSTEM_PROP_GET __my_system_property_get = (PFN_SYSTEM_PROP_GET)dlsym( handle, "__system_property_get" );
if ( __my_system_property_get != NULL )
{
__my_system_property_get( "ro.build.version.release", release );
__my_system_property_get( "ro.build.version.incremental", build );
}
}
snprintf( version, sizeof( version ), "Android %s (%s)", release, build );
return version;
#endif
}
static const char * GetCPUVersion()
{
#if defined( OS_WINDOWS )
HKEY hKey = 0;
if ( RegOpenKeyA( HKEY_LOCAL_MACHINE, "HARDWARE\\DESCRIPTION\\System\\CentralProcessor\\0", &hKey ) == ERROR_SUCCESS )
{
static char processor[1024];
DWORD processor_length = sizeof( processor );
DWORD dwType = REG_SZ;
DWORD dwRet = RegQueryValueExA( hKey, "ProcessorNameString", NULL, &dwType, (LPBYTE)&processor, &processor_length );
RegCloseKey( hKey );
if ( dwRet == ERROR_SUCCESS )
{
return processor;
}
}
return "unknown";
#elif defined( OS_APPLE )
static char processor[1024];
size_t processor_length = sizeof( processor );
sysctlbyname( "machdep.cpu.brand_string", &processor, &processor_length, NULL, 0 );
return processor;
#elif defined( OS_LINUX ) || defined( OS_ANDROID )
struct
{
const char * key;
char value[1024];
} keyValues[] =
{
{ "model name", "" },
{ "Processor", "" },
{ "Hardware", "" }
};
static char name[1024];
FILE * cpuinfo = fopen( "/proc/cpuinfo", "r" );
if ( cpuinfo != NULL )
{
char buffer[1024];
while ( fgets( buffer, sizeof( buffer ), cpuinfo ) )
{
for ( int i = 0; i < (int)( sizeof( keyValues ) / sizeof( keyValues[0] ) ); i++ )
{
const size_t length = strlen( keyValues[i].key );
if ( strncmp( buffer, keyValues[i].key, length ) == 0 )
{
char * pretty_name = buffer + length;
// remove newline and enclosing quotes
while( pretty_name[0] == ' ' ||
pretty_name[0] == '\t' ||
pretty_name[0] == ':' ||
pretty_name[0] == '\'' ||
pretty_name[0] == '\"' )
{
pretty_name++;
}
int last = strlen( pretty_name ) - 1;
while( last >= 0 && (
pretty_name[last] == '\n' ||
pretty_name[last] == '\'' ||
pretty_name[last] == '\"' ) )
{
pretty_name[last--] = '\0';
}
strcpy( keyValues[i].value, pretty_name );
break;
}
}
}
fclose( cpuinfo );
snprintf( name, sizeof(name), "%s%s%s", keyValues[2].value,
( keyValues[2].value[0] != '\0' ) ? " - " : "",
( keyValues[0].value[0] != '\0' ) ? keyValues[0].value : keyValues[1].value );
return name;
}
return "unknown";
#endif
}
#endif // !KSSYSINFO_H
|