123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. /*
  2. ** © 2014 by Philipp Dunkel <pip@pipobscure.com>
  3. ** Licensed under MIT License.
  4. */
  5. #include "nan.h"
  6. #include "uv.h"
  7. #include "v8.h"
  8. #include "pthread.h"
  9. #include "CoreFoundation/CoreFoundation.h"
  10. #include "CoreServices/CoreServices.h"
  11. #include <iostream>
  12. #include <vector>
  13. #include "src/storage.cc"
  14. namespace fse {
  15. class FSEvents : public Nan::ObjectWrap {
  16. public:
  17. explicit FSEvents(const char *path);
  18. ~FSEvents();
  19. // locking.cc
  20. bool lockStarted;
  21. pthread_mutex_t lockmutex;
  22. void lockingStart();
  23. void lock();
  24. void unlock();
  25. void lockingStop();
  26. // async.cc
  27. uv_async_t async;
  28. void asyncStart();
  29. void asyncTrigger();
  30. void asyncStop();
  31. // thread.cc
  32. pthread_t thread;
  33. CFRunLoopRef threadloop;
  34. void threadStart();
  35. static void *threadRun(void *ctx);
  36. void threadStop();
  37. // methods.cc - internal
  38. Nan::AsyncResource async_resource;
  39. void emitEvent(const char *path, UInt32 flags, UInt64 id);
  40. // Common
  41. CFArrayRef paths;
  42. std::vector<fse_event*> events;
  43. static void Initialize(v8::Handle<v8::Object> exports);
  44. // methods.cc - exposed
  45. static NAN_METHOD(New);
  46. static NAN_METHOD(Stop);
  47. static NAN_METHOD(Start);
  48. };
  49. }
  50. using namespace fse;
  51. FSEvents::FSEvents(const char *path)
  52. : async_resource("fsevents:FSEvents"), lockStarted(false) {
  53. CFStringRef dirs[] = { CFStringCreateWithCString(NULL, path, kCFStringEncodingUTF8) };
  54. paths = CFArrayCreate(NULL, (const void **)&dirs, 1, NULL);
  55. threadloop = NULL;
  56. lockingStart();
  57. }
  58. FSEvents::~FSEvents() {
  59. lockingStop();
  60. CFRelease(paths);
  61. }
  62. #ifndef kFSEventStreamEventFlagItemCreated
  63. #define kFSEventStreamEventFlagItemCreated 0x00000010
  64. #endif
  65. #include "src/locking.cc"
  66. #include "src/async.cc"
  67. #include "src/thread.cc"
  68. #include "src/constants.cc"
  69. #include "src/methods.cc"
  70. void FSEvents::Initialize(v8::Handle<v8::Object> exports) {
  71. v8::Local<v8::FunctionTemplate> tpl = Nan::New<v8::FunctionTemplate>(FSEvents::New);
  72. tpl->SetClassName(Nan::New<v8::String>("FSEvents").ToLocalChecked());
  73. tpl->InstanceTemplate()->SetInternalFieldCount(1);
  74. tpl->PrototypeTemplate()->Set(
  75. Nan::New<v8::String>("start").ToLocalChecked(),
  76. Nan::New<v8::FunctionTemplate>(FSEvents::Start));
  77. tpl->PrototypeTemplate()->Set(
  78. Nan::New<v8::String>("stop").ToLocalChecked(),
  79. Nan::New<v8::FunctionTemplate>(FSEvents::Stop));
  80. exports->Set(Nan::New<v8::String>("Constants").ToLocalChecked(), Constants());
  81. exports->Set(Nan::New<v8::String>("FSEvents").ToLocalChecked(),
  82. tpl->GetFunction());
  83. }
  84. NODE_MODULE(fse, FSEvents::Initialize)