blob: 1564c477d29e722336b29e9c595df6a251cb6fa2 [file] [log] [blame]
Dianne Hackborn68267412010-07-02 18:52:01 -07001/*
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
23using android::PollLoop;
24using android::sp;
25
26ALooper* ALooper_forThread() {
27 return PollLoop::getForThread().get();
28}
29
Dianne Hackborn85448bb2010-07-07 14:27:31 -070030ALooper* ALooper_prepare(int32_t opts) {
31 bool allowFds = (opts&ALOOPER_PREPARE_ALLOW_NON_CALLBACKS) != 0;
Dianne Hackborn68267412010-07-02 18:52:01 -070032 sp<PollLoop> loop = PollLoop::getForThread();
33 if (loop == NULL) {
Dianne Hackborn85448bb2010-07-07 14:27:31 -070034 loop = new PollLoop(allowFds);
Dianne Hackborn68267412010-07-02 18:52:01 -070035 PollLoop::setForThread(loop);
36 }
Dianne Hackborn85448bb2010-07-07 14:27:31 -070037 if (loop->getAllowNonCallbacks() != allowFds) {
38 LOGW("ALooper_prepare again with different ALOOPER_PREPARE_ALLOW_NON_CALLBACKS");
39 }
Dianne Hackborn68267412010-07-02 18:52:01 -070040 return loop.get();
41}
42
Dianne Hackborn85448bb2010-07-07 14:27:31 -070043int32_t ALooper_pollOnce(int timeoutMillis, int* outEvents, void** outData) {
Dianne Hackborn68267412010-07-02 18:52:01 -070044 sp<PollLoop> loop = PollLoop::getForThread();
45 if (loop == NULL) {
46 LOGW("ALooper_pollOnce: No looper for this thread!");
47 return -1;
48 }
Dianne Hackborn85448bb2010-07-07 14:27:31 -070049 return loop->pollOnce(timeoutMillis, outEvents, outData);
50}
51
52int32_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 Hackborn68267412010-07-02 18:52:01 -070065}
66
67void ALooper_acquire(ALooper* looper) {
68 static_cast<PollLoop*>(looper)->incStrong((void*)ALooper_acquire);
69}
70
71void ALooper_release(ALooper* looper) {
72 static_cast<PollLoop*>(looper)->decStrong((void*)ALooper_acquire);
73}
74
Dianne Hackborn85448bb2010-07-07 14:27:31 -070075void ALooper_addFd(ALooper* looper, int fd, int events,
Dianne Hackborn68267412010-07-02 18:52:01 -070076 ALooper_callbackFunc* callback, void* data) {
77 static_cast<PollLoop*>(looper)->setLooperCallback(fd, events, callback, data);
78}
79
Dianne Hackborn85448bb2010-07-07 14:27:31 -070080int32_t ALooper_removeFd(ALooper* looper, int fd) {
Dianne Hackborn68267412010-07-02 18:52:01 -070081 return static_cast<PollLoop*>(looper)->removeCallback(fd) ? 1 : 0;
82}