Removed RCS-style IDs.
[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         timer->callback = rb_block_proc ();
88         timer->deleted = false;
89         timer->real = ecore_timer_add (NUM2DBL (interval),
90                                        on_timer, timer);
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_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);
140 }