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