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"
40 struct sigaction saved_sigaction
;
41 void (*callback
) (void *id
, int signal
, void *userdata
);
43 struct signal_info
*previous
, *next
;
46 static struct pa_mainloop_api
*api
= NULL
;
47 static int signal_pipe
[2] = { -1, -1 };
48 static void* mainloop_source
= NULL
;
49 static struct signal_info
*signals
= NULL
;
51 static void signal_handler(int sig
) {
52 write(signal_pipe
[1], &sig
, sizeof(sig
));
55 static void callback(struct pa_mainloop_api
*a
, void *id
, int fd
, enum pa_mainloop_api_io_events events
, void *userdata
) {
56 assert(a
&& id
&& events
== PA_MAINLOOP_API_IO_EVENT_INPUT
&& id
== mainloop_source
&& fd
== signal_pipe
[0]);
63 if ((r
= read(signal_pipe
[0], &sig
, sizeof(sig
))) < 0) {
67 fprintf(stderr
, "signal.c: read(): %s\n", strerror(errno
));
71 if (r
!= sizeof(sig
)) {
72 fprintf(stderr
, "signal.c: short read()\n");
76 for (s
= signals
; s
; s
= s
->next
)
79 s
->callback(s
, sig
, s
->userdata
);
85 int pa_signal_init(struct pa_mainloop_api
*a
) {
87 if (pipe(signal_pipe
) < 0) {
88 fprintf(stderr
, "pipe() failed: %s\n", strerror(errno
));
92 pa_make_nonblock_fd(signal_pipe
[0]);
93 pa_make_nonblock_fd(signal_pipe
[1]);
96 mainloop_source
= api
->source_io(api
, signal_pipe
[0], PA_MAINLOOP_API_IO_EVENT_INPUT
, callback
, NULL
);
97 assert(mainloop_source
);
101 void pa_signal_done(void) {
102 assert(api
&& signal_pipe
[0] >= 0 && signal_pipe
[1] >= 0 && mainloop_source
);
104 api
->cancel_io(api
, mainloop_source
);
105 mainloop_source
= NULL
;
107 close(signal_pipe
[0]);
108 close(signal_pipe
[1]);
109 signal_pipe
[0] = signal_pipe
[1] = -1;
112 pa_signal_unregister(signals
);
117 void* pa_signal_register(int sig
, void (*callback
) (void *id
, int signal
, void *userdata
), void *userdata
) {
118 struct signal_info
*s
= NULL
;
120 assert(sig
> 0 && callback
);
122 for (s
= signals
; s
; s
= s
->next
)
126 s
= pa_xmalloc(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
);