| 1 |
(* |
| 2 |
* Copyright (C) 2010 The Android Open Source Project |
| 3 |
* |
| 4 |
* Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 |
* you may not use this file except in compliance with the License. |
| 6 |
* You may obtain a copy of the License at |
| 7 |
* |
| 8 |
* http://www.apache.org/licenses/LICENSE-2.0 |
| 9 |
* |
| 10 |
* Unless required by applicable law or agreed to in writing, software |
| 11 |
* distributed under the License is distributed on an "AS IS" BASIS, |
| 12 |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 |
* See the License for the specific language governing permissions and |
| 14 |
* limitations under the License. |
| 15 |
*) |
| 16 |
|
| 17 |
{ @exclude Internal for the engine. } |
| 18 |
unit CastleAndroidInternalLooper; |
| 19 |
|
| 20 |
{$I castleconf.inc} |
| 21 |
|
| 22 |
interface |
| 23 |
|
| 24 |
uses ctypes; |
| 25 |
|
| 26 |
(** |
| 27 |
* ALooper |
| 28 |
* |
| 29 |
* A looper is the state tracking an event loop for a thread. |
| 30 |
* Loopers do not define event structures or other such things; rather |
| 31 |
* they are a lower-level facility to attach one or more discrete objects |
| 32 |
* listening for an event. An "event" here is simply data available on |
| 33 |
* a file descriptor: each attached object has an associated file descriptor, |
| 34 |
* and waiting for "events" means (internally) polling on all of these file |
| 35 |
* descriptors until one or more of them have data available. |
| 36 |
* |
| 37 |
* A thread can have only one ALooper associated with it. |
| 38 |
*) |
| 39 |
|
| 40 |
type |
| 41 |
PALooper = ^ALooper; |
| 42 |
ALooper = record end; |
| 43 |
|
| 44 |
Pint = ^cint; |
| 45 |
|
| 46 |
(** |
| 47 |
* Returns the looper associated with the calling thread, or NULL if |
| 48 |
* there is not one. |
| 49 |
*) |
| 50 |
|
| 51 |
function ALooper_forThread: PALooper; cdecl; external; |
| 52 |
|
| 53 |
const |
| 54 |
(** |
| 55 |
* Option for ALooper_prepare: this looper will accept calls to |
| 56 |
* ALooper_addFd() that do not have a callback (that is provide NULL |
| 57 |
* for the callback). In this case the caller of ALooper_pollOnce() |
| 58 |
* or ALooper_pollAll() MUST check the return from these functions to |
| 59 |
* discover when data is available on such fds and process it. |
| 60 |
*) |
| 61 |
ALOOPER_PREPARE_ALLOW_NON_CALLBACKS = 1 shl 0; |
| 62 |
|
| 63 |
(** |
| 64 |
* Prepares a looper associated with the calling thread, and returns it. |
| 65 |
* If the thread already has a looper, it is returned. Otherwise, a new |
| 66 |
* one is created, associated with the thread, and returned. |
| 67 |
* |
| 68 |
* The opts may be ALOOPER_PREPARE_ALLOW_NON_CALLBACKS or 0. |
| 69 |
*) |
| 70 |
|
| 71 |
function ALooper_prepare(opts: cint): PALooper; cdecl; external; |
| 72 |
|
| 73 |
const |
| 74 |
(** |
| 75 |
* Result from ALooper_pollOnce() and ALooper_pollAll(): |
| 76 |
* The poll was awoken using wake() before the timeout expired |
| 77 |
* and no callbacks were executed and no other file descriptors were ready. |
| 78 |
*) |
| 79 |
ALOOPER_POLL_WAKE = - 1; |
| 80 |
(** |
| 81 |
* Result from ALooper_pollOnce() and ALooper_pollAll(): |
| 82 |
* One or more callbacks were executed. |
| 83 |
*) |
| 84 |
ALOOPER_POLL_CALLBACK = - 2; |
| 85 |
(** |
| 86 |
* Result from ALooper_pollOnce() and ALooper_pollAll(): |
| 87 |
* The timeout expired. |
| 88 |
*) |
| 89 |
ALOOPER_POLL_TIMEOUT = - 3; |
| 90 |
(** |
| 91 |
* Result from ALooper_pollOnce() and ALooper_pollAll(): |
| 92 |
* An error occurred. |
| 93 |
*) |
| 94 |
ALOOPER_POLL_ERROR = - 4; |
| 95 |
|
| 96 |
(** |
| 97 |
* Acquire a reference on the given ALooper object. This prevents the object |
| 98 |
* from being deleted until the reference is removed. This is only needed |
| 99 |
* to safely hand an ALooper from one thread to another. |
| 100 |
*) |
| 101 |
|
| 102 |
procedure ALooper_acquire(looper: PALooper); cdecl; external; |
| 103 |
|
| 104 |
(** |
| 105 |
* Remove a reference that was previously acquired with ALooper_acquire(). |
| 106 |
*) |
| 107 |
|
| 108 |
procedure ALooper_release(looper: PALooper); cdecl; external; |
| 109 |
|
| 110 |
(** |
| 111 |
* Flags for file descriptor events that a looper can monitor. |
| 112 |
* |
| 113 |
* These flag bits can be combined to monitor multiple events at once. |
| 114 |
*) |
| 115 |
|
| 116 |
const |
| 117 |
(** |
| 118 |
* The file descriptor is available for read operations. |
| 119 |
*) |
| 120 |
ALOOPER_EVENT_INPUT = 1 shl 0; |
| 121 |
(** |
| 122 |
* The file descriptor is available for write operations. |
| 123 |
*) |
| 124 |
ALOOPER_EVENT_OUTPUT = 1 shl 1; |
| 125 |
(** |
| 126 |
* The file descriptor has encountered an error condition. |
| 127 |
* |
| 128 |
* The looper always sends notifications about errors; it is not necessary |
| 129 |
* to specify this event flag in the requested event set. |
| 130 |
*) |
| 131 |
ALOOPER_EVENT_ERROR = 1 shl 2; |
| 132 |
(** |
| 133 |
* The file descriptor was hung up. |
| 134 |
* For example, indicates that the remote end of a pipe or socket was closed. |
| 135 |
* |
| 136 |
* The looper always sends notifications about hangups; it is not necessary |
| 137 |
* to specify this event flag in the requested event set. |
| 138 |
*) |
| 139 |
ALOOPER_EVENT_HANGUP = 1 shl 3; |
| 140 |
(** |
| 141 |
* The file descriptor is invalid. |
| 142 |
* For example, the file descriptor was closed prematurely. |
| 143 |
* |
| 144 |
* The looper always sends notifications about invalid file descriptors; it is not necessary |
| 145 |
* to specify this event flag in the requested event set. |
| 146 |
*) |
| 147 |
ALOOPER_EVENT_INVALID = 1 shl 4; |
| 148 |
|
| 149 |
(** |
| 150 |
* For callback-based event loops, this is the prototype of the function |
| 151 |
* that is called. It is given the file descriptor it is associated with, |
| 152 |
* a bitmask of the poll events that were triggered (typically ALOOPER_EVENT_INPUT), |
| 153 |
* and the data pointer that was originally supplied. |
| 154 |
* |
| 155 |
* Implementations should return 1 to continue receiving callbacks, or 0 |
| 156 |
* to have this file descriptor and callback unregistered from the looper. |
| 157 |
*) |
| 158 |
|
| 159 |
type |
| 160 |
ALooper_callbackFunc = function(fd, events: cint; data: Pointer): cint; cdecl; |
| 161 |
|
| 162 |
(** |
| 163 |
* Waits for events to be available, with optional timeout in milliseconds. |
| 164 |
* Invokes callbacks for all file descriptors on which an event occurred. |
| 165 |
* |
| 166 |
* If the timeout is zero, returns immediately without blocking. |
| 167 |
* If the timeout is negative, waits indefinitely until an event appears. |
| 168 |
* |
| 169 |
* Returns ALOOPER_POLL_WAKE if the poll was awoken using wake() before |
| 170 |
* the timeout expired and no callbacks were invoked and no other file |
| 171 |
* descriptors were ready. |
| 172 |
* |
| 173 |
* Returns ALOOPER_POLL_CALLBACK if one or more callbacks were invoked. |
| 174 |
* |
| 175 |
* Returns ALOOPER_POLL_TIMEOUT if there was no data before the given |
| 176 |
* timeout expired. |
| 177 |
* |
| 178 |
* Returns ALOOPER_POLL_ERROR if an error occurred. |
| 179 |
* |
| 180 |
* Returns a value >= 0 containing an identifier if its file descriptor has data |
| 181 |
* and it has no callback function (requiring the caller here to handle it). |
| 182 |
* In this (and only this) case outFd, outEvents and outData will contain the poll |
| 183 |
* events and data associated with the fd, otherwise they will be set to NULL. |
| 184 |
* |
| 185 |
* This method does not return until it has finished invoking the appropriate callbacks |
| 186 |
* for all file descriptors that were signalled. |
| 187 |
*) |
| 188 |
function ALooper_pollOnce(timeoutMillis: cint; outFd, outEvents: Pint; outData: PPointer): cint; cdecl; external; |
| 189 |
|
| 190 |
(** |
| 191 |
* Like ALooper_pollOnce(), but performs all pending callbacks until all |
| 192 |
* data has been consumed or a file descriptor is available with no callback. |
| 193 |
* This function will never return ALOOPER_POLL_CALLBACK. |
| 194 |
*) |
| 195 |
function ALooper_pollAll(timeoutMillis: cint; outFd, outEvents: Pint; outData: PPointer): cint; cdecl; external; |
| 196 |
|
| 197 |
(** |
| 198 |
* Wakes the poll asynchronously. |
| 199 |
* |
| 200 |
* This method can be called on any thread. |
| 201 |
* This method returns immediately. |
| 202 |
*) |
| 203 |
procedure ALooper_wake(looper: PALooper); cdecl; external; |
| 204 |
|
| 205 |
(** |
| 206 |
* Adds a new file descriptor to be polled by the looper. |
| 207 |
* If the same file descriptor was previously added, it is replaced. |
| 208 |
* |
| 209 |
* "fd" is the file descriptor to be added. |
| 210 |
* "ident" is an identifier for this event, which is returned from ALooper_pollOnce(). |
| 211 |
* The identifier must be >= 0, or ALOOPER_POLL_CALLBACK if providing a non-NULL callback. |
| 212 |
* "events" are the poll events to wake up on. Typically this is ALOOPER_EVENT_INPUT. |
| 213 |
* "callback" is the function to call when there is an event on the file descriptor. |
| 214 |
* "data" is a private data pointer to supply to the callback. |
| 215 |
* |
| 216 |
* There are two main uses of this function: |
| 217 |
* |
| 218 |
* (1) If "callback" is non-NULL, then this function will be called when there is |
| 219 |
* data on the file descriptor. It should execute any events it has pending, |
| 220 |
* appropriately reading from the file descriptor. The 'ident' is ignored in this case. |
| 221 |
* |
| 222 |
* (2) If "callback" is NULL, the 'ident' will be returned by ALooper_pollOnce |
| 223 |
* when its file descriptor has data available, requiring the caller to take |
| 224 |
* care of processing it. |
| 225 |
* |
| 226 |
* Returns 1 if the file descriptor was added or -1 if an error occurred. |
| 227 |
* |
| 228 |
* This method can be called on any thread. |
| 229 |
* This method may block briefly if it needs to wake the poll. |
| 230 |
*) |
| 231 |
function ALooper_addFd(looper: PALooper; fd, ident, events: cint; callback: ALooper_callbackFunc; data: Pointer): cint; cdecl; external; |
| 232 |
|
| 233 |
(** |
| 234 |
* Removes a previously added file descriptor from the looper. |
| 235 |
* |
| 236 |
* When this method returns, it is safe to close the file descriptor since the looper |
| 237 |
* will no longer have a reference to it. However, it is possible for the callback to |
| 238 |
* already be running or for it to run one last time if the file descriptor was already |
| 239 |
* signalled. Calling code is responsible for ensuring that this case is safely handled. |
| 240 |
* For example, if the callback takes care of removing itself during its own execution either |
| 241 |
* by returning 0 or by calling this method, then it can be guaranteed to not be invoked |
| 242 |
* again at any later time unless registered anew. |
| 243 |
* |
| 244 |
* Returns 1 if the file descriptor was removed, 0 if none was previously registered |
| 245 |
* or -1 if an error occurred. |
| 246 |
* |
| 247 |
* This method can be called on any thread. |
| 248 |
* This method may block briefly if it needs to wake the poll. |
| 249 |
*) |
| 250 |
|
| 251 |
function ALooper_removeFd(looper: PALooper; fd: cint): cint; cdecl; external; |
| 252 |
|
| 253 |
implementation |
| 254 |
|
| 255 |
end. |
| 256 |
|