Skip to content

Commit

Permalink
Merge pull request #58157 from Calinou/os-add-get-processor-name-3.x
Browse files Browse the repository at this point in the history
  • Loading branch information
akien-mga authored Feb 17, 2022
2 parents ac0d37d + 7f9e974 commit 47e34dc
Show file tree
Hide file tree
Showing 13 changed files with 78 additions and 4 deletions.
5 changes: 5 additions & 0 deletions core/bind/core_bind.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -960,6 +960,10 @@ int _OS::get_processor_count() const {
return OS::get_singleton()->get_processor_count();
}

String _OS::get_processor_name() const {
return OS::get_singleton()->get_processor_name();
}

bool _OS::is_stdout_verbose() const {
return OS::get_singleton()->is_stdout_verbose();
}
Expand Down Expand Up @@ -1319,6 +1323,7 @@ void _OS::_bind_methods() {
ClassDB::bind_method(D_METHOD("get_low_processor_usage_mode_sleep_usec"), &_OS::get_low_processor_usage_mode_sleep_usec);

ClassDB::bind_method(D_METHOD("get_processor_count"), &_OS::get_processor_count);
ClassDB::bind_method(D_METHOD("get_processor_name"), &_OS::get_processor_name);

ClassDB::bind_method(D_METHOD("get_executable_path"), &_OS::get_executable_path);
ClassDB::bind_method(D_METHOD("execute", "path", "arguments", "blocking", "output", "read_stderr", "open_console"), &_OS::execute, DEFVAL(true), DEFVAL(Array()), DEFVAL(false), DEFVAL(false));
Expand Down
1 change: 1 addition & 0 deletions core/bind/core_bind.h
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,7 @@ class _OS : public Object {
bool is_stdout_verbose() const;

int get_processor_count() const;
String get_processor_name() const;

enum SystemDir {
SYSTEM_DIR_DESKTOP,
Expand Down
4 changes: 4 additions & 0 deletions core/os/os.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -512,6 +512,10 @@ int OS::get_processor_count() const {
return 1;
}

String OS::get_processor_name() const {
return "";
}

Error OS::native_video_play(String p_path, float p_volume, String p_audio_track, String p_subtitle_track) {
return FAILED;
}
Expand Down
1 change: 1 addition & 0 deletions core/os/os.h
Original file line number Diff line number Diff line change
Expand Up @@ -519,6 +519,7 @@ class OS {
virtual bool is_custom_exit_code();

virtual int get_processor_count() const;
virtual String get_processor_name() const;

virtual String get_unique_id() const;

Expand Down
9 changes: 8 additions & 1 deletion doc/classes/OS.xml
Original file line number Diff line number Diff line change
Expand Up @@ -345,7 +345,14 @@
<method name="get_processor_count" qualifiers="const">
<return type="int" />
<description>
Returns the number of threads available on the host machine.
Returns the number of [i]logical[/i] CPU cores available on the host machine. On CPUs with HyperThreading enabled, this number will be greater than the number of [i]physical[/i] CPU cores.
</description>
</method>
<method name="get_processor_name" qualifiers="const">
<return type="String" />
<description>
Returns the name of the CPU model on the host machine (e.g. "Intel(R) Core(TM) i7-6700K CPU @ 4.00GHz").
[b]Note:[/b] This method is only implemented on Windows, macOS, Linux and iOS. On Android, HTML5 and UWP, [method get_processor_name] returns an empty string.
</description>
</method>
<method name="get_real_window_size" qualifiers="const">
Expand Down
1 change: 1 addition & 0 deletions platform/iphone/os_iphone.h
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ class OSIPhone : public OS_Unix {
String get_locale() const;

String get_unique_id() const;
virtual String get_processor_name() const;

virtual void vibrate_handheld(int p_duration_ms = 500);

Expand Down
10 changes: 10 additions & 0 deletions platform/iphone/os_iphone.mm
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@

#import <UIKit/UIKit.h>
#include <dlfcn.h>
#include <sys/sysctl.h>
#import <sys/utsname.h>

extern int gl_view_base_fb; // from gl_view.mm
Expand Down Expand Up @@ -665,6 +666,15 @@ void register_dynamic_symbol(char *name, void *address) {
}
}

String OSIPhone::get_processor_name() const {
char buffer[256];
size_t buffer_len = 256;
if (sysctlbyname("machdep.cpu.brand_string", &buffer, &buffer_len, NULL, 0) == 0) {
return String::utf8(buffer, buffer_len);
}
ERR_FAIL_V_MSG("", String("Couldn't get the CPU model name. Returning an empty string."));
}

void OSIPhone::vibrate_handheld(int p_duration_ms) {
// iOS does not support duration for vibration
AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);
Expand Down
1 change: 1 addition & 0 deletions platform/osx/os_osx.h
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,7 @@ class OS_OSX : public OS_Unix {
virtual String get_ime_text() const;

virtual String get_unique_id() const;
virtual String get_processor_name() const;

virtual OS::PowerState get_power_state();
virtual int get_power_seconds_left();
Expand Down
14 changes: 11 additions & 3 deletions platform/osx/os_osx.mm
Original file line number Diff line number Diff line change
Expand Up @@ -41,16 +41,15 @@
#include "servers/visual/visual_server_raster.h"

#include <mach-o/dyld.h>
#include <os/log.h>
#include <sys/sysctl.h>

#include <Carbon/Carbon.h>
#import <Cocoa/Cocoa.h>
#include <IOKit/IOCFPlugIn.h>
#include <IOKit/IOKitLib.h>
#include <IOKit/hid/IOHIDKeys.h>
#include <IOKit/hid/IOHIDLib.h>
#if MAC_OS_X_VERSION_MAX_ALLOWED >= 101200
#include <os/log.h>
#endif

#include <dlfcn.h>
#include <fcntl.h>
Expand Down Expand Up @@ -1510,6 +1509,15 @@ - (BOOL)canBecomeKeyWindow {
im_position = p_pos;
}

String OS_OSX::get_processor_name() const {
char buffer[256];
size_t buffer_len = 256;
if (sysctlbyname("machdep.cpu.brand_string", &buffer, &buffer_len, NULL, 0) == 0) {
return String::utf8(buffer, buffer_len);
}
ERR_FAIL_V_MSG("", String("Couldn't get the CPU model name. Returning an empty string."));
}

void OS_OSX::initialize_core() {
crash_handler.initialize();

Expand Down
20 changes: 20 additions & 0 deletions platform/windows/os_windows.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3130,6 +3130,26 @@ int OS_Windows::get_processor_count() const {
return sysinfo.dwNumberOfProcessors;
}

String OS_Windows::get_processor_name() const {
const String id = "Hardware\\Description\\System\\CentralProcessor\\0";

HKEY hkey;
if (RegOpenKeyExW(HKEY_LOCAL_MACHINE, (LPCWSTR)(id.c_str()), 0, KEY_QUERY_VALUE, &hkey) != ERROR_SUCCESS) {
ERR_FAIL_V_MSG("", String("Couldn't get the CPU model name. Returning an empty string."));
}

WCHAR buffer[256];
DWORD buffer_len = 256;
DWORD vtype = REG_SZ;
if (RegQueryValueExW(hkey, L"ProcessorNameString", NULL, &vtype, (LPBYTE)buffer, &buffer_len) == ERROR_SUCCESS) {
RegCloseKey(hkey);
return String((const wchar_t *)buffer, buffer_len).strip_edges();
} else {
RegCloseKey(hkey);
ERR_FAIL_V_MSG("", String("Couldn't get the CPU model name. Returning an empty string."));
}
}

OS::LatinKeyboardVariant OS_Windows::get_latin_keyboard_variant() const {
unsigned long azerty[] = {
0x00020401, // Arabic (102) AZERTY
Expand Down
1 change: 1 addition & 0 deletions platform/windows/os_windows.h
Original file line number Diff line number Diff line change
Expand Up @@ -520,6 +520,7 @@ class OS_Windows : public OS {
virtual String get_locale() const;

virtual int get_processor_count() const;
virtual String get_processor_name() const;

virtual LatinKeyboardVariant get_latin_keyboard_variant() const;
virtual int keyboard_get_layout_count() const;
Expand Down
14 changes: 14 additions & 0 deletions platform/x11/os_x11.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -806,6 +806,20 @@ String OS_X11::get_unique_id() const {
return machine_id;
}

String OS_X11::get_processor_name() const {
FileAccessRef f = FileAccess::open("/proc/cpuinfo", FileAccess::READ);
ERR_FAIL_COND_V_MSG(!f, "", String("Couldn't open `/proc/cpuinfo` to get the CPU model name. Returning an empty string."));

while (!f->eof_reached()) {
const String line = f->get_line();
if (line.find("model name") != -1) {
return line.split(":")[1].strip_edges();
}
}

ERR_FAIL_V_MSG("", String("Couldn't get the CPU model name from `/proc/cpuinfo`. Returning an empty string."));
}

void OS_X11::finalize() {
events_thread_done = true;
events_thread.wait_to_finish();
Expand Down
1 change: 1 addition & 0 deletions platform/x11/os_x11.h
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,7 @@ class OS_X11 : public OS_Unix {
virtual void set_ime_position(const Point2 &p_pos);

virtual String get_unique_id() const;
virtual String get_processor_name() const;

virtual void move_window_to_foreground();
virtual void alert(const String &p_alert, const String &p_title = "ALERT!");
Expand Down

0 comments on commit 47e34dc

Please sign in to comment.