source: roaraudio/libroarpulse/mainloop.c @ 3462:c8823247312c

Last change on this file since 3462:c8823247312c was 3462:c8823247312c, checked in by phi, 14 years ago

wrote basic framework

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