2 * Copyright (C) 2004 ruby-ecore team (see AUTHORS)
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.
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.
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
32 static int on_idler (void *data)
35 RbIdler *idler = data;
37 r = rb_funcall (idler->callback, rb_intern ("call"), 0);
39 /* if the callback returns false, we return 0 and Ecore
40 * will remove the idler
43 idler->deleted = true;
48 static void c_mark (RbIdler *idler)
50 rb_gc_mark (idler->callback);
53 static void c_free (RbIdler *idler)
55 if (idler->real && !idler->deleted)
56 ecore_idler_del (idler->real);
63 static VALUE c_alloc (VALUE klass)
69 return Data_Make_Struct (klass, RbIdler, c_mark, c_free, idler);
74 * Ecore::Idler.new { block } => idler
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.
80 static VALUE c_init (VALUE self)
82 GET_OBJ (self, RbIdler, idler);
84 if (!rb_block_given_p ())
85 rb_raise (rb_eStandardError, "block missing");
87 idler->callback = rb_block_proc ();
88 idler->deleted = false;
89 idler->real = ecore_idler_add (on_idler, idler);
98 * Deletes <i>idler</i>.
100 static VALUE c_delete (VALUE self)
102 GET_OBJ (self, RbIdler, idler);
104 if (idler->real && !idler->deleted) {
105 ecore_idler_del (idler->real);
107 idler->deleted = true;
109 rb_raise (rb_eException, "Idler already deleted!");
114 void Init_Idler (void)
116 VALUE c = rb_define_class_under (mEcore, "Idler", rb_cObject);
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);