Skip to content

Commit 09a4af0

Browse files
authored
move event_js.cpp out of fcitx5 source tree (#14)
1 parent 85dbd58 commit 09a4af0

File tree

8 files changed

+149
-193
lines changed

8 files changed

+149
-193
lines changed

CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ set(XKEYBOARDCONFIG_XKBBASE "${CMAKE_INSTALL_PREFIX}/share/X11/xkb")
4242
set(XKEYBOARDCONFIG_DATADIR "${CMAKE_INSTALL_PREFIX}/share")
4343

4444
set(CMAKE_INSTALL_LIBDATADIR "${CMAKE_INSTALL_PREFIX}/lib")
45+
set(EVENT_LOOP_BACKEND "none" CACHE STRING "")
4546
add_subdirectory(fcitx5)
4647

4748
add_subdirectory(wasmfrontend)

fcitx5-webview

patches/fcitx5.patch

Lines changed: 2 additions & 191 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,3 @@
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()
141
diff --git a/src/lib/fcitx-config/CMakeLists.txt b/src/lib/fcitx-config/CMakeLists.txt
152
index 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
2815
diff --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
21829
diff --git a/src/lib/fcitx/CMakeLists.txt b/src/lib/fcitx/CMakeLists.txt
21930
index df15dd57..36312bab 100644
22031
--- a/src/lib/fcitx/CMakeLists.txt

src/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ add_executable(Fcitx5
44
input_method.cpp
55
config.cpp
66
action.cpp
7+
event_js.cpp
78
)
89

910
target_include_directories(Fcitx5 PRIVATE

src/event_js.cpp

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
#include "event_js.h"
2+
#include <emscripten.h>
3+
#include <fcitx-utils/event_p.h>
4+
#include <fcitx-utils/log.h>
5+
6+
namespace fcitx {
7+
8+
template <typename Interface> struct JSEventSourceBase : public Interface {
9+
public:
10+
~JSEventSourceBase() override {}
11+
12+
bool isEnabled() const override { return enabled_; }
13+
14+
void setEnabled(bool enabled) override { enabled_ = enabled; }
15+
16+
bool isOneShot() const override { return oneShot_; }
17+
18+
void setOneShot() override { oneShot_ = true; }
19+
20+
private:
21+
bool enabled_ = false;
22+
bool oneShot_ = false;
23+
};
24+
25+
struct JSEventSource : public JSEventSourceBase<EventSource> {
26+
JSEventSource(EventCallback _callback)
27+
: callback_(std::make_shared<EventCallback>(std::move(_callback))) {}
28+
29+
std::shared_ptr<EventCallback> callback_;
30+
};
31+
32+
struct JSEventSourceIO : public JSEventSourceBase<EventSourceIO> {
33+
JSEventSourceIO(IOCallback _callback) {}
34+
35+
int fd() const override { return 0; }
36+
37+
void setFd(int fd) override {}
38+
39+
IOEventFlags events() const override { return IOEventFlag::In; }
40+
41+
void setEvents(IOEventFlags flags) override {}
42+
43+
IOEventFlags revents() const override { return IOEventFlag::In; }
44+
};
45+
46+
void TimeEventCallback(void *arg);
47+
48+
struct JSEventSourceTime : public JSEventSourceBase<EventSourceTime> {
49+
JSEventSourceTime(TimeCallback _callback, uint64_t time, clockid_t clockid)
50+
: callback_(std::make_shared<TimeCallback>(std::move(_callback))),
51+
time_(time), clockid_(clockid) {
52+
setOneShot();
53+
}
54+
55+
void setOneShot() override {
56+
int t = std::max<int64_t>(0, time_ - now(CLOCK_MONOTONIC)) / 1000;
57+
emscripten_async_call(TimeEventCallback, this, t);
58+
}
59+
60+
uint64_t time() const override { return time_; }
61+
62+
void setTime(uint64_t time) override { time_ = time; }
63+
64+
uint64_t accuracy() const override { return 0; }
65+
66+
void setAccuracy(uint64_t time) override {}
67+
68+
clockid_t clock() const override { return clockid_; }
69+
70+
std::shared_ptr<TimeCallback> callback_;
71+
72+
private:
73+
uint64_t time_;
74+
clockid_t clockid_;
75+
};
76+
77+
void TimeEventCallback(void *arg) {
78+
auto source = static_cast<JSEventSourceTime *>(arg);
79+
(*source->callback_)(source, source->time());
80+
}
81+
82+
std::unique_ptr<EventSourceIO>
83+
JSEventLoop::addIOEvent(int fd, IOEventFlags flags, IOCallback callback) {
84+
auto source = std::make_unique<JSEventSourceIO>(std::move(callback));
85+
return source;
86+
}
87+
88+
std::unique_ptr<EventSourceTime>
89+
JSEventLoop::addTimeEvent(clockid_t clock, uint64_t usec, uint64_t accuracy,
90+
TimeCallback callback) {
91+
auto source =
92+
std::make_unique<JSEventSourceTime>(std::move(callback), usec, clock);
93+
return source;
94+
}
95+
96+
std::unique_ptr<EventSource> JSEventLoop::addExitEvent(EventCallback callback) {
97+
auto source = std::make_unique<JSEventSource>(std::move(callback));
98+
return source;
99+
}
100+
101+
std::unique_ptr<EventSource>
102+
JSEventLoop::addDeferEvent(EventCallback callback) {
103+
return addTimeEvent(
104+
CLOCK_MONOTONIC, now(CLOCK_MONOTONIC), 0,
105+
[callback = std::move(callback)](EventSourceTime *source, uint64_t) {
106+
return callback(source);
107+
});
108+
}
109+
110+
std::unique_ptr<EventSource> JSEventLoop::addPostEvent(EventCallback callback) {
111+
FCITX_ERROR() << "Not implemented";
112+
return nullptr;
113+
}
114+
115+
} // namespace fcitx

src/event_js.h

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#pragma once
2+
3+
#include <fcitx-utils/eventloopinterface.h>
4+
5+
namespace fcitx {
6+
class JSEventLoop : public EventLoopInterface {
7+
public:
8+
JSEventLoop() = default;
9+
~JSEventLoop() override = default;
10+
bool exec() override { return true; }
11+
void exit() override {}
12+
const char *implementation() const override { return "js"; }
13+
void *nativeHandle() override { return nullptr; }
14+
15+
std::unique_ptr<EventSourceIO> addIOEvent(int fd, IOEventFlags flags,
16+
IOCallback callback) override;
17+
std::unique_ptr<EventSourceTime>
18+
addTimeEvent(clockid_t clock, uint64_t usec, uint64_t accuracy,
19+
TimeCallback callback) override;
20+
std::unique_ptr<EventSource> addExitEvent(EventCallback callback) override;
21+
std::unique_ptr<EventSource> addDeferEvent(EventCallback callback) override;
22+
std::unique_ptr<EventSource> addPostEvent(EventCallback callback) override;
23+
};
24+
} // namespace fcitx

src/fcitx.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
#include "../wasmfrontend/wasmfrontend.h"
22
#include "../webpanel/webpanel.h"
3+
#include "event_js.h"
34
#include "keyboard.h"
45
#include "keycode.h"
56
#include <emscripten.h>
7+
#include <fcitx-utils/event.h>
68
#include <fcitx/instance.h>
79
#include <sys/stat.h>
810

@@ -36,6 +38,8 @@ int main() {
3638
umask(007); // Fix config file's mode
3739
StandardPath::global().syncUmask();
3840
Log::setLogRule("*=5,notimedate");
41+
EventLoop::setEventLoopFactory(
42+
[] { return std::make_unique<JSEventLoop>(); });
3943
instance = std::make_unique<Instance>(0, nullptr);
4044
auto &addonMgr = instance->addonManager();
4145
addonMgr.registerDefaultLoader(&staticAddons);

0 commit comments

Comments
 (0)