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