We now use real structs to wrap objects.
[ruby-ecore.git] / src / ecore / rb_timer.c
index 543a5415f6dcc2c3ad2a423dce81ec361b8d04b1..fa9ab155cb960d0a10482272215e00b945a1fad2 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * $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
@@ -47,10 +47,15 @@ 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 ();
 
@@ -60,18 +65,18 @@ static void c_free (RbEcoreTimer *timer)
 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);
 
@@ -80,13 +85,11 @@ static VALUE c_new (VALUE klass, VALUE interval)
 
 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!");