X-Git-Url: http://git.code-monkey.de/?p=ruby-ecore.git;a=blobdiff_plain;f=src%2Fecore%2Frb_event_handler.c;h=8e7312926373a0f8ec9b8ad3efceec518d736f8d;hp=1edc71d4fdd3da45712a751659fd817cfc4959c3;hb=8df3535f85c3fd7a1f00d989c5e27416e7ade8b4;hpb=f199e32f0269ef563060795ea3c27229d685dddf diff --git a/src/ecore/rb_event_handler.c b/src/ecore/rb_event_handler.c index 1edc71d..8e73129 100644 --- a/src/ecore/rb_event_handler.c +++ b/src/ecore/rb_event_handler.c @@ -1,5 +1,5 @@ /* - * $Id: rb_event_handler.c 107 2004-08-29 18:37:58Z tilman $ + * $Id: rb_event_handler.c 359 2006-02-12 15:51:58Z tilman $ * * Copyright (C) 2004 ruby-ecore team (see AUTHORS) * @@ -27,40 +27,98 @@ #include "rb_ecore.h" #include "rb_event_handler.h" +typedef struct { + Ecore_Event_Handler *real; + VALUE callback; + bool deleted; +} RbEventHandler; + +static int on_ecore_event (void *data, int type, void *event); + VALUE event_classes, cEcoreEvent; static VALUE handlers; +static void c_mark (RbEventHandler *h) +{ + rb_gc_mark (h->callback); +} + +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); + } + + free (h); +} + +static VALUE c_alloc (VALUE klass) +{ + RbEventHandler *h = NULL; + + return Data_Make_Struct (klass, RbEventHandler, c_mark, c_free, h); +} + static VALUE c_init (VALUE self, VALUE type) { + RbEventHandler *h = NULL; int t; if (!rb_block_given_p ()) - return Qnil; + rb_raise (rb_eStandardError, "block missing"); + + Data_Get_Struct (self, RbEventHandler, h); t = NUM2INT (type); if (t <= ECORE_EVENT_NONE) - return Qnil; + rb_raise (rb_eStandardError, "invalid type"); rb_iv_set (self, "@type", type); - rb_iv_set (self, "handler", rb_block_proc ()); - rb_ary_push (handlers, self); + h->callback = rb_block_proc (); + h->deleted = false; + h->real = ecore_event_handler_add (t, on_ecore_event, NULL); - ecore_event_handler_add (t, on_ecore_event, NULL); + rb_ary_push (handlers, self); return self; } 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) - return Qnil; + if (el == self) { + Data_Get_Struct (self, RbEventHandler, h); + ecore_event_handler_del (h->real); + h->real = NULL; + h->deleted = true; + + break; + } rb_ary_push (handlers, el); } @@ -68,25 +126,24 @@ static VALUE c_delete (VALUE self) return Qnil; } -int on_ecore_event (void *data, int type, void *event) +static int on_ecore_event (void *data, int type, void *event) { - VALUE handler, klass, obj, argv[1], res; + RbEventHandler *h = NULL; + VALUE handler, klass, obj, tmp, 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)))) { + 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); - return 0; - } /* now create and init the object */ - obj = rb_obj_alloc (klass); - argv[0] = (VALUE) event; - rb_obj_call_init (obj, 1, argv); + tmp = (VALUE) event; + obj = rb_class_new_instance (1, &tmp, klass); len = RARRAY (handlers)->len; @@ -95,8 +152,8 @@ int on_ecore_event (void *data, int type, void *event) 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); + Data_Get_Struct (handler, RbEventHandler, h); + res = rb_funcall (h->callback, rb_intern ("call"), 1, obj); called = true; /* if the block returned false, don't call the other @@ -107,9 +164,6 @@ int on_ecore_event (void *data, int type, void *event) } } - if (type == ECORE_EVENT_SIGNAL_EXIT && !called) - ecore_main_loop_quit (); - /* call other event handlers, too */ return 1; } @@ -127,6 +181,7 @@ void Init_EventHandler (void) cEventHandler = rb_define_class_under (mEcore, "EventHandler", rb_cObject); + rb_define_alloc_func (cEventHandler, c_alloc); rb_define_method (cEventHandler, "initialize", c_init, 1); rb_define_method (cEventHandler, "delete", c_delete, 0);