4 This file is part of polypaudio.
6 polypaudio is free software; you can redistribute it and/or modify
7 it under the terms of the GNU 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.
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.
16 You should have received a copy of the GNU 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
34 #include "mainloop-signal.h"
39 struct sigaction saved_sigaction
;
40 void (*callback
) (void *id
, int signal
, void *userdata
);
42 struct signal_info
*previous
, *next
;
45 static struct pa_mainloop_api
*api
= NULL
;
46 static int signal_pipe
[2] = { -1, -1 };
47 static void* mainloop_source
= NULL
;
48 static struct signal_info
*signals
= NULL
;
50 static void signal_handler(int sig
) {
51 write(signal_pipe
[1], &sig
, sizeof(sig
));
54 static void callback(struct pa_mainloop_api
*a
, void *id
, int fd
, enum pa_mainloop_api_io_events events
, void *userdata
) {
55 assert(a
&& id
&& events
== PA_MAINLOOP_API_IO_EVENT_INPUT
&& id
== mainloop_source
&& fd
== signal_pipe
[0]);
62 if ((r
= read(signal_pipe
[0], &sig
, sizeof(sig
))) < 0) {
66 fprintf(stderr
, "signal.c: read(): %s\n", strerror(errno
));
70 if (r
!= sizeof(sig
)) {
71 fprintf(stderr
, "signal.c: short read()\n");
75 for (s
= signals
; s
; s
= s
->next
)
78 s
->callback(s
, sig
, s
->userdata
);
84 int pa_signal_init(struct pa_mainloop_api
*a
) {
86 if (pipe(signal_pipe
) < 0) {
87 fprintf(stderr
, "pipe() failed: %s\n", strerror(errno
));
91 pa_make_nonblock_fd(signal_pipe
[0]);
92 pa_make_nonblock_fd(signal_pipe
[1]);
95 mainloop_source
= api
->source_io(api
, signal_pipe
[0], PA_MAINLOOP_API_IO_EVENT_INPUT
, callback
, NULL
);
96 assert(mainloop_source
);
100 void pa_signal_done(void) {
101 assert(api
&& signal_pipe
[0] >= 0 && signal_pipe
[1] >= 0 && mainloop_source
);
103 api
->cancel_io(api
, mainloop_source
);
104 mainloop_source
= NULL
;
106 close(signal_pipe
[0]);
107 close(signal_pipe
[1]);
108 signal_pipe
[0] = signal_pipe
[1] = -1;
111 pa_signal_unregister(signals
);
116 void* pa_signal_register(int sig
, void (*callback
) (void *id
, int signal
, void *userdata
), void *userdata
) {
117 struct signal_info
*s
= NULL
;
119 assert(sig
> 0 && callback
);
121 for (s
= signals
; s
; s
= s
->next
)
125 s
= malloc(sizeof(struct signal_info
));
128 s
->callback
= callback
;
129 s
->userdata
= userdata
;
131 memset(&sa
, 0, sizeof(sa
));
132 sa
.sa_handler
= signal_handler
;
133 sigemptyset(&sa
.sa_mask
);
134 sa
.sa_flags
= SA_RESTART
;
136 if (sigaction(sig
, &sa
, &s
->saved_sigaction
) < 0)
150 void pa_signal_unregister(void *id
) {
151 struct signal_info
*s
= id
;
155 s
->next
->previous
= s
->previous
;
157 s
->previous
->next
= s
->next
;
161 sigaction(s
->sig
, &s
->saved_sigaction
, NULL
);