1 /*
2                                     __
3                                    / _|
4   __ _ _   _ _ __ ___  _ __ __ _  | |_ ___  ___ ___
5  / _` | | | | '__/ _ \| '__/ _` | |  _/ _ \/ __/ __|
6 | (_| | |_| | | | (_) | | | (_| | | || (_) \__ \__ \
7  \__,_|\__,_|_|  \___/|_|  \__,_| |_| \___/|___/___/
8 
9 Copyright (C) 2010 The Android Open Source Project.
10 Copyright (C) 2018-2019 Aurora Free Open Source Software.
11 
12 This file is part of the Aurora Free Open Source Software. This
13 organization promote free and open source software that you can
14 redistribute and/or modify under the terms of the GNU Lesser General
15 Public License Version 3 as published by the Free Software Foundation or
16 (at your option) any later version approved by the Aurora Free Open Source
17 Software Organization. The license is available in the package root path
18 as 'LICENSE' file. Please review the following information to ensure the
19 GNU Lesser General Public License version 3 requirements will be met:
20 https://www.gnu.org/licenses/lgpl.html .
21 
22 Alternatively, this file may be used under the terms of the GNU General
23 Public License version 3 or later as published by the Free Software
24 Foundation. Please review the following information to ensure the GNU
25 General Public License requirements will be met:
26 https://www.gnu.org/licenses/gpl-3.0.html.
27 
28 NOTE: All products, services or anything associated to trademarks and
29 service marks used or referenced on this file are the property of their
30 respective companies/owners or its subsidiaries. Other names and brands
31 may be claimed as the property of others.
32 
33 For more info about intellectual property visit: aurorafoss.org or
34 directly send an email to: contact (at) aurorafoss.org .
35 
36 This file has bindings for an existing code, part of The Android Open Source
37 Project implementation. Check it out at android.googlesource.com .
38 */
39 
40 module aurorafw.android.platform.looper;
41 
42 /**
43  * @addtogroup Looper
44  * @{
45  */
46 
47 /**
48  * @file aurorafw/android/platform/looper.d
49  */
50 
51 version (Android):
52 extern (C):
53 @system:
54 nothrow:
55 @nogc:
56 
57 struct ALooper;
58 /**
59  * ALooper
60  *
61  * A looper is the state tracking an event loop for a thread.
62  * Loopers do not define event structures or other such things; rather
63  * they are a lower-level facility to attach one or more discrete objects
64  * listening for an event.  An "event" here is simply data available on
65  * a file descriptor: each attached object has an associated file descriptor,
66  * and waiting for "events" means (internally) polling on all of these file
67  * descriptors until one or more of them have data available.
68  *
69  * A thread can have only one ALooper associated with it.
70  */
71 
72 /**
73  * Returns the looper associated with the calling thread, or NULL if
74  * there is not one.
75  */
76 ALooper* ALooper_forThread ();
77 
78 /** Option for for ALooper_prepare(). */
79 enum
80 {
81     /**
82      * This looper will accept calls to ALooper_addFd() that do not
83      * have a callback (that is provide NULL for the callback).  In
84      * this case the caller of ALooper_pollOnce() or ALooper_pollAll()
85      * MUST check the return from these functions to discover when
86      * data is available on such fds and process it.
87      */
88     ALOOPER_PREPARE_ALLOW_NON_CALLBACKS = 1
89 }
90 
91 /**
92  * Prepares a looper associated with the calling thread, and returns it.
93  * If the thread already has a looper, it is returned.  Otherwise, a new
94  * one is created, associated with the thread, and returned.
95  *
96  * The opts may be ALOOPER_PREPARE_ALLOW_NON_CALLBACKS or 0.
97  */
98 ALooper* ALooper_prepare (int opts);
99 
100 /** Result from ALooper_pollOnce() and ALooper_pollAll(). */
101 enum
102 {
103     /**
104      * The poll was awoken using wake() before the timeout expired
105      * and no callbacks were executed and no other file descriptors were ready.
106      */
107     ALOOPER_POLL_WAKE = -1,
108 
109     /**
110      * Result from ALooper_pollOnce() and ALooper_pollAll():
111      * One or more callbacks were executed.
112      */
113     ALOOPER_POLL_CALLBACK = -2,
114 
115     /**
116      * Result from ALooper_pollOnce() and ALooper_pollAll():
117      * The timeout expired.
118      */
119     ALOOPER_POLL_TIMEOUT = -3,
120 
121     /**
122      * Result from ALooper_pollOnce() and ALooper_pollAll():
123      * An error occurred.
124      */
125     ALOOPER_POLL_ERROR = -4
126 }
127 
128 /**
129  * Acquire a reference on the given ALooper object.  This prevents the object
130  * from being deleted until the reference is removed.  This is only needed
131  * to safely hand an ALooper from one thread to another.
132  */
133 void ALooper_acquire (ALooper* looper);
134 
135 /**
136  * Remove a reference that was previously acquired with ALooper_acquire().
137  */
138 void ALooper_release (ALooper* looper);
139 
140 /**
141  * Flags for file descriptor events that a looper can monitor.
142  *
143  * These flag bits can be combined to monitor multiple events at once.
144  */
145 enum
146 {
147     /**
148      * The file descriptor is available for read operations.
149      */
150     ALOOPER_EVENT_INPUT = 1,
151 
152     /**
153      * The file descriptor is available for write operations.
154      */
155     ALOOPER_EVENT_OUTPUT = 2,
156 
157     /**
158      * The file descriptor has encountered an error condition.
159      *
160      * The looper always sends notifications about errors; it is not necessary
161      * to specify this event flag in the requested event set.
162      */
163     ALOOPER_EVENT_ERROR = 4,
164 
165     /**
166      * The file descriptor was hung up.
167      * For example, indicates that the remote end of a pipe or socket was closed.
168      *
169      * The looper always sends notifications about hangups; it is not necessary
170      * to specify this event flag in the requested event set.
171      */
172     ALOOPER_EVENT_HANGUP = 8,
173 
174     /**
175      * The file descriptor is invalid.
176      * For example, the file descriptor was closed prematurely.
177      *
178      * The looper always sends notifications about invalid file descriptors; it is not necessary
179      * to specify this event flag in the requested event set.
180      */
181     ALOOPER_EVENT_INVALID = 16
182 }
183 
184 /**
185  * For callback-based event loops, this is the prototype of the function
186  * that is called when a file descriptor event occurs.
187  * It is given the file descriptor it is associated with,
188  * a bitmask of the poll events that were triggered (typically ALOOPER_EVENT_INPUT),
189  * and the data pointer that was originally supplied.
190  *
191  * Implementations should return 1 to continue receiving callbacks, or 0
192  * to have this file descriptor and callback unregistered from the looper.
193  */
194 alias ALooper_callbackFunc = int function (int fd, int events, void* data);
195 
196 /**
197  * Waits for events to be available, with optional timeout in milliseconds.
198  * Invokes callbacks for all file descriptors on which an event occurred.
199  *
200  * If the timeout is zero, returns immediately without blocking.
201  * If the timeout is negative, waits indefinitely until an event appears.
202  *
203  * Returns ALOOPER_POLL_WAKE if the poll was awoken using wake() before
204  * the timeout expired and no callbacks were invoked and no other file
205  * descriptors were ready.
206  *
207  * Returns ALOOPER_POLL_CALLBACK if one or more callbacks were invoked.
208  *
209  * Returns ALOOPER_POLL_TIMEOUT if there was no data before the given
210  * timeout expired.
211  *
212  * Returns ALOOPER_POLL_ERROR if an error occurred.
213  *
214  * Returns a value >= 0 containing an identifier (the same identifier
215  * `ident` passed to ALooper_addFd()) if its file descriptor has data
216  * and it has no callback function (requiring the caller here to
217  * handle it).  In this (and only this) case outFd, outEvents and
218  * outData will contain the poll events and data associated with the
219  * fd, otherwise they will be set to NULL.
220  *
221  * This method does not return until it has finished invoking the appropriate callbacks
222  * for all file descriptors that were signalled.
223  */
224 int ALooper_pollOnce (int timeoutMillis, int* outFd, int* outEvents, void** outData);
225 
226 /**
227  * Like ALooper_pollOnce(), but performs all pending callbacks until all
228  * data has been consumed or a file descriptor is available with no callback.
229  * This function will never return ALOOPER_POLL_CALLBACK.
230  */
231 int ALooper_pollAll (int timeoutMillis, int* outFd, int* outEvents, void** outData);
232 
233 /**
234  * Wakes the poll asynchronously.
235  *
236  * This method can be called on any thread.
237  * This method returns immediately.
238  */
239 void ALooper_wake (ALooper* looper);
240 
241 /**
242  * Adds a new file descriptor to be polled by the looper.
243  * If the same file descriptor was previously added, it is replaced.
244  *
245  * "fd" is the file descriptor to be added.
246  * "ident" is an identifier for this event, which is returned from ALooper_pollOnce().
247  * The identifier must be >= 0, or ALOOPER_POLL_CALLBACK if providing a non-NULL callback.
248  * "events" are the poll events to wake up on.  Typically this is ALOOPER_EVENT_INPUT.
249  * "callback" is the function to call when there is an event on the file descriptor.
250  * "data" is a private data pointer to supply to the callback.
251  *
252  * There are two main uses of this function:
253  *
254  * (1) If "callback" is non-NULL, then this function will be called when there is
255  * data on the file descriptor.  It should execute any events it has pending,
256  * appropriately reading from the file descriptor.  The 'ident' is ignored in this case.
257  *
258  * (2) If "callback" is NULL, the 'ident' will be returned by ALooper_pollOnce
259  * when its file descriptor has data available, requiring the caller to take
260  * care of processing it.
261  *
262  * Returns 1 if the file descriptor was added or -1 if an error occurred.
263  *
264  * This method can be called on any thread.
265  * This method may block briefly if it needs to wake the poll.
266  */
267 int ALooper_addFd (
268     ALooper* looper,
269     int fd,
270     int ident,
271     int events,
272     ALooper_callbackFunc callback,
273     void* data);
274 
275 /**
276  * Removes a previously added file descriptor from the looper.
277  *
278  * When this method returns, it is safe to close the file descriptor since the looper
279  * will no longer have a reference to it.  However, it is possible for the callback to
280  * already be running or for it to run one last time if the file descriptor was already
281  * signalled.  Calling code is responsible for ensuring that this case is safely handled.
282  * For example, if the callback takes care of removing itself during its own execution either
283  * by returning 0 or by calling this method, then it can be guaranteed to not be invoked
284  * again at any later time unless registered anew.
285  *
286  * Returns 1 if the file descriptor was removed, 0 if none was previously registered
287  * or -1 if an error occurred.
288  *
289  * This method can be called on any thread.
290  * This method may block briefly if it needs to wake the poll.
291  */
292 int ALooper_removeFd (ALooper* looper, int fd);
293 
294 // ANDROID_LOOPER_H
295 
296 /** @} */