From: Tilman Sauerbeck Date: Thu, 16 Feb 2006 21:37:43 +0000 (+0000) Subject: Event system tweaks. X-Git-Url: http://git.code-monkey.de/?p=ruby-ecore.git;a=commitdiff_plain;h=5466e8806f4496694a95033f548763ca771d5045 Event system tweaks. --- diff --git a/src/ecore/rb_event_handler.c b/src/ecore/rb_event_handler.c index 18e2fc7..39e2521 100644 --- a/src/ecore/rb_event_handler.c +++ b/src/ecore/rb_event_handler.c @@ -1,5 +1,5 @@ /* - * $Id: rb_event_handler.c 372 2006-02-16 20:39:55Z tilman $ + * $Id: rb_event_handler.c 373 2006-02-16 21:37:43Z tilman $ * * Copyright (C) 2004 ruby-ecore team (see AUTHORS) * @@ -29,7 +29,7 @@ typedef struct { Ecore_Event_Handler *real; - int type; + VALUE event_class; VALUE callback; bool deleted; } RbEventHandler; @@ -38,7 +38,7 @@ static int on_ecore_event (void *data, int type, void *event); static VALUE c_ev_raise (VALUE klass, VALUE argv); VALUE event_classes, cEcoreEvent; -static VALUE handlers; +static VALUE event_args; static void c_mark (RbEventHandler *h) { @@ -47,27 +47,8 @@ static void c_mark (RbEventHandler *h) static void c_free (RbEventHandler *h) { - RbEventHandler *h2 = NULL; - int len = RARRAY (handlers)->len, i; - VALUE el; - - if (!h->real || h->deleted) { - free (h); - return; - } - - for (i = 0; i < len; i++) { - el = rb_ary_shift (handlers); - - Data_Get_Struct (el, RbEventHandler, h2); - - if (h == h2) { - ecore_event_handler_del (h->real); - break; - } - - rb_ary_push (handlers, el); - } + if (h->real && !h->deleted) + ecore_event_handler_del (h->real); free (h); } @@ -79,7 +60,7 @@ static VALUE c_alloc (VALUE klass) return Data_Make_Struct (klass, RbEventHandler, c_mark, c_free, h); } -static VALUE c_init (VALUE self, VALUE type) +static VALUE c_init (VALUE self, VALUE klass) { RbEventHandler *h = NULL; int t; @@ -89,19 +70,17 @@ static VALUE c_init (VALUE self, VALUE type) Data_Get_Struct (self, RbEventHandler, h); - if (rb_obj_is_kind_of (type, rb_cModule) != Qtrue) + if (rb_obj_is_kind_of (klass, rb_cModule) != Qtrue) rb_raise (rb_eArgError, "invalid argument"); - t = NUM2INT (rb_const_get (type, rb_intern ("TYPE"))); + t = NUM2INT (rb_const_get (klass, rb_intern ("TYPE"))); if (t <= ECORE_EVENT_NONE) - rb_raise (rb_eStandardError, "invalid type"); + rb_raise (rb_eStandardError, "invalid event"); - h->type = t; + h->event_class = klass; h->callback = rb_block_proc (); h->deleted = false; - h->real = ecore_event_handler_add (t, on_ecore_event, NULL); - - rb_ary_push (handlers, self); + h->real = ecore_event_handler_add (t, on_ecore_event, h); return self; } @@ -109,73 +88,37 @@ static VALUE c_init (VALUE self, VALUE type) static VALUE c_delete (VALUE self) { RbEventHandler *h = NULL; - int len = RARRAY (handlers)->len, i; - VALUE el; - for (i = 0; i < len; i++) { - el = rb_ary_shift (handlers); - if (el == self) { - Data_Get_Struct (self, RbEventHandler, h); - ecore_event_handler_del (h->real); - h->real = NULL; - h->deleted = true; - - break; - } + Data_Get_Struct (self, RbEventHandler, h); - rb_ary_push (handlers, el); - } + if (h->real && !h->deleted) { + ecore_event_handler_del (h->real); + h->real = NULL; + h->deleted = true; + } else + rb_raise (rb_eException, "EventHandler already deleted!"); return Qnil; } static int on_ecore_event (void *data, int type, void *event) { - RbEventHandler *h = NULL; - VALUE handler, klass, obj, tmp, res; - int len, ret = 1, i; + RbEventHandler *h = data; + VALUE obj, tmp = (VALUE) event; - /* instantiate the event object - * first, find the class we're gonna use - */ - klass = rb_hash_aref (event_classes, INT2NUM (type)); - if (NIL_P (klass)) - rb_raise (rb_eException, "Cannot find event class " - "for event %i\n", type); + obj = rb_obj_alloc (h->event_class); - /* now create and init the object */ - tmp = (VALUE) event; - - /* if tmp is a Ruby class, we'll just pass the arguments to the - * initialize method. - * if it's a c struct, we can use rb_class_new_instance() - */ - if (rb_respond_to (klass, rb_intern ("raise"))) { - obj = rb_obj_alloc (klass); + if (rb_respond_to (h->event_class, rb_intern ("raise"))) rb_apply (obj, rb_intern ("initialize"), tmp); - } else - obj = rb_class_new_instance (1, &tmp, klass); - - len = RARRAY (handlers)->len; - - for (i = 0; i < len; i++) { - handler = rb_ary_entry (handlers, i); - Data_Get_Struct (handler, RbEventHandler, h); + else + rb_obj_call_init (obj, 1, &tmp); - if (h->type == type) { - res = rb_funcall (h->callback, rb_intern ("call"), 1, obj); + tmp = rb_funcall (h->callback, rb_intern ("call"), 1, obj); - /* if the block returned false, don't call the other - * event handlers - */ - if (res == Qfalse) { - ret = 0; - break; - } - } - } - - return ret; + /* if the block returned false, don't call the other + * event handlers + */ + return (tmp != Qfalse); } VALUE c_ev_inherited (VALUE klass, VALUE child) @@ -196,17 +139,19 @@ static VALUE c_ev_init (int argc, VALUE *argv, VALUE self) return self; } -static void free_ruby_event (void *data, void *event) +static void free_event_args (void *data, void *argv) { - /* do nothing */ + rb_ary_delete (event_args, (VALUE) argv); } static VALUE c_ev_raise (VALUE klass, VALUE argv) { VALUE t; + rb_ary_push (event_args, argv); + t = rb_const_get (klass, rb_intern ("TYPE")); - ecore_event_add (FIX2INT (t), (void *) argv, free_ruby_event, NULL); + ecore_event_add (FIX2INT (t), (void *) argv, free_event_args, NULL); return Qnil; } @@ -222,12 +167,12 @@ void Init_EventHandler (void) 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); + event_args = rb_ary_new (); + rb_global_variable (&event_args); + /* define a base event class */ cEcoreEvent = rb_define_class_under (mEcore, "Event", rb_cObject); rb_define_singleton_method (cEcoreEvent, "inherited", c_ev_inherited, 1);