Reworked event handling.
[ruby-ecore.git] / src / ecore / rb_event_handler.c
1 /*
2  * $Id: rb_event_handler.c 365 2006-02-14 21:50:47Z 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 event);
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         obj = rb_class_new_instance (1, &tmp, klass);
149
150         len = RARRAY (handlers)->len;
151
152         for (i = 0; i < len; i++) {
153                 handler = rb_ary_entry (handlers, i);
154                 Data_Get_Struct (handler, RbEventHandler, h);
155
156                 if (h->type == type) {
157                         res = rb_funcall (h->callback, rb_intern ("call"), 1, obj);
158
159                         /* if the block returned false, don't call the other
160                          * event handlers
161                          */
162                         if (res == Qfalse) {
163                                 ret = 0;
164                                 break;
165                         }
166                 }
167         }
168
169         return ret;
170 }
171
172 VALUE c_ev_inherited (VALUE klass, VALUE child)
173 {
174         VALUE t;
175
176         t = INT2FIX (ecore_event_type_new ());
177         rb_hash_aset (event_classes, t, child);
178
179         rb_define_const (child, "TYPE", t);
180         rb_define_singleton_method (child, "raise", c_ev_raise, 1);
181
182         return Qnil;
183 }
184
185 VALUE c_ev_inherited_noop (VALUE klass, VALUE child)
186 {
187         return Qnil;
188 }
189
190 static void free_ruby_event (void *data, void *event)
191 {
192         /* do nothing */
193 }
194
195 static VALUE c_ev_raise (VALUE klass, VALUE event)
196 {
197         VALUE t;
198
199         t = rb_const_get (klass, rb_intern ("TYPE"));
200         ecore_event_add (FIX2INT (t), (void *) event, free_ruby_event, NULL);
201
202         return Qnil;
203 }
204
205 void Init_EventHandler (void)
206 {
207         VALUE cEventHandler;
208
209         cEventHandler = rb_define_class_under (mEcore, "EventHandler",
210                                                rb_cObject);
211
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);
215
216         handlers = rb_ary_new ();
217         rb_global_variable (&handlers);
218
219         event_classes = rb_hash_new ();
220         rb_global_variable (&event_classes);
221
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);
225 }