From 2188c2d3c4358727b204a81de3799ff970a43527 Mon Sep 17 00:00:00 2001 From: Tilman Sauerbeck Date: Wed, 1 Sep 2004 20:33:15 +0000 Subject: [PATCH] Added Ecore::FdHandler. Moved DEF_CONST. --- src/ecore/Makefile.am | 5 +- src/ecore/rb_ecore.c | 4 +- src/ecore/rb_ecore.h | 6 +- src/ecore/rb_fd_handler.c | 144 ++++++++++++++++++++++++++++++++++++++ src/ecore/rb_fd_handler.h | 26 +++++++ src/ecore_x/rb_cursor.c | 3 +- src/ecore_x/rb_ecore_x.h | 6 +- 7 files changed, 184 insertions(+), 10 deletions(-) create mode 100644 src/ecore/rb_fd_handler.c create mode 100644 src/ecore/rb_fd_handler.h diff --git a/src/ecore/Makefile.am b/src/ecore/Makefile.am index f3c8a08..78a0790 100644 --- a/src/ecore/Makefile.am +++ b/src/ecore/Makefile.am @@ -1,4 +1,4 @@ -## $Id: Makefile.am 77 2004-08-19 17:39:29Z tilman $ +## $Id: Makefile.am 109 2004-09-01 20:33:15Z tilman $ AM_CFLAGS = $(ECORE_CFLAGS) INCLUDES = -I$(RUBYDIR) @@ -9,7 +9,8 @@ extdir = $(RUBYSITEDIR) ecore_la_SOURCES = rb_ecore.c rb_ecore.h \ rb_idler.c rb_idler.h \ rb_timer.c rb_timer.h \ - rb_event_handler.c rb_event_handler.h + rb_event_handler.c rb_event_handler.h \ + rb_fd_handler.c rb_fd_handler.h ecore_la_LIBADD = -L$(RUBYLIBDIR) -lruby $(ECORE_LIBS) ecore_la_LDFLAGS = -module -avoid-version diff --git a/src/ecore/rb_ecore.c b/src/ecore/rb_ecore.c index 52b7e7a..f950f7e 100644 --- a/src/ecore/rb_ecore.c +++ b/src/ecore/rb_ecore.c @@ -1,5 +1,5 @@ /* - * $Id: rb_ecore.c 83 2004-08-21 19:55:35Z tilman $ + * $Id: rb_ecore.c 109 2004-09-01 20:33:15Z tilman $ * * Copyright (C) 2004 ruby-ecore team (see AUTHORS) * @@ -27,6 +27,7 @@ #include "rb_timer.h" #include "rb_idler.h" #include "rb_event_handler.h" +#include "rb_fd_handler.h" VALUE mEcore; @@ -143,6 +144,7 @@ void Init_ecore (void) Init_Timer (); Init_Idler (); Init_EventHandler (); + Init_FdHandler (); /* SIGNAL_HUP */ ADD_EVENT (mEcore, ECORE_EVENT_, SIGNAL_HUP, diff --git a/src/ecore/rb_ecore.h b/src/ecore/rb_ecore.h index 5afbc03..95cbde4 100644 --- a/src/ecore/rb_ecore.h +++ b/src/ecore/rb_ecore.h @@ -1,5 +1,5 @@ /* - * $Id: rb_ecore.h 77 2004-08-19 17:39:29Z tilman $ + * $Id: rb_ecore.h 109 2004-09-01 20:33:15Z tilman $ * * Copyright (C) 2004 ruby-ecore team (see AUTHORS) * @@ -54,6 +54,10 @@ \ return rb_str_new2 (buf); +#define DEF_CONST(mod, prefix, name) \ + rb_define_const ((mod), #name, \ + INT2FIX (prefix##name)); + #ifndef __RB_ECORE_C VALUE mEcore; #endif 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); +} diff --git a/src/ecore/rb_fd_handler.h b/src/ecore/rb_fd_handler.h new file mode 100644 index 0000000..87f09f2 --- /dev/null +++ b/src/ecore/rb_fd_handler.h @@ -0,0 +1,26 @@ +/* + * $Id: rb_fd_handler.h 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 + */ + +#ifndef __RB_FD_HANDLER_H +#define __RB_FD_HANDLER_H + +void Init_FdHandler (void); + +#endif diff --git a/src/ecore_x/rb_cursor.c b/src/ecore_x/rb_cursor.c index 9109151..0612666 100644 --- a/src/ecore_x/rb_cursor.c +++ b/src/ecore_x/rb_cursor.c @@ -1,5 +1,5 @@ /* - * $Id: rb_cursor.c 98 2004-08-26 13:12:55Z tilman $ + * $Id: rb_cursor.c 109 2004-09-01 20:33:15Z tilman $ * * Copyright (C) 2004 ruby-ecore team (see AUTHORS) * @@ -24,6 +24,7 @@ #include #define __RB_CURSOR_C +#include "../ecore/rb_ecore.h" #include "rb_ecore_x.h" #include "rb_cursor.h" diff --git a/src/ecore_x/rb_ecore_x.h b/src/ecore_x/rb_ecore_x.h index a18a12d..f089738 100644 --- a/src/ecore_x/rb_ecore_x.h +++ b/src/ecore_x/rb_ecore_x.h @@ -1,5 +1,5 @@ /* - * $Id: rb_ecore_x.h 98 2004-08-26 13:12:55Z tilman $ + * $Id: rb_ecore_x.h 109 2004-09-01 20:33:15Z tilman $ * * Copyright (C) 2004 ruby-ecore team (see AUTHORS) * @@ -21,10 +21,6 @@ #ifndef __RB_ECORE_X_H #define __RB_ECORE_X_H -#define DEF_CONST(mod, prefix, name) \ - rb_define_const ((mod), #name, \ - INT2FIX (prefix##name)); - VALUE mX; #endif -- 2.30.2