From bcdc2cb8e5739e4a0af16c536c4ebdd8b761de60 Mon Sep 17 00:00:00 2001 From: Elias Aebi Date: Sat, 17 Feb 2024 18:25:16 +0100 Subject: [PATCH] macos: implement the directory watcher --- gral_macos.m | 25 ++++++++++++++++++++----- 1 file changed, 20 insertions(+), 5 deletions(-) diff --git a/gral_macos.m b/gral_macos.m index 367c3a6..f78eaa6 100644 --- a/gral_macos.m +++ b/gral_macos.m @@ -136,7 +136,7 @@ void gral_image_delete(struct gral_image *image) { } struct gral_font *gral_font_create(struct gral_window *window, char const *name, float size) { - CFStringRef string = CFStringCreateWithCString(NULL, name, kCFStringEncodingUTF8); + CFStringRef string = CFStringCreateWithCString(kCFAllocatorDefault, name, kCFStringEncodingUTF8); CTFontRef font = CTFontCreateWithName(string, size, NULL); CFRelease(string); return (struct gral_font *)font; @@ -160,7 +160,7 @@ void gral_font_get_metrics(struct gral_window *window, struct gral_font *font, f } struct gral_text *gral_text_create(struct gral_window *window, char const *text, struct gral_font *font) { - CFStringRef string = CFStringCreateWithCString(NULL, text, kCFStringEncodingUTF8); + CFStringRef string = CFStringCreateWithCString(kCFAllocatorDefault, text, kCFStringEncodingUTF8); CFMutableDictionaryRef attributes = CFDictionaryCreateMutable(NULL, 1, &kCFCopyStringDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks); CFDictionarySetValue(attributes, kCTFontAttributeName, font); CFAttributedStringRef attributed_string = CFAttributedStringCreate(NULL, string, attributes); @@ -710,6 +710,7 @@ void gral_run_on_main_thread(void (*callback)(void *user_data), void *user_data) #include #include #include +#include struct gral_file *gral_file_open_read(char const *path) { int fd = open(path, O_RDONLY); @@ -784,13 +785,27 @@ void gral_directory_remove(char const *path) { rmdir(path); } +static void directory_watcher_callback(CFFileDescriptorRef fdref, CFOptionFlags flags, void *info) { + int fd = CFFileDescriptorGetNativeDescriptor(fdref); + struct kevent event; + kevent(fd, NULL, 0, &event, 1, NULL); +} + struct gral_directory_watcher *gral_directory_watch(char const *path, void (*callback)(void *user_data), void *user_data) { - // TODO: implement - return NULL; + int fd = kqueue(); + struct kevent event; + EV_SET(&event, open(path, O_EVTONLY), EVFILT_VNODE, EV_ADD, NOTE_WRITE | NOTE_DELETE, 0, NULL); + kevent(fd, &event, 1, NULL, 0, NULL); + CFFileDescriptorRef fdref = CFFileDescriptorCreate(kCFAllocatorDefault, fd, TRUE, &directory_watcher_callback, NULL); + CFFileDescriptorEnableCallBacks(fdref, kCFFileDescriptorReadCallBack); + CFRunLoopSourceRef source = CFFileDescriptorCreateRunLoopSource(kCFAllocatorDefault, fdref, 0); + CFRunLoopAddSource(CFRunLoopGetMain(), source, kCFRunLoopDefaultMode); + CFRelease(source); + return (struct gral_directory_watcher *)fdref; } void gral_directory_watcher_delete(struct gral_directory_watcher *directory_watcher) { - // TODO: implement + }