Dianne Hackborn | 69969e4 | 2010-05-04 11:40:40 -0700 | [diff] [blame] | 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 | #define LOG_TAG "NativeActivity" |
| 18 | #include <utils/Log.h> |
| 19 | |
Dianne Hackborn | 3c80a4a | 2010-06-29 19:20:40 -0700 | [diff] [blame] | 20 | #include <poll.h> |
| 21 | #include <dlfcn.h> |
Dianne Hackborn | d76b67c | 2010-07-13 17:48:30 -0700 | [diff] [blame] | 22 | #include <fcntl.h> |
Dianne Hackborn | 3c80a4a | 2010-06-29 19:20:40 -0700 | [diff] [blame] | 23 | |
Dianne Hackborn | 69969e4 | 2010-05-04 11:40:40 -0700 | [diff] [blame] | 24 | #include <android_runtime/AndroidRuntime.h> |
Dianne Hackborn | 289b9b6 | 2010-07-09 11:44:11 -0700 | [diff] [blame] | 25 | #include <android_runtime/android_view_Surface.h> |
| 26 | #include <android_runtime/android_app_NativeActivity.h> |
Christopher Tate | 6cce32b | 2010-07-12 18:21:36 -0700 | [diff] [blame] | 27 | #include <android_runtime/android_util_AssetManager.h> |
Dianne Hackborn | 54a181b | 2010-06-30 18:35:14 -0700 | [diff] [blame] | 28 | #include <surfaceflinger/Surface.h> |
| 29 | #include <ui/egl/android_natives.h> |
Dianne Hackborn | a95e4cb | 2010-06-18 18:09:33 -0700 | [diff] [blame] | 30 | #include <ui/InputTransport.h> |
Jeff Brown | 4fe6c3e | 2010-09-13 23:17:30 -0700 | [diff] [blame] | 31 | #include <utils/Looper.h> |
Dianne Hackborn | 69969e4 | 2010-05-04 11:40:40 -0700 | [diff] [blame] | 32 | |
Dianne Hackborn | 3c80a4a | 2010-06-29 19:20:40 -0700 | [diff] [blame] | 33 | #include "JNIHelp.h" |
| 34 | #include "android_os_MessageQueue.h" |
| 35 | #include "android_view_InputChannel.h" |
| 36 | #include "android_view_KeyEvent.h" |
Dianne Hackborn | 69969e4 | 2010-05-04 11:40:40 -0700 | [diff] [blame] | 37 | |
Dianne Hackborn | db28a94 | 2010-10-21 17:22:30 -0700 | [diff] [blame] | 38 | #define LOG_TRACE(...) |
Steve Block | 28d9f02 | 2011-10-12 17:27:03 +0100 | [diff] [blame] | 39 | //#define LOG_TRACE(...) ALOG(LOG_DEBUG, LOG_TAG, __VA_ARGS__) |
Dianne Hackborn | d76b67c | 2010-07-13 17:48:30 -0700 | [diff] [blame] | 40 | |
Dianne Hackborn | 69969e4 | 2010-05-04 11:40:40 -0700 | [diff] [blame] | 41 | namespace android |
| 42 | { |
| 43 | |
Dianne Hackborn | 3c80a4a | 2010-06-29 19:20:40 -0700 | [diff] [blame] | 44 | static struct { |
Dianne Hackborn | 3c80a4a | 2010-06-29 19:20:40 -0700 | [diff] [blame] | 45 | jmethodID dispatchUnhandledKeyEvent; |
Dianne Hackborn | 2c6081c | 2010-07-15 17:44:53 -0700 | [diff] [blame] | 46 | jmethodID preDispatchKeyEvent; |
Dianne Hackborn | db28a94 | 2010-10-21 17:22:30 -0700 | [diff] [blame] | 47 | jmethodID finish; |
Dianne Hackborn | 54a181b | 2010-06-30 18:35:14 -0700 | [diff] [blame] | 48 | jmethodID setWindowFlags; |
| 49 | jmethodID setWindowFormat; |
Dianne Hackborn | d76b67c | 2010-07-13 17:48:30 -0700 | [diff] [blame] | 50 | jmethodID showIme; |
| 51 | jmethodID hideIme; |
Dianne Hackborn | 3c80a4a | 2010-06-29 19:20:40 -0700 | [diff] [blame] | 52 | } gNativeActivityClassInfo; |
| 53 | |
Dianne Hackborn | 54a181b | 2010-06-30 18:35:14 -0700 | [diff] [blame] | 54 | // ------------------------------------------------------------------------ |
| 55 | |
Dianne Hackborn | 289b9b6 | 2010-07-09 11:44:11 -0700 | [diff] [blame] | 56 | struct ActivityWork { |
| 57 | int32_t cmd; |
| 58 | int32_t arg1; |
| 59 | int32_t arg2; |
| 60 | }; |
| 61 | |
| 62 | enum { |
| 63 | CMD_DEF_KEY = 1, |
Dianne Hackborn | db28a94 | 2010-10-21 17:22:30 -0700 | [diff] [blame] | 64 | CMD_FINISH, |
Dianne Hackborn | 289b9b6 | 2010-07-09 11:44:11 -0700 | [diff] [blame] | 65 | CMD_SET_WINDOW_FORMAT, |
| 66 | CMD_SET_WINDOW_FLAGS, |
Dianne Hackborn | d76b67c | 2010-07-13 17:48:30 -0700 | [diff] [blame] | 67 | CMD_SHOW_SOFT_INPUT, |
| 68 | CMD_HIDE_SOFT_INPUT, |
Dianne Hackborn | 289b9b6 | 2010-07-09 11:44:11 -0700 | [diff] [blame] | 69 | }; |
| 70 | |
| 71 | static void write_work(int fd, int32_t cmd, int32_t arg1=0, int32_t arg2=0) { |
| 72 | ActivityWork work; |
| 73 | work.cmd = cmd; |
| 74 | work.arg1 = arg1; |
| 75 | work.arg2 = arg2; |
| 76 | |
Dianne Hackborn | d76b67c | 2010-07-13 17:48:30 -0700 | [diff] [blame] | 77 | LOG_TRACE("write_work: cmd=%d", cmd); |
| 78 | |
Dianne Hackborn | 289b9b6 | 2010-07-09 11:44:11 -0700 | [diff] [blame] | 79 | restart: |
| 80 | int res = write(fd, &work, sizeof(work)); |
| 81 | if (res < 0 && errno == EINTR) { |
| 82 | goto restart; |
| 83 | } |
| 84 | |
| 85 | if (res == sizeof(work)) return; |
| 86 | |
| 87 | if (res < 0) LOGW("Failed writing to work fd: %s", strerror(errno)); |
| 88 | else LOGW("Truncated writing to work fd: %d", res); |
| 89 | } |
| 90 | |
| 91 | static bool read_work(int fd, ActivityWork* outWork) { |
| 92 | int res = read(fd, outWork, sizeof(ActivityWork)); |
| 93 | // no need to worry about EINTR, poll loop will just come back again. |
| 94 | if (res == sizeof(ActivityWork)) return true; |
| 95 | |
| 96 | if (res < 0) LOGW("Failed reading work fd: %s", strerror(errno)); |
| 97 | else LOGW("Truncated reading work fd: %d", res); |
| 98 | return false; |
| 99 | } |
| 100 | |
| 101 | // ------------------------------------------------------------------------ |
| 102 | |
Dianne Hackborn | d76b67c | 2010-07-13 17:48:30 -0700 | [diff] [blame] | 103 | } // namespace android |
| 104 | |
| 105 | using namespace android; |
| 106 | |
| 107 | AInputQueue::AInputQueue(const sp<InputChannel>& channel, int workWrite) : |
Dianne Hackborn | 2c6081c | 2010-07-15 17:44:53 -0700 | [diff] [blame] | 108 | mWorkWrite(workWrite), mConsumer(channel), mSeq(0) { |
Dianne Hackborn | d76b67c | 2010-07-13 17:48:30 -0700 | [diff] [blame] | 109 | int msgpipe[2]; |
| 110 | if (pipe(msgpipe)) { |
| 111 | LOGW("could not create pipe: %s", strerror(errno)); |
| 112 | mDispatchKeyRead = mDispatchKeyWrite = -1; |
| 113 | } else { |
| 114 | mDispatchKeyRead = msgpipe[0]; |
| 115 | mDispatchKeyWrite = msgpipe[1]; |
| 116 | int result = fcntl(mDispatchKeyRead, F_SETFL, O_NONBLOCK); |
| 117 | SLOGW_IF(result != 0, "Could not make AInputQueue read pipe " |
| 118 | "non-blocking: %s", strerror(errno)); |
| 119 | result = fcntl(mDispatchKeyWrite, F_SETFL, O_NONBLOCK); |
| 120 | SLOGW_IF(result != 0, "Could not make AInputQueue write pipe " |
| 121 | "non-blocking: %s", strerror(errno)); |
Dianne Hackborn | 3c80a4a | 2010-06-29 19:20:40 -0700 | [diff] [blame] | 122 | } |
Dianne Hackborn | d76b67c | 2010-07-13 17:48:30 -0700 | [diff] [blame] | 123 | } |
| 124 | |
| 125 | AInputQueue::~AInputQueue() { |
| 126 | close(mDispatchKeyRead); |
| 127 | close(mDispatchKeyWrite); |
| 128 | } |
| 129 | |
Dianne Hackborn | 42c03e5 | 2010-09-07 15:28:30 -0700 | [diff] [blame] | 130 | void AInputQueue::attachLooper(ALooper* looper, int ident, |
Jeff Brown | 4fe6c3e | 2010-09-13 23:17:30 -0700 | [diff] [blame] | 131 | ALooper_callbackFunc callback, void* data) { |
| 132 | mLooper = static_cast<android::Looper*>(looper); |
| 133 | mLooper->addFd(mConsumer.getChannel()->getReceivePipeFd(), |
| 134 | ident, ALOOPER_EVENT_INPUT, callback, data); |
| 135 | mLooper->addFd(mDispatchKeyRead, |
| 136 | ident, ALOOPER_EVENT_INPUT, callback, data); |
Dianne Hackborn | d76b67c | 2010-07-13 17:48:30 -0700 | [diff] [blame] | 137 | } |
| 138 | |
| 139 | void AInputQueue::detachLooper() { |
Jeff Brown | 4fe6c3e | 2010-09-13 23:17:30 -0700 | [diff] [blame] | 140 | mLooper->removeFd(mConsumer.getChannel()->getReceivePipeFd()); |
| 141 | mLooper->removeFd(mDispatchKeyRead); |
Dianne Hackborn | d76b67c | 2010-07-13 17:48:30 -0700 | [diff] [blame] | 142 | } |
| 143 | |
| 144 | int32_t AInputQueue::hasEvents() { |
| 145 | struct pollfd pfd[2]; |
| 146 | |
| 147 | pfd[0].fd = mConsumer.getChannel()->getReceivePipeFd(); |
| 148 | pfd[0].events = POLLIN; |
| 149 | pfd[0].revents = 0; |
| 150 | pfd[1].fd = mDispatchKeyRead; |
Kenny Root | 2bb10f4 | 2011-02-22 15:00:35 -0800 | [diff] [blame] | 151 | pfd[1].events = POLLIN; |
| 152 | pfd[1].revents = 0; |
Dianne Hackborn | 3c80a4a | 2010-06-29 19:20:40 -0700 | [diff] [blame] | 153 | |
Dianne Hackborn | d76b67c | 2010-07-13 17:48:30 -0700 | [diff] [blame] | 154 | int nfd = poll(pfd, 2, 0); |
| 155 | if (nfd <= 0) return 0; |
Kenny Root | 2bb10f4 | 2011-02-22 15:00:35 -0800 | [diff] [blame] | 156 | return ((pfd[0].revents & POLLIN) || (pfd[1].revents & POLLIN)) ? 1 : -1; |
Dianne Hackborn | d76b67c | 2010-07-13 17:48:30 -0700 | [diff] [blame] | 157 | } |
| 158 | |
| 159 | int32_t AInputQueue::getEvent(AInputEvent** outEvent) { |
| 160 | *outEvent = NULL; |
| 161 | |
Dianne Hackborn | 2c6081c | 2010-07-15 17:44:53 -0700 | [diff] [blame] | 162 | bool finishNow = false; |
| 163 | |
Dianne Hackborn | d76b67c | 2010-07-13 17:48:30 -0700 | [diff] [blame] | 164 | char byteread; |
| 165 | ssize_t nRead = read(mDispatchKeyRead, &byteread, 1); |
| 166 | if (nRead == 1) { |
Dianne Hackborn | 3c80a4a | 2010-06-29 19:20:40 -0700 | [diff] [blame] | 167 | mLock.lock(); |
Dianne Hackborn | d76b67c | 2010-07-13 17:48:30 -0700 | [diff] [blame] | 168 | if (mDispatchingKeys.size() > 0) { |
| 169 | KeyEvent* kevent = mDispatchingKeys[0]; |
| 170 | *outEvent = kevent; |
| 171 | mDispatchingKeys.removeAt(0); |
Dianne Hackborn | 2c6081c | 2010-07-15 17:44:53 -0700 | [diff] [blame] | 172 | in_flight_event inflight; |
| 173 | inflight.event = kevent; |
| 174 | inflight.seq = -1; |
| 175 | inflight.doFinish = false; |
| 176 | mInFlightEvents.push(inflight); |
| 177 | } |
| 178 | if (mFinishPreDispatches.size() > 0) { |
| 179 | finish_pre_dispatch finish(mFinishPreDispatches[0]); |
| 180 | mFinishPreDispatches.removeAt(0); |
| 181 | const size_t N = mInFlightEvents.size(); |
| 182 | for (size_t i=0; i<N; i++) { |
| 183 | const in_flight_event& inflight(mInFlightEvents[i]); |
| 184 | if (inflight.seq == finish.seq) { |
| 185 | *outEvent = inflight.event; |
| 186 | finishNow = finish.handled; |
| 187 | } |
| 188 | } |
| 189 | if (*outEvent == NULL) { |
| 190 | LOGW("getEvent couldn't find inflight for seq %d", finish.seq); |
| 191 | } |
Dianne Hackborn | 3c80a4a | 2010-06-29 19:20:40 -0700 | [diff] [blame] | 192 | } |
| 193 | mLock.unlock(); |
Dianne Hackborn | 2c6081c | 2010-07-15 17:44:53 -0700 | [diff] [blame] | 194 | |
| 195 | if (finishNow) { |
Jeff Brown | 3915bb8 | 2010-11-05 15:02:16 -0700 | [diff] [blame] | 196 | finishEvent(*outEvent, true, false); |
Dianne Hackborn | 2c6081c | 2010-07-15 17:44:53 -0700 | [diff] [blame] | 197 | *outEvent = NULL; |
| 198 | return -1; |
| 199 | } else if (*outEvent != NULL) { |
Dianne Hackborn | d76b67c | 2010-07-13 17:48:30 -0700 | [diff] [blame] | 200 | return 0; |
| 201 | } |
Dianne Hackborn | 3c80a4a | 2010-06-29 19:20:40 -0700 | [diff] [blame] | 202 | } |
| 203 | |
Dianne Hackborn | d76b67c | 2010-07-13 17:48:30 -0700 | [diff] [blame] | 204 | int32_t res = mConsumer.receiveDispatchSignal(); |
| 205 | if (res != android::OK) { |
| 206 | LOGE("channel '%s' ~ Failed to receive dispatch signal. status=%d", |
| 207 | mConsumer.getChannel()->getName().string(), res); |
| 208 | return -1; |
| 209 | } |
| 210 | |
| 211 | InputEvent* myEvent = NULL; |
Dianne Hackborn | 2c6081c | 2010-07-15 17:44:53 -0700 | [diff] [blame] | 212 | res = mConsumer.consume(this, &myEvent); |
Dianne Hackborn | d76b67c | 2010-07-13 17:48:30 -0700 | [diff] [blame] | 213 | if (res != android::OK) { |
| 214 | LOGW("channel '%s' ~ Failed to consume input event. status=%d", |
| 215 | mConsumer.getChannel()->getName().string(), res); |
Jeff Brown | 3915bb8 | 2010-11-05 15:02:16 -0700 | [diff] [blame] | 216 | mConsumer.sendFinishedSignal(false); |
Dianne Hackborn | d76b67c | 2010-07-13 17:48:30 -0700 | [diff] [blame] | 217 | return -1; |
| 218 | } |
| 219 | |
Dianne Hackborn | 2c6081c | 2010-07-15 17:44:53 -0700 | [diff] [blame] | 220 | in_flight_event inflight; |
| 221 | inflight.event = myEvent; |
| 222 | inflight.seq = -1; |
| 223 | inflight.doFinish = true; |
| 224 | mInFlightEvents.push(inflight); |
| 225 | |
Dianne Hackborn | d76b67c | 2010-07-13 17:48:30 -0700 | [diff] [blame] | 226 | *outEvent = myEvent; |
| 227 | return 0; |
| 228 | } |
| 229 | |
Dianne Hackborn | 2c6081c | 2010-07-15 17:44:53 -0700 | [diff] [blame] | 230 | bool AInputQueue::preDispatchEvent(AInputEvent* event) { |
| 231 | if (((InputEvent*)event)->getType() != AINPUT_EVENT_TYPE_KEY) { |
| 232 | // The IME only cares about key events. |
| 233 | return false; |
| 234 | } |
| 235 | |
| 236 | // For now we only send system keys to the IME... this avoids having |
| 237 | // critical keys like DPAD go through this path. We really need to have |
| 238 | // the IME report which keys it wants. |
| 239 | if (!((KeyEvent*)event)->isSystemKey()) { |
| 240 | return false; |
| 241 | } |
| 242 | |
| 243 | return preDispatchKey((KeyEvent*)event); |
| 244 | } |
| 245 | |
Jeff Brown | 3915bb8 | 2010-11-05 15:02:16 -0700 | [diff] [blame] | 246 | void AInputQueue::finishEvent(AInputEvent* event, bool handled, bool didDefaultHandling) { |
| 247 | LOG_TRACE("finishEvent: %p handled=%d, didDefaultHandling=%d", event, |
| 248 | handled ? 1 : 0, didDefaultHandling ? 1 : 0); |
Dianne Hackborn | d76b67c | 2010-07-13 17:48:30 -0700 | [diff] [blame] | 249 | |
Jeff Brown | 3915bb8 | 2010-11-05 15:02:16 -0700 | [diff] [blame] | 250 | if (!handled && !didDefaultHandling |
| 251 | && ((InputEvent*)event)->getType() == AINPUT_EVENT_TYPE_KEY |
Dianne Hackborn | d76b67c | 2010-07-13 17:48:30 -0700 | [diff] [blame] | 252 | && ((KeyEvent*)event)->hasDefaultAction()) { |
| 253 | // The app didn't handle this, but it may have a default action |
| 254 | // associated with it. We need to hand this back to Java to be |
| 255 | // executed. |
Dianne Hackborn | 2c6081c | 2010-07-15 17:44:53 -0700 | [diff] [blame] | 256 | doUnhandledKey((KeyEvent*)event); |
| 257 | return; |
Dianne Hackborn | d76b67c | 2010-07-13 17:48:30 -0700 | [diff] [blame] | 258 | } |
| 259 | |
Dianne Hackborn | 2c6081c | 2010-07-15 17:44:53 -0700 | [diff] [blame] | 260 | mLock.lock(); |
| 261 | const size_t N = mInFlightEvents.size(); |
Dianne Hackborn | d76b67c | 2010-07-13 17:48:30 -0700 | [diff] [blame] | 262 | for (size_t i=0; i<N; i++) { |
Dianne Hackborn | 2c6081c | 2010-07-15 17:44:53 -0700 | [diff] [blame] | 263 | const in_flight_event& inflight(mInFlightEvents[i]); |
| 264 | if (inflight.event == event) { |
| 265 | if (inflight.doFinish) { |
Jeff Brown | 3915bb8 | 2010-11-05 15:02:16 -0700 | [diff] [blame] | 266 | int32_t res = mConsumer.sendFinishedSignal(handled); |
Dianne Hackborn | 2c6081c | 2010-07-15 17:44:53 -0700 | [diff] [blame] | 267 | if (res != android::OK) { |
| 268 | LOGW("Failed to send finished signal on channel '%s'. status=%d", |
| 269 | mConsumer.getChannel()->getName().string(), res); |
| 270 | } |
| 271 | } |
| 272 | if (static_cast<InputEvent*>(event)->getType() == AINPUT_EVENT_TYPE_KEY) { |
| 273 | mAvailKeyEvents.push(static_cast<KeyEvent*>(event)); |
| 274 | } else { |
| 275 | mAvailMotionEvents.push(static_cast<MotionEvent*>(event)); |
| 276 | } |
| 277 | mInFlightEvents.removeAt(i); |
| 278 | mLock.unlock(); |
| 279 | return; |
Dianne Hackborn | d76b67c | 2010-07-13 17:48:30 -0700 | [diff] [blame] | 280 | } |
| 281 | } |
Dianne Hackborn | 2c6081c | 2010-07-15 17:44:53 -0700 | [diff] [blame] | 282 | mLock.unlock(); |
Dianne Hackborn | 3c80a4a | 2010-06-29 19:20:40 -0700 | [diff] [blame] | 283 | |
Dianne Hackborn | 2c6081c | 2010-07-15 17:44:53 -0700 | [diff] [blame] | 284 | LOGW("finishEvent called for unknown event: %p", event); |
Dianne Hackborn | d76b67c | 2010-07-13 17:48:30 -0700 | [diff] [blame] | 285 | } |
| 286 | |
| 287 | void AInputQueue::dispatchEvent(android::KeyEvent* event) { |
| 288 | mLock.lock(); |
| 289 | LOG_TRACE("dispatchEvent: dispatching=%d write=%d\n", mDispatchingKeys.size(), |
| 290 | mDispatchKeyWrite); |
| 291 | mDispatchingKeys.add(event); |
Dianne Hackborn | 2c6081c | 2010-07-15 17:44:53 -0700 | [diff] [blame] | 292 | wakeupDispatch(); |
Dianne Hackborn | d76b67c | 2010-07-13 17:48:30 -0700 | [diff] [blame] | 293 | mLock.unlock(); |
Dianne Hackborn | 2c6081c | 2010-07-15 17:44:53 -0700 | [diff] [blame] | 294 | } |
| 295 | |
| 296 | void AInputQueue::finishPreDispatch(int seq, bool handled) { |
| 297 | mLock.lock(); |
| 298 | LOG_TRACE("finishPreDispatch: seq=%d handled=%d\n", seq, handled ? 1 : 0); |
| 299 | finish_pre_dispatch finish; |
| 300 | finish.seq = seq; |
| 301 | finish.handled = handled; |
| 302 | mFinishPreDispatches.add(finish); |
| 303 | wakeupDispatch(); |
| 304 | mLock.unlock(); |
| 305 | } |
| 306 | |
| 307 | KeyEvent* AInputQueue::consumeUnhandledEvent() { |
| 308 | KeyEvent* event = NULL; |
| 309 | |
| 310 | mLock.lock(); |
| 311 | if (mUnhandledKeys.size() > 0) { |
| 312 | event = mUnhandledKeys[0]; |
| 313 | mUnhandledKeys.removeAt(0); |
| 314 | } |
| 315 | mLock.unlock(); |
| 316 | |
| 317 | LOG_TRACE("consumeUnhandledEvent: KeyEvent=%p", event); |
| 318 | |
| 319 | return event; |
| 320 | } |
| 321 | |
| 322 | KeyEvent* AInputQueue::consumePreDispatchingEvent(int* outSeq) { |
| 323 | KeyEvent* event = NULL; |
| 324 | |
| 325 | mLock.lock(); |
| 326 | if (mPreDispatchingKeys.size() > 0) { |
| 327 | const in_flight_event& inflight(mPreDispatchingKeys[0]); |
| 328 | event = static_cast<KeyEvent*>(inflight.event); |
| 329 | *outSeq = inflight.seq; |
| 330 | mPreDispatchingKeys.removeAt(0); |
| 331 | } |
| 332 | mLock.unlock(); |
| 333 | |
| 334 | LOG_TRACE("consumePreDispatchingEvent: KeyEvent=%p", event); |
| 335 | |
| 336 | return event; |
| 337 | } |
| 338 | |
| 339 | KeyEvent* AInputQueue::createKeyEvent() { |
| 340 | mLock.lock(); |
| 341 | KeyEvent* event; |
| 342 | if (mAvailKeyEvents.size() <= 0) { |
| 343 | event = new KeyEvent(); |
| 344 | } else { |
| 345 | event = mAvailKeyEvents.top(); |
| 346 | mAvailKeyEvents.pop(); |
| 347 | } |
| 348 | mLock.unlock(); |
| 349 | return event; |
| 350 | } |
| 351 | |
| 352 | MotionEvent* AInputQueue::createMotionEvent() { |
| 353 | mLock.lock(); |
| 354 | MotionEvent* event; |
| 355 | if (mAvailMotionEvents.size() <= 0) { |
| 356 | event = new MotionEvent(); |
| 357 | } else { |
| 358 | event = mAvailMotionEvents.top(); |
| 359 | mAvailMotionEvents.pop(); |
| 360 | } |
| 361 | mLock.unlock(); |
| 362 | return event; |
| 363 | } |
| 364 | |
| 365 | void AInputQueue::doUnhandledKey(KeyEvent* keyEvent) { |
| 366 | mLock.lock(); |
| 367 | LOG_TRACE("Unhandled key: pending=%d write=%d\n", mUnhandledKeys.size(), mWorkWrite); |
| 368 | if (mUnhandledKeys.size() <= 0 && mWorkWrite >= 0) { |
| 369 | write_work(mWorkWrite, CMD_DEF_KEY); |
| 370 | } |
| 371 | mUnhandledKeys.add(keyEvent); |
| 372 | mLock.unlock(); |
| 373 | } |
| 374 | |
| 375 | bool AInputQueue::preDispatchKey(KeyEvent* keyEvent) { |
| 376 | mLock.lock(); |
| 377 | LOG_TRACE("preDispatch key: pending=%d write=%d\n", mPreDispatchingKeys.size(), mWorkWrite); |
| 378 | const size_t N = mInFlightEvents.size(); |
| 379 | for (size_t i=0; i<N; i++) { |
| 380 | in_flight_event& inflight(mInFlightEvents.editItemAt(i)); |
| 381 | if (inflight.event == keyEvent) { |
| 382 | if (inflight.seq >= 0) { |
| 383 | // This event has already been pre-dispatched! |
| 384 | LOG_TRACE("Event already pre-dispatched!"); |
| 385 | mLock.unlock(); |
| 386 | return false; |
| 387 | } |
| 388 | mSeq++; |
| 389 | if (mSeq < 0) mSeq = 1; |
| 390 | inflight.seq = mSeq; |
| 391 | |
| 392 | if (mPreDispatchingKeys.size() <= 0 && mWorkWrite >= 0) { |
| 393 | write_work(mWorkWrite, CMD_DEF_KEY); |
| 394 | } |
| 395 | mPreDispatchingKeys.add(inflight); |
| 396 | mLock.unlock(); |
| 397 | return true; |
| 398 | } |
| 399 | } |
| 400 | |
| 401 | LOGW("preDispatchKey called for unknown event: %p", keyEvent); |
| 402 | return false; |
| 403 | } |
| 404 | |
| 405 | void AInputQueue::wakeupDispatch() { |
Dianne Hackborn | d76b67c | 2010-07-13 17:48:30 -0700 | [diff] [blame] | 406 | restart: |
| 407 | char dummy = 0; |
| 408 | int res = write(mDispatchKeyWrite, &dummy, sizeof(dummy)); |
| 409 | if (res < 0 && errno == EINTR) { |
| 410 | goto restart; |
| 411 | } |
| 412 | |
| 413 | if (res == sizeof(dummy)) return; |
| 414 | |
| 415 | if (res < 0) LOGW("Failed writing to dispatch fd: %s", strerror(errno)); |
| 416 | else LOGW("Truncated writing to dispatch fd: %d", res); |
| 417 | } |
| 418 | |
Dianne Hackborn | d76b67c | 2010-07-13 17:48:30 -0700 | [diff] [blame] | 419 | namespace android { |
Dianne Hackborn | 3c80a4a | 2010-06-29 19:20:40 -0700 | [diff] [blame] | 420 | |
Dianne Hackborn | 54a181b | 2010-06-30 18:35:14 -0700 | [diff] [blame] | 421 | // ------------------------------------------------------------------------ |
| 422 | |
| 423 | /* |
| 424 | * Native state for interacting with the NativeActivity class. |
| 425 | */ |
Dianne Hackborn | 289b9b6 | 2010-07-09 11:44:11 -0700 | [diff] [blame] | 426 | struct NativeCode : public ANativeActivity { |
Dianne Hackborn | 2e9f93e | 2010-06-28 15:27:30 -0700 | [diff] [blame] | 427 | NativeCode(void* _dlhandle, ANativeActivity_createFunc* _createFunc) { |
Dianne Hackborn | d76b67c | 2010-07-13 17:48:30 -0700 | [diff] [blame] | 428 | memset((ANativeActivity*)this, 0, sizeof(ANativeActivity)); |
| 429 | memset(&callbacks, 0, sizeof(callbacks)); |
Dianne Hackborn | 74323fd | 2010-05-18 17:56:23 -0700 | [diff] [blame] | 430 | dlhandle = _dlhandle; |
| 431 | createActivityFunc = _createFunc; |
Dianne Hackborn | 54a181b | 2010-06-30 18:35:14 -0700 | [diff] [blame] | 432 | nativeWindow = NULL; |
Dianne Hackborn | a95e4cb | 2010-06-18 18:09:33 -0700 | [diff] [blame] | 433 | inputChannel = NULL; |
| 434 | nativeInputQueue = NULL; |
Dianne Hackborn | 3c80a4a | 2010-06-29 19:20:40 -0700 | [diff] [blame] | 435 | mainWorkRead = mainWorkWrite = -1; |
Dianne Hackborn | 74323fd | 2010-05-18 17:56:23 -0700 | [diff] [blame] | 436 | } |
| 437 | |
| 438 | ~NativeCode() { |
Dianne Hackborn | 289b9b6 | 2010-07-09 11:44:11 -0700 | [diff] [blame] | 439 | if (callbacks.onDestroy != NULL) { |
| 440 | callbacks.onDestroy(this); |
| 441 | } |
| 442 | if (env != NULL && clazz != NULL) { |
| 443 | env->DeleteGlobalRef(clazz); |
Dianne Hackborn | 3c80a4a | 2010-06-29 19:20:40 -0700 | [diff] [blame] | 444 | } |
Jeff Brown | 4fe6c3e | 2010-09-13 23:17:30 -0700 | [diff] [blame] | 445 | if (looper != NULL && mainWorkRead >= 0) { |
| 446 | looper->removeFd(mainWorkRead); |
Dianne Hackborn | 3c80a4a | 2010-06-29 19:20:40 -0700 | [diff] [blame] | 447 | } |
| 448 | if (nativeInputQueue != NULL) { |
| 449 | nativeInputQueue->mWorkWrite = -1; |
| 450 | } |
Dianne Hackborn | a95e4cb | 2010-06-18 18:09:33 -0700 | [diff] [blame] | 451 | setSurface(NULL); |
| 452 | setInputChannel(NULL); |
Dianne Hackborn | 3c80a4a | 2010-06-29 19:20:40 -0700 | [diff] [blame] | 453 | if (mainWorkRead >= 0) close(mainWorkRead); |
| 454 | if (mainWorkWrite >= 0) close(mainWorkWrite); |
Dianne Hackborn | 74323fd | 2010-05-18 17:56:23 -0700 | [diff] [blame] | 455 | if (dlhandle != NULL) { |
Dianne Hackborn | 54a181b | 2010-06-30 18:35:14 -0700 | [diff] [blame] | 456 | // for now don't unload... we probably should clean this |
| 457 | // up and only keep one open dlhandle per proc, since there |
| 458 | // is really no benefit to unloading the code. |
| 459 | //dlclose(dlhandle); |
Dianne Hackborn | 74323fd | 2010-05-18 17:56:23 -0700 | [diff] [blame] | 460 | } |
| 461 | } |
| 462 | |
| 463 | void setSurface(jobject _surface) { |
Dianne Hackborn | 74323fd | 2010-05-18 17:56:23 -0700 | [diff] [blame] | 464 | if (_surface != NULL) { |
Dianne Hackborn | 289b9b6 | 2010-07-09 11:44:11 -0700 | [diff] [blame] | 465 | nativeWindow = android_Surface_getNativeWindow(env, _surface); |
Dianne Hackborn | 74323fd | 2010-05-18 17:56:23 -0700 | [diff] [blame] | 466 | } else { |
Dianne Hackborn | 54a181b | 2010-06-30 18:35:14 -0700 | [diff] [blame] | 467 | nativeWindow = NULL; |
Dianne Hackborn | 74323fd | 2010-05-18 17:56:23 -0700 | [diff] [blame] | 468 | } |
| 469 | } |
| 470 | |
Dianne Hackborn | a95e4cb | 2010-06-18 18:09:33 -0700 | [diff] [blame] | 471 | status_t setInputChannel(jobject _channel) { |
| 472 | if (inputChannel != NULL) { |
| 473 | delete nativeInputQueue; |
Dianne Hackborn | 289b9b6 | 2010-07-09 11:44:11 -0700 | [diff] [blame] | 474 | env->DeleteGlobalRef(inputChannel); |
Dianne Hackborn | a95e4cb | 2010-06-18 18:09:33 -0700 | [diff] [blame] | 475 | } |
| 476 | inputChannel = NULL; |
| 477 | nativeInputQueue = NULL; |
| 478 | if (_channel != NULL) { |
Dianne Hackborn | 289b9b6 | 2010-07-09 11:44:11 -0700 | [diff] [blame] | 479 | inputChannel = env->NewGlobalRef(_channel); |
Dianne Hackborn | a95e4cb | 2010-06-18 18:09:33 -0700 | [diff] [blame] | 480 | sp<InputChannel> ic = |
Dianne Hackborn | 289b9b6 | 2010-07-09 11:44:11 -0700 | [diff] [blame] | 481 | android_view_InputChannel_getInputChannel(env, _channel); |
Dianne Hackborn | a95e4cb | 2010-06-18 18:09:33 -0700 | [diff] [blame] | 482 | if (ic != NULL) { |
Dianne Hackborn | d76b67c | 2010-07-13 17:48:30 -0700 | [diff] [blame] | 483 | nativeInputQueue = new AInputQueue(ic, mainWorkWrite); |
Dianne Hackborn | a95e4cb | 2010-06-18 18:09:33 -0700 | [diff] [blame] | 484 | if (nativeInputQueue->getConsumer().initialize() != android::OK) { |
| 485 | delete nativeInputQueue; |
| 486 | nativeInputQueue = NULL; |
| 487 | return UNKNOWN_ERROR; |
| 488 | } |
| 489 | } else { |
| 490 | return UNKNOWN_ERROR; |
| 491 | } |
| 492 | } |
| 493 | return OK; |
| 494 | } |
| 495 | |
Dianne Hackborn | 2e9f93e | 2010-06-28 15:27:30 -0700 | [diff] [blame] | 496 | ANativeActivityCallbacks callbacks; |
Dianne Hackborn | 69969e4 | 2010-05-04 11:40:40 -0700 | [diff] [blame] | 497 | |
| 498 | void* dlhandle; |
Dianne Hackborn | 2e9f93e | 2010-06-28 15:27:30 -0700 | [diff] [blame] | 499 | ANativeActivity_createFunc* createActivityFunc; |
Dianne Hackborn | 74323fd | 2010-05-18 17:56:23 -0700 | [diff] [blame] | 500 | |
Dianne Hackborn | 805fd7e | 2011-01-16 18:30:29 -0800 | [diff] [blame] | 501 | String8 internalDataPathObj; |
| 502 | String8 externalDataPathObj; |
| 503 | String8 obbPathObj; |
Dianne Hackborn | 6826741 | 2010-07-02 18:52:01 -0700 | [diff] [blame] | 504 | |
Dianne Hackborn | 54a181b | 2010-06-30 18:35:14 -0700 | [diff] [blame] | 505 | sp<ANativeWindow> nativeWindow; |
Dianne Hackborn | d76b67c | 2010-07-13 17:48:30 -0700 | [diff] [blame] | 506 | int32_t lastWindowWidth; |
| 507 | int32_t lastWindowHeight; |
| 508 | |
Dianne Hackborn | a95e4cb | 2010-06-18 18:09:33 -0700 | [diff] [blame] | 509 | jobject inputChannel; |
Dianne Hackborn | d76b67c | 2010-07-13 17:48:30 -0700 | [diff] [blame] | 510 | struct AInputQueue* nativeInputQueue; |
Dianne Hackborn | 3c80a4a | 2010-06-29 19:20:40 -0700 | [diff] [blame] | 511 | |
| 512 | // These are used to wake up the main thread to process work. |
| 513 | int mainWorkRead; |
| 514 | int mainWorkWrite; |
Jeff Brown | 4fe6c3e | 2010-09-13 23:17:30 -0700 | [diff] [blame] | 515 | sp<Looper> looper; |
Dianne Hackborn | 69969e4 | 2010-05-04 11:40:40 -0700 | [diff] [blame] | 516 | }; |
| 517 | |
Dianne Hackborn | db28a94 | 2010-10-21 17:22:30 -0700 | [diff] [blame] | 518 | void android_NativeActivity_finish(ANativeActivity* activity) { |
| 519 | NativeCode* code = static_cast<NativeCode*>(activity); |
| 520 | write_work(code->mainWorkWrite, CMD_FINISH, 0); |
| 521 | } |
| 522 | |
Dianne Hackborn | 289b9b6 | 2010-07-09 11:44:11 -0700 | [diff] [blame] | 523 | void android_NativeActivity_setWindowFormat( |
| 524 | ANativeActivity* activity, int32_t format) { |
| 525 | NativeCode* code = static_cast<NativeCode*>(activity); |
| 526 | write_work(code->mainWorkWrite, CMD_SET_WINDOW_FORMAT, format); |
| 527 | } |
| 528 | |
| 529 | void android_NativeActivity_setWindowFlags( |
| 530 | ANativeActivity* activity, int32_t values, int32_t mask) { |
| 531 | NativeCode* code = static_cast<NativeCode*>(activity); |
| 532 | write_work(code->mainWorkWrite, CMD_SET_WINDOW_FLAGS, values, mask); |
| 533 | } |
| 534 | |
Dianne Hackborn | d76b67c | 2010-07-13 17:48:30 -0700 | [diff] [blame] | 535 | void android_NativeActivity_showSoftInput( |
| 536 | ANativeActivity* activity, int32_t flags) { |
| 537 | NativeCode* code = static_cast<NativeCode*>(activity); |
| 538 | write_work(code->mainWorkWrite, CMD_SHOW_SOFT_INPUT, flags); |
| 539 | } |
| 540 | |
| 541 | void android_NativeActivity_hideSoftInput( |
| 542 | ANativeActivity* activity, int32_t flags) { |
| 543 | NativeCode* code = static_cast<NativeCode*>(activity); |
| 544 | write_work(code->mainWorkWrite, CMD_HIDE_SOFT_INPUT, flags); |
| 545 | } |
| 546 | |
Dianne Hackborn | 54a181b | 2010-06-30 18:35:14 -0700 | [diff] [blame] | 547 | // ------------------------------------------------------------------------ |
| 548 | |
Dianne Hackborn | db28a94 | 2010-10-21 17:22:30 -0700 | [diff] [blame] | 549 | static bool checkAndClearExceptionFromCallback(JNIEnv* env, const char* methodName) { |
| 550 | if (env->ExceptionCheck()) { |
| 551 | LOGE("An exception was thrown by callback '%s'.", methodName); |
| 552 | LOGE_EX(env); |
| 553 | env->ExceptionClear(); |
| 554 | return true; |
| 555 | } |
| 556 | return false; |
| 557 | } |
| 558 | |
Dianne Hackborn | 54a181b | 2010-06-30 18:35:14 -0700 | [diff] [blame] | 559 | /* |
| 560 | * Callback for handling native events on the application's main thread. |
| 561 | */ |
Jeff Brown | 4fe6c3e | 2010-09-13 23:17:30 -0700 | [diff] [blame] | 562 | static int mainWorkCallback(int fd, int events, void* data) { |
Dianne Hackborn | 3c80a4a | 2010-06-29 19:20:40 -0700 | [diff] [blame] | 563 | NativeCode* code = (NativeCode*)data; |
Dianne Hackborn | 289b9b6 | 2010-07-09 11:44:11 -0700 | [diff] [blame] | 564 | if ((events & POLLIN) == 0) { |
Jeff Brown | 4fe6c3e | 2010-09-13 23:17:30 -0700 | [diff] [blame] | 565 | return 1; |
Dianne Hackborn | 289b9b6 | 2010-07-09 11:44:11 -0700 | [diff] [blame] | 566 | } |
| 567 | |
| 568 | ActivityWork work; |
| 569 | if (!read_work(code->mainWorkRead, &work)) { |
Jeff Brown | 4fe6c3e | 2010-09-13 23:17:30 -0700 | [diff] [blame] | 570 | return 1; |
Dianne Hackborn | 289b9b6 | 2010-07-09 11:44:11 -0700 | [diff] [blame] | 571 | } |
Dianne Hackborn | d76b67c | 2010-07-13 17:48:30 -0700 | [diff] [blame] | 572 | |
| 573 | LOG_TRACE("mainWorkCallback: cmd=%d", work.cmd); |
| 574 | |
Dianne Hackborn | 289b9b6 | 2010-07-09 11:44:11 -0700 | [diff] [blame] | 575 | switch (work.cmd) { |
| 576 | case CMD_DEF_KEY: { |
| 577 | KeyEvent* keyEvent; |
Dianne Hackborn | d76b67c | 2010-07-13 17:48:30 -0700 | [diff] [blame] | 578 | while ((keyEvent=code->nativeInputQueue->consumeUnhandledEvent()) != NULL) { |
Dianne Hackborn | 289b9b6 | 2010-07-09 11:44:11 -0700 | [diff] [blame] | 579 | jobject inputEventObj = android_view_KeyEvent_fromNative( |
| 580 | code->env, keyEvent); |
Jeff Brown | 1f24510 | 2010-11-18 20:53:46 -0800 | [diff] [blame] | 581 | jboolean handled; |
| 582 | if (inputEventObj) { |
| 583 | handled = code->env->CallBooleanMethod(code->clazz, |
| 584 | gNativeActivityClassInfo.dispatchUnhandledKeyEvent, inputEventObj); |
| 585 | checkAndClearExceptionFromCallback(code->env, "dispatchUnhandledKeyEvent"); |
| 586 | code->env->DeleteLocalRef(inputEventObj); |
| 587 | } else { |
| 588 | LOGE("Failed to obtain key event for dispatchUnhandledKeyEvent."); |
| 589 | handled = false; |
| 590 | } |
Jeff Brown | 3915bb8 | 2010-11-05 15:02:16 -0700 | [diff] [blame] | 591 | code->nativeInputQueue->finishEvent(keyEvent, handled, true); |
Dianne Hackborn | 2c6081c | 2010-07-15 17:44:53 -0700 | [diff] [blame] | 592 | } |
| 593 | int seq; |
| 594 | while ((keyEvent=code->nativeInputQueue->consumePreDispatchingEvent(&seq)) != NULL) { |
| 595 | jobject inputEventObj = android_view_KeyEvent_fromNative( |
| 596 | code->env, keyEvent); |
Jeff Brown | 1f24510 | 2010-11-18 20:53:46 -0800 | [diff] [blame] | 597 | if (inputEventObj) { |
| 598 | code->env->CallVoidMethod(code->clazz, |
| 599 | gNativeActivityClassInfo.preDispatchKeyEvent, inputEventObj, seq); |
| 600 | checkAndClearExceptionFromCallback(code->env, "preDispatchKeyEvent"); |
| 601 | code->env->DeleteLocalRef(inputEventObj); |
| 602 | } else { |
| 603 | LOGE("Failed to obtain key event for preDispatchKeyEvent."); |
| 604 | } |
Dianne Hackborn | 3c80a4a | 2010-06-29 19:20:40 -0700 | [diff] [blame] | 605 | } |
Dianne Hackborn | 289b9b6 | 2010-07-09 11:44:11 -0700 | [diff] [blame] | 606 | } break; |
Dianne Hackborn | db28a94 | 2010-10-21 17:22:30 -0700 | [diff] [blame] | 607 | case CMD_FINISH: { |
| 608 | code->env->CallVoidMethod(code->clazz, gNativeActivityClassInfo.finish); |
| 609 | checkAndClearExceptionFromCallback(code->env, "finish"); |
| 610 | } break; |
Dianne Hackborn | 289b9b6 | 2010-07-09 11:44:11 -0700 | [diff] [blame] | 611 | case CMD_SET_WINDOW_FORMAT: { |
| 612 | code->env->CallVoidMethod(code->clazz, |
| 613 | gNativeActivityClassInfo.setWindowFormat, work.arg1); |
Dianne Hackborn | db28a94 | 2010-10-21 17:22:30 -0700 | [diff] [blame] | 614 | checkAndClearExceptionFromCallback(code->env, "setWindowFormat"); |
Dianne Hackborn | 289b9b6 | 2010-07-09 11:44:11 -0700 | [diff] [blame] | 615 | } break; |
| 616 | case CMD_SET_WINDOW_FLAGS: { |
| 617 | code->env->CallVoidMethod(code->clazz, |
| 618 | gNativeActivityClassInfo.setWindowFlags, work.arg1, work.arg2); |
Dianne Hackborn | db28a94 | 2010-10-21 17:22:30 -0700 | [diff] [blame] | 619 | checkAndClearExceptionFromCallback(code->env, "setWindowFlags"); |
Dianne Hackborn | 289b9b6 | 2010-07-09 11:44:11 -0700 | [diff] [blame] | 620 | } break; |
Dianne Hackborn | d76b67c | 2010-07-13 17:48:30 -0700 | [diff] [blame] | 621 | case CMD_SHOW_SOFT_INPUT: { |
| 622 | code->env->CallVoidMethod(code->clazz, |
| 623 | gNativeActivityClassInfo.showIme, work.arg1); |
Dianne Hackborn | db28a94 | 2010-10-21 17:22:30 -0700 | [diff] [blame] | 624 | checkAndClearExceptionFromCallback(code->env, "showIme"); |
Dianne Hackborn | d76b67c | 2010-07-13 17:48:30 -0700 | [diff] [blame] | 625 | } break; |
| 626 | case CMD_HIDE_SOFT_INPUT: { |
| 627 | code->env->CallVoidMethod(code->clazz, |
| 628 | gNativeActivityClassInfo.hideIme, work.arg1); |
Dianne Hackborn | db28a94 | 2010-10-21 17:22:30 -0700 | [diff] [blame] | 629 | checkAndClearExceptionFromCallback(code->env, "hideIme"); |
Dianne Hackborn | d76b67c | 2010-07-13 17:48:30 -0700 | [diff] [blame] | 630 | } break; |
Dianne Hackborn | 289b9b6 | 2010-07-09 11:44:11 -0700 | [diff] [blame] | 631 | default: |
| 632 | LOGW("Unknown work command: %d", work.cmd); |
| 633 | break; |
Dianne Hackborn | 3c80a4a | 2010-06-29 19:20:40 -0700 | [diff] [blame] | 634 | } |
| 635 | |
Jeff Brown | 4fe6c3e | 2010-09-13 23:17:30 -0700 | [diff] [blame] | 636 | return 1; |
Dianne Hackborn | 3c80a4a | 2010-06-29 19:20:40 -0700 | [diff] [blame] | 637 | } |
| 638 | |
Dianne Hackborn | 54a181b | 2010-06-30 18:35:14 -0700 | [diff] [blame] | 639 | // ------------------------------------------------------------------------ |
| 640 | |
Dianne Hackborn | 69969e4 | 2010-05-04 11:40:40 -0700 | [diff] [blame] | 641 | static jint |
Dianne Hackborn | e21d91c6 | 2010-10-24 14:56:38 -0700 | [diff] [blame] | 642 | loadNativeCode_native(JNIEnv* env, jobject clazz, jstring path, jstring funcName, |
Dianne Hackborn | 805fd7e | 2011-01-16 18:30:29 -0800 | [diff] [blame] | 643 | jobject messageQueue, jstring internalDataDir, jstring obbDir, |
| 644 | jstring externalDataDir, int sdkVersion, |
Dianne Hackborn | 08d5b8f | 2010-08-04 11:12:40 -0700 | [diff] [blame] | 645 | jobject jAssetMgr, jbyteArray savedState) |
Dianne Hackborn | 69969e4 | 2010-05-04 11:40:40 -0700 | [diff] [blame] | 646 | { |
Dianne Hackborn | d76b67c | 2010-07-13 17:48:30 -0700 | [diff] [blame] | 647 | LOG_TRACE("loadNativeCode_native"); |
| 648 | |
Dianne Hackborn | 69969e4 | 2010-05-04 11:40:40 -0700 | [diff] [blame] | 649 | const char* pathStr = env->GetStringUTFChars(path, NULL); |
| 650 | NativeCode* code = NULL; |
| 651 | |
| 652 | void* handle = dlopen(pathStr, RTLD_LAZY); |
| 653 | |
| 654 | env->ReleaseStringUTFChars(path, pathStr); |
| 655 | |
| 656 | if (handle != NULL) { |
Dianne Hackborn | e21d91c6 | 2010-10-24 14:56:38 -0700 | [diff] [blame] | 657 | const char* funcStr = env->GetStringUTFChars(funcName, NULL); |
Dianne Hackborn | 2e9f93e | 2010-06-28 15:27:30 -0700 | [diff] [blame] | 658 | code = new NativeCode(handle, (ANativeActivity_createFunc*) |
Dianne Hackborn | e21d91c6 | 2010-10-24 14:56:38 -0700 | [diff] [blame] | 659 | dlsym(handle, funcStr)); |
| 660 | env->ReleaseStringUTFChars(funcName, funcStr); |
| 661 | |
Dianne Hackborn | 69969e4 | 2010-05-04 11:40:40 -0700 | [diff] [blame] | 662 | if (code->createActivityFunc == NULL) { |
Dianne Hackborn | 2e9f93e | 2010-06-28 15:27:30 -0700 | [diff] [blame] | 663 | LOGW("ANativeActivity_onCreate not found"); |
Dianne Hackborn | 69969e4 | 2010-05-04 11:40:40 -0700 | [diff] [blame] | 664 | delete code; |
Dianne Hackborn | 69969e4 | 2010-05-04 11:40:40 -0700 | [diff] [blame] | 665 | return 0; |
| 666 | } |
Dianne Hackborn | 3c80a4a | 2010-06-29 19:20:40 -0700 | [diff] [blame] | 667 | |
Jeff Brown | 4fe6c3e | 2010-09-13 23:17:30 -0700 | [diff] [blame] | 668 | code->looper = android_os_MessageQueue_getLooper(env, messageQueue); |
| 669 | if (code->looper == NULL) { |
| 670 | LOGW("Unable to retrieve MessageQueue's Looper"); |
Dianne Hackborn | 3c80a4a | 2010-06-29 19:20:40 -0700 | [diff] [blame] | 671 | delete code; |
| 672 | return 0; |
| 673 | } |
| 674 | |
| 675 | int msgpipe[2]; |
| 676 | if (pipe(msgpipe)) { |
| 677 | LOGW("could not create pipe: %s", strerror(errno)); |
| 678 | delete code; |
| 679 | return 0; |
| 680 | } |
| 681 | code->mainWorkRead = msgpipe[0]; |
| 682 | code->mainWorkWrite = msgpipe[1]; |
Dianne Hackborn | d76b67c | 2010-07-13 17:48:30 -0700 | [diff] [blame] | 683 | int result = fcntl(code->mainWorkRead, F_SETFL, O_NONBLOCK); |
| 684 | SLOGW_IF(result != 0, "Could not make main work read pipe " |
| 685 | "non-blocking: %s", strerror(errno)); |
| 686 | result = fcntl(code->mainWorkWrite, F_SETFL, O_NONBLOCK); |
| 687 | SLOGW_IF(result != 0, "Could not make main work write pipe " |
| 688 | "non-blocking: %s", strerror(errno)); |
Jeff Brown | 4fe6c3e | 2010-09-13 23:17:30 -0700 | [diff] [blame] | 689 | code->looper->addFd(code->mainWorkRead, 0, ALOOPER_EVENT_INPUT, mainWorkCallback, code); |
Dianne Hackborn | 3c80a4a | 2010-06-29 19:20:40 -0700 | [diff] [blame] | 690 | |
Dianne Hackborn | 289b9b6 | 2010-07-09 11:44:11 -0700 | [diff] [blame] | 691 | code->ANativeActivity::callbacks = &code->callbacks; |
| 692 | if (env->GetJavaVM(&code->vm) < 0) { |
Dianne Hackborn | 2e9f93e | 2010-06-28 15:27:30 -0700 | [diff] [blame] | 693 | LOGW("NativeActivity GetJavaVM failed"); |
| 694 | delete code; |
| 695 | return 0; |
| 696 | } |
Dianne Hackborn | 289b9b6 | 2010-07-09 11:44:11 -0700 | [diff] [blame] | 697 | code->env = env; |
| 698 | code->clazz = env->NewGlobalRef(clazz); |
Christopher Tate | 6cce32b | 2010-07-12 18:21:36 -0700 | [diff] [blame] | 699 | |
Dianne Hackborn | 6826741 | 2010-07-02 18:52:01 -0700 | [diff] [blame] | 700 | const char* dirStr = env->GetStringUTFChars(internalDataDir, NULL); |
Dianne Hackborn | 805fd7e | 2011-01-16 18:30:29 -0800 | [diff] [blame] | 701 | code->internalDataPathObj = dirStr; |
| 702 | code->internalDataPath = code->internalDataPathObj.string(); |
| 703 | env->ReleaseStringUTFChars(internalDataDir, dirStr); |
Dianne Hackborn | 6826741 | 2010-07-02 18:52:01 -0700 | [diff] [blame] | 704 | |
| 705 | dirStr = env->GetStringUTFChars(externalDataDir, NULL); |
Dianne Hackborn | 805fd7e | 2011-01-16 18:30:29 -0800 | [diff] [blame] | 706 | code->externalDataPathObj = dirStr; |
| 707 | code->externalDataPath = code->externalDataPathObj.string(); |
| 708 | env->ReleaseStringUTFChars(externalDataDir, dirStr); |
Christopher Tate | 6cce32b | 2010-07-12 18:21:36 -0700 | [diff] [blame] | 709 | |
Dianne Hackborn | 289b9b6 | 2010-07-09 11:44:11 -0700 | [diff] [blame] | 710 | code->sdkVersion = sdkVersion; |
Dianne Hackborn | 6826741 | 2010-07-02 18:52:01 -0700 | [diff] [blame] | 711 | |
Christopher Tate | 6cce32b | 2010-07-12 18:21:36 -0700 | [diff] [blame] | 712 | code->assetManager = assetManagerForJavaObject(env, jAssetMgr); |
| 713 | |
Dianne Hackborn | 805fd7e | 2011-01-16 18:30:29 -0800 | [diff] [blame] | 714 | dirStr = env->GetStringUTFChars(obbDir, NULL); |
| 715 | code->obbPathObj = dirStr; |
| 716 | code->obbPath = code->obbPathObj.string(); |
| 717 | env->ReleaseStringUTFChars(obbDir, dirStr); |
| 718 | |
Dianne Hackborn | 08d5b8f | 2010-08-04 11:12:40 -0700 | [diff] [blame] | 719 | jbyte* rawSavedState = NULL; |
| 720 | jsize rawSavedSize = 0; |
| 721 | if (savedState != NULL) { |
| 722 | rawSavedState = env->GetByteArrayElements(savedState, NULL); |
| 723 | rawSavedSize = env->GetArrayLength(savedState); |
| 724 | } |
| 725 | |
| 726 | code->createActivityFunc(code, rawSavedState, rawSavedSize); |
| 727 | |
| 728 | if (rawSavedState != NULL) { |
| 729 | env->ReleaseByteArrayElements(savedState, rawSavedState, 0); |
| 730 | } |
Dianne Hackborn | 69969e4 | 2010-05-04 11:40:40 -0700 | [diff] [blame] | 731 | } |
| 732 | |
| 733 | return (jint)code; |
| 734 | } |
| 735 | |
| 736 | static void |
| 737 | unloadNativeCode_native(JNIEnv* env, jobject clazz, jint handle) |
| 738 | { |
Dianne Hackborn | d76b67c | 2010-07-13 17:48:30 -0700 | [diff] [blame] | 739 | LOG_TRACE("unloadNativeCode_native"); |
Dianne Hackborn | 69969e4 | 2010-05-04 11:40:40 -0700 | [diff] [blame] | 740 | if (handle != 0) { |
| 741 | NativeCode* code = (NativeCode*)handle; |
Dianne Hackborn | 69969e4 | 2010-05-04 11:40:40 -0700 | [diff] [blame] | 742 | delete code; |
| 743 | } |
| 744 | } |
| 745 | |
| 746 | static void |
| 747 | onStart_native(JNIEnv* env, jobject clazz, jint handle) |
| 748 | { |
Dianne Hackborn | d76b67c | 2010-07-13 17:48:30 -0700 | [diff] [blame] | 749 | LOG_TRACE("onStart_native"); |
Dianne Hackborn | 69969e4 | 2010-05-04 11:40:40 -0700 | [diff] [blame] | 750 | if (handle != 0) { |
| 751 | NativeCode* code = (NativeCode*)handle; |
| 752 | if (code->callbacks.onStart != NULL) { |
Dianne Hackborn | 289b9b6 | 2010-07-09 11:44:11 -0700 | [diff] [blame] | 753 | code->callbacks.onStart(code); |
Dianne Hackborn | 69969e4 | 2010-05-04 11:40:40 -0700 | [diff] [blame] | 754 | } |
| 755 | } |
| 756 | } |
| 757 | |
| 758 | static void |
| 759 | onResume_native(JNIEnv* env, jobject clazz, jint handle) |
| 760 | { |
Dianne Hackborn | d76b67c | 2010-07-13 17:48:30 -0700 | [diff] [blame] | 761 | LOG_TRACE("onResume_native"); |
Dianne Hackborn | 69969e4 | 2010-05-04 11:40:40 -0700 | [diff] [blame] | 762 | if (handle != 0) { |
| 763 | NativeCode* code = (NativeCode*)handle; |
| 764 | if (code->callbacks.onResume != NULL) { |
Dianne Hackborn | 289b9b6 | 2010-07-09 11:44:11 -0700 | [diff] [blame] | 765 | code->callbacks.onResume(code); |
Dianne Hackborn | 69969e4 | 2010-05-04 11:40:40 -0700 | [diff] [blame] | 766 | } |
| 767 | } |
| 768 | } |
| 769 | |
Dianne Hackborn | 08d5b8f | 2010-08-04 11:12:40 -0700 | [diff] [blame] | 770 | static jbyteArray |
Dianne Hackborn | 69969e4 | 2010-05-04 11:40:40 -0700 | [diff] [blame] | 771 | onSaveInstanceState_native(JNIEnv* env, jobject clazz, jint handle) |
| 772 | { |
Dianne Hackborn | d76b67c | 2010-07-13 17:48:30 -0700 | [diff] [blame] | 773 | LOG_TRACE("onSaveInstanceState_native"); |
Dianne Hackborn | 08d5b8f | 2010-08-04 11:12:40 -0700 | [diff] [blame] | 774 | |
| 775 | jbyteArray array = NULL; |
| 776 | |
Dianne Hackborn | 69969e4 | 2010-05-04 11:40:40 -0700 | [diff] [blame] | 777 | if (handle != 0) { |
| 778 | NativeCode* code = (NativeCode*)handle; |
| 779 | if (code->callbacks.onSaveInstanceState != NULL) { |
| 780 | size_t len = 0; |
Dianne Hackborn | 08d5b8f | 2010-08-04 11:12:40 -0700 | [diff] [blame] | 781 | jbyte* state = (jbyte*)code->callbacks.onSaveInstanceState(code, &len); |
| 782 | if (len > 0) { |
| 783 | array = env->NewByteArray(len); |
| 784 | if (array != NULL) { |
| 785 | env->SetByteArrayRegion(array, 0, len, state); |
| 786 | } |
| 787 | } |
| 788 | if (state != NULL) { |
| 789 | free(state); |
| 790 | } |
Dianne Hackborn | 69969e4 | 2010-05-04 11:40:40 -0700 | [diff] [blame] | 791 | } |
| 792 | } |
Dianne Hackborn | 08d5b8f | 2010-08-04 11:12:40 -0700 | [diff] [blame] | 793 | |
| 794 | return array; |
Dianne Hackborn | 69969e4 | 2010-05-04 11:40:40 -0700 | [diff] [blame] | 795 | } |
| 796 | |
| 797 | static void |
| 798 | onPause_native(JNIEnv* env, jobject clazz, jint handle) |
| 799 | { |
Dianne Hackborn | d76b67c | 2010-07-13 17:48:30 -0700 | [diff] [blame] | 800 | LOG_TRACE("onPause_native"); |
Dianne Hackborn | 69969e4 | 2010-05-04 11:40:40 -0700 | [diff] [blame] | 801 | if (handle != 0) { |
| 802 | NativeCode* code = (NativeCode*)handle; |
| 803 | if (code->callbacks.onPause != NULL) { |
Dianne Hackborn | 289b9b6 | 2010-07-09 11:44:11 -0700 | [diff] [blame] | 804 | code->callbacks.onPause(code); |
Dianne Hackborn | 69969e4 | 2010-05-04 11:40:40 -0700 | [diff] [blame] | 805 | } |
| 806 | } |
| 807 | } |
| 808 | |
| 809 | static void |
| 810 | onStop_native(JNIEnv* env, jobject clazz, jint handle) |
| 811 | { |
Dianne Hackborn | d76b67c | 2010-07-13 17:48:30 -0700 | [diff] [blame] | 812 | LOG_TRACE("onStop_native"); |
Dianne Hackborn | 69969e4 | 2010-05-04 11:40:40 -0700 | [diff] [blame] | 813 | if (handle != 0) { |
| 814 | NativeCode* code = (NativeCode*)handle; |
| 815 | if (code->callbacks.onStop != NULL) { |
Dianne Hackborn | 289b9b6 | 2010-07-09 11:44:11 -0700 | [diff] [blame] | 816 | code->callbacks.onStop(code); |
Dianne Hackborn | 69969e4 | 2010-05-04 11:40:40 -0700 | [diff] [blame] | 817 | } |
| 818 | } |
| 819 | } |
| 820 | |
| 821 | static void |
Dianne Hackborn | 08d5b8f | 2010-08-04 11:12:40 -0700 | [diff] [blame] | 822 | onConfigurationChanged_native(JNIEnv* env, jobject clazz, jint handle) |
| 823 | { |
| 824 | LOG_TRACE("onConfigurationChanged_native"); |
| 825 | if (handle != 0) { |
| 826 | NativeCode* code = (NativeCode*)handle; |
| 827 | if (code->callbacks.onConfigurationChanged != NULL) { |
| 828 | code->callbacks.onConfigurationChanged(code); |
| 829 | } |
| 830 | } |
| 831 | } |
| 832 | |
| 833 | static void |
Dianne Hackborn | 69969e4 | 2010-05-04 11:40:40 -0700 | [diff] [blame] | 834 | onLowMemory_native(JNIEnv* env, jobject clazz, jint handle) |
| 835 | { |
Dianne Hackborn | d76b67c | 2010-07-13 17:48:30 -0700 | [diff] [blame] | 836 | LOG_TRACE("onLowMemory_native"); |
Dianne Hackborn | 69969e4 | 2010-05-04 11:40:40 -0700 | [diff] [blame] | 837 | if (handle != 0) { |
| 838 | NativeCode* code = (NativeCode*)handle; |
| 839 | if (code->callbacks.onLowMemory != NULL) { |
Dianne Hackborn | 289b9b6 | 2010-07-09 11:44:11 -0700 | [diff] [blame] | 840 | code->callbacks.onLowMemory(code); |
Dianne Hackborn | 69969e4 | 2010-05-04 11:40:40 -0700 | [diff] [blame] | 841 | } |
| 842 | } |
| 843 | } |
| 844 | |
| 845 | static void |
| 846 | onWindowFocusChanged_native(JNIEnv* env, jobject clazz, jint handle, jboolean focused) |
| 847 | { |
Dianne Hackborn | d76b67c | 2010-07-13 17:48:30 -0700 | [diff] [blame] | 848 | LOG_TRACE("onWindowFocusChanged_native"); |
Dianne Hackborn | 69969e4 | 2010-05-04 11:40:40 -0700 | [diff] [blame] | 849 | if (handle != 0) { |
| 850 | NativeCode* code = (NativeCode*)handle; |
| 851 | if (code->callbacks.onWindowFocusChanged != NULL) { |
Dianne Hackborn | 289b9b6 | 2010-07-09 11:44:11 -0700 | [diff] [blame] | 852 | code->callbacks.onWindowFocusChanged(code, focused ? 1 : 0); |
Dianne Hackborn | 69969e4 | 2010-05-04 11:40:40 -0700 | [diff] [blame] | 853 | } |
| 854 | } |
| 855 | } |
| 856 | |
Dianne Hackborn | 74323fd | 2010-05-18 17:56:23 -0700 | [diff] [blame] | 857 | static void |
| 858 | onSurfaceCreated_native(JNIEnv* env, jobject clazz, jint handle, jobject surface) |
| 859 | { |
Dianne Hackborn | d76b67c | 2010-07-13 17:48:30 -0700 | [diff] [blame] | 860 | LOG_TRACE("onSurfaceCreated_native"); |
Dianne Hackborn | 74323fd | 2010-05-18 17:56:23 -0700 | [diff] [blame] | 861 | if (handle != 0) { |
| 862 | NativeCode* code = (NativeCode*)handle; |
| 863 | code->setSurface(surface); |
Dianne Hackborn | 54a181b | 2010-06-30 18:35:14 -0700 | [diff] [blame] | 864 | if (code->nativeWindow != NULL && code->callbacks.onNativeWindowCreated != NULL) { |
Dianne Hackborn | 289b9b6 | 2010-07-09 11:44:11 -0700 | [diff] [blame] | 865 | code->callbacks.onNativeWindowCreated(code, |
Dianne Hackborn | 54a181b | 2010-06-30 18:35:14 -0700 | [diff] [blame] | 866 | code->nativeWindow.get()); |
Dianne Hackborn | 74323fd | 2010-05-18 17:56:23 -0700 | [diff] [blame] | 867 | } |
| 868 | } |
| 869 | } |
| 870 | |
Dianne Hackborn | d76b67c | 2010-07-13 17:48:30 -0700 | [diff] [blame] | 871 | static int32_t getWindowProp(ANativeWindow* window, int what) { |
| 872 | int value; |
| 873 | int res = window->query(window, what, &value); |
| 874 | return res < 0 ? res : value; |
| 875 | } |
| 876 | |
Dianne Hackborn | 74323fd | 2010-05-18 17:56:23 -0700 | [diff] [blame] | 877 | static void |
| 878 | onSurfaceChanged_native(JNIEnv* env, jobject clazz, jint handle, jobject surface, |
| 879 | jint format, jint width, jint height) |
| 880 | { |
Dianne Hackborn | d76b67c | 2010-07-13 17:48:30 -0700 | [diff] [blame] | 881 | LOG_TRACE("onSurfaceChanged_native"); |
Dianne Hackborn | 74323fd | 2010-05-18 17:56:23 -0700 | [diff] [blame] | 882 | if (handle != 0) { |
| 883 | NativeCode* code = (NativeCode*)handle; |
Dianne Hackborn | 54a181b | 2010-06-30 18:35:14 -0700 | [diff] [blame] | 884 | sp<ANativeWindow> oldNativeWindow = code->nativeWindow; |
| 885 | code->setSurface(surface); |
| 886 | if (oldNativeWindow != code->nativeWindow) { |
Dianne Hackborn | 8ae5a8e | 2010-07-01 18:44:46 -0700 | [diff] [blame] | 887 | if (oldNativeWindow != NULL && code->callbacks.onNativeWindowDestroyed != NULL) { |
Dianne Hackborn | 289b9b6 | 2010-07-09 11:44:11 -0700 | [diff] [blame] | 888 | code->callbacks.onNativeWindowDestroyed(code, |
Dianne Hackborn | 8ae5a8e | 2010-07-01 18:44:46 -0700 | [diff] [blame] | 889 | oldNativeWindow.get()); |
| 890 | } |
Dianne Hackborn | d76b67c | 2010-07-13 17:48:30 -0700 | [diff] [blame] | 891 | if (code->nativeWindow != NULL) { |
| 892 | if (code->callbacks.onNativeWindowCreated != NULL) { |
| 893 | code->callbacks.onNativeWindowCreated(code, |
| 894 | code->nativeWindow.get()); |
| 895 | } |
| 896 | code->lastWindowWidth = getWindowProp(code->nativeWindow.get(), |
| 897 | NATIVE_WINDOW_WIDTH); |
| 898 | code->lastWindowHeight = getWindowProp(code->nativeWindow.get(), |
| 899 | NATIVE_WINDOW_HEIGHT); |
Dianne Hackborn | 54a181b | 2010-06-30 18:35:14 -0700 | [diff] [blame] | 900 | } |
Dianne Hackborn | d76b67c | 2010-07-13 17:48:30 -0700 | [diff] [blame] | 901 | } else { |
| 902 | // Maybe it resized? |
| 903 | int32_t newWidth = getWindowProp(code->nativeWindow.get(), |
| 904 | NATIVE_WINDOW_WIDTH); |
| 905 | int32_t newHeight = getWindowProp(code->nativeWindow.get(), |
| 906 | NATIVE_WINDOW_HEIGHT); |
| 907 | if (newWidth != code->lastWindowWidth |
| 908 | || newHeight != code->lastWindowHeight) { |
| 909 | if (code->callbacks.onNativeWindowResized != NULL) { |
| 910 | code->callbacks.onNativeWindowResized(code, |
| 911 | code->nativeWindow.get()); |
| 912 | } |
| 913 | } |
| 914 | } |
| 915 | } |
| 916 | } |
| 917 | |
| 918 | static void |
| 919 | onSurfaceRedrawNeeded_native(JNIEnv* env, jobject clazz, jint handle) |
| 920 | { |
| 921 | LOG_TRACE("onSurfaceRedrawNeeded_native"); |
| 922 | if (handle != 0) { |
| 923 | NativeCode* code = (NativeCode*)handle; |
| 924 | if (code->nativeWindow != NULL && code->callbacks.onNativeWindowRedrawNeeded != NULL) { |
| 925 | code->callbacks.onNativeWindowRedrawNeeded(code, code->nativeWindow.get()); |
Dianne Hackborn | 74323fd | 2010-05-18 17:56:23 -0700 | [diff] [blame] | 926 | } |
| 927 | } |
| 928 | } |
| 929 | |
| 930 | static void |
| 931 | onSurfaceDestroyed_native(JNIEnv* env, jobject clazz, jint handle, jobject surface) |
| 932 | { |
Dianne Hackborn | d76b67c | 2010-07-13 17:48:30 -0700 | [diff] [blame] | 933 | LOG_TRACE("onSurfaceDestroyed_native"); |
Dianne Hackborn | 74323fd | 2010-05-18 17:56:23 -0700 | [diff] [blame] | 934 | if (handle != 0) { |
| 935 | NativeCode* code = (NativeCode*)handle; |
Dianne Hackborn | 54a181b | 2010-06-30 18:35:14 -0700 | [diff] [blame] | 936 | if (code->nativeWindow != NULL && code->callbacks.onNativeWindowDestroyed != NULL) { |
Dianne Hackborn | 289b9b6 | 2010-07-09 11:44:11 -0700 | [diff] [blame] | 937 | code->callbacks.onNativeWindowDestroyed(code, |
Dianne Hackborn | 54a181b | 2010-06-30 18:35:14 -0700 | [diff] [blame] | 938 | code->nativeWindow.get()); |
Dianne Hackborn | 74323fd | 2010-05-18 17:56:23 -0700 | [diff] [blame] | 939 | } |
| 940 | code->setSurface(NULL); |
| 941 | } |
| 942 | } |
| 943 | |
Dianne Hackborn | a95e4cb | 2010-06-18 18:09:33 -0700 | [diff] [blame] | 944 | static void |
| 945 | onInputChannelCreated_native(JNIEnv* env, jobject clazz, jint handle, jobject channel) |
| 946 | { |
Dianne Hackborn | d76b67c | 2010-07-13 17:48:30 -0700 | [diff] [blame] | 947 | LOG_TRACE("onInputChannelCreated_native"); |
Dianne Hackborn | a95e4cb | 2010-06-18 18:09:33 -0700 | [diff] [blame] | 948 | if (handle != 0) { |
| 949 | NativeCode* code = (NativeCode*)handle; |
| 950 | status_t err = code->setInputChannel(channel); |
| 951 | if (err != OK) { |
| 952 | jniThrowException(env, "java/lang/IllegalStateException", |
| 953 | "Error setting input channel"); |
| 954 | return; |
| 955 | } |
| 956 | if (code->callbacks.onInputQueueCreated != NULL) { |
Dianne Hackborn | 289b9b6 | 2010-07-09 11:44:11 -0700 | [diff] [blame] | 957 | code->callbacks.onInputQueueCreated(code, |
Dianne Hackborn | a95e4cb | 2010-06-18 18:09:33 -0700 | [diff] [blame] | 958 | code->nativeInputQueue); |
| 959 | } |
| 960 | } |
| 961 | } |
| 962 | |
| 963 | static void |
| 964 | onInputChannelDestroyed_native(JNIEnv* env, jobject clazz, jint handle, jobject channel) |
| 965 | { |
Dianne Hackborn | d76b67c | 2010-07-13 17:48:30 -0700 | [diff] [blame] | 966 | LOG_TRACE("onInputChannelDestroyed_native"); |
Dianne Hackborn | a95e4cb | 2010-06-18 18:09:33 -0700 | [diff] [blame] | 967 | if (handle != 0) { |
| 968 | NativeCode* code = (NativeCode*)handle; |
| 969 | if (code->nativeInputQueue != NULL |
| 970 | && code->callbacks.onInputQueueDestroyed != NULL) { |
Dianne Hackborn | 289b9b6 | 2010-07-09 11:44:11 -0700 | [diff] [blame] | 971 | code->callbacks.onInputQueueDestroyed(code, |
Dianne Hackborn | a95e4cb | 2010-06-18 18:09:33 -0700 | [diff] [blame] | 972 | code->nativeInputQueue); |
| 973 | } |
| 974 | code->setInputChannel(NULL); |
| 975 | } |
| 976 | } |
| 977 | |
Dianne Hackborn | d76b67c | 2010-07-13 17:48:30 -0700 | [diff] [blame] | 978 | static void |
| 979 | onContentRectChanged_native(JNIEnv* env, jobject clazz, jint handle, |
| 980 | jint x, jint y, jint w, jint h) |
| 981 | { |
| 982 | LOG_TRACE("onContentRectChanged_native"); |
| 983 | if (handle != 0) { |
| 984 | NativeCode* code = (NativeCode*)handle; |
| 985 | if (code->callbacks.onContentRectChanged != NULL) { |
| 986 | ARect rect; |
| 987 | rect.left = x; |
| 988 | rect.top = y; |
| 989 | rect.right = x+w; |
| 990 | rect.bottom = y+h; |
| 991 | code->callbacks.onContentRectChanged(code, &rect); |
| 992 | } |
| 993 | } |
| 994 | } |
| 995 | |
| 996 | static void |
| 997 | dispatchKeyEvent_native(JNIEnv* env, jobject clazz, jint handle, jobject eventObj) |
| 998 | { |
| 999 | LOG_TRACE("dispatchKeyEvent_native"); |
| 1000 | if (handle != 0) { |
| 1001 | NativeCode* code = (NativeCode*)handle; |
| 1002 | if (code->nativeInputQueue != NULL) { |
Dianne Hackborn | 2c6081c | 2010-07-15 17:44:53 -0700 | [diff] [blame] | 1003 | KeyEvent* event = code->nativeInputQueue->createKeyEvent(); |
Jeff Brown | 1f24510 | 2010-11-18 20:53:46 -0800 | [diff] [blame] | 1004 | status_t status = android_view_KeyEvent_toNative(env, eventObj, event); |
| 1005 | if (status) { |
| 1006 | delete event; |
| 1007 | jniThrowRuntimeException(env, "Could not read contents of KeyEvent object."); |
| 1008 | return; |
| 1009 | } |
Dianne Hackborn | d76b67c | 2010-07-13 17:48:30 -0700 | [diff] [blame] | 1010 | code->nativeInputQueue->dispatchEvent(event); |
| 1011 | } |
| 1012 | } |
| 1013 | } |
| 1014 | |
Dianne Hackborn | 2c6081c | 2010-07-15 17:44:53 -0700 | [diff] [blame] | 1015 | static void |
| 1016 | finishPreDispatchKeyEvent_native(JNIEnv* env, jobject clazz, jint handle, |
| 1017 | jint seq, jboolean handled) |
| 1018 | { |
| 1019 | LOG_TRACE("finishPreDispatchKeyEvent_native"); |
| 1020 | if (handle != 0) { |
| 1021 | NativeCode* code = (NativeCode*)handle; |
| 1022 | if (code->nativeInputQueue != NULL) { |
| 1023 | code->nativeInputQueue->finishPreDispatch(seq, handled ? true : false); |
| 1024 | } |
| 1025 | } |
| 1026 | } |
| 1027 | |
Dianne Hackborn | 69969e4 | 2010-05-04 11:40:40 -0700 | [diff] [blame] | 1028 | static const JNINativeMethod g_methods[] = { |
Dianne Hackborn | 805fd7e | 2011-01-16 18:30:29 -0800 | [diff] [blame] | 1029 | { "loadNativeCode", "(Ljava/lang/String;Ljava/lang/String;Landroid/os/MessageQueue;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;ILandroid/content/res/AssetManager;[B)I", |
Dianne Hackborn | 6826741 | 2010-07-02 18:52:01 -0700 | [diff] [blame] | 1030 | (void*)loadNativeCode_native }, |
Dianne Hackborn | 69969e4 | 2010-05-04 11:40:40 -0700 | [diff] [blame] | 1031 | { "unloadNativeCode", "(I)V", (void*)unloadNativeCode_native }, |
| 1032 | { "onStartNative", "(I)V", (void*)onStart_native }, |
| 1033 | { "onResumeNative", "(I)V", (void*)onResume_native }, |
Dianne Hackborn | 08d5b8f | 2010-08-04 11:12:40 -0700 | [diff] [blame] | 1034 | { "onSaveInstanceStateNative", "(I)[B", (void*)onSaveInstanceState_native }, |
Dianne Hackborn | 69969e4 | 2010-05-04 11:40:40 -0700 | [diff] [blame] | 1035 | { "onPauseNative", "(I)V", (void*)onPause_native }, |
| 1036 | { "onStopNative", "(I)V", (void*)onStop_native }, |
Dianne Hackborn | 08d5b8f | 2010-08-04 11:12:40 -0700 | [diff] [blame] | 1037 | { "onConfigurationChangedNative", "(I)V", (void*)onConfigurationChanged_native }, |
Dianne Hackborn | 69969e4 | 2010-05-04 11:40:40 -0700 | [diff] [blame] | 1038 | { "onLowMemoryNative", "(I)V", (void*)onLowMemory_native }, |
| 1039 | { "onWindowFocusChangedNative", "(IZ)V", (void*)onWindowFocusChanged_native }, |
Dianne Hackborn | 54a181b | 2010-06-30 18:35:14 -0700 | [diff] [blame] | 1040 | { "onSurfaceCreatedNative", "(ILandroid/view/Surface;)V", (void*)onSurfaceCreated_native }, |
| 1041 | { "onSurfaceChangedNative", "(ILandroid/view/Surface;III)V", (void*)onSurfaceChanged_native }, |
Dianne Hackborn | d76b67c | 2010-07-13 17:48:30 -0700 | [diff] [blame] | 1042 | { "onSurfaceRedrawNeededNative", "(ILandroid/view/Surface;)V", (void*)onSurfaceRedrawNeeded_native }, |
Dianne Hackborn | 54a181b | 2010-06-30 18:35:14 -0700 | [diff] [blame] | 1043 | { "onSurfaceDestroyedNative", "(I)V", (void*)onSurfaceDestroyed_native }, |
Dianne Hackborn | a95e4cb | 2010-06-18 18:09:33 -0700 | [diff] [blame] | 1044 | { "onInputChannelCreatedNative", "(ILandroid/view/InputChannel;)V", (void*)onInputChannelCreated_native }, |
| 1045 | { "onInputChannelDestroyedNative", "(ILandroid/view/InputChannel;)V", (void*)onInputChannelDestroyed_native }, |
Dianne Hackborn | d76b67c | 2010-07-13 17:48:30 -0700 | [diff] [blame] | 1046 | { "onContentRectChangedNative", "(IIIII)V", (void*)onContentRectChanged_native }, |
| 1047 | { "dispatchKeyEventNative", "(ILandroid/view/KeyEvent;)V", (void*)dispatchKeyEvent_native }, |
Dianne Hackborn | 2c6081c | 2010-07-15 17:44:53 -0700 | [diff] [blame] | 1048 | { "finishPreDispatchKeyEventNative", "(IIZ)V", (void*)finishPreDispatchKeyEvent_native }, |
Dianne Hackborn | 69969e4 | 2010-05-04 11:40:40 -0700 | [diff] [blame] | 1049 | }; |
| 1050 | |
| 1051 | static const char* const kNativeActivityPathName = "android/app/NativeActivity"; |
| 1052 | |
Dianne Hackborn | 3c80a4a | 2010-06-29 19:20:40 -0700 | [diff] [blame] | 1053 | #define FIND_CLASS(var, className) \ |
| 1054 | var = env->FindClass(className); \ |
Carl Shapiro | 17cc33a | 2011-03-05 20:53:16 -0800 | [diff] [blame] | 1055 | LOG_FATAL_IF(! var, "Unable to find class %s", className); |
Dianne Hackborn | 3c80a4a | 2010-06-29 19:20:40 -0700 | [diff] [blame] | 1056 | |
| 1057 | #define GET_METHOD_ID(var, clazz, methodName, fieldDescriptor) \ |
| 1058 | var = env->GetMethodID(clazz, methodName, fieldDescriptor); \ |
| 1059 | LOG_FATAL_IF(! var, "Unable to find method" methodName); |
| 1060 | |
Dianne Hackborn | 69969e4 | 2010-05-04 11:40:40 -0700 | [diff] [blame] | 1061 | int register_android_app_NativeActivity(JNIEnv* env) |
| 1062 | { |
| 1063 | //LOGD("register_android_app_NativeActivity"); |
Carl Shapiro | 17cc33a | 2011-03-05 20:53:16 -0800 | [diff] [blame] | 1064 | jclass clazz; |
| 1065 | FIND_CLASS(clazz, kNativeActivityPathName); |
Dianne Hackborn | 69969e4 | 2010-05-04 11:40:40 -0700 | [diff] [blame] | 1066 | |
Dianne Hackborn | 3c80a4a | 2010-06-29 19:20:40 -0700 | [diff] [blame] | 1067 | GET_METHOD_ID(gNativeActivityClassInfo.dispatchUnhandledKeyEvent, |
Carl Shapiro | 17cc33a | 2011-03-05 20:53:16 -0800 | [diff] [blame] | 1068 | clazz, |
Jeff Brown | 3915bb8 | 2010-11-05 15:02:16 -0700 | [diff] [blame] | 1069 | "dispatchUnhandledKeyEvent", "(Landroid/view/KeyEvent;)Z"); |
Dianne Hackborn | 2c6081c | 2010-07-15 17:44:53 -0700 | [diff] [blame] | 1070 | GET_METHOD_ID(gNativeActivityClassInfo.preDispatchKeyEvent, |
Carl Shapiro | 17cc33a | 2011-03-05 20:53:16 -0800 | [diff] [blame] | 1071 | clazz, |
Dianne Hackborn | 2c6081c | 2010-07-15 17:44:53 -0700 | [diff] [blame] | 1072 | "preDispatchKeyEvent", "(Landroid/view/KeyEvent;I)V"); |
Dianne Hackborn | 54a181b | 2010-06-30 18:35:14 -0700 | [diff] [blame] | 1073 | |
Dianne Hackborn | db28a94 | 2010-10-21 17:22:30 -0700 | [diff] [blame] | 1074 | GET_METHOD_ID(gNativeActivityClassInfo.finish, |
Carl Shapiro | 17cc33a | 2011-03-05 20:53:16 -0800 | [diff] [blame] | 1075 | clazz, |
Dianne Hackborn | db28a94 | 2010-10-21 17:22:30 -0700 | [diff] [blame] | 1076 | "finish", "()V"); |
Dianne Hackborn | 54a181b | 2010-06-30 18:35:14 -0700 | [diff] [blame] | 1077 | GET_METHOD_ID(gNativeActivityClassInfo.setWindowFlags, |
Carl Shapiro | 17cc33a | 2011-03-05 20:53:16 -0800 | [diff] [blame] | 1078 | clazz, |
Dianne Hackborn | 54a181b | 2010-06-30 18:35:14 -0700 | [diff] [blame] | 1079 | "setWindowFlags", "(II)V"); |
| 1080 | GET_METHOD_ID(gNativeActivityClassInfo.setWindowFormat, |
Carl Shapiro | 17cc33a | 2011-03-05 20:53:16 -0800 | [diff] [blame] | 1081 | clazz, |
Dianne Hackborn | 54a181b | 2010-06-30 18:35:14 -0700 | [diff] [blame] | 1082 | "setWindowFormat", "(I)V"); |
Dianne Hackborn | d76b67c | 2010-07-13 17:48:30 -0700 | [diff] [blame] | 1083 | GET_METHOD_ID(gNativeActivityClassInfo.showIme, |
Carl Shapiro | 17cc33a | 2011-03-05 20:53:16 -0800 | [diff] [blame] | 1084 | clazz, |
Dianne Hackborn | d76b67c | 2010-07-13 17:48:30 -0700 | [diff] [blame] | 1085 | "showIme", "(I)V"); |
| 1086 | GET_METHOD_ID(gNativeActivityClassInfo.hideIme, |
Carl Shapiro | 17cc33a | 2011-03-05 20:53:16 -0800 | [diff] [blame] | 1087 | clazz, |
Dianne Hackborn | d76b67c | 2010-07-13 17:48:30 -0700 | [diff] [blame] | 1088 | "hideIme", "(I)V"); |
Dianne Hackborn | 54a181b | 2010-06-30 18:35:14 -0700 | [diff] [blame] | 1089 | |
Dianne Hackborn | 69969e4 | 2010-05-04 11:40:40 -0700 | [diff] [blame] | 1090 | return AndroidRuntime::registerNativeMethods( |
| 1091 | env, kNativeActivityPathName, |
| 1092 | g_methods, NELEM(g_methods)); |
| 1093 | } |
| 1094 | |
Dianne Hackborn | a95e4cb | 2010-06-18 18:09:33 -0700 | [diff] [blame] | 1095 | } // namespace android |