Blame src/seq/seqmid.c

Packit 4a16fb
/*
Packit 4a16fb
 *  Sequencer Interface - middle-level routines
Packit 4a16fb
 *
Packit 4a16fb
 *  Copyright (c) 1999 by Takashi Iwai <tiwai@suse.de>
Packit 4a16fb
 *
Packit 4a16fb
 *
Packit 4a16fb
 *   This library is free software; you can redistribute it and/or modify
Packit 4a16fb
 *   it under the terms of the GNU Lesser General Public License as
Packit 4a16fb
 *   published by the Free Software Foundation; either version 2.1 of
Packit 4a16fb
 *   the License, or (at your option) any later version.
Packit 4a16fb
 *
Packit 4a16fb
 *   This program is distributed in the hope that it will be useful,
Packit 4a16fb
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 4a16fb
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
Packit 4a16fb
 *   GNU Lesser General Public License for more details.
Packit 4a16fb
 *
Packit 4a16fb
 *   You should have received a copy of the GNU Lesser General Public
Packit 4a16fb
 *   License along with this library; if not, write to the Free Software
Packit 4a16fb
 *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
Packit 4a16fb
 *
Packit 4a16fb
 */
Packit 4a16fb
Packit 4a16fb
#include <stdio.h>
Packit 4a16fb
#include <stdlib.h>
Packit 4a16fb
#include <unistd.h>
Packit 4a16fb
#include <string.h>
Packit 4a16fb
#include <fcntl.h>
Packit 4a16fb
#include <ctype.h>
Packit 4a16fb
#include <sys/ioctl.h>
Packit 4a16fb
#include "seq_local.h"
Packit 4a16fb
Packit 4a16fb
/**
Packit 4a16fb
 * \brief queue controls - start/stop/continue
Packit 4a16fb
 * \param seq sequencer handle
Packit 4a16fb
 * \param q queue id to control
Packit 4a16fb
 * \param type event type
Packit 4a16fb
 * \param value event value
Packit 4a16fb
 * \param ev event instance
Packit 4a16fb
 *
Packit 4a16fb
 * This function sets up general queue control event and sends it.
Packit 4a16fb
 * To send at scheduled time, set the schedule in \a ev.
Packit 4a16fb
 * If \a ev is NULL, the event is composed locally and sent immediately
Packit 4a16fb
 * to the specified queue.  In any cases, you need to call #snd_seq_drain_output()
Packit 4a16fb
 * appropriately to feed the event.
Packit 4a16fb
 *
Packit 4a16fb
 * \sa snd_seq_alloc_queue()
Packit 4a16fb
 */
