/* Generated from "/home/hww3/pike-git/src/modules/FSEvents/fsevents.cmod" by precompile.pike running Pike v8.0 release 1094
 *
 * Do NOT edit this file.
 */

#undef PRECOMPILE_API_VERSION
#define PRECOMPILE_API_VERSION 6

#undef PRECOMPILE_SUB_PIKEVARS
#define PRECOMPILE_SUB_PIKEVARS 1

#undef PRECOMPILE_DYNAMIC_TYPES
#define PRECOMPILE_DYNAMIC_TYPES 1



#undef cmod___CMOD__
#define cmod___CMOD__ 1
#line 1 "/home/hww3/pike-git/src/modules/FSEvents/fsevents.cmod"
#line 1 "/home/hww3/pike-git/src/modules/FSEvents/fsevents.cmod"
#line 1 "/home/hww3/pike-git/src/modules/FSEvents/fsevents.cmod"
#line 3003 "/home/hww3/pike-git/lib/modules/Tools.pmod/Standalone.pmod/precompile.pike"







#line 3010 "/home/hww3/pike-git/lib/modules/Tools.pmod/Standalone.pmod/precompile.pike"
#line 1 "/home/hww3/pike-git/src/modules/FSEvents/fsevents.cmod"
/*! @module System
 */

/*! @module FSEvents
 */

#line 7 "/home/hww3/pike-git/src/modules/FSEvents/fsevents.cmod"
#include "global.h"
#include "interpret.h"
#include "module.h"
#include "program.h"
#include "stralloc.h"
#include "svalue.h"
#include "threads.h"
#include "object.h"
#include "pike_types.h"
#include "builtin_functions.h"

#include "fsevents_config.h"

#define ADD_ICONST(name) do {                   \
        add_integer_constant(#name, name, 0);   \
} while(0);

#ifdef HAVE_FRAMEWORK_CORESERVICES
#import <CoreServices/CoreServices.h>
#endif /* HAVE_FRAMEWORK_CORESERVICES */



#ifndef PIKE_UNUSED_ATTRIBUTE
#define PIKE_UNUSED_ATTRIBUTE
#endif
#define CMOD_MAP_PROGRAM_IDS_DEFINED 1
static int ___cmod_map_program_ids(int id) PIKE_UNUSED_ATTRIBUTE;
#ifndef TYPEOF
/* Compat with older Pikes. */
#define TYPEOF(SVAL)	((SVAL).type)
#define SUBTYPEOF(SVAL)	((SVAL).subtype)
#define SET_SVAL_TYPE(SVAL, TYPE)	(TYPEOF(SVAL) = TYPE)
#define SET_SVAL_SUBTYPE(SVAL, TYPE)	(SUBTYPEOF(SVAL) = TYPE)
#define SET_SVAL(SVAL, TYPE, SUBTYPE, FIELD, EXPR) do {	\
    /* Set the type afterwards to avoid a clobbered	\
     * svalue in case EXPR throws. */			\
    (SVAL).u.FIELD = (EXPR);				\
    SET_SVAL_TYPE((SVAL), (TYPE));			\
    SET_SVAL_SUBTYPE((SVAL), (SUBTYPE));		\
  } while(0)
#endif /* !TYPEOF */
#ifndef PIKE_UNUSED
/* Compat with Pike 7.8 and earlier. */
/* NB: Not strictly correct; PIKE_UNUSED was added the
 *     day after set_program_id_to_id(), but good enough.
 */
#ifndef UNUSED
#define UNUSED(X)	X
#endif
#ifndef set_program_id_to_id
/* NB: Recent Pike 7.8 has a #define that conflicts with
 *     the following declaration.
 */
static void set_program_id_to_id(void*UNUSED(id)){}
#endif
#else /* */
PMOD_EXPORT void set_program_id_to_id( int (*to)(int) );
#endif /* !PIKE_UNUSED */


#ifndef DEFAULT_CMOD_STORAGE
#define CMOD_COND_USED
#define DEFAULT_CMOD_STORAGE static
#endif
#ifndef CMOD_COND_USED
# define f_FSEvents_EventStream_flush_sync_fun_num_used 1
#else
#undef f_FSEvents_EventStream_flush_sync_fun_num_used
#endif
#ifndef CMOD_COND_USED
# define f_FSEvents_EventStream_start_fun_num_used 1
#else
#undef f_FSEvents_EventStream_start_fun_num_used
#endif
#ifndef CMOD_COND_USED
# define f_FSEvents_EventStream_set_callback_fun_num_used 1
#else
#undef f_FSEvents_EventStream_set_callback_fun_num_used
#endif
#ifndef CMOD_COND_USED
# define f_FSEvents_EventStream_add_path_fun_num_used 1
#else
#undef f_FSEvents_EventStream_add_path_fun_num_used
#endif
#ifndef CMOD_COND_USED
# define f_FSEvents_EventStream_create_fun_num_used 1
#else
#undef f_FSEvents_EventStream_create_fun_num_used
#endif
#ifndef CMOD_COND_USED
# define f_FSEvents_EventStream_flush_async_fun_num_used 1
#else
#undef f_FSEvents_EventStream_flush_async_fun_num_used
#endif
#ifndef CMOD_COND_USED
# define f_FSEvents_EventStream_is_started_fun_num_used 1
#else
#undef f_FSEvents_EventStream_is_started_fun_num_used
#endif
#ifndef CMOD_COND_USED
# define FSEvents_EventStream_program_fun_num_used 1
#else
#undef FSEvents_EventStream_program_fun_num_used
#endif
#ifndef CMOD_COND_USED
# define f_FSEvents_EventStream_stop_fun_num_used 1
#else
#undef f_FSEvents_EventStream_stop_fun_num_used
#endif
#line 29 "/home/hww3/pike-git/src/modules/FSEvents/fsevents.cmod"
#ifdef HAVE_FRAMEWORK_CORESERVICES

void low_stop(void);
struct pike_string * string_from_cfstring(CFStringRef cfString);

struct event_callback_ctx 
{
ConstFSEventStreamRef streamRef;
void *clientCallBackInfo;
size_t numEvents;
void *eventPaths;
const FSEventStreamEventFlags * eventFlags;
const FSEventStreamEventId * eventIds;
};

static void do_event_callback(struct event_callback_ctx * ctx);

