1 module pulse.mainloop;
2 
3 version(linux):
4 
5 import core.stdc.config;
6 
7 import pulse.mainloopapi;
8 
9 extern (C):
10 
11 /***
12   This file is part of PulseAudio.
13 
14   Copyright 2004-2006 Lennart Poettering
15   Copyright 2006 Pierre Ossman <ossman@cendio.se> for Cendio AB
16 
17   PulseAudio is free software; you can redistribute it and/or modify
18   it under the terms of the GNU Lesser General Public License as published
19   by the Free Software Foundation; either version 2.1 of the License,
20   or (at your option) any later version.
21 
22   PulseAudio is distributed in the hope that it will be useful, but
23   WITHOUT ANY WARRANTY; without even the implied warranty of
24   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
25   General Public License for more details.
26 
27   You should have received a copy of the GNU Lesser General Public License
28   along with PulseAudio; if not, see <http://www.gnu.org/licenses/>.
29 ***/
30 
31 struct pollfd;
32 
33 /** \page mainloop Main Loop
34  *
35  * \section overv_sec Overview
36  *
37  * The built-in main loop implementation is based on the poll() system call.
38  * It supports the functions defined in the main loop abstraction and very
39  * little else.
40  *
41  * The main loop is created using pa_mainloop_new() and destroyed using
42  * pa_mainloop_free(). To get access to the main loop abstraction,
43  * pa_mainloop_get_api() is used.
44  *
45  * \section iter_sec Iteration
46  *
47  * The main loop is designed around the concept of iterations. Each iteration
48  * consists of three steps that repeat during the application's entire
49  * lifetime:
50  *
51  * -# Prepare - Build a list of file descriptors
52  *               that need to be monitored and calculate the next timeout.
53  * -# Poll - Execute the actual poll() system call.
54  * -# Dispatch - Dispatch any events that have fired.
55  *
56  * When using the main loop, the application can either execute each
57  * iteration, one at a time, using pa_mainloop_iterate(), or let the library
58  * iterate automatically using pa_mainloop_run().
59  *
60  * \section thread_sec Threads
61  *
62  * The main loop functions are designed to be thread safe, but the objects
63  * are not. What this means is that multiple main loops can be used, but only
64  * one object per thread.
65  *
66  */
67 
68 /** \file
69  *
70  * A minimal main loop implementation based on the C library's poll()
71  * function. Using the routines defined herein you may create a simple
72  * main loop supporting the generic main loop abstraction layer as
73  * defined in \ref mainloop-api.h. This implementation is thread safe
74  * as long as you access the main loop object from a single thread only.
75  *
76  * See also \subpage mainloop
77  */
78 
79 /** An opaque main loop object */
80 struct pa_mainloop;
81 
82 /** Allocate a new main loop object. Free with pa_mainloop_free. */
83 pa_mainloop* pa_mainloop_new ();
84 
85 /** Free a main loop object */
86 void pa_mainloop_free (pa_mainloop* m);
87 
88 /** Prepare for a single iteration of the main loop. Returns a negative value
89 on error or exit request. timeout specifies a maximum timeout for the subsequent
90 poll, or -1 for blocking behaviour. The timeout is specified in microseconds. */
91 int pa_mainloop_prepare (pa_mainloop* m, int timeout);
92 
93 /** Execute the previously prepared poll. Returns a negative value on error.*/
94 int pa_mainloop_poll (pa_mainloop* m);
95 
96 /** Dispatch timeout, io and deferred events from the previously executed poll. Returns
97 a negative value on error. On success returns the number of source dispatched. */
98 int pa_mainloop_dispatch (pa_mainloop* m);
99 
100 /** Return the return value as specified with the main loop's quit() routine. */
101 int pa_mainloop_get_retval (const(pa_mainloop)* m);
102 
103 /** Run a single iteration of the main loop. This is a convenience function
104 for pa_mainloop_prepare(), pa_mainloop_poll() and pa_mainloop_dispatch().
105 Returns a negative value on error or exit request. If block is nonzero,
106 block for events if none are queued. Optionally return the return value as
107 specified with the main loop's quit() routine in the integer variable retval points
108 to. On success returns the number of sources dispatched in this iteration. */
109 int pa_mainloop_iterate (pa_mainloop* m, int block, int* retval);
110 
111 /** Run unlimited iterations of the main loop object until the main loop's
112 quit() routine is called. Returns a negative value on error. Optionally return
113 the return value as specified with the main loop's quit() routine in the integer
114 variable retval points to. */
115 int pa_mainloop_run (pa_mainloop* m, int* retval);
116 
117 /** Return the abstract main loop abstraction layer vtable for this
118     main loop. No need to free the API as it is owned by the loop
119     and is destroyed when the loop is freed. */
120 pa_mainloop_api* pa_mainloop_get_api (pa_mainloop* m);
121 
122 /** Shutdown the main loop with the specified return value */
123 void pa_mainloop_quit (pa_mainloop* m, int retval);
124 
125 /** Interrupt a running poll (for threaded systems) */
126 void pa_mainloop_wakeup (pa_mainloop* m);
127 
128 /** Generic prototype of a poll() like function */
129 alias pa_poll_func = int function (pollfd* ufds, c_ulong nfds, int timeout, void* userdata);
130 
131 /** Change the poll() implementation */
132 void pa_mainloop_set_poll_func (pa_mainloop* m, pa_poll_func poll_func, void* userdata);
133