]> code.delx.au - pulseaudio/blob - polyp/iochannel.c
add initial glib mainloop adapter
[pulseaudio] / polyp / iochannel.c
1 /* $Id$ */
2
3 /***
4 This file is part of polypaudio.
5
6 polypaudio is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published
8 by the Free Software Foundation; either version 2 of the License,
9 or (at your option) any later version.
10
11 polypaudio is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with polypaudio; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19 USA.
20 ***/
21
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25
26 #include <stdlib.h>
27 #include <assert.h>
28 #include <fcntl.h>
29 #include <unistd.h>
30
31 #include "iochannel.h"
32 #include "util.h"
33 #include "socket-util.h"
34 #include "xmalloc.h"
35
36 struct pa_iochannel {
37 int ifd, ofd;
38 struct pa_mainloop_api* mainloop;
39
40 void (*callback)(struct pa_iochannel*io, void *userdata);
41 void*userdata;
42
43 int readable;
44 int writable;
45 int hungup;
46
47 int no_close;
48
49 struct pa_io_event* input_event, *output_event;
50 };
51
52 static void enable_mainloop_sources(struct pa_iochannel *io) {
53 assert(io);
54
55 if (io->input_event == io->output_event) {
56 enum pa_io_event_flags f = PA_IO_EVENT_NULL;
57 assert(io->input_event);
58
59 if (!io->readable)
60 f |= PA_IO_EVENT_INPUT;
61 if (!io->writable)
62 f |= PA_IO_EVENT_OUTPUT;
63
64 io->mainloop->io_enable(io->input_event, f);
65 } else {
66 if (io->input_event)
67 io->mainloop->io_enable(io->input_event, io->readable ? PA_IO_EVENT_NULL : PA_IO_EVENT_INPUT);
68 if (io->output_event)
69 io->mainloop->io_enable(io->output_event, io->writable ? PA_IO_EVENT_NULL : PA_IO_EVENT_OUTPUT);
70 }
71 }
72
73 static void callback(struct pa_mainloop_api* m, struct pa_io_event *e, int fd, enum pa_io_event_flags f, void *userdata) {
74 struct pa_iochannel *io = userdata;
75 int changed = 0;
76 assert(m && e && fd >= 0 && userdata);
77
78 if ((f & (PA_IO_EVENT_HANGUP|PA_IO_EVENT_ERROR)) && !io->hungup) {
79 io->hungup = 1;
80 changed = 1;
81 }
82
83 if ((f & PA_IO_EVENT_INPUT) && !io->readable) {
84 io->readable = 1;
85 changed = 1;
86 assert(e == io->input_event);
87 }
88
89 if ((f & PA_IO_EVENT_OUTPUT) && !io->writable) {
90 io->writable = 1;
91 changed = 1;
92 assert(e == io->output_event);
93 }
94
95 if (changed) {
96 enable_mainloop_sources(io);
97
98 if (io->callback)
99 io->callback(io, io->userdata);
100 }
101 }
102
103 struct pa_iochannel* pa_iochannel_new(struct pa_mainloop_api*m, int ifd, int ofd) {
104 struct pa_iochannel *io;
105 assert(m && (ifd >= 0 || ofd >= 0));
106
107 io = pa_xmalloc(sizeof(struct pa_iochannel));
108 io->ifd = ifd;
109 io->ofd = ofd;
110 io->mainloop = m;
111
112 io->userdata = NULL;
113 io->callback = NULL;
114 io->readable = 0;
115 io->writable = 0;
116 io->hungup = 0;
117 io->no_close = 0;
118
119 io->input_event = io->output_event = NULL;
120
121 if (ifd == ofd) {
122 assert(ifd >= 0);
123 pa_make_nonblock_fd(io->ifd);
124 io->input_event = io->output_event = m->io_new(m, ifd, PA_IO_EVENT_INPUT|PA_IO_EVENT_OUTPUT, callback, io);
125 } else {
126
127 if (ifd >= 0) {
128 pa_make_nonblock_fd(io->ifd);
129 io->input_event = m->io_new(m, ifd, PA_IO_EVENT_INPUT, callback, io);
130 }
131
132 if (ofd >= 0) {
133 pa_make_nonblock_fd(io->ofd);
134 io->output_event = m->io_new(m, ofd, PA_IO_EVENT_OUTPUT, callback, io);
135 }
136 }
137
138 return io;
139 }
140
141 void pa_iochannel_free(struct pa_iochannel*io) {
142 assert(io);
143
144 if (io->input_event)
145 io->mainloop->io_free(io->input_event);
146 if (io->output_event && (io->output_event != io->input_event))
147 io->mainloop->io_free(io->output_event);
148
149 if (!io->no_close) {
150 if (io->ifd >= 0)
151 close(io->ifd);
152 if (io->ofd >= 0 && io->ofd != io->ifd)
153 close(io->ofd);
154 }
155
156 pa_xfree(io);
157 }
158
159 int pa_iochannel_is_readable(struct pa_iochannel*io) {
160 assert(io);
161 return io->readable;
162 }
163
164 int pa_iochannel_is_writable(struct pa_iochannel*io) {
165 assert(io);
166 return io->writable;
167 }
168
169 int pa_iochannel_is_hungup(struct pa_iochannel*io) {
170 assert(io);
171 return io->hungup;
172 }
173
174 ssize_t pa_iochannel_write(struct pa_iochannel*io, const void*data, size_t l) {
175 ssize_t r;
176 assert(io && data && l && io->ofd >= 0);
177
178 if ((r = write(io->ofd, data, l)) >= 0) {
179 io->writable = 0;
180 enable_mainloop_sources(io);
181 }
182
183 return r;
184 }
185
186 ssize_t pa_iochannel_read(struct pa_iochannel*io, void*data, size_t l) {
187 ssize_t r;
188
189 assert(io && data && io->ifd >= 0);
190
191 if ((r = read(io->ifd, data, l)) >= 0) {
192 io->readable = 0;
193 enable_mainloop_sources(io);
194 }
195
196 return r;
197 }
198
199 void pa_iochannel_set_callback(struct pa_iochannel*io, void (*callback)(struct pa_iochannel*io, void *userdata), void *userdata) {
200 assert(io);
201 io->callback = callback;
202 io->userdata = userdata;
203 }
204
205 void pa_iochannel_set_noclose(struct pa_iochannel*io, int b) {
206 assert(io);
207 io->no_close = b;
208 }
209
210 void pa_iochannel_socket_peer_to_string(struct pa_iochannel*io, char*s, size_t l) {
211 assert(io && s && l);
212 pa_socket_peer_to_string(io->ifd, s, l);
213 }
214
215 int pa_iochannel_socket_set_rcvbuf(struct pa_iochannel *io, size_t l) {
216 assert(io);
217 return pa_socket_set_rcvbuf(io->ifd, l);
218 }
219
220 int pa_iochannel_socket_set_sndbuf(struct pa_iochannel *io, size_t l) {
221 assert(io);
222 return pa_socket_set_sndbuf(io->ofd, l);
223 }