Removed RCS-style IDs.
[ruby-ecore.git] / src / ecore / rb_event_handler.c
1 /*
2  * Copyright (C) 2004 ruby-ecore team (see AUTHORS)
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
17  */
18
19 #include <ruby.h>
20 #include <stdbool.h>
21
22 #include <Ecore.h>
23
24 #define __RB_EVENT_HANDLER_C
25 #include "rb_ecore.h"
26 #include "rb_event_handler.h"
27
28 typedef struct {
29         Ecore_Event_Handler *real;
30         VALUE event_class;
31         VALUE callback;
32         bool deleted;
33 } RbEventHandler;
34
35 static int on_ecore_event (void *data, int type, void *event);
36 static VALUE c_ev_raise (VALUE klass, VALUE argv);
37
38 VALUE event_classes, cEcoreEvent;
39 static VALUE event_args;
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         if (h->real && !h->deleted)
49                 ecore_event_handler_del (h->real);
50
51         free (h);
52 }
53
54 static VALUE c_alloc (VALUE klass)
55 {
56         RbEventHandler *h = NULL;
57
58         return Data_Make_Struct (klass, RbEventHandler, c_mark, c_free, h);
59 }
60
61 static VALUE c_init (VALUE self, VALUE klass)
62 {
63         RbEventHandler *h = NULL;
64         int t;
65
66         if (!rb_block_given_p ())
67                 rb_raise (rb_eStandardError, "block missing");
68
69         Data_Get_Struct (self, RbEventHandler, h);
70
71         if (rb_obj_is_kind_of (klass, rb_cModule) != Qtrue)
72                 rb_raise (rb_eArgError, "invalid argument");
73
74         t = NUM2INT (rb_const_get (klass, rb_intern ("TYPE")));
75         if (t <= ECORE_EVENT_NONE)
76                 rb_raise (rb_eStandardError, "invalid event");
77
78         h->event_class = klass;
79         h->callback = rb_block_proc ();
80         h->deleted = false;
81         h->real = ecore_event_handler_add (t, on_ecore_event, h);
82
83         return self;
84 }
85
86 static VALUE c_delete (VALUE self)
87 {
88         RbEventHandler *h = NULL;
89
90         Data_Get_Struct (self, RbEventHandler, h);
91
92         if (h->real && !h->deleted) {
93                 ecore_event_handler_del (h->real);
94                 h->real = NULL;
95                 h->deleted = true;
96         } else
97                 rb_raise (rb_eException, "EventHandler already deleted!");
98
99         return Qnil;
100 }
101
102 static int on_ecore_event (void *data, int type, void *event)
103 {
104         RbEventHandler *h = data;
105         VALUE obj, tmp = (VALUE) event;
106
107         obj = rb_obj_alloc (h->event_class);
108
109         if (rb_respond_to (h->event_class, rb_intern ("raise")))
110                 rb_apply (obj, rb_intern ("initialize"), tmp);
111         else
112                 rb_obj_call_init (obj, 1, &tmp);
113
114         tmp = rb_funcall (h->callback, rb_intern ("call"), 1, obj);
115
116         /* if the block returned false, don't call the other
117          * event handlers
118          */
119         return (tmp != Qfalse);
120 }
121
122 VALUE c_ev_inherited (VALUE klass, VALUE child)
123 {
124         VALUE t;
125
126         t = INT2FIX (ecore_event_type_new ());
127         rb_hash_aset (event_classes, t, child);
128
129         rb_define_const (child, "TYPE", t);
130         rb_define_singleton_method (child, "raise", c_ev_raise, -2);
131
132         return Qnil;
133 }
134
135 static VALUE c_ev_init (int argc, VALUE *argv, VALUE self)
136 {
137         return self;
138 }
139
140 static void free_event_args (void *data, void *argv)
141 {
142         rb_ary_delete (event_args, (VALUE) argv);
143 }
144
145 static VALUE c_ev_raise (VALUE klass, VALUE argv)
146 {
147         VALUE t;
148
149         rb_ary_push (event_args, argv);
150
151         t = rb_const_get (klass, rb_intern ("TYPE"));
152         ecore_event_add (FIX2INT (t), (void *) argv, free_event_args, NULL);
153
154         return Qnil;
155 }
156
157 void Init_EventHandler (void)
158 {
159         VALUE cEventHandler;
160
161         cEventHandler = rb_define_class_under (mEcore, "EventHandler",
162                                                rb_cObject);
163
164         rb_define_alloc_func (cEventHandler, c_alloc);
165         rb_define_method (cEventHandler, "initialize", c_init, 1);
166         rb_define_method (cEventHandler, "delete", c_delete, 0);
167
168         event_classes = rb_hash_new ();
169         rb_global_variable (&event_classes);
170
171         event_args = rb_ary_new ();
172         rb_global_variable (&event_args);
173
174         /* define a base event class */
175         cEcoreEvent = rb_define_class_under (mEcore, "Event", rb_cObject);
176         rb_define_singleton_method (cEcoreEvent, "inherited", c_ev_inherited, 1);
177         rb_define_method (cEcoreEvent, "initialize", c_ev_init, -1);
178 }