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