Added Ecore::Evas::EcoreEvas#has_alpha? and #has_alpha=.
[ruby-ecore.git] / src / ecore / rb_fd_handler.c
1 /*
2  * $Id: rb_fd_handler.c 351 2006-02-10 15:25:40Z tilman $
3  *
4  * Copyright (C) 2004 ruby-ecore team (see AUTHORS)
5  *
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.
10  *
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.
15  *
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
19  */
20
21 #include <ruby.h>
22 #include <stdbool.h>
23
24 #include <Ecore.h>
25
26 #include "rb_ecore.h"
27
28 typedef struct {
29         Ecore_Fd_Handler *real;
30         VALUE callback;
31         bool deleted;
32 } RbFdHandler;
33
34 static int on_change (void *data, Ecore_Fd_Handler *unused)
35 {
36         VALUE r;
37         RbFdHandler *fdh = data;
38
39         r = rb_funcall (fdh->callback, rb_intern ("call"), 0);
40
41         /* if the callback returns false, we return 0 and Ecore
42          * will remove the FD Handler
43          */
44         if (r == Qfalse)
45                 fdh->deleted = true;
46
47         return (r != Qfalse);
48 }
49
50 static void c_mark (RbFdHandler *fdh)
51 {
52         rb_gc_mark (fdh->callback);
53 }
54
55 static void c_free (RbFdHandler *fdh)
56 {
57         if (fdh->real && !fdh->deleted)
58                 ecore_main_fd_handler_del (fdh->real);
59
60         ecore_shutdown ();
61
62         free (fdh);
63 }
64
65 static VALUE c_alloc (VALUE klass)
66 {
67         RbFdHandler *fdh = NULL;
68
69         ecore_init ();
70
71         return Data_Make_Struct (klass, RbFdHandler, c_mark, c_free, fdh);
72 }
73
74 static VALUE c_init (VALUE self, VALUE fd, VALUE flags)
75 {
76         GET_OBJ (self, RbFdHandler, fdh);
77
78         if (!rb_block_given_p ())
79                 rb_raise (rb_eStandardError, "block missing");
80
81         Check_Type (fd, T_FIXNUM);
82         Check_Type (flags, T_FIXNUM);
83
84         fdh->callback = rb_block_proc ();
85         fdh->real = ecore_main_fd_handler_add (FIX2INT (fd),
86                                                FIX2INT (flags), on_change,
87                                                fdh, NULL, NULL);
88
89         return self;
90 }
91
92 static VALUE c_delete (VALUE self)
93 {
94         GET_OBJ (self, RbFdHandler, fdh);
95
96         ecore_main_fd_handler_del (fdh->real);
97         fdh->real = NULL;
98
99         return Qnil;
100 }
101
102 static VALUE c_fd_get (VALUE self)
103 {
104         GET_OBJ (self, RbFdHandler, fdh);
105
106         return INT2FIX (ecore_main_fd_handler_fd_get (fdh->real));
107 }
108
109 static VALUE c_get_active (VALUE self, VALUE flags)
110 {
111         int r;
112
113         GET_OBJ (self, RbFdHandler, fdh);
114
115         Check_Type (flags, T_FIXNUM);
116
117         r = ecore_main_fd_handler_active_get (fdh->real, FIX2INT (flags));
118
119         return INT2FIX (r);
120 }
121
122 static VALUE c_set_active (VALUE self, VALUE flags)
123 {
124         GET_OBJ (self, RbFdHandler, fdh);
125
126         Check_Type (flags, T_FIXNUM);
127
128         ecore_main_fd_handler_active_set (fdh->real, FIX2INT (flags));
129
130         return Qnil;
131 }
132
133 void Init_FdHandler (void)
134 {
135         VALUE c = rb_define_class_under (mEcore, "FdHandler", rb_cObject);
136
137         rb_define_alloc_func (c, c_alloc);
138         rb_define_method (c, "initialize", c_init, 2);
139         rb_define_method (c, "delete", c_delete, 0);
140         rb_define_method (c, "fd", c_fd_get, 0);
141         rb_define_method (c, "get_active", c_get_active, 1);
142         rb_define_method (c, "set_active", c_set_active, 1);
143
144         DEF_CONST (c, ECORE_FD_, READ);
145         DEF_CONST (c, ECORE_FD_, WRITE);
146         DEF_CONST (c, ECORE_FD_, ERROR);
147 }