Migrate FileWatcher to EFSW

This commit is contained in:
Joshua Granick
2017-09-26 10:41:03 -07:00
parent efcecb3842
commit 28e5833a39
8 changed files with 142 additions and 64 deletions

View File

@@ -11,6 +11,7 @@
<set name="LIME_CAIRO" value="1" />
<set name="LIME_CURL" value="1" unless="emscripten" />
<set name="LIME_EFSW" value="1" if="windows" />
<set name="LIME_JPEG" value="1" />
<set name="LIME_FREETYPE" value="1" />
<set name="LIME_HARFBUZZ" value="1" />
@@ -26,7 +27,6 @@
<set name="LIME_SDL" value="1" />
<!-- <set name="LIME_SDL_ANGLE" value="1" if="windows" unless="static_link" /> -->
<set name="LIME_SDL_ANGLE" value="1" if="windows LIME_SDL_ANGLE" unless="static_link" />
<set name="LIME_SIMPLEFILEWATCHER" value="1" if="windows" />
<set name="LIME_TINYFILEDIALOGS" value="1" if="windows || mac || linux" />
<set name="LIME_VORBIS" value="1" />
<set name="LIME_ZLIB" value="1" />
@@ -78,6 +78,15 @@
</section>
<section if="LIME_EFSW">
<compilerflag value="-I${NATIVE_TOOLKIT_PATH}/efsw/include/" />
<compilerflag value="-DLIME_EFSW" />
<file name="src/system/FileWatcher.cpp" />
</section>
<section if="LIME_JPEG">
<compilerflag value="-I${NATIVE_TOOLKIT_PATH}/jpeg/" />
@@ -214,15 +223,6 @@
</section>
<section if="LIME_SIMPLEFILEWATCHER">
<compilerflag value="-I${NATIVE_TOOLKIT_PATH}/simplefilewatcher/include/" />
<compilerflag value="-DLIME_SIMPLEFILEWATCHER" />
<file name="src/system/FileWatcher.cpp" />
</section>
<section if="LIME_TINYFILEDIALOGS">
<compilerflag value="-I${NATIVE_TOOLKIT_PATH}/tinyfiledialogs/" />
@@ -291,6 +291,7 @@
<include name="lib/cairo/files.xml" />
<include name="lib/curl/files.xml" />
<include name="lib/efsw/files.xml" />
<include name="lib/freetype/files.xml" />
<include name="lib/harfbuzz/files.xml" />
<include name="lib/jpeg/files.xml" />
@@ -301,7 +302,6 @@
<include name="lib/pixman/files.xml" />
<include name="lib/png/files.xml" />
<include name="lib/sdl/files.xml" />
<include name="lib/simplefilewatcher/files.xml" />
<include name="lib/tinyfiledialogs/files.xml" />
<include name="lib/vorbis/files.xml" />
<include name="lib/zlib/files.xml" />
@@ -318,6 +318,7 @@
<files id="native-toolkit-cairo" if="LIME_CAIRO" />
<files id="native-toolkit-curl" if="LIME_CURL" />
<files id="native-toolkit-efsw" if="LIME_EFSW" />
<files id="native-toolkit-jpeg" if="LIME_JPEG" />
<files id="native-toolkit-freetype" if="LIME_FREETYPE" />
<files id="native-toolkit-harfbuzz" if="LIME_HARFBUZZ" />
@@ -328,7 +329,6 @@
<files id="native-toolkit-pixman" if="LIME_PIXMAN" />
<files id="native-toolkit-png" if="LIME_PNG" />
<files id="native-toolkit-sdl" if="LIME_SDL" unless="emscripten" />
<files id="native-toolkit-simplefilewatcher" if="LIME_SIMPLEFILEWATCHER" />
<files id="native-toolkit-tinyfiledialogs" if="LIME_TINYFILEDIALOGS" />
<files id="native-toolkit-vorbis" if="LIME_VORBIS" />
<files id="native-toolkit-zlib" if="LIME_ZLIB" />

View File