static void event_callback(ConstFSEventStreamRef streamRef,
void *clientCallBackInfo,
size_t numEvents,
void *eventPaths,
const FSEventStreamEventFlags eventFlags[],
const FSEventStreamEventId eventIds[]);

/*! @class EventStream
 */


#undef class_FSEvents_EventStream_defined
#define class_FSEvents_EventStream_defined
DEFAULT_CMOD_STORAGE struct program *FSEvents_EventStream_program=NULL;

#ifdef FSEvents_EventStream_program_fun_num_used
DEFAULT_CMOD_STORAGE int FSEvents_EventStream_program_fun_num=-1;
#endif /* FSEvents_EventStream_program_fun_num_used */

#undef var_runLoop_FSEvents_EventStream_defined
#define var_runLoop_FSEvents_EventStream_defined

#undef var_stream_FSEvents_EventStream_defined
#define var_stream_FSEvents_EventStream_defined

#undef var_isRunning_FSEvents_EventStream_defined
#define var_isRunning_FSEvents_EventStream_defined

#undef var_cq__paths_FSEvents_EventStream_defined
#define var_cq__paths_FSEvents_EventStream_defined

#undef var_cq__sinceWhen_FSEvents_EventStream_defined
#define var_cq__sinceWhen_FSEvents_EventStream_defined

#undef var_cq__flags_FSEvents_EventStream_defined
#define var_cq__flags_FSEvents_EventStream_defined

#undef var_cq__latency_FSEvents_EventStream_defined
#define var_cq__latency_FSEvents_EventStream_defined

#undef var_callback_func_FSEvents_EventStream_defined
#define var_callback_func_FSEvents_EventStream_defined

#undef THIS
#define THIS ((struct FSEvents_EventStream_struct *)(Pike_interpreter.frame_pointer->current_storage))

#undef THIS_FSEVENTS_EVENTSTREAM
#define THIS_FSEVENTS_EVENTSTREAM ((struct FSEvents_EventStream_struct *)(Pike_interpreter.frame_pointer->current_storage))

#undef OBJ2_FSEVENTS_EVENTSTREAM
#define OBJ2_FSEVENTS_EVENTSTREAM(o) ((struct FSEvents_EventStream_struct *)(o->storage+FSEvents_EventStream_storage_offset))

#undef GET_FSEVENTS_EVENTSTREAM_STORAGE
#define GET_FSEVENTS_EVENTSTREAM_STORAGE(o) ((struct FSEvents_EventStream_struct *)(o->storage+FSEvents_EventStream_storage_offset)
static ptrdiff_t FSEvents_EventStream_storage_offset;
struct FSEvents_EventStream_struct {

#ifdef var_runLoop_FSEvents_EventStream_defined
#line 58 "/home/hww3/pike-git/src/modules/FSEvents/fsevents.cmod"
CFRunLoopRef runLoop;
#endif /* var_runLoop_FSEvents_EventStream_defined */

#ifdef var_stream_FSEvents_EventStream_defined
#line 59 "/home/hww3/pike-git/src/modules/FSEvents/fsevents.cmod"
FSEventStreamRef stream;
#endif /* var_stream_FSEvents_EventStream_defined */

#ifdef var_isRunning_FSEvents_EventStream_defined
#line 60 "/home/hww3/pike-git/src/modules/FSEvents/fsevents.cmod"
int isRunning;
#endif /* var_isRunning_FSEvents_EventStream_defined */

#ifdef var_cq__paths_FSEvents_EventStream_defined
#line 61 "/home/hww3/pike-git/src/modules/FSEvents/fsevents.cmod"
CFArrayRef _paths;
#endif /* var_cq__paths_FSEvents_EventStream_defined */

#ifdef var_cq__sinceWhen_FSEvents_EventStream_defined
#line 62 "/home/hww3/pike-git/src/modules/FSEvents/fsevents.cmod"
FSEventStreamEventId _sinceWhen;
#endif /* var_cq__sinceWhen_FSEvents_EventStream_defined */

#ifdef var_cq__flags_FSEvents_EventStream_defined
#line 63 "/home/hww3/pike-git/src/modules/FSEvents/fsevents.cmod"
FSEventStreamCreateFlags _flags;
#endif /* var_cq__flags_FSEvents_EventStream_defined */

#ifdef var_cq__latency_FSEvents_EventStream_defined
#line 64 "/home/hww3/pike-git/src/modules/FSEvents/fsevents.cmod"
CFAbsoluteTime _latency;
#endif /* var_cq__latency_FSEvents_EventStream_defined */

#ifdef var_callback_func_FSEvents_EventStream_defined
  struct svalue callback_func;

#endif /* var_callback_func_FSEvents_EventStream_defined */
};
#ifdef PIKE_DEBUG
/* Ensure the struct is used in a variable declaration, or else gdb might not see it. */
static struct FSEvents_EventStream_struct *FSEvents_EventStream_gdb_dummy_ptr;
#endif
/*! @decl int is_started()
 *! Has start() been called?
 */
#define f_FSEvents_EventStream_is_started_defined

#ifdef f_FSEvents_EventStream_is_started_fun_num_used
DEFAULT_CMOD_STORAGE ptrdiff_t f_FSEvents_EventStream_is_started_fun_num = 0;

#endif /* f_FSEvents_EventStream_is_started_fun_num_used */
DEFAULT_CMOD_STORAGE void f_FSEvents_EventStream_is_started(INT32 args) {
#line 71 "/home/hww3/pike-git/src/modules/FSEvents/fsevents.cmod"
if(args != 0) wrong_number_of_args_error("is_started",args,0);
{
do { INT_TYPE ret_=((THIS->isRunning));  push_int(ret_); return; }while(0);
#line 74 "/home/hww3/pike-git/src/modules/FSEvents/fsevents.cmod"
}

}
/*! @decl void flush_async()
 *!
 *! Requests that the FS Events service to flush out any events that have occurred but have not yet been 
 *! delivered, due to the latency parameter that was supplied when the stream was created. 
 *!
 *! This flushing occurs asynchronously -- events most likely will not have been delivered
 *! by the time this call returns. 
 *!
 *! @note 
 *! Only call this function after the stream has been started, via start().
 */
#define f_FSEvents_EventStream_flush_async_defined

