Reworked on_ecore_event().
[ruby-ecore.git] / src / ecore / rb_event_handler.c
1 /*
2  * $Id: rb_event_handler.c 364 2006-02-14 19:06:43Z 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         VALUE callback;
33         bool deleted;
34 } RbEventHandler;
35
36 static int on_ecore_event (void *data, int type, void *event);
37
38 VALUE event_classes, cEcoreEvent;
39 static VALUE handlers;
40
41 static void c_mark (RbEventHandler *h)
42 {
43         rb_gc_mark (h->callback);
44 }
45
46 static void c_free (RbEventHandler *h)
47 {
48         RbEventHandler *h2 = NULL;
49         int len = RARRAY (handlers)->len, i;
50         VALUE el;
51
52         if (!h->real || h->deleted) {
53                 free (h);
54                 return;
55         }
56
57         for (i = 0; i < len; i++) {
58                 el = rb_ary_shift (handlers);
59
60                 Data_Get_Struct (el, RbEventHandler, h2);
61
62                 if (h == h2) {
63                         ecore_event_handler_del (h->real);
64                         break;
65                 }
66
67                 rb_ary_push (handlers, el);
68         }
69
70         free (h);
71 }
72
73 static VALUE c_alloc (VALUE klass)
74 {
75         RbEventHandler *h = NULL;
76
77         return Data_Make_Struct (klass, RbEventHandler, c_mark, c_free, h);
78 }
79
80 static VALUE c_init (VALUE self, VALUE type)
81 {
82         RbEventHandler *h = NULL;
83         int t;
84
85         if (!rb_block_given_p ())
86                 rb_raise (rb_eStandardError, "block missing");
87
88         Data_Get_Struct (self, RbEventHandler, h);
89
90         t = NUM2INT (type);
91
92         if (t <= ECORE_EVENT_NONE)
93                 rb_raise (rb_eStandardError, "invalid type");
94
95         rb_iv_set (self, "@type", type);
96
97         h->callback = rb_block_proc ();
98         h->deleted = false;
99         h->real = ecore_event_handler_add (t, on_ecore_event, NULL);
100
101         rb_ary_push (handlers, self);
102
103         return self;
104 }
105
106 static VALUE c_delete (VALUE self)
107 {
108         RbEventHandler *h = NULL;
109         int len = RARRAY (handlers)->len, i;
110         VALUE el;
111
112         for (i = 0; i < len; i++) {
113                 el = rb_ary_shift (handlers);
114                 if (el == self) {
115                         Data_Get_Struct (self, RbEventHandler, h);
116                         ecore_event_handler_del (h->real);
117                         h->real = NULL;
118                         h->deleted = true;
119
120                         break;
121                 }
122
123                 rb_ary_push (handlers, el);
124         }
125
126         return Qnil;
127 }
128
129 static int on_ecore_event (void *data, int type, void *event)
130 {
131         RbEventHandler *h = NULL;
132         VALUE handler, klass, obj, tmp, res;
133         int handler_type, len, ret = 1, i;
134
135         /* instantiate the event object
136          * first, find the class we're gonna use
137          */
138         klass = rb_hash_aref (event_classes, INT2NUM (type));
139         if (NIL_P (klass))
140                 rb_raise (rb_eException, "Cannot find event class "
141                                          "for event %i\n", type);
142
143         /* now create and init the object */
144         tmp = (VALUE) event;
145         obj = rb_class_new_instance (1, &tmp, klass);
146
147         len = RARRAY (handlers)->len;
148
149         for (i = 0; i < len; i++) {
150                 handler = rb_ary_entry (handlers, i);
151                 handler_type = NUM2INT (rb_iv_get (handler, "@type"));
152
153                 if (handler_type == type) {
154                         Data_Get_Struct (handler, RbEventHandler, h);
155                         res = rb_funcall (h->callback, rb_intern ("call"), 1, obj);
156
157                         /* if the block returned false, don't call the other
158                          * event handlers
159                          */
160                         if (res == Qfalse) {
161                                 ret = 0;
162                                 break;
163                         }
164                 }
165         }
166
167         return ret;
168 }
169
170 VALUE c_ev_generic_init (VALUE self, VALUE event)
171 {
172         /* dummy */
173         return self;
174 }
175
176 void Init_EventHandler (void)
177 {
178         VALUE cEventHandler;
179
180         cEventHandler = rb_define_class_under (mEcore, "EventHandler",
181                                                rb_cObject);
182
183         rb_define_alloc_func (cEventHandler, c_alloc);
184         rb_define_method (cEventHandler, "initialize", c_init, 1);
185         rb_define_method (cEventHandler, "delete", c_delete, 0);
186
187         handlers = rb_ary_new ();
188         rb_global_variable (&handlers);
189
190         event_classes = rb_hash_new ();
191         rb_global_variable (&event_classes);
192
193         /* define a base event class */
194         cEcoreEvent = rb_define_class_under (mEcore, "Event", rb_cObject);
195         rb_define_private_method (rb_singleton_class (cEcoreEvent),
196                                   "new", NULL, 0);
197 }