1.\" $OpenBSD: mio_open.3,v 1.4 2011/04/16 10:52:22 ratchov 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: April 16 2011 $ 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 interface to MIDI streams 30.Sh SYNOPSIS 31.Fd #include <sndio.h> 32.Ft "struct mio_hdl *" 33.Fn "mio_open" "const char *name" "unsigned 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 midicat 1 55MIDI thru boxes in a uniform way. 56.Ss Opening and closing an 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.Fn mio_open 65function tries to connect to the 66.Xr midicat 1 67software MIDI thru box or to use the 68.Xr midi 4 69hardware device. 70The 71.Ar name 72parameter gives the device string discussed in 73.Xr sndio 7 . 74If the program is using a single device and is providing no device chooser, 75it should be set to NULL to allow the user to select it using the 76.Ev MIDIDEVICE 77environment variable. 78.Pp 79The 80.Ar mode 81parameter gives the direction of the stream. 82The following are supported: 83.Bl -tag -width "MIO_OUT | MIO_IN" 84.It MIO_OUT 85The stream is output-only; data written to the stream will be sent 86to the hardware or other programs. 87.It MIO_IN 88The stream is input-only; received data from the hardware or 89other programs must be read from the stream. 90.It MIO_IN | MIO_OUT 91The stream sends and receives data. 92This mode should be used rather than calling 93.Fn mio_open 94twice. 95.El 96.Pp 97If the 98.Ar nbio_flag 99argument is true (i.e. non-zero), then the 100.Fn mio_read 101and 102.Fn mio_write 103functions (see below) will be non-blocking. 104.Pp 105The 106.Fn mio_close 107function closes the stream and frees all allocated resources 108associated with the 109.Nm libsndio 110handle. 111.Ss Sending and receiving data 112When input mode is selected, the 113.Fn mio_read 114function must be called to retrieve received data; it must be called 115often enough to ensure that internal buffers will not overrun. 116It will store at most 117.Ar nbytes 118bytes at the 119.Ar addr 120location and return the number of bytes stored. 121Unless the 122.Ar nbio_flag 123flag is set, it will block until data becomes available and 124will return zero only on error. 125.Pp 126When output mode is selected, the 127.Fn mio_write 128function can be called to provide data to transmit. 129Unless the 130.Ar nbio_flag 131is set, 132.Fn mio_write 133will block until the requested amount of data is written. 134.Ss Non-blocking mode operation 135If the 136.Ar nbio_flag 137is set on 138.Fn mio_open , 139then the 140.Fn mio_read 141and 142.Fn mio_write 143functions will never block; if no data is available, they will 144return zero immediately. 145.Pp 146To avoid busy loops when non-blocking mode is used, the 147.Xr poll 2 148system call can be used to check if data can be 149read from or written to the stream. 150The 151.Fn mio_pollfd 152function fills the array 153.Ar pfd 154of 155.Va pollfd 156structures, used by 157.Xr poll 2 , 158with 159.Ar events ; 160the latter is a bit-mask of 161.Va POLLIN 162and 163.Va POLLOUT 164constants; refer to 165.Xr poll 2 166for more details. 167.Fn mio_pollfd 168returns the number of 169.Va pollfd 170structures filled. 171The 172.Fn mio_revents 173function returns the bit-mask set by 174.Xr poll 2 175in the 176.Va pfd 177array of 178.Va pollfd 179structures. 180If 181.Va POLLIN 182is set, 183.Fn mio_read 184can be called without blocking. 185If 186.Va POLLOUT 187is set, 188.Fn mio_write 189can be called without blocking. 190POLLHUP may be set if an error occurs, even if 191it is not selected with 192.Fn mio_pollfd . 193.Pp 194The 195.Fn mio_nfds 196function returns the number of 197.Va pollfd 198structures the caller must preallocate in order to be sure 199that 200.Fn mio_pollfd 201will never overrun. 202.Ss Error handling 203Errors related to the MIDI subsystem 204(like hardware errors or dropped connections) and 205programming errors (such as a call to 206.Fn mio_read 207on a play-only stream) are considered fatal. 208Once an error occurs, all functions which take a 209.Va mio_hdl 210argument, except 211.Fn mio_close 212and 213.Fn mio_eof , 214stop working (i.e. always return 0). 215.Pp 216The 217.Fn mio_eof 218function can be used at any stage; 219it returns 0 if there's no pending error, and a non-zero 220value if there's an error. 221.Sh RETURN VALUES 222The 223.Fn mio_open 224function returns the newly created handle on success or NULL 225on failure. 226The 227.Fn mio_pollfd 228function returns 1 on success and 0 on failure. 229The 230.Fn mio_read 231and 232.Fn mio_write 233functions return the number of bytes transferred. 234.Sh ENVIRONMENT 235.Bl -tag -width "MIO_DEBUGXXX" -compact 236.It Ev SNDIO_DEBUG 237The debug level: 238may be a value between 0 and 2. 239.El 240.Sh FILES 241.Bl -tag -width "/tmp/aucat-<uid>/midithru0" -compact 242.It Pa /tmp/aucat-<uid>/midithru0 243Default path to 244.Xr midicat 1 245socket to connect to. 246.It Pa /dev/rmidiX 247.Xr midi 4 248devices. 249.El 250.Sh SEE ALSO 251.Xr midicat 1 , 252.Xr midi 4 , 253.Xr sndio 7 254