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