mirror of
https://github.com/alsa-project/alsa-lib.git
synced 2025-11-04 13:30:08 -05:00
Merged pcmfinal branch.
This commit is contained in:
parent
3cc2b957fb
commit
41bb7068f2
57 changed files with 5189 additions and 3088 deletions
|
|
@ -1,6 +1,8 @@
|
|||
EXTRA_LTLIBRARIES=librawmidi.la
|
||||
|
||||
librawmidi_la_SOURCES = rawmidi.c
|
||||
librawmidi_la_SOURCES = rawmidi.c rawmidi_hw.c
|
||||
noinst_HEADERS = rawmidi_local.h
|
||||
|
||||
all: librawmidi.la
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -1,7 +1,7 @@
|
|||
/*
|
||||
* RawMIDI Interface - main file
|
||||
* Copyright (c) 1998 by Jaroslav Kysela <perex@suse.cz>
|
||||
*
|
||||
* Copyright (c) 2000 by Jaroslav Kysela <perex@suse.cz>
|
||||
* Abramo Bagnara <abramo@alsa-project.org>
|
||||
*
|
||||
* This library is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Library General Public License as
|
||||
|
|
@ -21,221 +21,214 @@
|
|||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdarg.h>
|
||||
#include <unistd.h>
|
||||
#include <string.h>
|
||||
#include <errno.h>
|
||||
#include <fcntl.h>
|
||||
#include <sys/ioctl.h>
|
||||
#include <dlfcn.h>
|
||||
#include "rawmidi_local.h"
|
||||
#include "asoundlib.h"
|
||||
|
||||
#define SND_FILE_RAWMIDI "/dev/snd/midiC%iD%i"
|
||||
#define SND_RAWMIDI_VERSION_MAX SND_PROTOCOL_VERSION(2, 0, 0)
|
||||
|
||||
struct snd_rawmidi {
|
||||
int card;
|
||||
int device;
|
||||
int fd;
|
||||
int mode;
|
||||
};
|
||||
|
||||
int snd_rawmidi_open_subdevice(snd_rawmidi_t **handle, int card, int device, int subdevice, int mode)
|
||||
{
|
||||
int fd, ver, ret;
|
||||
int attempt = 0;
|
||||
char filename[32];
|
||||
snd_ctl_t *ctl;
|
||||
snd_rawmidi_t *rmidi;
|
||||
snd_rawmidi_info_t info;
|
||||
|
||||
*handle = NULL;
|
||||
|
||||
if (card < 0 || card >= SND_CARDS)
|
||||
return -EINVAL;
|
||||
|
||||
if ((ret = snd_ctl_hw_open(&ctl, NULL, card)) < 0)
|
||||
return ret;
|
||||
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;
|
||||
}
|
||||
if ((fd = open(filename, mode)) < 0) {
|
||||
snd_card_load(card);
|
||||
if ((fd = open(filename, mode)) < 0) {
|
||||
snd_ctl_close(ctl);
|
||||
return -errno;
|
||||
}
|
||||
}
|
||||
if (ioctl(fd, SND_RAWMIDI_IOCTL_PVERSION, &ver) < 0) {
|
||||
ret = -errno;
|
||||
close(fd);
|
||||
snd_ctl_close(ctl);
|
||||
return ret;
|
||||
}
|
||||
if (SND_PROTOCOL_INCOMPATIBLE(ver, SND_RAWMIDI_VERSION_MAX)) {
|
||||
close(fd);
|
||||
snd_ctl_close(ctl);
|
||||
return -SND_ERROR_INCOMPATIBLE_VERSION;
|
||||
}
|
||||
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;
|
||||
}
|
||||
}
|
||||
rmidi = (snd_rawmidi_t *) calloc(1, sizeof(snd_rawmidi_t));
|
||||
if (rmidi == NULL) {
|
||||
close(fd);
|
||||
snd_ctl_close(ctl);
|
||||
return -ENOMEM;
|
||||
}
|
||||
rmidi->card = card;
|
||||
rmidi->device = device;
|
||||
rmidi->fd = fd;
|
||||
rmidi->mode = mode;
|
||||
*handle = rmidi;
|
||||
return 0;
|
||||
}
|
||||
|
||||
int snd_rawmidi_open(snd_rawmidi_t **handle, int card, int device, int mode)
|
||||
{
|
||||
return snd_rawmidi_open_subdevice(handle, card, device, -1, mode);
|
||||
}
|
||||
|
||||
int snd_rawmidi_close(snd_rawmidi_t *rmidi)
|
||||
{
|
||||
int res;
|
||||
|
||||
if (!rmidi)
|
||||
return -EINVAL;
|
||||
res = close(rmidi->fd) < 0 ? -errno : 0;
|
||||
int err;
|
||||
assert(rmidi);
|
||||
if ((err = rmidi->ops->close(rmidi)) < 0)
|
||||
return err;
|
||||
if (rmidi->name)
|
||||
free(rmidi->name);
|
||||
free(rmidi);
|
||||
return res;
|
||||
return 0;
|
||||
}
|
||||
|
||||
int snd_rawmidi_poll_descriptor(snd_rawmidi_t *rmidi)
|
||||
{
|
||||
if (!rmidi)
|
||||
return -EINVAL;
|
||||
return rmidi->fd;
|
||||
assert(rmidi);
|
||||
return rmidi->poll_fd;
|
||||
}
|
||||
|
||||
int snd_rawmidi_block_mode(snd_rawmidi_t *rmidi, int enable)
|
||||
int snd_rawmidi_nonblock(snd_rawmidi_t *rmidi, int nonblock)
|
||||
{
|
||||
long flags;
|
||||
|
||||
if (!rmidi)
|
||||
return -EINVAL;
|
||||
if (rmidi->mode == SND_RAWMIDI_OPEN_OUTPUT_APPEND ||
|
||||
rmidi->mode == SND_RAWMIDI_OPEN_DUPLEX_APPEND)
|
||||
return -EINVAL;
|
||||
if ((flags = fcntl(rmidi->fd, F_GETFL)) < 0)
|
||||
return -errno;
|
||||
if (enable)
|
||||
flags &= ~O_NONBLOCK;
|
||||
int err;
|
||||
assert(rmidi);
|
||||
assert(!(rmidi->mode & SND_RAWMIDI_APPEND));
|
||||
if ((err = rmidi->ops->nonblock(rmidi, nonblock)) < 0)
|
||||
return err;
|
||||
if (nonblock)
|
||||
rmidi->mode |= SND_RAWMIDI_NONBLOCK;
|
||||
else
|
||||
flags |= O_NONBLOCK;
|
||||
if (fcntl(rmidi->fd, F_SETFL, flags) < 0)
|
||||
return -errno;
|
||||
rmidi->mode &= ~SND_RAWMIDI_NONBLOCK;
|
||||
return 0;
|
||||
}
|
||||
|
||||
int snd_rawmidi_info(snd_rawmidi_t *rmidi, snd_rawmidi_info_t * info)
|
||||
{
|
||||
if (!rmidi || !info)
|
||||
return -EINVAL;
|
||||
if (ioctl(rmidi->fd, SND_RAWMIDI_IOCTL_INFO, info) < 0)
|
||||
return -errno;
|
||||
return 0;
|
||||
assert(rmidi && info);
|
||||
return rmidi->ops->info(rmidi, info);
|
||||
}
|
||||
|
||||
int snd_rawmidi_params(snd_rawmidi_t *rmidi, snd_rawmidi_params_t * params)
|
||||
{
|
||||
if (!rmidi || !params)
|
||||
return -EINVAL;
|
||||
if (params->stream < 0 || params->stream > 1)
|
||||
return -EINVAL;
|
||||
if (ioctl(rmidi->fd, SND_RAWMIDI_IOCTL_PARAMS, params) < 0)
|
||||
return -errno;
|
||||
return 0;
|
||||
assert(rmidi && params);
|
||||
return rmidi->ops->params(rmidi, params);
|
||||
}
|
||||
|
||||
int snd_rawmidi_status(snd_rawmidi_t *rmidi, snd_rawmidi_status_t * status)
|
||||
{
|
||||
if (!rmidi || !status)
|
||||
return -EINVAL;
|
||||
if (status->stream < 0 || status->stream > 1)
|
||||
return -EINVAL;
|
||||
if (ioctl(rmidi->fd, SND_RAWMIDI_IOCTL_STATUS, status) < 0)
|
||||
return -errno;
|
||||
return 0;
|
||||
assert(rmidi && status);
|
||||
return rmidi->ops->status(rmidi, status);
|
||||
}
|
||||
|
||||
int snd_rawmidi_drop(snd_rawmidi_t *rmidi, int str)
|
||||
{
|
||||
assert(rmidi);
|
||||
assert(str >= 0 && str <= 1);
|
||||
assert(rmidi->streams & (1 << str));
|
||||
return rmidi->ops->drop(rmidi, str);
|
||||
}
|
||||
|
||||
int snd_rawmidi_output_drop(snd_rawmidi_t *rmidi)
|
||||
{
|
||||
int str = SND_RAWMIDI_STREAM_OUTPUT;
|
||||
if (!rmidi)
|
||||
return -EINVAL;
|
||||
if (ioctl(rmidi->fd, SND_RAWMIDI_IOCTL_DROP, &str) < 0)
|
||||
return -errno;
|
||||
return 0;
|
||||
return snd_rawmidi_drop(rmidi, SND_RAWMIDI_STREAM_OUTPUT);
|
||||
}
|
||||
|
||||
int snd_rawmidi_stream_drain(snd_rawmidi_t *rmidi, int str)
|
||||
int snd_rawmidi_drain(snd_rawmidi_t *rmidi, int str)
|
||||
{
|
||||
if (!rmidi)
|
||||
return -EINVAL;
|
||||
if (str < 0 || str > 1)
|
||||
return -EINVAL;
|
||||
if (ioctl(rmidi->fd, SND_RAWMIDI_IOCTL_DRAIN, &str) < 0)
|
||||
return -errno;
|
||||
return 0;
|
||||
assert(rmidi);
|
||||
assert(str >= 0 && str <= 1);
|
||||
assert(rmidi->streams & (1 << str));
|
||||
return rmidi->ops->drain(rmidi, str);
|
||||
}
|
||||
|
||||
int snd_rawmidi_output_drain(snd_rawmidi_t *rmidi)
|
||||
{
|
||||
return snd_rawmidi_stream_drain(rmidi, SND_RAWMIDI_STREAM_OUTPUT);
|
||||
return snd_rawmidi_drain(rmidi, SND_RAWMIDI_STREAM_OUTPUT);
|
||||
}
|
||||
|
||||
int snd_rawmidi_input_drain(snd_rawmidi_t *rmidi)
|
||||
{
|
||||
return snd_rawmidi_stream_drain(rmidi, SND_RAWMIDI_STREAM_INPUT);
|
||||
return snd_rawmidi_drain(rmidi, SND_RAWMIDI_STREAM_INPUT);
|
||||
}
|
||||
|
||||
ssize_t snd_rawmidi_write(snd_rawmidi_t *rmidi, const void *buffer, size_t size)
|
||||
{
|
||||
ssize_t result;
|
||||
|
||||
if (!rmidi || (!buffer && size > 0))
|
||||
return -EINVAL;
|
||||
result = write(rmidi->fd, buffer, size);
|
||||
if (result < 0)
|
||||
return -errno;
|
||||
return result;
|
||||
assert(rmidi && (buffer || size == 0));
|
||||
return rmidi->ops->write(rmidi, buffer, size);
|
||||
}
|
||||
|
||||
ssize_t snd_rawmidi_read(snd_rawmidi_t *rmidi, void *buffer, size_t size)
|
||||
{
|
||||
ssize_t result;
|
||||
|
||||
if (!rmidi || (!buffer && size > 0))
|
||||
return -EINVAL;
|
||||
result = read(rmidi->fd, buffer, size);
|
||||
if (result < 0)
|
||||
return -errno;
|
||||
return result;
|
||||
assert(rmidi && (buffer || size == 0));
|
||||
return rmidi->ops->read(rmidi, buffer, size);
|
||||
}
|
||||
|
||||
int snd_rawmidi_open(snd_rawmidi_t **rawmidip, char *name,
|
||||
int streams, int mode)
|
||||
{
|
||||
char *str;
|
||||
int err;
|
||||
snd_config_t *rawmidi_conf, *conf, *type_conf;
|
||||
snd_config_iterator_t i;
|
||||
char *lib = NULL, *open = NULL;
|
||||
int (*open_func)(snd_rawmidi_t **rawmidip, char *name, snd_config_t *conf,
|
||||
int streams, int mode);
|
||||
void *h;
|
||||
assert(rawmidip && name);
|
||||
err = snd_config_update();
|
||||
if (err < 0)
|
||||
return err;
|
||||
err = snd_config_searchv(snd_config, &rawmidi_conf, "rawmidi", name, 0);
|
||||
if (err < 0) {
|
||||
int card, dev, subdev;
|
||||
err = sscanf(name, "hw:%d,%d,%d", &card, &dev, &subdev);
|
||||
if (err == 3)
|
||||
return snd_rawmidi_hw_open(rawmidip, name, card, dev, subdev, streams, mode);
|
||||
err = sscanf(name, "hw:%d,%d", &card, &dev);
|
||||
if (err == 2)
|
||||
return snd_rawmidi_hw_open(rawmidip, name, card, dev, -1, streams, mode);
|
||||
ERR("Unknown RAWMIDI %s", name);
|
||||
return -ENOENT;
|
||||
}
|
||||
if (snd_config_type(rawmidi_conf) != SND_CONFIG_TYPE_COMPOUND) {
|
||||
ERR("Invalid type for RAWMIDI definition");
|
||||
return -EINVAL;
|
||||
}
|
||||
err = snd_config_search(rawmidi_conf, "streams", &conf);
|
||||
if (err >= 0) {
|
||||
err = snd_config_string_get(conf, &str);
|
||||
if (err < 0) {
|
||||
ERR("Invalid type for streams");
|
||||
return err;
|
||||
}
|
||||
if (strcmp(str, "output") == 0) {
|
||||
if (streams == SND_RAWMIDI_OPEN_INPUT)
|
||||
return -EINVAL;
|
||||
} else if (strcmp(str, "input") == 0) {
|
||||
if (streams == SND_RAWMIDI_OPEN_OUTPUT)
|
||||
return -EINVAL;
|
||||
} else if (strcmp(str, "duplex") == 0) {
|
||||
if (streams != SND_RAWMIDI_OPEN_DUPLEX)
|
||||
return -EINVAL;
|
||||
} else {
|
||||
ERR("Invalid value for streams");
|
||||
return -EINVAL;
|
||||
}
|
||||
}
|
||||
err = snd_config_search(rawmidi_conf, "type", &conf);
|
||||
if (err < 0) {
|
||||
ERR("type is not defined");
|
||||
return err;
|
||||
}
|
||||
err = snd_config_string_get(conf, &str);
|
||||
if (err < 0) {
|
||||
ERR("Invalid type for type");
|
||||
return err;
|
||||
}
|
||||
err = snd_config_searchv(snd_config, &type_conf, "rawmiditype", str, 0);
|
||||
if (err < 0) {
|
||||
ERR("Unknown RAWMIDI type %s", str);
|
||||
return err;
|
||||
}
|
||||
snd_config_foreach(i, type_conf) {
|
||||
snd_config_t *n = snd_config_entry(i);
|
||||
if (strcmp(n->id, "comment") == 0)
|
||||
continue;
|
||||
if (strcmp(n->id, "lib") == 0) {
|
||||
err = snd_config_string_get(n, &lib);
|
||||
if (err < 0) {
|
||||
ERR("Invalid type for lib");
|
||||
return -EINVAL;
|
||||
}
|
||||
continue;
|
||||
}
|
||||
if (strcmp(n->id, "open") == 0) {
|
||||
err = snd_config_string_get(n, &open);
|
||||
if (err < 0) {
|
||||
ERR("Invalid type for open");
|
||||
return -EINVAL;
|
||||
}
|
||||
continue;
|
||||
ERR("Unknown field: %s", n->id);
|
||||
return -EINVAL;
|
||||
}
|
||||
}
|
||||
if (!open) {
|
||||
ERR("open is not defined");
|
||||
return -EINVAL;
|
||||
}
|
||||
if (!lib)
|
||||
lib = "libasound.so";
|
||||
h = dlopen(lib, RTLD_NOW);
|
||||
if (!h) {
|
||||
ERR("Cannot open shared library %s", lib);
|
||||
return -ENOENT;
|
||||
}
|
||||
open_func = dlsym(h, open);
|
||||
dlclose(h);
|
||||
if (!open_func) {
|
||||
ERR("symbol %s is not defined inside %s", open, lib);
|
||||
return -ENXIO;
|
||||
}
|
||||
return open_func(rawmidip, name, rawmidi_conf, streams, mode);
|
||||
}
|
||||
|
||||
|
|
|
|||
320
src/rawmidi/rawmidi_hw.c
Normal file
320
src/rawmidi/rawmidi_hw.c
Normal file
|
|
@ -0,0 +1,320 @@
|
|||
/*
|
||||
* RawMIDI - Hardware
|
||||
* Copyright (c) 2000 by Jaroslav Kysela <perex@suse.cz>
|
||||
* Abramo Bagnara <abramo@alsa-project.org>
|
||||
*
|
||||
*
|
||||
* 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>
|
||||
#include "../control/control_local.h"
|
||||
#include "rawmidi_local.h"
|
||||
#include "asoundlib.h"
|
||||
|
||||
#define SND_FILE_RAWMIDI "/dev/snd/midiC%iD%i"
|
||||
#define SND_RAWMIDI_VERSION_MAX SND_PROTOCOL_VERSION(2, 0, 0)
|
||||
|
||||
typedef struct {
|
||||
int fd;
|
||||
int card, device, subdevice;
|
||||
} snd_rawmidi_hw_t;
|
||||
|
||||
static int snd_rawmidi_hw_close(snd_rawmidi_t *rmidi)
|
||||
{
|
||||
snd_rawmidi_hw_t *hw = rmidi->private;
|
||||
if (close(hw->fd)) {
|
||||
SYSERR("close failed\n");
|
||||
return -errno;
|
||||
}
|
||||
free(hw);
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int snd_rawmidi_hw_nonblock(snd_rawmidi_t *rmidi, int nonblock)
|
||||
{
|
||||
snd_rawmidi_hw_t *hw = rmidi->private;
|
||||
long flags;
|
||||
|
||||
if ((flags = fcntl(hw->fd, F_GETFL)) < 0) {
|
||||
SYSERR("F_GETFL failed");
|
||||
return -errno;
|
||||
}
|
||||
if (nonblock)
|
||||
flags &= ~O_NONBLOCK;
|
||||
else
|
||||
flags |= O_NONBLOCK;
|
||||
if (fcntl(hw->fd, F_SETFL, flags) < 0) {
|
||||
SYSERR("F_SETFL for O_NONBLOCK failed");
|
||||
return -errno;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int snd_rawmidi_hw_info(snd_rawmidi_t *rmidi, snd_rawmidi_info_t * info)
|
||||
{
|
||||
snd_rawmidi_hw_t *hw = rmidi->private;
|
||||
if (ioctl(hw->fd, SND_RAWMIDI_IOCTL_INFO, info) < 0) {
|
||||
SYSERR("SND_RAWMIDI_IOCTL_INFO failed");
|
||||
return -errno;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int snd_rawmidi_hw_params(snd_rawmidi_t *rmidi, snd_rawmidi_params_t * params)
|
||||
{
|
||||
snd_rawmidi_hw_t *hw = rmidi->private;
|
||||
if (ioctl(hw->fd, SND_RAWMIDI_IOCTL_PARAMS, params) < 0) {
|
||||
SYSERR("SND_RAWMIDI_IOCTL_PARAMS failed");
|
||||
return -errno;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int snd_rawmidi_hw_status(snd_rawmidi_t *rmidi, snd_rawmidi_status_t * status)
|
||||
{
|
||||
snd_rawmidi_hw_t *hw = rmidi->private;
|
||||
if (ioctl(hw->fd, SND_RAWMIDI_IOCTL_STATUS, status) < 0) {
|
||||
SYSERR("SND_RAWMIDI_IOCTL_STATUS failed");
|
||||
return -errno;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int snd_rawmidi_hw_drop(snd_rawmidi_t *rmidi, int stream)
|
||||
{
|
||||
snd_rawmidi_hw_t *hw = rmidi->private;
|
||||
if (ioctl(hw->fd, SND_RAWMIDI_IOCTL_DROP, &stream) < 0) {
|
||||
SYSERR("SND_RAWMIDI_IOCTL_DROP failed");
|
||||
return -errno;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int snd_rawmidi_hw_drain(snd_rawmidi_t *rmidi, int stream)
|
||||
{
|
||||
snd_rawmidi_hw_t *hw = rmidi->private;
|
||||
if (ioctl(hw->fd, SND_RAWMIDI_IOCTL_DRAIN, &stream) < 0) {
|
||||
SYSERR("SND_RAWMIDI_IOCTL_DRAIN failed");
|
||||
return -errno;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
static ssize_t snd_rawmidi_hw_write(snd_rawmidi_t *rmidi, const void *buffer, size_t size)
|
||||
{
|
||||
snd_rawmidi_hw_t *hw = rmidi->private;
|
||||
ssize_t result;
|
||||
result = write(hw->fd, buffer, size);
|
||||
if (result < 0)
|
||||
return -errno;
|
||||
return result;
|
||||
}
|
||||
|
||||
static ssize_t snd_rawmidi_hw_read(snd_rawmidi_t *rmidi, void *buffer, size_t size)
|
||||
{
|
||||
snd_rawmidi_hw_t *hw = rmidi->private;
|
||||
ssize_t result;
|
||||
result = read(hw->fd, buffer, size);
|
||||
if (result < 0)
|
||||
return -errno;
|
||||
return result;
|
||||
}
|
||||
|
||||
snd_rawmidi_ops_t snd_rawmidi_hw_ops = {
|
||||
close: snd_rawmidi_hw_close,
|
||||
nonblock: snd_rawmidi_hw_nonblock,
|
||||
info: snd_rawmidi_hw_info,
|
||||
params: snd_rawmidi_hw_params,
|
||||
status: snd_rawmidi_hw_status,
|
||||
drop: snd_rawmidi_hw_drop,
|
||||
drain: snd_rawmidi_hw_drain,
|
||||
write: snd_rawmidi_hw_write,
|
||||
read: snd_rawmidi_hw_read,
|
||||
};
|
||||
|
||||
|
||||
int snd_rawmidi_hw_open(snd_rawmidi_t **handlep, char *name, int card, int device, int subdevice, int streams, int mode)
|
||||
{
|
||||
int fd, ver, ret;
|
||||
int attempt = 0;
|
||||
char filename[32];
|
||||
snd_ctl_t *ctl;
|
||||
snd_rawmidi_t *rmidi;
|
||||
snd_rawmidi_hw_t *hw;
|
||||
snd_rawmidi_info_t info;
|
||||
int fmode;
|
||||
|
||||
*handlep = NULL;
|
||||
|
||||
assert(card >= 0 && card < SND_CARDS);
|
||||
|
||||
if ((ret = snd_ctl_hw_open(&ctl, NULL, card)) < 0)
|
||||
return ret;
|
||||
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;
|
||||
}
|
||||
|
||||
switch (streams) {
|
||||
case SND_RAWMIDI_OPEN_OUTPUT:
|
||||
fmode = O_WRONLY;
|
||||
break;
|
||||
case SND_RAWMIDI_OPEN_INPUT:
|
||||
fmode = O_RDONLY;
|
||||
break;
|
||||
case SND_RAWMIDI_OPEN_DUPLEX:
|
||||
fmode = O_RDWR;
|
||||
break;
|
||||
default:
|
||||
assert(0);
|
||||
return -EINVAL;
|
||||
}
|
||||
|
||||
if (mode & SND_RAWMIDI_APPEND) {
|
||||
assert(streams & SND_RAWMIDI_OPEN_OUTPUT);
|
||||
fmode |= O_APPEND;
|
||||
}
|
||||
|
||||
if (mode & SND_RAWMIDI_NONBLOCK) {
|
||||
fmode |= O_NONBLOCK;
|
||||
}
|
||||
|
||||
assert(!(mode & ~(SND_RAWMIDI_APPEND|SND_RAWMIDI_NONBLOCK)));
|
||||
|
||||
if ((fd = open(filename, fmode)) < 0) {
|
||||
snd_card_load(card);
|
||||
if ((fd = open(filename, fmode)) < 0) {
|
||||
snd_ctl_close(ctl);
|
||||
SYSERR("open %s failed", filename);
|
||||
return -errno;
|
||||
}
|
||||
}
|
||||
if (ioctl(fd, SND_RAWMIDI_IOCTL_PVERSION, &ver) < 0) {
|
||||
ret = -errno;
|
||||
SYSERR("SND_RAWMIDI_IOCTL_PVERSION failed");
|
||||
close(fd);
|
||||
snd_ctl_close(ctl);
|
||||
return ret;
|
||||
}
|
||||
if (SND_PROTOCOL_INCOMPATIBLE(ver, SND_RAWMIDI_VERSION_MAX)) {
|
||||
close(fd);
|
||||
snd_ctl_close(ctl);
|
||||
return -SND_ERROR_INCOMPATIBLE_VERSION;
|
||||
}
|
||||
if (subdevice >= 0) {
|
||||
memset(&info, 0, sizeof(info));
|
||||
if (ioctl(fd, SND_RAWMIDI_IOCTL_INFO, &info) < 0) {
|
||||
SYSERR("SND_RAWMIDI_IOCTL_INFO failed");
|
||||
ret = -errno;
|
||||
close(fd);
|
||||
snd_ctl_close(ctl);
|
||||
return ret;
|
||||
}
|
||||
if (info.subdevice != subdevice) {
|
||||
close(fd);
|
||||
goto __again;
|
||||
}
|
||||
}
|
||||
hw = calloc(1, sizeof(snd_rawmidi_hw_t));
|
||||
if (hw == NULL) {
|
||||
close(fd);
|
||||
snd_ctl_close(ctl);
|
||||
return -ENOMEM;
|
||||
}
|
||||
rmidi = calloc(1, sizeof(snd_rawmidi_t));
|
||||
if (rmidi == NULL) {
|
||||
free(hw);
|
||||
close(fd);
|
||||
snd_ctl_close(ctl);
|
||||
return -ENOMEM;
|
||||
}
|
||||
hw->card = card;
|
||||
hw->device = device;
|
||||
hw->subdevice = subdevice;
|
||||
hw->fd = fd;
|
||||
if (name)
|
||||
rmidi->name = strdup(name);
|
||||
rmidi->type = SND_RAWMIDI_TYPE_HW;
|
||||
rmidi->streams = streams;
|
||||
rmidi->mode = mode;
|
||||
rmidi->poll_fd = fd;
|
||||
rmidi->ops = &snd_rawmidi_hw_ops;
|
||||
rmidi->private = hw;
|
||||
*handlep = rmidi;
|
||||
return 0;
|
||||
}
|
||||
|
||||
int _snd_rawmidi_hw_open(snd_rawmidi_t **handlep, char *name, snd_config_t *conf,
|
||||
int streams, int mode)
|
||||
{
|
||||
snd_config_iterator_t i;
|
||||
long card = -1, device = 0, subdevice = -1;
|
||||
char *str;
|
||||
int err;
|
||||
snd_config_foreach(i, conf) {
|
||||
snd_config_t *n = snd_config_entry(i);
|
||||
if (strcmp(n->id, "comment") == 0)
|
||||
continue;
|
||||
if (strcmp(n->id, "type") == 0)
|
||||
continue;
|
||||
if (strcmp(n->id, "streams") == 0)
|
||||
continue;
|
||||
if (strcmp(n->id, "card") == 0) {
|
||||
err = snd_config_integer_get(n, &card);
|
||||
if (err < 0) {
|
||||
err = snd_config_string_get(n, &str);
|
||||
if (err < 0)
|
||||
return -EINVAL;
|
||||
card = snd_card_get_index(str);
|
||||
if (card < 0)
|
||||
return card;
|
||||
}
|
||||
continue;
|
||||
}
|
||||
if (strcmp(n->id, "device") == 0) {
|
||||
err = snd_config_integer_get(n, &device);
|
||||
if (err < 0)
|
||||
return err;
|
||||
continue;
|
||||
}
|
||||
if (strcmp(n->id, "subdevice") == 0) {
|
||||
err = snd_config_integer_get(n, &subdevice);
|
||||
if (err < 0)
|
||||
return err;
|
||||
continue;
|
||||
}
|
||||
return -EINVAL;
|
||||
}
|
||||
if (card < 0)
|
||||
return -EINVAL;
|
||||
return snd_rawmidi_hw_open(handlep, name, card, device, subdevice, streams, mode);
|
||||
}
|
||||
|
||||
60
src/rawmidi/rawmidi_local.h
Normal file
60
src/rawmidi/rawmidi_local.h
Normal file
|
|
@ -0,0 +1,60 @@
|
|||
/*
|
||||
* Rawmidi interface - local header file
|
||||
* Copyright (c) 2000 by Abramo Bagnara <abramo@alsa-project.org>
|
||||
*
|
||||
*
|
||||
* 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 <assert.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <limits.h>
|
||||
#include <errno.h>
|
||||
#include "asoundlib.h"
|
||||
|
||||
#if __GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ >= 95)
|
||||
#define ERR(...) snd_lib_error(__FILE__, __LINE__, __FUNCTION__, 0, __VA_ARGS__)
|
||||
#define SYSERR(...) snd_lib_error(__FILE__, __LINE__, __FUNCTION__, errno, __VA_ARGS__)
|
||||
#else
|
||||
#define ERR(args...) snd_lib_error(__FILE__, __LINE__, __FUNCTION__, 0, ##args)
|
||||
#define SYSERR(args...) snd_lib_error(__FILE__, __LINE__, __FUNCTION__, errno, ##args)
|
||||
#endif
|
||||
|
||||
typedef struct {
|
||||
int (*close)(snd_rawmidi_t *rawmidi);
|
||||
int (*nonblock)(snd_rawmidi_t *rawmidi, int nonblock);
|
||||
int (*info)(snd_rawmidi_t *rawmidi, snd_rawmidi_info_t *info);
|
||||
int (*params)(snd_rawmidi_t *rawmidi, snd_rawmidi_params_t *params);
|
||||
int (*status)(snd_rawmidi_t *rawmidi, snd_rawmidi_status_t *status);
|
||||
int (*drop)(snd_rawmidi_t *rawmidi, int stream);
|
||||
int (*drain)(snd_rawmidi_t *rawmidi, int stream);
|
||||
ssize_t (*write)(snd_rawmidi_t *rawmidi, const void *buffer, size_t size);
|
||||
ssize_t (*read)(snd_rawmidi_t *rawmidi, void *buffer, size_t size);
|
||||
} snd_rawmidi_ops_t;
|
||||
|
||||
struct _snd_rawmidi {
|
||||
char *name;
|
||||
snd_rawmidi_type_t type;
|
||||
int streams;
|
||||
int mode;
|
||||
int poll_fd;
|
||||
snd_rawmidi_ops_t *ops;
|
||||
void *private;
|
||||
};
|
||||
|
||||
int snd_rawmidi_hw_open(snd_rawmidi_t **handle, char *name, int card, int device, int subdevice, int streams, int mode);
|
||||
|
||||
Loading…
Add table
Add a link
Reference in a new issue