Debian Codegraph @

components/extension/protocols/systeminfo.cpp

raw symbols SCIP
1 /* fre:ac - free audio converter
2 * Copyright (C) 2001-2022 Robert Kausch <[email protected]>
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License as
6 * published by the Free Software Foundation, either version 2 of
7 * the License, or (at your option) any later version.
8 *
9 * THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR
10 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
11 * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE. */
12
13 #include "systeminfo.h"
14
15 #ifdef __WIN32__
16 # include <windows.h>
17 #else
18 # include <stdio.h>
19
20 # if defined __linux__
21 # include <sys/sysinfo.h>
22 # elif defined __APPLE__ || defined __FreeBSD__ || defined __NetBSD__ || defined __OpenBSD__
23 # include <sys/sysctl.h>
24 # endif
25 #endif
26
27 #ifdef __WIN32__
28 namespace BoCA
29 {
30 static String QueryStringValue(HKEY key, const String &id)
31 {
32 Buffer<wchar_t> buffer(2048);
33 DWORD size = buffer.Size() - 1;
34
35 buffer.Zero();
36
37 RegQueryValueExW(key, id, NULL, NULL, (BYTE *) (wchar_t *) buffer, &size);
38
39 return String(buffer);
40 }
41 }
42 #endif
43
44 const String &BoCA::SystemInfo::GetOperatingSystem()
45 {
46 static String operatingSystem;
47
48 if (operatingSystem == NIL)
49 {
50 #if defined __WIN32__
51 operatingSystem = "Windows (unknown version)";
52
53 HKEY currentVersion;
54
55 if (RegOpenKeyW(HKEY_LOCAL_MACHINE, L"Software\\Microsoft\\Windows NT\\CurrentVersion", &currentVersion) == ERROR_SUCCESS)
56 {
57 String productName = QueryStringValue(currentVersion, "ProductName");
58 String servicePack = QueryStringValue(currentVersion, "CSDVersion");
59 String displayVersion = QueryStringValue(currentVersion, "DisplayVersion");
60 String buildNumber = QueryStringValue(currentVersion, "CurrentBuildNumber");
61
62 RegCloseKey(currentVersion);
63
64 if (buildNumber.ToInt() >= 22000) productName.Replace("Windows 10", "Windows 11");
65
66 operatingSystem = String(productName)
67 .Append(" (").Append(servicePack != NIL ? String(servicePack).Append(", ") : String())
68 .Append(displayVersion != NIL ? String("Release ").Append(displayVersion).Append(", ") : String())
69 .Append(buildNumber != NIL ? String("Build ").Append(buildNumber) : "unknown build").Append(")");
70 }
71 #else
72 Buffer<char> buffer(2048);
73
74 buffer.Zero();
75
76 # if defined __linux__
77 operatingSystem = "Linux (unknown version)";
78
79 FILE *pstdin = popen("echo \"`lsb_release -ds || uname -o` (`uname -s` `uname -r`)\"", "r");
80 # elif defined __APPLE__
81 operatingSystem = "macOS (unknown version)";
82
83 FILE *pstdin = popen("echo \"`sw_vers -productName` `sw_vers -productVersion`\"", "r");
84 # elif defined __HAIKU__
85 operatingSystem = "Haiku (unknown version)";
86
87 FILE *pstdin = popen("echo \"`uname -o` `uname -r` (`uname -v`)\"", "r");
88 # else
89 operatingSystem = "Unknown";
90
91 FILE *pstdin = popen("echo \"`uname -s` `uname -r`\"", "r");
92 # endif
93
94 if (fscanf(pstdin, String("%[^\n]").Append(String::FromInt(buffer.Size() - 1)), (char *) buffer) > 0) operatingSystem = buffer;
95
96 pclose(pstdin);
97 #endif
98 }
99
100 return operatingSystem;
101 }
102
103 const String &BoCA::SystemInfo::GetArchitecture()
104 {
105 #if defined __i386__ || defined _M_IX86
106 static String architecture = "x86";
107 #elif defined __x86_64__ || defined _M_AMD64
108 static String architecture = "x86-64";
109 #elif defined __arm__ || defined _M_ARM
110 static String architecture = "ARM";
111 #elif defined __aarch64__ || defined _M_ARM64
112 static String architecture = "ARM64";
113 #elif defined __mips__
114 static String architecture = "MIPS";
115 #elif defined __ppc__
116 static String architecture = "PPC";
117 #elif defined __ppc64__
118 static String architecture = "PPC64";
119 #else
120 static String architecture = "unknown architecture";
121 #endif
122
123 return architecture;
124 }
125
126 const String &BoCA::SystemInfo::GetCPUModel()
127 {
128 static String cpuModel = S::System::CPU().GetBrandString();
129
130 if (cpuModel == NIL)
131 {
132 String architecture = GetArchitecture();
133 Int numCores = S::System::CPU().GetNumCores();
134 Int numThreads = S::System::CPU().GetNumLogicalCPUs();
135
136 cpuModel = String("unidentified ").Append(String::FromInt(numCores)).Append(" core ")
137 .Append(numThreads != numCores ? String("(").Append(String::FromInt(numThreads)).Append(" threads) ") : String())
138 .Append(architecture != "unknown architecture" ? String(architecture).Append(" ") : String()).Append("CPU");
139 }
140
141 return cpuModel;
142 }
143
144 const String &BoCA::SystemInfo::GetInstalledRAM()
145 {
146 static String installedRAM;
147
148 if (installedRAM == NIL)
149 {
150 UnsignedInt64 installedMemory = 0;
151
152 #if defined __WIN32__
153 MEMORYSTATUSEX memory;
154
155 memory.dwLength = sizeof(MEMORYSTATUSEX);
156
157 if (GlobalMemoryStatusEx(&memory)) installedMemory = memory.ullTotalPhys;
158 #elif defined __linux__
159 struct sysinfo info;
160
161 if (sysinfo(&info) == 0) installedMemory = UnsignedInt64(info.totalram) * info.mem_unit;
162 #elif defined __APPLE__ || defined __FreeBSD__ || defined __NetBSD__ || defined __OpenBSD__
163 # if defined HW_MEMSIZE
164 int mib[2] = { CTL_HW, HW_MEMSIZE };
165 # elif defined HW_PHYSMEM64
166 int mib[2] = { CTL_HW, HW_PHYSMEM64 };
167 # else
168 int mib[2] = { CTL_HW, HW_PHYSMEM };
169 # endif
170
171 size_t size = sizeof(installedMemory);
172
173 sysctl(mib, 2, &installedMemory, &size, NULL, 0);
174 #endif
175
176 Float amount = Math::Ceil(Float(installedMemory) / 1024.0 / 128.0) / 8.0;
177 String unit = "MB";
178
179 if (amount > 768)
180 {
181 amount = Math::Ceil(amount / 512.0) / 2.0;
182 unit = "GB";
183 }
184
185 if (installedMemory) installedRAM = String::FromFloat(amount).Append(" ").Append(unit);
186 else installedRAM = "unknown";
187 }
188
189 return installedRAM;
190 }
191
192 const String &BoCA::SystemInfo::GetNewLineCharacters()
193 {
194 #ifdef __WIN32__
195 static String newLine = "\r\n";
196 #else
197 static String newLine = "\n";
198 #endif
199
200 return newLine;
201 }