1 /**
2 * \file async.c
3 * \brief Async notification helpers
4 * \author Abramo Bagnara <abramo@alsa-project.org>
5 * \date 2001
6 */
7 /*
8 * Async notification helpers
9 * Copyright (c) 2001 by Abramo Bagnara <abramo@alsa-project.org>
10 *
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.
15 *
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.
20 *
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
24 *
25 */
26
27 #include "pcm/pcm_local.h"
28 #include "control/control_local.h"
29 #include <signal.h>
30
31 static struct sigaction previous_action;
32 #define MAX_SIG_FUNCTION_CODE 10 /* i.e. SIG_DFL SIG_IGN SIG_HOLD et al */
33
34 #ifdef SND_ASYNC_RT_SIGNAL
35 /** async signal number */
36 static int snd_async_signo;
37
38 void snd_async_init(void) __attribute__ ((constructor));
39
snd_async_init(void)40 void snd_async_init(void)
41 {
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");
45 exit(1);
46 }
47 }
48 #else
49 /** async signal number */
50 static const int snd_async_signo = SIGIO;
51 #endif
52
53 static LIST_HEAD(snd_async_handlers);
54
snd_async_handler(int signo ATTRIBUTE_UNUSED,siginfo_t * siginfo,void * context ATTRIBUTE_UNUSED)55 static void snd_async_handler(int signo ATTRIBUTE_UNUSED, siginfo_t *siginfo, void *context ATTRIBUTE_UNUSED)
56 {
57 int fd;
58 struct list_head *i;
59 //assert(siginfo->si_code == SI_SIGIO);
60 if (signo == SIGIO
61 && (unsigned long)(previous_action.sa_sigaction) > MAX_SIG_FUNCTION_CODE)
62 previous_action.sa_sigaction(signo, siginfo, context);
63 fd = siginfo->si_fd;
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)
67 h->callback(h);
68 }
69 }
70
71 /**
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.
79 *
80 * This function associates the callback function with the given file,
81 * and saves this association in a \c snd_async_handler_t object.
82 *
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.
86 *
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.
91 *
92 * The \c SIGIO signal may have been replaced with another signal,
93 * see #snd_async_handler_get_signo.
94 *
95 * When the async handler isn't needed anymore, you must delete it with
96 * #snd_async_del_handler.
97 *
98 * \see snd_async_add_pcm_handler, snd_async_add_ctl_handler
99 */
snd_async_add_handler(snd_async_handler_t ** handler,int fd,snd_async_callback_t callback,void * private_data)100 int snd_async_add_handler(snd_async_handler_t **handler, int fd,
101 snd_async_callback_t callback, void *private_data)
102 {
103 snd_async_handler_t *h;
104 int was_empty;
105 assert(handler);
106 h = malloc(sizeof(*h));
107 if (!h)
108 return -ENOMEM;
109 h->fd = fd;
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);
115 *handler = h;
116 if (was_empty) {
117 int err;
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);
125 if (err < 0) {
126 SYSERR("sigaction");
127 return -errno;
128 }
129 }
130 return 0;
131 }
132
133 /**
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.
137 */
snd_async_del_handler(snd_async_handler_t * handler)138 int snd_async_del_handler(snd_async_handler_t *handler)
139 {
140 int err = 0;
141 int was_empty = list_empty(&snd_async_handlers);
142 assert(handler);
143 list_del(&handler->glist);
144 if (!was_empty
145 && list_empty(&snd_async_handlers)) {
146 err = sigaction(snd_async_signo, &previous_action, NULL);
147 if (err < 0) {
148 SYSERR("sigaction");
149 return -errno;
150 }
151 memset(&previous_action, 0, sizeof(previous_action));
152 }
153 if (handler->type == SND_ASYNC_HANDLER_GENERIC)
154 goto _end;
155 if (!list_empty(&handler->hlist))
156 list_del(&handler->hlist);
157 if (!list_empty(&handler->hlist))
158 goto _end;
159 switch (handler->type) {
160 #ifdef BUILD_PCM
161 case SND_ASYNC_HANDLER_PCM:
162 err = snd_pcm_async(handler->u.pcm, -1, 1);
163 break;
164 #endif
165 case SND_ASYNC_HANDLER_CTL:
166 err = snd_ctl_async(handler->u.ctl, -1, 1);
167 break;
168 default:
169 assert(0);
170 }
171 _end:
172 free(handler);
173 return err;
174 }
175
176 /**
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.
180 *
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.
184 */
snd_async_handler_get_signo(snd_async_handler_t * handler)185 int snd_async_handler_get_signo(snd_async_handler_t *handler)
186 {
187 assert(handler);
188 return snd_async_signo;
189 }
190
191 /**
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.
195 */
snd_async_handler_get_fd(snd_async_handler_t * handler)196 int snd_async_handler_get_fd(snd_async_handler_t *handler)
197 {
198 assert(handler);
199 return handler->fd;
200 }
201
202 /**
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.
206 */
snd_async_handler_get_callback_private(snd_async_handler_t * handler)207 void *snd_async_handler_get_callback_private(snd_async_handler_t *handler)
208 {
209 assert(handler);
210 return handler->private_data;
211 }
212
213