X-Git-Url: http://git.code-monkey.de/?p=ruby-ecore.git;a=blobdiff_plain;f=src%2Fecore%2Frb_timer.c;h=4d05d48d66a4284a91d73a3e2c1e81307285eea5;hp=543a5415f6dcc2c3ad2a423dce81ec361b8d04b1;hb=0f8d24c745efc1057f8d25ada1375c88a1ab2ed7;hpb=094fc971409b22abd413ed08935786dba73a9062 diff --git a/src/ecore/rb_timer.c b/src/ecore/rb_timer.c index 543a541..4d05d48 100644 --- a/src/ecore/rb_timer.c +++ b/src/ecore/rb_timer.c @@ -1,7 +1,5 @@ /* - * $Id: rb_timer.c 40 2004-07-25 13:14:34Z 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 @@ -19,24 +17,24 @@ */ #include +#include #include -#include #include "rb_ecore.h" typedef struct { - Ecore_Timer *timer; - void *cb; + Ecore_Timer *real; + VALUE callback; bool deleted; -} RbEcoreTimer; +} RbTimer; static int on_timer (void *data) { VALUE r; - RbEcoreTimer *timer = data; + RbTimer *timer = data; - r = rb_funcall ((VALUE) timer->cb, rb_intern ("call"), 0); + r = rb_funcall (timer->callback, rb_intern ("call"), 0); /* if the callback returns false, we return 0 and Ecore * will remove the timer @@ -47,46 +45,66 @@ static int on_timer (void *data) return (r != Qfalse); } -static void c_free (RbEcoreTimer *timer) +static void c_mark (RbTimer *timer) +{ + rb_gc_mark (timer->callback); +} + +static void c_free (RbTimer *timer) { - if (timer->timer && !timer->deleted) - ecore_timer_del (timer->timer); + if (timer->real && !timer->deleted) + ecore_timer_del (timer->real); ecore_shutdown (); free (timer); } -static VALUE c_new (VALUE klass, VALUE interval) +static VALUE c_alloc (VALUE klass) { - VALUE self; - RbEcoreTimer *timer; + RbTimer *timer = NULL; - if (!rb_block_given_p ()) - return Qnil; + ecore_init (); - self = Data_Make_Struct (klass, RbEcoreTimer, NULL, c_free, timer); + return Data_Make_Struct (klass, RbTimer, c_mark, c_free, timer); +} - ecore_init (); +/* + * 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); - timer->cb = (void *) rb_block_proc (); - timer->timer = ecore_timer_add (NUM2DBL (interval), - on_timer, timer); + if (!rb_block_given_p ()) + rb_raise (rb_eStandardError, "block missing"); - rb_obj_call_init (self, 0, NULL); + timer->callback = rb_block_proc (); + timer->deleted = false; + timer->real = ecore_timer_add (NUM2DBL (interval), + on_timer, timer); return self; } +/* + * call-seq: + * timer.delete => nil + * + * Deletes timer. + */ static VALUE c_delete (VALUE self) { - RbEcoreTimer *timer = NULL; - - Data_Get_Struct (self, RbEcoreTimer, timer); + GET_OBJ (self, RbTimer, timer); - if (timer->timer && !timer->deleted) { - ecore_timer_del (timer->timer); - timer->timer = NULL; + if (timer->real && !timer->deleted) { + ecore_timer_del (timer->real); + timer->real = NULL; timer->deleted = true; } else rb_raise (rb_eException, "Timer already deleted!"); @@ -94,10 +112,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); }