/*
- * $Id: rb_timer.c 40 2004-07-25 13:14:34Z tilman $
+ * $Id: rb_timer.c 50 2004-08-01 10:18:39Z tilman $
*
* Copyright (C) 2004 Tilman Sauerbeck (tilman at code-monkey de)
*
*/
#include <ruby.h>
+#include <stdbool.h>
#include <Ecore.h>
-#include <stdbool.h>
#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
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 ();
static VALUE c_new (VALUE klass, VALUE interval)
{
VALUE self;
- RbEcoreTimer *timer;
+ RbTimer *timer = NULL;
if (!rb_block_given_p ())
return Qnil;
- self = Data_Make_Struct (klass, RbEcoreTimer, NULL, c_free, timer);
+ self = Data_Make_Struct (klass, RbTimer, c_mark, c_free, timer);
ecore_init ();
- timer->cb = (void *) rb_block_proc ();
- timer->timer = ecore_timer_add (NUM2DBL (interval),
- on_timer, timer);
+ timer->callback = rb_block_proc ();
+ timer->real = ecore_timer_add (NUM2DBL (interval),
+ on_timer, timer);
rb_obj_call_init (self, 0, NULL);
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!");