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