#ifdef f_FSEvents_EventStream_flush_async_fun_num_used
DEFAULT_CMOD_STORAGE ptrdiff_t f_FSEvents_EventStream_flush_async_fun_num = 0;

#endif /* f_FSEvents_EventStream_flush_async_fun_num_used */
DEFAULT_CMOD_STORAGE void f_FSEvents_EventStream_flush_async(INT32 args) {
#line 87 "/home/hww3/pike-git/src/modules/FSEvents/fsevents.cmod"
if(args != 0) wrong_number_of_args_error("flush_async",args,0);
{
if(THIS->stream)
FSEventStreamFlushAsync(THIS->stream);
else
Pike_error("FSEvents.EventStream: not started\n");
}

}
/*! @decl void flush_sync()
 *!
 *! Requests that the FS Events service to flush out any events that have occurred but have not yet been 
 *! delivered, due to the latency parameter that was supplied when the stream was created. 
 *!
 *! Flushing synchronously when using this method; clients will have received all the callbacks by 
 *! the time this call returns to them.
 *!
 *! @note 
 *! Only call this function after the stream has been started, via start().
 *!
 */
#define f_FSEvents_EventStream_flush_sync_defined

#ifdef f_FSEvents_EventStream_flush_sync_fun_num_used
DEFAULT_CMOD_STORAGE ptrdiff_t f_FSEvents_EventStream_flush_sync_fun_num = 0;

#endif /* f_FSEvents_EventStream_flush_sync_fun_num_used */
DEFAULT_CMOD_STORAGE void f_FSEvents_EventStream_flush_sync(INT32 args) {
#line 107 "/home/hww3/pike-git/src/modules/FSEvents/fsevents.cmod"
if(args != 0) wrong_number_of_args_error("flush_sync",args,0);
{
if(THIS->stream)
FSEventStreamFlushSync(THIS->stream);
else
Pike_error("FSEvents.EventStream: not started\n");
}

}
/*! @decl void create(array(string) paths, float latency, int|void since_when, int|void flags)
 *!   Creates a new Public.System.FSEvents.EventStream object
 *!
 *! @param paths
 *! An array with each element containing a path to a directory, signifying the root of a 
 *! filesystem hierarchy to be watched for modifications.
 *!
 *! Additional paths may be added later using @[add_path()], though only if the stream is stopped.
 *!
 *! @param latency
 *! The number of seconds the service should wait after hearing about an event from the kernel 
 *! before passing it along to the client via its callback. Specifying a larger value may 
 *! result in more effective temporal coalescing, resulting in fewer callbacks and greater 
 *! overall efficiency.
 *!
 *! @param since_when
 *! The service will supply events that have happened after the given event ID. To ask for events
 *! "since now" pass the constant kFSEventStreamEventIdSinceNow. Do not pass zero for this value
 *! unless you want to receive events for the requested directories "since the beginning of time".
 *!
 *! @param flags
 *! Flags that modify the behavior of the stream being created. See Apple's FSEvents documentation
 *! for details of the various flags available.
 */
#define f_FSEvents_EventStream_create_defined

#ifdef f_FSEvents_EventStream_create_fun_num_used
DEFAULT_CMOD_STORAGE ptrdiff_t f_FSEvents_EventStream_create_fun_num = 0;

#endif /* f_FSEvents_EventStream_create_fun_num_used */
DEFAULT_CMOD_STORAGE void f_FSEvents_EventStream_create(INT32 args) {
#line 139 "/home/hww3/pike-git/src/modules/FSEvents/fsevents.cmod"
struct array * paths;
#line 139 "/home/hww3/pike-git/src/modules/FSEvents/fsevents.cmod"
FLOAT_TYPE latency;
#line 139 "/home/hww3/pike-git/src/modules/FSEvents/fsevents.cmod"
struct svalue * sinceWhen;
#line 139 "/home/hww3/pike-git/src/modules/FSEvents/fsevents.cmod"
struct svalue * flags;
#line 139 "/home/hww3/pike-git/src/modules/FSEvents/fsevents.cmod"
if(args < 2) wrong_number_of_args_error("create",args,2);
#line 139 "/home/hww3/pike-git/src/modules/FSEvents/fsevents.cmod"
if(args > 4) wrong_number_of_args_error("create",args,4);
#line 139 "/home/hww3/pike-git/src/modules/FSEvents/fsevents.cmod"
if(TYPEOF(Pike_sp[0-args]) != PIKE_T_ARRAY) SIMPLE_ARG_TYPE_ERROR("create",1,"array");
#line 139 "/home/hww3/pike-git/src/modules/FSEvents/fsevents.cmod"
debug_malloc_pass(paths=Pike_sp[0-args].u.array);
#line 139 "/home/hww3/pike-git/src/modules/FSEvents/fsevents.cmod"
if(TYPEOF(Pike_sp[1-args]) != PIKE_T_FLOAT) SIMPLE_ARG_TYPE_ERROR("create",2,"float");
#line 139 "/home/hww3/pike-git/src/modules/FSEvents/fsevents.cmod"
latency=Pike_sp[1-args].u.float_number;
#line 139 "/home/hww3/pike-git/src/modules/FSEvents/fsevents.cmod"
if ((args > 2) &&
    ((TYPEOF(Pike_sp[2-args]) != PIKE_T_INT) ||
     (SUBTYPEOF(Pike_sp[2-args]) != NUMBER_UNDEFINED))) {
#line 139 "/home/hww3/pike-git/src/modules/FSEvents/fsevents.cmod"
if(TYPEOF(Pike_sp[2-args]) != PIKE_T_INT) SIMPLE_ARG_TYPE_ERROR("create",3,"int|void");
#line 139 "/home/hww3/pike-git/src/modules/FSEvents/fsevents.cmod"
sinceWhen=Pike_sp+2-args; dmalloc_touch_svalue(Pike_sp+2-args);
#line 139 "/home/hww3/pike-git/src/modules/FSEvents/fsevents.cmod"
} else sinceWhen = NULL;
#line 139 "/home/hww3/pike-git/src/modules/FSEvents/fsevents.cmod"
if ((args > 3) &&
    ((TYPEOF(Pike_sp[3-args]) != PIKE_T_INT) ||
     (SUBTYPEOF(Pike_sp[3-args]) != NUMBER_UNDEFINED))) {
#line 139 "/home/hww3/pike-git/src/modules/FSEvents/fsevents.cmod"
if(TYPEOF(Pike_sp[3-args]) != PIKE_T_INT) SIMPLE_ARG_TYPE_ERROR("create",4,"int|void");
#line 139 "/home/hww3/pike-git/src/modules/FSEvents/fsevents.cmod"
flags=Pike_sp+3-args; dmalloc_touch_svalue(Pike_sp+3-args);
#line 139 "/home/hww3/pike-git/src/modules/FSEvents/fsevents.cmod"
} else flags = NULL;
{
int idx = 0, cnt = 0;

THIS->isRunning = 0;
THIS->_latency = latency;

if(sinceWhen && TYPEOF(*sinceWhen) == T_INT)
{
THIS->_sinceWhen = sinceWhen->u.integer;
}
else
{
THIS->_sinceWhen = kFSEventStreamEventIdSinceNow;
}

if(flags && TYPEOF(*flags) == T_INT)
{
THIS->_flags = flags->u.integer;
}
else
{
THIS->_flags = kFSEventStreamCreateFlagNone;
}

THIS->_paths = CFArrayCreateMutable(NULL, 0, NULL);
CFRetain(THIS->_paths);

if(paths && paths->size)
{
for(idx = 0; idx < paths->size; idx++)
{
struct svalue sv;
CFStringRef str;
if(TYPEOF(ITEM(paths)[idx]) != T_STRING) continue;
sv = ITEM(paths)[idx];
push_svalue(&sv);
f_string_to_utf8(1);
str = CFStringCreateWithBytes(NULL, (const UInt8 *)(Pike_sp[-1].u.string->str), (CFIndex)Pike_sp[-1].u.string->len, kCFStringEncodingUTF8, false);
pop_stack();
CFArrayInsertValueAtIndex( (CFMutableArrayRef)THIS->_paths, cnt, str);
cnt++;
}
}

pop_n_elems(args);

return;
}

}
/*! @decl void add_path(string path)
 *!   Add a path to the monitor list.
 *!
 *! @param path
 *!
 *!  @note
 *!    this can only be called when the monitor is stopped.
 */
