Initial commit.
[ruby-ecore.git] / src / ecore / rb_idler.c
1 /*
2  * $Id$
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_Idler *idler;
30         void *cb;
31         bool deleted;
32 } RbEcoreIdler;
33
34 VALUE cIdler;
35
36 static int on_idler (void *data)
37 {
38         VALUE r;
39         RbEcoreIdler *idler = data;
40
41         r = rb_funcall ((VALUE) idler->cb, rb_intern ("call"), 0);
42
43         /* if the callback returns false, we return 0 and Ecore
44          * will remove the idler
45          */
46         if (r == Qfalse)
47                 idler->deleted = true;
48
49         return (r != Qfalse);
50 }
51
52 static VALUE c_init (VALUE self)
53 {
54         RbEcoreIdler *idler = NULL;
55
56         Data_Get_Struct (self, RbEcoreIdler, idler);
57
58         idler->cb = (void *) rb_block_proc ();
59         idler->idler = ecore_idler_add (on_idler, idler);
60
61         return self;
62 }
63
64 static void c_free (RbEcoreIdler *idler)
65 {
66         if (idler->idler && !idler->deleted)
67                 ecore_idler_del (idler->idler);
68
69         free (idler);
70 }
71
72 static VALUE c_new (VALUE klass)
73 {
74         VALUE self;
75         RbEcoreIdler *idler;
76
77         self = Data_Make_Struct (klass, RbEcoreIdler, NULL, c_free, idler);
78
79         rb_obj_call_init (self, 0, NULL);
80
81         return self;
82 }
83
84 static VALUE c_delete (VALUE self)
85 {
86         RbEcoreIdler *idler = NULL;
87
88         Data_Get_Struct (self, RbEcoreIdler, idler);
89
90         if (idler->idler && !idler->deleted) {
91                 ecore_idler_del (idler->idler);
92                 idler->idler = NULL;
93                 idler->deleted = true;
94         } else
95                 rb_raise (rb_eException, "Idler already deleted!");
96
97         return Qnil;
98 }
99
100 void Init_Idler (void)
101 {
102         cIdler = rb_define_class_under (mEcore, "Idler", rb_cObject);
103
104         rb_define_singleton_method (cIdler, "new", c_new, 1);
105         rb_define_method (cIdler, "initialize", c_init, 1);
106         rb_define_method (cIdler, "delete", c_delete, 0);
107 }
108