Dianne Hackborn | 6826741 | 2010-07-02 18:52:01 -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 "ALooper" |
| 18 | #include <utils/Log.h> |
| 19 | |
| 20 | #include <android/looper.h> |
| 21 | #include <utils/PollLoop.h> |
| 22 | |
| 23 | using android::PollLoop; |
| 24 | using android::sp; |
| 25 | |
| 26 | ALooper* ALooper_forThread() { |
| 27 | return PollLoop::getForThread().get(); |
| 28 | } |
| 29 | |
Dianne Hackborn | 85448bb | 2010-07-07 14:27:31 -0700 | [diff] [blame] | 30 | ALooper* ALooper_prepare(int32_t opts) { |
| 31 | bool allowFds = (opts&ALOOPER_PREPARE_ALLOW_NON_CALLBACKS) != 0; |
Dianne Hackborn | 6826741 | 2010-07-02 18:52:01 -0700 | [diff] [blame] | 32 | sp<PollLoop> loop = PollLoop::getForThread(); |
| 33 | if (loop == NULL) { |
Dianne Hackborn | 85448bb | 2010-07-07 14:27:31 -0700 | [diff] [blame] | 34 | loop = new PollLoop(allowFds); |
Dianne Hackborn | 6826741 | 2010-07-02 18:52:01 -0700 | [diff] [blame] | 35 | PollLoop::setForThread(loop); |
| 36 | } |
Dianne Hackborn | 85448bb | 2010-07-07 14:27:31 -0700 | [diff] [blame] | 37 | if (loop->getAllowNonCallbacks() != allowFds) { |
| 38 | LOGW("ALooper_prepare again with different ALOOPER_PREPARE_ALLOW_NON_CALLBACKS"); |
| 39 | } |
Dianne Hackborn | 6826741 | 2010-07-02 18:52:01 -0700 | [diff] [blame] | 40 | return loop.get(); |
| 41 | } |
| 42 | |
Dianne Hackborn | 85448bb | 2010-07-07 14:27:31 -0700 | [diff] [blame] | 43 | int32_t ALooper_pollOnce(int timeoutMillis, int* outEvents, void** outData) { |
Dianne Hackborn | 6826741 | 2010-07-02 18:52:01 -0700 | [diff] [blame] | 44 | sp<PollLoop> loop = PollLoop::getForThread(); |
| 45 | if (loop == NULL) { |
| 46 | LOGW("ALooper_pollOnce: No looper for this thread!"); |
| 47 | return -1; |
| 48 | } |
Dianne Hackborn | 85448bb | 2010-07-07 14:27:31 -0700 | [diff] [blame] | 49 | return loop->pollOnce(timeoutMillis, outEvents, outData); |
| 50 | } |
| 51 | |
| 52 | int32_t ALooper_pollAll(int timeoutMillis, int* outEvents, void** outData) { |
| 53 | sp<PollLoop> loop = PollLoop::getForThread(); |
| 54 | if (loop == NULL) { |
| 55 | LOGW("ALooper_pollOnce: No looper for this thread!"); |
| 56 | return -1; |
| 57 | } |
| 58 | |
| 59 | int32_t result; |
| 60 | while ((result = loop->pollOnce(timeoutMillis, outEvents, outData)) == ALOOPER_POLL_CALLBACK) { |
| 61 | ; |
| 62 | } |
| 63 | |
| 64 | return result; |
Dianne Hackborn | 6826741 | 2010-07-02 18:52:01 -0700 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | void ALooper_acquire(ALooper* looper) { |
| 68 | static_cast<PollLoop*>(looper)->incStrong((void*)ALooper_acquire); |
| 69 | } |
| 70 | |
| 71 | void ALooper_release(ALooper* looper) { |
| 72 | static_cast<PollLoop*>(looper)->decStrong((void*)ALooper_acquire); |
| 73 | } |
| 74 | |
Dianne Hackborn | 85448bb | 2010-07-07 14:27:31 -0700 | [diff] [blame] | 75 | void ALooper_addFd(ALooper* looper, int fd, int events, |
Dianne Hackborn | 6826741 | 2010-07-02 18:52:01 -0700 | [diff] [blame] | 76 | ALooper_callbackFunc* callback, void* data) { |
| 77 | static_cast<PollLoop*>(looper)->setLooperCallback(fd, events, callback, data); |
| 78 | } |
| 79 | |
Dianne Hackborn | 85448bb | 2010-07-07 14:27:31 -0700 | [diff] [blame] | 80 | int32_t ALooper_removeFd(ALooper* looper, int fd) { |
Dianne Hackborn | 6826741 | 2010-07-02 18:52:01 -0700 | [diff] [blame] | 81 | return static_cast<PollLoop*>(looper)->removeCallback(fd) ? 1 : 0; |
| 82 | } |