#define f_FSEvents_EventStream_add_path_defined

#ifdef f_FSEvents_EventStream_add_path_fun_num_used
DEFAULT_CMOD_STORAGE ptrdiff_t f_FSEvents_EventStream_add_path_fun_num = 0;

#endif /* f_FSEvents_EventStream_add_path_fun_num_used */
DEFAULT_CMOD_STORAGE void f_FSEvents_EventStream_add_path(INT32 args) {
#line 197 "/home/hww3/pike-git/src/modules/FSEvents/fsevents.cmod"
struct pike_string * path;
#line 197 "/home/hww3/pike-git/src/modules/FSEvents/fsevents.cmod"
if(args != 1) wrong_number_of_args_error("add_path",args,1);
#line 197 "/home/hww3/pike-git/src/modules/FSEvents/fsevents.cmod"
if(TYPEOF(Pike_sp[0-1]) != PIKE_T_STRING) SIMPLE_ARG_TYPE_ERROR("add_path",1,"string");
#line 197 "/home/hww3/pike-git/src/modules/FSEvents/fsevents.cmod"
debug_malloc_pass(path=Pike_sp[0-1].u.string);
{
if(THIS->isRunning)
{
Pike_error("Cannot add paths while monitor is started.\n");
}

if(path && path->len)
{
int size;
CFStringRef str;
f_string_to_utf8(1);
str = CFStringCreateWithBytes(NULL, (const UInt8 *)(Pike_sp[-1].u.string->str), (CFIndex)Pike_sp[-1].u.string->len, kCFStringEncodingUTF8, false);
size = CFArrayGetCount(THIS->_paths);
CFArrayInsertValueAtIndex( (CFMutableArrayRef)THIS->_paths, size, str);
}
pop_stack();
}

}
/*! @decl void set_callback(function callback)
 *! Sets the function that will be called when a file notification event is received.
 *! 
 *! The method signature for the callback is:
 *! 
 *! void event_callback(string path, int flags, int event_id)
 */
#define f_FSEvents_EventStream_set_callback_defined

#ifdef f_FSEvents_EventStream_set_callback_fun_num_used
DEFAULT_CMOD_STORAGE ptrdiff_t f_FSEvents_EventStream_set_callback_fun_num = 0;

#endif /* f_FSEvents_EventStream_set_callback_fun_num_used */
DEFAULT_CMOD_STORAGE void f_FSEvents_EventStream_set_callback(INT32 args) {
#line 223 "/home/hww3/pike-git/src/modules/FSEvents/fsevents.cmod"
struct svalue * callback;
#line 223 "/home/hww3/pike-git/src/modules/FSEvents/fsevents.cmod"
if(args != 1) wrong_number_of_args_error("set_callback",args,1);
#line 223 "/home/hww3/pike-git/src/modules/FSEvents/fsevents.cmod"
if(TYPEOF(Pike_sp[0-1]) != PIKE_T_FUNCTION) SIMPLE_ARG_TYPE_ERROR("set_callback",1,"function");
#line 223 "/home/hww3/pike-git/src/modules/FSEvents/fsevents.cmod"
callback=Pike_sp+0-1; dmalloc_touch_svalue(Pike_sp+0-1);
{
assign_svalue(&THIS->callback_func, callback);
pop_stack();
}

}
#line 229 "/home/hww3/pike-git/src/modules/FSEvents/fsevents.cmod"
/*
 *   TODO we should allow the runloop to be specified.
 */

/*! @decl void start()
 *!
 *! Requests that new events be delivered to this EventStream.
 *! 
 *! If a value was supplied for the since_when parameter then "historical" events will be sent via 
 *! your callback first, then a HistoryDone event, then "contemporary" events will be sent on 
 *! an ongoing basis.
 */
#define f_FSEvents_EventStream_start_defined

#ifdef f_FSEvents_EventStream_start_fun_num_used
DEFAULT_CMOD_STORAGE ptrdiff_t f_FSEvents_EventStream_start_fun_num = 0;

