Added Ecore.new_event_type and Ecore.add_event.
[ruby-ecore.git] / src / ecore / rb_ecore.c
index 7cd8641ccd6456e2fe0f63d0a9f732ac3e20ac28..239da0f89518826932c508c86cec865585c47143 100644 (file)
@@ -1,7 +1,7 @@
 /*
- * $Id: rb_ecore.c 27 2004-07-08 18:25:05Z tilman $
+ * $Id: rb_ecore.c 360 2006-02-12 15:53:44Z tilman $
  *
- * Copyright (C) 2004 Tilman Sauerbeck (tilman at code-monkey de)
+ * Copyright (C) 2004 ruby-ecore team (see AUTHORS)
  *
  * This library is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Lesser General Public
 
 #include <Ecore.h>
 
+#define __RB_ECORE_C
 #include "rb_ecore.h"
 #include "rb_timer.h"
+#include "rb_animator.h"
 #include "rb_idler.h"
+#include "rb_idle_enterer.h"
+#include "rb_event_handler.h"
+#include "rb_fd_handler.h"
 
-#ifdef DEBUG
-static VALUE m_init (VALUE self)
-{
-       return INT2FIX (ecore_init ());
-}
-
-static VALUE m_shutdown (VALUE self)
-{
-       return INT2FIX (ecore_shutdown ());
-}
-#endif
+VALUE mEcore;
 
+/*
+ * call-seq:
+ *  Ecore.main_loop_begin
+ *
+ * Starts the Ecore main loop.
+ */
 static VALUE m_main_loop_begin (VALUE self)
 {
        ecore_main_loop_begin ();
@@ -45,6 +46,12 @@ static VALUE m_main_loop_begin (VALUE self)
        return Qnil;
 }
 
+/*
+ * call-seq:
+ *  Ecore.main_loop_iterate
+ *
+ * Run one iteration of the Ecore main loop.
+ */
 static VALUE m_main_loop_iterate (VALUE self)
 {
        ecore_main_loop_iterate ();
@@ -52,6 +59,12 @@ static VALUE m_main_loop_iterate (VALUE self)
        return Qnil;
 }
 
+/*
+ * call-seq:
+ *  Ecore.main_loop_quit
+ *
+ * Stops the Ecore main loop.
+ */
 static VALUE m_main_loop_quit (VALUE self)
 {
        ecore_main_loop_quit ();
@@ -59,25 +72,157 @@ static VALUE m_main_loop_quit (VALUE self)
        return Qnil;
 }
 
-void Init_ecore (void)
+static VALUE m_time_get (VALUE self)
 {
-       mEcore = rb_define_module ("Ecore");
+       return rb_float_new (ecore_time_get ());
+}
+
+static VALUE m_new_event_type (VALUE self, VALUE klass)
+{
+       VALUE num;
+
+       num = INT2FIX (ecore_event_type_new());
+       rb_hash_aset (event_classes, num, klass);
+
+       return num;
+}
+
+static void free_ruby_event (void *data, void *event)
+{
+       /* do nothing */
+}
+
+static VALUE m_add_event (VALUE self, VALUE type, VALUE event)
+{
+       Check_Type (type, T_FIXNUM);
+
+       ecore_event_add (FIX2INT (type), (void *) event, free_ruby_event, NULL);
 
-#ifdef DEBUG
-       rb_define_module_function (mEcore, "init",
-                                  m_init, 0);
-       rb_define_module_function (mEcore, "shutdown",
-                                  m_shutdown, 0);
+       return Qnil;
+}
+
+#if 0
+static VALUE c_ev_exe_exit_init (VALUE self, VALUE event)
+{
+       VALUE c = CLASS_OF (self);
+       Ecore_Event_Exe_Exit *e = (void *) event;
+
+       rb_define_attr (c, "pid", 1, 0);
+       rb_define_attr (c, "exit_code", 1, 0);
+       rb_define_attr (c, "exit_signal", 1, 0);
+       rb_define_attr (c, "exited", 1, 0);
+       rb_define_attr (c, "signalled", 1, 0);
+
+       rb_iv_set (self, "@pid", INT2FIX (e->pid));
+       rb_iv_set (self, "@exit_code", INT2FIX (e->exit_code));
+       rb_iv_set (self, "@exit_signal", INT2FIX (e->exit_signal));
+       rb_iv_set (self, "@exited", e->exited ? Qtrue : Qfalse);
+       rb_iv_set (self, "@signalled", e->signalled ? Qtrue : Qfalse);
+
+       return self;
+}
 #endif
 