Packit 4a16fb
int snd_seq_control_queue(snd_seq_t *seq, int q, int type, int value, snd_seq_event_t *ev)
Packit 4a16fb
{
Packit 4a16fb
	snd_seq_event_t tmpev;
Packit 4a16fb
	if (ev == NULL) {
Packit 4a16fb
		snd_seq_ev_clear(&tmpev);
Packit 4a16fb
		ev = &tmpev;
Packit 4a16fb
		snd_seq_ev_set_direct(ev);
Packit 4a16fb
	}
Packit 4a16fb
	snd_seq_ev_set_queue_control(ev, type, q, value);
Packit 4a16fb
	return snd_seq_event_output(seq, ev);
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
Packit 4a16fb
/**
Packit 4a16fb
 * \brief create a port - simple version
Packit 4a16fb
 * \param seq sequencer handle
Packit 4a16fb
 * \param name the name of the port
Packit 4a16fb
 * \param caps capability bits
Packit 4a16fb
 * \param type type bits
Packit 4a16fb
 * \return the created port number or negative error code
Packit 4a16fb
 *
Packit 4a16fb
 * Creates a port with the given capability and type bits.
Packit 4a16fb
 *
Packit 4a16fb
 * \sa snd_seq_create_port(), snd_seq_delete_simple_port()
Packit 4a16fb
 */
Packit 4a16fb
int snd_seq_create_simple_port(snd_seq_t *seq, const char *name,
Packit 4a16fb
			       unsigned int caps, unsigned int type)
Packit 4a16fb
{
Packit 4a16fb
	snd_seq_port_info_t pinfo;
Packit 4a16fb
	int result;
Packit 4a16fb
Packit 4a16fb
	memset(&pinfo, 0, sizeof(pinfo));
Packit 4a16fb
	if (name)
Packit 4a16fb
		strncpy(pinfo.name, name, sizeof(pinfo.name) - 1);
Packit 4a16fb
	pinfo.capability = caps;
Packit 4a16fb
	pinfo.type = type;
Packit 4a16fb
	pinfo.midi_channels = 16;
Packit 4a16fb
	pinfo.midi_voices = 64; /* XXX */
Packit 4a16fb
	pinfo.synth_voices = 0; /* XXX */
Packit 4a16fb
Packit 4a16fb
	result = snd_seq_create_port(seq, &pinfo);
Packit 4a16fb
	if (result < 0)
Packit 4a16fb
		return result;
Packit 4a16fb
	else
Packit 4a16fb
		return pinfo.addr.port;
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
/**
Packit 4a16fb
 * \brief delete the port
Packit 4a16fb
 * \param seq sequencer handle
Packit 4a16fb
 * \param port port id
Packit 4a16fb
 * \return 0 on success or negative error code
Packit 4a16fb
 *
Packit 4a16fb
 * \sa snd_seq_delete_port(), snd_seq_create_simple_port()
Packit 4a16fb
 */
Packit 4a16fb
int snd_seq_delete_simple_port(snd_seq_t *seq, int port)
Packit 4a16fb
{
Packit 4a16fb
	return snd_seq_delete_port(seq, port);
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
/**
Packit 4a16fb
 * \brief simple subscription (w/o exclusive & time conversion)
Packit 4a16fb
 * \param seq sequencer handle
Packit 4a16fb
 * \param myport the port id as receiver
Packit 4a16fb
 * \param src_client sender client id
Packit 4a16fb
 * \param src_port sender port id
Packit 4a16fb
 * \return 0 on success or negative error code
Packit 4a16fb
 *
Packit 4a16fb
 * Connect from the given sender client:port to the given destination port in the
Packit 4a16fb
 * current client.
Packit 4a16fb
 *
Packit 4a16fb
 * \sa snd_seq_subscribe_port(), snd_seq_disconnect_from()
Packit 4a16fb
 */
Packit 4a16fb
int snd_seq_connect_from(snd_seq_t *seq, int myport, int src_client, int src_port)
Packit 4a16fb
{
Packit 4a16fb
	snd_seq_port_subscribe_t subs;
Packit 4a16fb
	
Packit 4a16fb
	memset(&subs, 0, sizeof(subs));
Packit 4a16fb
	subs.sender.client = src_client;
Packit 4a16fb
	subs.sender.port = src_port;
Packit 4a16fb
	/*subs.dest.client = seq->client;*/
Packit 4a16fb
	subs.dest.client = snd_seq_client_id(seq);
Packit 4a16fb
	subs.dest.port = myport;
Packit 4a16fb
Packit 4a16fb
	return snd_seq_subscribe_port(seq, &subs);
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
/**
Packit 4a16fb
 * \brief simple subscription (w/o exclusive & time conversion)
Packit 4a16fb
 * \param seq sequencer handle
Packit 4a16fb
 * \param myport the port id as sender
Packit 4a16fb
 * \param dest_client destination client id
Packit 4a16fb
 * \param dest_port destination port id
Packit 4a16fb
 * \return 0 on success or negative error code
Packit 4a16fb
 *
Packit 4a16fb
 * Connect from the given receiver port in the current client
Packit 4a16fb
 * to the given destination client:port.
Packit 4a16fb
 *
Packit 4a16fb
 * \sa snd_seq_subscribe_port(), snd_seq_disconnect_to()
Packit 4a16fb
 */
Packit 4a16fb
int snd_seq_connect_to(snd_seq_t *seq, int myport, int dest_client, int dest_port)
Packit 4a16fb
{
Packit 4a16fb
	snd_seq_port_subscribe_t subs;
Packit 4a16fb
	
Packit 4a16fb
	memset(&subs, 0, sizeof(subs));
Packit 4a16fb
	/*subs.sender.client = seq->client;*/
Packit 4a16fb
	subs.sender.client = snd_seq_client_id(seq);
Packit 4a16fb
	subs.sender.port = myport;
Packit 4a16fb
	subs.dest.client = dest_client;
Packit 4a16fb
	subs.dest.port = dest_port;
Packit 4a16fb
Packit 4a16fb
	return snd_seq_subscribe_port(seq, &subs);
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
/**
Packit 4a16fb
 * \brief simple disconnection
Packit 4a16fb
 * \param seq sequencer handle
Packit 4a16fb
 * \param myport the port id as receiver
Packit 4a16fb
 * \param src_client sender client id
Packit 4a16fb
 * \param src_port sender port id
Packit 4a16fb
 * \return 0 on success or negative error code
Packit 4a16fb
 *
Packit 4a16fb
 * Remove connection from the given sender client:port
Packit 4a16fb
 * to the given destination port in the current client.
Packit 4a16fb
 *
Packit 4a16fb
 * \sa snd_seq_unsubscribe_port(), snd_seq_connect_from()
Packit 4a16fb
 */
Packit 4a16fb
int snd_seq_disconnect_from(snd_seq_t *seq, int myport, int src_client, int src_port)
Packit 4a16fb
{
Packit 4a16fb
	snd_seq_port_subscribe_t subs;
Packit 4a16fb
	
Packit 4a16fb
	memset(&subs, 0, sizeof(subs));
Packit 4a16fb
	subs.sender.client = src_client;
Packit 4a16fb
	subs.sender.port = src_port;
Packit 4a16fb
	/*subs.dest.client = seq->client;*/
Packit 4a16fb
	subs.dest.client = snd_seq_client_id(seq);
Packit 4a16fb
	subs.dest.port = myport;
Packit 4a16fb
Packit 4a16fb
	return snd_seq_unsubscribe_port(seq, &subs);
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
/**
Packit 4a16fb
 * \brief simple disconnection
Packit 4a16fb
 * \param seq sequencer handle
Packit 4a16fb
 * \param myport the port id as sender
Packit 4a16fb
 * \param dest_client destination client id
Packit 4a16fb
 * \param dest_port destination port id
Packit 4a16fb
 * \return 0 on success or negative error code
Packit 4a16fb
 *
Packit 4a16fb
 * Remove connection from the given sender client:port
Packit 4a16fb
 * to the given destination port in the current client.
Packit 4a16fb
 *
Packit 4a16fb
 * \sa snd_seq_unsubscribe_port(), snd_seq_connect_to()
Packit 4a16fb
 */
Packit 4a16fb
int snd_seq_disconnect_to(snd_seq_t *seq, int myport, int dest_client, int dest_port)
Packit 4a16fb
{
Packit 4a16fb
	snd_seq_port_subscribe_t subs;
Packit 4a16fb
	
Packit 4a16fb
	memset(&subs, 0, sizeof(subs));
Packit 4a16fb
	/*subs.sender.client = seq->client;*/
Packit 4a16fb
	subs.sender.client = snd_seq_client_id(seq);
Packit 4a16fb
	subs.sender.port = myport;
Packit 4a16fb
	subs.dest.client = dest_client;
Packit 4a16fb
	subs.dest.port = dest_port;
Packit 4a16fb
Packit 4a16fb
	return snd_seq_unsubscribe_port(seq, &subs);
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
/*
Packit 4a16fb
 * set client information
Packit 4a16fb
 */
Packit 4a16fb
Packit 4a16fb
/**
Packit 4a16fb
 * \brief set client name
Packit 4a16fb
 * \param seq sequencer handle
Packit 4a16fb
 * \param name name string
Packit 4a16fb
 * \return 0 on success or negative error code
Packit 4a16fb
 *
Packit 4a16fb
 * \sa snd_seq_set_client_info()
Packit 4a16fb
 */
Packit 4a16fb
int snd_seq_set_client_name(snd_seq_t *seq, const char *name)
Packit 4a16fb
{
Packit 4a16fb
	snd_seq_client_info_t info;
Packit 4a16fb
	int err;
Packit 4a16fb
Packit 4a16fb
	if ((err = snd_seq_get_client_info(seq, &info)) < 0)
Packit 4a16fb
		return err;
Packit 4a16fb
	strncpy(info.name, name, sizeof(info.name) - 1);
Packit 4a16fb
	return snd_seq_set_client_info(seq, &info;;
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
/**
Packit 4a16fb
 * \brief add client event filter
Packit 4a16fb
 * \param seq sequencer handle
Packit 4a16fb
 * \param event_type event type to be added
Packit 4a16fb
 * \return 0 on success or negative error code
Packit 4a16fb
 *
Packit 4a16fb
 * \sa snd_seq_set_client_info()
Packit 4a16fb
 */
Packit 4a16fb
int snd_seq_set_client_event_filter(snd_seq_t *seq, int event_type)
Packit 4a16fb
{
Packit 4a16fb
	snd_seq_client_info_t info;
Packit 4a16fb
	int err;
Packit 4a16fb
Packit 4a16fb
	if ((err = snd_seq_get_client_info(seq, &info)) < 0)
Packit 4a16fb
		return err;
Packit 4a16fb
	snd_seq_client_info_event_filter_add(&info, event_type);
Packit 4a16fb
	return snd_seq_set_client_info(seq, &info;;
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
/**
Packit 4a16fb
 * \brief change the output pool size of the given client
Packit 4a16fb
 * \param seq sequencer handle
Packit 4a16fb
 * \param size output pool size
Packit 4a16fb
 * \return 0 on success or negative error code
Packit 4a16fb
 *
Packit 4a16fb
 * \sa snd_seq_set_client_pool()
Packit 4a16fb
 */
Packit 4a16fb
int snd_seq_set_client_pool_output(snd_seq_t *seq, size_t size)
Packit 4a16fb
{
Packit 4a16fb
	snd_seq_client_pool_t info;
Packit 4a16fb
	int err;
Packit 4a16fb
Packit 4a16fb
	if ((err = snd_seq_get_client_pool(seq, &info)) < 0)
Packit 4a16fb
		return err;
Packit 4a16fb
	info.output_pool = size;
Packit 4a16fb
	return snd_seq_set_client_pool(seq, &info;;
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
/**
Packit 4a16fb
 * \brief change the output room size of the given client
Packit 4a16fb
 * \param seq sequencer handle
Packit 4a16fb
 * \param size output room size
Packit 4a16fb
 * \return 0 on success or negative error code
Packit 4a16fb
 *
Packit 4a16fb
 * \sa snd_seq_set_client_pool()
Packit 4a16fb
 */
Packit 4a16fb
int snd_seq_set_client_pool_output_room(snd_seq_t *seq, size_t size)
Packit 4a16fb
{
Packit 4a16fb
	snd_seq_client_pool_t info;
Packit 4a16fb
	int err;
Packit 4a16fb
Packit 4a16fb
	if ((err = snd_seq_get_client_pool(seq, &info)) < 0)
Packit 4a16fb
		return err;
Packit 4a16fb
	info.output_room = size;
Packit 4a16fb
	return snd_seq_set_client_pool(seq, &info;;
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
/**
Packit 4a16fb
 * \brief change the input pool size of the given client
Packit 4a16fb
 * \param seq sequencer handle
Packit 4a16fb
 * \param size input pool size
Packit 4a16fb
 * \return 0 on success or negative error code
Packit 4a16fb
 *
Packit 4a16fb
 * \sa snd_seq_set_client_pool()
Packit 4a16fb
 */
Packit 4a16fb
int snd_seq_set_client_pool_input(snd_seq_t *seq, size_t size)
Packit 4a16fb
{
Packit 4a16fb
	snd_seq_client_pool_t info;
Packit 4a16fb
	int err;
Packit 4a16fb
Packit 4a16fb
	if ((err = snd_seq_get_client_pool(seq, &info)) < 0)
Packit 4a16fb
		return err;
Packit 4a16fb
	info.input_pool = size;
Packit 4a16fb
	return snd_seq_set_client_pool(seq, &info;;
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
/**
Packit 4a16fb
 * \brief reset client output pool
Packit 4a16fb
 * \param seq sequencer handle
Packit 4a16fb
 * \return 0 on success or negative error code
Packit 4a16fb
 *
Packit 4a16fb
 * So far, this works identically like #snd_seq_drop_output().
Packit 4a16fb
 */
Packit 4a16fb
int snd_seq_reset_pool_output(snd_seq_t *seq)
Packit 4a16fb
{
Packit 4a16fb
	return snd_seq_drop_output(seq);
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
/**
Packit 4a16fb
 * \brief reset client input pool
Packit 4a16fb
 * \param seq sequencer handle
Packit 4a16fb
 * \return 0 on success or negative error code
Packit 4a16fb
 *
Packit 4a16fb
 * So far, this works identically like #snd_seq_drop_input().
Packit 4a16fb
 */
Packit 4a16fb
int snd_seq_reset_pool_input(snd_seq_t *seq)
Packit 4a16fb
{
Packit 4a16fb
	return snd_seq_drop_input(seq);
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
/**
Packit 4a16fb
 * \brief wait until all events are processed
Packit 4a16fb
 * \param seq sequencer handle
Packit 4a16fb
 * \return 0 on success or negative error code
Packit 4a16fb
 *
Packit 4a16fb
 * This function waits until all events of this client are processed.
Packit 4a16fb
 *
Packit 4a16fb
 * \sa snd_seq_drain_output()
Packit 4a16fb
 */
Packit 4a16fb
int snd_seq_sync_output_queue(snd_seq_t *seq)
Packit 4a16fb
{
Packit 4a16fb
	int err;
Packit 4a16fb
	snd_seq_client_pool_t info;
Packit 4a16fb
	int saved_room;
Packit 4a16fb
	struct pollfd pfd;
Packit 4a16fb
Packit 4a16fb
	assert(seq);
Packit 4a16fb
	/* reprogram the room size to full */
Packit 4a16fb
	if ((err = snd_seq_get_client_pool(seq, &info)) < 0)
Packit 4a16fb
		return err;
Packit 4a16fb
	saved_room = info.output_room;
Packit 4a16fb
	info.output_room = info.output_pool; /* wait until all gone */
Packit 4a16fb
	if ((err = snd_seq_set_client_pool(seq, &info)) < 0)
Packit 4a16fb
		return err;
Packit 4a16fb
	/* wait until all events are purged */
Packit 4a16fb
	pfd.fd = seq->poll_fd;
Packit 4a16fb
	pfd.events = POLLOUT;
Packit 4a16fb
	err = poll(&pfd, 1, -1);
Packit 4a16fb
	/* restore the room size */ 
Packit 4a16fb
	info.output_room = saved_room;
Packit 4a16fb
	snd_seq_set_client_pool(seq, &info;;
Packit 4a16fb
	return err;
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
/**
Packit 4a16fb
 * \brief parse the given string and get the sequencer address
Packit 4a16fb
 * \param seq sequencer handle
Packit 4a16fb
 * \param addr the address pointer to be returned
Packit 4a16fb
 * \param arg the string to be parsed
Packit 4a16fb
 * \return 0 on success or negative error code
Packit 4a16fb
 *
Packit 4a16fb
 * This function parses the sequencer client and port numbers from the given string.
Packit 4a16fb
 * The client and port tokens are separated by either colon or period, e.g. 128:1.
Packit 4a16fb
 * When \a seq is not NULL, the function accepts also a client name not only
Packit 4a16fb
 * digit numbers.
Packit 4a16fb
 * Actually \a arg need to be only a prefix of the wanted client.
Packit 4a16fb
 * That is, if a client named "Foobar XXL Master 2012" with number 128 is available,
Packit 4a16fb
 * then parsing "Foobar" will return the address 128:0 if no other client is
Packit 4a16fb
 * an exact match.
Packit 4a16fb
 */
Packit 4a16fb
int snd_seq_parse_address(snd_seq_t *seq, snd_seq_addr_t *addr, const char *arg)
Packit 4a16fb
{
Packit 4a16fb
	char *p;
Packit 4a16fb
	int client, port;
Packit 4a16fb
	int len;
Packit 4a16fb
Packit 4a16fb
	assert(addr && arg);
Packit 4a16fb
Packit 4a16fb
	if ((p = strpbrk(arg, ":.")) != NULL) {
Packit 4a16fb
		if ((port = atoi(p + 1)) < 0)
Packit 4a16fb
			return -EINVAL;
Packit 4a16fb
		len = (int)(p - arg); /* length of client name */
Packit 4a16fb
	} else {
Packit 4a16fb
		port = 0;
Packit 4a16fb
		len = strlen(arg);
Packit 4a16fb
	}
Packit 4a16fb
	addr->port = port;
Packit 4a16fb
	if (isdigit(*arg)) {
Packit 4a16fb
		client = atoi(arg);
Packit 4a16fb
		if (client < 0)
Packit 4a16fb
			return -EINVAL;
Packit 4a16fb
		addr->client = client;
Packit 4a16fb
	} else {
Packit 4a16fb
		/* convert from the name */
Packit 4a16fb
		snd_seq_client_info_t cinfo;
Packit 4a16fb
Packit 4a16fb
		if (! seq)
Packit 4a16fb
			return -EINVAL;
Packit 4a16fb
		if (len <= 0)
Packit 4a16fb
			return -EINVAL;
Packit 4a16fb
		client = -1;
Packit 4a16fb
		cinfo.client = -1;
Packit 4a16fb
		while (snd_seq_query_next_client(seq, &cinfo) >= 0) {
Packit 4a16fb
			if (!strncmp(arg, cinfo.name, len)) {
Packit 4a16fb
				if (strlen(cinfo.name) == (size_t)len) {
Packit 4a16fb
					/* exact match */
Packit 4a16fb
					addr->client = cinfo.client;
Packit 4a16fb
					return 0;
Packit 4a16fb
				}
Packit 4a16fb
				if (client < 0)
Packit 4a16fb
					client = cinfo.client;
Packit 4a16fb
			}
Packit 4a16fb
		}
Packit 4a16fb
		if (client >= 0) {
Packit 4a16fb
			/* prefix match */
Packit 4a16fb
			addr->client = client;
Packit 4a16fb
			return 0;
Packit 4a16fb
		}
Packit 4a16fb
		return -ENOENT; /* not found */
Packit 4a16fb
	}
Packit 4a16fb
	return 0;
Packit 4a16fb
}
Packit 4a16fb