2 * Copyright (C) 2004 ruby-ecore team (see AUTHORS)
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 #define __RB_EVENT_HANDLER_C
26 #include "rb_event_handler.h"
29 Ecore_Event_Handler *real;
35 static int on_ecore_event (void *data, int type, void *event);
36 static VALUE c_ev_raise (VALUE klass, VALUE argv);
38 VALUE event_classes, cEcoreEvent;
39 static VALUE event_args;
41 static void c_mark (RbEventHandler *h)
43 rb_gc_mark (h->callback);
46 static void c_free (RbEventHandler *h)
48 if (h->real && !h->deleted)
49 ecore_event_handler_del (h->real);
54 static VALUE c_alloc (VALUE klass)
56 RbEventHandler *h = NULL;
58 return Data_Make_Struct (klass, RbEventHandler, c_mark, c_free, h);
61 static VALUE c_init (VALUE self, VALUE klass)
63 RbEventHandler *h = NULL;
66 if (!rb_block_given_p ())
67 rb_raise (rb_eStandardError, "block missing");
69 Data_Get_Struct (self, RbEventHandler, h);
71 if (rb_obj_is_kind_of (klass, rb_cModule) != Qtrue)
72 rb_raise (rb_eArgError, "invalid argument");
74 t = NUM2INT (rb_const_get (klass, rb_intern ("TYPE")));
75 if (t <= ECORE_EVENT_NONE)
76 rb_raise (rb_eStandardError, "invalid event");
78 h->event_class = klass;
79 h->callback = rb_block_proc ();
81 h->real = ecore_event_handler_add (t, on_ecore_event, h);
86 static VALUE c_delete (VALUE self)
88 RbEventHandler *h = NULL;
90 Data_Get_Struct (self, RbEventHandler, h);
92 if (h->real && !h->deleted) {
93 ecore_event_handler_del (h->real);
97 rb_raise (rb_eException, "EventHandler already deleted!");
102 static int on_ecore_event (void *data, int type, void *event)
104 RbEventHandler *h = data;
105 VALUE obj, tmp = (VALUE) event;
107 obj = rb_obj_alloc (h->event_class);
109 if (rb_respond_to (h->event_class, rb_intern ("raise")))
110 rb_apply (obj, rb_intern ("initialize"), tmp);
112 rb_obj_call_init (obj, 1, &tmp);
114 tmp = rb_funcall (h->callback, rb_intern ("call"), 1, obj);
116 /* if the block returned false, don't call the other
119 return (tmp != Qfalse);
122 VALUE c_ev_inherited (VALUE klass, VALUE child)
126 t = INT2FIX (ecore_event_type_new ());
127 rb_hash_aset (event_classes, t, child);
129 rb_define_const (child, "TYPE", t);
130 rb_define_singleton_method (child, "raise", c_ev_raise, -2);
135 static VALUE c_ev_init (int argc, VALUE *argv, VALUE self)
140 static void free_event_args (void *data, void *argv)
142 rb_ary_delete (event_args, (VALUE) argv);
145 static VALUE c_ev_raise (VALUE klass, VALUE argv)
149 rb_ary_push (event_args, argv);
151 t = rb_const_get (klass, rb_intern ("TYPE"));
152 ecore_event_add (FIX2INT (t), (void *) argv, free_event_args, NULL);
157 void Init_EventHandler (void)
161 cEventHandler = rb_define_class_under (mEcore, "EventHandler",
164 rb_define_alloc_func (cEventHandler, c_alloc);
165 rb_define_method (cEventHandler, "initialize", c_init, 1);
166 rb_define_method (cEventHandler, "delete", c_delete, 0);
168 event_classes = rb_hash_new ();
169 rb_global_variable (&event_classes);
171 event_args = rb_ary_new ();
172 rb_global_variable (&event_args);
174 /* define a base event class */
175 cEcoreEvent = rb_define_class_under (mEcore, "Event", rb_cObject);
176 rb_define_singleton_method (cEcoreEvent, "inherited", c_ev_inherited, 1);
177 rb_define_method (cEcoreEvent, "initialize", c_ev_init, -1);