alsa-lib/src/rawmidi/rawmidi.c

212 lines
4.9 KiB
C
Raw Normal View History

1998-08-27 20:47:51 +00:00
/*
* RawMIDI Interface - main file
* Copyright (c) 1998 by Jaroslav Kysela <perex@suse.cz>
1998-08-27 20:47:51 +00:00
*
*
* This library is free software; you can redistribute it and/or modify
* it under the terms of the GNU Library General Public License as
* published by the Free Software Foundation; either version 2 of
* the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include <fcntl.h>
#include <sys/ioctl.h>
1998-08-30 21:08:44 +00:00
#include "asoundlib.h"
1998-08-27 20:47:51 +00:00
#define SND_FILE_RAWMIDI "/dev/snd/midiC%iD%i"
#define SND_RAWMIDI_VERSION_MAX SND_PROTOCOL_VERSION(2, 0, 0)
1998-11-27 14:57:39 +00:00
struct snd_rawmidi {
1998-11-27 14:57:39 +00:00
int card;
int device;
int fd;
1999-12-05 21:12:24 +00:00
int mode;
};
1998-11-27 14:57:39 +00:00
int snd_rawmidi_open(snd_rawmidi_t **handle, int card, int device, int mode)
1998-08-27 20:47:51 +00:00
{
1998-11-27 14:57:39 +00:00
int fd, ver;
char filename[32];
snd_rawmidi_t *rmidi;
*handle = NULL;
1999-01-30 18:35:52 +00:00
1998-11-27 14:57:39 +00:00
if (card < 0 || card >= SND_CARDS)
return -EINVAL;
sprintf(filename, SND_FILE_RAWMIDI, card, device);
1999-01-30 18:35:52 +00:00
if ((fd = open(filename, mode)) < 0) {
snd_card_load(card);
if ((fd = open(filename, mode)) < 0)
return -errno;
}
1998-11-27 14:57:39 +00:00
if (ioctl(fd, SND_RAWMIDI_IOCTL_PVERSION, &ver) < 0) {
close(fd);
return -errno;
}
1999-07-22 10:49:39 +00:00
if (SND_PROTOCOL_INCOMPATIBLE(ver, SND_RAWMIDI_VERSION_MAX)) {
1998-11-27 14:57:39 +00:00
close(fd);
1999-07-22 10:49:39 +00:00
return -SND_ERROR_INCOMPATIBLE_VERSION;
1998-11-27 14:57:39 +00:00
}
rmidi = (snd_rawmidi_t *) calloc(1, sizeof(snd_rawmidi_t));
if (rmidi == NULL) {
close(fd);
return -ENOMEM;
}
rmidi->card = card;
rmidi->device = device;
rmidi->fd = fd;
1999-12-05 21:12:24 +00:00
rmidi->mode = mode;
1998-11-27 14:57:39 +00:00
*handle = rmidi;
return 0;
1998-08-27 20:47:51 +00:00
}
1999-12-05 21:12:24 +00:00
int snd_rawmidi_close(snd_rawmidi_t *rmidi)
1998-08-27 20:47:51 +00:00
{
1998-11-27 14:57:39 +00:00
int res;
if (!rmidi)
return -EINVAL;
res = close(rmidi->fd) < 0 ? -errno : 0;
free(rmidi);
return res;
1998-08-27 20:47:51 +00:00
}
1999-12-05 21:12:24 +00:00
int snd_rawmidi_file_descriptor(snd_rawmidi_t *rmidi)
1998-08-27 20:47:51 +00:00
{
1998-11-27 14:57:39 +00:00
if (!rmidi)
return -EINVAL;
return rmidi->fd;
1998-08-27 20:47:51 +00:00
}
1999-12-05 21:12:24 +00:00
int snd_rawmidi_block_mode(snd_rawmidi_t *rmidi, int enable)
1998-08-27 20:47:51 +00:00
{
1998-11-27 14:57:39 +00:00
long flags;
if (!rmidi)
return -EINVAL;
1999-12-05 21:12:24 +00:00
if (rmidi->mode == SND_RAWMIDI_OPEN_OUTPUT_APPEND ||
rmidi->mode == SND_RAWMIDI_OPEN_DUPLEX_APPEND)
return -EINVAL;
1999-01-06 15:25:15 +00:00
if ((flags = fcntl(rmidi->fd, F_GETFL)) < 0)
1998-11-27 14:57:39 +00:00
return -errno;
if (enable)
flags &= ~O_NONBLOCK;
1999-01-13 14:51:21 +00:00
else
flags |= O_NONBLOCK;
1999-01-06 15:25:15 +00:00
if (fcntl(rmidi->fd, F_SETFL, flags) < 0)
1998-11-27 14:57:39 +00:00
return -errno;
return 0;
1998-08-27 20:47:51 +00:00
}
1999-12-05 21:12:24 +00:00
int snd_rawmidi_info(snd_rawmidi_t *rmidi, snd_rawmidi_info_t * info)
1998-08-27 20:47:51 +00:00
{
1998-12-27 01:01:47 +00:00
if (!rmidi || !info)
1998-11-27 14:57:39 +00:00
return -EINVAL;
if (ioctl(rmidi->fd, SND_RAWMIDI_IOCTL_INFO, info) < 0)
return -errno;
return 0;
1998-08-27 20:47:51 +00:00
}
int snd_rawmidi_stream_params(snd_rawmidi_t *rmidi, snd_rawmidi_params_t * params)
1998-08-27 20:47:51 +00:00
{
1998-12-27 01:01:47 +00:00
if (!rmidi || !params)
1998-11-27 14:57:39 +00:00
return -EINVAL;
if (params->stream < 0 || params->stream > 1)
1998-11-27 14:57:39 +00:00
return -EINVAL;
if (ioctl(rmidi->fd, SND_RAWMIDI_IOCTL_STREAM_PARAMS, params) < 0)
1998-11-27 14:57:39 +00:00
return -errno;
return 0;
1998-08-27 20:47:51 +00:00
}
int snd_rawmidi_stream_setup(snd_rawmidi_t *rmidi, snd_rawmidi_setup_t * setup)
{
if (!rmidi || !setup)
return -EINVAL;
if (setup->stream < 0 || setup->stream > 1)
return -EINVAL;
if (ioctl(rmidi->fd, SND_RAWMIDI_IOCTL_STREAM_SETUP, setup) < 0)
return -errno;
return 0;
}
int snd_rawmidi_stream_status(snd_rawmidi_t *rmidi, snd_rawmidi_status_t * status)
1998-08-27 20:47:51 +00:00
{
1998-12-27 01:01:47 +00:00
if (!rmidi || !status)
1998-11-27 14:57:39 +00:00
return -EINVAL;
if (status->stream < 0 || status->stream > 1)
2000-01-09 22:44:52 +00:00
return -EINVAL;
if (ioctl(rmidi->fd, SND_RAWMIDI_IOCTL_STREAM_STATUS, status) < 0)
1998-11-27 14:57:39 +00:00
return -errno;
return 0;
1998-08-27 20:47:51 +00:00
}
2000-01-09 22:44:52 +00:00
int snd_rawmidi_output_drain(snd_rawmidi_t *rmidi)
1998-08-27 20:47:51 +00:00
{
int str = SND_RAWMIDI_STREAM_OUTPUT;
2000-01-09 22:44:52 +00:00
if (!rmidi)
1998-11-27 14:57:39 +00:00
return -EINVAL;
if (ioctl(rmidi->fd, SND_RAWMIDI_IOCTL_STREAM_DRAIN, &str) < 0)
1998-11-27 14:57:39 +00:00
return -errno;
return 0;
1998-08-27 20:47:51 +00:00
}
int snd_rawmidi_stream_flush(snd_rawmidi_t *rmidi, int str)
1998-08-27 20:47:51 +00:00
{
1998-11-27 14:57:39 +00:00
if (!rmidi)
return -EINVAL;
if (str < 0 || str > 1)
2000-01-09 22:44:52 +00:00
return -EINVAL;
if (ioctl(rmidi->fd, SND_RAWMIDI_IOCTL_STREAM_FLUSH, &str) < 0)
1998-11-27 14:57:39 +00:00
return -errno;
return 0;
1998-08-27 20:47:51 +00:00
}
2000-01-09 22:44:52 +00:00
int snd_rawmidi_output_flush(snd_rawmidi_t *rmidi)
1998-08-27 20:47:51 +00:00
{
return snd_rawmidi_stream_flush(rmidi, SND_RAWMIDI_STREAM_OUTPUT);
1998-08-27 20:47:51 +00:00
}
2000-01-09 22:44:52 +00:00
int snd_rawmidi_input_flush(snd_rawmidi_t *rmidi)
1998-08-27 20:47:51 +00:00
{
return snd_rawmidi_stream_flush(rmidi, SND_RAWMIDI_STREAM_INPUT);
1998-08-27 20:47:51 +00:00
}
1999-12-05 21:12:24 +00:00
ssize_t snd_rawmidi_write(snd_rawmidi_t *rmidi, const void *buffer, size_t size)
1998-08-27 20:47:51 +00:00
{
1998-11-27 14:57:39 +00:00
ssize_t result;
2000-05-08 18:53:38 +00:00
if (!rmidi || (!buffer && size > 0))
1998-11-27 14:57:39 +00:00
return -EINVAL;
result = write(rmidi->fd, buffer, size);
if (result < 0)
return -errno;
return result;
1998-08-27 20:47:51 +00:00
}
1999-12-05 21:12:24 +00:00
ssize_t snd_rawmidi_read(snd_rawmidi_t *rmidi, void *buffer, size_t size)
1998-08-27 20:47:51 +00:00
{
1998-11-27 14:57:39 +00:00
ssize_t result;
2000-05-08 18:53:38 +00:00
if (!rmidi || (!buffer && size > 0))
1998-11-27 14:57:39 +00:00
return -EINVAL;
result = read(rmidi->fd, buffer, size);
if (result < 0)
return -errno;
return result;
1998-08-27 20:47:51 +00:00
}