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