Permalink
Browse files

add ffi-test

  • Loading branch information...
1 parent 961ddfa commit 43fba8b288f1bb31e75888f69ab8f26950206e8b @jeremyz committed Apr 15, 2012
View
@@ -0,0 +1,27 @@
+CC = gcc
+
+default: all
+
+clib.o: clib.c
+ $(CC) -fPIC -c $<
+
+libclib.so: clib.o
+ $(CC) -shared -Wl,-soname,libclib.so -Wall -o libclib.so $<
+
+enum.o: enum.c
+ $(CC) -c -Wall -I. $<
+
+callback.o: callback.c
+ $(CC) -c -Wall -I. $<
+
+enum: enum.o libclib.so
+ $(CC) enum.o -L. -lclib -lcrypto -o enum
+
+callback: callback.o libclib.so
+ $(CC) callback.o -L. -lclib -lcrypto -o callback
+
+all: enum callback
+
+clean:
+ rm -f enum callback *.o *.so *~
+
View
Binary file not shown.
View
@@ -0,0 +1,41 @@
+#include<stdio.h>
+#include<clib.h>
+
+static void my_cb0(int val) { fprintf(stderr,"\\-CB0 %d\n",val); }
+static void my_cb1(int val) { fprintf(stderr,"\\-CB1 %d\n",val); }
+static void my_cb2(int val) { fprintf(stderr,"\\-CB2 %d\n",val); }
+static int my_cb3(int val) { fprintf(stderr,"\\-CB3 %d\n",val); return val*2; }
+static CB1 my_cb4() { fprintf(stderr,"\\-CB4\n"); return my_cb3; }
+static int my_cb5(CB1 cb, int val) { fprintf(stderr,"\\-CB5 0x%X %d\n",cb,val); return cb(val); }
+static int my_cb6(int val) { fprintf(stderr," \\-CB6 %d\n",val); return val*2; }
+
+int main(int argc, char* argv[], char* envp[])
+{
+ CB0 cb = 0;
+
+ fprintf(stderr,"\n *** C callback\n");
+
+ init(my_cb0,my_cb1);
+
+ set_cb(my_cb0);
+ call_cb();
+
+ register_cb(66,my_cb0,-1);
+ call_cb();
+
+ register_cbs(69,my_cb1,666,my_cb2,-2);
+ call_cbs();
+
+ cb = get_cb(0);
+ cb(6);
+ cb = get_cb(1);
+ cb(9);
+
+ lookup_block_call_with_enum_arg(my_cb4, 2);
+ lookup_block_call_with_enum_arg(my_cb4, 3);
+
+ callback_call_with_callback_arg(my_cb5, my_cb6, 666);
+
+ return 0;
+}
+
View
@@ -0,0 +1,79 @@
+
+
+#include"clib.h"
+#include<stdio.h>
+
+static CB0 cb0_ = 0;
+static CB0 cb1_ = 0;
+static int a0_ = 0;
+static int a1_ = 0;
+
+void init (CB0 cb0, CB0 cb1) {
+ cb0_ = cb0;
+ cb1_ = cb1;
+}
+
+void set_value(int a0, int verbose) {
+ if(verbose>0) fprintf(stderr,"|--CLIB : set value %d\n",a0);
+ a0_ = a0;
+}
+
+int get_value(int verbose) {
+ if(verbose>0) fprintf(stderr,"|--CLIB : get value %d\n",a0_);
+ return a0_;
+}
+
+void set_cb (CB0 cb0) {
+ fprintf(stderr,"|--CLIB : register callback 0x%X (void)\n",cb0);
+ cb0_ = cb0;
+ a0_ = 99;
+}
+
+CB0 get_cb(int a0) {
+ fprintf(stderr,"|--CLIB : get_cb 0x%X\n",( a0==0 ? cb0_ : cb1_ ));
+ return ( a0==0 ? cb0_ : cb1_ );
+}
+
+void register_cb (int a0, CB0 cb0, int a2) {
+ fprintf(stderr,"|--CLIB : register callback 0x%X (%d)\n",cb0,a0);
+ cb0_ = cb0;
+ a0_ = a0;
+}
+
+void call_cb () {
+ fprintf(stderr,"|--CLIB : call callback 0x%X\n",cb0_);
+ cb0_(a0_);
+}
+
+void register_cbs (int a0, CB0 cb0, int a1, CB0 cb1, int a2) {
+ fprintf(stderr,"|--CLIB : register callback 0x%X (%d)\n",cb0,a0);
+ cb0_ = cb0;
+ a0_ = a0;
+ fprintf(stderr,"|--CLIB : register callback 0x%X (%d)\n",cb1,a1);
+ cb1_ = cb1;
+ a1_ = a1;
+}
+
+void call_cbs () {
+ fprintf(stderr,"|--CLIB : call callback 0x%X (%d)\n",cb0_,a0_);
+ cb0_(a0_);
+ fprintf(stderr,"|--CLIB : call callback 0x%X (%d)\n",cb1_,a1_);
+ cb1_(a1_);
+}
+
+int block_call_with_enum_arg(CB1 cb1, int val) {
+ fprintf(stderr,"|--CLIB : block_call_with_enum_arg(0x%X, %d)\n",cb1,val);
+ return (*cb1)(val);
+}
+
+int lookup_block_call_with_enum_arg(CB2 cb2, int val) {
+ fprintf(stderr,"|--CLIB : lookup_block_call_with_enum_arg(0x%X,%d)\n",cb2,val);
+ CB1 cb1 = cb2 ? (*cb2)() : NULL;
+ fprintf(stderr," \\--CLIB : block_call_with_enum_arg(0x%X,%d)\n",cb1,val);
+ return cb1 ? (*cb1)(val) : 0;
+}
+
+int callback_call_with_callback_arg(CB3 cb3, CB1 cb1, int val) {
+ fprintf(stderr,"|--CLIB : callback_call_with_callback_arg(0x%X,0x%X,%d)\n",cb3,cb1,val);
+ return (*cb3)(cb1,val);
+}
View
@@ -0,0 +1,24 @@
+
+typedef void(*CB0)(int val);
+typedef int(*CB1)(int val);
+typedef CB1(*CB2)();
+typedef int(*CB3)(CB1, int val);
+
+void init (CB0 cb0, CB0 cb1);
+
+void set_value(int a0, int verbose);
+int get_value(int verbose);
+
+void set_cb (CB0 cb);
+CB0 get_cb(int a0);
+
+void register_cb (int a0, CB0 cb, int a2);
+void call_cb ();
+
+void register_cbs (int a0, CB0 cb0, int a1, CB0 cb1, int a2);
+void call_cbs ();
+
+int block_call_with_enum_arg(CB1 cb1, int val);
+int lookup_block_call_with_enum_arg(CB2 cb2, int val);
+
+int callback_call_with_callback_arg(CB3 cb3, CB1 cb1, int val);
View
@@ -0,0 +1,82 @@
+#! /usr/bin/env ruby
+# -*- coding: UTF-8 -*-
+#
+module Native
+ #
+ extend FFI::Library
+ #
+ ffi_lib 'elementary-ver-pre-svn-09.so.0'
+ #
+ attach_variable :elm_version, :pointer
+ attach_function :elm_init, [ :int, :pointer ], :int
+ attach_function :elm_shutdown, [ ], :int
+ attach_function :elm_run, [ ], :void
+ attach_function :elm_exit, [ ], :void
+ #
+ typedef :pointer, :evas_object
+ callback :evas_smart_cb, [ :pointer, :evas_object, :pointer ], :void
+ attach_function :evas_object_show, [ :evas_object ], :void
+ attach_function :evas_object_move, [ :evas_object, :int, :int ], :void
+ attach_function :evas_object_resize, [ :evas_object, :int, :int ], :void
+ attach_function :evas_object_size_hint_weight_set, [ :evas_object, :double, :double ], :void
+ attach_function :evas_object_smart_callback_add, [ :evas_object, :string, :evas_smart_cb, :pointer ], :void
+ #
+ attach_function :elm_object_part_text_set, [ :evas_object, :string, :string ], :void
+ #
+ enum :elm_win_type, [ :elm_win_basic, :elm_win_dialog_basic, :elm_win_desktop, :elm_win_dock, :melm_win_toolbar, :elm_win_menu ]
+ attach_function :elm_win_add, [ :evas_object, :string, :elm_win_type ], :evas_object
+ attach_function :elm_win_title_set, [ :evas_object, :string ], :void
+ attach_function :elm_win_resize_object_add, [ :evas_object, :evas_object ], :void
+ attach_function :elm_bg_add, [ :evas_object ], :evas_object
+ attach_function :elm_label_add, [ :evas_object ], :evas_object
+ #
+ class VersionStruct < FFI::Struct
+ layout :major, :int,
+ :minor, :int,
+ :micro, :int,
+ :revision, :int
+
+ def full
+ [:major,:minor,:micro,:revision].collect { |e| self[e].to_s }.join '.'
+ end
+ end
+ def self.version
+ VersionStruct.new(Native.elm_version).full
+ end
+ #
+end
+#
+puts "Elementary #{Native.version}"
+#
+Native.elm_init 0, FFI::MemoryPointer::NULL
+#
+win_del = Proc.new { |data,evas_object,event_info|
+ Native.elm_exit;
+}
+#
+win = Native.elm_win_add nil, "App name", :elm_win_basic
+Native.elm_win_title_set win, "Window title"
+Native.evas_object_smart_callback_add win, "delete,request", win_del, nil
+#
+bg = Native.elm_bg_add win
+Native.evas_object_size_hint_weight_set bg, 1.0, 1.0
+Native.elm_win_resize_object_add win, bg
+Native.evas_object_show bg
+#
+lb = Native.elm_label_add win
+Native.elm_object_part_text_set lb, nil, "Hello World"
+Native.evas_object_size_hint_weight_set lb, 1.0, 1.0
+Native.elm_win_resize_object_add win, lb
+Native.evas_object_show lb
+#
+Native.evas_object_move win, 300, 300
+Native.evas_object_resize win, 200, 100
+#
+Native.evas_object_show win
+#
+Native.elm_run
+Native.elm_shutdown
+#
+puts "all good"
+#
+# EOF
View
Binary file not shown.
View
@@ -0,0 +1,25 @@
+#include<stdio.h>
+#include<clib.h>
+
+static void my_cb0(int val) { fprintf(stderr,"\\-CB0 %d\n",val); }
+static void my_cb1(int val) { fprintf(stderr,"\\-CB1 %d\n",val); }
+static int my_cb2(int val) { fprintf(stderr,"\\-CB2 %d\n",val); return val*2; }
+
+int main(int argc, char* argv[], char* envp[])
+{
+ fprintf(stderr,"\n *** C enum\n");
+
+ init(my_cb0,my_cb1);
+
+ set_value(6,1);
+ get_value(1);
+
+ set_value(9,1);
+ get_value(1);
+
+ fprintf(stderr,"|-- %d\n",block_call_with_enum_arg(my_cb2, 2));
+ fprintf(stderr,"|-- %d\n",block_call_with_enum_arg(my_cb2, 3));
+
+ return 0;
+}
+
View
@@ -0,0 +1,75 @@
+#! /usr/bin/env ruby
+# -*- coding: UTF-8 -*-
+#
+require 'ffi'
+#
+module Native
+ #
+ extend FFI::Library
+ ffi_lib 'libclib.so'
+ #
+ enum :nums, [ :three, 3, :four, :five, :zero, 0, :one, :two, :sixxx,6, :seven, :eight, :nine, :ten ]
+ #
+ callback :my_cb, [ :nums ], :void
+ attach_function :set_cb, [ :my_cb ], :void
+ attach_function :register_cb, [ :int, :my_cb, :int ], :void
+ attach_function :call_cb, [ ], :void
+ attach_function :register_cbs, [ :int, :my_cb, :int, :my_cb, :int ], :void
+ attach_function :call_cbs, [ ], :void
+ #
+ callback :cb_enum, [ :nums ], :nums
+ callback :cb_lookup, [ ], :cb_enum
+ callback :cb_int_int, [:int ], :int
+ callback :cb_with_cb, [ :cb_int_int, :int ], :int
+ attach_function :lookup_block_call_with_enum_arg, [ :cb_lookup, :nums ], :nums
+ attach_function :callback_call_with_callback_arg, [:cb_with_cb, :cb_int_int, :int], :int
+ #
+ attach_function :get_cb, [ :int ], :my_cb
+ #
+end
+#
+puts "\n *** Ruby callback"
+#
+cb0 = Proc.new { |a| puts "\\-CB0 #{a}" }
+cb1 = Proc.new { |a| puts "\\-CB1 #{a}" }
+cb2 = Proc.new { |a| puts "\\-CB2 #{a}" }
+#
+# callbacks as parameter
+Native.set_cb cb0
+Native.call_cb
+Native.register_cb 66, cb0, -1
+Native.call_cb
+Native.register_cbs 69, cb1, 666, cb2, -2
+Native.call_cbs
+#
+CB0 = Proc.new do |enum_sym|
+ v = Native.enum_type(:nums)[enum_sym]
+ puts "\\-CB3 #{v}"
+ v * 2
+end
+#
+CB4 = Proc.new do
+ puts "\\-CB4"
+ CB0
+end
+#
+CB6 = Proc.new do |v|
+ puts " \\-CB6 #{v}"
+ v*2
+end
+#
+CB5 = Proc.new do |cb,val|
+ puts "\\-CB5 #{val}"
+ cb.call(val)
+end
+#
+cb = Native.get_cb 0
+puts "\\-CB1 #{cb.call(:sixxx)}"
+cb = Native.get_cb 1
+puts "\\-CB2 #{cb.call(9)}"
+#
+puts Native.lookup_block_call_with_enum_arg CB4, 2
+puts Native.lookup_block_call_with_enum_arg CB4, :three
+#
+puts Native.callback_call_with_callback_arg(CB5, CB6, 666)
+#
Oops, something went wrong.

0 comments on commit 43fba8b

Please sign in to comment.