2 * $Id: rb_event_handler.c 372 2006-02-16 20:39:55Z tilman $
4 * Copyright (C) 2004 ruby-ecore team (see AUTHORS)
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
26 #define __RB_EVENT_HANDLER_C
28 #include "rb_event_handler.h"
31 Ecore_Event_Handler *real;
37 static int on_ecore_event (void *data, int type, void *event);
38 static VALUE c_ev_raise (VALUE klass, VALUE argv);
40 VALUE event_classes, cEcoreEvent;
41 static VALUE handlers;
43 static void c_mark (RbEventHandler *h)
45 rb_gc_mark (h->callback);
48 static void c_free (RbEventHandler *h)
50 RbEventHandler *h2 = NULL;
51 int len = RARRAY (handlers)->len, i;
54 if (!h->real || h->deleted) {
59 for (i = 0; i < len; i++) {
60 el = rb_ary_shift (handlers);
62 Data_Get_Struct (el, RbEventHandler, h2);
65 ecore_event_handler_del (h->real);
69 rb_ary_push (handlers, el);
75 static VALUE c_alloc (VALUE klass)
77 RbEventHandler *h = NULL;
79 return Data_Make_Struct (klass, RbEventHandler, c_mark, c_free, h);
82 static VALUE c_init (VALUE self, VALUE type)
84 RbEventHandler *h = NULL;
87 if (!rb_block_given_p ())
88 rb_raise (rb_eStandardError, "block missing");
90 Data_Get_Struct (self, RbEventHandler, h);
92 if (rb_obj_is_kind_of (type, rb_cModule) != Qtrue)
93 rb_raise (rb_eArgError, "invalid argument");
95 t = NUM2INT (rb_const_get (type, rb_intern ("TYPE")));
96 if (t <= ECORE_EVENT_NONE)
97 rb_raise (rb_eStandardError, "invalid type");
100 h->callback = rb_block_proc ();
102 h->real = ecore_event_handler_add (t, on_ecore_event, NULL);
104 rb_ary_push (handlers, self);
109 static VALUE c_delete (VALUE self)
111 RbEventHandler *h = NULL;
112 int len = RARRAY (handlers)->len, i;
115 for (i = 0; i < len; i++) {
116 el = rb_ary_shift (handlers);
118 Data_Get_Struct (self, RbEventHandler, h);
119 ecore_event_handler_del (h->real);
126 rb_ary_push (handlers, el);
132 static int on_ecore_event (void *data, int type, void *event)
134 RbEventHandler *h = NULL;
135 VALUE handler, klass, obj, tmp, res;
138 /* instantiate the event object
139 * first, find the class we're gonna use
141 klass = rb_hash_aref (event_classes, INT2NUM (type));
143 rb_raise (rb_eException, "Cannot find event class "
144 "for event %i\n", type);
146 /* now create and init the object */
149 /* if tmp is a Ruby class, we'll just pass the arguments to the
151 * if it's a c struct, we can use rb_class_new_instance()
153 if (rb_respond_to (klass, rb_intern ("raise"))) {
154 obj = rb_obj_alloc (klass);
155 rb_apply (obj, rb_intern ("initialize"), tmp);
157 obj = rb_class_new_instance (1, &tmp, klass);
159 len = RARRAY (handlers)->len;
161 for (i = 0; i < len; i++) {
162 handler = rb_ary_entry (handlers, i);
163 Data_Get_Struct (handler, RbEventHandler, h);
165 if (h->type == type) {
166 res = rb_funcall (h->callback, rb_intern ("call"), 1, obj);
168 /* if the block returned false, don't call the other
181 VALUE c_ev_inherited (VALUE klass, VALUE child)
185 t = INT2FIX (ecore_event_type_new ());
186 rb_hash_aset (event_classes, t, child);
188 rb_define_const (child, "TYPE", t);
189 rb_define_singleton_method (child, "raise", c_ev_raise, -2);
194 static VALUE c_ev_init (int argc, VALUE *argv, VALUE self)
199 static void free_ruby_event (void *data, void *event)
204 static VALUE c_ev_raise (VALUE klass, VALUE argv)
208 t = rb_const_get (klass, rb_intern ("TYPE"));
209 ecore_event_add (FIX2INT (t), (void *) argv, free_ruby_event, NULL);
214 void Init_EventHandler (void)
218 cEventHandler = rb_define_class_under (mEcore, "EventHandler",
221 rb_define_alloc_func (cEventHandler, c_alloc);
222 rb_define_method (cEventHandler, "initialize", c_init, 1);
223 rb_define_method (cEventHandler, "delete", c_delete, 0);
225 handlers = rb_ary_new ();
226 rb_global_variable (&handlers);
228 event_classes = rb_hash_new ();
229 rb_global_variable (&event_classes);
231 /* define a base event class */
232 cEcoreEvent = rb_define_class_under (mEcore, "Event", rb_cObject);
233 rb_define_singleton_method (cEcoreEvent, "inherited", c_ev_inherited, 1);
234 rb_define_method (cEcoreEvent, "initialize", c_ev_init, -1);