summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJuha Alanen <juha.alanen@mapbox.com>2020-01-20 16:34:15 +0200
committerJuha Alanen <juha.alanen@mapbox.com>2020-02-06 17:52:12 +0200
commitce5b7ad1138f5095f5dbf1fb67586c0f5ba9dc47 (patch)
treee13194abacc69f06fbe1cafcf2fa3381b729a547
parent38d80dd3e1adbef8c4d70ed7ae827d215cf14231 (diff)
downloadqtlocation-mapboxgl-ce5b7ad1138f5095f5dbf1fb67586c0f5ba9dc47.tar.gz
[android] Implement watch handler in RunLoop
-rw-r--r--platform/android/src/run_loop.cpp35
-rw-r--r--platform/android/src/run_loop_impl.hpp5
2 files changed, 36 insertions, 4 deletions
diff --git a/platform/android/src/run_loop.cpp b/platform/android/src/run_loop.cpp
index d62816d8cd..c344353fc9 100644
--- a/platform/android/src/run_loop.cpp
+++ b/platform/android/src/run_loop.cpp
@@ -55,6 +55,17 @@ int looperCallbackDefault(int fd, int, void* data) {
return 1;
}
+int looperCallbackReadEvent(int fd, int, void* data) {
+ auto runLoopImpl = reinterpret_cast<RunLoop::Impl*>(data);
+
+ auto it = runLoopImpl->readPoll.find(fd);
+ if (it != runLoopImpl->readPoll.end()) {
+ assert(it->second);
+ it->second(fd, RunLoop::Event::Read);
+ }
+ return 1;
+}
+
} // namespace
namespace mbgl {
@@ -243,12 +254,28 @@ void RunLoop::stop() {
});
}
-void RunLoop::addWatch(int, Event, std::function<void(int, Event)>&&) {
- throw std::runtime_error("Not implemented.");
+void RunLoop::addWatch(int fd, Event event, std::function<void(int, Event)>&& cb) {
+ MBGL_VERIFY_THREAD(tid);
+
+ if (event == Event::Read) {
+ impl->readPoll[fd] = std::move(cb);
+
+ ALooper* looper = ALooper_forThread();
+ ALooper_addFd(
+ looper, fd, ALOOPER_POLL_CALLBACK, ALOOPER_EVENT_INPUT, looperCallbackReadEvent, this->impl.get());
+ }
}
-void RunLoop::removeWatch(int) {
- throw std::runtime_error("Not implemented.");
+void RunLoop::removeWatch(int fd) {
+ MBGL_VERIFY_THREAD(tid);
+
+ auto it = impl->readPoll.find(fd);
+ if (it != impl->readPoll.end()) {
+ impl->readPoll.erase(it);
+
+ ALooper* looper = ALooper_forThread();
+ ALooper_removeFd(looper, fd);
+ }
}
} // namespace util
diff --git a/platform/android/src/run_loop_impl.hpp b/platform/android/src/run_loop_impl.hpp
index c6a1b23a7b..8f0a07f8ba 100644
--- a/platform/android/src/run_loop_impl.hpp
+++ b/platform/android/src/run_loop_impl.hpp
@@ -9,12 +9,15 @@
#include <list>
#include <memory>
#include <mutex>
+#include <unordered_map>
struct ALooper;
namespace mbgl {
namespace util {
+using WatchCallback = std::function<void(int, RunLoop::Event)>;
+
template <typename T> class Thread;
class Alarm;
@@ -43,6 +46,8 @@ public:
std::atomic<bool> running;
std::atomic_flag coalesce = ATOMIC_FLAG_INIT;
+ std::unordered_map<int, WatchCallback> readPoll;
+
private:
friend RunLoop;