johnallsup/alsa-rawmidi — comment on commit 4bd987db90616b6bebc38514b6a187728e425315
File:  line 0
By johnallsup:

Hi Ari,

In answer to your query,

*Upon success, err is positive and equal to the number of bytes read
into the buffer.*

From the alsa-lib sourcecode (rawmidi_hw.c) the snd_rawmidi_hw_read
function is
called by snd_rawmidi_read for a raw midi interface.

static ssize_t snd_rawmidi_hw_read(snd_rawmidi_t *rmidi, void *buffer,
size_t size)
{
snd_rawmidi_hw_t *hw = rmidi->private_data;
ssize_t result;
result = read(hw->fd, buffer, size);
if (result < 0)
return -errno;
return result;
}

Note the last three lines: if the result of read() is positive, it
returns that result.

From man -s2 read:

SYNOPSIS
#include

ssize_t read(int fd, void *buf, size_t count);

DESCRIPTION
read() attempts to read up to count bytes from file
descriptor fd into the buffer starting at buf.

RETURN VALUE
On success, the number of bytes read is returned (zero
indicates end of file), and the file position is advanced by this
number. It is not an error if this number is smaller
than the number of bytes requested; this may happen for
example because fewer bytes are actually available right now (maybe
because we were close to end-of-file, or because we
are reading from a pipe, or from a terminal), or because
read() was interrupted by a signal. On error, -1 is returned, and
errno is set appropriately. In this case it is left
unspecified whether the file position (if any) changes.

So the read() call returns the number of bytes read from the MIDI
device, upon success, and snd_rawmidi_read returns that same number.

Thus we pass err to get_bytes.

Hope this makes it clear.

Cheers,

John

At least that's On 06/01/12 01:23, Ari Russo wrote:
> wait a second, why did you pass err to get_bytes? isn't err an ALSA error code? I think I may have had that particular part right...
>
> ---
> Reply to this email directly or view it on GitHub:
> https://github.com/johnallsup/alsa-rawmidi/commit/4bd987db90616b6bebc38514b6a187728e425315#commitcomment-842332