[3460] | 1 | //mainloop.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 | |
---|
[3462] | 41 | struct pa_mainloop { |
---|
| 42 | pa_mainloop_api api; |
---|
| 43 | pa_poll_func poll_func; |
---|
| 44 | void * poll_userdata; |
---|
| 45 | int quit; |
---|
| 46 | int quitval; |
---|
| 47 | }; |
---|
| 48 | |
---|
[3463] | 49 | struct pa_io_event { |
---|
| 50 | pa_mainloop_api *api; |
---|
| 51 | int fd; |
---|
| 52 | pa_io_event_flags_t events; |
---|
| 53 | pa_io_event_cb_t cb; |
---|
| 54 | void *userdata; |
---|
| 55 | }; |
---|
| 56 | |
---|
[3461] | 57 | /** Allocate a new main loop object */ |
---|
[3462] | 58 | pa_mainloop *pa_mainloop_new(void) { |
---|
| 59 | pa_mainloop * m = roar_mm_malloc(sizeof(pa_mainloop)); |
---|
| 60 | |
---|
| 61 | if ( m == NULL ) |
---|
| 62 | return NULL; |
---|
| 63 | |
---|
| 64 | memset(m, 0, sizeof(pa_mainloop)); |
---|
| 65 | |
---|
| 66 | m->api.userdata = m; |
---|
| 67 | |
---|
| 68 | return m; |
---|
| 69 | } |
---|
[3461] | 70 | |
---|
| 71 | /** Free a main loop object */ |
---|
[3462] | 72 | void pa_mainloop_free(pa_mainloop* m) { |
---|
| 73 | if ( m == NULL ) |
---|
| 74 | return; |
---|
| 75 | |
---|
| 76 | roar_mm_free(m); |
---|
| 77 | } |
---|
[3461] | 78 | |
---|
| 79 | /** Prepare for a single iteration of the main loop. Returns a negative value |
---|
| 80 | on error or exit request. timeout specifies a maximum timeout for the subsequent |
---|
| 81 | poll, or -1 for blocking behaviour. .*/ |
---|
[3462] | 82 | int pa_mainloop_prepare(pa_mainloop *m, int timeout) { |
---|
| 83 | if ( m == NULL ) |
---|
| 84 | return -1; |
---|
| 85 | |
---|
| 86 | m->quit = 1; |
---|
| 87 | |
---|
| 88 | if ( m->quit ) |
---|
| 89 | return -2; |
---|
| 90 | |
---|
| 91 | return -1; |
---|
| 92 | } |
---|
[3461] | 93 | |
---|
| 94 | /** Execute the previously prepared poll. Returns a negative value on error.*/ |
---|
[3462] | 95 | int pa_mainloop_poll(pa_mainloop *m) { |
---|
| 96 | if ( m == NULL ) |
---|
| 97 | return -1; |
---|
| 98 | |
---|
| 99 | if ( m->quit ) |
---|
| 100 | return -2; |
---|
| 101 | |
---|
| 102 | return -1; |
---|
| 103 | } |
---|
[3461] | 104 | |
---|
| 105 | /** Dispatch timeout, io and deferred events from the previously executed poll. Returns |
---|
| 106 | a negative value on error. On success returns the number of source dispatched. */ |
---|
[3462] | 107 | int pa_mainloop_dispatch(pa_mainloop *m) { |
---|
| 108 | if ( m == NULL ) |
---|
| 109 | return -1; |
---|
| 110 | |
---|
| 111 | if ( m->quit ) |
---|
| 112 | return -2; |
---|
| 113 | |
---|
| 114 | return -1; |
---|
| 115 | } |
---|
[3461] | 116 | |
---|
| 117 | /** Return the return value as specified with the main loop's quit() routine. */ |
---|
[3462] | 118 | int pa_mainloop_get_retval(pa_mainloop *m) { |
---|
| 119 | if ( m == NULL ) |
---|
| 120 | return -1; |
---|
| 121 | |
---|
| 122 | return m->quitval; |
---|
| 123 | } |
---|
[3461] | 124 | |
---|
| 125 | /** Run a single iteration of the main loop. This is a convenience function |
---|
| 126 | for pa_mainloop_prepare(), pa_mainloop_poll() and pa_mainloop_dispatch(). |
---|
| 127 | Returns a negative value on error or exit request. If block is nonzero, |
---|
| 128 | block for events if none are queued. Optionally return the return value as |
---|
| 129 | specified with the main loop's quit() routine in the integer variable retval points |
---|
| 130 | to. On success returns the number of sources dispatched in this iteration. */ |
---|
[3462] | 131 | int pa_mainloop_iterate(pa_mainloop *m, int block, int *retval) { |
---|
| 132 | int r; |
---|
| 133 | |
---|
| 134 | if ( m == NULL ) |
---|
| 135 | return -1; |
---|
| 136 | |
---|
| 137 | r = pa_mainloop_prepare(m, block ? -1 : 0); |
---|
| 138 | |
---|
| 139 | if ( r > 0 ) |
---|
| 140 | r = pa_mainloop_poll(m); |
---|
| 141 | |
---|
| 142 | if ( r > 0 ) |
---|
| 143 | r = pa_mainloop_dispatch(m); |
---|
| 144 | |
---|
| 145 | if ( r == -2 && retval != NULL ) { |
---|
| 146 | *retval = m->quitval; |
---|
| 147 | } |
---|
| 148 | |
---|
| 149 | return r; |
---|
| 150 | } |
---|
[3461] | 151 | |
---|
| 152 | /** Run unlimited iterations of the main loop object until the main loop's quit() routine is called. */ |
---|
[3462] | 153 | int pa_mainloop_run(pa_mainloop *m, int *retval) { |
---|
| 154 | int r = 1; |
---|
| 155 | |
---|
| 156 | if ( m == NULL ) |
---|
| 157 | return -1; |
---|
| 158 | |
---|
| 159 | while (!(m->quit) && r > 0) { |
---|
| 160 | r = pa_mainloop_iterate(m, 1, retval); |
---|
| 161 | } |
---|
| 162 | |
---|
| 163 | if ( r == -2 ) |
---|
| 164 | return 1; |
---|
| 165 | |
---|
| 166 | if ( r < 0 ) |
---|
| 167 | return -1; |
---|
| 168 | |
---|
| 169 | return 0; |
---|
| 170 | } |
---|
[3461] | 171 | |
---|
| 172 | /** Return the abstract main loop abstraction layer vtable for this main loop. */ |
---|
[3462] | 173 | pa_mainloop_api* pa_mainloop_get_api(pa_mainloop*m) { |
---|
| 174 | if ( m == NULL ) |
---|
| 175 | return NULL; |
---|
| 176 | |
---|
| 177 | return &(m->api); |
---|
| 178 | } |
---|
[3461] | 179 | |
---|
| 180 | /** Shutdown the main loop */ |
---|
[3462] | 181 | void pa_mainloop_quit(pa_mainloop *m, int r) { |
---|
| 182 | if ( m == NULL ) |
---|
| 183 | return; |
---|
| 184 | |
---|
| 185 | m->quitval = r; |
---|
| 186 | m->quit = 1; |
---|
| 187 | } |
---|
[3461] | 188 | |
---|
| 189 | /** Interrupt a running poll (for threaded systems) */ |
---|
| 190 | void pa_mainloop_wakeup(pa_mainloop *m); |
---|
| 191 | |
---|
[3462] | 192 | /** Change the poll() implementation */ |
---|
| 193 | void pa_mainloop_set_poll_func(pa_mainloop *m, pa_poll_func poll_func, void *userdata) { |
---|
| 194 | if ( m == NULL ) |
---|
| 195 | return; |
---|
[3461] | 196 | |
---|
[3462] | 197 | m->poll_func = poll_func; |
---|
| 198 | m->poll_userdata = userdata; |
---|
| 199 | } |
---|
[3461] | 200 | |
---|
[3460] | 201 | //ll |
---|