]> code.delx.au - pulseaudio/blob - polyp/mainloop-signal.c
add simple hardware auto detection module
[pulseaudio] / polyp / mainloop-signal.c
1 /* $Id$ */
2
3 /***
4 This file is part of polypaudio.
5
6 polypaudio is free software; you can redistribute it and/or modify
7 it under the terms of the GNU Lesser General Public License as published
8 by the Free Software Foundation; either version 2 of the License,
9 or (at your option) any later version.
10
11 polypaudio is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
15
16 You should have received a copy of the GNU Lesser General Public License
17 along with polypaudio; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19 USA.
20 ***/
21
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25
26 #include <stdio.h>
27 #include <assert.h>
28 #include <signal.h>
29 #include <errno.h>
30 #include <stdlib.h>
31 #include <string.h>
32 #include <unistd.h>
33 #include <fcntl.h>
34
35 #ifdef HAVE_WINDOWS_H
36 #include <windows.h>
37 #endif
38
39 #include "mainloop-signal.h"
40 #include "util.h"
41 #include "xmalloc.h"
42 #include "log.h"
43 #include "gccmacro.h"
44
45 struct pa_signal_event {
46 int sig;
47 #ifdef HAVE_SIGACTION
48 struct sigaction saved_sigaction;
49 #else
50 void (*saved_handler)(int sig);
51 #endif
52 void (*callback) (pa_mainloop_api*a, pa_signal_event *e, int sig, void *userdata);
53 void *userdata;
54 void (*destroy_callback) (pa_mainloop_api*a, pa_signal_event*e, void *userdata);
55 pa_signal_event *previous, *next;
56 };
57
58 static pa_mainloop_api *api = NULL;
59 static int signal_pipe[2] = { -1, -1 };
60 static pa_io_event* io_event = NULL;
61 static pa_defer_event *defer_event = NULL;
62 static pa_signal_event *signals = NULL;
63
64 #ifdef OS_IS_WIN32
65 static unsigned int waiting_signals = 0;
66 static CRITICAL_SECTION crit;
67 #endif
68
69 static void signal_handler(int sig) {
70 #ifndef HAVE_SIGACTION
71 signal(sig, signal_handler);
72 #endif
73 write(signal_pipe[1], &sig, sizeof(sig));
74
75 #ifdef OS_IS_WIN32
76 EnterCriticalSection(&crit);
77 waiting_signals++;
78 LeaveCriticalSection(&crit);
79 #endif
80 }
81
82 static void dispatch(pa_mainloop_api*a, int sig) {
83 pa_signal_event*s;
84
85 for (s = signals; s; s = s->next)
86 if (s->sig == sig) {
87 assert(s->callback);
88 s->callback(a, s, sig, s->userdata);
89 break;
90 }
91 }
92
93 static void defer(pa_mainloop_api*a, PA_GCC_UNUSED pa_defer_event*e, PA_GCC_UNUSED void *userdata) {
94 ssize_t r;
95 int sig;
96 unsigned int sigs;
97
98 #ifdef OS_IS_WIN32
99 EnterCriticalSection(&crit);
100 sigs = waiting_signals;
101 waiting_signals = 0;
102 LeaveCriticalSection(&crit);
103 #endif
104
105 while (sigs) {
106 if ((r = read(signal_pipe[0], &sig, sizeof(sig))) < 0) {
107 pa_log(__FILE__": read(): %s\n", strerror(errno));
108 return;
109 }
110
111 if (r != sizeof(sig)) {
112 pa_log(__FILE__": short read()\n");
113 return;
114 }
115
116 dispatch(a, sig);
117
118 sigs--;
119 }
120 }
121
122 static void callback(pa_mainloop_api*a, pa_io_event*e, int fd, pa_io_event_flags_t f, PA_GCC_UNUSED void *userdata) {
123 ssize_t r;
124 int sig;
125 assert(a && e && f == PA_IO_EVENT_INPUT && e == io_event && fd == signal_pipe[0]);
126
127
128 if ((r = read(signal_pipe[0], &sig, sizeof(sig))) < 0) {
129 if (errno == EAGAIN)
130 return;
131
132 pa_log(__FILE__": read(): %s\n", strerror(errno));
133 return;
134 }
135
136 if (r != sizeof(sig)) {
137 pa_log(__FILE__": short read()\n");
138 return;
139 }
140
141 dispatch(a, sig);
142 }
143
144 int pa_signal_init(pa_mainloop_api *a) {
145 assert(!api && a && signal_pipe[0] == -1 && signal_pipe[1] == -1 && !io_event && !defer_event);
146
147 #ifdef OS_IS_WIN32
148 if (_pipe(signal_pipe, 200, _O_BINARY) < 0) {
149 #else
150 if (pipe(signal_pipe) < 0) {
151 #endif
152 pa_log(__FILE__": pipe() failed: %s\n", strerror(errno));
153 return -1;
154 }
155
156 pa_make_nonblock_fd(signal_pipe[0]);
157 pa_make_nonblock_fd(signal_pipe[1]);
158 pa_fd_set_cloexec(signal_pipe[0], 1);
159 pa_fd_set_cloexec(signal_pipe[1], 1);
160
161 api = a;
162
163 #ifndef OS_IS_WIN32
164 io_event = api->io_new(api, signal_pipe[0], PA_IO_EVENT_INPUT, callback, NULL);
165 assert(io_event);
166 #else
167 defer_event = api->defer_new(api, defer, NULL);
168 assert(defer_event);
169
170 InitializeCriticalSection(&crit);
171 #endif
172
173 return 0;
174 }
175
176 void pa_signal_done(void) {
177 assert(api && signal_pipe[0] >= 0 && signal_pipe[1] >= 0 && (io_event || defer_event));
178
179 while (signals)
180 pa_signal_free(signals);
181
182
183 #ifndef OS_IS_WIN32
184 api->io_free(io_event);
185 io_event = NULL;
186 #else
187 api->defer_free(defer_event);
188 defer_event = NULL;
189
190 DeleteCriticalSection(&crit);
191 #endif
192
193 close(signal_pipe[0]);
194 close(signal_pipe[1]);
195 signal_pipe[0] = signal_pipe[1] = -1;
196
197 api = NULL;
198 }
199
200 pa_signal_event* pa_signal_new(int sig, void (*_callback) (pa_mainloop_api *api, pa_signal_event*e, int sig, void *userdata), void *userdata) {
201 pa_signal_event *e = NULL;
202
203 #ifdef HAVE_SIGACTION
204 struct sigaction sa;
205 #endif
206
207 assert(sig > 0 && _callback);
208
209 for (e = signals; e; e = e->next)
210 if (e->sig == sig)
211 goto fail;
212
213 e = pa_xmalloc(sizeof(pa_signal_event));
214 e->sig = sig;
215 e->callback = _callback;
216 e->userdata = userdata;
217 e->destroy_callback = NULL;
218
219 #ifdef HAVE_SIGACTION
220 memset(&sa, 0, sizeof(sa));
221 sa.sa_handler = signal_handler;
222 sigemptyset(&sa.sa_mask);
223 sa.sa_flags = SA_RESTART;
224
225 if (sigaction(sig, &sa, &e->saved_sigaction) < 0)
226 #else
227 if ((e->saved_handler = signal(sig, signal_handler)) == SIG_ERR)
228 #endif
229 goto fail;
230
231 e->previous = NULL;
232 e->next = signals;
233 signals = e;
234
235 return e;
236 fail:
237 if (e)
238 pa_xfree(e);
239 return NULL;
240 }
241
242 void pa_signal_free(pa_signal_event *e) {
243 assert(e);
244
245 if (e->next)
246 e->next->previous = e->previous;
247 if (e->previous)
248 e->previous->next = e->next;
249 else
250 signals = e->next;
251
252 #ifdef HAVE_SIGACTION
253 sigaction(e->sig, &e->saved_sigaction, NULL);
254 #else
255 signal(e->sig, e->saved_handler);
256 #endif
257
258 if (e->destroy_callback)
259 e->destroy_callback(api, e, e->userdata);
260
261 pa_xfree(e);
262 }
263
264 void pa_signal_set_destroy(pa_signal_event *e, void (*_callback) (pa_mainloop_api *api, pa_signal_event*e, void *userdata)) {
265 assert(e);
266 e->destroy_callback = _callback;
267 }