Added Ecore::Animator.
[ruby-ecore.git] / src / ecore / rb_animator.c
1 /*
2  * $Id: rb_animator.c 146 2004-11-27 15:38:52Z 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_Animator *real;
30         VALUE callback;
31         bool deleted;
32 } RbAnimator;
33
34 static int on_animator (void *data)
35 {
36         VALUE r;
37         RbAnimator *animator = data;
38
39         r = rb_funcall (animator->callback, rb_intern ("call"), 0);
40
41         /* if the callback returns false, we return 0 and Ecore
42          * will remove the animator
43          */
44         if (r == Qfalse)
45                 animator->deleted = true;
46
47         return (r != Qfalse);
48 }
49
50 static void c_mark (RbAnimator *animator)
51 {
52         rb_gc_mark (animator->callback);
53 }
54
55 static void c_free (RbAnimator *animator)
56 {
57         if (animator->real && !animator->deleted)
58                 ecore_animator_del (animator->real);
59
60         ecore_shutdown ();
61
62         free (animator);
63 }
64
65 static VALUE c_new (VALUE klass)
66 {
67         VALUE self;
68         RbAnimator *animator = NULL;
69
70         if (!rb_block_given_p ())
71                 return Qnil;
72
73         self = Data_Make_Struct (klass, RbAnimator, c_mark, c_free, animator);
74
75         ecore_init ();
76
77         animator->callback = rb_block_proc ();
78         animator->deleted = false;
79         animator->real = ecore_animator_add (on_animator, animator);
80
81         rb_obj_call_init (self, 0, NULL);
82
83         return self;
84 }
85
86 static VALUE c_delete (VALUE self)
87 {
88         GET_OBJ (self, RbAnimator, animator);
89
90         if (animator->real && !animator->deleted) {
91                 ecore_animator_del (animator->real);
92                 animator->real = NULL;
93                 animator->deleted = true;
94         } else
95                 rb_raise (rb_eException, "Animator already deleted!");
96
97         return Qnil;
98 }
99
100 void Init_Animator (void)
101 {
102         VALUE c = rb_define_class_under (mEcore, "Animator", rb_cObject);
103
104         rb_define_singleton_method (c, "new", c_new, 0);
105         rb_define_method (c, "delete", c_delete, 0);
106 }