Reworked ADD_EVENT() wrt handling of the inherited method.
[ruby-ecore.git] / src / ecore / rb_event_handler.c
1 /*
2  * $Id: rb_event_handler.c 372 2006-02-16 20:39:55Z tilman $
3  *
4  * Copyright (C) 2004 ruby-ecore team (see AUTHORS)
5  *
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.
10  *
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.
15  *
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
19  */
20
21 #include <ruby.h>
22 #include <stdbool.h>
23
24 #include <Ecore.h>
25
26 #define __RB_EVENT_HANDLER_C
27 #include "rb_ecore.h"
28 #include "rb_event_handler.h"
29
30 typedef struct {
31         Ecore_Event_Handler *real;
32         int type;
33         VALUE callback;
34         bool deleted;
35 } RbEventHandler;
36
37 static int on_ecore_event (void *data, int type, void *event);
38 static VALUE c_ev_raise (VALUE klass, VALUE argv);
39
40 VALUE event_classes, cEcoreEvent;
41 static VALUE handlers;
42
43 static void c_mark (RbEventHandler *h)
44 {
45         rb_gc_mark (h->callback);
46 }
47
48 static void c_free (RbEventHandler *h)
49 {
50         RbEventHandler *h2 = NULL;
51         int len = RARRAY (handlers)->len, i;
52         VALUE el;
53
54         if (!h->real || h->deleted) {
55                 free (h);
56                 return;
57         }
58
59         for (i = 0; i < len; i++) {
60                 el = rb_ary_shift (handlers);
61
62                 Data_Get_Struct (el, RbEventHandler, h2);
63
64                 if (h == h2) {
65                         ecore_event_handler_del (h->real);
66                         break;
67                 }
68
69                 rb_ary_push (handlers, el);
70         }
71
72         free (h);
73 }
74
75 static VALUE c_alloc (VALUE klass)
76 {
77         RbEventHandler *h = NULL;
78
79         return Data_Make_Struct (klass, RbEventHandler, c_mark, c_free, h);
80 }
81
82 static VALUE c_init (VALUE self, VALUE type)
83 {
84         RbEventHandler *h = NULL;
85         int t;
86
87         if (!rb_block_given_p ())
88                 rb_raise (rb_eStandardError, "block missing");
89
90         Data_Get_Struct (self, RbEventHandler, h);
91
92         if (rb_obj_is_kind_of (type, rb_cModule) != Qtrue)
93                 rb_raise (rb_eArgError, "invalid argument");
94
95         t = NUM2INT (rb_const_get (type, rb_intern ("TYPE")));
96         if (t <= ECORE_EVENT_NONE)
97                 rb_raise (rb_eStandardError, "invalid type");
98
99         h->type = t;
100         h->callback = rb_block_proc ();
101         h->deleted = false;
102         h->real = ecore_event_handler_add (t, on_ecore_event, NULL);
103
104         rb_ary_push (handlers, self);
105
106         return self;
107 }
108
109 static VALUE c_delete (VALUE self)
110 {
111         RbEventHandler *h = NULL;
112         int len = RARRAY (handlers)->len, i;
113         VALUE el;
114
115         for (i = 0; i < len; i++) {
116                 el = rb_ary_shift (handlers);
117                 if (el == self) {
118                         Data_Get_Struct (self, RbEventHandler, h);
119                         ecore_event_handler_del (h->real);
120                         h->real = NULL;
121                         h->deleted = true;
122
123                         break;
124                 }
125
126                 rb_ary_push (handlers, el);
127         }
128
129         return Qnil;
130 }
131
132 static int on_ecore_event (void *data, int type, void *event)
133 {
134         RbEventHandler *h = NULL;
135         VALUE handler, klass, obj, tmp, res;
136         int len, ret = 1, i;
137
138         /* instantiate the event object
139          * first, find the class we're gonna use
140          */
141         klass = rb_hash_aref (event_classes, INT2NUM (type));
142         if (NIL_P (klass))
143                 rb_raise (rb_eException, "Cannot find event class "
144                                          "for event %i\n", type);
145
146         /* now create and init the object */
147         tmp = (VALUE) event;
148
149         /* if tmp is a Ruby class, we'll just pass the arguments to the
150          * initialize method.
151          * if it's a c struct, we can use rb_class_new_instance()
152          */
153         if (rb_respond_to (klass, rb_intern ("raise"))) {
154                 obj = rb_obj_alloc (klass);
155                 rb_apply (obj, rb_intern ("initialize"), tmp);
156         } else
157                 obj = rb_class_new_instance (1, &tmp, klass);
158
159         len = RARRAY (handlers)->len;
160
161         for (i = 0; i < len; i++) {
162                 handler = rb_ary_entry (handlers, i);
163                 Data_Get_Struct (handler, RbEventHandler, h);
164
165                 if (h->type == type) {
166                         res = rb_funcall (h->callback, rb_intern ("call"), 1, obj);
167
168                         /* if the block returned false, don't call the other
169                          * event handlers
170                          */
171                         if (res == Qfalse) {
172                                 ret = 0;
173                                 break;
174                         }
175                 }
176         }
177
178         return ret;
179 }
180
181 VALUE c_ev_inherited (VALUE klass, VALUE child)
182 {
183         VALUE t;
184
185         t = INT2FIX (ecore_event_type_new ());
186         rb_hash_aset (event_classes, t, child);
187
188         rb_define_const (child, "TYPE", t);
189         rb_define_singleton_method (child, "raise", c_ev_raise, -2);
190
191         return Qnil;
192 }
193
194 static VALUE c_ev_init (int argc, VALUE *argv, VALUE self)
195 {
196         return self;
197 }
198
199 static void free_ruby_event (void *data, void *event)
200 {
201         /* do nothing */
202 }
203
204 static VALUE c_ev_raise (VALUE klass, VALUE argv)
205 {
206         VALUE t;
207
208         t = rb_const_get (klass, rb_intern ("TYPE"));
209         ecore_event_add (FIX2INT (t), (void *) argv, free_ruby_event, NULL);
210
211         return Qnil;
212 }
213
214 void Init_EventHandler (void)
215 {
216         VALUE cEventHandler;
217
218         cEventHandler = rb_define_class_under (mEcore, "EventHandler",
219                                                rb_cObject);
220
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);
224
225         handlers = rb_ary_new ();
226         rb_global_variable (&handlers);
227
228         event_classes = rb_hash_new ();
229         rb_global_variable (&event_classes);
230
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);
235 }