2 * $Id: rb_timer.c 27 2004-07-08 18:25:05Z tilman $
4 * Copyright (C) 2004 Tilman Sauerbeck (tilman at code-monkey de)
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
36 static int on_timer (void *data)
39 RbEcoreTimer *timer = data;
41 r = rb_funcall ((VALUE) timer->cb, rb_intern ("call"), 0);
43 /* if the callback returns false, we return 0 and Ecore
44 * will remove the timer
47 timer->deleted = true;
52 static void c_free (RbEcoreTimer *timer)
54 if (timer->timer && !timer->deleted)
55 ecore_timer_del (timer->timer);
62 static VALUE c_new (VALUE klass, VALUE interval)
67 if (!rb_block_given_p ())
70 self = Data_Make_Struct (klass, RbEcoreTimer, NULL, c_free, timer);
74 timer->cb = (void *) rb_block_proc ();
75 timer->timer = ecore_timer_add (NUM2DBL (interval),
78 rb_obj_call_init (self, 0, NULL);
83 static VALUE c_delete (VALUE self)
85 RbEcoreTimer *timer = NULL;
87 Data_Get_Struct (self, RbEcoreTimer, timer);
89 if (timer->timer && !timer->deleted) {
90 ecore_timer_del (timer->timer);
92 timer->deleted = true;
94 rb_raise (rb_eException, "Timer already deleted!");
99 void Init_Timer (void)
101 cTimer = rb_define_class_under (mEcore, "Timer", rb_cObject);
103 rb_define_singleton_method (cTimer, "new", c_new, 1);
104 rb_define_method (cTimer, "delete", c_delete, 0);