Declare class variables the right way.
[ruby-ecore.git] / src / ecore / rb_timer.c
1 /*
2  * $Id: rb_timer.c 40 2004-07-25 13:14:34Z 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 int on_timer (void *data)
35 {
36         VALUE r;
37         RbEcoreTimer *timer = data;
38
39         r = rb_funcall ((VALUE) timer->cb, 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_free (RbEcoreTimer *timer)
51 {
52         if (timer->timer && !timer->deleted)
53                 ecore_timer_del (timer->timer);
54
55         ecore_shutdown ();
56
57         free (timer);
58 }
59
60 static VALUE c_new (VALUE klass, VALUE interval)
61 {
62         VALUE self;
63         RbEcoreTimer *timer;
64
65         if (!rb_block_given_p ())
66                 return Qnil;
67
68         self = Data_Make_Struct (klass, RbEcoreTimer, NULL, c_free, timer);
69
70         ecore_init ();
71
72         timer->cb = (void *) rb_block_proc ();
73         timer->timer = ecore_timer_add (NUM2DBL (interval),
74                                         on_timer, timer);
75
76         rb_obj_call_init (self, 0, NULL);
77
78         return self;
79 }
80
81 static VALUE c_delete (VALUE self)
82 {
83         RbEcoreTimer *timer = NULL;
84
85         Data_Get_Struct (self, RbEcoreTimer, timer);
86
87         if (timer->timer && !timer->deleted) {
88                 ecore_timer_del (timer->timer);
89                 timer->timer = NULL;
90                 timer->deleted = true;
91         } else
92                 rb_raise (rb_eException, "Timer already deleted!");
93
94         return Qnil;
95 }
96
97 void Init_Timer (void)
98 {
99         VALUE c = rb_define_class_under (mEcore, "Timer", rb_cObject);
100
101         rb_define_singleton_method (c, "new", c_new, 1);
102         rb_define_method (c, "delete", c_delete, 0);
103 }