]>
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 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.
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.
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
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
&& io
->input_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
) {
82 if (e
== io
->input_event
) {
83 io
->mainloop
->io_free(io
->input_event
);
84 io
->input_event
= NULL
;
87 if (e
== io
->output_event
) {
88 io
->mainloop
->io_free(io
->output_event
);
89 io
->output_event
= NULL
;
93 if ((f
& PA_IO_EVENT_INPUT
) && !io
->readable
) {
96 assert(e
== io
->input_event
);
99 if ((f
& PA_IO_EVENT_OUTPUT
) && !io
->writable
) {
102 assert(e
== io
->output_event
);
107 enable_mainloop_sources(io
);
110 io
->callback(io
, io
->userdata
);
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));
118 io
= pa_xmalloc(sizeof(struct pa_iochannel
));
130 io
->input_event
= io
->output_event
= NULL
;
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
);
139 pa_make_nonblock_fd(io
->ifd
);
140 io
->input_event
= m
->io_new(m
, ifd
, PA_IO_EVENT_INPUT
, callback
, io
);
144 pa_make_nonblock_fd(io
->ofd
);
145 io
->output_event
= m
->io_new(m
, ofd
, PA_IO_EVENT_OUTPUT
, callback
, io
);
152 void pa_iochannel_free(struct pa_iochannel
*io
) {
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
);
163 if (io
->ofd
>= 0 && io
->ofd
!= io
->ifd
)
170 int pa_iochannel_is_readable(struct pa_iochannel
*io
) {
172 return io
->readable
|| io
->hungup
;
175 int pa_iochannel_is_writable(struct pa_iochannel
*io
) {
177 return io
->writable
&& !io
->hungup
;
180 int pa_iochannel_is_hungup(struct pa_iochannel
*io
) {
185 ssize_t
pa_iochannel_write(struct pa_iochannel
*io
, const void*data
, size_t l
) {
187 assert(io
&& data
&& l
&& io
->ofd
>= 0);
189 if ((r
= write(io
->ofd
, data
, l
)) >= 0) {
191 enable_mainloop_sources(io
);
197 ssize_t
pa_iochannel_read(struct pa_iochannel
*io
, void*data
, size_t l
) {
199 assert(io
&& data
&& io
->ifd
>= 0);
201 if ((r
= read(io
->ifd
, data
, l
)) >= 0) {
203 enable_mainloop_sources(io
);
209 void pa_iochannel_set_callback(struct pa_iochannel
*io
, void (*callback
)(struct pa_iochannel
*io
, void *userdata
), void *userdata
) {
211 io
->callback
= callback
;
212 io
->userdata
= userdata
;
215 void pa_iochannel_set_noclose(struct pa_iochannel
*io
, int b
) {
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
);
225 int pa_iochannel_socket_set_rcvbuf(struct pa_iochannel
*io
, size_t l
) {
227 return pa_socket_set_rcvbuf(io
->ifd
, l
);
230 int pa_iochannel_socket_set_sndbuf(struct pa_iochannel
*io
, size_t l
) {
232 return pa_socket_set_sndbuf(io
->ofd
, l
);