Fixed class instantiation.
[ruby-ecore.git] / src / ecore / rb_event_handler.c
1 /*
2  * $Id: rb_event_handler.c 343 2005-05-07 20:22:56Z 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 static int on_ecore_event (void *data, int type, void *event);
31
32 VALUE event_classes, cEcoreEvent;
33 static VALUE handlers;
34
35 static VALUE c_init (VALUE self, VALUE type)
36 {
37         int t;
38
39         if (!rb_block_given_p ())
40                 return Qnil;
41
42         t = NUM2INT (type);
43
44         if (t <= ECORE_EVENT_NONE)
45                 return Qnil;
46
47         rb_iv_set (self, "@type", type);
48         rb_iv_set (self, "handler", rb_block_proc ());
49
50         rb_ary_push (handlers, self);
51
52         ecore_event_handler_add (t, on_ecore_event, NULL);
53
54         return self;
55 }
56
57 static VALUE c_delete (VALUE self)
58 {
59         int len = RARRAY (handlers)->len, i;
60         VALUE el;
61
62         for (i = 0; i < len; i++) {
63                 el = rb_ary_shift (handlers);
64                 if (el == self)
65                         return Qnil;
66
67                 rb_ary_push (handlers, el);
68         }
69
70         return Qnil;
71 }
72
73 static int on_ecore_event (void *data, int type, void *event)
74 {
75         VALUE handler, klass, obj, argv[1], res;
76         int handler_type, len, i;
77         bool called = false;
78
79         /* instantiate the event object
80          * first, find the class we're gonna use
81          */
82         if (NIL_P (klass = rb_hash_aref (event_classes, INT2NUM (type)))) {
83                 rb_raise (rb_eException, "Cannot find event class "
84                                          "for event %i\n", type);
85                 return 0;
86         }
87
88         /* now create and init the object */
89         obj = rb_obj_alloc (klass);
90         argv[0] = (VALUE) event;
91         rb_obj_call_init (obj, 1, argv);
92
93         len = RARRAY (handlers)->len;
94
95         for (i = 0; i < len; i++) {
96                 handler = rb_ary_entry (handlers, i);
97                 handler_type = NUM2INT (rb_iv_get (handler, "@type"));
98
99                 if (handler_type == type) {
100                         res = rb_funcall (rb_iv_get (handler, "handler"),
101                                           rb_intern ("call"), 1, obj);
102                         called = true;
103
104                         /* if the block returned false, don't call the other
105                          * event handlers
106                          */
107                         if (res == Qfalse)
108                                 break;
109                 }
110         }
111
112         if (type == ECORE_EVENT_SIGNAL_EXIT && !called)
113                 ecore_main_loop_quit ();
114
115         /* call other event handlers, too */
116         return 1;
117 }
118
119 VALUE c_ev_generic_init (VALUE self, VALUE event)
120 {
121         /* dummy */
122         return self;
123 }
124
125 void Init_EventHandler (void)
126 {
127         VALUE cEventHandler;
128
129         cEventHandler = rb_define_class_under (mEcore, "EventHandler",
130                                                rb_cObject);
131
132         rb_define_method (cEventHandler, "initialize", c_init, 1);
133         rb_define_method (cEventHandler, "delete", c_delete, 0);
134
135         handlers = rb_ary_new ();
136         rb_global_variable (&handlers);
137
138         event_classes = rb_hash_new ();
139         rb_global_variable (&event_classes);
140
141         /* define a base event class */
142         cEcoreEvent = rb_define_class_under (mEcore, "Event", rb_cObject);
143         rb_define_private_method (rb_singleton_class (cEcoreEvent),
144                                   "new", NULL, 0);
145 }