xref: /openbsd/lib/libsndio/mio_open.3 (revision 18bc0c2f)
1.\" $OpenBSD: mio_open.3,v 1.18 2018/12/18 20:37:27 jmc Exp $
2.\"
3.\" Copyright (c) 2007 Alexandre Ratchov <alex@caoua.org>
4.\"
5.\" Permission to use, copy, modify, and distribute this software for any
6.\" purpose with or without fee is hereby granted, provided that the above
7.\" copyright notice and this permission notice appear in all copies.
8.\"
9.\" THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10.\" WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11.\" MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12.\" ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13.\" WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14.\" ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15.\" OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16.\"
17.Dd $Mdocdate: December 18 2018 $
18.Dt MIO_OPEN 3
19.Os
20.Sh NAME
21.Nm mio_open ,
22.Nm mio_close ,
23.Nm mio_read ,
24.Nm mio_write ,
25.Nm mio_nfds ,
26.Nm mio_pollfd ,
27.Nm mio_revents ,
28.Nm mio_eof
29.Nd sndio interface to MIDI streams
30.Sh SYNOPSIS
31.In sndio.h
32.Ft "struct mio_hdl *"
33.Fn mio_open "const char *name" "unsigned int mode" "int nbio_flag"
34.Ft "void"
35.Fn mio_close "struct mio_hdl *hdl"
36.Ft "size_t"
37.Fn mio_read "struct mio_hdl *hdl" "void *addr" "size_t nbytes"
38.Ft "size_t"
39.Fn mio_write "struct mio_hdl *hdl" "const void *addr" "size_t nbytes"
40.Ft "int"
41.Fn mio_nfds "struct mio_hdl *hdl"
42.Ft "int"
43.Fn mio_pollfd "struct mio_hdl *hdl" "struct pollfd *pfd" "int events"
44.Ft "int"
45.Fn mio_revents "struct mio_hdl *hdl" "struct pollfd *pfd"
46.Ft "int"
47.Fn mio_eof "struct mio_hdl *hdl"
48.Sh DESCRIPTION
49The
50.Nm sndio
51library allows user processes to access
52.Xr midi 4
53hardware and
54.Xr sndiod 8
55MIDI thru boxes and control ports in a uniform way.
56.Ss Opening and closing a MIDI stream
57First the application must call the
58.Fn mio_open
59function to obtain a handle representing the newly created stream;
60later it will be passed as the
61.Ar hdl
62argument of most other functions.
63The
64.Ar name
65parameter gives the device string discussed in
66.Xr sndio 7 .
67If the program is using a single device and is providing no device chooser,
68it should be set to MIO_PORTANY to allow the user to select it using the
69.Ev MIDIDEVICE
70environment variable.
71.Pp
72The
73.Ar mode
74parameter gives the direction of the stream.
75The following are supported:
76.Bl -tag -width "MIO_OUT | MIO_IN"
77.It MIO_OUT
78The stream is output-only; data written to the stream will be sent
79to the hardware or other programs.
80.It MIO_IN
81The stream is input-only; received data from the hardware or
82other programs must be read from the stream.
83.It MIO_IN | MIO_OUT
84The stream sends and receives data.
85This mode should be used rather than calling
86.Fn mio_open
87twice.
88.El
89.Pp
90If the
91.Ar nbio_flag
92argument is true (i.e. non-zero), then the
93.Fn mio_read
94and
95.Fn mio_write
96functions (see below) will be non-blocking.
97.Pp
98The
99.Fn mio_close
100function closes the stream and frees all allocated resources
101associated with the
102.Nm libsndio
103handle.
104.Ss Sending and receiving data
105When input mode is selected, the
106.Fn mio_read
107function must be called to retrieve received data; it must be called
108often enough to ensure that internal buffers will not overrun.
109It will store at most
110.Ar nbytes
111bytes at the
112.Ar addr
113location.
114Unless the
115.Ar nbio_flag
116flag is set, it will block until data becomes available and
117will return zero only on error.
118.Pp
119When output mode is selected, the
120.Fn mio_write
121function can be called to provide data to transmit.
122Unless the
123.Ar nbio_flag
124is set,
125.Fn mio_write
126will block until the requested amount of data is written.
127.Ss Non-blocking mode operation
128If the
129.Ar nbio_flag
130is set on
131.Fn mio_open ,
132then the
133.Fn mio_read
134and
135.Fn mio_write
136functions will never block; if no data is available, they will
137return zero immediately.
138.Pp
139To avoid busy loops when non-blocking mode is used, the
140.Xr poll 2
141system call can be used to check if data can be
142read from or written to the stream.
143The
144.Fn mio_pollfd
145function prepares the array
146.Ar pfd
147of
148.Va pollfd
149structures for use with
150.Xr poll 2 .
151The optimal size of the
152.Ar pfd
153array, which the caller must pre-allocate, is provided by the
154.Fn mio_nfds
155function.
156.Pp
157.Xr poll 2
158will sleep until any of the
159.Ar events
160requested with
161.Fn mio_pollfd
162have occurred.
163Events are represented as a bit-mask of
164.Va POLLIN
165and
166.Va POLLOUT
167constants.
168The events which woke up
169.Xr poll 2
170can be obtained with the
171.Fn mio_revents
172function.
173If
174.Va POLLIN
175is set,
176.Fn mio_read
177can be called without blocking.
178If
179.Va POLLOUT
180is set,
181.Fn mio_write
182can be called without blocking.
183POLLHUP may be set if an error occurs, even if
184it is not requested with
185.Fn mio_pollfd .
186.Ss Error handling
187Errors related to the MIDI subsystem
188(like hardware errors or dropped connections) and
189programming errors (such as a call to
190.Fn mio_read
191on a play-only stream) are considered fatal.
192Once an error occurs, all functions which take a
193.Va mio_hdl
194argument, except
195.Fn mio_close
196and
197.Fn mio_eof ,
198stop working (i.e. always return 0).
199.Sh RETURN VALUES
200The
201.Fn mio_open
202function returns the newly created handle on success or NULL
203on failure.
204.Pp
205The
206.Fn mio_pollfd
207function returns the number of
208.Va pollfd
209structures filled.
210The
211.Fn mio_nfds
212function returns the number of
213.Va pollfd
214structures the caller must preallocate in order to be sure
215that
216.Fn mio_pollfd
217will never overrun.
218.Pp
219The
220.Fn mio_revents
221function returns the bit-mask set by
222.Xr poll 2
223in the
224.Va pfd
225array of
226.Va pollfd
227structures.
228.Pp
229The
230.Fn mio_read
231and
232.Fn mio_write
233functions return the number of bytes transferred.
234.Pp
235The
236.Fn mio_eof
237function returns 0 if there's no pending error, and a non-zero
238value if there's an error.
239.Sh ENVIRONMENT
240.Bl -tag -width "SNDIO_DEBUGXXX" -compact
241.It Ev SNDIO_DEBUG
242The debug level:
243may be a value between 0 and 2.
244.El
245.Sh SEE ALSO
246.Xr poll 2 ,
247.Xr midi 4 ,
248.Xr sndio 7 ,
249.Xr sndiod 8
250.Sh HISTORY
251These functions first appeared in
252.Ox 4.7 .
253.Sh AUTHORS
254.An Alexandre Ratchov Aq Mt ratchov@openbsd.org
255