From c08409a9819d40f3e700ed439c85edc5d8c58de7 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Thu, 9 Apr 2026 07:04:12 +0530 Subject: [PATCH] Function to get machine ID on cocoa --- kitty/cocoa_window.m | 25 +++++++++++++++++++++++++ kitty/fast_data_types.pyi | 1 + setup.py | 2 +- 3 files changed, 27 insertions(+), 1 deletion(-) diff --git a/kitty/cocoa_window.m b/kitty/cocoa_window.m index 84b80900f..d8b8a641b 100644 --- a/kitty/cocoa_window.m +++ b/kitty/cocoa_window.m @@ -12,6 +12,7 @@ #include #include #include +#import #include #import @@ -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, ""}, diff --git a/kitty/fast_data_types.pyi b/kitty/fast_data_types.pyi index 05f65ffe9..4a598660c 100644 --- a/kitty/fast_data_types.pyi +++ b/kitty/fast_data_types.pyi @@ -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]: ... diff --git a/setup.py b/setup.py index 12fa0e663..5844c4e31 100755 --- a/setup.py +++ b/setup.py @@ -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 int main(void) { return 0; }\n'''