Skip to content

Instantly share code, notes, and snippets.

@samuelkadolph
Created August 12, 2011 02:27
Show Gist options
  • Save samuelkadolph/1141315 to your computer and use it in GitHub Desktop.
Save samuelkadolph/1141315 to your computer and use it in GitHub Desktop.
require "ffi"
class ULLEnum < FFI::Enum
def native_type
FFI::Type::ULONG_LONG
end
end
class MachMsgHeader < FFI::Struct
layout :bits, :uint, :size, :uint, :remote_port, :uint, :local_port, :uint, :reserved, :uint, :id, :int
end
module CarbonCore
extend FFI::Library
ffi_lib "/System/Library/Frameworks/CoreServices.framework/Frameworks/CarbonCore.framework/Versions/Current/CarbonCore"
MACH_MSG_TYPE_MAKE_SEND = 20
def MACH_MSGH_BITS(remote, local) ((remote) | (local << 8)) end
SIGINT = 2
attach_variable :kCFAllocatorDefault, :pointer
attach_variable :kCFRunLoopDefaultMode, :pointer
typedef enum([:kCFStringEncodingASCII, 0x0600, :kCFStringEncodingUTF8, 0x08000100]), :kCFStringEncoding
# typedef ULLEnum.new([:kFSEventStreamEventIdSinceNow, 0xFFFFFFFFFFFFFFFF], :FSEventStreamEventId), :FSEventStreamEventId
typedef :long, :CFIndex
typedef :uint, :mach_port_t
typedef :pointer, :CFMachPortRef
callback :CFMachPortCallBack, [:pointer, :pointer, :CFIndex, :pointer], :void
callback :FSEventStreamCallback, [:pointer, :pointer, :int, :pointer, :pointer, :pointer], :void
callback :signal_handler, [:int], :void
attach_function :CFArrayCreate, [:pointer, :pointer, :int, :pointer], :pointer
attach_function :CFMachPortCreate, [:pointer, :CFMachPortCallBack, :pointer, :bool], :CFMachPortRef
attach_function :CFMachPortGetPort, [:CFMachPortRef], :mach_port_t
attach_function :CFRunLoopAddSource, [:pointer, :pointer, :pointer], :void
attach_function :CFRunLoopGetCurrent, [], :pointer
attach_function :CFRunLoopRun, [], :void
attach_function :CFRunLoopStop, [:pointer], :void
attach_function :CFStringCreateWithCString, [:pointer, :string, :kCFStringEncoding], :pointer
attach_function :FSEventStreamCreate, [:pointer, :FSEventStreamCallback, :pointer, :pointer, :long, :double, :int], :pointer
attach_function :FSEventStreamScheduleWithRunLoop, [:pointer, :pointer, :pointer], :void
attach_function :FSEventStreamStart, [:pointer], :void
attach_function :FSEventStreamStop, [:pointer], :void
attach_function :mach_msg_send, [:pointer], :int
attach_function :signal, [:int, :signal_handler], :void
callback :c, [:pointer], :void
attach_function :pthread_attr_init, [:pointer], :int
attach_function :pthread_create, [:pointer, :pointer, :c, :pointer], :int
end
include CarbonCore
paths = Dir["/Users/samuelkadolph/{abc,def}"]
# paths_pointer = FFI::MemoryPointer.new(:pointer, paths.size)
# paths.each_with_index do |path, i|
# path_pointer = CFStringCreateWithCString(nil, path.encode("UTF-8"), :kCFStringEncodingUTF8)
# paths_pointer.put_pointer(i, path_pointer)
# end
# paths_cfarray = CFArrayCreate(CarbonCore.kCFAllocatorDefault, paths_pointer, paths.size, nil)
paths_ptr = FFI::MemoryPointer.new(:pointer)
paths.each do |path|
path_cfstring = CFStringCreateWithCString(nil, path, :kCFStringEncodingASCII)
paths_ptr.write_pointer(path_cfstring)
end
paths_cfarray = CarbonCore.CFArrayCreate nil, paths_ptr, 1, nil
callback = FFI::Function.new(:void, [:pointer, :pointer, :int, :pointer, :pointer, :pointer], :blocking => true) do |stream, data, events, paths, flags, ids|
p [stream, data, events, paths, flags, ids]
end
stream = FSEventStreamCreate(nil, callback, nil, paths_cfarray, -1, 0.0, 0)
c = FFI::Function.new(:void, [:pointer], :blocking => true) do |a|
FSEventStreamScheduleWithRunLoop(stream, CFRunLoopGetCurrent(), CarbonCore.kCFRunLoopDefaultMode)
FSEventStreamStart(a)
CFRunLoopRun()
end
thread = FFI::MemoryPointer.new(:pointer)
ret = pthread_create(thread, nil, c, stream)
sleep 10000
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment