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