1- diff --git a/CMakeLists.txt b/CMakeLists.txt
2- index aa90bad4..b4641520 100644
3- --- a/CMakeLists.txt
4- +++ b/CMakeLists.txt
5- @@ -70,7 +70,7 @@ if (NOT TARGET Systemd::Systemd)
6- pkg_get_variable(DBUS_SYSTEM_BUS_DEFAULT_ADDRESS "dbus-1" "system_bus_default_address")
7- endif()
8-
9- - if (NOT LIBUV_TARGET)
10- + if (NOT LIBUV_TARGET AND NOT EMSCRIPTEN)
11- if (NOT (TARGET PkgConfig::LibUV))
12- pkg_check_modules(LibUV REQUIRED IMPORTED_TARGET "libuv")
13- endif()
141diff --git a/src/lib/fcitx-config/CMakeLists.txt b/src/lib/fcitx-config/CMakeLists.txt
152index 85c9865b..3a995ca1 100644
163--- a/src/lib/fcitx-config/CMakeLists.txt
@@ -26,32 +13,10 @@ index 85c9865b..3a995ca1 100644
2613 )
2714 target_include_directories(Fcitx5Config PUBLIC
2815diff --git a/src/lib/fcitx-utils/CMakeLists.txt b/src/lib/fcitx-utils/CMakeLists.txt
29- index dd67e07d..21680660 100644
16+ index 4fe67c8d..f5a8e66b 100644
3017--- a/src/lib/fcitx-utils/CMakeLists.txt
3118+++ b/src/lib/fcitx-utils/CMakeLists.txt
32- @@ -27,14 +27,18 @@ if (ENABLE_DBUS)
33- endif()
34- endif()
35-
36- - if (NOT TARGET Systemd::Systemd)
37- + if (TARGET Systemd::Systemd)
38- set(FCITX_UTILS_SOURCES
39- ${FCITX_UTILS_SOURCES}
40- - event_libuv.cpp)
41- + event_sdevent.cpp)
42- + elseif (EMSCRIPTEN)
43- + set(FCITX_UTILS_SOURCES
44- + ${FCITX_UTILS_SOURCES}
45- + event_js.cpp)
46- else()
47- set(FCITX_UTILS_SOURCES
48- ${FCITX_UTILS_SOURCES}
49- - event_sdevent.cpp)
50- + event_libuv.cpp)
51- endif()
52-
53- set(FCITX_UTILS_SOURCES
54- @@ -121,8 +125,7 @@ ecm_setup_version(PROJECT
19+ @@ -121,8 +121,7 @@ ecm_setup_version(PROJECT
5520
5621 add_library(Fcitx5Utils SHARED ${FCITX_UTILS_SOURCES})
5722 set_target_properties(Fcitx5Utils
@@ -61,160 +26,6 @@ index dd67e07d..21680660 100644
6126 EXPORT_NAME Utils
6227 )
6328 target_include_directories(Fcitx5Utils PUBLIC
64- @@ -135,7 +138,7 @@ if(LIBKVM_FOUND)
65- endif()
66-
67- if (NOT TARGET Systemd::Systemd)
68- - target_link_libraries(Fcitx5Utils PRIVATE ${LIBUV_TARGET})
69- + # target_link_libraries(Fcitx5Utils PRIVATE ${LIBUV_TARGET})
70- if (ENABLE_DBUS)
71- target_link_libraries(Fcitx5Utils PRIVATE PkgConfig::DBus)
72- endif()
73- diff --git a/src/lib/fcitx-utils/event_js.cpp b/src/lib/fcitx-utils/event_js.cpp
74- new file mode 100644
75- index 00000000..68f7ee7f
76- --- /dev/null
77- +++ b/src/lib/fcitx-utils/event_js.cpp
78- @@ -0,0 +1,139 @@
79- + #include <cassert>
80- + #include <emscripten.h>
81- + #include "event.h"
82- + #include "log.h"
83- +
84- + namespace fcitx {
85- +
86- + template <typename Interface>
87- + struct JSEventSourceBase : public Interface {
88- + public:
89- + ~JSEventSourceBase() override {}
90- +
91- + bool isEnabled() const override { return enabled_; }
92- +
93- + void setEnabled(bool enabled) override { enabled_ = enabled; }
94- +
95- + bool isOneShot() const override { return oneShot_; }
96- +
97- + void setOneShot() override { oneShot_ = true; }
98- +
99- + private:
100- + bool enabled_ = false;
101- + bool oneShot_ = false;
102- + };
103- +
104- + struct JSEventSource : public JSEventSourceBase<EventSource> {
105- + JSEventSource(EventCallback _callback)
106- + : callback_(std::make_shared<EventCallback>(std::move(_callback))) {}
107- +
108- + std::shared_ptr<EventCallback> callback_;
109- + };
110- +
111- + struct JSEventSourceIO : public JSEventSourceBase<EventSourceIO> {
112- + JSEventSourceIO(IOCallback _callback) {}
113- +
114- + int fd() const override { return 0; }
115- +
116- + void setFd(int fd) override {}
117- +
118- + IOEventFlags events() const override { return IOEventFlag::In; }
119- +
120- + void setEvents(IOEventFlags flags) override {}
121- +
122- + IOEventFlags revents() const override { return IOEventFlag::In; }
123- + };
124- +
125- + void TimeEventCallback(void *arg);
126- +
127- + struct JSEventSourceTime : public JSEventSourceBase<EventSourceTime> {
128- + JSEventSourceTime(TimeCallback _callback, uint64_t time, clockid_t clockid)
129- + : callback_(std::make_shared<TimeCallback>(std::move(_callback))),
130- + time_(time), clockid_(clockid) {
131- + assert(clockid == CLOCK_MONOTONIC);
132- + setOneShot();
133- + }
134- +
135- + void setOneShot() override {
136- + int t = std::max<int64_t>(0, time_ - now(CLOCK_MONOTONIC)) / 1000;
137- + emscripten_async_call(TimeEventCallback, this, t);
138- + }
139- +
140- + uint64_t time() const override { return time_; }
141- +
142- + void setTime(uint64_t time) override { time_ = time; }
143- +
144- + uint64_t accuracy() const override { return 0; }
145- +
146- + void setAccuracy(uint64_t time) override {}
147- +
148- + clockid_t clock() const override { return clockid_; }
149- +
150- + std::shared_ptr<TimeCallback> callback_;
151- +
152- + private:
153- + uint64_t time_;
154- + clockid_t clockid_;
155- + };
156- +
157- + void TimeEventCallback(void *arg) {
158- + auto source = static_cast<JSEventSourceTime *>(arg);
159- + (*source->callback_)(source, source->time());
160- + }
161- +
162- + class EventLoopPrivate {
163- + public:
164- + EventLoopPrivate() {}
165- +
166- + ~EventLoopPrivate() {}
167- + };
168- +
169- + EventLoop::EventLoop() : d_ptr(std::make_unique<EventLoopPrivate>()) {}
170- +
171- + EventLoop::~EventLoop() = default;
172- +
173- + const char *EventLoop::impl() { return "js-event"; }
174- +
175- + void *EventLoop::nativeHandle() { return nullptr; }
176- +
177- + bool EventLoop::exec() { return true; }
178- +
179- + void EventLoop::exit() {}
180- +
181- + std::unique_ptr<EventSourceIO> EventLoop::addIOEvent(int fd, IOEventFlags flags,
182- + IOCallback callback) {
183- + FCITX_D();
184- + auto source = std::make_unique<JSEventSourceIO>(std::move(callback));
185- + return source;
186- + }
187- +
188- + std::unique_ptr<EventSourceTime>
189- + EventLoop::addTimeEvent(clockid_t clock, uint64_t usec, uint64_t accuracy,
190- + TimeCallback callback) {
191- + auto source =
192- + std::make_unique<JSEventSourceTime>(std::move(callback), usec, clock);
193- + return source;
194- + }
195- +
196- + std::unique_ptr<EventSource> EventLoop::addExitEvent(EventCallback callback) {
197- + FCITX_D();
198- + auto source = std::make_unique<JSEventSource>(std::move(callback));
199- + return source;
200- + }
201- +
202- + std::unique_ptr<EventSource> EventLoop::addDeferEvent(EventCallback callback) {
203- + return addTimeEvent(
204- + CLOCK_MONOTONIC, now(CLOCK_MONOTONIC), 0,
205- + [callback = std::move(callback)](EventSourceTime *source, uint64_t) {
206- + return callback(source);
207- + });
208- + }
209- +
210- + std::unique_ptr<EventSource> EventLoop::addPostEvent(EventCallback callback) {
211- + return addTimeEvent(
212- + CLOCK_MONOTONIC, now(CLOCK_MONOTONIC), 0,
213- + [callback = std::move(callback)](EventSourceTime *source, uint64_t) {
214- + return callback(source);
215- + });
216- + }
217- + } // namespace fcitx
21829diff --git a/src/lib/fcitx/CMakeLists.txt b/src/lib/fcitx/CMakeLists.txt
21930index df15dd57..36312bab 100644
22031--- a/src/lib/fcitx/CMakeLists.txt
0 commit comments