/* * $Id: rb_timer.c 351 2006-02-10 15:25:40Z 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 #include #include #include "rb_ecore.h" typedef struct { Ecore_Timer *real; VALUE callback; bool deleted; } RbTimer; static int on_timer (void *data) { VALUE r; RbTimer *timer = data; r = rb_funcall (timer->callback, rb_intern ("call"), 0); /* if the callback returns false, we return 0 and Ecore * will remove the timer */ if (r == Qfalse) timer->deleted = true; return (r != Qfalse); } static void c_mark (RbTimer *timer) { rb_gc_mark (timer->callback); } static void c_free (RbTimer *timer) { if (timer->real && !timer->deleted) ecore_timer_del (timer->real); ecore_shutdown (); free (timer); } static VALUE c_alloc (VALUE klass) { RbTimer *timer = NULL; ecore_init (); return Data_Make_Struct (klass, RbTimer, c_mark, c_free, timer); } /* * call-seq: * Ecore::Timer.new(interval) { block } => timer * * Creates an Ecore::Timer object with the specified interval. * When the timeout is hit, the block is called. * If the block returns false, the timer is deleted. */ static VALUE c_init (VALUE self, VALUE interval) { GET_OBJ (self, RbTimer, timer); if (!rb_block_given_p ()) rb_raise (rb_eStandardError, "block missing"); timer->callback = rb_block_proc (); timer->deleted = false; timer->real = ecore_timer_add (NUM2DBL (interval), on_timer, timer); return self; } /* * call-seq: * timer.delete => nil * * Deletes timer. */ static VALUE c_delete (VALUE self) { GET_OBJ (self, RbTimer, timer); if (timer->real && !timer->deleted) { ecore_timer_del (timer->real); timer->real = NULL; timer->deleted = true; } else rb_raise (rb_eException, "Timer already deleted!"); return Qnil; } static VALUE c_deleted_get (VALUE self) { GET_OBJ (self, RbTimer, timer); return timer->deleted ? Qtrue : Qfalse; } static VALUE c_interval_set (VALUE self, VALUE interval) { GET_OBJ (self, RbTimer, timer); ecore_timer_interval_set (timer->real, NUM2DBL (interval)); return Qnil; } void Init_Timer (void) { VALUE c = rb_define_class_under (mEcore, "Timer", rb_cObject); rb_define_alloc_func (c, c_alloc); rb_define_method (c, "initialize", c_init, 1); rb_define_method (c, "delete", c_delete, 0); rb_define_method (c, "deleted?", c_deleted_get, 0); rb_define_method (c, "interval=", c_interval_set, 1); }