Don't use global variables for the Ruby classes.
[ruby-ecore.git] / src / ecore / rb_timer.c
1 /*
2  * $Id: rb_timer.c 25 2004-06-26 23:07:01Z tilman $
3  *
4  * Copyright (C) 2004 Tilman Sauerbeck (tilman at code-monkey de)
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
23 #include <Ecore.h>
24 #include <stdbool.h>
25
26 #include "rb_ecore.h"
27
28 typedef struct {
29         Ecore_Timer *timer;
30         void *cb;
31         bool deleted;
32 } RbEcoreTimer;
33
34 static VALUE cTimer;
35
36 static int on_timer (void *data)
37 {
38         VALUE r;
39         RbEcoreTimer *timer = data;
40
41         r = rb_funcall ((VALUE) timer->cb, rb_intern ("call"), 0);
42
43         /* if the callback returns false, we return 0 and Ecore
44          * will remove the timer
45          */
46         if (r == Qfalse)
47                 timer->deleted = true;
48
49         return (r != Qfalse);
50 }
51
52 static VALUE c_init (VALUE self, VALUE interval)
53 {
54         RbEcoreTimer *timer = NULL;
55
56         Data_Get_Struct (self, RbEcoreTimer, timer);
57
58         timer->cb = (void *) rb_block_proc ();
59         timer->timer = ecore_timer_add (NUM2DBL (interval),
60                                         on_timer, timer);
61
62         return self;
63 }
64
65 static void c_free (RbEcoreTimer *timer)
66 {
67         if (timer->timer && !timer->deleted)
68                 ecore_timer_del (timer->timer);
69
70         free (timer);
71 }
72
73 static VALUE c_new (VALUE klass, VALUE interval)
74 {
75         VALUE self, argv[1];
76         RbEcoreTimer *timer;
77
78         self = Data_Make_Struct (klass, RbEcoreTimer, NULL, c_free, timer);
79
80         argv[0] = interval;
81         rb_obj_call_init (self, 1, argv);
82
83         return self;
84 }
85
86 static VALUE c_delete (VALUE self)
87 {
88         RbEcoreTimer *timer = NULL;
89
90         Data_Get_Struct (self, RbEcoreTimer, timer);
91
92         if (timer->timer && !timer->deleted) {
93                 ecore_timer_del (timer->timer);
94                 timer->timer = NULL;
95                 timer->deleted = true;
96         } else
97                 rb_raise (rb_eException, "Timer already deleted!");
98
99         return Qnil;
100 }
101
102 void Init_Timer (void)
103 {
104         cTimer = rb_define_class_under (mEcore, "Timer", rb_cObject);
105
106         rb_define_singleton_method (cTimer, "new", c_new, 1);
107         rb_define_method (cTimer, "initialize", c_init, 1);
108         rb_define_method (cTimer, "delete", c_delete, 0);
109 }
110