Updated copyright notice. Added event handler code.
[ruby-ecore.git] / src / ecore / rb_event_handler.c
diff --git a/src/ecore/rb_event_handler.c b/src/ecore/rb_event_handler.c
new file mode 100644 (file)
index 0000000..63c5016
--- /dev/null
@@ -0,0 +1,140 @@
+/*
+ * $Id: rb_event_handler.c 77 2004-08-19 17:39:29Z tilman $
+ *
+ * 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
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ */
+
+#include <ruby.h>
+#include <stdbool.h>
+
+#include <Ecore.h>
+
+#define __RB_EVENT_HANDLER_C
+#include "rb_ecore.h"
+#include "rb_event_handler.h"
+
+VALUE event_classes, cEcoreEvent;
+static VALUE handlers;
+
+static VALUE c_init (VALUE self, VALUE type)
+{
+       int t;
+
+       if (!rb_block_given_p ())
+               return Qnil;
+
+       t = NUM2INT (type);
+
+       if (t <= ECORE_EVENT_NONE)
+               return Qnil;
+
+       rb_iv_set (self, "@type", type);
+       rb_iv_set (self, "handler", rb_block_proc ());
+
+       rb_ary_push (handlers, self);
+
+       return self;
+}
+
+static VALUE c_delete (VALUE self)
+{
+       int len = RARRAY (handlers)->len, i;
+       VALUE el;
+
+       for (i = 0; i < len; i++) {
+               el = rb_ary_shift (handlers);
+               if (el == self)
+                       return Qnil;
+
+               rb_ary_push (handlers, el);
+       }
+
+       return Qnil;
+}
+
+int on_ecore_event (void *data, int type, void *event)
+{
+       VALUE handler, klass, obj, argv[1], res;
+       int handler_type, len, i;
+       bool called = false;
+
+       /* instantiate the event object
+        * first, find the class we're gonna use
+        */
+       if (NIL_P (klass = rb_hash_aref (event_classes, INT2NUM (type)))) {
+               rb_raise (rb_eException, "Cannot find event class "
+                                        "for event %i\n", type);
+               return 0;
+       }
+
+       /* now create and init the object */
+       obj = rb_obj_alloc (klass);
+       argv[0] = (VALUE) event;
+       rb_obj_call_init (obj, 1, argv);
+
+       len = RARRAY (handlers)->len;
+
+       for (i = 0; i < len; i++) {
+               handler = rb_ary_entry (handlers, i);
+               handler_type = NUM2INT (rb_iv_get (handler, "@type"));
+
+               if (handler_type == type) {
+                       res = rb_funcall (rb_iv_get (handler, "handler"),
+                                         rb_intern ("call"), 1, obj);
+                       called = true;
+
+                       /* if the block returned false, don't call the other
+                        * event handlers
+                        */
+                       if (res == Qfalse)
+                               break;
+               }
+       }
+
+       if (type == ECORE_EVENT_SIGNAL_EXIT && !called)
+               ecore_main_loop_quit ();
+
+       return 0;
+}
+
+VALUE c_ev_generic_init (VALUE self, VALUE event)
+{
+       /* dummy */
+       return self;
+}
+
+void Init_EventHandler (void)
+{
+       VALUE cEventHandler;
+
+       cEventHandler = rb_define_class_under (mEcore, "EventHandler",
+                                              rb_cObject);
+
+       rb_define_method (cEventHandler, "initialize", c_init, 1);
+       rb_define_method (cEventHandler, "delete", c_delete, 0);
+
+       handlers = rb_ary_new ();
+       rb_global_variable (&handlers);
+
+       event_classes = rb_hash_new ();
+       rb_global_variable (&event_classes);
+
+       /* define a base event class */
+       cEcoreEvent = rb_define_class_under (mEcore, "Event", rb_cObject);
+       rb_define_private_method (rb_singleton_class (cEcoreEvent),
+                                 "new", NULL, 0);
+}