Reworked event handling.
[ruby-ecore.git] / src / ecore / rb_event_handler.c
index a360ef70cac34cb42447fcb5f831adb6930ac68a..5dabfe73fb939c77e08ade7d784740de923a6bd2 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * $Id: rb_event_handler.c 364 2006-02-14 19:06:43Z tilman $
+ * $Id: rb_event_handler.c 365 2006-02-14 21:50:47Z tilman $
  *
  * Copyright (C) 2004 ruby-ecore team (see AUTHORS)
  *
 
 typedef struct {
        Ecore_Event_Handler *real;
+       int type;
        VALUE callback;
        bool deleted;
 } RbEventHandler;
 
 static int on_ecore_event (void *data, int type, void *event);
+static VALUE c_ev_raise (VALUE klass, VALUE event);
 
 VALUE event_classes, cEcoreEvent;
 static VALUE handlers;
@@ -87,13 +89,14 @@ static VALUE c_init (VALUE self, VALUE type)
 
        Data_Get_Struct (self, RbEventHandler, h);
 
-       t = NUM2INT (type);
+       if (rb_obj_is_kind_of (type, rb_cModule) != Qtrue)
+               rb_raise (rb_eArgError, "invalid argument");
 
+       t = NUM2INT (rb_const_get (type, rb_intern ("TYPE")));
        if (t <= ECORE_EVENT_NONE)
                rb_raise (rb_eStandardError, "invalid type");
 
-       rb_iv_set (self, "@type", type);
-
+       h->type = t;
        h->callback = rb_block_proc ();
        h->deleted = false;
        h->real = ecore_event_handler_add (t, on_ecore_event, NULL);
@@ -130,7 +133,7 @@ static int on_ecore_event (void *data, int type, void *event)
 {
        RbEventHandler *h = NULL;
        VALUE handler, klass, obj, tmp, res;
-       int handler_type, len, ret = 1, i;
+       int len, ret = 1, i;
 
        /* instantiate the event object
         * first, find the class we're gonna use
@@ -148,10 +151,9 @@ static int on_ecore_event (void *data, int type, void *event)
 
        for (i = 0; i < len; i++) {
                handler = rb_ary_entry (handlers, i);
-               handler_type = NUM2INT (rb_iv_get (handler, "@type"));
+               Data_Get_Struct (handler, RbEventHandler, h);
 
-               if (handler_type == type) {
-                       Data_Get_Struct (handler, RbEventHandler, h);
+               if (h->type == type) {
                        res = rb_funcall (h->callback, rb_intern ("call"), 1, obj);
 
                        /* if the block returned false, don't call the other
@@ -167,10 +169,37 @@ static int on_ecore_event (void *data, int type, void *event)
        return ret;
 }
 
-VALUE c_ev_generic_init (VALUE self, VALUE event)
+VALUE c_ev_inherited (VALUE klass, VALUE child)
 {
-       /* dummy */
-       return self;
+       VALUE t;
+
+       t = INT2FIX (ecore_event_type_new ());
+       rb_hash_aset (event_classes, t, child);
+
+       rb_define_const (child, "TYPE", t);
+       rb_define_singleton_method (child, "raise", c_ev_raise, 1);
+
+       return Qnil;
+}
+
+VALUE c_ev_inherited_noop (VALUE klass, VALUE child)
+{
+       return Qnil;
+}
+
+static void free_ruby_event (void *data, void *event)
+{
+       /* do nothing */
+}
+
+static VALUE c_ev_raise (VALUE klass, VALUE event)
+{
+       VALUE t;
+
+       t = rb_const_get (klass, rb_intern ("TYPE"));
+       ecore_event_add (FIX2INT (t), (void *) event, free_ruby_event, NULL);
+
+       return Qnil;
 }
 
 void Init_EventHandler (void)
@@ -192,6 +221,5 @@ void Init_EventHandler (void)
 
        /* 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);
+       rb_define_singleton_method (cEcoreEvent, "inherited", c_ev_inherited, 1);
 }