Christopher Tate | fa9e7c0 | 2010-05-06 12:07:10 -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 | |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 17 | #define LOG_TAG "MessageQueue-JNI" |
Christopher Tate | fa9e7c0 | 2010-05-06 12:07:10 -0700 | [diff] [blame] | 18 | |
| 19 | #include "JNIHelp.h" |
Jeff Brown | 603b445 | 2012-04-06 17:39:41 -0700 | [diff] [blame] | 20 | #include <android_runtime/AndroidRuntime.h> |
Christopher Tate | fa9e7c0 | 2010-05-06 12:07:10 -0700 | [diff] [blame] | 21 | |
Jeff Brown | 4fe6c3e | 2010-09-13 23:17:30 -0700 | [diff] [blame] | 22 | #include <utils/Looper.h> |
Christopher Tate | fa9e7c0 | 2010-05-06 12:07:10 -0700 | [diff] [blame] | 23 | #include <utils/Log.h> |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 24 | #include "android_os_MessageQueue.h" |
Christopher Tate | fa9e7c0 | 2010-05-06 12:07:10 -0700 | [diff] [blame] | 25 | |
Andreas Gampe | 987f79f | 2014-11-18 17:29:46 -0800 | [diff] [blame] | 26 | #include "core_jni_helpers.h" |
| 27 | |
Christopher Tate | fa9e7c0 | 2010-05-06 12:07:10 -0700 | [diff] [blame] | 28 | namespace android { |
Christopher Tate | fa9e7c0 | 2010-05-06 12:07:10 -0700 | [diff] [blame] | 29 | |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 30 | static struct { |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 31 | jfieldID mPtr; // native object attached to the DVM MessageQueue |
Jeff Brown | dc3eb4b | 2015-03-05 18:21:06 -0800 | [diff] [blame] | 32 | jmethodID dispatchEvents; |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 33 | } gMessageQueueClassInfo; |
| 34 | |
Jeff Brown | dc3eb4b | 2015-03-05 18:21:06 -0800 | [diff] [blame] | 35 | // Must be kept in sync with the constants in Looper.FileDescriptorCallback |
| 36 | static const int CALLBACK_EVENT_INPUT = 1 << 0; |
| 37 | static const int CALLBACK_EVENT_OUTPUT = 1 << 1; |
| 38 | static const int CALLBACK_EVENT_ERROR = 1 << 2; |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 39 | |
Jeff Brown | dc3eb4b | 2015-03-05 18:21:06 -0800 | [diff] [blame] | 40 | |
| 41 | class NativeMessageQueue : public MessageQueue, public LooperCallback { |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 42 | public: |
| 43 | NativeMessageQueue(); |
Jeff Brown | 603b445 | 2012-04-06 17:39:41 -0700 | [diff] [blame] | 44 | virtual ~NativeMessageQueue(); |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 45 | |
Jeff Brown | 603b445 | 2012-04-06 17:39:41 -0700 | [diff] [blame] | 46 | virtual void raiseException(JNIEnv* env, const char* msg, jthrowable exceptionObj); |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 47 | |
Jeff Brown | dc3eb4b | 2015-03-05 18:21:06 -0800 | [diff] [blame] | 48 | void pollOnce(JNIEnv* env, jobject obj, int timeoutMillis); |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 49 | void wake(); |
Jeff Brown | dc3eb4b | 2015-03-05 18:21:06 -0800 | [diff] [blame] | 50 | void setFileDescriptorEvents(int fd, int events); |
| 51 | |
| 52 | virtual int handleEvent(int fd, int events, void* data); |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 53 | |
| 54 | private: |
Jeff Brown | dc3eb4b | 2015-03-05 18:21:06 -0800 | [diff] [blame] | 55 | JNIEnv* mPollEnv; |
| 56 | jobject mPollObj; |
Jeff Brown | 603b445 | 2012-04-06 17:39:41 -0700 | [diff] [blame] | 57 | jthrowable mExceptionObj; |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 58 | }; |
| 59 | |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 60 | |
Jeff Brown | 603b445 | 2012-04-06 17:39:41 -0700 | [diff] [blame] | 61 | MessageQueue::MessageQueue() { |
| 62 | } |
| 63 | |
| 64 | MessageQueue::~MessageQueue() { |
| 65 | } |
| 66 | |
| 67 | bool MessageQueue::raiseAndClearException(JNIEnv* env, const char* msg) { |
Mathieu Chartier | 98671c3 | 2014-08-20 10:04:08 -0700 | [diff] [blame] | 68 | if (env->ExceptionCheck()) { |
| 69 | jthrowable exceptionObj = env->ExceptionOccurred(); |
Jeff Brown | 603b445 | 2012-04-06 17:39:41 -0700 | [diff] [blame] | 70 | env->ExceptionClear(); |
| 71 | raiseException(env, msg, exceptionObj); |
| 72 | env->DeleteLocalRef(exceptionObj); |
| 73 | return true; |
| 74 | } |
| 75 | return false; |
| 76 | } |
| 77 | |
Jeff Brown | dc3eb4b | 2015-03-05 18:21:06 -0800 | [diff] [blame] | 78 | NativeMessageQueue::NativeMessageQueue() : |
| 79 | mPollEnv(NULL), mPollObj(NULL), mExceptionObj(NULL) { |
Jeff Brown | 4fe6c3e | 2010-09-13 23:17:30 -0700 | [diff] [blame] | 80 | mLooper = Looper::getForThread(); |
| 81 | if (mLooper == NULL) { |
Jeff Brown | 942c474 | 2015-03-12 19:36:28 -0700 | [diff] [blame] | 82 | mLooper = new Looper(false); |
Jeff Brown | 4fe6c3e | 2010-09-13 23:17:30 -0700 | [diff] [blame] | 83 | Looper::setForThread(mLooper); |
Dianne Hackborn | 6826741 | 2010-07-02 18:52:01 -0700 | [diff] [blame] | 84 | } |
Christopher Tate | fa9e7c0 | 2010-05-06 12:07:10 -0700 | [diff] [blame] | 85 | } |
| 86 | |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 87 | NativeMessageQueue::~NativeMessageQueue() { |
Christopher Tate | fa9e7c0 | 2010-05-06 12:07:10 -0700 | [diff] [blame] | 88 | } |
| 89 | |
Jeff Brown | 603b445 | 2012-04-06 17:39:41 -0700 | [diff] [blame] | 90 | void NativeMessageQueue::raiseException(JNIEnv* env, const char* msg, jthrowable exceptionObj) { |
| 91 | if (exceptionObj) { |
Jeff Brown | dc3eb4b | 2015-03-05 18:21:06 -0800 | [diff] [blame] | 92 | if (mPollEnv == env) { |
Jeff Brown | 603b445 | 2012-04-06 17:39:41 -0700 | [diff] [blame] | 93 | if (mExceptionObj) { |
| 94 | env->DeleteLocalRef(mExceptionObj); |
| 95 | } |
| 96 | mExceptionObj = jthrowable(env->NewLocalRef(exceptionObj)); |
| 97 | ALOGE("Exception in MessageQueue callback: %s", msg); |
| 98 | jniLogException(env, ANDROID_LOG_ERROR, LOG_TAG, exceptionObj); |
| 99 | } else { |
| 100 | ALOGE("Exception: %s", msg); |
| 101 | jniLogException(env, ANDROID_LOG_ERROR, LOG_TAG, exceptionObj); |
| 102 | LOG_ALWAYS_FATAL("raiseException() was called when not in a callback, exiting."); |
| 103 | } |
| 104 | } |
| 105 | } |
| 106 | |
Jeff Brown | dc3eb4b | 2015-03-05 18:21:06 -0800 | [diff] [blame] | 107 | void NativeMessageQueue::pollOnce(JNIEnv* env, jobject pollObj, int timeoutMillis) { |
| 108 | mPollEnv = env; |
| 109 | mPollObj = pollObj; |
Jeff Brown | 415d8c3 | 2010-10-05 15:35:37 -0700 | [diff] [blame] | 110 | mLooper->pollOnce(timeoutMillis); |
Jeff Brown | dc3eb4b | 2015-03-05 18:21:06 -0800 | [diff] [blame] | 111 | mPollObj = NULL; |
| 112 | mPollEnv = NULL; |
| 113 | |
Jeff Brown | 603b445 | 2012-04-06 17:39:41 -0700 | [diff] [blame] | 114 | if (mExceptionObj) { |
| 115 | env->Throw(mExceptionObj); |
| 116 | env->DeleteLocalRef(mExceptionObj); |
| 117 | mExceptionObj = NULL; |
| 118 | } |
Christopher Tate | fa9e7c0 | 2010-05-06 12:07:10 -0700 | [diff] [blame] | 119 | } |
| 120 | |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 121 | void NativeMessageQueue::wake() { |
Jeff Brown | 4fe6c3e | 2010-09-13 23:17:30 -0700 | [diff] [blame] | 122 | mLooper->wake(); |
Christopher Tate | fa9e7c0 | 2010-05-06 12:07:10 -0700 | [diff] [blame] | 123 | } |
| 124 | |
Jeff Brown | dc3eb4b | 2015-03-05 18:21:06 -0800 | [diff] [blame] | 125 | void NativeMessageQueue::setFileDescriptorEvents(int fd, int events) { |
| 126 | if (events) { |
| 127 | int looperEvents = 0; |
| 128 | if (events & CALLBACK_EVENT_INPUT) { |
| 129 | looperEvents |= Looper::EVENT_INPUT; |
| 130 | } |
| 131 | if (events & CALLBACK_EVENT_OUTPUT) { |
| 132 | looperEvents |= Looper::EVENT_OUTPUT; |
| 133 | } |
| 134 | mLooper->addFd(fd, Looper::POLL_CALLBACK, looperEvents, this, |
| 135 | reinterpret_cast<void*>(events)); |
| 136 | } else { |
| 137 | mLooper->removeFd(fd); |
| 138 | } |
| 139 | } |
| 140 | |
| 141 | int NativeMessageQueue::handleEvent(int fd, int looperEvents, void* data) { |
| 142 | int events = 0; |
| 143 | if (looperEvents & Looper::EVENT_INPUT) { |
| 144 | events |= CALLBACK_EVENT_INPUT; |
| 145 | } |
| 146 | if (looperEvents & Looper::EVENT_OUTPUT) { |
| 147 | events |= CALLBACK_EVENT_OUTPUT; |
| 148 | } |
| 149 | if (looperEvents & (Looper::EVENT_ERROR | Looper::EVENT_HANGUP | Looper::EVENT_INVALID)) { |
| 150 | events |= CALLBACK_EVENT_ERROR; |
| 151 | } |
Greg Hackmann | d5e97f9 | 2015-03-11 16:47:15 -0700 | [diff] [blame] | 152 | int oldWatchedEvents = reinterpret_cast<intptr_t>(data); |
Jeff Brown | dc3eb4b | 2015-03-05 18:21:06 -0800 | [diff] [blame] | 153 | int newWatchedEvents = mPollEnv->CallIntMethod(mPollObj, |
| 154 | gMessageQueueClassInfo.dispatchEvents, fd, events); |
| 155 | if (!newWatchedEvents) { |
| 156 | return 0; // unregister the fd |
| 157 | } |
| 158 | if (newWatchedEvents != oldWatchedEvents) { |
| 159 | setFileDescriptorEvents(fd, newWatchedEvents); |
| 160 | } |
| 161 | return 1; |
| 162 | } |
| 163 | |
| 164 | |
Christopher Tate | fa9e7c0 | 2010-05-06 12:07:10 -0700 | [diff] [blame] | 165 | // ---------------------------------------------------------------------------- |
| 166 | |
Jeff Brown | 86469346 | 2013-01-28 14:25:53 -0800 | [diff] [blame] | 167 | sp<MessageQueue> android_os_MessageQueue_getMessageQueue(JNIEnv* env, jobject messageQueueObj) { |
Ashok Bhat | 63a3715 | 2014-01-10 14:15:21 +0000 | [diff] [blame] | 168 | jlong ptr = env->GetLongField(messageQueueObj, gMessageQueueClassInfo.mPtr); |
| 169 | return reinterpret_cast<NativeMessageQueue*>(ptr); |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 170 | } |
| 171 | |
Ashok Bhat | 63a3715 | 2014-01-10 14:15:21 +0000 | [diff] [blame] | 172 | static jlong android_os_MessageQueue_nativeInit(JNIEnv* env, jclass clazz) { |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 173 | NativeMessageQueue* nativeMessageQueue = new NativeMessageQueue(); |
Jeff Brown | 603b445 | 2012-04-06 17:39:41 -0700 | [diff] [blame] | 174 | if (!nativeMessageQueue) { |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 175 | jniThrowRuntimeException(env, "Unable to allocate native queue"); |
Jeff Brown | 86469346 | 2013-01-28 14:25:53 -0800 | [diff] [blame] | 176 | return 0; |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 177 | } |
| 178 | |
Jeff Brown | 603b445 | 2012-04-06 17:39:41 -0700 | [diff] [blame] | 179 | nativeMessageQueue->incStrong(env); |
Ashok Bhat | 63a3715 | 2014-01-10 14:15:21 +0000 | [diff] [blame] | 180 | return reinterpret_cast<jlong>(nativeMessageQueue); |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 181 | } |
| 182 | |
Ashok Bhat | 63a3715 | 2014-01-10 14:15:21 +0000 | [diff] [blame] | 183 | static void android_os_MessageQueue_nativeDestroy(JNIEnv* env, jclass clazz, jlong ptr) { |
Jeff Brown | 86469346 | 2013-01-28 14:25:53 -0800 | [diff] [blame] | 184 | NativeMessageQueue* nativeMessageQueue = reinterpret_cast<NativeMessageQueue*>(ptr); |
| 185 | nativeMessageQueue->decStrong(env); |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 186 | } |
| 187 | |
Jeff Brown | dc3eb4b | 2015-03-05 18:21:06 -0800 | [diff] [blame] | 188 | static void android_os_MessageQueue_nativePollOnce(JNIEnv* env, jobject obj, |
Ashok Bhat | 63a3715 | 2014-01-10 14:15:21 +0000 | [diff] [blame] | 189 | jlong ptr, jint timeoutMillis) { |
Jeff Brown | 415d8c3 | 2010-10-05 15:35:37 -0700 | [diff] [blame] | 190 | NativeMessageQueue* nativeMessageQueue = reinterpret_cast<NativeMessageQueue*>(ptr); |
Jeff Brown | dc3eb4b | 2015-03-05 18:21:06 -0800 | [diff] [blame] | 191 | nativeMessageQueue->pollOnce(env, obj, timeoutMillis); |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 192 | } |
| 193 | |
Ashok Bhat | 63a3715 | 2014-01-10 14:15:21 +0000 | [diff] [blame] | 194 | static void android_os_MessageQueue_nativeWake(JNIEnv* env, jclass clazz, jlong ptr) { |
Jeff Brown | 415d8c3 | 2010-10-05 15:35:37 -0700 | [diff] [blame] | 195 | NativeMessageQueue* nativeMessageQueue = reinterpret_cast<NativeMessageQueue*>(ptr); |
Jeff Brown | dc3eb4b | 2015-03-05 18:21:06 -0800 | [diff] [blame] | 196 | nativeMessageQueue->wake(); |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 197 | } |
| 198 | |
Jeff Brown | 6c7b41a | 2015-02-26 14:43:53 -0800 | [diff] [blame] | 199 | static jboolean android_os_MessageQueue_nativeIsPolling(JNIEnv* env, jclass clazz, jlong ptr) { |
Dianne Hackborn | efa92b2 | 2013-05-03 14:11:43 -0700 | [diff] [blame] | 200 | NativeMessageQueue* nativeMessageQueue = reinterpret_cast<NativeMessageQueue*>(ptr); |
Jeff Brown | 6c7b41a | 2015-02-26 14:43:53 -0800 | [diff] [blame] | 201 | return nativeMessageQueue->getLooper()->isPolling(); |
Dianne Hackborn | efa92b2 | 2013-05-03 14:11:43 -0700 | [diff] [blame] | 202 | } |
| 203 | |
Jeff Brown | dc3eb4b | 2015-03-05 18:21:06 -0800 | [diff] [blame] | 204 | static void android_os_MessageQueue_nativeSetFileDescriptorEvents(JNIEnv* env, jclass clazz, |
| 205 | jlong ptr, jint fd, jint events) { |
| 206 | NativeMessageQueue* nativeMessageQueue = reinterpret_cast<NativeMessageQueue*>(ptr); |
| 207 | nativeMessageQueue->setFileDescriptorEvents(fd, events); |
| 208 | } |
| 209 | |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 210 | // ---------------------------------------------------------------------------- |
Christopher Tate | fa9e7c0 | 2010-05-06 12:07:10 -0700 | [diff] [blame] | 211 | |
Daniel Micay | 76f6a86 | 2015-09-19 17:31:01 -0400 | [diff] [blame] | 212 | static const JNINativeMethod gMessageQueueMethods[] = { |
Christopher Tate | fa9e7c0 | 2010-05-06 12:07:10 -0700 | [diff] [blame] | 213 | /* name, signature, funcPtr */ |
Ashok Bhat | 63a3715 | 2014-01-10 14:15:21 +0000 | [diff] [blame] | 214 | { "nativeInit", "()J", (void*)android_os_MessageQueue_nativeInit }, |
| 215 | { "nativeDestroy", "(J)V", (void*)android_os_MessageQueue_nativeDestroy }, |
| 216 | { "nativePollOnce", "(JI)V", (void*)android_os_MessageQueue_nativePollOnce }, |
| 217 | { "nativeWake", "(J)V", (void*)android_os_MessageQueue_nativeWake }, |
Jeff Brown | dc3eb4b | 2015-03-05 18:21:06 -0800 | [diff] [blame] | 218 | { "nativeIsPolling", "(J)Z", (void*)android_os_MessageQueue_nativeIsPolling }, |
| 219 | { "nativeSetFileDescriptorEvents", "(JII)V", |
| 220 | (void*)android_os_MessageQueue_nativeSetFileDescriptorEvents }, |
Christopher Tate | fa9e7c0 | 2010-05-06 12:07:10 -0700 | [diff] [blame] | 221 | }; |
| 222 | |
| 223 | int register_android_os_MessageQueue(JNIEnv* env) { |
Andreas Gampe | 987f79f | 2014-11-18 17:29:46 -0800 | [diff] [blame] | 224 | int res = RegisterMethodsOrDie(env, "android/os/MessageQueue", gMessageQueueMethods, |
| 225 | NELEM(gMessageQueueMethods)); |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 226 | |
Andreas Gampe | 987f79f | 2014-11-18 17:29:46 -0800 | [diff] [blame] | 227 | jclass clazz = FindClassOrDie(env, "android/os/MessageQueue"); |
Andreas Gampe | 987f79f | 2014-11-18 17:29:46 -0800 | [diff] [blame] | 228 | gMessageQueueClassInfo.mPtr = GetFieldIDOrDie(env, clazz, "mPtr", "J"); |
Jeff Brown | dc3eb4b | 2015-03-05 18:21:06 -0800 | [diff] [blame] | 229 | gMessageQueueClassInfo.dispatchEvents = GetMethodIDOrDie(env, clazz, |
| 230 | "dispatchEvents", "(II)I"); |
Andreas Gampe | 987f79f | 2014-11-18 17:29:46 -0800 | [diff] [blame] | 231 | |
| 232 | return res; |
Christopher Tate | fa9e7c0 | 2010-05-06 12:07:10 -0700 | [diff] [blame] | 233 | } |
| 234 | |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 235 | } // namespace android |