2 * $Id: rb_event_handler.c 365 2006-02-14 21:50:47Z 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 event);
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 */
148 obj = rb_class_new_instance (1, &tmp, klass);
150 len = RARRAY (handlers)->len;
152 for (i = 0; i < len; i++) {
153 handler = rb_ary_entry (handlers, i);
154 Data_Get_Struct (handler, RbEventHandler, h);
156 if (h->type == type) {
157 res = rb_funcall (h->callback, rb_intern ("call"), 1, obj);
159 /* if the block returned false, don't call the other
172 VALUE c_ev_inherited (VALUE klass, VALUE child)
176 t = INT2FIX (ecore_event_type_new ());
177 rb_hash_aset (event_classes, t, child);
179 rb_define_const (child, "TYPE", t);
180 rb_define_singleton_method (child, "raise", c_ev_raise, 1);
185 VALUE c_ev_inherited_noop (VALUE klass, VALUE child)
190 static void free_ruby_event (void *data, void *event)
195 static VALUE c_ev_raise (VALUE klass, VALUE event)
199 t = rb_const_get (klass, rb_intern ("TYPE"));
200 ecore_event_add (FIX2INT (t), (void *) event, free_ruby_event, NULL);
205 void Init_EventHandler (void)
209 cEventHandler = rb_define_class_under (mEcore, "EventHandler",
212 rb_define_alloc_func (cEventHandler, c_alloc);
213 rb_define_method (cEventHandler, "initialize", c_init, 1);
214 rb_define_method (cEventHandler, "delete", c_delete, 0);
216 handlers = rb_ary_new ();
217 rb_global_variable (&handlers);
219 event_classes = rb_hash_new ();
220 rb_global_variable (&event_classes);
222 /* define a base event class */
223 cEcoreEvent = rb_define_class_under (mEcore, "Event", rb_cObject);
224 rb_define_singleton_method (cEcoreEvent, "inherited", c_ev_inherited, 1);