From 5f320af6e6e0a0e34b47169d8b6f965fdeec5f59 Mon Sep 17 00:00:00 2001 From: dijunkun Date: Mon, 29 Dec 2025 01:01:08 +0800 Subject: [PATCH] [fix] fix macOS clipboard monitoring compilation error using changeCount polling --- src/tools/clipboard_mac.mm | 35 +++++++++++++++++++++-------------- 1 file changed, 21 insertions(+), 14 deletions(-) diff --git a/src/tools/clipboard_mac.mm b/src/tools/clipboard_mac.mm index 3914692..d488a0f 100644 --- a/src/tools/clipboard_mac.mm +++ b/src/tools/clipboard_mac.mm @@ -70,20 +70,29 @@ void StartMacOSClipboardMonitoring() { CFRetain(g_monitor_runloop); } - // Register for clipboard change notifications - id observer = - [[NSNotificationCenter defaultCenter] addObserverForName:NSPasteboardDidChangeNotification - object:pasteboard - queue:nil - usingBlock:^(NSNotification* notification) { - if (!g_monitoring.load()) { - return; - } - HandleClipboardChange(); - }]; + // Track changeCount to detect clipboard changes + // Use __block to allow modification inside the block + __block NSInteger lastChangeCount = [pasteboard changeCount]; LOG_INFO("Clipboard event monitoring started (macOS)"); + // Use a timer to periodically check changeCount + // This is more reliable than NSPasteboardDidChangeNotification which may not be available + NSTimer* timer = + [NSTimer scheduledTimerWithTimeInterval:0.1 + repeats:YES + block:^(NSTimer* timer) { + if (!g_monitoring.load()) { + [timer invalidate]; + return; + } + NSInteger currentChangeCount = [pasteboard changeCount]; + if (currentChangeCount != lastChangeCount) { + lastChangeCount = currentChangeCount; + HandleClipboardChange(); + } + }]; + while (g_monitoring.load()) { @autoreleasepool { NSDate* date = [NSDate dateWithTimeIntervalSinceNow:0.1]; @@ -92,9 +101,7 @@ void StartMacOSClipboardMonitoring() { } // Cleanup - [[NSNotificationCenter defaultCenter] removeObserver:observer - name:NSPasteboardDidChangeNotification - object:pasteboard]; + [timer invalidate]; if (g_monitor_runloop) { CFRelease(g_monitor_runloop); g_monitor_runloop = nullptr;