diff --git a/src/platform.c b/src/platform.c index 01aa141..668b74b 100644 --- a/src/platform.c +++ b/src/platform.c @@ -4,7 +4,6 @@ #include #include #include -#define PLATFORM_INOTIFY_EVENT_BUFFER_SIZE 4096 #endif #ifdef _WIN32 @@ -13,6 +12,8 @@ #include #endif +#define EVENT_BUFFER_SIZE 4096 + void *platform_load_library(const char *path) { #ifdef _WIN32 @@ -60,7 +61,16 @@ void *platform_directory_watch_start(const char *path) return (void *)(intptr_t)fd; #elif defined(_WIN32) - return NULL + HANDLE handle; + handle = + CreateFileA(path, FILE_LIST_DIRECTORY, + FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, + NULL, OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, NULL); + + if (handle == INVALID_HANDLE_VALUE) + return NULL; + + return (void *)handle; #else return NULL #endif @@ -76,7 +86,9 @@ void platform_directory_watch_stop(void *handle) fd = (int)(intptr_t)handle; close(fd); #elif defined(_WIN32) - (void)handle; + HANDLE h; + h = (HANDLE)handle; + CloseHandle(h); #else (void)handle; #endif @@ -86,7 +98,7 @@ char **platform_directory_watch_check(void *handle, size_t *out_count) { #ifdef __linux__ int fd; - char buffer[PLATFORM_INOTIFY_EVENT_BUFFER_SIZE]; + char buffer[EVENT_BUFFER_SIZE]; ssize_t bytes_read; struct inotify_event *event; char *event_ptr; @@ -139,6 +151,56 @@ char **platform_directory_watch_check(void *handle, size_t *out_count) *out_count = index; return file_list; #elif defined(_WIN32) + HANDLE h; + BYTE buffer[EVENT_BUFFER_SIZE]; + DWORD bytes_returned; + FILE_NOTIFY_INFORMATION *info; + BYTE *event_ptr; + char **file_list; + size_t file_count, index; + int char_count; + BOOL result; + + h = (HANDLE)handle; + + result = ReadDirectoryChangesW(h, buffer, sizeof(buffer), FALSE, + FILE_NOTIFY_CHANGE_FILE_NAME | + FILE_NOTIFY_CHANGE_LAST_WRITE, + &bytes_returned, NULL, NULL); + if (!result || bytes_returned == 0) { + *out_count = 0; + return NULL; + } + + file_count = 0; + event_ptr = buffer; + + while (1) { + info = (FILE_NOTIFY_INFORMATION *)event_ptr; + char_count = WideCharToMultiByte( + CP_UTF8, info->FileName, + info->FileNameLength / sizeof(WCHAR), NULL, 0, NULL, NULL); + + if (char_count > 0) { + file_list[index] = malloc(char_count + 1); + if (file_list[index]) { + WideCharToMultiByte( + CP_UTF8, 0, info->FileName, + info->FileNameLength / sizeof(WCHAR), + file_list[index], char_count, NULL, NULL); + file_list[index][char_count] = '\0'; + ++index + } + } + + if (info->NextEntryOffset == 0) + break; + + event_ptr += info->NextEntryOffset; + } + + *out_count = index; + return file_list; #else #endif }