Code cleanup.
[ruby-ecore.git] / src / ecore / rb_event_handler.c
1 /*
2  * $Id: rb_event_handler.c 359 2006-02-12 15:51:58Z 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, i;
134         bool called = false;
135
136         /* instantiate the event object
137          * first, find the class we're gonna use
138          */
139         klass = rb_hash_aref (event_classes, INT2NUM (type));
140         if (NIL_P (klass))
141                 rb_raise (rb_eException, "Cannot find event class "
142                                          "for event %i\n", type);
143
144         /* now create and init the object */
145         tmp = (VALUE) event;
146         obj = rb_class_new_instance (1, &tmp, klass);
147
148         len = RARRAY (handlers)->len;
149
150         for (i = 0; i < len; i++) {
151                 handler = rb_ary_entry (handlers, i);
152                 handler_type = NUM2INT (rb_iv_get (handler, "@type"));
153
154                 if (handler_type == type) {
155                         Data_Get_Struct (handler, RbEventHandler, h);
156                         res = rb_funcall (h->callback, rb_intern ("call"), 1, obj);
157                         called = true;
158
159                         /* if the block returned false, don't call the other
160                          * event handlers
161                          */
162                         if (res == Qfalse)
163                                 break;
164                 }
165         }
166
167         /* call other event handlers, too */
168         return 1;
169 }
170
171 VALUE c_ev_generic_init (VALUE self, VALUE event)
172 {
173         /* dummy */
174         return self;
175 }
176
177 void Init_EventHandler (void)
178 {
179         VALUE cEventHandler;
180
181         cEventHandler = rb_define_class_under (mEcore, "EventHandler",
182                                                rb_cObject);
183
184         rb_define_alloc_func (cEventHandler, c_alloc);
185         rb_define_method (cEventHandler, "initialize", c_init, 1);
186         rb_define_method (cEventHandler, "delete", c_delete, 0);
187
188         handlers = rb_ary_new ();
189         rb_global_variable (&handlers);
190
191         event_classes = rb_hash_new ();
192         rb_global_variable (&event_classes);
193
194         /* define a base event class */
195         cEcoreEvent = rb_define_class_under (mEcore, "Event", rb_cObject);
196         rb_define_private_method (rb_singleton_class (cEcoreEvent),
197                                   "new", NULL, 0);
198 }