+static VALUE c_ev_sig_user_init (VALUE self, VALUE event)
+{
+       Ecore_Event_Signal_User *e = (void *) event;
+
+       rb_define_attr (CLASS_OF (self), "number", 1, 0);
+
+       rb_iv_set (self, "@number", INT2FIX (e->number));
+
+       return self;
+}
+
+static VALUE c_ev_sig_exit_init (VALUE self, VALUE event)
+{
+       VALUE c = CLASS_OF (self);
+       Ecore_Event_Signal_Exit *e = (void *) event;
+
+       rb_define_attr (c, "interrupt", 1, 0);
+       rb_define_attr (c, "quit", 1, 0);
+       rb_define_attr (c, "terminate", 1, 0);
+
+       rb_iv_set (self, "@interrupt", e->interrupt ? Qtrue : Qfalse);
+       rb_iv_set (self, "@quit", e->quit ? Qtrue : Qfalse);
+       rb_iv_set (self, "@terminate", e->terminate ? Qtrue : Qfalse);
+
+       return self;
+}
+
+static VALUE c_ev_sig_rt_init (VALUE self, VALUE event)
+{
+       Ecore_Event_Signal_Realtime *e = (void *) event;
+
+       rb_define_attr (CLASS_OF (self), "number", 1, 0);
+
+       rb_iv_set (self, "@number", INT2FIX (e->num));
+
+       return self;
+}
+
+static void at_exit ()
+{
+       ecore_shutdown ();
+}
+
+void Init_ecore (void)
+{
+       VALUE c;
+
+       ecore_init ();
+
+       atexit (at_exit);
+
+       mEcore = rb_define_module ("Ecore");
+
        rb_define_module_function (mEcore, "main_loop_begin",
                                   m_main_loop_begin, 0);
        rb_define_module_function (mEcore, "main_loop_iterate",
                                   m_main_loop_iterate, 0);
        rb_define_module_function (mEcore, "main_loop_quit",
                                   m_main_loop_quit, 0);
+       rb_define_module_function (mEcore, "time", m_time_get, 0);
+       rb_define_module_function (mEcore, "new_event_type",
+                                  m_new_event_type, 1);
+       rb_define_module_function (mEcore, "add_event", m_add_event, 2);
 
        Init_Timer ();
+       Init_Animator ();
        Init_Idler ();
+       Init_IdleEnterer ();
+       Init_EventHandler ();
+       Init_FdHandler ();
+
+       /* SIGNAL_HUP */
+       ADD_EVENT (mEcore, ECORE_EVENT_, SIGNAL_HUP,
+                  "SignalHup", c);
+       rb_define_private_method (c, "initialize", c_ev_generic_init, 1);
+
+       /* SIGNAL_POWER */
+       ADD_EVENT (mEcore, ECORE_EVENT_, SIGNAL_POWER,
+                  "SignalPower", c);
+       rb_define_private_method (c, "initialize", c_ev_generic_init, 1);
+
+#if 0
+       /* EXE_EXIT */
+       ADD_EVENT (mEcore, ECORE_EVENT_, EXE_EXIT, "ExeExit", c);
+       rb_define_private_method (c, "initialize", c_ev_exe_exit_init, 1);
+#endif
+
+       /* SIGNAL_USER */
+       ADD_EVENT (mEcore, ECORE_EVENT_, SIGNAL_USER,
+                  "SignalUser", c);
+       rb_define_private_method (c, "initialize", c_ev_sig_user_init, 1);
+
+       /* SIGNAL_EXIT */
+       ADD_EVENT (mEcore, ECORE_EVENT_, SIGNAL_EXIT,
+                  "SignalExit", c);
+       rb_define_private_method (c, "initialize", c_ev_sig_exit_init, 1);
+
+       /* SIGNAL_REALTIME */
+       ADD_EVENT (mEcore, ECORE_EVENT_, SIGNAL_REALTIME,
+                  "SignalRealtime", c);
+       rb_define_private_method (c, "initialize", c_ev_sig_rt_init, 1);
 }