2 * $Id: rb_event_handler.c 373 2006-02-16 21:37:43Z 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 event_args;
43 static void c_mark (RbEventHandler *h)
45 rb_gc_mark (h->callback);
48 static void c_free (RbEventHandler *h)
50 if (h->real && !h->deleted)
51 ecore_event_handler_del (h->real);
56 static VALUE c_alloc (VALUE klass)
58 RbEventHandler *h = NULL;
60 return Data_Make_Struct (klass, RbEventHandler, c_mark, c_free, h);
63 static VALUE c_init (VALUE self, VALUE klass)
65 RbEventHandler *h = NULL;
68 if (!rb_block_given_p ())
69 rb_raise (rb_eStandardError, "block missing");
71 Data_Get_Struct (self, RbEventHandler, h);
73 if (rb_obj_is_kind_of (klass, rb_cModule) != Qtrue)
74 rb_raise (rb_eArgError, "invalid argument");
76 t = NUM2INT (rb_const_get (klass, rb_intern ("TYPE")));
77 if (t <= ECORE_EVENT_NONE)
78 rb_raise (rb_eStandardError, "invalid event");
80 h->event_class = klass;
81 h->callback = rb_block_proc ();
83 h->real = ecore_event_handler_add (t, on_ecore_event, h);
88 static VALUE c_delete (VALUE self)
90 RbEventHandler *h = NULL;
92 Data_Get_Struct (self, RbEventHandler, h);
94 if (h->real && !h->deleted) {
95 ecore_event_handler_del (h->real);
99 rb_raise (rb_eException, "EventHandler already deleted!");
104 static int on_ecore_event (void *data, int type, void *event)
106 RbEventHandler *h = data;
107 VALUE obj, tmp = (VALUE) event;
109 obj = rb_obj_alloc (h->event_class);
111 if (rb_respond_to (h->event_class, rb_intern ("raise")))
112 rb_apply (obj, rb_intern ("initialize"), tmp);
114 rb_obj_call_init (obj, 1, &tmp);
116 tmp = rb_funcall (h->callback, rb_intern ("call"), 1, obj);
118 /* if the block returned false, don't call the other
121 return (tmp != Qfalse);
124 VALUE c_ev_inherited (VALUE klass, VALUE child)
128 t = INT2FIX (ecore_event_type_new ());
129 rb_hash_aset (event_classes, t, child);
131 rb_define_const (child, "TYPE", t);
132 rb_define_singleton_method (child, "raise", c_ev_raise, -2);
137 static VALUE c_ev_init (int argc, VALUE *argv, VALUE self)
142 static void free_event_args (void *data, void *argv)
144 rb_ary_delete (event_args, (VALUE) argv);
147 static VALUE c_ev_raise (VALUE klass, VALUE argv)
151 rb_ary_push (event_args, argv);
153 t = rb_const_get (klass, rb_intern ("TYPE"));
154 ecore_event_add (FIX2INT (t), (void *) argv, free_event_args, NULL);
159 void Init_EventHandler (void)
163 cEventHandler = rb_define_class_under (mEcore, "EventHandler",
166 rb_define_alloc_func (cEventHandler, c_alloc);
167 rb_define_method (cEventHandler, "initialize", c_init, 1);
168 rb_define_method (cEventHandler, "delete", c_delete, 0);
170 event_classes = rb_hash_new ();
171 rb_global_variable (&event_classes);
173 event_args = rb_ary_new ();
174 rb_global_variable (&event_args);
176 /* define a base event class */
177 cEcoreEvent = rb_define_class_under (mEcore, "Event", rb_cObject);
178 rb_define_singleton_method (cEcoreEvent, "inherited", c_ev_inherited, 1);
179 rb_define_method (cEcoreEvent, "initialize", c_ev_init, -1);