alsa-lib/src/rawmidi/rawmidi.c

242 lines
5.4 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_subdevice(snd_rawmidi_t **handle, int card, int device, int subdevice, int mode)
1998-08-27 20:47:51 +00:00
{
int fd, ver, ret;
int attempt = 0;
1998-11-27 14:57:39 +00:00
char filename[32];
snd_ctl_t *ctl;
1998-11-27 14:57:39 +00:00
snd_rawmidi_t *rmidi;
snd_rawmidi_info_t info;
1998-11-27 14:57:39 +00:00
*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;
if ((ret = snd_ctl_hw_open(&ctl, NULL, card)) < 0)
return ret;
1998-11-27 14:57:39 +00:00
sprintf(filename, SND_FILE_RAWMIDI, card, device);
__again:
if (attempt++ > 3) {
snd_ctl_close(ctl);
return -EBUSY;
}
ret = snd_ctl_rawmidi_prefer_subdevice(ctl, subdevice);
if (ret < 0) {
snd_ctl_close(ctl);
return ret;
}
1999-01-30 18:35:52 +00:00
if ((fd = open(filename, mode)) < 0) {
snd_card_load(card);
if ((fd = open(filename, mode)) < 0) {
snd_ctl_close(ctl);
1999-01-30 18:35:52 +00:00
return -errno;
}
1999-01-30 18:35:52 +00:00
}
1998-11-27 14:57:39 +00:00
if (ioctl(fd, SND_RAWMIDI_IOCTL_PVERSION, &ver) < 0) {
ret = -errno;
1998-11-27 14:57:39 +00:00
close(fd);
snd_ctl_close(ctl);
return ret;
1998-11-27 14:57:39 +00:00
}
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);
snd_ctl_close(ctl);
1999-07-22 10:49:39 +00:00
return -SND_ERROR_INCOMPATIBLE_VERSION;
1998-11-27 14:57:39 +00:00
}
if (subdevice >= 0) {
memset(&info, 0, sizeof(info));
if (ioctl(fd, SND_RAWMIDI_IOCTL_INFO, &info) < 0) {
ret = -errno;
close(fd);
snd_ctl_close(ctl);
return ret;
}
if (info.subdevice != subdevice) {
close(fd);
goto __again;
}
}
1998-11-27 14:57:39 +00:00
rmidi = (snd_rawmidi_t *) calloc(1, sizeof(snd_rawmidi_t));
if (rmidi == NULL) {
close(fd);
snd_ctl_close(ctl);
1998-11-27 14:57:39 +00:00
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
}
int snd_rawmidi_open(snd_rawmidi_t **handle, int card, int device, int mode)
{
return snd_rawmidi_open_subdevice(handle, card, device, -1, mode);
}
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
}
2000-09-24 09:57:26 +00:00
int snd_rawmidi_poll_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_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_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_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_STATUS, status) < 0)
1998-11-27 14:57:39 +00:00
return -errno;
return 0;
1998-08-27 20:47:51 +00:00
}
int snd_rawmidi_output_drop(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_DROP, &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_drain(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_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_output_drain(snd_rawmidi_t *rmidi)
1998-08-27 20:47:51 +00:00
{
return snd_rawmidi_stream_drain(rmidi, SND_RAWMIDI_STREAM_OUTPUT);
1998-08-27 20:47:51 +00:00
}
int snd_rawmidi_input_drain(snd_rawmidi_t *rmidi)
1998-08-27 20:47:51 +00:00
{
return snd_rawmidi_stream_drain(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
}