#endif /* f_FSEvents_EventStream_start_fun_num_used */
DEFAULT_CMOD_STORAGE void f_FSEvents_EventStream_start(INT32 args) {
#line 241 "/home/hww3/pike-git/src/modules/FSEvents/fsevents.cmod"
if(args != 0) wrong_number_of_args_error("start",args,0);
{
FSEventStreamContext context;

if(THIS->isRunning)
{
Pike_error("EventStream: monitor is already running.\n");
}

if(!THIS->_paths)
Pike_error("EventStream: no paths.\n");

if(!THIS->_sinceWhen)
Pike_error("EventStream: no startdate.\n");

if(!THIS->_latency)
Pike_error("EventStream: no latency.\n");

if(CFArrayGetCount(THIS->_paths))
{ 
THIS->runLoop = CFRunLoopGetCurrent();
CFRetain(THIS->runLoop);

context.version = 0; 
context.info = THIS;
context.retain = NULL;
context.release = NULL;
context.copyDescription = NULL;

THIS->stream = FSEventStreamCreate(kCFAllocatorDefault,
&event_callback,
&context,
THIS->_paths,
THIS->_sinceWhen,
THIS->_latency,
THIS->_flags |
kFSEventStreamCreateFlagUseCFTypes
);

THIS->isRunning = 1;
FSEventStreamScheduleWithRunLoop(THIS->stream, THIS->runLoop, kCFRunLoopDefaultMode);
FSEventStreamStart(THIS->stream);
}
else
{
Pike_error("EventStream: No paths registered for monitoring.\n");
}
}

}
/*! @decl void stop()
 *! Stops watching for new events.
 */
#define f_FSEvents_EventStream_stop_defined

#ifdef f_FSEvents_EventStream_stop_fun_num_used
DEFAULT_CMOD_STORAGE ptrdiff_t f_FSEvents_EventStream_stop_fun_num = 0;

#endif /* f_FSEvents_EventStream_stop_fun_num_used */
DEFAULT_CMOD_STORAGE void f_FSEvents_EventStream_stop(INT32 args) {
#line 293 "/home/hww3/pike-git/src/modules/FSEvents/fsevents.cmod"
if(args != 0) wrong_number_of_args_error("stop",args,0);
{
low_stop();
}

}
#line 298 "/home/hww3/pike-git/src/modules/FSEvents/fsevents.cmod"
void low_stop(void)
{
if(THIS->isRunning)
{
FSEventStreamStop(THIS->stream);
FSEventStreamUnscheduleFromRunLoop(THIS->stream, THIS->runLoop, kCFRunLoopDefaultMode);
FSEventStreamInvalidate(THIS->stream);
FSEventStreamRelease(THIS->stream);
CFRelease(THIS->runLoop);
THIS->isRunning = 0;
}
}

/**
 * FSEvents callback function. The frequency at which this callback is
 * called depends upon the notification latency value. This callback is usually
 * called with more than one event and so multiple calls to the callback occur.
 *
 * @param streamRef          The calling stream reference
 * @param clientCallBackInfo Any client callback info that was supplied when the stream was created
 * @param numEvents          The number of events being supplied
 * @param eventPaths         An array of the event's paths
 * @param eventFlags         An array of flags associated with the events
 * @param eventIds           An array of IDs associated with the events
 */

static void event_callback(ConstFSEventStreamRef streamRef,
void *clientCallBackInfo,
size_t numEvents,
void *eventPaths,
const FSEventStreamEventFlags eventFlags[],
const FSEventStreamEventId eventIds[])
{
struct event_callback_ctx ctx;
struct event_callback_ctx * ctx_ptr;

ctx.streamRef = streamRef;
ctx.clientCallBackInfo = clientCallBackInfo;
ctx.numEvents = numEvents;
ctx.eventPaths = eventPaths;
ctx.eventFlags = eventFlags;
ctx.eventIds = eventIds;

ctx_ptr = &ctx;

call_with_interpreter((void *)&do_event_callback, (void *)ctx_ptr); 
/*
      do_event_callback(streamRef, clientCallBackInfo, numEvents, eventPaths, eventFlags, eventIds);
*/
}

static void do_event_callback(struct event_callback_ctx * ctx)
{
size_t cnt = 0;

for(cnt = 0; cnt < ctx->numEvents; cnt++)
{
CFStringRef eventPath;
struct pike_string * str;
const char * u8s;
struct FSEvents_EventStream_struct * eventStreamObj;

eventPath = CFArrayGetValueAtIndex(ctx->eventPaths, (CFIndex)cnt);
str = string_from_cfstring(eventPath);
push_string(str);
f_utf8_to_string(1);
push_int(ctx->eventFlags[cnt]);
push_int(ctx->eventIds[cnt]);
eventStreamObj = (struct FSEvents_EventStream_struct *)(ctx->clientCallBackInfo);
apply_svalue(&eventStreamObj->callback_func, 3);
}
}

struct pike_string * string_from_cfstring(CFStringRef cfString)
{
const char *useUTF8StringPtr = NULL;
UInt8 *freeUTF8StringPtr = NULL;
struct pike_string * str = NULL;
long utf8Length;

CFIndex stringLength = CFStringGetLength(cfString), usedBytes = 0L;

if((useUTF8StringPtr = CFStringGetCStringPtr(cfString, kCFStringEncodingUTF8)) == NULL) {
if((freeUTF8StringPtr = malloc(stringLength + 1L)) != NULL) {
CFStringGetBytes(cfString, CFRangeMake(0L, stringLength), kCFStringEncodingUTF8, '?', false, freeUTF8StringPtr, stringLength, &usedBytes);
freeUTF8StringPtr[usedBytes] = 0;
useUTF8StringPtr = (const char *)freeUTF8StringPtr;
}
}

utf8Length = (long)((freeUTF8StringPtr != NULL) ? usedBytes : stringLength);

if(useUTF8StringPtr != NULL) {
/*
     *  useUTF8StringPtr points to a NULL terminated UTF8 encoded string.
     *  utf8Length contains the length of the UTF8 string.
     */
str = make_shared_binary_string(useUTF8StringPtr, utf8Length);
}

if(freeUTF8StringPtr != NULL) { free(freeUTF8StringPtr); freeUTF8StringPtr = NULL; }

return str;
}


#undef internal_init_FSEvents_EventStream_defined
#define internal_init_FSEvents_EventStream_defined

#undef FSEvents_EventStream_event_handler_defined
#define FSEvents_EventStream_event_handler_defined
static void init_FSEvents_EventStream_struct(void)
#line 404 "/home/hww3/pike-git/src/modules/FSEvents/fsevents.cmod"
{
}


