Function to get machine ID on cocoa

This commit is contained in:
Kovid Goyal 2026-04-09 07:04:12 +05:30
parent 47b5b2ea65
commit c08409a981
No known key found for this signature in database
GPG key ID: 06BC317B515ACE7C
3 changed files with 27 additions and 1 deletions

View file

@ -12,6 +12,7 @@
#include <Availability.h>
#include <Carbon/Carbon.h>
#include <Cocoa/Cocoa.h>
#import <IOKit/IOKitLib.h>
#include <UserNotifications/UserNotifications.h>
#import <AudioToolbox/AudioServices.h>
@ -1382,9 +1383,33 @@ cocoa_clear_dock_badge_if_set(void) {
// }}}
static PyObject*
cocoa_get_machine_id(PyObject *self UNUSED, PyObject *args UNUSED) {
static char ans[1024] = {0};
static bool done = false;
if (!done) {
done = true;
CFMutableDictionaryRef matching = IOServiceMatching("IOPlatformExpertDevice");
// Get the matching service
io_service_t service = IOServiceGetMatchingService(kIOMainPortDefault, matching);
if (service) {
CFTypeRef uuid = IORegistryEntryCreateCFProperty(service, CFSTR("IOPlatformUUID"), kCFAllocatorDefault, 0);
if (uuid) {
// Transfer ownership to NSString using ARC __bridge_transfer
NSString *s = (NSString*)uuid;
[s getCString:ans maxLength:sizeof(ans) encoding:NSUTF8StringEncoding];
}
// Release the I/O object
IOObjectRelease(service);
}
}
return PyUnicode_FromString(ans);
}
static PyMethodDef module_methods[] = {
{"cocoa_play_system_sound_by_id_async", play_system_sound_by_id_async, METH_O, ""},
{"cocoa_get_lang", (PyCFunction)cocoa_get_lang, METH_NOARGS, ""},
{"cocoa_get_machine_id", (PyCFunction)cocoa_get_machine_id, METH_NOARGS, ""},
{"cocoa_set_global_shortcut", (PyCFunction)cocoa_set_global_shortcut, METH_VARARGS, ""},
{"cocoa_send_notification", (PyCFunction)(void(*)(void))cocoa_send_notification, METH_VARARGS | METH_KEYWORDS, ""},
{"cocoa_remove_delivered_notification", (PyCFunction)cocoa_remove_delivered_notification, METH_O, ""},

View file

@ -1781,6 +1781,7 @@ def base64_decode(src: Union[str, ReadableBuffer]) -> bytes: ...
def base64_decode_into(src: Union[str, ReadableBuffer], output: WriteableBuffer) -> int: ...
def cocoa_recreate_global_menu() -> None: ...
def cocoa_clear_global_shortcuts() -> None: ...
def cocoa_get_machine_id() -> str: ...
def update_pointer_shape(os_window_id: int) -> None: ...
def is_layer_shell_supported() -> bool: ...
def os_window_focus_counters() -> Dict[int, int]: ...

View file

@ -655,7 +655,7 @@ def kitty_env(args: Options) -> Env:
if is_macos:
platform_libs = [
'-framework', 'Carbon', '-framework', 'CoreText', '-framework', 'CoreGraphics',
'-framework', 'AudioToolbox',
'-framework', 'AudioToolbox', '-framework', 'IOKit',
]
test_program_src = '''#include <UserNotifications/UserNotifications.h>
int main(void) { return 0; }\n'''