X-Git-Url: http://git.code-monkey.de/?a=blobdiff_plain;f=src%2Fecore%2Frb_fd_handler.c;fp=src%2Fecore%2Frb_fd_handler.c;h=4d7e08c9e616be99bb835a44954c6c41ecc002ca;hb=2188c2d3c4358727b204a81de3799ff970a43527;hp=0000000000000000000000000000000000000000;hpb=582e3062e93fdc1d864c8d0332c20bfffaf9b26a;p=ruby-ecore.git diff --git a/src/ecore/rb_fd_handler.c b/src/ecore/rb_fd_handler.c new file mode 100644 index 0000000..4d7e08c --- /dev/null +++ b/src/ecore/rb_fd_handler.c @@ -0,0 +1,144 @@ +/* + * $Id: rb_fd_handler.c 109 2004-09-01 20:33:15Z 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_Fd_Handler *real; + VALUE callback; + bool deleted; +} RbFdHandler; + +static int on_change (void *data, Ecore_Fd_Handler *unused) +{ + VALUE r; + RbFdHandler *fdh = data; + + r = rb_funcall (fdh->callback, rb_intern ("call"), 0); + + /* if the callback returns false, we return 0 and Ecore + * will remove the FD Handler + */ + if (r == Qfalse) + fdh->deleted = true; + + return (r != Qfalse); +} + +static void c_mark (RbFdHandler *fdh) +{ + rb_gc_mark (fdh->callback); +} + +static void c_free (RbFdHandler *fdh) +{ + if (fdh->real && !fdh->deleted) + ecore_main_fd_handler_del (fdh->real); + + ecore_shutdown (); + + free (fdh); +} + +static VALUE c_new (VALUE klass, VALUE fd, VALUE flags) +{ + VALUE self; + RbFdHandler *fdh = NULL; + + if (!rb_block_given_p ()) + return Qnil; + + Check_Type (fd, T_FIXNUM); + Check_Type (flags, T_FIXNUM); + + self = Data_Make_Struct (klass, RbFdHandler, c_mark, c_free, fdh); + + ecore_init (); + + fdh->callback = rb_block_proc (); + fdh->real = ecore_main_fd_handler_add (FIX2INT (fd), + FIX2INT (flags), on_change, + fdh, NULL, NULL); + + rb_obj_call_init (self, 0, NULL); + + return self; +} + +static VALUE c_delete (VALUE self) +{ + GET_OBJ (self, RbFdHandler, fdh); + + ecore_main_fd_handler_del (fdh->real); + fdh->real = NULL; + + return Qnil; +} + +static VALUE c_fd_get (VALUE self) +{ + GET_OBJ (self, RbFdHandler, fdh); + + return INT2FIX (ecore_main_fd_handler_fd_get (fdh->real)); +} + +static VALUE c_get_active (VALUE self, VALUE flags) +{ + int r; + + GET_OBJ (self, RbFdHandler, fdh); + + Check_Type (flags, T_FIXNUM); + + r = ecore_main_fd_handler_active_get (fdh->real, FIX2INT (flags)); + + return INT2FIX (r); +} + +static VALUE c_set_active (VALUE self, VALUE flags) +{ + GET_OBJ (self, RbFdHandler, fdh); + + Check_Type (flags, T_FIXNUM); + + ecore_main_fd_handler_active_set (fdh->real, FIX2INT (flags)); + + return Qnil; +} + +void Init_FdHandler (void) +{ + VALUE c = rb_define_class_under (mEcore, "FdHandler", rb_cObject); + + rb_define_singleton_method (c, "new", c_new, 2); + rb_define_method (c, "delete", c_delete, 0); + rb_define_method (c, "fd", c_fd_get, 0); + rb_define_method (c, "get_active", c_get_active, 1); + rb_define_method (c, "set_active", c_set_active, 1); + + DEF_CONST (c, ECORE_FD_, READ); + DEF_CONST (c, ECORE_FD_, WRITE); + DEF_CONST (c, ECORE_FD_, ERROR); +}