Removed RCS-style IDs.
[ruby-ecore.git] / src / ecore / rb_animator.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 void Init_stack (VALUE *addr);
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         static bool initted;
39
40         /* this fixes a weird segfault that occured when the animator's
41          * callback was called from a Ecore::Timer callback.
42          */
43         if (!initted) {
44                 Init_stack (0);
45                 initted = true;
46         }
47
48         r = rb_funcall (animator->callback, rb_intern ("call"), 0);
49
50         /* if the callback returns false, we return 0 and Ecore
51          * will remove the animator
52          */
53         if (r == Qfalse)
54                 animator->deleted = true;
55
56         return (r != Qfalse);
57 }
58
59 static void c_mark (RbAnimator *animator)
60 {
61         rb_gc_mark (animator->callback);
62 }
63
64 static void c_free (RbAnimator *animator)
65 {
66         if (animator->real && !animator->deleted)
67                 ecore_animator_del (animator->real);
68
69         ecore_shutdown ();
70
71         free (animator);
72 }
73
74 static VALUE c_alloc (VALUE klass)
75 {
76         RbAnimator *animator = NULL;
77
78         ecore_init ();
79
80         return Data_Make_Struct (klass, RbAnimator, c_mark, c_free, animator);
81 }
82
83 static VALUE c_init (VALUE self)
84 {
85         GET_OBJ (self, RbAnimator, animator);
86
87         if (!rb_block_given_p ())
88                 rb_raise (rb_eStandardError, "block missing");
89
90         animator->callback = rb_block_proc ();
91         animator->deleted = false;
92         animator->real = ecore_animator_add (on_animator, animator);
93
94         return self;
95 }
96
97 static VALUE c_delete (VALUE self)
98 {
99         GET_OBJ (self, RbAnimator, animator);
100
101         if (animator->real && !animator->deleted) {
102                 ecore_animator_del (animator->real);
103                 animator->real = NULL;
104                 animator->deleted = true;
105         } else
106                 rb_raise (rb_eException, "Animator already deleted!");
107
108         return Qnil;
109 }
110
111 void Init_Animator (void)
112 {
113         VALUE c = rb_define_class_under (mEcore, "Animator", rb_cObject);
114
115         rb_define_alloc_func (c, c_alloc);
116         rb_define_method (c, "initialize", c_init, 0);
117         rb_define_method (c, "delete", c_delete, 0);
118 }