#undef FSEvents_EventStream_gc_live_obj
#define FSEvents_EventStream_gc_live_obj

#undef internal_exit_FSEvents_EventStream_defined
#define internal_exit_FSEvents_EventStream_defined

#undef FSEvents_EventStream_event_handler_defined
#define FSEvents_EventStream_event_handler_defined
static void exit_FSEvents_EventStream_struct(void)
#line 408 "/home/hww3/pike-git/src/modules/FSEvents/fsevents.cmod"
{
#ifdef HAVE_FRAMEWORK_CORESERVICES
 low_stop();
if(THIS->_paths) CFRelease(THIS->_paths);
#endif /* HAVE_FRAMEWORK_CORESERVICES */
 }


#ifdef FSEvents_EventStream_event_handler_defined
static void FSEvents_EventStream_event_handler(int ev) {
  switch(ev) {

#ifdef internal_init_FSEvents_EventStream_defined
  case PROG_EVENT_INIT: init_FSEvents_EventStream_struct(); break;

#endif /* internal_init_FSEvents_EventStream_defined */

#ifdef internal_exit_FSEvents_EventStream_defined
  case PROG_EVENT_EXIT: exit_FSEvents_EventStream_struct(); break;

#endif /* internal_exit_FSEvents_EventStream_defined */
  default: break; 
  }
}

#endif /* FSEvents_EventStream_event_handler_defined */
#line 417 "/home/hww3/pike-git/src/modules/FSEvents/fsevents.cmod"
#endif /* HAVE_FRAMEWORK_CORESERVICES */

