Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement get_model_name on macOS and Windows. #99112

Merged
merged 1 commit into from
Nov 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion doc/classes/OS.xml
Original file line number Diff line number Diff line change
Expand Up @@ -356,7 +356,7 @@
<return type="String" />
<description>
Returns the model name of the current device.
[b]Note:[/b] This method is implemented on Android and iOS. Returns [code]"GenericDevice"[/code] on unsupported platforms.
[b]Note:[/b] This method is implemented on Android, iOS, macOS, and Windows. Returns [code]"GenericDevice"[/code] on unsupported platforms.
</description>
</method>
<method name="get_name" qualifiers="const">
Expand Down
2 changes: 2 additions & 0 deletions platform/macos/os_macos.h
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,8 @@ class OS_MacOS : public OS_Unix {
virtual String get_unique_id() const override;
virtual String get_processor_name() const override;

virtual String get_model_name() const override;

virtual bool is_sandboxed() const override;
virtual Vector<String> get_granted_permissions() const override;
virtual void revoke_granted_permissions() override;
Expand Down
9 changes: 9 additions & 0 deletions platform/macos/os_macos.mm
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,15 @@
initialize_core();
}

String OS_MacOS::get_model_name() const {
char buffer[256];
size_t buffer_len = 256;
if (sysctlbyname("hw.model", &buffer, &buffer_len, nullptr, 0) == 0 && buffer_len != 0) {
return String::utf8(buffer, buffer_len);
}
return OS_Unix::get_model_name();
}

String OS_MacOS::get_processor_name() const {
char buffer[256];
size_t buffer_len = 256;
Expand Down
28 changes: 28 additions & 0 deletions platform/windows/os_windows.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1740,6 +1740,34 @@ String OS_Windows::get_locale() const {
return "en";
}

String OS_Windows::get_model_name() const {
HKEY hkey;
if (RegOpenKeyExW(HKEY_LOCAL_MACHINE, L"Hardware\\Description\\System\\BIOS", 0, KEY_QUERY_VALUE, &hkey) != ERROR_SUCCESS) {
return OS::get_model_name();
}

String sys_name;
String board_name;
WCHAR buffer[256];
DWORD buffer_len = 256;
DWORD vtype = REG_SZ;
if (RegQueryValueExW(hkey, L"SystemProductName", nullptr, &vtype, (LPBYTE)buffer, &buffer_len) == ERROR_SUCCESS && buffer_len != 0) {
sys_name = String::utf16((const char16_t *)buffer, buffer_len).strip_edges();
}
buffer_len = 256;
if (RegQueryValueExW(hkey, L"BaseBoardProduct", nullptr, &vtype, (LPBYTE)buffer, &buffer_len) == ERROR_SUCCESS && buffer_len != 0) {
board_name = String::utf16((const char16_t *)buffer, buffer_len).strip_edges();
}
RegCloseKey(hkey);
if (!sys_name.is_empty() && sys_name.to_lower() != "system product name") {
return sys_name;
}
if (!board_name.is_empty() && board_name.to_lower() != "base board product") {
return board_name;
}
return OS::get_model_name();
}

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

Expand Down
2 changes: 2 additions & 0 deletions platform/windows/os_windows.h
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,8 @@ class OS_Windows : public OS {

virtual String get_processor_name() const override;

virtual String get_model_name() const override;

virtual uint64_t get_embedded_pck_offset() const override;

virtual String get_config_path() const override;
Expand Down