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