#!/usr/bin/env ruby # # ioservice.rb # # Copyright (c) 2007 Naoki Hiroshima # You can redistribute it and/or modify it under the same terms as GPL2. # # Author:: Naoki Hiroshima # require 'osx/cocoa' require 'mach' OSX.require_framework 'IOKit' OSX.load_bridge_support_file 'myIOKit.bridgesupport' class IOService def initialize @conn = nil @@masterPort ||= OSX::ObjcPtr.allocate_as_int32 unless @@masterPort.int OSX::IOMasterPort 0, @@masterPort end end def open(service, &block) dict = OSX::IOServiceMatching service iterator = OSX::ObjcPtr.allocate_as_int32 r = OSX::IOServiceGetMatchingServices @@masterPort.uint, dict, iterator return if r != 0 device = OSX::IOIteratorNext iterator.uint OSX::IOObjectRelease iterator.uint return if device == 0 @conn = OSX::ObjcPtr.allocate_as_int32 r = OSX::IOServiceOpen device, OSX::Mach.taskSelf, 0, @conn OSX::IOObjectRelease device return if r != 0 if block yield self close end end def close OSX::IOServiceClose @conn.uint if @conn end def structure_io(cmd, isize, osize, i, o) return OSX::IOConnectMethodStructureIStructureO(@conn.int, cmd, isize, osize, i, o) end def alloc_structure(size) return OSX::ObjcPtr.new(size) end end