Simplify Windows event handling

- Use STK_MOD_LOAD/STK_MOD_UNLOAD for Windows events
- Map FILE_ACTION_ADDED/MODIFIED/RENAMED_NEW_NAME to load
- Map FILE_ACTION_REMOVED/RENAMED_OLD_NAME to unload
This commit is contained in:
2025-11-06 20:42:41 +01:00
parent 13604e3f3e
commit c95daad754
+9 -28
View File
@@ -73,7 +73,7 @@ void *platform_directory_watch_start(const char *path)
return (void *)handle; return (void *)handle;
#else #else
return NULL return NULL;
#endif #endif
} }
@@ -99,6 +99,7 @@ stk_module_event_t *platform_directory_watch_check(void *handle,
char ***file_list, char ***file_list,
size_t *out_count) size_t *out_count)
{ {
size_t file_count = 0, index = 0;
stk_module_event_t *events = NULL; stk_module_event_t *events = NULL;
#ifdef __linux__ #ifdef __linux__
int fd; int fd;
@@ -106,7 +107,6 @@ stk_module_event_t *platform_directory_watch_check(void *handle,
ssize_t bytes_read; ssize_t bytes_read;
struct inotify_event *event; struct inotify_event *event;
char *event_ptr; char *event_ptr;
size_t file_count, index;
fd = (int)(long)handle; fd = (int)(long)handle;
bytes_read = read(fd, buffer, sizeof(buffer)); bytes_read = read(fd, buffer, sizeof(buffer));
@@ -157,16 +157,12 @@ stk_module_event_t *platform_directory_watch_check(void *handle,
event_ptr += sizeof(struct inotify_event) + event->len; event_ptr += sizeof(struct inotify_event) + event->len;
} }
*out_count = index;
return events;
#elif defined(_WIN32) #elif defined(_WIN32)
HANDLE h; HANDLE h;
BYTE buffer[EVENT_BUFFER_SIZE]; BYTE buffer[EVENT_BUFFER_SIZE];
DWORD bytes_returned; DWORD bytes_returned;
FILE_NOTIFY_INFORMATION *info; FILE_NOTIFY_INFORMATION *info;
BYTE *event_ptr; BYTE *event_ptr;
size_t file_count, index;
int char_count; int char_count;
BOOL result; BOOL result;
@@ -198,7 +194,7 @@ stk_module_event_t *platform_directory_watch_check(void *handle,
return NULL; return NULL;
} }
events = malloc(file_count * sizeof(stk_file_event_t)); events = malloc(file_count * sizeof(stk_module_event_t));
*file_list = malloc(file_count * sizeof(char *)); *file_list = malloc(file_count * sizeof(char *));
if (!events || !*file_list) { if (!events || !*file_list) {
free(events); free(events);
@@ -211,22 +207,11 @@ stk_module_event_t *platform_directory_watch_check(void *handle,
event_ptr = buffer; event_ptr = buffer;
while (1) { while (1) {
info = (FILE_NOTIFY_INFORMATION *)event_ptr; info = (FILE_NOTIFY_INFORMATION *)event_ptr;
events[index] = (info->Action == FILE_ACTION_ADDED ||
switch (info->Action) { info->Action == FILE_ACTION_MODIFIED ||
case FILE_ACTION_ADDED: info->Action == FILE_ACTION_RENAMED_NEW_NAME)
events[index] = STK_FILE_CREATED; ? STK_MOD_LOAD
break; : STK_MOD_UNLOAD;
case FILE_ACTION_MODIFIED:
events[index] = STK_FILE_MODIFIED;
break;
case FILE_ACTION_REMOVED:
events[index] = STK_FILE_DELETED;
break;
case FILE_ACTION_RENAMED:
events[index] = STK_FILE_RENAMED;
break;
}
char_count = WideCharToMultiByte( char_count = WideCharToMultiByte(
CP_UTF8, 0, info->FileName, CP_UTF8, 0, info->FileName,
info->FileNameLength / sizeof(WCHAR), NULL, 0, NULL, NULL); info->FileNameLength / sizeof(WCHAR), NULL, 0, NULL, NULL);
@@ -249,12 +234,9 @@ stk_module_event_t *platform_directory_watch_check(void *handle,
event_ptr += info->NextEntryOffset; event_ptr += info->NextEntryOffset;
} }
#endif
*out_count = index; *out_count = index;
return events; return events;
#else
return NULL;
#endif
} }
char **platform_directory_init_scan(const char *path, size_t *out_count) char **platform_directory_init_scan(const char *path, size_t *out_count)
@@ -373,7 +355,6 @@ char **platform_directory_init_scan(const char *path, size_t *out_count)
FindClose(find_handle); FindClose(find_handle);
#else #else
return NULL;
#endif #endif
*out_count = index; *out_count = index;
return file_list; return file_list;