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