@@ -5,14 +5,27 @@
#undef RemoveDirectory
#endif
#include <system/Mutex.h>
#include <hx/CFFI.h>
#include <map>
#include <string>
#include <vector>
namespace lime {
struct FileWatcherEvent {
long watchID;
std::string dir;
std::string file;
int action;
std::string oldFile;
};
class FileWatcher {
@@ -21,16 +34,19 @@ namespace lime {
FileWatcher (value callback);
~FileWatcher ();
unsigned long AddDirectory (const std::string directory, bool recursive);
void RemoveDirectory (unsigned long watchID);
long AddDirectory (const std::string directory, bool recursive);
void QueueEvent (FileWatcherEvent event);
void RemoveDirectory (long watchID);
void Update ();
AutoGCRoot* callback;
private:
AutoGCRoot* callback;
void* fileWatcher = 0;
std::map<unsigned long, void*> watchListeners;
Mutex* mutex;
std::vector<FileWatcherEvent> queue;
std::map<long, void*> listeners;
};

1
project/lib/efsw Submodule

Submodule project/lib/efsw added at 56f50e8d4a

View File

@@ -67,7 +67,7 @@ namespace lime {
void gc_file_watcher (value handle) {
#ifdef LIME_SIMPLEFILEWATCHER
#ifdef LIME_EFSW
FileWatcher* watcher = (FileWatcher*)val_data (handle);
delete watcher;
#endif
@@ -504,7 +504,7 @@ namespace lime {
value lime_file_watcher_create (value callback) {
#ifdef LIME_SIMPLEFILEWATCHER
#ifdef LIME_EFSW
FileWatcher* watcher = new FileWatcher (callback);
return CFFIPointer (watcher, gc_file_watcher);
#else
@@ -516,7 +516,7 @@ namespace lime {
value lime_file_watcher_add_directory (value handle, value path, bool recursive) {
#ifdef LIME_SIMPLEFILEWATCHER
#ifdef LIME_EFSW
FileWatcher* watcher = (FileWatcher*)val_data (handle);
return alloc_int (watcher->AddDirectory (val_string (path), recursive));
#else
@@ -529,7 +529,7 @@ namespace lime {
void lime_file_watcher_remove_directory (value handle, value watchID) {
#ifdef LIME_SIMPLEFILEWATCHER
#ifdef LIME_EFSW
FileWatcher* watcher = (FileWatcher*)val_data (handle);
watcher->RemoveDirectory (val_int (watchID));
#endif
@@ -539,7 +539,7 @@ namespace lime {
void lime_file_watcher_update (value handle) {
#ifdef LIME_SIMPLEFILEWATCHER
#ifdef LIME_EFSW
FileWatcher* watcher = (FileWatcher*)val_data (handle);
watcher->Update ();
#endif

View File

@@ -1,11 +1,11 @@
#include <system/FileWatcher.h>
#include <FileWatcher/FileWatcher.h>
#include <efsw/efsw.hpp>
namespace lime {
class UpdateListener : public FW::FileWatchListener {
class UpdateListener : public efsw::FileWatchListener {
public:
@@ -15,10 +15,10 @@ namespace lime {
}
void handleFileAction (FW::WatchID watchid, const FW::String& dir, const FW::String& filename, FW::Action action) {
void handleFileAction (efsw::WatchID watchid, const std::string& dir, const std::string& filename, efsw::Action action, std::string oldFilename = "") {
value callback = watcher->callback->get ();
val_call3 (callback, alloc_string (dir.c_str ()), alloc_string (filename.c_str ()), alloc_int (action));
FileWatcherEvent event = { watchid, std::string (dir.begin (), dir.end ()), std::string (filename.begin (), filename.end ()), action, oldFilename };
watcher->QueueEvent (event);
}
@@ -30,7 +30,7 @@ namespace lime {
FileWatcher::FileWatcher (value _callback) {
callback = new AutoGCRoot (_callback);
fileWatcher = new FW::FileWatcher ();
fileWatcher = new efsw::FileWatcher ();
}
@@ -40,35 +40,58 @@ namespace lime {
delete callback;
delete fileWatcher;
std::map<unsigned long, void*>::iterator it;
for (it = watchListeners.begin (); it != watchListeners.end (); it++) {
if (mutex) {
delete (UpdateListener*)watchListeners[it->first];
delete mutex;
}
std::map<long, void*>::iterator it;
for (it = listeners.begin (); it != listeners.end (); it++) {
delete (UpdateListener*)listeners[it->first];
}
}
unsigned long FileWatcher::AddDirectory (std::string directory, bool recursive) {
long FileWatcher::AddDirectory (std::string directory, bool recursive) {
UpdateListener* listener = new UpdateListener (this);
FW::WatchID watchID = ((FW::FileWatcher*)fileWatcher)->addWatch (directory, listener, true);
watchListeners[watchID] = listener;
efsw::WatchID watchID = ((efsw::FileWatcher*)fileWatcher)->addWatch (directory, listener, true);
listeners[watchID] = listener;
if (!mutex) {
mutex = new Mutex ();
((efsw::FileWatcher*)fileWatcher)->watch ();
}
return watchID;
}
void FileWatcher::RemoveDirectory (unsigned long watchID) {
void FileWatcher::QueueEvent (FileWatcherEvent event) {
((FW::FileWatcher*)fileWatcher)->removeWatch (watchID);
mutex->Lock ();
queue.push_back (event);
mutex->Unlock ();
if (watchListeners.find (watchID) != watchListeners.end ()) {
}
void FileWatcher::RemoveDirectory (long watchID) {
((efsw::FileWatcher*)fileWatcher)->removeWatch (watchID);
if (listeners.find (watchID) != listeners.end ()) {
delete (UpdateListener*)watchListeners[watchID];
watchListeners.erase (watchID);
delete (UpdateListener*)listeners[watchID];
listeners.erase (watchID);
}
@@ -77,7 +100,28 @@ namespace lime {
void FileWatcher::Update () {
((FW::FileWatcher*)fileWatcher)->update ();
mutex->Lock ();
int size = queue.size ();
if (size > 0) {
FileWatcherEvent event;
value _callback = callback->get ();
for (int i = 0; i < size; i++) {
event = queue[i];
value args[4] = { alloc_string (event.dir.c_str ()), alloc_string (event.file.c_str ()), alloc_int (event.action), alloc_string (event.oldFile.c_str ()) };
val_callN (_callback, args, 4);
}
queue.clear ();
}
mutex->Unlock ();
}