2 * $Id: rb_timer.c 351 2006-02-10 15:25:40Z tilman $
4 * Copyright (C) 2004 ruby-ecore team (see AUTHORS)
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
34 static int on_timer (void *data)
37 RbTimer *timer = data;
39 r = rb_funcall (timer->callback, rb_intern ("call"), 0);
41 /* if the callback returns false, we return 0 and Ecore
42 * will remove the timer
45 timer->deleted = true;
50 static void c_mark (RbTimer *timer)
52 rb_gc_mark (timer->callback);
55 static void c_free (RbTimer *timer)
57 if (timer->real && !timer->deleted)
58 ecore_timer_del (timer->real);
65 static VALUE c_alloc (VALUE klass)
67 RbTimer *timer = NULL;
71 return Data_Make_Struct (klass, RbTimer, c_mark, c_free, timer);
76 * Ecore::Timer.new(interval) { block } => timer
78 * Creates an Ecore::Timer object with the specified interval.
79 * When the timeout is hit, the block is called.
80 * If the block returns false, the timer is deleted.
82 static VALUE c_init (VALUE self, VALUE interval)
84 GET_OBJ (self, RbTimer, timer);
86 if (!rb_block_given_p ())
87 rb_raise (rb_eStandardError, "block missing");
89 timer->callback = rb_block_proc ();
90 timer->deleted = false;
91 timer->real = ecore_timer_add (NUM2DBL (interval),
101 * Deletes <i>timer</i>.
103 static VALUE c_delete (VALUE self)
105 GET_OBJ (self, RbTimer, timer);
107 if (timer->real && !timer->deleted) {
108 ecore_timer_del (timer->real);
110 timer->deleted = true;
112 rb_raise (rb_eException, "Timer already deleted!");
117 static VALUE c_deleted_get (VALUE self)
119 GET_OBJ (self, RbTimer, timer);
121 return timer->deleted ? Qtrue : Qfalse;
124 static VALUE c_interval_set (VALUE self, VALUE interval)
126 GET_OBJ (self, RbTimer, timer);
128 ecore_timer_interval_set (timer->real, NUM2DBL (interval));
133 void Init_Timer (void)
135 VALUE c = rb_define_class_under (mEcore, "Timer", rb_cObject);
137 rb_define_alloc_func (c, c_alloc);
138 rb_define_method (c, "initialize", c_init, 1);
139 rb_define_method (c, "delete", c_delete, 0);
140 rb_define_method (c, "deleted?", c_deleted_get, 0);
141 rb_define_method (c, "interval=", c_interval_set, 1);