[3470] | 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 | |
---|
[3471] | 41 | #define MAX_SIG 64 /* there is no way to find out */ |
---|
| 42 | |
---|
| 43 | typedef void (*pa_signal_cb_t) (pa_mainloop_api *api, pa_signal_event*e, int sig, void *userdata); |
---|
| 44 | |
---|
| 45 | struct pa_signal_event { |
---|
| 46 | int used; |
---|
| 47 | int sig; |
---|
| 48 | pa_signal_cb_t cb; |
---|
| 49 | void * userdata; |
---|
| 50 | }; |
---|
| 51 | |
---|
| 52 | static int _roar_pa_signal_inited = 0; |
---|
| 53 | |
---|
| 54 | static struct { |
---|
| 55 | pa_mainloop_api * api; |
---|
| 56 | pa_signal_event sig[MAX_SIG]; |
---|
[3480] | 57 | int pipefh[2]; |
---|
| 58 | pa_io_event * io_event; |
---|
[3471] | 59 | } _roar_pa_signal; |
---|
| 60 | |
---|
| 61 | static void _roar_pa_signal_handler (int sig) { |
---|
[3480] | 62 | write(_roar_pa_signal.pipefh[1], &sig, sizeof(sig)); |
---|
| 63 | } |
---|
[3473] | 64 | |
---|
[3480] | 65 | static void _roar_pa_signal_iocb(pa_mainloop_api * a, |
---|
| 66 | pa_io_event * e, |
---|
| 67 | int fd, |
---|
| 68 | pa_io_event_flags_t f, |
---|
| 69 | void *userdata ) { |
---|
| 70 | pa_signal_event * se; |
---|
| 71 | int sig; |
---|
| 72 | size_t ret; |
---|
| 73 | |
---|
| 74 | ret = read(fd, &sig, sizeof(sig)); |
---|
| 75 | |
---|
| 76 | if ( ret != sizeof(sig) ) |
---|
| 77 | return; |
---|
[3473] | 78 | |
---|
| 79 | if ( sig >= MAX_SIG ) |
---|
| 80 | return; |
---|
| 81 | |
---|
[3480] | 82 | se = &(_roar_pa_signal.sig[sig]); |
---|
[3473] | 83 | |
---|
[3480] | 84 | if ( !se->used ) |
---|
[3473] | 85 | return; |
---|
| 86 | |
---|
[3482] | 87 | ROAR_DBG("_roar_pa_signal_iocb(*): sig=%s(%i), se->cb=%p", strsignal(sig), sig, se->cb); |
---|
| 88 | |
---|
[3480] | 89 | if ( se->cb != NULL ) |
---|
| 90 | se->cb(_roar_pa_signal.api, se, sig, se->userdata); |
---|
[3471] | 91 | } |
---|
| 92 | |
---|
| 93 | /** Initialize the UNIX signal subsystem and bind it to the specified main loop */ |
---|
| 94 | int pa_signal_init(pa_mainloop_api *api) { |
---|
| 95 | |
---|
| 96 | if ( _roar_pa_signal_inited ) |
---|
| 97 | return -1; |
---|
| 98 | |
---|
| 99 | memset(&_roar_pa_signal, 0, sizeof(_roar_pa_signal)); |
---|
| 100 | |
---|
| 101 | _roar_pa_signal.api = api; |
---|
| 102 | |
---|
[3480] | 103 | if ( pipe(_roar_pa_signal.pipefh) == -1 ) |
---|
| 104 | return -1; |
---|
| 105 | |
---|
| 106 | _roar_pa_signal.io_event = api->io_new(api, _roar_pa_signal.pipefh[0], PA_IO_EVENT_INPUT, _roar_pa_signal_iocb, NULL); |
---|
| 107 | |
---|
[3471] | 108 | _roar_pa_signal_inited = 1; |
---|
| 109 | |
---|
| 110 | return 0; |
---|
| 111 | } |
---|
| 112 | |
---|
| 113 | /** Cleanup the signal subsystem */ |
---|
| 114 | void pa_signal_done(void) { |
---|
[3480] | 115 | int i; |
---|
| 116 | |
---|
| 117 | for (i = 0; i < MAX_SIG; i++) { |
---|
| 118 | _roar_pa_signal.sig[i].used = 0; |
---|
| 119 | } |
---|
| 120 | |
---|
| 121 | _roar_pa_signal.api->io_free(_roar_pa_signal.io_event); |
---|
| 122 | |
---|
[3471] | 123 | _roar_pa_signal_inited = 0; |
---|
| 124 | } |
---|
| 125 | |
---|
| 126 | /** Create a new UNIX signal event source object */ |
---|
| 127 | pa_signal_event* pa_signal_new(int sig, pa_signal_cb_t callback, void *userdata) { |
---|
| 128 | if ( !_roar_pa_signal_inited ) |
---|
| 129 | return NULL; |
---|
| 130 | |
---|
| 131 | if ( sig >= MAX_SIG ) |
---|
| 132 | return NULL; |
---|
| 133 | |
---|
| 134 | _roar_pa_signal.sig[sig].used = 1; |
---|
| 135 | _roar_pa_signal.sig[sig].sig = sig; |
---|
| 136 | _roar_pa_signal.sig[sig].cb = callback; |
---|
| 137 | _roar_pa_signal.sig[sig].userdata = userdata; |
---|
| 138 | |
---|
| 139 | signal(sig, _roar_pa_signal_handler); |
---|
| 140 | |
---|
| 141 | return &(_roar_pa_signal.sig[sig]); |
---|
| 142 | } |
---|
| 143 | |
---|
| 144 | /** Free a UNIX signal event source object */ |
---|
| 145 | void pa_signal_free(pa_signal_event *e) { |
---|
| 146 | if ( !_roar_pa_signal_inited ) |
---|
| 147 | return; |
---|
| 148 | |
---|
| 149 | if ( e == NULL ) |
---|
| 150 | return; |
---|
| 151 | |
---|
| 152 | signal(e->sig, SIG_DFL); |
---|
| 153 | |
---|
| 154 | e->used = 0; |
---|
| 155 | } |
---|
| 156 | |
---|
| 157 | /** Set a function that is called when the signal event source is destroyed. Use this to free the userdata argument if required */ |
---|
| 158 | void pa_signal_set_destroy(pa_signal_event *e, void (*callback) (pa_mainloop_api *api, pa_signal_event*e, void *userdata)); |
---|
| 159 | |
---|
[3470] | 160 | //ll |
---|