source: roaraudio/libroarpulse/mainloop-signal.c @ 4896:0cd0dc3bc104

Last change on this file since 4896:0cd0dc3bc104 was 4708:c9d40761088a, checked in by phi, 13 years ago

updated copyright statements

File size: 4.8 KB
Line 
1//mainloop-signal.c:
2
3/*
4 *      Copyright (C) Philipp 'ph3-der-loewe' Schafft - 2010-2011
5 *  The code (may) include prototypes and comments (and maybe
6 *  other code fragements) from libpulse*. They are mostly copyrighted by:
7 *  Lennart Poettering <poettering@users.sourceforge.net> and
8 *  Pierre Ossman <drzeus@drzeus.cx>
9 *
10 *  This file is part of libroarpulse a part of RoarAudio,
11 *  a cross-platform sound system for both, home and professional use.
12 *  See README for details.
13 *
14 *  This file is free software; you can redistribute it and/or modify
15 *  it under the terms of the GNU General Public License version 3
16 *  as published by the Free Software Foundation.
17 *
18 *  RoarAudio is distributed in the hope that it will be useful,
19 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
20 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21 *  GNU General Public License for more details.
22 *
23 *  You should have received a copy of the GNU General Public License
24 *  along with this software; see the file COPYING.  If not, write to
25 *  the Free Software Foundation, 51 Franklin Street, Fifth Floor,
26 *  Boston, MA 02110-1301, USA.
27 *
28 *  NOTE for everyone want's to change something and send patches:
29 *  read README and HACKING! There a addition information on
30 *  the license of this document you need to read before you send
31 *  any patches.
32 *
33 *  NOTE for uses of non-GPL (LGPL,...) software using libesd, libartsc
34 *  or libpulse*:
35 *  The libs libroaresd, libroararts and libroarpulse link this libroar
36 *  and are therefore GPL. Because of this it may be illigal to use
37 *  them with any software that uses libesd, libartsc or libpulse*.
38 */
39
40#include <libroarpulse/libroarpulse.h>
41
42#define MAX_SIG 64 /* there is no way to find out */
43
44typedef void (*pa_signal_cb_t) (pa_mainloop_api *api, pa_signal_event*e, int sig, void *userdata);
45
46struct pa_signal_event {
47 int used;
48 int sig;
49 pa_signal_cb_t cb;
50 void * userdata;
51};
52
53static int _roar_pa_signal_inited = 0;
54
55static struct {
56 pa_mainloop_api * api;
57 pa_signal_event sig[MAX_SIG];
58#ifdef ROAR_HAVE_PIPE
59 int pipefh[2];
60 pa_io_event * io_event;
61#endif
62} _roar_pa_signal;
63
64static void _roar_pa_signal_iocb(
65#ifndef ROAR_HAVE_PIPE
66                                 int                 sig
67#else
68                                 pa_mainloop_api   * a,
69                                 pa_io_event       * e,
70                                 int                 fd,
71                                 pa_io_event_flags_t f,
72                                 void *userdata
73#endif
74                                ) {
75 pa_signal_event * se;
76#ifdef ROAR_HAVE_PIPE
77 int sig;
78 size_t ret;
79
80 ret = read(fd, &sig, sizeof(sig));
81
82 if ( ret != sizeof(sig) )
83  return;
84#endif
85
86 if ( sig >= MAX_SIG )
87  return;
88
89 se = &(_roar_pa_signal.sig[sig]);
90
91 if ( !se->used )
92  return;
93
94 ROAR_DBG("_roar_pa_signal_iocb(*): sig=%s(%i), se->cb=%p", strsignal(sig), sig, se->cb);
95
96 if ( se->cb != NULL )
97  se->cb(_roar_pa_signal.api, se, sig, se->userdata);
98}
99
100static void _roar_pa_signal_handler (int sig) {
101#ifdef ROAR_HAVE_PIPE
102 write(_roar_pa_signal.pipefh[1], &sig, sizeof(sig));
103#else
104 _roar_pa_signal_iocb(sig);
105#endif
106}
107
108/** Initialize the UNIX signal subsystem and bind it to the specified main loop */
109int pa_signal_init(pa_mainloop_api *api) {
110
111 if ( _roar_pa_signal_inited )
112  return -1;
113
114 memset(&_roar_pa_signal, 0, sizeof(_roar_pa_signal));
115
116 _roar_pa_signal.api = api;
117
118#ifdef ROAR_HAVE_PIPE
119 if ( pipe(_roar_pa_signal.pipefh) == -1 )
120  return -1;
121
122 _roar_pa_signal.io_event = api->io_new(api, _roar_pa_signal.pipefh[0], PA_IO_EVENT_INPUT, _roar_pa_signal_iocb, NULL);
123#endif
124
125 _roar_pa_signal_inited = 1;
126
127 return 0;
128}
129
130/** Cleanup the signal subsystem */
131void pa_signal_done(void) {
132 int i;
133
134 for (i = 0; i < MAX_SIG; i++) {
135  _roar_pa_signal.sig[i].used = 0;
136 }
137
138#ifdef ROAR_HAVE_PIPE
139 _roar_pa_signal.api->io_free(_roar_pa_signal.io_event);
140#endif
141
142 _roar_pa_signal_inited = 0;
143}
144
145/** Create a new UNIX signal event source object */
146pa_signal_event* pa_signal_new(int sig, pa_signal_cb_t callback, void *userdata) {
147 if ( !_roar_pa_signal_inited )
148  return NULL;
149
150 if ( sig >= MAX_SIG )
151  return NULL;
152
153 _roar_pa_signal.sig[sig].used     = 1;
154 _roar_pa_signal.sig[sig].sig      = sig;
155 _roar_pa_signal.sig[sig].cb       = callback;
156 _roar_pa_signal.sig[sig].userdata = userdata;
157
158 signal(sig, _roar_pa_signal_handler);
159
160 return &(_roar_pa_signal.sig[sig]);
161}
162
163/** Free a UNIX signal event source object */
164void pa_signal_free(pa_signal_event *e) {
165 if ( !_roar_pa_signal_inited )
166  return;
167
168 if ( e == NULL )
169  return;
170
171 signal(e->sig, SIG_DFL);
172
173 e->used = 0;
174}
175
176/** Set a function that is called when the signal event source is destroyed. Use this to free the userdata argument if required */
177void pa_signal_set_destroy(pa_signal_event *e, void (*callback) (pa_mainloop_api *api, pa_signal_event*e, void *userdata));
178
179//ll
Note: See TracBrowser for help on using the repository browser.