/*
- * $Id: rb_timer.c 40 2004-07-25 13:14:34Z tilman $
+ * $Id: rb_timer.c 77 2004-08-19 17:39:29Z 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
*/
#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 ();
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_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->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 <i>timer</i>.
+ */
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!");