Removed RCS-style IDs.
[ruby-ecore.git] / src / ecore / rb_idler.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_Idler *real;
28         VALUE callback;
29         bool deleted;
30 } RbIdler;
31
32 static int on_idler (void *data)
33 {
34         VALUE r;
35         RbIdler *idler = data;
36
37         r = rb_funcall (idler->callback, rb_intern ("call"), 0);
38
39         /* if the callback returns false, we return 0 and Ecore
40          * will remove the idler
41          */
42         if (r == Qfalse)
43                 idler->deleted = true;
44
45         return (r != Qfalse);
46 }
47
48 static void c_mark (RbIdler *idler)
49 {
50         rb_gc_mark (idler->callback);
51 }
52
53 static void c_free (RbIdler *idler)
54 {
55         if (idler->real && !idler->deleted)
56                 ecore_idler_del (idler->real);
57
58         ecore_shutdown ();
59
60         free (idler);
61 }
62
63 static VALUE c_alloc (VALUE klass)
64 {
65         RbIdler *idler;
66
67         ecore_init ();
68
69         return Data_Make_Struct (klass, RbIdler, c_mark, c_free, idler);
70 }
71
72 /*
73  * call-seq:
74  *  Ecore::Idler.new { block } => idler
75  *
76  * Creates an Ecore::Idler object.
77  * When Ecore is idle, the specified block will be called.
78  * If the block returns false, the idler is deleted.
79  */
80 static VALUE c_init (VALUE self)
81 {
82         GET_OBJ (self, RbIdler, idler);
83
84         if (!rb_block_given_p ())
85                 rb_raise (rb_eStandardError, "block missing");
86
87         idler->callback = rb_block_proc ();
88         idler->deleted = false;
89         idler->real = ecore_idler_add (on_idler, idler);
90
91         return self;
92 }
93
94 /*
95  * call-seq:
96  *  idler.delete => nil
97  *
98  * Deletes <i>idler</i>.
99  */
100 static VALUE c_delete (VALUE self)
101 {
102         GET_OBJ (self, RbIdler, idler);
103
104         if (idler->real && !idler->deleted) {
105                 ecore_idler_del (idler->real);
106                 idler->real = NULL;
107                 idler->deleted = true;
108         } else
109                 rb_raise (rb_eException, "Idler already deleted!");
110
111         return Qnil;
112 }
113
114 void Init_Idler (void)
115 {
116         VALUE c = rb_define_class_under (mEcore, "Idler", rb_cObject);
117
118         rb_define_alloc_func (c, c_alloc);
119         rb_define_method (c, "initialize", c_init, 0);
120         rb_define_method (c, "delete", c_delete, 0);
121 }