From d7bdca3f48f0486319d013d8b59e68e4b5e3a87c Mon Sep 17 00:00:00 2001 From: Francesco Abbate Date: Wed, 14 Jul 2021 10:11:46 +0200 Subject: [PATCH] Add missing files from previous commits --- src/dirmonitor.c | 56 ++++++++++++++++++++++++++++++++++++++++++++++++ src/dirmonitor.h | 14 ++++++++++++ 2 files changed, 70 insertions(+) create mode 100644 src/dirmonitor.c create mode 100644 src/dirmonitor.h diff --git a/src/dirmonitor.c b/src/dirmonitor.c new file mode 100644 index 00000000..e94c7687 --- /dev/null +++ b/src/dirmonitor.c @@ -0,0 +1,56 @@ +#include +#include + +#include + +#define DMON_IMPL +#include "dmon.h" + +#include "dirmonitor.h" + +static void send_sdl_event(dmon_watch_id watch_id, dmon_action action, const char *filepath) { + SDL_Event ev; + const int size = strlen(filepath) + 1; + char *new_filepath = malloc(size); + if (!new_filepath) return; + memcpy(new_filepath, filepath, size); +#ifdef _WIN32 + for (int i = 0; i < size; i++) { + if (new_filepath[i] == '/') { + new_filepath[i] = '\\'; + } + } +#endif + SDL_zero(ev); + ev.type = SDL_USEREVENT; + fprintf(stderr, "DEBUG: send watch_id: %d action; %d\n", watch_id.id, action); fflush(stderr); + ev.user.code = ((watch_id.id & 0xffff) << 16) | (action & 0xffff); + ev.user.data1 = new_filepath; + SDL_PushEvent(&ev); +} + +void dirmonitor_init() { + dmon_init(); + /* FIXME: not needed ? */ + /* sdl_dmon_event_type = SDL_RegisterEvents(1); */ +} + +void dirmonitor_deinit() { + dmon_deinit(); +} + +void dirmonitor_push_event(dmon_watch_id watch_id, dmon_action action, const char *filepath, + const char *oldfilepath) +{ + switch (action) { + case DMON_ACTION_MOVE: + send_sdl_event(watch_id, DMON_ACTION_DELETE, oldfilepath); + send_sdl_event(watch_id, DMON_ACTION_CREATE, filepath); + break; + case DMON_ACTION_MODIFY: + break; + default: + send_sdl_event(watch_id, action, filepath); + } +} + diff --git a/src/dirmonitor.h b/src/dirmonitor.h new file mode 100644 index 00000000..1fae4635 --- /dev/null +++ b/src/dirmonitor.h @@ -0,0 +1,14 @@ +#ifndef DIRMONITOR_H +#define DIRMONITOR_H + +#include + +#include "dmon.h" + +void dirmonitor_init(); +void dirmonitor_deinit(); +void dirmonitor_push_event(dmon_watch_id watch_id, dmon_action action, const char *filepath, + const char *oldfilepath); + +#endif +