]> code.delx.au - pulseaudio/blob - polyp/iochannel.c
Make the whole stuff LGPL only
[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 Lesser General Public License as
8 published by the Free Software Foundation; either version 2.1 of the
9 License, 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 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 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 && io->input_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 if (e == io->input_event) {
83 io->mainloop->io_free(io->input_event);
84 io->input_event = NULL;
85 }
86
87 if (e == io->output_event) {
88 io->mainloop->io_free(io->output_event);
89 io->output_event = NULL;
90 }
91 } else {
92
93 if ((f & PA_IO_EVENT_INPUT) && !io->readable) {
94 io->readable = 1;
95 changed = 1;
96 assert(e == io->input_event);
97 }
98
99 if ((f & PA_IO_EVENT_OUTPUT) && !io->writable) {
100 io->writable = 1;
101 changed = 1;
102 assert(e == io->output_event);
103 }
104 }
105
106 if (changed) {
107 enable_mainloop_sources(io);
108
109 if (io->callback)
110 io->callback(io, io->userdata);
111 }
112 }
113
114 struct pa_iochannel* pa_iochannel_new(struct pa_mainloop_api*m, int ifd, int ofd) {
115 struct pa_iochannel *io;
116 assert(m && (ifd >= 0 || ofd >= 0));
117
118 io = pa_xmalloc(sizeof(struct pa_iochannel));
119 io->ifd = ifd;
120 io->ofd = ofd;
121 io->mainloop = m;
122
123 io->userdata = NULL;
124 io->callback = NULL;
125 io->readable = 0;
126 io->writable = 0;
127 io->hungup = 0;
128 io->no_close = 0;
129
130 io->input_event = io->output_event = NULL;
131
132 if (ifd == ofd) {
133 assert(ifd >= 0);
134 pa_make_nonblock_fd(io->ifd);
135 io->input_event = io->output_event = m->io_new(m, ifd, PA_IO_EVENT_INPUT|PA_IO_EVENT_OUTPUT, callback, io);
136 } else {
137
138 if (ifd >= 0) {
139 pa_make_nonblock_fd(io->ifd);
140 io->input_event = m->io_new(m, ifd, PA_IO_EVENT_INPUT, callback, io);
141 }
142
143 if (ofd >= 0) {
144 pa_make_nonblock_fd(io->ofd);
145 io->output_event = m->io_new(m, ofd, PA_IO_EVENT_OUTPUT, callback, io);
146 }
147 }
148
149 return io;
150 }
151
152 void pa_iochannel_free(struct pa_iochannel*io) {
153 assert(io);
154
155 if (io->input_event)
156 io->mainloop->io_free(io->input_event);
157 if (io->output_event && (io->output_event != io->input_event))
158 io->mainloop->io_free(io->output_event);
159
160 if (!io->no_close) {
161 if (io->ifd >= 0)
162 close(io->ifd);
163 if (io->ofd >= 0 && io->ofd != io->ifd)
164 close(io->ofd);
165 }
166
167 pa_xfree(io);
168 }
169
170 int pa_iochannel_is_readable(struct pa_iochannel*io) {
171 assert(io);
172 return io->readable || io->hungup;
173 }
174
175 int pa_iochannel_is_writable(struct pa_iochannel*io) {
176 assert(io);
177 return io->writable && !io->hungup;
178 }
179
180 int pa_iochannel_is_hungup(struct pa_iochannel*io) {
181 assert(io);
182 return io->hungup;
183 }
184
185 ssize_t pa_iochannel_write(struct pa_iochannel*io, const void*data, size_t l) {
186 ssize_t r;
187 assert(io && data && l && io->ofd >= 0);
188
189 if ((r = write(io->ofd, data, l)) >= 0) {
190 io->writable = 0;
191 enable_mainloop_sources(io);
192 }
193
194 return r;
195 }
196
197 ssize_t pa_iochannel_read(struct pa_iochannel*io, void*data, size_t l) {
198 ssize_t r;
199 assert(io && data && io->ifd >= 0);
200
201 if ((r = read(io->ifd, data, l)) >= 0) {
202 io->readable = 0;
203 enable_mainloop_sources(io);
204 }
205
206 return r;
207 }
208
209 void pa_iochannel_set_callback(struct pa_iochannel*io, void (*callback)(struct pa_iochannel*io, void *userdata), void *userdata) {
210 assert(io);
211 io->callback = callback;
212 io->userdata = userdata;
213 }
214
215 void pa_iochannel_set_noclose(struct pa_iochannel*io, int b) {
216 assert(io);
217 io->no_close = b;
218 }
219
220 void pa_iochannel_socket_peer_to_string(struct pa_iochannel*io, char*s, size_t l) {
221 assert(io && s && l);
222 pa_socket_peer_to_string(io->ifd, s, l);
223 }
224
225 int pa_iochannel_socket_set_rcvbuf(struct pa_iochannel *io, size_t l) {
226 assert(io);
227 return pa_socket_set_rcvbuf(io->ifd, l);
228 }
229
230 int pa_iochannel_socket_set_sndbuf(struct pa_iochannel *io, size_t l) {
231 assert(io);
232 return pa_socket_set_sndbuf(io->ofd, l);
233 }
234