diff --git a/gral_macos.m b/gral_macos.m index 367c3a6..b0b3cd2 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,55 @@ void gral_directory_remove(char const *path) { rmdir(path); } +static void callback_object_release(void const *info) { + CallbackObject *callback_object = info; + [callback_object release]; +} +static void const *callback_object_retain(void const *info) { + CallbackObject *callback_object = info; + [callback_object retain]; + return info; +} + +static void directory_watcher_callback(CFFileDescriptorRef fdref, CFOptionFlags flags, void *info) { + CallbackObject *callback_object = info; + int fd = CFFileDescriptorGetNativeDescriptor(fdref); + struct kevent event; + kevent(fd, NULL, 0, &event, 1, NULL); + callback_object->callback(callback_object->user_data); + CFFileDescriptorEnableCallBacks(fdref, kCFFileDescriptorReadCallBack); +} + struct gral_directory_watcher *gral_directory_watch(char const *path, void (*callback)(void *user_data), void *user_data) { - // TODO: implement - return NULL; + CallbackObject *callback_object = [[CallbackObject alloc] init]; + callback_object->callback = callback; + callback_object->user_data = user_data; + int fd = kqueue(); + struct kevent event; + event.ident = open(path, O_EVTONLY); // TODO: close + event.filter = EVFILT_VNODE; + event.flags = EV_ADD | EV_CLEAR; + event.fflags = NOTE_WRITE; + event.data = 0; + event.udata = NULL; + kevent(fd, &event, 1, NULL, 0, NULL); + CFFileDescriptorContext context; + context.copyDescription = NULL; + context.info = callback_object; + context.release = callback_object_release; + context.retain = callback_object_retain; + context.version = 0; + CFFileDescriptorRef fdref = CFFileDescriptorCreate(kCFAllocatorDefault, fd, TRUE, &directory_watcher_callback, &context); + CFRunLoopSourceRef source = CFFileDescriptorCreateRunLoopSource(kCFAllocatorDefault, fdref, 0); + CFRunLoopAddSource(CFRunLoopGetMain(), source, kCFRunLoopDefaultMode); + CFRelease(source); + CFFileDescriptorEnableCallBacks(fdref, kCFFileDescriptorReadCallBack); + return (struct gral_directory_watcher *)fdref; } void gral_directory_watcher_delete(struct gral_directory_watcher *directory_watcher) { - // TODO: implement + CFFileDescriptorInvalidate((CFFileDescriptorRef)directory_watcher); + CFRelease((CFFileDescriptorRef)directory_watcher); }