source: roaraudio/libroarpulse/mainloop-signal.c @ 3471:8c6856b26672

Last change on this file since 3471:8c6856b26672 was 3471:8c6856b26672, checked in by phi, 14 years ago

worte unix signal stuff

File size: 3.5 KB
Line 
1//mainloop-signal.c:
2
3/*
4 *      Copyright (C) Philipp 'ph3-der-loewe' Schafft - 2010
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, 675 Mass Ave, Cambridge, MA 02139, USA.
26 *
27 *  NOTE for everyone want's to change something and send patches:
28 *  read README and HACKING! There a addition information on
29 *  the license of this document you need to read before you send
30 *  any patches.
31 *
32 *  NOTE for uses of non-GPL (LGPL,...) software using libesd, libartsc
33 *  or libpulse*:
34 *  The libs libroaresd, libroararts and libroarpulse link this libroar
35 *  and are therefore GPL. Because of this it may be illigal to use
36 *  them with any software that uses libesd, libartsc or libpulse*.
37 */
38
39#include <libroarpulse/libroarpulse.h>
40
41#define MAX_SIG 64 /* there is no way to find out */
42
43typedef void (*pa_signal_cb_t) (pa_mainloop_api *api, pa_signal_event*e, int sig, void *userdata);
44
45struct pa_signal_event {
46 int used;
47 int sig;
48 pa_signal_cb_t cb;
49 void * userdata;
50};
51
52static int _roar_pa_signal_inited = 0;
53
54static struct {
55 pa_mainloop_api * api;
56 pa_signal_event sig[MAX_SIG];
57} _roar_pa_signal;
58
59static void _roar_pa_signal_handler (int sig) {
60 pa_signal_event * e = &(_roar_pa_signal.sig[sig]);
61
62 if ( e->cb != NULL )
63  e->cb(_roar_pa_signal.api, e, sig, e->userdata);
64}
65
66/** Initialize the UNIX signal subsystem and bind it to the specified main loop */
67int pa_signal_init(pa_mainloop_api *api) {
68
69 if ( _roar_pa_signal_inited )
70  return -1;
71
72 memset(&_roar_pa_signal, 0, sizeof(_roar_pa_signal));
73
74 _roar_pa_signal.api = api;
75
76 _roar_pa_signal_inited = 1;
77
78 return 0;
79}
80
81/** Cleanup the signal subsystem */
82void pa_signal_done(void) {
83 _roar_pa_signal_inited = 0;
84}
85
86/** Create a new UNIX signal event source object */
87pa_signal_event* pa_signal_new(int sig, pa_signal_cb_t callback, void *userdata) {
88 if ( !_roar_pa_signal_inited )
89  return NULL;
90
91 if ( sig >= MAX_SIG )
92  return NULL;
93
94 _roar_pa_signal.sig[sig].used     = 1;
95 _roar_pa_signal.sig[sig].sig      = sig;
96 _roar_pa_signal.sig[sig].cb       = callback;
97 _roar_pa_signal.sig[sig].userdata = userdata;
98
99 signal(sig, _roar_pa_signal_handler);
100
101 return &(_roar_pa_signal.sig[sig]);
102}
103
104/** Free a UNIX signal event source object */
105void pa_signal_free(pa_signal_event *e) {
106 if ( !_roar_pa_signal_inited )
107  return;
108
109 if ( e == NULL )
110  return;
111
112 signal(e->sig, SIG_DFL);
113
114 e->used = 0;
115}
116
117/** Set a function that is called when the signal event source is destroyed. Use this to free the userdata argument if required */
118void pa_signal_set_destroy(pa_signal_event *e, void (*callback) (pa_mainloop_api *api, pa_signal_event*e, void *userdata));
119
120//ll
Note: See TracBrowser for help on using the repository browser.