From: DeNigris S. <se...@cl...> - 2010-02-19 11:28:54
|
> In my RubyCocoa projects I just creat an objective C class that > contains wrapper functions around any c functions that I need. As it > is objective c, rubycocoa knows how to bridge automagically. Thanks! I tried it two ways: 1. Wrapping it in obj-C 2. Writing a C extension that opens the OSX module and inserts the method The C way seemed a little easier/more straight-forward even with gluing manually (about 9 non-comment lines): #include "ruby.h" #include <Carbon/Carbon.h> // CGSSymbolicHotKey is adopted from work by Joe Ranieri, Alacatia Labs. extern CGError CGSSetSymbolicHotKeyEnabled (int hot_key, bool is_enabled); // Function will be mapped as a class method on the OSX module static VALUE t_set_symbolic_hot_key_enabled(VALUE self, VALUE hot_key_code, VALUE is_enabled) { /* Ruby objects must be converted for use in C. Bools are probably okay, but I'm tired, lol*/ CGError result = CGSSetSymbolicHotKeyEnabled(NUM2INT(hot_key_code), is_enabled == Qtrue); return (VALUE) INT2FIX(result); } /* Every extension defines a global Init_* function * which is called when the interpreter loads it */ void Init_symbolic_hot_keys() { mOSX = rb_define_module("OSX"); // Define a module function, the C function it maps to, which takes 2 arguments (not counting self) rb_define_module_function(mOSX, "CGSSetSymbolicHotKeyEnabled", t_set_symbolic_hot_key_enabled, 2); } Sean DeNigris se...@cl... |