ext/fsevent.c in ruby-fsevent-0.1.0 vs ext/fsevent.c in ruby-fsevent-0.2.0

- old
+ new

@@ -8,10 +8,12 @@ /* * Thanks to fswatch.c for providing a starting point * http://github.com/alandipert/fswatch */ +FSEventStreamRef stream; +VALUE fsevent_class; void callback( ConstFSEventStreamRef streamRef, void *context, size_t numEvents, @@ -52,11 +54,10 @@ context.info = (VALUE *)self; context.retain = NULL; context.release = NULL; context.copyDescription = NULL; - FSEventStreamRef stream; stream = FSEventStreamCreate(NULL, &callback, &context, pathsToWatch, kFSEventStreamEventIdSinceNow, @@ -87,38 +88,60 @@ } VALUE rb_registered_directories = rb_iv_get(self, "@registered_directories"); return rb_registered_directories; } -int pid, status; -static VALUE t_run(VALUE self) { +static VALUE t_start(VALUE self) { VALUE rb_registered_directories = rb_iv_get(self, "@registered_directories"); Check_Type(rb_registered_directories, T_ARRAY); - if (pid = fork()) { - wait(&status); + + watch_directory(self); + return self; +} + +static VALUE t_stop(VALUE self) { + FSEventStreamStop(stream); + FSEventStreamInvalidate(stream); + FSEventStreamRelease(stream); + CFRunLoopStop(CFRunLoopGetCurrent()); + return self; +} + +static VALUE t_restart(VALUE self) { + t_stop(self); + watch_directory(self); + return self; +} + +void delegate_signal_to_ruby(int signal) { + VALUE signal_mod = rb_const_get(rb_cObject, rb_intern("Signal")); + if (rb_funcall(signal_mod, rb_intern("handles?"), 1, INT2FIX(signal)) == Qtrue) { + rb_funcall(signal_mod, rb_intern("handle"), 1, INT2FIX(signal)); } else { - watch_directory(self); + ruby_default_signal(signal); } - return self; } -void kill_watcher() { - if (pid) { - kill(pid, SIGKILL); - printf("\n"); +void register_signal_delegation() { + int i; + for(i = 0; i < 33; i++) { // Signal.list.values.size yields 32 different signals + (void) signal(i, delegate_signal_to_ruby); } } -VALUE fsevent_class; void Init_fsevent() { + rb_require("fsevent/signal_ext"); + fsevent_class = rb_define_class("FSEvent", rb_cObject); rb_define_method(fsevent_class, "initialize", t_init, 0); rb_define_method(fsevent_class, "on_change", t_on_change, 1); rb_define_method(fsevent_class, "watch_directories", t_watch_directories, 1); - rb_define_method(fsevent_class, "run", t_run, 0); + rb_define_method(fsevent_class, "start", t_start, 0); + rb_define_method(fsevent_class, "stop", t_stop, 0); + rb_define_method(fsevent_class, "restart", t_restart, 0); rb_define_attr(fsevent_class, "latency", 1, 1); rb_define_attr(fsevent_class, "registered_directories", 1, 1); - atexit(kill_watcher); + register_signal_delegation(); }