X-Git-Url: http://git.code-monkey.de/?p=ruby-ecore.git;a=blobdiff_plain;f=src%2Fecore%2Frb_timer.c;h=6ef4d75c06d2a26b1240db3bf7fd95d8b8c9cc03;hp=fa9ab155cb960d0a10482272215e00b945a1fad2;hb=d6e45172f31e9a724a5da3b99ef26d519740d691;hpb=f805cf241a9d1fb9765892f0af48ede8359e9b65 diff --git a/src/ecore/rb_timer.c b/src/ecore/rb_timer.c index fa9ab15..6ef4d75 100644 --- a/src/ecore/rb_timer.c +++ b/src/ecore/rb_timer.c @@ -1,7 +1,5 @@ /* - * $Id: rb_timer.c 50 2004-08-01 10:18:39Z 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 @@ -62,27 +60,49 @@ static void c_free (RbTimer *timer) free (timer); } -static VALUE c_new (VALUE klass, VALUE interval) +static VALUE c_alloc (VALUE klass) { - VALUE self; RbTimer *timer = NULL; + ecore_init (); + + return Data_Make_Struct (klass, RbTimer, c_mark, c_free, timer); +} + +/* + * call-seq: + * Ecore::Timer.new(interval) { block } => timer + * + * Creates an Ecore::Timer object with the specified interval. + * When the timeout is hit, the block is called. + * If the block returns false, the timer is deleted. + */ +static VALUE c_init (VALUE self, VALUE interval) +{ + GET_OBJ (self, RbTimer, timer); + if (!rb_block_given_p ()) - return Qnil; + rb_raise (rb_eStandardError, "block missing"); - self = Data_Make_Struct (klass, RbTimer, c_mark, c_free, timer); + interval = rb_funcall2 (interval, rb_intern ("to_f"), 0, NULL); - ecore_init (); + if (rb_funcall2 (interval, rb_intern ("finite?"), 0, NULL) == Qfalse) + rb_raise (rb_eArgError, "Argument must not be infinite"); timer->callback = rb_block_proc (); + timer->deleted = false; timer->real = ecore_timer_add (NUM2DBL (interval), on_timer, timer); - rb_obj_call_init (self, 0, NULL); - return self; } +/* + * call-seq: + * timer.delete => nil + * + * Deletes timer. + */ static VALUE c_delete (VALUE self) { GET_OBJ (self, RbTimer, timer); @@ -97,10 +117,29 @@ static VALUE c_delete (VALUE self) return Qnil; } +static VALUE c_deleted_get (VALUE self) +{ + GET_OBJ (self, RbTimer, timer); + + return timer->deleted ? Qtrue : Qfalse; +} + +static VALUE c_interval_set (VALUE self, VALUE interval) +{ + GET_OBJ (self, RbTimer, timer); + + ecore_timer_interval_set (timer->real, NUM2DBL (interval)); + + return Qnil; +} + void Init_Timer (void) { VALUE c = rb_define_class_under (mEcore, "Timer", rb_cObject); - rb_define_singleton_method (c, "new", c_new, 1); + rb_define_alloc_func (c, c_alloc); + rb_define_method (c, "initialize", c_init, 1); rb_define_method (c, "delete", c_delete, 0); + rb_define_method (c, "deleted?", c_deleted_get, 0); + rb_define_method (c, "interval=", c_interval_set, 1); }