Added Ecore.new_event_type and Ecore.add_event.
[ruby-ecore.git] / src / ecore / rb_ecore.c
1 /*
2  * $Id: rb_ecore.c 360 2006-02-12 15:53:44Z 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
23 #include <Ecore.h>
24
25 #define __RB_ECORE_C
26 #include "rb_ecore.h"
27 #include "rb_timer.h"
28 #include "rb_animator.h"
29 #include "rb_idler.h"
30 #include "rb_idle_enterer.h"
31 #include "rb_event_handler.h"
32 #include "rb_fd_handler.h"
33
34 VALUE mEcore;
35
36 /*
37  * call-seq:
38  *  Ecore.main_loop_begin
39  *
40  * Starts the Ecore main loop.
41  */
42 static VALUE m_main_loop_begin (VALUE self)
43 {
44         ecore_main_loop_begin ();
45
46         return Qnil;
47 }
48
49 /*
50  * call-seq:
51  *  Ecore.main_loop_iterate
52  *
53  * Run one iteration of the Ecore main loop.
54  */
55 static VALUE m_main_loop_iterate (VALUE self)
56 {
57         ecore_main_loop_iterate ();
58
59         return Qnil;
60 }
61
62 /*
63  * call-seq:
64  *  Ecore.main_loop_quit
65  *
66  * Stops the Ecore main loop.
67  */
68 static VALUE m_main_loop_quit (VALUE self)
69 {
70         ecore_main_loop_quit ();
71
72         return Qnil;
73 }
74
75 static VALUE m_time_get (VALUE self)
76 {
77         return rb_float_new (ecore_time_get ());
78 }
79
80 static VALUE m_new_event_type (VALUE self, VALUE klass)
81 {
82         VALUE num;
83
84         num = INT2FIX (ecore_event_type_new());
85         rb_hash_aset (event_classes, num, klass);
86
87         return num;
88 }
89
90 static void free_ruby_event (void *data, void *event)
91 {
92         /* do nothing */
93 }
94
95 static VALUE m_add_event (VALUE self, VALUE type, VALUE event)
96 {
97         Check_Type (type, T_FIXNUM);
98
99         ecore_event_add (FIX2INT (type), (void *) event, free_ruby_event, NULL);
100
101         return Qnil;
102 }
103
104 #if 0
105 static VALUE c_ev_exe_exit_init (VALUE self, VALUE event)
106 {
107         VALUE c = CLASS_OF (self);
108         Ecore_Event_Exe_Exit *e = (void *) event;
109
110         rb_define_attr (c, "pid", 1, 0);
111         rb_define_attr (c, "exit_code", 1, 0);
112         rb_define_attr (c, "exit_signal", 1, 0);
113         rb_define_attr (c, "exited", 1, 0);
114         rb_define_attr (c, "signalled", 1, 0);
115
116         rb_iv_set (self, "@pid", INT2FIX (e->pid));
117         rb_iv_set (self, "@exit_code", INT2FIX (e->exit_code));
118         rb_iv_set (self, "@exit_signal", INT2FIX (e->exit_signal));
119         rb_iv_set (self, "@exited", e->exited ? Qtrue : Qfalse);
120         rb_iv_set (self, "@signalled", e->signalled ? Qtrue : Qfalse);
121
122         return self;
123 }
124 #endif
125
126 static VALUE c_ev_sig_user_init (VALUE self, VALUE event)
127 {
128         Ecore_Event_Signal_User *e = (void *) event;
129
130         rb_define_attr (CLASS_OF (self), "number", 1, 0);
131
132         rb_iv_set (self, "@number", INT2FIX (e->number));
133
134         return self;
135 }
136
137 static VALUE c_ev_sig_exit_init (VALUE self, VALUE event)
138 {
139         VALUE c = CLASS_OF (self);
140         Ecore_Event_Signal_Exit *e = (void *) event;
141
142         rb_define_attr (c, "interrupt", 1, 0);
143         rb_define_attr (c, "quit", 1, 0);
144         rb_define_attr (c, "terminate", 1, 0);
145
146         rb_iv_set (self, "@interrupt", e->interrupt ? Qtrue : Qfalse);
147         rb_iv_set (self, "@quit", e->quit ? Qtrue : Qfalse);
148         rb_iv_set (self, "@terminate", e->terminate ? Qtrue : Qfalse);
149
150         return self;
151 }
152
153 static VALUE c_ev_sig_rt_init (VALUE self, VALUE event)
154 {
155         Ecore_Event_Signal_Realtime *e = (void *) event;
156
157         rb_define_attr (CLASS_OF (self), "number", 1, 0);
158
159         rb_iv_set (self, "@number", INT2FIX (e->num));
160
161         return self;
162 }
163
164 static void at_exit ()
165 {
166         ecore_shutdown ();
167 }
168
169 void Init_ecore (void)
170 {
171         VALUE c;
172
173         ecore_init ();
174
175         atexit (at_exit);
176
177         mEcore = rb_define_module ("Ecore");
178
179         rb_define_module_function (mEcore, "main_loop_begin",
180                                    m_main_loop_begin, 0);
181         rb_define_module_function (mEcore, "main_loop_iterate",
182                                    m_main_loop_iterate, 0);
183         rb_define_module_function (mEcore, "main_loop_quit",
184                                    m_main_loop_quit, 0);
185         rb_define_module_function (mEcore, "time", m_time_get, 0);
186         rb_define_module_function (mEcore, "new_event_type",
187                                    m_new_event_type, 1);
188         rb_define_module_function (mEcore, "add_event", m_add_event, 2);
189
190         Init_Timer ();
191         Init_Animator ();
192         Init_Idler ();
193         Init_IdleEnterer ();
194         Init_EventHandler ();
195         Init_FdHandler ();
196
197         /* SIGNAL_HUP */
198         ADD_EVENT (mEcore, ECORE_EVENT_, SIGNAL_HUP,
199                    "SignalHup", c);
200         rb_define_private_method (c, "initialize", c_ev_generic_init, 1);
201
202         /* SIGNAL_POWER */
203         ADD_EVENT (mEcore, ECORE_EVENT_, SIGNAL_POWER,
204                    "SignalPower", c);
205         rb_define_private_method (c, "initialize", c_ev_generic_init, 1);
206
207 #if 0
208         /* EXE_EXIT */
209         ADD_EVENT (mEcore, ECORE_EVENT_, EXE_EXIT, "ExeExit", c);
210         rb_define_private_method (c, "initialize", c_ev_exe_exit_init, 1);
211 #endif
212
213         /* SIGNAL_USER */
214         ADD_EVENT (mEcore, ECORE_EVENT_, SIGNAL_USER,
215                    "SignalUser", c);
216         rb_define_private_method (c, "initialize", c_ev_sig_user_init, 1);
217
218         /* SIGNAL_EXIT */
219         ADD_EVENT (mEcore, ECORE_EVENT_, SIGNAL_EXIT,
220                    "SignalExit", c);
221         rb_define_private_method (c, "initialize", c_ev_sig_exit_init, 1);
222
223         /* SIGNAL_REALTIME */
224         ADD_EVENT (mEcore, ECORE_EVENT_, SIGNAL_REALTIME,
225                    "SignalRealtime", c);
226         rb_define_private_method (c, "initialize", c_ev_sig_rt_init, 1);
227 }
228