Blame src/async.c

Packit 4a16fb
/**
Packit 4a16fb
 * \file async.c
Packit 4a16fb
 * \brief Async notification helpers
Packit 4a16fb
 * \author Abramo Bagnara <abramo@alsa-project.org>
Packit 4a16fb
 * \date 2001
Packit 4a16fb
 */
Packit 4a16fb
/*
Packit 4a16fb
 *  Async notification helpers
Packit 4a16fb
 *  Copyright (c) 2001 by Abramo Bagnara <abramo@alsa-project.org>
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 "pcm/pcm_local.h"
Packit 4a16fb
#include "control/control_local.h"
Packit 4a16fb
#include <signal.h>
Packit 4a16fb
Packit 4a16fb
static struct sigaction previous_action;
Packit 4a16fb
#define MAX_SIG_FUNCTION_CODE 10 /* i.e. SIG_DFL SIG_IGN SIG_HOLD et al */
Packit 4a16fb
Packit 4a16fb
#ifdef SND_ASYNC_RT_SIGNAL
Packit 4a16fb
/** async signal number */
Packit 4a16fb
static int snd_async_signo;
Packit 4a16fb
Packit 4a16fb
void snd_async_init(void) __attribute__ ((constructor));
Packit 4a16fb
Packit 4a16fb
void snd_async_init(void)
Packit 4a16fb
{
Packit 4a16fb
	snd_async_signo = __libc_allocate_rtsig(0);
Packit 4a16fb
	if (snd_async_signo < 0) {
Packit 4a16fb
		SNDERR("Unable to find a RT signal to use for snd_async");
Packit 4a16fb
		exit(1);
Packit 4a16fb
	}
Packit 4a16fb
}
Packit 4a16fb
#else
Packit 4a16fb
/** async signal number */
Packit 4a16fb
static const int snd_async_signo = SIGIO;
Packit 4a16fb
#endif
Packit 4a16fb
Packit 4a16fb
static LIST_HEAD(snd_async_handlers);
Packit 4a16fb
Packit 4a16fb
static void snd_async_handler(int signo ATTRIBUTE_UNUSED, siginfo_t *siginfo, void *context ATTRIBUTE_UNUSED)
Packit 4a16fb
{
Packit 4a16fb
	int fd;
Packit 4a16fb
	struct list_head *i;
Packit 4a16fb
	//assert(siginfo->si_code == SI_SIGIO);
Packit 4a16fb
	if (signo == SIGIO
Packit 4a16fb
	 && (unsigned long)(previous_action.sa_sigaction) > MAX_SIG_FUNCTION_CODE)
Packit 4a16fb
		previous_action.sa_sigaction(signo, siginfo, context);
Packit 4a16fb
	fd = siginfo->si_fd;
Packit 4a16fb
	list_for_each(i, &snd_async_handlers) {
Packit 4a16fb
		snd_async_handler_t *h = list_entry(i, snd_async_handler_t, glist);
Packit 4a16fb
		if (h->fd == fd && h->callback)
Packit 4a16fb
			h->callback(h);
Packit 4a16fb
	}
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
/**
Packit 4a16fb
 * \brief Registers an async handler.
Packit 4a16fb
 * \param handler The function puts the pointer to the new async handler
Packit 4a16fb
 *                object at the address specified by \p handler.
Packit 4a16fb
 * \param fd The file descriptor to be associated with the callback.
Packit 4a16fb
 * \param callback The async callback function.
Packit 4a16fb
 * \param private_data Private data for the async callback function.
Packit 4a16fb
 * \result Zero if successful, otherwise a negative error code.
Packit 4a16fb
 *
Packit 4a16fb
 * This function associates the callback function with the given file,
Packit 4a16fb
 * and saves this association in a \c snd_async_handler_t object.
Packit 4a16fb
 *
Packit 4a16fb
 * Whenever the \c SIGIO signal is raised for the file \p fd, the callback
Packit 4a16fb
 * function will be called with its parameter pointing to the async handler
Packit 4a16fb
 * object returned by this function.
Packit 4a16fb
 *
Packit 4a16fb
 * The ALSA \c sigaction handler for the \c SIGIO signal automatically
Packit 4a16fb
 * multiplexes the notifications to the registered async callbacks.
Packit 4a16fb
 * However, the application is responsible for instructing the device driver
Packit 4a16fb
 * to generate the \c SIGIO signal.
Packit 4a16fb
 *
Packit 4a16fb
 * The \c SIGIO signal may have been replaced with another signal,
Packit 4a16fb
 * see #snd_async_handler_get_signo.
Packit 4a16fb
 *
Packit 4a16fb
 * When the async handler isn't needed anymore, you must delete it with
Packit 4a16fb
 * #snd_async_del_handler.
Packit 4a16fb
 *
Packit 4a16fb
 * \see snd_async_add_pcm_handler, snd_async_add_ctl_handler
Packit 4a16fb
 */
Packit 4a16fb
int snd_async_add_handler(snd_async_handler_t **handler, int fd, 
Packit 4a16fb
			  snd_async_callback_t callback, void *private_data)
Packit 4a16fb
{
Packit 4a16fb
	snd_async_handler_t *h;
Packit 4a16fb
	int was_empty;
Packit 4a16fb
	assert(handler);
Packit 4a16fb
	h = malloc(sizeof(*h));
Packit 4a16fb
	if (!h)
Packit 4a16fb
		return -ENOMEM;
Packit 4a16fb
	h->fd = fd;
Packit 4a16fb
	h->callback = callback;
Packit 4a16fb
	h->private_data = private_data;
Packit 4a16fb
	was_empty = list_empty(&snd_async_handlers);
Packit 4a16fb
	list_add_tail(&h->glist, &snd_async_handlers);
Packit 4a16fb
	INIT_LIST_HEAD(&h->hlist);
Packit 4a16fb
	*handler = h;
Packit 4a16fb
	if (was_empty) {
Packit 4a16fb
		int err;
Packit 4a16fb
		struct sigaction act;
Packit 4a16fb
		memset(&act, 0, sizeof(act));
Packit 4a16fb
		act.sa_flags = SA_RESTART | SA_SIGINFO;
Packit 4a16fb
		act.sa_sigaction = snd_async_handler;
Packit 4a16fb
		sigemptyset(&act.sa_mask);
Packit 4a16fb
		assert(!previous_action.sa_sigaction);
Packit 4a16fb
		err = sigaction(snd_async_signo, &act, &previous_action);
Packit 4a16fb
		if (err < 0) {
Packit 4a16fb
			SYSERR("sigaction");
Packit 4a16fb
			return -errno;
Packit 4a16fb
		}
Packit 4a16fb
	}
Packit 4a16fb
	return 0;
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
/**
Packit 4a16fb
 * \brief Deletes an async handler.
Packit 4a16fb
 * \param handler Handle of the async handler to delete.
Packit 4a16fb
 * \result Zero if successful, otherwise a negative error code.
Packit 4a16fb
 */
Packit 4a16fb
int snd_async_del_handler(snd_async_handler_t *handler)
Packit 4a16fb
{
Packit 4a16fb
	int err = 0;
Packit 4a16fb
	int was_empty = list_empty(&snd_async_handlers);
Packit 4a16fb
	assert(handler);
Packit 4a16fb
	list_del(&handler->glist);
Packit 4a16fb
	if (!was_empty
Packit 4a16fb
	 && list_empty(&snd_async_handlers)) {
Packit 4a16fb
		err = sigaction(snd_async_signo, &previous_action, NULL);
Packit 4a16fb
		if (err < 0) {
Packit 4a16fb
			SYSERR("sigaction");
Packit 4a16fb
			return -errno;
Packit 4a16fb
		}
Packit 4a16fb
		memset(&previous_action, 0, sizeof(previous_action));
Packit 4a16fb
	}
Packit 4a16fb
	if (handler->type == SND_ASYNC_HANDLER_GENERIC)
Packit 4a16fb
		goto _end;
Packit 4a16fb
	if (!list_empty(&handler->hlist))
Packit 4a16fb
		list_del(&handler->hlist);
Packit 4a16fb
	if (!list_empty(&handler->hlist))
Packit 4a16fb
		goto _end;
Packit 4a16fb
	switch (handler->type) {
Packit 4a16fb
#ifdef BUILD_PCM
Packit 4a16fb
	case SND_ASYNC_HANDLER_PCM:
Packit 4a16fb
		err = snd_pcm_async(handler->u.pcm, -1, 1);
Packit 4a16fb
		break;
Packit 4a16fb
#endif
Packit 4a16fb
	case SND_ASYNC_HANDLER_CTL:
Packit 4a16fb
		err = snd_ctl_async(handler->u.ctl, -1, 1);
Packit 4a16fb
		break;
Packit 4a16fb
	default:
Packit 4a16fb
		assert(0);
Packit 4a16fb
	}
Packit 4a16fb
 _end:
Packit 4a16fb
	free(handler);
Packit 4a16fb
	return err;
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
/**
Packit 4a16fb
 * \brief Returns the signal number assigned to an async handler.
Packit 4a16fb
 * \param handler Handle to an async handler.
Packit 4a16fb
 * \result The signal number if successful, otherwise a negative error code.
Packit 4a16fb
 *
Packit 4a16fb
 * The signal number for async handlers usually is \c SIGIO,
Packit 4a16fb
 * but wizards can redefine it to a realtime signal
Packit 4a16fb
 * when compiling the ALSA library.
Packit 4a16fb
 */
Packit 4a16fb
int snd_async_handler_get_signo(snd_async_handler_t *handler)
Packit 4a16fb
{
Packit 4a16fb
	assert(handler);
Packit 4a16fb
	return snd_async_signo;
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
/**
Packit 4a16fb
 * \brief Returns the file descriptor assigned to an async handler.
Packit 4a16fb
 * \param handler Handle to an async handler.
Packit 4a16fb
 * \result The file descriptor if successful, otherwise a negative error code.
Packit 4a16fb
 */
Packit 4a16fb
int snd_async_handler_get_fd(snd_async_handler_t *handler)
Packit 4a16fb
{
Packit 4a16fb
	assert(handler);
Packit 4a16fb
	return handler->fd;
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
/**
Packit 4a16fb
 * \brief Returns the private data assigned to an async handler.
Packit 4a16fb
 * \param handler Handle to an async handler.
Packit 4a16fb
 * \result The \c private_data value registered with the async handler.
Packit 4a16fb
 */
Packit 4a16fb
void *snd_async_handler_get_callback_private(snd_async_handler_t *handler)
Packit 4a16fb
{
Packit 4a16fb
	assert(handler);
Packit 4a16fb
	return handler->private_data;
Packit 4a16fb
}
Packit 4a16fb