Added Ecore::Animator.
[ruby-ecore.git] / src / ecore / rb_animator.c
diff --git a/src/ecore/rb_animator.c b/src/ecore/rb_animator.c
new file mode 100644 (file)
index 0000000..6544baa
--- /dev/null
@@ -0,0 +1,106 @@
+/*
+ * $Id: rb_animator.c 146 2004-11-27 15:38:52Z tilman $
+ *
+ * Copyright (C) 2004 ruby-ecore team (see AUTHORS)
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ */
+
+#include <ruby.h>
+#include <stdbool.h>
+
+#include <Ecore.h>
+
+#include "rb_ecore.h"
+
+typedef struct {
+       Ecore_Animator *real;
+       VALUE callback;
+       bool deleted;
+} RbAnimator;
+
+static int on_animator (void *data)
+{
+       VALUE r;
+       RbAnimator *animator = data;
+
+       r = rb_funcall (animator->callback, rb_intern ("call"), 0);
+
+       /* if the callback returns false, we return 0 and Ecore
+        * will remove the animator
+        */
+       if (r == Qfalse)
+               animator->deleted = true;
+
+       return (r != Qfalse);
+}
+
+static void c_mark (RbAnimator *animator)
+{
+       rb_gc_mark (animator->callback);
+}
+
+static void c_free (RbAnimator *animator)
+{
+       if (animator->real && !animator->deleted)
+               ecore_animator_del (animator->real);
+
+       ecore_shutdown ();
+
+       free (animator);
+}
+
+static VALUE c_new (VALUE klass)
+{
+       VALUE self;
+       RbAnimator *animator = NULL;
+
+       if (!rb_block_given_p ())
+               return Qnil;
+
+       self = Data_Make_Struct (klass, RbAnimator, c_mark, c_free, animator);
+
+       ecore_init ();
+
+       animator->callback = rb_block_proc ();
+       animator->deleted = false;
+       animator->real = ecore_animator_add (on_animator, animator);
+
+       rb_obj_call_init (self, 0, NULL);
+
+       return self;
+}
+
+static VALUE c_delete (VALUE self)
+{
+       GET_OBJ (self, RbAnimator, animator);
+
+       if (animator->real && !animator->deleted) {
+               ecore_animator_del (animator->real);
+               animator->real = NULL;
+               animator->deleted = true;
+       } else
+               rb_raise (rb_eException, "Animator already deleted!");
+
+       return Qnil;
+}
+
+void Init_Animator (void)
+{
+       VALUE c = rb_define_class_under (mEcore, "Animator", rb_cObject);
+
+       rb_define_singleton_method (c, "new", c_new, 0);
+       rb_define_method (c, "delete", c_delete, 0);
+}