3 * \brief Async notification helpers
4 * \author Abramo Bagnara <abramo@alsa-project.org>
8 * Async notification helpers
9 * Copyright (c) 2001 by Abramo Bagnara <abramo@alsa-project.org>
11 * This library is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU Lesser General Public License as
13 * published by the Free Software Foundation; either version 2.1 of
14 * the License, or (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU Lesser General Public License for more details.
21 * You should have received a copy of the GNU Lesser General Public
22 * License along with this library; if not, write to the Free Software
23 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
27 #include "pcm/pcm_local.h"
28 #include "control/control_local.h"
31 static struct sigaction previous_action;
32 #define MAX_SIG_FUNCTION_CODE 10 /* i.e. SIG_DFL SIG_IGN SIG_HOLD et al */
34 #ifdef SND_ASYNC_RT_SIGNAL
35 /** async signal number */
36 static int snd_async_signo;
38 void snd_async_init(void) __attribute__ ((constructor));
40 void snd_async_init(void)
42 snd_async_signo = __libc_allocate_rtsig(0);
43 if (snd_async_signo < 0) {
44 SNDERR("Unable to find a RT signal to use for snd_async");
49 /** async signal number */
50 static const int snd_async_signo = SIGIO;
53 static LIST_HEAD(snd_async_handlers);
55 static void snd_async_handler(int signo ATTRIBUTE_UNUSED, siginfo_t *siginfo, void *context ATTRIBUTE_UNUSED)
59 //assert(siginfo->si_code == SI_SIGIO);
61 && (unsigned long)(previous_action.sa_sigaction) > MAX_SIG_FUNCTION_CODE)
62 previous_action.sa_sigaction(signo, siginfo, context);
64 list_for_each(i, &snd_async_handlers) {
65 snd_async_handler_t *h = list_entry(i, snd_async_handler_t, glist);
66 if (h->fd == fd && h->callback)
72 * \brief Registers an async handler.
73 * \param handler The function puts the pointer to the new async handler
74 * object at the address specified by \p handler.
75 * \param fd The file descriptor to be associated with the callback.
76 * \param callback The async callback function.
77 * \param private_data Private data for the async callback function.
78 * \result Zero if successful, otherwise a negative error code.
80 * This function associates the callback function with the given file,
81 * and saves this association in a \c snd_async_handler_t object.
83 * Whenever the \c SIGIO signal is raised for the file \p fd, the callback
84 * function will be called with its parameter pointing to the async handler
85 * object returned by this function.
87 * The ALSA \c sigaction handler for the \c SIGIO signal automatically
88 * multiplexes the notifications to the registered async callbacks.
89 * However, the application is responsible for instructing the device driver
90 * to generate the \c SIGIO signal.
92 * The \c SIGIO signal may have been replaced with another signal,
93 * see #snd_async_handler_get_signo.
95 * When the async handler isn't needed anymore, you must delete it with
96 * #snd_async_del_handler.
98 * \see snd_async_add_pcm_handler, snd_async_add_ctl_handler
100 int snd_async_add_handler(snd_async_handler_t **handler, int fd,
101 snd_async_callback_t callback, void *private_data)
103 snd_async_handler_t *h;
106 h = malloc(sizeof(*h));
110 h->callback = callback;
111 h->private_data = private_data;
112 was_empty = list_empty(&snd_async_handlers);
113 list_add_tail(&h->glist, &snd_async_handlers);
114 INIT_LIST_HEAD(&h->hlist);
118 struct sigaction act;
119 memset(&act, 0, sizeof(act));
120 act.sa_flags = SA_RESTART | SA_SIGINFO;
121 act.sa_sigaction = snd_async_handler;
122 sigemptyset(&act.sa_mask);
123 assert(!previous_action.sa_sigaction);
124 err = sigaction(snd_async_signo, &act, &previous_action);
134 * \brief Deletes an async handler.
135 * \param handler Handle of the async handler to delete.
136 * \result Zero if successful, otherwise a negative error code.
138 int snd_async_del_handler(snd_async_handler_t *handler)
141 int was_empty = list_empty(&snd_async_handlers);
143 list_del(&handler->glist);
145 && list_empty(&snd_async_handlers)) {
146 err = sigaction(snd_async_signo, &previous_action, NULL);
151 memset(&previous_action, 0, sizeof(previous_action));
153 if (handler->type == SND_ASYNC_HANDLER_GENERIC)
155 if (!list_empty(&handler->hlist))
156 list_del(&handler->hlist);
157 if (!list_empty(&handler->hlist))
159 switch (handler->type) {
161 case SND_ASYNC_HANDLER_PCM:
162 err = snd_pcm_async(handler->u.pcm, -1, 1);
165 case SND_ASYNC_HANDLER_CTL:
166 err = snd_ctl_async(handler->u.ctl, -1, 1);
177 * \brief Returns the signal number assigned to an async handler.
178 * \param handler Handle to an async handler.
179 * \result The signal number if successful, otherwise a negative error code.
181 * The signal number for async handlers usually is \c SIGIO,
182 * but wizards can redefine it to a realtime signal
183 * when compiling the ALSA library.
185 int snd_async_handler_get_signo(snd_async_handler_t *handler)
188 return snd_async_signo;
192 * \brief Returns the file descriptor assigned to an async handler.
193 * \param handler Handle to an async handler.
194 * \result The file descriptor if successful, otherwise a negative error code.
196 int snd_async_handler_get_fd(snd_async_handler_t *handler)
203 * \brief Returns the private data assigned to an async handler.
204 * \param handler Handle to an async handler.
205 * \result The \c private_data value registered with the async handler.
207 void *snd_async_handler_get_callback_private(snd_async_handler_t *handler)
210 return handler->private_data;