2 * $Id: rb_fd_handler.c 109 2004-09-01 20:33:15Z tilman $
4 * Copyright (C) 2004 ruby-ecore team (see AUTHORS)
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.
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.
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
29 Ecore_Fd_Handler *real;
34 static int on_change (void *data, Ecore_Fd_Handler *unused)
37 RbFdHandler *fdh = data;
39 r = rb_funcall (fdh->callback, rb_intern ("call"), 0);
41 /* if the callback returns false, we return 0 and Ecore
42 * will remove the FD Handler
50 static void c_mark (RbFdHandler *fdh)
52 rb_gc_mark (fdh->callback);
55 static void c_free (RbFdHandler *fdh)
57 if (fdh->real && !fdh->deleted)
58 ecore_main_fd_handler_del (fdh->real);
65 static VALUE c_new (VALUE klass, VALUE fd, VALUE flags)
68 RbFdHandler *fdh = NULL;
70 if (!rb_block_given_p ())
73 Check_Type (fd, T_FIXNUM);
74 Check_Type (flags, T_FIXNUM);
76 self = Data_Make_Struct (klass, RbFdHandler, c_mark, c_free, fdh);
80 fdh->callback = rb_block_proc ();
81 fdh->real = ecore_main_fd_handler_add (FIX2INT (fd),
82 FIX2INT (flags), on_change,
85 rb_obj_call_init (self, 0, NULL);
90 static VALUE c_delete (VALUE self)
92 GET_OBJ (self, RbFdHandler, fdh);
94 ecore_main_fd_handler_del (fdh->real);
100 static VALUE c_fd_get (VALUE self)
102 GET_OBJ (self, RbFdHandler, fdh);
104 return INT2FIX (ecore_main_fd_handler_fd_get (fdh->real));
107 static VALUE c_get_active (VALUE self, VALUE flags)
111 GET_OBJ (self, RbFdHandler, fdh);
113 Check_Type (flags, T_FIXNUM);
115 r = ecore_main_fd_handler_active_get (fdh->real, FIX2INT (flags));
120 static VALUE c_set_active (VALUE self, VALUE flags)
122 GET_OBJ (self, RbFdHandler, fdh);
124 Check_Type (flags, T_FIXNUM);
126 ecore_main_fd_handler_active_set (fdh->real, FIX2INT (flags));
131 void Init_FdHandler (void)
133 VALUE c = rb_define_class_under (mEcore, "FdHandler", rb_cObject);
135 rb_define_singleton_method (c, "new", c_new, 2);
136 rb_define_method (c, "delete", c_delete, 0);
137 rb_define_method (c, "fd", c_fd_get, 0);
138 rb_define_method (c, "get_active", c_get_active, 1);
139 rb_define_method (c, "set_active", c_set_active, 1);
141 DEF_CONST (c, ECORE_FD_, READ);
142 DEF_CONST (c, ECORE_FD_, WRITE);
143 DEF_CONST (c, ECORE_FD_, ERROR);