PIKE_MODULE_INIT
{

#ifdef CMOD_MAP_PROGRAM_IDS_DEFINED
set_program_id_to_id( ___cmod_map_program_ids );

#endif /* CMOD_MAP_PROGRAM_IDS_DEFINED */

#ifdef class_FSEvents_EventStream_defined

#ifdef PROG_FSEVENTS_EVENTSTREAM_ID
#line 56 "/home/hww3/pike-git/src/modules/FSEvents/fsevents.cmod"
  START_NEW_PROGRAM_ID(FSEVENTS_EVENTSTREAM);
#else
#line 56 "/home/hww3/pike-git/src/modules/FSEvents/fsevents.cmod"
  start_new_program();

#endif /* PROG_FSEVENTS_EVENTSTREAM_ID */
#line 56 "/home/hww3/pike-git/src/modules/FSEvents/fsevents.cmod"
  FSEvents_EventStream_program = Pike_compiler->new_program;

#ifndef tObjImpl_FSEVENTS_EVENTSTREAM

#undef tObjIs_FSEVENTS_EVENTSTREAM
#define tObjIs_FSEVENTS_EVENTSTREAM "\3\1\177\0\0\2"

#undef tObjImpl_FSEVENTS_EVENTSTREAM
#define tObjImpl_FSEVENTS_EVENTSTREAM "\3\0\177\0\0\2"

#endif /* tObjImpl_FSEVENTS_EVENTSTREAM */

#ifdef THIS_FSEVENTS_EVENTSTREAM
  FSEvents_EventStream_storage_offset = ADD_STORAGE(struct FSEvents_EventStream_struct);
#endif /* THIS_FSEVENTS_EVENTSTREAM */

#ifdef var_callback_func_FSEvents_EventStream_defined
  PIKE_MAP_VARIABLE("callback_func", FSEvents_EventStream_storage_offset + OFFSETOF(FSEvents_EventStream_struct, callback_func),
                    tFuncV(tNone,tMix,tAny), PIKE_T_MIXED, 0);
#endif /* var_callback_func_FSEvents_EventStream_defined */

#ifdef FSEvents_EventStream_event_handler_defined
  pike_set_prog_event_callback(FSEvents_EventStream_event_handler);

#ifndef FSEvents_EventStream_gc_live_obj
  Pike_compiler->new_program->flags &= ~PROGRAM_LIVE_OBJ;

#endif /* FSEvents_EventStream_gc_live_obj */

#endif /* FSEvents_EventStream_event_handler_defined */

#ifdef f_FSEvents_EventStream_is_started_defined

#ifdef f_FSEvents_EventStream_is_started_fun_num_used
  f_FSEvents_EventStream_is_started_fun_num =
#endif /* f_FSEvents_EventStream_is_started_fun_num_used */
#line 71 "/home/hww3/pike-git/src/modules/FSEvents/fsevents.cmod"
    ADD_FUNCTION2("is_started", f_FSEvents_EventStream_is_started, tFunc(tNone,"\10\200\0\0\0\177\377\377\377"), 0, OPT_EXTERNAL_DEPEND|OPT_SIDE_EFFECT);

#endif /* f_FSEvents_EventStream_is_started_defined */

#ifdef f_FSEvents_EventStream_flush_async_defined

#ifdef f_FSEvents_EventStream_flush_async_fun_num_used
  f_FSEvents_EventStream_flush_async_fun_num =
#endif /* f_FSEvents_EventStream_flush_async_fun_num_used */
#line 87 "/home/hww3/pike-git/src/modules/FSEvents/fsevents.cmod"
    ADD_FUNCTION2("flush_async", f_FSEvents_EventStream_flush_async, tFunc(tNone,tVoid), 0, OPT_EXTERNAL_DEPEND|OPT_SIDE_EFFECT);

#endif /* f_FSEvents_EventStream_flush_async_defined */

#ifdef f_FSEvents_EventStream_flush_sync_defined

#ifdef f_FSEvents_EventStream_flush_sync_fun_num_used
  f_FSEvents_EventStream_flush_sync_fun_num =
#endif /* f_FSEvents_EventStream_flush_sync_fun_num_used */
#line 107 "/home/hww3/pike-git/src/modules/FSEvents/fsevents.cmod"
    ADD_FUNCTION2("flush_sync", f_FSEvents_EventStream_flush_sync, tFunc(tNone,tVoid), 0, OPT_EXTERNAL_DEPEND|OPT_SIDE_EFFECT);

#endif /* f_FSEvents_EventStream_flush_sync_defined */

#ifdef f_FSEvents_EventStream_create_defined

#ifdef f_FSEvents_EventStream_create_fun_num_used
  f_FSEvents_EventStream_create_fun_num =
#endif /* f_FSEvents_EventStream_create_fun_num_used */
#line 139 "/home/hww3/pike-git/src/modules/FSEvents/fsevents.cmod"
    ADD_FUNCTION2("create", f_FSEvents_EventStream_create, tFunc(tArray tFloat tOr("\10\200\0\0\0\177\377\377\377",tVoid) tOr("\10\200\0\0\0\177\377\377\377",tVoid),tVoid), 0, OPT_EXTERNAL_DEPEND|OPT_SIDE_EFFECT);

#endif /* f_FSEvents_EventStream_create_defined */

#ifdef f_FSEvents_EventStream_add_path_defined

#ifdef f_FSEvents_EventStream_add_path_fun_num_used
  f_FSEvents_EventStream_add_path_fun_num =
#endif /* f_FSEvents_EventStream_add_path_fun_num_used */
#line 197 "/home/hww3/pike-git/src/modules/FSEvents/fsevents.cmod"
    ADD_FUNCTION2("add_path", f_FSEvents_EventStream_add_path, tFunc(tStr,tVoid), 0, OPT_EXTERNAL_DEPEND|OPT_SIDE_EFFECT);

#endif /* f_FSEvents_EventStream_add_path_defined */

#ifdef f_FSEvents_EventStream_set_callback_defined

#ifdef f_FSEvents_EventStream_set_callback_fun_num_used
  f_FSEvents_EventStream_set_callback_fun_num =
#endif /* f_FSEvents_EventStream_set_callback_fun_num_used */
#line 223 "/home/hww3/pike-git/src/modules/FSEvents/fsevents.cmod"
    ADD_FUNCTION2("set_callback", f_FSEvents_EventStream_set_callback, tFunc(tFuncV(tNone,tMix,tAny),tVoid), 0, OPT_EXTERNAL_DEPEND|OPT_SIDE_EFFECT);

#endif /* f_FSEvents_EventStream_set_callback_defined */

#ifdef f_FSEvents_EventStream_start_defined

#ifdef f_FSEvents_EventStream_start_fun_num_used
  f_FSEvents_EventStream_start_fun_num =
#endif /* f_FSEvents_EventStream_start_fun_num_used */
#line 241 "/home/hww3/pike-git/src/modules/FSEvents/fsevents.cmod"
    ADD_FUNCTION2("start", f_FSEvents_EventStream_start, tFunc(tNone,tVoid), 0, OPT_EXTERNAL_DEPEND|OPT_SIDE_EFFECT);

#endif /* f_FSEvents_EventStream_start_defined */

#ifdef f_FSEvents_EventStream_stop_defined

#ifdef f_FSEvents_EventStream_stop_fun_num_used
  f_FSEvents_EventStream_stop_fun_num =
#endif /* f_FSEvents_EventStream_stop_fun_num_used */
#line 293 "/home/hww3/pike-git/src/modules/FSEvents/fsevents.cmod"
    ADD_FUNCTION2("stop", f_FSEvents_EventStream_stop, tFunc(tNone,tVoid), 0, OPT_EXTERNAL_DEPEND|OPT_SIDE_EFFECT);

#endif /* f_FSEvents_EventStream_stop_defined */
#line 56 "/home/hww3/pike-git/src/modules/FSEvents/fsevents.cmod"
  FSEvents_EventStream_program=end_program();
#line 56 "/home/hww3/pike-git/src/modules/FSEvents/fsevents.cmod"
#ifdef FSEvents_EventStream_program_fun_num_used
FSEvents_EventStream_program_fun_num=
#endif
 add_program_constant("EventStream",FSEvents_EventStream_program,0);

#endif /* class_FSEvents_EventStream_defined */

#ifdef CMOD_MAP_PROGRAM_IDS_DEFINED
set_program_id_to_id( 0 );
#endif /* CMOD_MAP_PROGRAM_IDS_DEFINED */
#line 421 "/home/hww3/pike-git/src/modules/FSEvents/fsevents.cmod"
;
/*! @decl constant kFSEventStreamEventFlagNone 
 */

/*! @decl constant kFSEventStreamEventFlagMustScanSubDirs 
 */

/*! @decl constant kFSEventStreamEventFlagUserDropped 
 */

/*! @decl constant kFSEventStreamEventFlagKernelDropped
 */

/*! @decl constant kFSEventStreamEventFlagEventIdsWrapped 
 */

/*! @decl constant kFSEventStreamEventFlagHistoryDone
 */

/*! @decl constant kFSEventStreamEventFlagRootChanged
 */

/*! @decl constant kFSEventStreamEventFlagMount
 */

/*! @decl constant kFSEventStreamEventFlagUnmount
 */

/*! @decl constant kFSEventStreamEventFlagItemCreated
 *! Available in MacOS X 10.7 and newer.
 */

/*! @decl constant kFSEventStreamEventFlagItemRemoved 
 *! Available in MacOS X 10.7 and newer.
 */

/*! @decl constant kFSEventStreamEventFlagInodeMetaMod
 *! Available in MacOS X 10.7 and newer.
 */

/*! @decl constant kFSEventStreamEventFlagRenamed
 *! Available in MacOS X 10.7 and newer.
 */

/*! @decl constant kFSEventStreamEventFlagItemModified 
 *! Available in MacOS X 10.7 and newer.
 */

/*! @decl constant kFSEventStreamEventFlagFinderInfoMod
 *! Available in MacOS X 10.7 and newer.
 */

/*! @decl constant kFSEventStreamEventFlagChangeOwner
 *! Available in MacOS X 10.7 and newer.
 */

/*! @decl constant kFSEventStreamEventFlagXattrMod 
 *! Available in MacOS X 10.7 and newer.
 */

/*! @decl constant kFSEventStreamEventFlagIsFile
 *! Available in MacOS X 10.7 and newer.
 */

/*! @decl constant kFSEventStreamEventFlagIsDir
 *! Available in MacOS X 10.7 and newer.
 */

/*! @decl constant kFSEventStreamEventFlagIsSymlink
 *! Available in MacOS X 10.7 and newer.
 */

/*! @decl constant kFSEventStreamCreateFlagFileEvents 
 *! Available in MacOS X 10.7 and newer.
 */

/*! @decl constant kFSEventStreamCreateFlagIgnoreSelf
 *! Available in MacOS X 10.6 and newer.
 */

/*! @decl constant kFSEventStreamCreateFlagWatchRoot
 */

/*! @decl constant kFSEventStreamCreateFlagNoDefer
 */

/*! @decl constant kFSEventStreamCreateFlagNone
 */

/*! @decl constant kFSEventStreamEventIdSinceNow
 */

#if HAVE_DECL_KFSEVENTSTREAMEVENTFLAGNONE
 ADD_ICONST(kFSEventStreamEventFlagNone);
#endif

#if HAVE_DECL_KFSEVENTSTREAMEVENTFLAGMUSTSCANSUBDIRS
 ADD_ICONST(kFSEventStreamEventFlagMustScanSubDirs);
#endif

#if HAVE_DECL_KFSEVENTSTREAMEVENTFLAGUSERDROPPED
 ADD_ICONST(kFSEventStreamEventFlagUserDropped);
#endif

#if HAVE_DECL_KFSEVENTSTREAMEVENTFLAGKERNELDROPPED
 ADD_ICONST(kFSEventStreamEventFlagKernelDropped);
#endif

#if HAVE_DECL_KFSEVENTSTREAMEVENTFLAGEVENTIDSWRAPPED
 ADD_ICONST(kFSEventStreamEventFlagEventIdsWrapped);
#endif

#if HAVE_DECL_KFSEVENTSTREAMEVENTFLAGHISTORYDONE
 ADD_ICONST(kFSEventStreamEventFlagHistoryDone);
#endif

#if HAVE_DECL_KFSEVENTSTREAMEVENTFLAGROOTCHANGED
 ADD_ICONST(kFSEventStreamEventFlagRootChanged);
#endif

#if HAVE_DECL_KFSEVENTSTREAMEVENTFLAGMOUNT
 ADD_ICONST(kFSEventStreamEventFlagMount);
#endif

#if HAVE_DECL_KFSEVENTSTREAMEVENTFLAGUNMOUNT
 ADD_ICONST(kFSEventStreamEventFlagUnmount);
#endif

#if HAVE_DECL_KFSEVENTSTREAMEVENTFLAGITEMCREATED
 ADD_ICONST(kFSEventStreamEventFlagItemCreated);
#endif

#if HAVE_DECL_KFSEVENTSTREAMEVENTFLAGITEMREMOVED
 ADD_ICONST(kFSEventStreamEventFlagItemRemoved);
#endif 

#if HAVE_DECL_KFSEVENTSTREAMEVENTFLAGINODEMETAMOD
 ADD_ICONST(kFSEventStreamEventFlagItemInodeMetaMod);
#endif 

#if HAVE_DECL_KFSEVENTSTREAMEVENTFLAGITEMRENAMED
 ADD_ICONST(kFSEventStreamEventFlagItemRenamed);
#endif

#if HAVE_DECL_KFSEVENTSTREAMEVENTFLAGITEMMODIFIED
 ADD_ICONST(kFSEventStreamEventFlagItemModified);
#endif

#if HAVE_DECL_KFSEVENTSTREAMEVENTFLAGFINDERINFOMOD
 ADD_ICONST(kFSEventStreamEventFlagItemFinderInfoMod);
#endif

#if HAVE_DECL_KFSEVENTSTREAMEVENTFLAGITEMCHANGEOWNER
 ADD_ICONST(kFSEventStreamEventFlagItemChangeOwner);
#endif

#if HAVE_DECL_KFSEVENTSTREAMEVENTFLAGITEMXATTRMOD
 ADD_ICONST(kFSEventStreamEventFlagItemXattrMod);
#endif

#if HAVE_DECL_KFSEVENTSTREAMEVENTFLAGITEMISFILE
 ADD_ICONST(kFSEventStreamEventFlagItemIsFile);
#endif

#if HAVE_DECL_KFSEVENTSTREAMEVENTFLAGITEMISDIR
 ADD_ICONST(kFSEventStreamEventFlagItemIsDir);
#endif

#if HAVE_DECL_KFSEVENTSTREAMEVENTFLAGITEMISSYMLINK
 ADD_ICONST(kFSEventStreamEventFlagItemIsSymlink);
#endif

/*
 * flags for the stream creation
 */

#if HAVE_DECL_KFSEVENTSTREAMCREATEFLAGFILEEVENTS
 ADD_ICONST(kFSEventStreamCreateFlagFileEvents);
#endif

#if HAVE_DECL_KFSEVENTSTREAMCREATEFLAGIGNORESELF
 ADD_ICONST(kFSEventStreamCreateFlagIgnoreSelf);
#endif

#if HAVE_DECL_KFSEVENTSTREAMCREATEFLAGWATCHROOT
 ADD_ICONST(kFSEventStreamCreateFlagWatchRoot);
#endif

#if HAVE_DECL_KFSEVENTSTREAMCREATEFLAGNODEFER
 ADD_ICONST(kFSEventStreamCreateFlagNoDefer);
#endif

#if HAVE_DECL_KFSEVENTSTREAMCREATEFLAGNONE

ADD_ICONST(kFSEventStreamCreateFlagNone);
#endif

#if HAVE_DECL_KFSEVENTSTREAMEVENTIDSINCENOW
 ADD_ICONST(kFSEventStreamEventIdSinceNow);
#endif

}

PIKE_MODULE_EXIT
{

#ifdef class_FSEvents_EventStream_defined
  if(FSEvents_EventStream_program) {
#line 56 "/home/hww3/pike-git/src/modules/FSEvents/fsevents.cmod"
    free_program(FSEvents_EventStream_program);
    FSEvents_EventStream_program=0;
  }

#endif /* class_FSEvents_EventStream_defined */
#line 626 "/home/hww3/pike-git/src/modules/FSEvents/fsevents.cmod"
;
}

/*! @endclass
 */

/*! @endmodule
 */

/*! @endmodule
 */


#ifdef CMOD_MAP_PROGRAM_IDS_DEFINED
static int ___cmod_map_program_ids(int id)
{
  int i = 0;
  if( (id&0x7f000000) != 0x7f000000 ) return id;
  id = id&0x00ffffff;
#ifdef class_FSEvents_EventStream_defined
if(id==2) return FSEvents_EventStream_program->id;
#endif
  return 0;
}
#endif /* CMOD_MAP_PROGRAM_IDS_DEFINED */
