| /* | |
| * Copyright (C) 2026 The Android Open Source Project | |
| * | |
| * Licensed under the Apache License, Version 2.0 (the "License"); | |
| * you may not use this file except in compliance with the License. | |
| * You may obtain a copy of the License at | |
| * | |
| * http://www.apache.org/licenses/LICENSE-2.0 | |
| * | |
| * Unless required by applicable law or agreed to in writing, software | |
| * distributed under the License is distributed on an "AS IS" BASIS, | |
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
| * See the License for the specific language governing permissions and | |
| * limitations under the License. | |
| */ | |
| #include "looper_wrapper.h" | |
| #include <utils/Looper.h> | |
| struct RustLooper { | |
| android::sp<android::Looper> looper; | |
| }; | |
| extern "C" { | |
| RustLooper* RustLooper_getForThread() { | |
| android::sp<android::Looper> looper = android::Looper::getForThread(); | |
| if (looper != nullptr) { | |
| return new RustLooper{looper}; | |
| } | |
| return nullptr; | |
| } | |
| RustLooper* RustLooper_fromRaw(void* ptr) { | |
| if (!ptr) return nullptr; | |
| return new RustLooper{static_cast<android::Looper*>(ptr)}; | |
| } | |
| RustLooper* RustLooper_clone(RustLooper* looper) { | |
| if (!looper) return nullptr; | |
| // Copy constructor of sp increments the ref count correctly | |
| return new RustLooper{looper->looper}; | |
| } | |
| void RustLooper_free(RustLooper* looper) { | |
| // deleting the struct calls ~RustLooper() which drops the sp<Looper> safely | |
| delete looper; | |
| } | |
| bool RustLooper_isCurrentThread(RustLooper* looper) { | |
| if (!looper) return false; | |
| android::sp<android::Looper> current = android::Looper::getForThread(); | |
| return current == looper->looper; | |
| } | |
| int RustLooper_addFd(RustLooper* looper, int fd, int ident, int events, | |
| int (*callback)(int, int, void*), void* data) { | |
| if (!looper || !looper->looper) return -1; | |
| return looper->looper->addFd(fd, ident, events, callback, data); | |
| } | |
| int RustLooper_removeFd(RustLooper* looper, int fd) { | |
| if (!looper || !looper->looper) return -1; | |
| return looper->looper->removeFd(fd); | |
| } | |
| } |