Fixed the build with the latest Ecore snapshot.
[ruby-ecore.git] / src / ecore / rb_fd_handler.c
1 /*
2  * Copyright (C) 2004 ruby-ecore team (see AUTHORS)
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
17  */
18
19 #include <ruby.h>
20 #include <stdbool.h>
21
22 #include <Ecore.h>
23
24 #include "rb_ecore.h"
25
26 typedef struct {
27         Ecore_Fd_Handler *real;
28         VALUE callback;
29         bool deleted;
30 } RbFdHandler;
31
32 static int on_change (void *data, Ecore_Fd_Handler *unused)
33 {
34         VALUE r;
35         RbFdHandler *fdh = data;
36
37         r = rb_funcall (fdh->callback, rb_intern ("call"), 0);
38
39         /* if the callback returns false, we return 0 and Ecore
40          * will remove the FD Handler
41          */
42         if (r == Qfalse)
43                 fdh->deleted = true;
44
45         return (r != Qfalse);
46 }
47
48 static void c_mark (RbFdHandler *fdh)
49 {
50         rb_gc_mark (fdh->callback);
51 }
52
53 static void c_free (RbFdHandler *fdh)
54 {
55         if (fdh->real && !fdh->deleted)
56                 ecore_main_fd_handler_del (fdh->real);
57
58         ecore_shutdown ();
59
60         free (fdh);
61 }
62
63 static VALUE c_alloc (VALUE klass)
64 {
65         RbFdHandler *fdh = NULL;
66
67         ecore_init ();
68
69         return Data_Make_Struct (klass, RbFdHandler, c_mark, c_free, fdh);
70 }
71
72 static VALUE c_init (VALUE self, VALUE fd, VALUE flags)
73 {
74         GET_OBJ (self, RbFdHandler, fdh);
75
76         if (!rb_block_given_p ())
77                 rb_raise (rb_eStandardError, "block missing");
78
79         Check_Type (fd, T_FIXNUM);
80         Check_Type (flags, T_FIXNUM);
81
82         fdh->callback = rb_block_proc ();
83         fdh->real = ecore_main_fd_handler_add (FIX2INT (fd),
84                                                FIX2INT (flags), on_change,
85                                                fdh, NULL, NULL);
86
87         return self;
88 }
89
90 static VALUE c_delete (VALUE self)
91 {
92         GET_OBJ (self, RbFdHandler, fdh);
93
94         ecore_main_fd_handler_del (fdh->real);
95         fdh->real = NULL;
96
97         return Qnil;
98 }
99
100 static VALUE c_fd_get (VALUE self)
101 {
102         GET_OBJ (self, RbFdHandler, fdh);
103
104         return INT2FIX (ecore_main_fd_handler_fd_get (fdh->real));
105 }
106
107 static VALUE c_get_active (VALUE self, VALUE flags)
108 {
109         int r;
110
111         GET_OBJ (self, RbFdHandler, fdh);
112
113         Check_Type (flags, T_FIXNUM);
114
115         r = ecore_main_fd_handler_active_get (fdh->real, FIX2INT (flags));
116
117         return INT2FIX (r);
118 }
119
120 static VALUE c_set_active (VALUE self, VALUE flags)
121 {
122         GET_OBJ (self, RbFdHandler, fdh);
123
124         Check_Type (flags, T_FIXNUM);
125
126         ecore_main_fd_handler_active_set (fdh->real, FIX2INT (flags));
127
128         return Qnil;
129 }
130
131 void Init_FdHandler (void)
132 {
133         VALUE c = rb_define_class_under (mEcore, "FdHandler", rb_cObject);
134
135         rb_define_alloc_func (c, c_alloc);
136         rb_define_method (c, "initialize", c_init, 2);
137         rb_define_method (c, "delete", c_delete, 0);
138         rb_define_method (c, "fd", c_fd_get, 0);
139         rb_define_method (c, "get_active", c_get_active, 1);
140         rb_define_method (c, "set_active", c_set_active, 1);
141
142         DEF_CONST (c, ECORE_FD_, READ);
143         DEF_CONST (c, ECORE_FD_, WRITE);
144         DEF_CONST (c, ECORE_FD_, ERROR);
145 }