]>
code.delx.au - pulseaudio/blob - polyp/iochannel.c
4 This file is part of polypaudio.
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.
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.
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
31 #include "iochannel.h"
33 #include "socket-util.h"
38 struct pa_mainloop_api
* mainloop
;
40 void (*callback
)(struct pa_iochannel
*io
, void *userdata
);
49 struct pa_io_event
* input_event
, *output_event
;
52 static void enable_mainloop_sources(struct pa_iochannel
*io
) {
55 if (io
->input_event
== io
->output_event
) {
56 enum pa_io_event_flags f
= PA_IO_EVENT_NULL
;
57 assert(io
->input_event
);
60 f
|= PA_IO_EVENT_INPUT
;
62 f
|= PA_IO_EVENT_OUTPUT
;
64 io
->mainloop
->io_enable(io
->input_event
, f
);
67 io
->mainloop
->io_enable(io
->input_event
, io
->readable
? PA_IO_EVENT_NULL
: PA_IO_EVENT_INPUT
);
69 io
->mainloop
->io_enable(io
->output_event
, io
->writable
? PA_IO_EVENT_NULL
: PA_IO_EVENT_OUTPUT
);
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
;
76 assert(m
&& e
&& fd
>= 0 && userdata
);
78 if ((f
& (PA_IO_EVENT_HANGUP
|PA_IO_EVENT_ERROR
)) && !io
->hungup
) {
83 if ((f
& PA_IO_EVENT_INPUT
) && !io
->readable
) {
86 assert(e
== io
->input_event
);
89 if ((f
& PA_IO_EVENT_OUTPUT
) && !io
->writable
) {
92 assert(e
== io
->output_event
);
96 enable_mainloop_sources(io
);
99 io
->callback(io
, io
->userdata
);
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));
107 io
= pa_xmalloc(sizeof(struct pa_iochannel
));
119 io
->input_event
= io
->output_event
= NULL
;
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
);
128 pa_make_nonblock_fd(io
->ifd
);
129 io
->input_event
= m
->io_new(m
, ifd
, PA_IO_EVENT_INPUT
, callback
, io
);
133 pa_make_nonblock_fd(io
->ofd
);
134 io
->output_event
= m
->io_new(m
, ofd
, PA_IO_EVENT_OUTPUT
, callback
, io
);
141 void pa_iochannel_free(struct pa_iochannel
*io
) {
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
);
152 if (io
->ofd
>= 0 && io
->ofd
!= io
->ifd
)
159 int pa_iochannel_is_readable(struct pa_iochannel
*io
) {
164 int pa_iochannel_is_writable(struct pa_iochannel
*io
) {
169 int pa_iochannel_is_hungup(struct pa_iochannel
*io
) {
174 ssize_t
pa_iochannel_write(struct pa_iochannel
*io
, const void*data
, size_t l
) {
179 assert(io
->ofd
>= 0);
182 assert(io
&& data
&& l
&& io
->ofd
>= 0);
186 if ((r
= write(io
->ofd
, data
, l
)) >= 0) {
188 enable_mainloop_sources(io
);
194 ssize_t
pa_iochannel_read(struct pa_iochannel
*io
, void*data
, size_t l
) {
197 assert(io
&& data
&& io
->ifd
>= 0);
199 if ((r
= read(io
->ifd
, data
, l
)) >= 0) {
201 enable_mainloop_sources(io
);
207 void pa_iochannel_set_callback(struct pa_iochannel
*io
, void (*callback
)(struct pa_iochannel
*io
, void *userdata
), void *userdata
) {
209 io
->callback
= callback
;
210 io
->userdata
= userdata
;
213 void pa_iochannel_set_noclose(struct pa_iochannel
*io
, int b
) {
218 void pa_iochannel_socket_peer_to_string(struct pa_iochannel
*io
, char*s
, size_t l
) {
219 assert(io
&& s
&& l
);
220 pa_socket_peer_to_string(io
->ifd
, s
, l
);
223 int pa_iochannel_socket_set_rcvbuf(struct pa_iochannel
*io
, size_t l
) {
225 return pa_socket_set_rcvbuf(io
->ifd
, l
);
228 int pa_iochannel_socket_set_sndbuf(struct pa_iochannel
*io
, size_t l
) {
230 return pa_socket_set_sndbuf(io
->ofd
, l
);
233 void pa_iochannel_force_unreadable(struct pa_iochannel
*io
) {
236 enable_mainloop_sources(io
);
239 void pa_iochannel_force_unwritable(struct pa_iochannel
*io
) {
241 enable_mainloop_sources(io
);