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