Fixed the build with the latest Ecore snapshot.
[ruby-ecore.git] / src / ecore / rb_timer.c
1 /*
2  * Copyright (C) 2004 ruby-ecore team (see AUTHORS)
3  *
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.
8  *
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.
13  *
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
17  */
18
19 #include <ruby.h>
20 #include <stdbool.h>
21
22 #include <Ecore.h>
23
24 #include "rb_ecore.h"
25
26 typedef struct {
27         Ecore_Timer *real;
28         VALUE callback;
29         bool deleted;
30 } RbTimer;
31
32 static int on_timer (void *data)
33 {
34         VALUE r;
35         RbTimer *timer = data;
36
37         r = rb_funcall (timer->callback, rb_intern ("call"), 0);
38
39         /* if the callback returns false, we return 0 and Ecore
40          * will remove the timer
41          */
42         if (r == Qfalse)
43                 timer->deleted = true;
44
45         return (r != Qfalse);
46 }
47
48 static void c_mark (RbTimer *timer)
49 {
50         rb_gc_mark (timer->callback);
51 }
52
53 static void c_free (RbTimer *timer)
54 {
55         if (timer->real && !timer->deleted)
56                 ecore_timer_del (timer->real);
57
58         ecore_shutdown ();
59
60         free (timer);
61 }
62
63 static VALUE c_alloc (VALUE klass)
64 {
65         RbTimer *timer = NULL;
66
67         ecore_init ();
68
69         return Data_Make_Struct (klass, RbTimer, c_mark, c_free, timer);
70 }
71
72 /*
73  * call-seq:
74  *  Ecore::Timer.new(interval) { block } => timer
75  *
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.
79  */
80 static VALUE c_init (VALUE self, VALUE interval)
81 {
82         GET_OBJ (self, RbTimer, timer);
83
84         if (!rb_block_given_p ())
85                 rb_raise (rb_eStandardError, "block missing");
86
87         interval = rb_funcall2 (interval, rb_intern ("to_f"), 0, NULL);
88
89         if (rb_funcall2 (interval, rb_intern ("finite?"), 0, NULL) == Qfalse)
90                 rb_raise (rb_eArgError, "Argument must not be infinite");
91
92         timer->callback = rb_block_proc ();
93         timer->deleted = false;
94         timer->real = ecore_timer_add (NUM2DBL (interval),
95                                        on_timer, timer);
96
97         return self;
98 }
99
100 /*
101  * call-seq:
102  *  timer.delete => nil
103  *
104  * Deletes <i>timer</i>.
105  */
106 static VALUE c_delete (VALUE self)
107 {
108         GET_OBJ (self, RbTimer, timer);
109
110         if (timer->real && !timer->deleted) {
111                 ecore_timer_del (timer->real);
112                 timer->real = NULL;
113                 timer->deleted = true;
114         } else
115                 rb_raise (rb_eException, "Timer already deleted!");
116
117         return Qnil;
118 }
119
120 static VALUE c_deleted_get (VALUE self)
121 {
122         GET_OBJ (self, RbTimer, timer);
123
124         return timer->deleted ? Qtrue : Qfalse;
125 }
126
127 static VALUE c_interval_set (VALUE self, VALUE interval)
128 {
129         GET_OBJ (self, RbTimer, timer);
130
131         ecore_timer_interval_set (timer->real, NUM2DBL (interval));
132
133         return Qnil;
134 }
135
136 void Init_Timer (void)
137 {
138         VALUE c = rb_define_class_under (mEcore, "Timer", rb_cObject);
139
140         rb_define_alloc_func (c, c_alloc);
141         rb_define_method (c, "initialize", c_init, 1);
142         rb_define_method (c, "delete", c_delete, 0);
143         rb_define_method (c, "deleted?", c_deleted_get, 0);
144         rb_define_method (c, "interval=", c_interval_set, 1);
145 }