| 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 CastleAndroidInternalNativeActivity; |
| 19 |
|
| 20 |
{$I castleconf.inc} |
| 21 |
|
| 22 |
interface |
| 23 |
|
| 24 |
uses CastleAndroidInternalAssetManager, CastleAndroidInternalInput, CastleAndroidInternalNativeWindow, |
| 25 |
CastleAndroidInternalRect, jni, ctypes; |
| 26 |
|
| 27 |
type |
| 28 |
PANativeActivityCallbacks = ^ANativeActivityCallbacks; |
| 29 |
|
| 30 |
{ Native side of an android.app.NativeActivity. |
| 31 |
It is created by the framework, and handed to the application's native |
| 32 |
code as it is being launched. |
| 33 |
|
| 34 |
See https://developer.android.com/ndk/reference/struct/a-native-activity |
| 35 |
} |
| 36 |
ANativeActivity = packed record |
| 37 |
(** |
| 38 |
* Pointer to the callback function table of the native application. |
| 39 |
* You can set the functions here to your own callbacks. The callbacks |
| 40 |
* pointer itself here should not be changed; it is allocated and managed |
| 41 |
* for you by the framework. |
| 42 |
*) |
| 43 |
callbacks : PANativeActivityCallbacks; |
| 44 |
(** |
| 45 |
* The global handle on the process's Java VM. |
| 46 |
*) |
| 47 |
vm : PJavaVM; |
| 48 |
(** |
| 49 |
* JNI context for the main thread of the app. Note that this field |
| 50 |
* can ONLY be used from the main thread of the process; that is, the |
| 51 |
* thread that calls into the ANativeActivityCallbacks. |
| 52 |
*) |
| 53 |
env : PJNIEnv; |
| 54 |
(** |
| 55 |
* The NativeActivity Java class. |
| 56 |
*) |
| 57 |
clazz : jobject; |
| 58 |
(** |
| 59 |
* Path to this application's internal data directory. |
| 60 |
*) |
| 61 |
internalDataPath : Pchar; |
| 62 |
(** |
| 63 |
* Path to this application's external (removable/mountable) data directory. |
| 64 |
*) |
| 65 |
externalDataPath : Pchar; |
| 66 |
(** |
| 67 |
* The platform's SDK version code. |
| 68 |
*) |
| 69 |
sdkVersion : UInt32; |
| 70 |
{$ifdef CPU64} |
| 71 |
{ Padding, to match structure layout on 64-bit CPU (Aarch64). } |
| 72 |
padding : UInt32; |
| 73 |
{$endif} |
| 74 |
(** |
| 75 |
* This is the native instance of the application. It is not used by |
| 76 |
* the framework, but can be set by the application to its own instance |
| 77 |
* state. |
| 78 |
*) |
| 79 |
instance : Pointer; |
| 80 |
(** |
| 81 |
* Pointer to the Asset Manager instance for the application. The application |
| 82 |
* uses this to access binary assets bundled inside its own .apk file. |
| 83 |
*) |
| 84 |
assetManager : PAAssetManager; |
| 85 |
end; |
| 86 |
|
| 87 |
|
| 88 |
{* |
| 89 |
* These are the callbacks the framework makes into a native application. |
| 90 |
* All of these callbacks happen on the main thread of the application. |
| 91 |
* By default, all callbacks are NULL; set to a pointer to your own function |
| 92 |
* to have it called. |
| 93 |
} |
| 94 |
Psize_t = ^csize_t; |
| 95 |
PANativeActivity = ^ANativeActivity; |
| 96 |
ANativeActivityCallbacks = packed record |
| 97 |
(** |
| 98 |
* NativeActivity has started. See Java documentation for Activity.onStart() |
| 99 |
* for more information. |
| 100 |
*) |
| 101 |
onStart : procedure(activity: PANativeActivity); cdecl; |
| 102 |
(** |
| 103 |
* NativeActivity has resumed. See Java documentation for Activity.onResume() |
| 104 |
* for more information. |
| 105 |
*) |
| 106 |
onResume : procedure(activity: PANativeActivity); cdecl; |
| 107 |
(** |
| 108 |
* Framework is asking NativeActivity to save its current instance state. |
| 109 |
* See Java documentation for Activity.onSaveInstanceState() for more |
| 110 |
* information. The returned pointer needs to be created with malloc(); |
| 111 |
* the framework will call free() on it for you. You also must fill in |
| 112 |
* outSize with the number of bytes in the allocation. Note that the |
| 113 |
* saved state will be persisted, so it can not contain any active |
| 114 |
* entities (pointers to memory, file descriptors, etc). |
| 115 |
*) |
| 116 |
onSaveInstanceState : function(activity: PANativeActivity; outSize: Psize_t): Pointer; cdecl; |
| 117 |
(** |
| 118 |
* NativeActivity has paused. See Java documentation for Activity.onPause() |
| 119 |
* for more information. |
| 120 |
*) |
| 121 |
onPause : procedure(activity: PANativeActivity); cdecl; |
| 122 |
(** |
| 123 |
* NativeActivity has stopped. See Java documentation for Activity.onStop() |
| 124 |
* for more information. |
| 125 |
*) |
| 126 |
onStop : procedure(activity: PANativeActivity); cdecl; |
| 127 |
(** |
| 128 |
* NativeActivity is being destroyed. See Java documentation for Activity.onDestroy() |
| 129 |
* for more information. |
| 130 |
*) |
| 131 |
onDestroy : procedure(activity: PANativeActivity); cdecl; |
| 132 |
(** |
| 133 |
* Focus has changed in this NativeActivity's window. This is often used, |
| 134 |
* for example, to pause a game when it loses input focus. |
| 135 |
*) |
| 136 |
onWindowFocusChanged : procedure(activity: PANativeActivity; hasFocus: cint); cdecl; |
| 137 |
(** |
| 138 |
* The drawing window for this native activity has been created. You |
| 139 |
* can use the given native window object to start drawing. |
| 140 |
*) |
| 141 |
onNativeWindowCreated : procedure(activity: PANativeActivity; window: PANativeWindow); cdecl; |
| 142 |
(** |
| 143 |
* The drawing window for this native activity has been resized. You should |
| 144 |
* retrieve the new size from the window and ensure that your rendering in |
| 145 |
* it now matches. |
| 146 |
*) |
| 147 |
onNativeWindowResized : procedure(activity: PANativeActivity; window: PANativeWindow); cdecl; |
| 148 |
(** |
| 149 |
* The drawing window for this native activity needs to be redrawn. To avoid |
| 150 |
* transient artifacts during screen changes (such resizing after rotation), |
| 151 |
* applications should not return from this function until they have finished |
| 152 |
* drawing their window in its current state. |
| 153 |
*) |
| 154 |
onNativeWindowRedrawNeeded : procedure(activity: PANativeActivity; window: PANativeWindow); cdecl; |
| 155 |
(** |
| 156 |
* The drawing window for this native activity is going to be destroyed. |
| 157 |
* You MUST ensure that you do not touch the window object after returning |
| 158 |
* from this function: in the common case of drawing to the window from |
| 159 |
* another thread, that means the implementation of this callback must |
| 160 |
* properly synchronize with the other thread to stop its drawing before |
| 161 |
* returning from here. |
| 162 |
*) |
| 163 |
onNativeWindowDestroyed : procedure(activity: PANativeActivity; window: PANativeWindow); cdecl; |
| 164 |
(** |
| 165 |
* The input queue for this native activity's window has been created. |
| 166 |
* You can use the given input queue to start retrieving input events. |
| 167 |
*) |
| 168 |
onInputQueueCreated : procedure(activity: PANativeActivity; queue: PAInputQueue); cdecl; |
| 169 |
(** |
| 170 |
* The input queue for this native activity's window is being destroyed. |
| 171 |
* You should no longer try to reference this object upon returning from this |
| 172 |
* function. |
| 173 |
*) |
| 174 |
onInputQueueDestroyed : procedure(activity: PANativeActivity; queue: PAInputQueue); cdecl; |
| 175 |
(** |
| 176 |
* The rectangle in the window in which content should be placed has changed. |
| 177 |
*) |
| 178 |
onContentRectChanged : procedure(activity: PANativeActivity; rect: PARect); cdecl; |
| 179 |
(** |
| 180 |
* The current device AConfiguration has changed. The new configuration can |
| 181 |
* be retrieved from assetManager. |
| 182 |
*) |
| 183 |
onConfigurationChanged : procedure(activity: PANativeActivity); cdecl; |
| 184 |
(** |
| 185 |
* The system is running low on memory. Use this callback to release |
| 186 |
* resources you do not need, to help the system avoid killing more |
| 187 |
* important processes. |
| 188 |
*) |
| 189 |
onLowMemory : procedure(activity: PANativeActivity); cdecl; |
| 190 |
end; |
| 191 |
|
| 192 |
|
| 193 |
{* |
| 194 |
* This is the function that must be in the native code to instantiate the |
| 195 |
* application's native activity. It is called with the activity instance (see |
| 196 |
* above); if the code is being instantiated from a previously saved instance, |
| 197 |
* the savedState will be non-NULL and point to the saved data. You must make |
| 198 |
* any copy of this data you need -- it will be released after you return from |
| 199 |
* this function. |
| 200 |
} |
| 201 |
ANativeActivity_createFunc = procedure(activity: PANativeActivity; savedState: pointer; savedStateSize: SizeInt); cdecl; |
| 202 |
|
| 203 |
{* |
| 204 |
* The name of the function that NativeInstance looks for when launching its |
| 205 |
* native code. This is the default function that is used, you can specify |
| 206 |
* "android.app.func_name" string meta-data in your manifest to use a different |
| 207 |
* function. |
| 208 |
} |
| 209 |
{var |
| 210 |
ANativeActivity_onCreate : ANativeActivity_createFunc; external;} |
| 211 |
|
| 212 |
(** |
| 213 |
* Finish the given activity. Its finish() method will be called, causing it |
| 214 |
* to be stopped and destroyed. Note that this method can be called from |
| 215 |
* *any* thread; it will send a message to the main thread of the process |
| 216 |
* where the Java finish call will take place. |
| 217 |
*) |
| 218 |
procedure ANativeActivity_finish(activity: PANativeActivity); cdecl; external; |
| 219 |
|
| 220 |
(** |
| 221 |
* Change the window format of the given activity. Calls getWindow().setFormat() |
| 222 |
* of the given activity. Note that this method can be called from |
| 223 |
* *any* thread; it will send a message to the main thread of the process |
| 224 |
* where the Java finish call will take place. |
| 225 |
*) |
| 226 |
procedure ANativeActivity_setWindowFormat(activity: PANativeActivity; format: cint32); cdecl; external; |
| 227 |
|
| 228 |
(** |
| 229 |
* Change the window flags of the given activity. Calls getWindow().setFlags() |
| 230 |
* of the given activity. Note that this method can be called from |
| 231 |
* *any* thread; it will send a message to the main thread of the process |
| 232 |
* where the Java finish call will take place. See window.h for flag constants. |
| 233 |
*) |
| 234 |
procedure ANativeActivity_setWindowFlags(activity: PANativeActivity; addFlags, removeFlags: cuint32); cdecl; external; |
| 235 |
|
| 236 |
(** |
| 237 |
* Flags for ANativeActivity_showSoftInput; see the Java InputMethodManager |
| 238 |
* API for documentation. |
| 239 |
*) |
| 240 |
const |
| 241 |
ANATIVEACTIVITY_SHOW_SOFT_INPUT_IMPLICIT = $0001; |
| 242 |
ANATIVEACTIVITY_SHOW_SOFT_INPUT_FORCED = $0002; |
| 243 |
|
| 244 |
(** |
| 245 |
* Show the IME while in the given activity. Calls InputMethodManager.showSoftInput() |
| 246 |
* for the given activity. Note that this method can be called from |
| 247 |
* *any* thread; it will send a message to the main thread of the process |
| 248 |
* where the Java finish call will take place. |
| 249 |
*) |
| 250 |
procedure ANativeActivity_showSoftInput(activity: PANativeActivity; flags: cuint32); cdecl; external; |
| 251 |
|
| 252 |
(** |
| 253 |
* Flags for ANativeActivity_hideSoftInput; see the Java InputMethodManager |
| 254 |
* API for documentation. |
| 255 |
*) |
| 256 |
const |
| 257 |
ANATIVEACTIVITY_HIDE_SOFT_INPUT_IMPLICIT_ONLY = $0001; |
| 258 |
ANATIVEACTIVITY_HIDE_SOFT_INPUT_NOT_ALWAYS = $0002; |
| 259 |
|
| 260 |
(** |
| 261 |
* Hide the IME while in the given activity. Calls InputMethodManager.hideSoftInput() |
| 262 |
* for the given activity. Note that this method can be called from |
| 263 |
* *any* thread; it will send a message to the main thread of the process |
| 264 |
* where the Java finish call will take place. |
| 265 |
*) |
| 266 |
procedure ANativeActivity_hideSoftInput(activity: PANativeActivity; flags: cuint32); cdecl; external; |
| 267 |
|
| 268 |
implementation |
| 269 |
|
| 270 |
(* |
| 271 |
CGE fix: |
| 272 |
Without this linklib, running the project fails, with messages in logcat: |
| 273 |
java.lang.RuntimeException: Unable to start activity ComponentInfo{....cgeandroidtest/android.app.NativeActivity}: java.lang.IllegalArgumentException: Unable to load native library: /data/data/....castleengine.cgeandroidtest/lib/libcge_android_lib.so |
| 274 |
*) |
| 275 |
{$linklib android} |
| 276 |
|
| 277 |
end. |
| 278 |
|